1313
1414#include "macglue.h"
1515
16- #define USE_MAC_MP_MULTITHREADING 1
16+ /* Macro to test whether a weak-loaded CFM function exists */
17+ #define PyMac_PRECHECK (rtn ) do { if ( &rtn == NULL ) {\
18+ PyErr_SetString(PyExc_NotImplementedError, \
19+ "Not available in this shared library/OS version"); \
20+ return; \
21+ }} while(0)
22+
23+
24+ #define USE_MAC_MP_MULTITHREADING 0
1725
1826#if USE_MAC_MP_MULTITHREADING
1927static PyThreadState * _save ;
@@ -84,11 +92,11 @@ EventHotKeyID_Convert(PyObject *v, EventHotKeyID *out)
8492
8593/********** end EventHotKeyID *******/
8694
87- /******** handlecommand ***********/
95+ /******** myEventHandler ***********/
8896
89- static EventHandlerUPP gEventHandlerUPP ;
97+ static EventHandlerUPP myEventHandlerUPP ;
9098
91- pascal OSStatus CarbonEvents_HandleEvent (EventHandlerCallRef handlerRef , EventRef event , void * outPyObject ) {
99+ pascal OSStatus myEventHandler (EventHandlerCallRef handlerRef , EventRef event , void * outPyObject ) {
92100 PyObject * retValue ;
93101 int status ;
94102
@@ -108,7 +116,7 @@ pascal OSStatus CarbonEvents_HandleEvent(EventHandlerCallRef handlerRef, EventRe
108116 return status ;
109117}
110118
111- /******** end handlecommand ***********/
119+ /******** end myEventHandler ***********/
112120
113121
114122static PyObject * CarbonEvents_Error ;
@@ -1059,7 +1067,7 @@ static PyObject *EventTargetRef_InstallEventHandler(EventTargetRefObject *_self,
10591067 if (!PyArg_ParseTuple (_args , "O&O" , EventTypeSpec_Convert , & inSpec , & callback ))
10601068 return NULL ;
10611069
1062- _err = InstallEventHandler (_self -> ob_itself , gEventHandlerUPP , 1 , & inSpec , (void * )callback , & outRef );
1070+ _err = InstallEventHandler (_self -> ob_itself , myEventHandlerUPP , 1 , & inSpec , (void * )callback , & outRef );
10631071 if (_err != noErr ) return PyMac_Error (_err );
10641072
10651073 return Py_BuildValue ("O&" , EventHandlerRef_New , outRef );
@@ -1146,7 +1154,22 @@ static void EventHotKeyRef_dealloc(EventHotKeyRefObject *self)
11461154 PyMem_DEL (self );
11471155}
11481156
1157+ static PyObject * EventHotKeyRef_UnregisterEventHotKey (EventHotKeyRefObject * _self , PyObject * _args )
1158+ {
1159+ PyObject * _res = NULL ;
1160+ OSStatus _err ;
1161+ if (!PyArg_ParseTuple (_args , "" ))
1162+ return NULL ;
1163+ _err = UnregisterEventHotKey (_self -> ob_itself );
1164+ if (_err != noErr ) return PyMac_Error (_err );
1165+ Py_INCREF (Py_None );
1166+ _res = Py_None ;
1167+ return _res ;
1168+ }
1169+
11491170static PyMethodDef EventHotKeyRef_methods [] = {
1171+ {"UnregisterEventHotKey" , (PyCFunction )EventHotKeyRef_UnregisterEventHotKey , 1 ,
1172+ "() -> None" },
11501173 {NULL , NULL , 0 }
11511174};
11521175
@@ -1366,6 +1389,18 @@ static PyObject *CarbonEvents_GetUserFocusEventTarget(PyObject *_self, PyObject
13661389 return _res ;
13671390}
13681391
1392+ static PyObject * CarbonEvents_GetEventDispatcherTarget (PyObject * _self , PyObject * _args )
1393+ {
1394+ PyObject * _res = NULL ;
1395+ EventTargetRef _rv ;
1396+ if (!PyArg_ParseTuple (_args , "" ))
1397+ return NULL ;
1398+ _rv = GetEventDispatcherTarget ();
1399+ _res = Py_BuildValue ("O&" ,
1400+ EventTargetRef_New , _rv );
1401+ return _res ;
1402+ }
1403+
13691404static PyObject * CarbonEvents_QuitApplicationEventLoop (PyObject * _self , PyObject * _args )
13701405{
13711406 PyObject * _res = NULL ;
@@ -1377,6 +1412,66 @@ static PyObject *CarbonEvents_QuitApplicationEventLoop(PyObject *_self, PyObject
13771412 return _res ;
13781413}
13791414
1415+ static PyObject * CarbonEvents_RunAppModalLoopForWindow (PyObject * _self , PyObject * _args )
1416+ {
1417+ PyObject * _res = NULL ;
1418+ OSStatus _err ;
1419+ WindowPtr inWindow ;
1420+ if (!PyArg_ParseTuple (_args , "O&" ,
1421+ WinObj_Convert , & inWindow ))
1422+ return NULL ;
1423+ _err = RunAppModalLoopForWindow (inWindow );
1424+ if (_err != noErr ) return PyMac_Error (_err );
1425+ Py_INCREF (Py_None );
1426+ _res = Py_None ;
1427+ return _res ;
1428+ }
1429+
1430+ static PyObject * CarbonEvents_QuitAppModalLoopForWindow (PyObject * _self , PyObject * _args )
1431+ {
1432+ PyObject * _res = NULL ;
1433+ OSStatus _err ;
1434+ WindowPtr inWindow ;
1435+ if (!PyArg_ParseTuple (_args , "O&" ,
1436+ WinObj_Convert , & inWindow ))
1437+ return NULL ;
1438+ _err = QuitAppModalLoopForWindow (inWindow );
1439+ if (_err != noErr ) return PyMac_Error (_err );
1440+ Py_INCREF (Py_None );
1441+ _res = Py_None ;
1442+ return _res ;
1443+ }
1444+
1445+ static PyObject * CarbonEvents_BeginAppModalStateForWindow (PyObject * _self , PyObject * _args )
1446+ {
1447+ PyObject * _res = NULL ;
1448+ OSStatus _err ;
1449+ WindowPtr inWindow ;
1450+ if (!PyArg_ParseTuple (_args , "O&" ,
1451+ WinObj_Convert , & inWindow ))
1452+ return NULL ;
1453+ _err = BeginAppModalStateForWindow (inWindow );
1454+ if (_err != noErr ) return PyMac_Error (_err );
1455+ Py_INCREF (Py_None );
1456+ _res = Py_None ;
1457+ return _res ;
1458+ }
1459+
1460+ static PyObject * CarbonEvents_EndAppModalStateForWindow (PyObject * _self , PyObject * _args )
1461+ {
1462+ PyObject * _res = NULL ;
1463+ OSStatus _err ;
1464+ WindowPtr inWindow ;
1465+ if (!PyArg_ParseTuple (_args , "O&" ,
1466+ WinObj_Convert , & inWindow ))
1467+ return NULL ;
1468+ _err = EndAppModalStateForWindow (inWindow );
1469+ if (_err != noErr ) return PyMac_Error (_err );
1470+ Py_INCREF (Py_None );
1471+ _res = Py_None ;
1472+ return _res ;
1473+ }
1474+
13801475static PyObject * CarbonEvents_SetUserFocusWindow (PyObject * _self , PyObject * _args )
13811476{
13821477 PyObject * _res = NULL ;
@@ -1474,13 +1569,42 @@ static PyObject *CarbonEvents_GetWindowCancelButton(PyObject *_self, PyObject *_
14741569 return _res ;
14751570}
14761571
1572+ static PyObject * CarbonEvents_RegisterEventHotKey (PyObject * _self , PyObject * _args )
1573+ {
1574+ PyObject * _res = NULL ;
1575+ OSStatus _err ;
1576+ UInt32 inHotKeyCode ;
1577+ UInt32 inHotKeyModifiers ;
1578+ EventHotKeyID inHotKeyID ;
1579+ EventTargetRef inTarget ;
1580+ OptionBits inOptions ;
1581+ EventHotKeyRef outRef ;
1582+ if (!PyArg_ParseTuple (_args , "llO&O&l" ,
1583+ & inHotKeyCode ,
1584+ & inHotKeyModifiers ,
1585+ EventHotKeyID_Convert , & inHotKeyID ,
1586+ EventTargetRef_Convert , & inTarget ,
1587+ & inOptions ))
1588+ return NULL ;
1589+ _err = RegisterEventHotKey (inHotKeyCode ,
1590+ inHotKeyModifiers ,
1591+ inHotKeyID ,
1592+ inTarget ,
1593+ inOptions ,
1594+ & outRef );
1595+ if (_err != noErr ) return PyMac_Error (_err );
1596+ _res = Py_BuildValue ("O&" ,
1597+ EventHotKeyRef_New , outRef );
1598+ return _res ;
1599+ }
1600+
14771601static PyObject * CarbonEvents_RunApplicationEventLoop (PyObject * _self , PyObject * _args )
14781602{
14791603 PyObject * _res = NULL ;
14801604
14811605#if USE_MAC_MP_MULTITHREADING
14821606 if (MPCreateCriticalRegion (& reentrantLock ) != noErr ) {
1483- printf ("lock failure\n" );
1607+ PySys_WriteStderr ("lock failure\n" );
14841608 return NULL ;
14851609 }
14861610 _save = PyEval_SaveThread ();
@@ -1527,8 +1651,18 @@ static PyMethodDef CarbonEvents_methods[] = {
15271651 "() -> (EventTargetRef _rv)" },
15281652 {"GetUserFocusEventTarget" , (PyCFunction )CarbonEvents_GetUserFocusEventTarget , 1 ,
15291653 "() -> (EventTargetRef _rv)" },
1654+ {"GetEventDispatcherTarget" , (PyCFunction )CarbonEvents_GetEventDispatcherTarget , 1 ,
1655+ "() -> (EventTargetRef _rv)" },
15301656 {"QuitApplicationEventLoop" , (PyCFunction )CarbonEvents_QuitApplicationEventLoop , 1 ,
15311657 "() -> None" },
1658+ {"RunAppModalLoopForWindow" , (PyCFunction )CarbonEvents_RunAppModalLoopForWindow , 1 ,
1659+ "(WindowPtr inWindow) -> None" },
1660+ {"QuitAppModalLoopForWindow" , (PyCFunction )CarbonEvents_QuitAppModalLoopForWindow , 1 ,
1661+ "(WindowPtr inWindow) -> None" },
1662+ {"BeginAppModalStateForWindow" , (PyCFunction )CarbonEvents_BeginAppModalStateForWindow , 1 ,
1663+ "(WindowPtr inWindow) -> None" },
1664+ {"EndAppModalStateForWindow" , (PyCFunction )CarbonEvents_EndAppModalStateForWindow , 1 ,
1665+ "(WindowPtr inWindow) -> None" },
15321666 {"SetUserFocusWindow" , (PyCFunction )CarbonEvents_SetUserFocusWindow , 1 ,
15331667 "(WindowPtr inWindow) -> None" },
15341668 {"GetUserFocusWindow" , (PyCFunction )CarbonEvents_GetUserFocusWindow , 1 ,
@@ -1541,6 +1675,8 @@ static PyMethodDef CarbonEvents_methods[] = {
15411675 "(WindowPtr inWindow) -> (ControlHandle outControl)" },
15421676 {"GetWindowCancelButton" , (PyCFunction )CarbonEvents_GetWindowCancelButton , 1 ,
15431677 "(WindowPtr inWindow) -> (ControlHandle outControl)" },
1678+ {"RegisterEventHotKey" , (PyCFunction )CarbonEvents_RegisterEventHotKey , 1 ,
1679+ "(UInt32 inHotKeyCode, UInt32 inHotKeyModifiers, EventHotKeyID inHotKeyID, EventTargetRef inTarget, OptionBits inOptions) -> (EventHotKeyRef outRef)" },
15441680 {"RunApplicationEventLoop" , (PyCFunction )CarbonEvents_RunApplicationEventLoop , 1 ,
15451681 "() -> ()" },
15461682 {NULL , NULL , 0 }
@@ -1556,7 +1692,8 @@ void init_CarbonEvt(void)
15561692
15571693
15581694
1559- gEventHandlerUPP = NewEventHandlerUPP (CarbonEvents_HandleEvent );
1695+ PyMac_PRECHECK (NewEventHandlerUPP ); /* This can fail if CarbonLib is too old */
1696+ myEventHandlerUPP = NewEventHandlerUPP (myEventHandler );
15601697
15611698
15621699 m = Py_InitModule ("_CarbonEvt" , CarbonEvents_methods );
0 commit comments