blob: bff6e1e56c7dcabfc6eac53730f7249b032fceb6 [file] [log] [blame]
Guido van Rossum17448e21995-01-30 11:53:55 +00001
2/* =========================== Module Evt =========================== */
3
4#include "Python.h"
5
6
7
Guido van Rossum17448e21995-01-30 11:53:55 +00008#include "macglue.h"
Jack Jansen9d8b96c2000-07-14 22:16:45 +00009#include "pymactoolbox.h"
Guido van Rossume26c2631995-02-28 09:11:41 +000010
Jack Jansenfa77e1a2001-05-22 21:56:42 +000011#ifdef WITHOUT_FRAMEWORKS
Guido van Rossum17448e21995-01-30 11:53:55 +000012#include <Events.h>
Jack Jansenfa77e1a2001-05-22 21:56:42 +000013#else
14#include <Carbon/Carbon.h>
15#endif
16
Guido van Rossum17448e21995-01-30 11:53:55 +000017
Guido van Rossum17448e21995-01-30 11:53:55 +000018static PyObject *Evt_Error;
19
Jack Jansenfa77e1a2001-05-22 21:56:42 +000020static PyObject *Evt_GetMouse(PyObject *_self, PyObject *_args)
Jack Jansen21f96871998-02-20 16:02:09 +000021{
22 PyObject *_res = NULL;
23 Point mouseLoc;
24 if (!PyArg_ParseTuple(_args, ""))
25 return NULL;
26 GetMouse(&mouseLoc);
27 _res = Py_BuildValue("O&",
28 PyMac_BuildPoint, mouseLoc);
29 return _res;
30}
31
Jack Jansenfa77e1a2001-05-22 21:56:42 +000032static PyObject *Evt_Button(PyObject *_self, PyObject *_args)
Jack Jansen21f96871998-02-20 16:02:09 +000033{
34 PyObject *_res = NULL;
35 Boolean _rv;
36 if (!PyArg_ParseTuple(_args, ""))
37 return NULL;
38 _rv = Button();
39 _res = Py_BuildValue("b",
40 _rv);
41 return _res;
42}
43
Jack Jansenfa77e1a2001-05-22 21:56:42 +000044static PyObject *Evt_StillDown(PyObject *_self, PyObject *_args)
Jack Jansen21f96871998-02-20 16:02:09 +000045{
46 PyObject *_res = NULL;
47 Boolean _rv;
48 if (!PyArg_ParseTuple(_args, ""))
49 return NULL;
50 _rv = StillDown();
51 _res = Py_BuildValue("b",
52 _rv);
53 return _res;
54}
55
Jack Jansenfa77e1a2001-05-22 21:56:42 +000056static PyObject *Evt_WaitMouseUp(PyObject *_self, PyObject *_args)
Jack Jansen21f96871998-02-20 16:02:09 +000057{
58 PyObject *_res = NULL;
59 Boolean _rv;
60 if (!PyArg_ParseTuple(_args, ""))
61 return NULL;
62 _rv = WaitMouseUp();
63 _res = Py_BuildValue("b",
64 _rv);
65 return _res;
66}
67
Jack Jansenfa77e1a2001-05-22 21:56:42 +000068static PyObject *Evt_TickCount(PyObject *_self, PyObject *_args)
Jack Jansen21f96871998-02-20 16:02:09 +000069{
70 PyObject *_res = NULL;
71 UInt32 _rv;
72 if (!PyArg_ParseTuple(_args, ""))
73 return NULL;
74 _rv = TickCount();
75 _res = Py_BuildValue("l",
76 _rv);
77 return _res;
78}
79
Jack Jansenfa77e1a2001-05-22 21:56:42 +000080static PyObject *Evt_GetCaretTime(PyObject *_self, PyObject *_args)
Jack Jansenb81cf9d1995-06-06 13:08:40 +000081{
82 PyObject *_res = NULL;
Jack Jansen7d0bc831995-06-09 20:56:31 +000083 UInt32 _rv;
Jack Jansenb81cf9d1995-06-06 13:08:40 +000084 if (!PyArg_ParseTuple(_args, ""))
85 return NULL;
86 _rv = GetCaretTime();
87 _res = Py_BuildValue("l",
88 _rv);
89 return _res;
90}
91
Jack Jansenfa77e1a2001-05-22 21:56:42 +000092static PyObject *Evt_GetKeys(PyObject *_self, PyObject *_args)
Jack Jansen21f96871998-02-20 16:02:09 +000093{
94 PyObject *_res = NULL;
95 KeyMap theKeys__out__;
96 if (!PyArg_ParseTuple(_args, ""))
97 return NULL;
98 GetKeys(theKeys__out__);
99 _res = Py_BuildValue("s#",
100 (char *)&theKeys__out__, (int)sizeof(KeyMap));
101 theKeys__error__: ;
102 return _res;
103}
104
Jack Jansenfa77e1a2001-05-22 21:56:42 +0000105static PyObject *Evt_GetDblTime(PyObject *_self, PyObject *_args)
Jack Jansen21f96871998-02-20 16:02:09 +0000106{
107 PyObject *_res = NULL;
108 UInt32 _rv;
109 if (!PyArg_ParseTuple(_args, ""))
110 return NULL;
111 _rv = GetDblTime();
112 _res = Py_BuildValue("l",
113 _rv);
114 return _res;
115}
116
Jack Jansenfa77e1a2001-05-22 21:56:42 +0000117static PyObject *Evt_SetEventMask(PyObject *_self, PyObject *_args)
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000118{
119 PyObject *_res = NULL;
Jack Jansend40f3c61995-10-09 23:12:22 +0000120 EventMask value;
Jack Jansen0b13e7c2000-07-07 13:09:35 +0000121 if (!PyArg_ParseTuple(_args, "H",
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000122 &value))
123 return NULL;
124 SetEventMask(value);
125 Py_INCREF(Py_None);
126 _res = Py_None;
127 return _res;
128}
129
Jack Jansenfa77e1a2001-05-22 21:56:42 +0000130static PyObject *Evt_GetNextEvent(PyObject *_self, PyObject *_args)
Guido van Rossum17448e21995-01-30 11:53:55 +0000131{
132 PyObject *_res = NULL;
133 Boolean _rv;
Jack Jansend40f3c61995-10-09 23:12:22 +0000134 EventMask eventMask;
Guido van Rossum17448e21995-01-30 11:53:55 +0000135 EventRecord theEvent;
Jack Jansen0b13e7c2000-07-07 13:09:35 +0000136 if (!PyArg_ParseTuple(_args, "H",
Guido van Rossum17448e21995-01-30 11:53:55 +0000137 &eventMask))
138 return NULL;
139 _rv = GetNextEvent(eventMask,
140 &theEvent);
141 _res = Py_BuildValue("bO&",
142 _rv,
143 PyMac_BuildEventRecord, &theEvent);
144 return _res;
145}
146
Jack Jansenfa77e1a2001-05-22 21:56:42 +0000147static PyObject *Evt_EventAvail(PyObject *_self, PyObject *_args)
Guido van Rossum17448e21995-01-30 11:53:55 +0000148{
149 PyObject *_res = NULL;
150 Boolean _rv;
Jack Jansend40f3c61995-10-09 23:12:22 +0000151 EventMask eventMask;
Guido van Rossum17448e21995-01-30 11:53:55 +0000152 EventRecord theEvent;
Jack Jansen0b13e7c2000-07-07 13:09:35 +0000153 if (!PyArg_ParseTuple(_args, "H",
Guido van Rossum17448e21995-01-30 11:53:55 +0000154 &eventMask))
155 return NULL;
156 _rv = EventAvail(eventMask,
157 &theEvent);
158 _res = Py_BuildValue("bO&",
159 _rv,
160 PyMac_BuildEventRecord, &theEvent);
161 return _res;
162}
163
Jack Jansenfa77e1a2001-05-22 21:56:42 +0000164static PyObject *Evt_PostEvent(PyObject *_self, PyObject *_args)
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000165{
166 PyObject *_res = NULL;
167 OSErr _err;
Jack Jansend40f3c61995-10-09 23:12:22 +0000168 EventKind eventNum;
Jack Jansen7d0bc831995-06-09 20:56:31 +0000169 UInt32 eventMsg;
Jack Jansen0b13e7c2000-07-07 13:09:35 +0000170 if (!PyArg_ParseTuple(_args, "Hl",
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000171 &eventNum,
172 &eventMsg))
173 return NULL;
174 _err = PostEvent(eventNum,
175 eventMsg);
176 if (_err != noErr) return PyMac_Error(_err);
177 Py_INCREF(Py_None);
178 _res = Py_None;
179 return _res;
180}
181
Jack Jansen74a1e632000-07-14 22:37:27 +0000182#if !TARGET_API_MAC_CARBON
Jack Jansenabf17032000-06-20 07:42:23 +0000183
Jack Jansenfa77e1a2001-05-22 21:56:42 +0000184static PyObject *Evt_OSEventAvail(PyObject *_self, PyObject *_args)
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000185{
186 PyObject *_res = NULL;
187 Boolean _rv;
Jack Jansend40f3c61995-10-09 23:12:22 +0000188 EventMask mask;
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000189 EventRecord theEvent;
Jack Jansen0b13e7c2000-07-07 13:09:35 +0000190 if (!PyArg_ParseTuple(_args, "H",
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000191 &mask))
192 return NULL;
193 _rv = OSEventAvail(mask,
194 &theEvent);
195 _res = Py_BuildValue("bO&",
196 _rv,
197 PyMac_BuildEventRecord, &theEvent);
198 return _res;
199}
Jack Jansenabf17032000-06-20 07:42:23 +0000200#endif
201
Jack Jansen74a1e632000-07-14 22:37:27 +0000202#if !TARGET_API_MAC_CARBON
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000203
Jack Jansenfa77e1a2001-05-22 21:56:42 +0000204static PyObject *Evt_GetOSEvent(PyObject *_self, PyObject *_args)
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000205{
206 PyObject *_res = NULL;
207 Boolean _rv;
Jack Jansend40f3c61995-10-09 23:12:22 +0000208 EventMask mask;
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000209 EventRecord theEvent;
Jack Jansen0b13e7c2000-07-07 13:09:35 +0000210 if (!PyArg_ParseTuple(_args, "H",
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000211 &mask))
212 return NULL;
213 _rv = GetOSEvent(mask,
214 &theEvent);
215 _res = Py_BuildValue("bO&",
216 _rv,
217 PyMac_BuildEventRecord, &theEvent);
218 return _res;
219}
Jack Jansenabf17032000-06-20 07:42:23 +0000220#endif
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000221
Jack Jansenfa77e1a2001-05-22 21:56:42 +0000222static PyObject *Evt_FlushEvents(PyObject *_self, PyObject *_args)
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000223{
224 PyObject *_res = NULL;
Jack Jansend40f3c61995-10-09 23:12:22 +0000225 EventMask whichMask;
226 EventMask stopMask;
Jack Jansen0b13e7c2000-07-07 13:09:35 +0000227 if (!PyArg_ParseTuple(_args, "HH",
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000228 &whichMask,
229 &stopMask))
230 return NULL;
231 FlushEvents(whichMask,
232 stopMask);
233 Py_INCREF(Py_None);
234 _res = Py_None;
235 return _res;
236}
237
Jack Jansen74a1e632000-07-14 22:37:27 +0000238#if !TARGET_API_MAC_CARBON
Jack Jansenabf17032000-06-20 07:42:23 +0000239
Jack Jansenfa77e1a2001-05-22 21:56:42 +0000240static PyObject *Evt_SystemClick(PyObject *_self, PyObject *_args)
Guido van Rossum17448e21995-01-30 11:53:55 +0000241{
242 PyObject *_res = NULL;
Guido van Rossume26c2631995-02-28 09:11:41 +0000243 EventRecord theEvent;
244 WindowPtr theWindow;
245 if (!PyArg_ParseTuple(_args, "O&O&",
246 PyMac_GetEventRecord, &theEvent,
247 WinObj_Convert, &theWindow))
Guido van Rossum17448e21995-01-30 11:53:55 +0000248 return NULL;
Guido van Rossume26c2631995-02-28 09:11:41 +0000249 SystemClick(&theEvent,
250 theWindow);
251 Py_INCREF(Py_None);
252 _res = Py_None;
Guido van Rossum17448e21995-01-30 11:53:55 +0000253 return _res;
254}
Jack Jansenabf17032000-06-20 07:42:23 +0000255#endif
256
Jack Jansen74a1e632000-07-14 22:37:27 +0000257#if !TARGET_API_MAC_CARBON
Guido van Rossum17448e21995-01-30 11:53:55 +0000258
Jack Jansenfa77e1a2001-05-22 21:56:42 +0000259static PyObject *Evt_SystemTask(PyObject *_self, PyObject *_args)
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000260{
261 PyObject *_res = NULL;
262 if (!PyArg_ParseTuple(_args, ""))
263 return NULL;
264 SystemTask();
265 Py_INCREF(Py_None);
266 _res = Py_None;
267 return _res;
268}
Jack Jansenabf17032000-06-20 07:42:23 +0000269#endif
270
Jack Jansen74a1e632000-07-14 22:37:27 +0000271#if !TARGET_API_MAC_CARBON
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000272
Jack Jansenfa77e1a2001-05-22 21:56:42 +0000273static PyObject *Evt_SystemEvent(PyObject *_self, PyObject *_args)
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000274{
275 PyObject *_res = NULL;
276 Boolean _rv;
277 EventRecord theEvent;
278 if (!PyArg_ParseTuple(_args, "O&",
279 PyMac_GetEventRecord, &theEvent))
280 return NULL;
281 _rv = SystemEvent(&theEvent);
282 _res = Py_BuildValue("b",
283 _rv);
284 return _res;
285}
Jack Jansenabf17032000-06-20 07:42:23 +0000286#endif
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000287
Jack Jansenf7d5aa62000-12-10 23:43:49 +0000288#if TARGET_API_MAC_CARBON
289
Jack Jansenfa77e1a2001-05-22 21:56:42 +0000290static PyObject *Evt_GetGlobalMouse(PyObject *_self, PyObject *_args)
Jack Jansenf7d5aa62000-12-10 23:43:49 +0000291{
292 PyObject *_res = NULL;
293 Point globalMouse;
294 if (!PyArg_ParseTuple(_args, ""))
295 return NULL;
296 GetGlobalMouse(&globalMouse);
297 _res = Py_BuildValue("O&",
298 PyMac_BuildPoint, globalMouse);
299 return _res;
300}
301#endif
302
303#if TARGET_API_MAC_CARBON
304
Jack Jansenfa77e1a2001-05-22 21:56:42 +0000305static PyObject *Evt_GetCurrentKeyModifiers(PyObject *_self, PyObject *_args)
Jack Jansenf7d5aa62000-12-10 23:43:49 +0000306{
307 PyObject *_res = NULL;
308 UInt32 _rv;
309 if (!PyArg_ParseTuple(_args, ""))
310 return NULL;
311 _rv = GetCurrentKeyModifiers();
312 _res = Py_BuildValue("l",
313 _rv);
314 return _res;
315}
316#endif
317
318#if TARGET_API_MAC_CARBON
319
Jack Jansenfa77e1a2001-05-22 21:56:42 +0000320static PyObject *Evt_CheckEventQueueForUserCancel(PyObject *_self, PyObject *_args)
Jack Jansenf7d5aa62000-12-10 23:43:49 +0000321{
322 PyObject *_res = NULL;
323 Boolean _rv;
324 if (!PyArg_ParseTuple(_args, ""))
325 return NULL;
326 _rv = CheckEventQueueForUserCancel();
327 _res = Py_BuildValue("b",
328 _rv);
329 return _res;
330}
331#endif
332
Jack Jansenfa77e1a2001-05-22 21:56:42 +0000333static PyObject *Evt_WaitNextEvent(PyObject *_self, PyObject *_args)
Jack Jansen91b5bed1998-09-17 15:28:58 +0000334{
335 PyObject *_res = NULL;
336
337 Boolean _rv;
338 EventMask eventMask;
339 EventRecord theEvent;
340 UInt32 sleep;
341 Handle mouseregion = (Handle)0;
342
Jack Jansen4fb67f42000-07-24 19:59:17 +0000343 if (!PyArg_ParseTuple(_args, "Hl|O&",
Jack Jansen91b5bed1998-09-17 15:28:58 +0000344 &eventMask,
345 &sleep,
346 OptResObj_Convert, &mouseregion))
347 return NULL;
348 _rv = WaitNextEvent(eventMask,
349 &theEvent,
350 sleep,
351 (RgnHandle)mouseregion);
352 _res = Py_BuildValue("bO&",
353 _rv,
354 PyMac_BuildEventRecord, &theEvent);
355 return _res;
356
357}
358
Guido van Rossum17448e21995-01-30 11:53:55 +0000359static PyMethodDef Evt_methods[] = {
Guido van Rossum17448e21995-01-30 11:53:55 +0000360 {"GetMouse", (PyCFunction)Evt_GetMouse, 1,
361 "() -> (Point mouseLoc)"},
362 {"Button", (PyCFunction)Evt_Button, 1,
363 "() -> (Boolean _rv)"},
364 {"StillDown", (PyCFunction)Evt_StillDown, 1,
365 "() -> (Boolean _rv)"},
366 {"WaitMouseUp", (PyCFunction)Evt_WaitMouseUp, 1,
367 "() -> (Boolean _rv)"},
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000368 {"TickCount", (PyCFunction)Evt_TickCount, 1,
Jack Jansen7d0bc831995-06-09 20:56:31 +0000369 "() -> (UInt32 _rv)"},
Jack Jansen21f96871998-02-20 16:02:09 +0000370 {"GetCaretTime", (PyCFunction)Evt_GetCaretTime, 1,
371 "() -> (UInt32 _rv)"},
372 {"GetKeys", (PyCFunction)Evt_GetKeys, 1,
373 "() -> (KeyMap theKeys)"},
374 {"GetDblTime", (PyCFunction)Evt_GetDblTime, 1,
375 "() -> (UInt32 _rv)"},
376 {"SetEventMask", (PyCFunction)Evt_SetEventMask, 1,
377 "(EventMask value) -> None"},
378 {"GetNextEvent", (PyCFunction)Evt_GetNextEvent, 1,
379 "(EventMask eventMask) -> (Boolean _rv, EventRecord theEvent)"},
Jack Jansen21f96871998-02-20 16:02:09 +0000380 {"EventAvail", (PyCFunction)Evt_EventAvail, 1,
381 "(EventMask eventMask) -> (Boolean _rv, EventRecord theEvent)"},
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000382 {"PostEvent", (PyCFunction)Evt_PostEvent, 1,
Jack Jansend40f3c61995-10-09 23:12:22 +0000383 "(EventKind eventNum, UInt32 eventMsg) -> None"},
Jack Jansenabf17032000-06-20 07:42:23 +0000384
Jack Jansen74a1e632000-07-14 22:37:27 +0000385#if !TARGET_API_MAC_CARBON
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000386 {"OSEventAvail", (PyCFunction)Evt_OSEventAvail, 1,
Jack Jansend40f3c61995-10-09 23:12:22 +0000387 "(EventMask mask) -> (Boolean _rv, EventRecord theEvent)"},
Jack Jansenabf17032000-06-20 07:42:23 +0000388#endif
389
Jack Jansen74a1e632000-07-14 22:37:27 +0000390#if !TARGET_API_MAC_CARBON
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000391 {"GetOSEvent", (PyCFunction)Evt_GetOSEvent, 1,
Jack Jansend40f3c61995-10-09 23:12:22 +0000392 "(EventMask mask) -> (Boolean _rv, EventRecord theEvent)"},
Jack Jansenabf17032000-06-20 07:42:23 +0000393#endif
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000394 {"FlushEvents", (PyCFunction)Evt_FlushEvents, 1,
Jack Jansend40f3c61995-10-09 23:12:22 +0000395 "(EventMask whichMask, EventMask stopMask) -> None"},
Jack Jansenabf17032000-06-20 07:42:23 +0000396
Jack Jansen74a1e632000-07-14 22:37:27 +0000397#if !TARGET_API_MAC_CARBON
Guido van Rossume26c2631995-02-28 09:11:41 +0000398 {"SystemClick", (PyCFunction)Evt_SystemClick, 1,
399 "(EventRecord theEvent, WindowPtr theWindow) -> None"},
Jack Jansenabf17032000-06-20 07:42:23 +0000400#endif
401
Jack Jansen74a1e632000-07-14 22:37:27 +0000402#if !TARGET_API_MAC_CARBON
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000403 {"SystemTask", (PyCFunction)Evt_SystemTask, 1,
404 "() -> None"},
Jack Jansenabf17032000-06-20 07:42:23 +0000405#endif
406
Jack Jansen74a1e632000-07-14 22:37:27 +0000407#if !TARGET_API_MAC_CARBON
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000408 {"SystemEvent", (PyCFunction)Evt_SystemEvent, 1,
409 "(EventRecord theEvent) -> (Boolean _rv)"},
Jack Jansenabf17032000-06-20 07:42:23 +0000410#endif
Jack Jansenf7d5aa62000-12-10 23:43:49 +0000411
412#if TARGET_API_MAC_CARBON
413 {"GetGlobalMouse", (PyCFunction)Evt_GetGlobalMouse, 1,
414 "() -> (Point globalMouse)"},
415#endif
416
417#if TARGET_API_MAC_CARBON
418 {"GetCurrentKeyModifiers", (PyCFunction)Evt_GetCurrentKeyModifiers, 1,
419 "() -> (UInt32 _rv)"},
420#endif
421
422#if TARGET_API_MAC_CARBON
423 {"CheckEventQueueForUserCancel", (PyCFunction)Evt_CheckEventQueueForUserCancel, 1,
424 "() -> (Boolean _rv)"},
425#endif
Jack Jansen91b5bed1998-09-17 15:28:58 +0000426 {"WaitNextEvent", (PyCFunction)Evt_WaitNextEvent, 1,
427 "(EventMask eventMask, UInt32 sleep [,RegionHandle]) -> (Boolean _rv, EventRecord theEvent)"},
Guido van Rossum17448e21995-01-30 11:53:55 +0000428 {NULL, NULL, 0}
429};
430
431
432
433
Jack Jansenfa77e1a2001-05-22 21:56:42 +0000434void initEvt(void)
Guido van Rossum17448e21995-01-30 11:53:55 +0000435{
436 PyObject *m;
437 PyObject *d;
438
439
440
441
442 m = Py_InitModule("Evt", Evt_methods);
443 d = PyModule_GetDict(m);
444 Evt_Error = PyMac_GetOSErrException();
445 if (Evt_Error == NULL ||
446 PyDict_SetItemString(d, "Error", Evt_Error) != 0)
Jack Jansenf7d5aa62000-12-10 23:43:49 +0000447 return;
Guido van Rossum17448e21995-01-30 11:53:55 +0000448}
449
450/* ========================= End module Evt ========================= */
451