home *** CD-ROM | disk | FTP | other *** search
/ linuxmafia.com 2016 / linuxmafia.com.tar / linuxmafia.com / pub / palmos / pippy-0.6beta-src.tar.gz / pippy-0.6beta-src.tar / pippy-0.6beta-src / src / Modules / palmsysmodule.c < prev    next >
C/C++ Source or Header  |  2001-02-14  |  5KB  |  218 lines

  1. /*
  2.  
  3.   palmsys module
  4.  
  5.   This module provides an interface to various PalmOS system functions.
  6.  
  7. */
  8.  
  9. #include "Python.h"
  10. #include <PalmOS.h>
  11.  
  12. static PyObject *palmsys_error;
  13.  
  14. static PyObject *palmsys_getstackinfo(PyObject *self, PyObject *args) SEG_PALMSYSMODULE_C;
  15. static PyObject *palmsys_reset(PyObject *self, PyObject *args) SEG_PALMSYSMODULE_C;
  16. static PyObject *palmsys_ticksPerSecond(PyObject *self, PyObject *args) SEG_PALMSYSMODULE_C;
  17. static PyObject *palmsys_batteryinfo(PyObject *self, PyObject *args) SEG_PALMSYSMODULE_C;
  18. static Int32 get_heapid(PyObject *args) SEG_PALMSYSMODULE_C;
  19. static PyObject *palmsys_heapcompact(PyObject *self, PyObject *args) SEG_PALMSYSMODULE_C;
  20. static PyObject *palmsys_heapfreebytes(PyObject *self, PyObject *args) SEG_PALMSYSMODULE_C;
  21. static PyObject *palmsys_heapid(PyObject *self, PyObject *args) SEG_PALMSYSMODULE_C;
  22. void initpalmsys(void) SEG_PALMSYSMODULE_C;
  23.  
  24.  
  25. static PyObject *
  26. palmsys_getstackinfo(PyObject *self, PyObject *args)
  27. {
  28.     Boolean stack_not_overflow;
  29.     MemPtr start, end;
  30.     PyObject *res = NULL;
  31.  
  32.     if (!PyArg_NoArgs(args))
  33.         return NULL;
  34.  
  35.     stack_not_overflow = SysGetStackInfo (&start, &end);
  36.  
  37.     if (!(res = Py_BuildValue("(ill)", stack_not_overflow, start, end)))
  38.         return NULL;
  39.  
  40.     return res;
  41. }
  42.  
  43. static PyObject *
  44. palmsys_reset(PyObject *self, PyObject *args)
  45. {
  46.     /* perform a soft reset of the device */
  47.     if (!PyArg_NoArgs(args))
  48.         return NULL;
  49.  
  50.     SysReset ();
  51.  
  52.     /* will never get here, so just add these lines to please
  53.        the compiler */
  54.     Py_INCREF(Py_None);
  55.     return Py_None;
  56. }
  57.  
  58. static PyObject *
  59. palmsys_ticksPerSecond(PyObject *self, PyObject *args)
  60. {
  61.     if (!PyArg_NoArgs(args))
  62.         return NULL;
  63.  
  64.     return PyInt_FromLong((long)SysTicksPerSecond());
  65. }
  66.  
  67. /* batteryinfo returns a tuple describing the state of the battery. */
  68.  
  69. static PyObject *
  70. palmsys_batteryinfo(PyObject *self, PyObject *args)
  71. {
  72.     UInt16 volts_100;
  73.     UInt16 warnThresh, criticalThresh, maxTicks;
  74.     SysBatteryKind kind;
  75.     Boolean pluggedIn;
  76.     UInt8 percent;
  77.     char *s;
  78.         
  79.     /* perform a soft reset of the device */
  80.     if (!PyArg_NoArgs(args))
  81.         return NULL;
  82.  
  83.     volts_100 = SysBatteryInfo (0, &warnThresh, &criticalThresh, 
  84.                     &maxTicks, &kind, &pluggedIn, &percent);
  85.     
  86.     switch(kind) {
  87.     case sysBatteryKindAlkaline:
  88.         s = "Alkaline";
  89.         break;
  90.     case sysBatteryKindNiCad:
  91.         s = "NiCad";
  92.         break;
  93.     case sysBatteryKindLiIon:
  94.         s = "LiIon";
  95.         break;
  96.     case sysBatteryKindRechAlk:
  97.         s = "RechAlk";
  98.         break;
  99.     case sysBatteryKindNiMH:
  100.         s = "NiMH";
  101.         break;
  102.     case sysBatteryKindLiIon1400:
  103.         s = "LiIon1400";
  104.         break;
  105.     default:
  106.         s = "Unknown";
  107.     }
  108.  
  109.     return Py_BuildValue("(iiiisii)", volts_100, warnThresh, criticalThresh,
  110.                  maxTicks, s, (UInt16) pluggedIn, 
  111.                  (UInt16) percent);
  112. }
  113.  
  114. static Int32
  115. get_heapid(PyObject *args)
  116. {
  117.     UInt16 card, heap_index;
  118.     Int32 res = -1; /* really want UInt16, but need to return a
  119.                negative result if an error occurs */
  120.  
  121.     if (!PyArg_ParseTuple(args, "ii", &card, &heap_index))
  122.         return res;
  123.     if (card != 0){
  124.         PyErr_SetString(palmsys_error, "card must be 0");
  125.         return res;
  126.     }
  127.     if (heap_index < 0 || heap_index > MemNumHeaps(card)-1) {
  128.         PyErr_SetString(palmsys_error, "heap index is out of range");
  129.         return res;
  130.     }
  131.  
  132.     return MemHeapID (card, heap_index);
  133. }
  134.  
  135.  
  136. static PyObject *
  137. palmsys_heapcompact(PyObject *self, PyObject *args)
  138. {
  139.     Int32 id;
  140.  
  141.     if ((id=get_heapid(args)) < 0)
  142.         return NULL;
  143.  
  144.     /* always returns 0 */
  145.     MemHeapCompact((UInt16)id);
  146.  
  147.     Py_INCREF(Py_None);
  148.     return Py_None;
  149. }
  150.  
  151.  
  152.     
  153. static PyObject *
  154. palmsys_heapfreebytes(PyObject *self, PyObject *args)
  155. {
  156.     Int32 id;
  157.     UInt32 amt_free, max_contig_free;
  158.  
  159.     if ((id=get_heapid(args)) < 0)
  160.         return NULL;
  161.  
  162.     /* always returns 0 */
  163.     MemHeapFreeBytes ((UInt16)id, &amt_free, &max_contig_free);
  164.  
  165.     return Py_BuildValue("(ll)", amt_free, max_contig_free);
  166. }
  167.  
  168. static PyObject *
  169. palmsys_heapid(PyObject *self, PyObject *args)
  170. {
  171.     Int32 res;
  172.     if ((res=get_heapid(args)) < 0)
  173.         return NULL;
  174.     return PyInt_FromLong((UInt32)res);
  175. }
  176.  
  177. static PyObject *
  178. palmsys_displayError(PyObject *self, PyObject *args)
  179. {
  180.     char *msg;
  181.     int cond;
  182.  
  183.     if (!PyArg_ParseTuple(args, "is", &cond, &msg))
  184.         return NULL;
  185.     
  186.     ErrFatalDisplayIf(cond, msg);
  187.  
  188.     Py_INCREF(Py_None);
  189.     return Py_None;
  190. }    
  191.  
  192. static PyMethodDef palmsys_methods[] = {
  193.     {"getstackinfo",  (PyCFunction)palmsys_getstackinfo, 0},
  194.     {"reset",  (PyCFunction)palmsys_reset, 0},
  195.     {"ticksPerSecond",  (PyCFunction)palmsys_ticksPerSecond, 0},
  196.     {"batteryinfo",  (PyCFunction)palmsys_batteryinfo, 0},
  197.     {"heapcompact",  (PyCFunction)palmsys_heapcompact, METH_VARARGS},
  198.     {"heapfreebytes",  (PyCFunction)palmsys_heapfreebytes, METH_VARARGS},
  199.     {"heapid",  (PyCFunction)palmsys_heapid, METH_VARARGS},
  200.     {"displayError",  (PyCFunction)    palmsys_displayError, METH_VARARGS},
  201.     {NULL, NULL} };
  202.  
  203.  
  204.  
  205.  
  206. void initpalmsys(void) {
  207.     PyObject *m, *d;
  208.  
  209.     m = Py_InitModule3("palmsys", palmsys_methods, NULL);
  210.     d = PyModule_GetDict(m);
  211.     palmsys_error = PyErr_NewException("palmsys.error", NULL, NULL);
  212.     if (palmsys_error == NULL)
  213.         return;
  214.  
  215.     PyDict_SetItemString(d, "error", palmsys_error);
  216.  
  217. }
  218.