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