home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Applications / Python 1.3 / source code / Mac / macosmodule.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-17  |  6.5 KB  |  261 lines  |  [TEXT/R*ch]

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI not be used in advertising or publicity pertaining to
  13. distribution of the software without specific, written prior permission.
  14.  
  15. STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  16. THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  18. FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  20. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  21. OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  
  23. ******************************************************************/
  24.  
  25. /* Macintosh OS-specific interface */
  26.  
  27. #include "Python.h"
  28. #include "macglue.h"
  29.  
  30. #include <Windows.h>
  31.  
  32. static PyObject *MacOS_Error; /* Exception MacOS.Error */
  33.  
  34. #ifdef MPW
  35. #define bufferIsSmall -607    /*error returns from Post and Accept */
  36. #endif
  37.  
  38.  
  39. /*----------------------------------------------------------------------*/
  40. /* Miscellaneous File System Operations */
  41.  
  42. static PyObject *
  43. MacOS_GetCreatorAndType(PyObject *self, PyObject *args)
  44. {
  45.     Str255 name;
  46.     FInfo info;
  47.     PyObject *creator, *type, *res;
  48.     OSErr err;
  49.     
  50.     if (!PyArg_ParseTuple(args, "O&", PyMac_GetStr255, &name))
  51.         return NULL;
  52.     if ((err = GetFInfo(name, 0, &info)) != noErr)
  53.         return PyErr_Mac(MacOS_Error, err);
  54.     creator = PyString_FromStringAndSize((char *)&info.fdCreator, 4);
  55.     type = PyString_FromStringAndSize((char *)&info.fdType, 4);
  56.     res = Py_BuildValue("OO", creator, type);
  57.     Py_DECREF(creator);
  58.     Py_DECREF(type);
  59.     return res;
  60. }
  61.  
  62. static PyObject *
  63. MacOS_SetCreatorAndType(PyObject *self, PyObject *args)
  64. {
  65.     Str255 name;
  66.     ResType creator, type;
  67.     FInfo info;
  68.     OSErr err;
  69.     
  70.     if (!PyArg_ParseTuple(args, "O&O&O&",
  71.             PyMac_GetStr255, &name, PyMac_GetOSType, &creator, PyMac_GetOSType, &type))
  72.         return NULL;
  73.     if ((err = GetFInfo(name, 0, &info)) != noErr)
  74.         return PyErr_Mac(MacOS_Error, err);
  75.     info.fdCreator = creator;
  76.     info.fdType = type;
  77.     if ((err = SetFInfo(name, 0, &info)) != noErr)
  78.         return PyErr_Mac(MacOS_Error, err);
  79.     Py_INCREF(Py_None);
  80.     return Py_None;
  81. }
  82.  
  83. /*----------------------------------------------------------------------*/
  84. /* STDWIN High Level Event interface */
  85.  
  86. #include <EPPC.h>
  87. #include <Events.h>
  88.  
  89. #ifdef USE_STDWIN
  90.  
  91. extern void (*_w_high_level_event_proc)(EventRecord *);
  92.  
  93. static PyObject *MacOS_HighLevelEventHandler = NULL;
  94.  
  95. static void
  96. MacOS_HighLevelEventProc(EventRecord *e)
  97. {
  98.     if (MacOS_HighLevelEventHandler != NULL) {
  99.         PyObject *args = PyMac_BuildEventRecord(e);
  100.         PyObject *res;
  101.         if (args == NULL)
  102.             res = NULL;
  103.         else {
  104.             res = PyEval_CallObject(MacOS_HighLevelEventHandler, args);
  105.             Py_DECREF(args);
  106.         }
  107.         if (res == NULL) {
  108.             fprintf(stderr, "Exception in MacOS_HighLevelEventProc:\n");
  109.             PyErr_Print();
  110.         }
  111.         else
  112.             Py_DECREF(res);
  113.     }
  114. }
  115.  
  116. /* XXXX Need to come here from PyMac_DoYield too... */
  117.  
  118. static PyObject *
  119. MacOS_SetHighLevelEventHandler(self, args)
  120.     PyObject *self;
  121.     PyObject *args;
  122. {
  123.     PyObject *previous = MacOS_HighLevelEventHandler;
  124.     PyObject *next = NULL;
  125.     if (!PyArg_ParseTuple(args, "|O", &next))
  126.         return NULL;
  127.     if (next == Py_None)
  128.         next = NULL;
  129.     Py_INCREF(next);
  130.     MacOS_HighLevelEventHandler = next;
  131.     if (next == NULL)
  132.         _w_high_level_event_proc = NULL;
  133.     else
  134.         _w_high_level_event_proc = MacOS_HighLevelEventProc;
  135.     if (previous == NULL) {
  136.         Py_INCREF(Py_None);
  137.         previous = Py_None;
  138.     }
  139.     return previous;
  140. }
  141.  
  142. #endif /* USE_STDWIN */
  143.  
  144. static PyObject *
  145. MacOS_AcceptHighLevelEvent(self, args)
  146.     PyObject *self;
  147.     PyObject *args;
  148. {
  149.     TargetID sender;
  150.     unsigned long refcon;
  151.     Ptr buf;
  152.     unsigned long len;
  153.     OSErr err;
  154.     PyObject *res;
  155.     
  156.     buf = NULL;
  157.     len = 0;
  158.     err = AcceptHighLevelEvent(&sender, &refcon, buf, &len);
  159.     if (err == bufferIsSmall) {
  160.         buf = malloc(len);
  161.         if (buf == NULL)
  162.             return PyErr_NoMemory();
  163.         err = AcceptHighLevelEvent(&sender, &refcon, buf, &len);
  164.         if (err != noErr) {
  165.             free(buf);
  166.             return PyErr_Mac(MacOS_Error, (int)err);
  167.         }
  168.     }
  169.     else if (err != noErr)
  170.         return PyErr_Mac(MacOS_Error, (int)err);
  171.     res = Py_BuildValue("s#ls#",
  172.         (char *)&sender, (int)(sizeof sender), refcon, (char *)buf, (int)len);
  173.     free(buf);
  174.     return res;
  175. }
  176.  
  177. /*
  178. ** Set poll frequency and cpu-yield-time
  179. */
  180. static PyObject *
  181. MacOS_SetScheduleTimes(PyObject *self, PyObject *args)
  182. {
  183.     long fgi, fgy, bgi, bgy;
  184.     
  185.     bgi = bgy = -2;    
  186.     if (!PyArg_ParseTuple(args, "ll|ll", &fgi, &fgy, &bgi, &bgy))
  187.         return NULL;
  188.     if ( bgi == -2 || bgy == -2 ) {
  189.         bgi = fgi;
  190.         bgy = fgy;
  191.     }
  192.     PyMac_SetYield(fgi, fgy, bgi, bgy);
  193.     Py_INCREF(Py_None);
  194.     return Py_None;
  195. }
  196.  
  197. static PyObject *
  198. MacOS_EnableAppswitch(PyObject *self, PyObject *args)
  199. {
  200.     int old, new;
  201.     
  202.     if (!PyArg_ParseTuple(args, "i", &new))
  203.         return NULL;
  204.     old = PyMac_DoYieldEnabled;
  205.     PyMac_DoYieldEnabled = new;
  206.     return Py_BuildValue("i", old);
  207. }
  208.  
  209.  
  210. static PyObject *
  211. MacOS_HandleEvent(PyObject *self, PyObject *args)
  212. {
  213.     EventRecord ev;
  214.     
  215.     if (!PyArg_ParseTuple(args, "O&", PyMac_GetEventRecord, &ev))
  216.         return NULL;
  217.     PyMac_HandleEvent(&ev);
  218.     Py_INCREF(Py_None);
  219.     return Py_None;
  220. }
  221.  
  222. static PyObject *
  223. MacOS_GetErrorString(PyObject *self, PyObject *args)
  224. {
  225.     int errn;
  226.     
  227.     if (!PyArg_ParseTuple(args, "i", &errn))
  228.         return NULL;
  229.     return Py_BuildValue("s", PyMac_StrError(errn));
  230. }
  231.  
  232. static PyMethodDef MacOS_Methods[] = {
  233.     {"AcceptHighLevelEvent",    MacOS_AcceptHighLevelEvent, 1},
  234.     {"GetCreatorAndType",        MacOS_GetCreatorAndType, 1},
  235.     {"SetCreatorAndType",        MacOS_SetCreatorAndType, 1},
  236. #ifdef USE_STDWIN
  237.     {"SetHighLevelEventHandler",    MacOS_SetHighLevelEventHandler, 1},
  238. #endif
  239.     {"SetScheduleTimes",    MacOS_SetScheduleTimes, 1},
  240.     {"EnableAppswitch",        MacOS_EnableAppswitch, 1},
  241.     {"HandleEvent",            MacOS_HandleEvent, 1},
  242.     {"GetErrorString",        MacOS_GetErrorString, 1},
  243.     {NULL,                NULL}         /* Sentinel */
  244. };
  245.  
  246.  
  247. void
  248. MacOS_Init()
  249. {
  250.     PyObject *m, *d;
  251.     
  252.     m = Py_InitModule("MacOS", MacOS_Methods);
  253.     d = PyModule_GetDict(m);
  254.     
  255.     /* Initialize MacOS.Error exception */
  256.     MacOS_Error = PyMac_GetOSErrException();
  257.     if (MacOS_Error == NULL || PyDict_SetItemString(d, "Error", MacOS_Error) != 0)
  258.         Py_FatalError("can't define MacOS.Error");
  259. }
  260.  
  261.