/Mac/Modules/evt/_Evtmodule.c

http://unladen-swallow.googlecode.com/ · C · 559 lines · 504 code · 52 blank · 3 comment · 35 complexity · 1dd8756feb989d25dfdf306f3fa078f1 MD5 · raw file

  1. /* ========================== Module _Evt =========================== */
  2. #include "Python.h"
  3. #ifndef __LP64__
  4. #include "pymactoolbox.h"
  5. /* Macro to test whether a weak-loaded CFM function exists */
  6. #define PyMac_PRECHECK(rtn) do { if ( &rtn == NULL ) {\
  7. PyErr_SetString(PyExc_NotImplementedError, \
  8. "Not available in this shared library/OS version"); \
  9. return NULL; \
  10. }} while(0)
  11. #include <Carbon/Carbon.h>
  12. static PyObject *Evt_Error;
  13. static PyObject *Evt_GetMouse(PyObject *_self, PyObject *_args)
  14. {
  15. PyObject *_res = NULL;
  16. Point mouseLoc;
  17. #ifndef GetMouse
  18. PyMac_PRECHECK(GetMouse);
  19. #endif
  20. if (!PyArg_ParseTuple(_args, ""))
  21. return NULL;
  22. GetMouse(&mouseLoc);
  23. _res = Py_BuildValue("O&",
  24. PyMac_BuildPoint, mouseLoc);
  25. return _res;
  26. }
  27. static PyObject *Evt_Button(PyObject *_self, PyObject *_args)
  28. {
  29. PyObject *_res = NULL;
  30. Boolean _rv;
  31. #ifndef Button
  32. PyMac_PRECHECK(Button);
  33. #endif
  34. if (!PyArg_ParseTuple(_args, ""))
  35. return NULL;
  36. _rv = Button();
  37. _res = Py_BuildValue("b",
  38. _rv);
  39. return _res;
  40. }
  41. static PyObject *Evt_StillDown(PyObject *_self, PyObject *_args)
  42. {
  43. PyObject *_res = NULL;
  44. Boolean _rv;
  45. #ifndef StillDown
  46. PyMac_PRECHECK(StillDown);
  47. #endif
  48. if (!PyArg_ParseTuple(_args, ""))
  49. return NULL;
  50. _rv = StillDown();
  51. _res = Py_BuildValue("b",
  52. _rv);
  53. return _res;
  54. }
  55. static PyObject *Evt_WaitMouseUp(PyObject *_self, PyObject *_args)
  56. {
  57. PyObject *_res = NULL;
  58. Boolean _rv;
  59. #ifndef WaitMouseUp
  60. PyMac_PRECHECK(WaitMouseUp);
  61. #endif
  62. if (!PyArg_ParseTuple(_args, ""))
  63. return NULL;
  64. _rv = WaitMouseUp();
  65. _res = Py_BuildValue("b",
  66. _rv);
  67. return _res;
  68. }
  69. static PyObject *Evt_GetCaretTime(PyObject *_self, PyObject *_args)
  70. {
  71. PyObject *_res = NULL;
  72. UInt32 _rv;
  73. #ifndef GetCaretTime
  74. PyMac_PRECHECK(GetCaretTime);
  75. #endif
  76. if (!PyArg_ParseTuple(_args, ""))
  77. return NULL;
  78. _rv = GetCaretTime();
  79. _res = Py_BuildValue("l",
  80. _rv);
  81. return _res;
  82. }
  83. static PyObject *Evt_GetKeys(PyObject *_self, PyObject *_args)
  84. {
  85. PyObject *_res = NULL;
  86. KeyMap theKeys__out__;
  87. #ifndef GetKeys
  88. PyMac_PRECHECK(GetKeys);
  89. #endif
  90. if (!PyArg_ParseTuple(_args, ""))
  91. return NULL;
  92. GetKeys(theKeys__out__);
  93. _res = Py_BuildValue("s#",
  94. (char *)&theKeys__out__, (int)sizeof(KeyMap));
  95. return _res;
  96. }
  97. static PyObject *Evt_GetDblTime(PyObject *_self, PyObject *_args)
  98. {
  99. PyObject *_res = NULL;
  100. UInt32 _rv;
  101. #ifndef GetDblTime
  102. PyMac_PRECHECK(GetDblTime);
  103. #endif
  104. if (!PyArg_ParseTuple(_args, ""))
  105. return NULL;
  106. _rv = GetDblTime();
  107. _res = Py_BuildValue("l",
  108. _rv);
  109. return _res;
  110. }
  111. static PyObject *Evt_SetEventMask(PyObject *_self, PyObject *_args)
  112. {
  113. PyObject *_res = NULL;
  114. EventMask value;
  115. #ifndef SetEventMask
  116. PyMac_PRECHECK(SetEventMask);
  117. #endif
  118. if (!PyArg_ParseTuple(_args, "H",
  119. &value))
  120. return NULL;
  121. SetEventMask(value);
  122. Py_INCREF(Py_None);
  123. _res = Py_None;
  124. return _res;
  125. }
  126. static PyObject *Evt_GetNextEvent(PyObject *_self, PyObject *_args)
  127. {
  128. PyObject *_res = NULL;
  129. Boolean _rv;
  130. EventMask eventMask;
  131. EventRecord theEvent;
  132. #ifndef GetNextEvent
  133. PyMac_PRECHECK(GetNextEvent);
  134. #endif
  135. if (!PyArg_ParseTuple(_args, "H",
  136. &eventMask))
  137. return NULL;
  138. _rv = GetNextEvent(eventMask,
  139. &theEvent);
  140. _res = Py_BuildValue("bO&",
  141. _rv,
  142. PyMac_BuildEventRecord, &theEvent);
  143. return _res;
  144. }
  145. static PyObject *Evt_EventAvail(PyObject *_self, PyObject *_args)
  146. {
  147. PyObject *_res = NULL;
  148. Boolean _rv;
  149. EventMask eventMask;
  150. EventRecord theEvent;
  151. #ifndef EventAvail
  152. PyMac_PRECHECK(EventAvail);
  153. #endif
  154. if (!PyArg_ParseTuple(_args, "H",
  155. &eventMask))
  156. return NULL;
  157. _rv = EventAvail(eventMask,
  158. &theEvent);
  159. _res = Py_BuildValue("bO&",
  160. _rv,
  161. PyMac_BuildEventRecord, &theEvent);
  162. return _res;
  163. }
  164. static PyObject *Evt_PostEvent(PyObject *_self, PyObject *_args)
  165. {
  166. PyObject *_res = NULL;
  167. OSErr _err;
  168. EventKind eventNum;
  169. UInt32 eventMsg;
  170. #ifndef PostEvent
  171. PyMac_PRECHECK(PostEvent);
  172. #endif
  173. if (!PyArg_ParseTuple(_args, "Hl",
  174. &eventNum,
  175. &eventMsg))
  176. return NULL;
  177. _err = PostEvent(eventNum,
  178. eventMsg);
  179. if (_err != noErr) return PyMac_Error(_err);
  180. Py_INCREF(Py_None);
  181. _res = Py_None;
  182. return _res;
  183. }
  184. static PyObject *Evt_FlushEvents(PyObject *_self, PyObject *_args)
  185. {
  186. PyObject *_res = NULL;
  187. EventMask whichMask;
  188. EventMask stopMask;
  189. #ifndef FlushEvents
  190. PyMac_PRECHECK(FlushEvents);
  191. #endif
  192. if (!PyArg_ParseTuple(_args, "HH",
  193. &whichMask,
  194. &stopMask))
  195. return NULL;
  196. FlushEvents(whichMask,
  197. stopMask);
  198. Py_INCREF(Py_None);
  199. _res = Py_None;
  200. return _res;
  201. }
  202. static PyObject *Evt_GetGlobalMouse(PyObject *_self, PyObject *_args)
  203. {
  204. PyObject *_res = NULL;
  205. Point globalMouse;
  206. #ifndef GetGlobalMouse
  207. PyMac_PRECHECK(GetGlobalMouse);
  208. #endif
  209. if (!PyArg_ParseTuple(_args, ""))
  210. return NULL;
  211. GetGlobalMouse(&globalMouse);
  212. _res = Py_BuildValue("O&",
  213. PyMac_BuildPoint, globalMouse);
  214. return _res;
  215. }
  216. static PyObject *Evt_GetCurrentKeyModifiers(PyObject *_self, PyObject *_args)
  217. {
  218. PyObject *_res = NULL;
  219. UInt32 _rv;
  220. #ifndef GetCurrentKeyModifiers
  221. PyMac_PRECHECK(GetCurrentKeyModifiers);
  222. #endif
  223. if (!PyArg_ParseTuple(_args, ""))
  224. return NULL;
  225. _rv = GetCurrentKeyModifiers();
  226. _res = Py_BuildValue("l",
  227. _rv);
  228. return _res;
  229. }
  230. static PyObject *Evt_CheckEventQueueForUserCancel(PyObject *_self, PyObject *_args)
  231. {
  232. PyObject *_res = NULL;
  233. Boolean _rv;
  234. #ifndef CheckEventQueueForUserCancel
  235. PyMac_PRECHECK(CheckEventQueueForUserCancel);
  236. #endif
  237. if (!PyArg_ParseTuple(_args, ""))
  238. return NULL;
  239. _rv = CheckEventQueueForUserCancel();
  240. _res = Py_BuildValue("b",
  241. _rv);
  242. return _res;
  243. }
  244. static PyObject *Evt_KeyScript(PyObject *_self, PyObject *_args)
  245. {
  246. PyObject *_res = NULL;
  247. short code;
  248. #ifndef KeyScript
  249. PyMac_PRECHECK(KeyScript);
  250. #endif
  251. if (!PyArg_ParseTuple(_args, "h",
  252. &code))
  253. return NULL;
  254. KeyScript(code);
  255. Py_INCREF(Py_None);
  256. _res = Py_None;
  257. return _res;
  258. }
  259. static PyObject *Evt_IsCmdChar(PyObject *_self, PyObject *_args)
  260. {
  261. PyObject *_res = NULL;
  262. Boolean _rv;
  263. EventRecord event;
  264. short test;
  265. #ifndef IsCmdChar
  266. PyMac_PRECHECK(IsCmdChar);
  267. #endif
  268. if (!PyArg_ParseTuple(_args, "O&h",
  269. PyMac_GetEventRecord, &event,
  270. &test))
  271. return NULL;
  272. _rv = IsCmdChar(&event,
  273. test);
  274. _res = Py_BuildValue("b",
  275. _rv);
  276. return _res;
  277. }
  278. static PyObject *Evt_LMGetKeyThresh(PyObject *_self, PyObject *_args)
  279. {
  280. PyObject *_res = NULL;
  281. SInt16 _rv;
  282. #ifndef LMGetKeyThresh
  283. PyMac_PRECHECK(LMGetKeyThresh);
  284. #endif
  285. if (!PyArg_ParseTuple(_args, ""))
  286. return NULL;
  287. _rv = LMGetKeyThresh();
  288. _res = Py_BuildValue("h",
  289. _rv);
  290. return _res;
  291. }
  292. static PyObject *Evt_LMSetKeyThresh(PyObject *_self, PyObject *_args)
  293. {
  294. PyObject *_res = NULL;
  295. SInt16 value;
  296. #ifndef LMSetKeyThresh
  297. PyMac_PRECHECK(LMSetKeyThresh);
  298. #endif
  299. if (!PyArg_ParseTuple(_args, "h",
  300. &value))
  301. return NULL;
  302. LMSetKeyThresh(value);
  303. Py_INCREF(Py_None);
  304. _res = Py_None;
  305. return _res;
  306. }
  307. static PyObject *Evt_LMGetKeyRepThresh(PyObject *_self, PyObject *_args)
  308. {
  309. PyObject *_res = NULL;
  310. SInt16 _rv;
  311. #ifndef LMGetKeyRepThresh
  312. PyMac_PRECHECK(LMGetKeyRepThresh);
  313. #endif
  314. if (!PyArg_ParseTuple(_args, ""))
  315. return NULL;
  316. _rv = LMGetKeyRepThresh();
  317. _res = Py_BuildValue("h",
  318. _rv);
  319. return _res;
  320. }
  321. static PyObject *Evt_LMSetKeyRepThresh(PyObject *_self, PyObject *_args)
  322. {
  323. PyObject *_res = NULL;
  324. SInt16 value;
  325. #ifndef LMSetKeyRepThresh
  326. PyMac_PRECHECK(LMSetKeyRepThresh);
  327. #endif
  328. if (!PyArg_ParseTuple(_args, "h",
  329. &value))
  330. return NULL;
  331. LMSetKeyRepThresh(value);
  332. Py_INCREF(Py_None);
  333. _res = Py_None;
  334. return _res;
  335. }
  336. static PyObject *Evt_LMGetKbdLast(PyObject *_self, PyObject *_args)
  337. {
  338. PyObject *_res = NULL;
  339. UInt8 _rv;
  340. #ifndef LMGetKbdLast
  341. PyMac_PRECHECK(LMGetKbdLast);
  342. #endif
  343. if (!PyArg_ParseTuple(_args, ""))
  344. return NULL;
  345. _rv = LMGetKbdLast();
  346. _res = Py_BuildValue("b",
  347. _rv);
  348. return _res;
  349. }
  350. static PyObject *Evt_LMSetKbdLast(PyObject *_self, PyObject *_args)
  351. {
  352. PyObject *_res = NULL;
  353. UInt8 value;
  354. #ifndef LMSetKbdLast
  355. PyMac_PRECHECK(LMSetKbdLast);
  356. #endif
  357. if (!PyArg_ParseTuple(_args, "b",
  358. &value))
  359. return NULL;
  360. LMSetKbdLast(value);
  361. Py_INCREF(Py_None);
  362. _res = Py_None;
  363. return _res;
  364. }
  365. static PyObject *Evt_LMGetKbdType(PyObject *_self, PyObject *_args)
  366. {
  367. PyObject *_res = NULL;
  368. UInt8 _rv;
  369. #ifndef LMGetKbdType
  370. PyMac_PRECHECK(LMGetKbdType);
  371. #endif
  372. if (!PyArg_ParseTuple(_args, ""))
  373. return NULL;
  374. _rv = LMGetKbdType();
  375. _res = Py_BuildValue("b",
  376. _rv);
  377. return _res;
  378. }
  379. static PyObject *Evt_LMSetKbdType(PyObject *_self, PyObject *_args)
  380. {
  381. PyObject *_res = NULL;
  382. UInt8 value;
  383. #ifndef LMSetKbdType
  384. PyMac_PRECHECK(LMSetKbdType);
  385. #endif
  386. if (!PyArg_ParseTuple(_args, "b",
  387. &value))
  388. return NULL;
  389. LMSetKbdType(value);
  390. Py_INCREF(Py_None);
  391. _res = Py_None;
  392. return _res;
  393. }
  394. static PyObject *Evt_TickCount(PyObject *_self, PyObject *_args)
  395. {
  396. PyObject *_res = NULL;
  397. UInt32 _rv;
  398. #ifndef TickCount
  399. PyMac_PRECHECK(TickCount);
  400. #endif
  401. if (!PyArg_ParseTuple(_args, ""))
  402. return NULL;
  403. _rv = TickCount();
  404. _res = Py_BuildValue("l",
  405. _rv);
  406. return _res;
  407. }
  408. static PyObject *Evt_WaitNextEvent(PyObject *_self, PyObject *_args)
  409. {
  410. PyObject *_res = NULL;
  411. Boolean _rv;
  412. EventMask eventMask;
  413. EventRecord theEvent;
  414. UInt32 sleep;
  415. Handle mouseregion = (Handle)0;
  416. if (!PyArg_ParseTuple(_args, "Hl|O&",
  417. &eventMask,
  418. &sleep,
  419. OptResObj_Convert, &mouseregion))
  420. return NULL;
  421. _rv = WaitNextEvent(eventMask,
  422. &theEvent,
  423. sleep,
  424. (RgnHandle)mouseregion);
  425. _res = Py_BuildValue("bO&",
  426. _rv,
  427. PyMac_BuildEventRecord, &theEvent);
  428. return _res;
  429. }
  430. static PyMethodDef Evt_methods[] = {
  431. {"GetMouse", (PyCFunction)Evt_GetMouse, 1,
  432. PyDoc_STR("() -> (Point mouseLoc)")},
  433. {"Button", (PyCFunction)Evt_Button, 1,
  434. PyDoc_STR("() -> (Boolean _rv)")},
  435. {"StillDown", (PyCFunction)Evt_StillDown, 1,
  436. PyDoc_STR("() -> (Boolean _rv)")},
  437. {"WaitMouseUp", (PyCFunction)Evt_WaitMouseUp, 1,
  438. PyDoc_STR("() -> (Boolean _rv)")},
  439. {"GetCaretTime", (PyCFunction)Evt_GetCaretTime, 1,
  440. PyDoc_STR("() -> (UInt32 _rv)")},
  441. {"GetKeys", (PyCFunction)Evt_GetKeys, 1,
  442. PyDoc_STR("() -> (KeyMap theKeys)")},
  443. {"GetDblTime", (PyCFunction)Evt_GetDblTime, 1,
  444. PyDoc_STR("() -> (UInt32 _rv)")},
  445. {"SetEventMask", (PyCFunction)Evt_SetEventMask, 1,
  446. PyDoc_STR("(EventMask value) -> None")},
  447. {"GetNextEvent", (PyCFunction)Evt_GetNextEvent, 1,
  448. PyDoc_STR("(EventMask eventMask) -> (Boolean _rv, EventRecord theEvent)")},
  449. {"EventAvail", (PyCFunction)Evt_EventAvail, 1,
  450. PyDoc_STR("(EventMask eventMask) -> (Boolean _rv, EventRecord theEvent)")},
  451. {"PostEvent", (PyCFunction)Evt_PostEvent, 1,
  452. PyDoc_STR("(EventKind eventNum, UInt32 eventMsg) -> None")},
  453. {"FlushEvents", (PyCFunction)Evt_FlushEvents, 1,
  454. PyDoc_STR("(EventMask whichMask, EventMask stopMask) -> None")},
  455. {"GetGlobalMouse", (PyCFunction)Evt_GetGlobalMouse, 1,
  456. PyDoc_STR("() -> (Point globalMouse)")},
  457. {"GetCurrentKeyModifiers", (PyCFunction)Evt_GetCurrentKeyModifiers, 1,
  458. PyDoc_STR("() -> (UInt32 _rv)")},
  459. {"CheckEventQueueForUserCancel", (PyCFunction)Evt_CheckEventQueueForUserCancel, 1,
  460. PyDoc_STR("() -> (Boolean _rv)")},
  461. {"KeyScript", (PyCFunction)Evt_KeyScript, 1,
  462. PyDoc_STR("(short code) -> None")},
  463. {"IsCmdChar", (PyCFunction)Evt_IsCmdChar, 1,
  464. PyDoc_STR("(EventRecord event, short test) -> (Boolean _rv)")},
  465. {"LMGetKeyThresh", (PyCFunction)Evt_LMGetKeyThresh, 1,
  466. PyDoc_STR("() -> (SInt16 _rv)")},
  467. {"LMSetKeyThresh", (PyCFunction)Evt_LMSetKeyThresh, 1,
  468. PyDoc_STR("(SInt16 value) -> None")},
  469. {"LMGetKeyRepThresh", (PyCFunction)Evt_LMGetKeyRepThresh, 1,
  470. PyDoc_STR("() -> (SInt16 _rv)")},
  471. {"LMSetKeyRepThresh", (PyCFunction)Evt_LMSetKeyRepThresh, 1,
  472. PyDoc_STR("(SInt16 value) -> None")},
  473. {"LMGetKbdLast", (PyCFunction)Evt_LMGetKbdLast, 1,
  474. PyDoc_STR("() -> (UInt8 _rv)")},
  475. {"LMSetKbdLast", (PyCFunction)Evt_LMSetKbdLast, 1,
  476. PyDoc_STR("(UInt8 value) -> None")},
  477. {"LMGetKbdType", (PyCFunction)Evt_LMGetKbdType, 1,
  478. PyDoc_STR("() -> (UInt8 _rv)")},
  479. {"LMSetKbdType", (PyCFunction)Evt_LMSetKbdType, 1,
  480. PyDoc_STR("(UInt8 value) -> None")},
  481. {"TickCount", (PyCFunction)Evt_TickCount, 1,
  482. PyDoc_STR("() -> (UInt32 _rv)")},
  483. {"WaitNextEvent", (PyCFunction)Evt_WaitNextEvent, 1,
  484. PyDoc_STR("(EventMask eventMask, UInt32 sleep [,RegionHandle]) -> (Boolean _rv, EventRecord theEvent)")},
  485. {NULL, NULL, 0}
  486. };
  487. #else /* __LP64__ */
  488. static PyMethodDef Evt_methods[] = {
  489. {NULL, NULL, 0}
  490. };
  491. #endif /* __LP64__ */
  492. void init_Evt(void)
  493. {
  494. PyObject *m;
  495. #ifndef __LP64__
  496. PyObject *d;
  497. #endif /* __LP64__ */
  498. m = Py_InitModule("_Evt", Evt_methods);
  499. #ifndef __LP64__
  500. d = PyModule_GetDict(m);
  501. Evt_Error = PyMac_GetOSErrException();
  502. if (Evt_Error == NULL ||
  503. PyDict_SetItemString(d, "Error", Evt_Error) != 0)
  504. return;
  505. #endif /* __LP64__ */
  506. }
  507. /* ======================== End module _Evt ========================= */