Move setting of ioready 'wait' earlier in call chain, to
[python/dscho.git] / Mac / Modules / evt / _Evtmodule.c
blob48c878d1ac19ac06d3ec6c554d8dacd0cffe48cb
2 /* ========================== Module _Evt =========================== */
4 #include "Python.h"
8 #ifdef _WIN32
9 #include "pywintoolbox.h"
10 #else
11 #include "macglue.h"
12 #include "pymactoolbox.h"
13 #endif
15 /* Macro to test whether a weak-loaded CFM function exists */
16 #define PyMac_PRECHECK(rtn) do { if ( &rtn == NULL ) {\
17 PyErr_SetString(PyExc_NotImplementedError, \
18 "Not available in this shared library/OS version"); \
19 return NULL; \
20 }} while(0)
23 #ifdef WITHOUT_FRAMEWORKS
24 #include <Events.h>
25 #else
26 #include <Carbon/Carbon.h>
27 #endif
30 static PyObject *Evt_Error;
32 static PyObject *Evt_GetMouse(PyObject *_self, PyObject *_args)
34 PyObject *_res = NULL;
35 Point mouseLoc;
36 #ifndef GetMouse
37 PyMac_PRECHECK(GetMouse);
38 #endif
39 if (!PyArg_ParseTuple(_args, ""))
40 return NULL;
41 GetMouse(&mouseLoc);
42 _res = Py_BuildValue("O&",
43 PyMac_BuildPoint, mouseLoc);
44 return _res;
47 static PyObject *Evt_Button(PyObject *_self, PyObject *_args)
49 PyObject *_res = NULL;
50 Boolean _rv;
51 #ifndef Button
52 PyMac_PRECHECK(Button);
53 #endif
54 if (!PyArg_ParseTuple(_args, ""))
55 return NULL;
56 _rv = Button();
57 _res = Py_BuildValue("b",
58 _rv);
59 return _res;
62 static PyObject *Evt_StillDown(PyObject *_self, PyObject *_args)
64 PyObject *_res = NULL;
65 Boolean _rv;
66 #ifndef StillDown
67 PyMac_PRECHECK(StillDown);
68 #endif
69 if (!PyArg_ParseTuple(_args, ""))
70 return NULL;
71 _rv = StillDown();
72 _res = Py_BuildValue("b",
73 _rv);
74 return _res;
77 static PyObject *Evt_WaitMouseUp(PyObject *_self, PyObject *_args)
79 PyObject *_res = NULL;
80 Boolean _rv;
81 #ifndef WaitMouseUp
82 PyMac_PRECHECK(WaitMouseUp);
83 #endif
84 if (!PyArg_ParseTuple(_args, ""))
85 return NULL;
86 _rv = WaitMouseUp();
87 _res = Py_BuildValue("b",
88 _rv);
89 return _res;
92 static PyObject *Evt_GetCaretTime(PyObject *_self, PyObject *_args)
94 PyObject *_res = NULL;
95 UInt32 _rv;
96 #ifndef GetCaretTime
97 PyMac_PRECHECK(GetCaretTime);
98 #endif
99 if (!PyArg_ParseTuple(_args, ""))
100 return NULL;
101 _rv = GetCaretTime();
102 _res = Py_BuildValue("l",
103 _rv);
104 return _res;
107 static PyObject *Evt_GetKeys(PyObject *_self, PyObject *_args)
109 PyObject *_res = NULL;
110 KeyMap theKeys__out__;
111 #ifndef GetKeys
112 PyMac_PRECHECK(GetKeys);
113 #endif
114 if (!PyArg_ParseTuple(_args, ""))
115 return NULL;
116 GetKeys(theKeys__out__);
117 _res = Py_BuildValue("s#",
118 (char *)&theKeys__out__, (int)sizeof(KeyMap));
119 return _res;
122 static PyObject *Evt_GetDblTime(PyObject *_self, PyObject *_args)
124 PyObject *_res = NULL;
125 UInt32 _rv;
126 #ifndef GetDblTime
127 PyMac_PRECHECK(GetDblTime);
128 #endif
129 if (!PyArg_ParseTuple(_args, ""))
130 return NULL;
131 _rv = GetDblTime();
132 _res = Py_BuildValue("l",
133 _rv);
134 return _res;
137 static PyObject *Evt_SetEventMask(PyObject *_self, PyObject *_args)
139 PyObject *_res = NULL;
140 EventMask value;
141 #ifndef SetEventMask
142 PyMac_PRECHECK(SetEventMask);
143 #endif
144 if (!PyArg_ParseTuple(_args, "H",
145 &value))
146 return NULL;
147 SetEventMask(value);
148 Py_INCREF(Py_None);
149 _res = Py_None;
150 return _res;
153 static PyObject *Evt_GetNextEvent(PyObject *_self, PyObject *_args)
155 PyObject *_res = NULL;
156 Boolean _rv;
157 EventMask eventMask;
158 EventRecord theEvent;
159 #ifndef GetNextEvent
160 PyMac_PRECHECK(GetNextEvent);
161 #endif
162 if (!PyArg_ParseTuple(_args, "H",
163 &eventMask))
164 return NULL;
165 _rv = GetNextEvent(eventMask,
166 &theEvent);
167 _res = Py_BuildValue("bO&",
168 _rv,
169 PyMac_BuildEventRecord, &theEvent);
170 return _res;
173 static PyObject *Evt_EventAvail(PyObject *_self, PyObject *_args)
175 PyObject *_res = NULL;
176 Boolean _rv;
177 EventMask eventMask;
178 EventRecord theEvent;
179 #ifndef EventAvail
180 PyMac_PRECHECK(EventAvail);
181 #endif
182 if (!PyArg_ParseTuple(_args, "H",
183 &eventMask))
184 return NULL;
185 _rv = EventAvail(eventMask,
186 &theEvent);
187 _res = Py_BuildValue("bO&",
188 _rv,
189 PyMac_BuildEventRecord, &theEvent);
190 return _res;
193 static PyObject *Evt_PostEvent(PyObject *_self, PyObject *_args)
195 PyObject *_res = NULL;
196 OSErr _err;
197 EventKind eventNum;
198 UInt32 eventMsg;
199 #ifndef PostEvent
200 PyMac_PRECHECK(PostEvent);
201 #endif
202 if (!PyArg_ParseTuple(_args, "Hl",
203 &eventNum,
204 &eventMsg))
205 return NULL;
206 _err = PostEvent(eventNum,
207 eventMsg);
208 if (_err != noErr) return PyMac_Error(_err);
209 Py_INCREF(Py_None);
210 _res = Py_None;
211 return _res;
214 static PyObject *Evt_FlushEvents(PyObject *_self, PyObject *_args)
216 PyObject *_res = NULL;
217 EventMask whichMask;
218 EventMask stopMask;
219 #ifndef FlushEvents
220 PyMac_PRECHECK(FlushEvents);
221 #endif
222 if (!PyArg_ParseTuple(_args, "HH",
223 &whichMask,
224 &stopMask))
225 return NULL;
226 FlushEvents(whichMask,
227 stopMask);
228 Py_INCREF(Py_None);
229 _res = Py_None;
230 return _res;
233 static PyObject *Evt_GetGlobalMouse(PyObject *_self, PyObject *_args)
235 PyObject *_res = NULL;
236 Point globalMouse;
237 #ifndef GetGlobalMouse
238 PyMac_PRECHECK(GetGlobalMouse);
239 #endif
240 if (!PyArg_ParseTuple(_args, ""))
241 return NULL;
242 GetGlobalMouse(&globalMouse);
243 _res = Py_BuildValue("O&",
244 PyMac_BuildPoint, globalMouse);
245 return _res;
248 static PyObject *Evt_GetCurrentKeyModifiers(PyObject *_self, PyObject *_args)
250 PyObject *_res = NULL;
251 UInt32 _rv;
252 #ifndef GetCurrentKeyModifiers
253 PyMac_PRECHECK(GetCurrentKeyModifiers);
254 #endif
255 if (!PyArg_ParseTuple(_args, ""))
256 return NULL;
257 _rv = GetCurrentKeyModifiers();
258 _res = Py_BuildValue("l",
259 _rv);
260 return _res;
263 static PyObject *Evt_CheckEventQueueForUserCancel(PyObject *_self, PyObject *_args)
265 PyObject *_res = NULL;
266 Boolean _rv;
267 #ifndef CheckEventQueueForUserCancel
268 PyMac_PRECHECK(CheckEventQueueForUserCancel);
269 #endif
270 if (!PyArg_ParseTuple(_args, ""))
271 return NULL;
272 _rv = CheckEventQueueForUserCancel();
273 _res = Py_BuildValue("b",
274 _rv);
275 return _res;
278 static PyObject *Evt_KeyScript(PyObject *_self, PyObject *_args)
280 PyObject *_res = NULL;
281 short code;
282 #ifndef KeyScript
283 PyMac_PRECHECK(KeyScript);
284 #endif
285 if (!PyArg_ParseTuple(_args, "h",
286 &code))
287 return NULL;
288 KeyScript(code);
289 Py_INCREF(Py_None);
290 _res = Py_None;
291 return _res;
294 static PyObject *Evt_IsCmdChar(PyObject *_self, PyObject *_args)
296 PyObject *_res = NULL;
297 Boolean _rv;
298 EventRecord event;
299 short test;
300 #ifndef IsCmdChar
301 PyMac_PRECHECK(IsCmdChar);
302 #endif
303 if (!PyArg_ParseTuple(_args, "O&h",
304 PyMac_GetEventRecord, &event,
305 &test))
306 return NULL;
307 _rv = IsCmdChar(&event,
308 test);
309 _res = Py_BuildValue("b",
310 _rv);
311 return _res;
314 static PyObject *Evt_LMGetKeyThresh(PyObject *_self, PyObject *_args)
316 PyObject *_res = NULL;
317 SInt16 _rv;
318 #ifndef LMGetKeyThresh
319 PyMac_PRECHECK(LMGetKeyThresh);
320 #endif
321 if (!PyArg_ParseTuple(_args, ""))
322 return NULL;
323 _rv = LMGetKeyThresh();
324 _res = Py_BuildValue("h",
325 _rv);
326 return _res;
329 static PyObject *Evt_LMSetKeyThresh(PyObject *_self, PyObject *_args)
331 PyObject *_res = NULL;
332 SInt16 value;
333 #ifndef LMSetKeyThresh
334 PyMac_PRECHECK(LMSetKeyThresh);
335 #endif
336 if (!PyArg_ParseTuple(_args, "h",
337 &value))
338 return NULL;
339 LMSetKeyThresh(value);
340 Py_INCREF(Py_None);
341 _res = Py_None;
342 return _res;
345 static PyObject *Evt_LMGetKeyRepThresh(PyObject *_self, PyObject *_args)
347 PyObject *_res = NULL;
348 SInt16 _rv;
349 #ifndef LMGetKeyRepThresh
350 PyMac_PRECHECK(LMGetKeyRepThresh);
351 #endif
352 if (!PyArg_ParseTuple(_args, ""))
353 return NULL;
354 _rv = LMGetKeyRepThresh();
355 _res = Py_BuildValue("h",
356 _rv);
357 return _res;
360 static PyObject *Evt_LMSetKeyRepThresh(PyObject *_self, PyObject *_args)
362 PyObject *_res = NULL;
363 SInt16 value;
364 #ifndef LMSetKeyRepThresh
365 PyMac_PRECHECK(LMSetKeyRepThresh);
366 #endif
367 if (!PyArg_ParseTuple(_args, "h",
368 &value))
369 return NULL;
370 LMSetKeyRepThresh(value);
371 Py_INCREF(Py_None);
372 _res = Py_None;
373 return _res;
376 static PyObject *Evt_LMGetKbdLast(PyObject *_self, PyObject *_args)
378 PyObject *_res = NULL;
379 UInt8 _rv;
380 #ifndef LMGetKbdLast
381 PyMac_PRECHECK(LMGetKbdLast);
382 #endif
383 if (!PyArg_ParseTuple(_args, ""))
384 return NULL;
385 _rv = LMGetKbdLast();
386 _res = Py_BuildValue("b",
387 _rv);
388 return _res;
391 static PyObject *Evt_LMSetKbdLast(PyObject *_self, PyObject *_args)
393 PyObject *_res = NULL;
394 UInt8 value;
395 #ifndef LMSetKbdLast
396 PyMac_PRECHECK(LMSetKbdLast);
397 #endif
398 if (!PyArg_ParseTuple(_args, "b",
399 &value))
400 return NULL;
401 LMSetKbdLast(value);
402 Py_INCREF(Py_None);
403 _res = Py_None;
404 return _res;
407 static PyObject *Evt_LMGetKbdType(PyObject *_self, PyObject *_args)
409 PyObject *_res = NULL;
410 UInt8 _rv;
411 #ifndef LMGetKbdType
412 PyMac_PRECHECK(LMGetKbdType);
413 #endif
414 if (!PyArg_ParseTuple(_args, ""))
415 return NULL;
416 _rv = LMGetKbdType();
417 _res = Py_BuildValue("b",
418 _rv);
419 return _res;
422 static PyObject *Evt_LMSetKbdType(PyObject *_self, PyObject *_args)
424 PyObject *_res = NULL;
425 UInt8 value;
426 #ifndef LMSetKbdType
427 PyMac_PRECHECK(LMSetKbdType);
428 #endif
429 if (!PyArg_ParseTuple(_args, "b",
430 &value))
431 return NULL;
432 LMSetKbdType(value);
433 Py_INCREF(Py_None);
434 _res = Py_None;
435 return _res;
438 static PyObject *Evt_TickCount(PyObject *_self, PyObject *_args)
440 PyObject *_res = NULL;
441 UInt32 _rv;
442 #ifndef TickCount
443 PyMac_PRECHECK(TickCount);
444 #endif
445 if (!PyArg_ParseTuple(_args, ""))
446 return NULL;
447 _rv = TickCount();
448 _res = Py_BuildValue("l",
449 _rv);
450 return _res;
453 static PyObject *Evt_WaitNextEvent(PyObject *_self, PyObject *_args)
455 PyObject *_res = NULL;
457 Boolean _rv;
458 EventMask eventMask;
459 EventRecord theEvent;
460 UInt32 sleep;
461 Handle mouseregion = (Handle)0;
463 if (!PyArg_ParseTuple(_args, "Hl|O&",
464 &eventMask,
465 &sleep,
466 OptResObj_Convert, &mouseregion))
467 return NULL;
468 _rv = WaitNextEvent(eventMask,
469 &theEvent,
470 sleep,
471 (RgnHandle)mouseregion);
472 _res = Py_BuildValue("bO&",
473 _rv,
474 PyMac_BuildEventRecord, &theEvent);
475 return _res;
479 static PyMethodDef Evt_methods[] = {
480 {"GetMouse", (PyCFunction)Evt_GetMouse, 1,
481 PyDoc_STR("() -> (Point mouseLoc)")},
482 {"Button", (PyCFunction)Evt_Button, 1,
483 PyDoc_STR("() -> (Boolean _rv)")},
484 {"StillDown", (PyCFunction)Evt_StillDown, 1,
485 PyDoc_STR("() -> (Boolean _rv)")},
486 {"WaitMouseUp", (PyCFunction)Evt_WaitMouseUp, 1,
487 PyDoc_STR("() -> (Boolean _rv)")},
488 {"GetCaretTime", (PyCFunction)Evt_GetCaretTime, 1,
489 PyDoc_STR("() -> (UInt32 _rv)")},
490 {"GetKeys", (PyCFunction)Evt_GetKeys, 1,
491 PyDoc_STR("() -> (KeyMap theKeys)")},
492 {"GetDblTime", (PyCFunction)Evt_GetDblTime, 1,
493 PyDoc_STR("() -> (UInt32 _rv)")},
494 {"SetEventMask", (PyCFunction)Evt_SetEventMask, 1,
495 PyDoc_STR("(EventMask value) -> None")},
496 {"GetNextEvent", (PyCFunction)Evt_GetNextEvent, 1,
497 PyDoc_STR("(EventMask eventMask) -> (Boolean _rv, EventRecord theEvent)")},
498 {"EventAvail", (PyCFunction)Evt_EventAvail, 1,
499 PyDoc_STR("(EventMask eventMask) -> (Boolean _rv, EventRecord theEvent)")},
500 {"PostEvent", (PyCFunction)Evt_PostEvent, 1,
501 PyDoc_STR("(EventKind eventNum, UInt32 eventMsg) -> None")},
502 {"FlushEvents", (PyCFunction)Evt_FlushEvents, 1,
503 PyDoc_STR("(EventMask whichMask, EventMask stopMask) -> None")},
504 {"GetGlobalMouse", (PyCFunction)Evt_GetGlobalMouse, 1,
505 PyDoc_STR("() -> (Point globalMouse)")},
506 {"GetCurrentKeyModifiers", (PyCFunction)Evt_GetCurrentKeyModifiers, 1,
507 PyDoc_STR("() -> (UInt32 _rv)")},
508 {"CheckEventQueueForUserCancel", (PyCFunction)Evt_CheckEventQueueForUserCancel, 1,
509 PyDoc_STR("() -> (Boolean _rv)")},
510 {"KeyScript", (PyCFunction)Evt_KeyScript, 1,
511 PyDoc_STR("(short code) -> None")},
512 {"IsCmdChar", (PyCFunction)Evt_IsCmdChar, 1,
513 PyDoc_STR("(EventRecord event, short test) -> (Boolean _rv)")},
514 {"LMGetKeyThresh", (PyCFunction)Evt_LMGetKeyThresh, 1,
515 PyDoc_STR("() -> (SInt16 _rv)")},
516 {"LMSetKeyThresh", (PyCFunction)Evt_LMSetKeyThresh, 1,
517 PyDoc_STR("(SInt16 value) -> None")},
518 {"LMGetKeyRepThresh", (PyCFunction)Evt_LMGetKeyRepThresh, 1,
519 PyDoc_STR("() -> (SInt16 _rv)")},
520 {"LMSetKeyRepThresh", (PyCFunction)Evt_LMSetKeyRepThresh, 1,
521 PyDoc_STR("(SInt16 value) -> None")},
522 {"LMGetKbdLast", (PyCFunction)Evt_LMGetKbdLast, 1,
523 PyDoc_STR("() -> (UInt8 _rv)")},
524 {"LMSetKbdLast", (PyCFunction)Evt_LMSetKbdLast, 1,
525 PyDoc_STR("(UInt8 value) -> None")},
526 {"LMGetKbdType", (PyCFunction)Evt_LMGetKbdType, 1,
527 PyDoc_STR("() -> (UInt8 _rv)")},
528 {"LMSetKbdType", (PyCFunction)Evt_LMSetKbdType, 1,
529 PyDoc_STR("(UInt8 value) -> None")},
530 {"TickCount", (PyCFunction)Evt_TickCount, 1,
531 PyDoc_STR("() -> (UInt32 _rv)")},
532 {"WaitNextEvent", (PyCFunction)Evt_WaitNextEvent, 1,
533 PyDoc_STR("(EventMask eventMask, UInt32 sleep [,RegionHandle]) -> (Boolean _rv, EventRecord theEvent)")},
534 {NULL, NULL, 0}
540 void init_Evt(void)
542 PyObject *m;
543 PyObject *d;
548 m = Py_InitModule("_Evt", Evt_methods);
549 d = PyModule_GetDict(m);
550 Evt_Error = PyMac_GetOSErrException();
551 if (Evt_Error == NULL ||
552 PyDict_SetItemString(d, "Error", Evt_Error) != 0)
553 return;
556 /* ======================== End module _Evt ========================= */