blob: ed18f9c89419265a9b371780ef01c5ad40e7ac4f [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
Guido van Rossum17448e21995-01-30 11:53:55 +000011#include <Events.h>
12
Guido van Rossum17448e21995-01-30 11:53:55 +000013static PyObject *Evt_Error;
14
Jack Jansen21f96871998-02-20 16:02:09 +000015static PyObject *Evt_GetMouse(_self, _args)
16 PyObject *_self;
17 PyObject *_args;
18{
19 PyObject *_res = NULL;
20 Point mouseLoc;
21 if (!PyArg_ParseTuple(_args, ""))
22 return NULL;
23 GetMouse(&mouseLoc);
24 _res = Py_BuildValue("O&",
25 PyMac_BuildPoint, mouseLoc);
26 return _res;
27}
28
29static PyObject *Evt_Button(_self, _args)
30 PyObject *_self;
31 PyObject *_args;
32{
33 PyObject *_res = NULL;
34 Boolean _rv;
35 if (!PyArg_ParseTuple(_args, ""))
36 return NULL;
37 _rv = Button();
38 _res = Py_BuildValue("b",
39 _rv);
40 return _res;
41}
42
43static PyObject *Evt_StillDown(_self, _args)
44 PyObject *_self;
45 PyObject *_args;
46{
47 PyObject *_res = NULL;
48 Boolean _rv;
49 if (!PyArg_ParseTuple(_args, ""))
50 return NULL;
51 _rv = StillDown();
52 _res = Py_BuildValue("b",
53 _rv);
54 return _res;
55}
56
57static PyObject *Evt_WaitMouseUp(_self, _args)
58 PyObject *_self;
59 PyObject *_args;
60{
61 PyObject *_res = NULL;
62 Boolean _rv;
63 if (!PyArg_ParseTuple(_args, ""))
64 return NULL;
65 _rv = WaitMouseUp();
66 _res = Py_BuildValue("b",
67 _rv);
68 return _res;
69}
70
71static PyObject *Evt_TickCount(_self, _args)
72 PyObject *_self;
73 PyObject *_args;
74{
75 PyObject *_res = NULL;
76 UInt32 _rv;
77 if (!PyArg_ParseTuple(_args, ""))
78 return NULL;
79 _rv = TickCount();
80 _res = Py_BuildValue("l",
81 _rv);
82 return _res;
83}
84
Jack Jansenb81cf9d1995-06-06 13:08:40 +000085static PyObject *Evt_GetCaretTime(_self, _args)
86 PyObject *_self;
87 PyObject *_args;
88{
89 PyObject *_res = NULL;
Jack Jansen7d0bc831995-06-09 20:56:31 +000090 UInt32 _rv;
Jack Jansenb81cf9d1995-06-06 13:08:40 +000091 if (!PyArg_ParseTuple(_args, ""))
92 return NULL;
93 _rv = GetCaretTime();
94 _res = Py_BuildValue("l",
95 _rv);
96 return _res;
97}
98
Jack Jansen21f96871998-02-20 16:02:09 +000099static PyObject *Evt_GetKeys(_self, _args)
100 PyObject *_self;
101 PyObject *_args;
102{
103 PyObject *_res = NULL;
104 KeyMap theKeys__out__;
105 if (!PyArg_ParseTuple(_args, ""))
106 return NULL;
107 GetKeys(theKeys__out__);
108 _res = Py_BuildValue("s#",
109 (char *)&theKeys__out__, (int)sizeof(KeyMap));
110 theKeys__error__: ;
111 return _res;
112}
113
114static PyObject *Evt_GetDblTime(_self, _args)
115 PyObject *_self;
116 PyObject *_args;
117{
118 PyObject *_res = NULL;
119 UInt32 _rv;
120 if (!PyArg_ParseTuple(_args, ""))
121 return NULL;
122 _rv = GetDblTime();
123 _res = Py_BuildValue("l",
124 _rv);
125 return _res;
126}
127
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000128static PyObject *Evt_SetEventMask(_self, _args)
129 PyObject *_self;
130 PyObject *_args;
131{
132 PyObject *_res = NULL;
Jack Jansend40f3c61995-10-09 23:12:22 +0000133 EventMask value;
Jack Jansen0b13e7c2000-07-07 13:09:35 +0000134 if (!PyArg_ParseTuple(_args, "H",
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000135 &value))
136 return NULL;
137 SetEventMask(value);
138 Py_INCREF(Py_None);
139 _res = Py_None;
140 return _res;
141}
142
Guido van Rossum17448e21995-01-30 11:53:55 +0000143static PyObject *Evt_GetNextEvent(_self, _args)
144 PyObject *_self;
145 PyObject *_args;
146{
147 PyObject *_res = NULL;
148 Boolean _rv;
Jack Jansend40f3c61995-10-09 23:12:22 +0000149 EventMask eventMask;
Guido van Rossum17448e21995-01-30 11:53:55 +0000150 EventRecord theEvent;
Jack Jansen0b13e7c2000-07-07 13:09:35 +0000151 if (!PyArg_ParseTuple(_args, "H",
Guido van Rossum17448e21995-01-30 11:53:55 +0000152 &eventMask))
153 return NULL;
154 _rv = GetNextEvent(eventMask,
155 &theEvent);
156 _res = Py_BuildValue("bO&",
157 _rv,
158 PyMac_BuildEventRecord, &theEvent);
159 return _res;
160}
161
Guido van Rossum17448e21995-01-30 11:53:55 +0000162static PyObject *Evt_EventAvail(_self, _args)
163 PyObject *_self;
164 PyObject *_args;
165{
166 PyObject *_res = NULL;
167 Boolean _rv;
Jack Jansend40f3c61995-10-09 23:12:22 +0000168 EventMask eventMask;
Guido van Rossum17448e21995-01-30 11:53:55 +0000169 EventRecord theEvent;
Jack Jansen0b13e7c2000-07-07 13:09:35 +0000170 if (!PyArg_ParseTuple(_args, "H",
Guido van Rossum17448e21995-01-30 11:53:55 +0000171 &eventMask))
172 return NULL;
173 _rv = EventAvail(eventMask,
174 &theEvent);
175 _res = Py_BuildValue("bO&",
176 _rv,
177 PyMac_BuildEventRecord, &theEvent);
178 return _res;
179}
180
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000181static PyObject *Evt_PostEvent(_self, _args)
182 PyObject *_self;
183 PyObject *_args;
184{
185 PyObject *_res = NULL;
186 OSErr _err;
Jack Jansend40f3c61995-10-09 23:12:22 +0000187 EventKind eventNum;
Jack Jansen7d0bc831995-06-09 20:56:31 +0000188 UInt32 eventMsg;
Jack Jansen0b13e7c2000-07-07 13:09:35 +0000189 if (!PyArg_ParseTuple(_args, "Hl",
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000190 &eventNum,
191 &eventMsg))
192 return NULL;
193 _err = PostEvent(eventNum,
194 eventMsg);
195 if (_err != noErr) return PyMac_Error(_err);
196 Py_INCREF(Py_None);
197 _res = Py_None;
198 return _res;
199}
200
Jack Jansen74a1e632000-07-14 22:37:27 +0000201#if !TARGET_API_MAC_CARBON
Jack Jansenabf17032000-06-20 07:42:23 +0000202
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000203static PyObject *Evt_OSEventAvail(_self, _args)
204 PyObject *_self;
205 PyObject *_args;
206{
207 PyObject *_res = NULL;
208 Boolean _rv;
Jack Jansend40f3c61995-10-09 23:12:22 +0000209 EventMask mask;
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000210 EventRecord theEvent;
Jack Jansen0b13e7c2000-07-07 13:09:35 +0000211 if (!PyArg_ParseTuple(_args, "H",
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000212 &mask))
213 return NULL;
214 _rv = OSEventAvail(mask,
215 &theEvent);
216 _res = Py_BuildValue("bO&",
217 _rv,
218 PyMac_BuildEventRecord, &theEvent);
219 return _res;
220}
Jack Jansenabf17032000-06-20 07:42:23 +0000221#endif
222
Jack Jansen74a1e632000-07-14 22:37:27 +0000223#if !TARGET_API_MAC_CARBON
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000224
225static PyObject *Evt_GetOSEvent(_self, _args)
226 PyObject *_self;
227 PyObject *_args;
228{
229 PyObject *_res = NULL;
230 Boolean _rv;
Jack Jansend40f3c61995-10-09 23:12:22 +0000231 EventMask mask;
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000232 EventRecord theEvent;
Jack Jansen0b13e7c2000-07-07 13:09:35 +0000233 if (!PyArg_ParseTuple(_args, "H",
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000234 &mask))
235 return NULL;
236 _rv = GetOSEvent(mask,
237 &theEvent);
238 _res = Py_BuildValue("bO&",
239 _rv,
240 PyMac_BuildEventRecord, &theEvent);
241 return _res;
242}
Jack Jansenabf17032000-06-20 07:42:23 +0000243#endif
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000244
245static PyObject *Evt_FlushEvents(_self, _args)
246 PyObject *_self;
247 PyObject *_args;
248{
249 PyObject *_res = NULL;
Jack Jansend40f3c61995-10-09 23:12:22 +0000250 EventMask whichMask;
251 EventMask stopMask;
Jack Jansen0b13e7c2000-07-07 13:09:35 +0000252 if (!PyArg_ParseTuple(_args, "HH",
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000253 &whichMask,
254 &stopMask))
255 return NULL;
256 FlushEvents(whichMask,
257 stopMask);
258 Py_INCREF(Py_None);
259 _res = Py_None;
260 return _res;
261}
262
Jack Jansen74a1e632000-07-14 22:37:27 +0000263#if !TARGET_API_MAC_CARBON
Jack Jansenabf17032000-06-20 07:42:23 +0000264
Guido van Rossume26c2631995-02-28 09:11:41 +0000265static PyObject *Evt_SystemClick(_self, _args)
Guido van Rossum17448e21995-01-30 11:53:55 +0000266 PyObject *_self;
267 PyObject *_args;
268{
269 PyObject *_res = NULL;
Guido van Rossume26c2631995-02-28 09:11:41 +0000270 EventRecord theEvent;
271 WindowPtr theWindow;
272 if (!PyArg_ParseTuple(_args, "O&O&",
273 PyMac_GetEventRecord, &theEvent,
274 WinObj_Convert, &theWindow))
Guido van Rossum17448e21995-01-30 11:53:55 +0000275 return NULL;
Guido van Rossume26c2631995-02-28 09:11:41 +0000276 SystemClick(&theEvent,
277 theWindow);
278 Py_INCREF(Py_None);
279 _res = Py_None;
Guido van Rossum17448e21995-01-30 11:53:55 +0000280 return _res;
281}
Jack Jansenabf17032000-06-20 07:42:23 +0000282#endif
283
Jack Jansen74a1e632000-07-14 22:37:27 +0000284#if !TARGET_API_MAC_CARBON
Guido van Rossum17448e21995-01-30 11:53:55 +0000285
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000286static PyObject *Evt_SystemTask(_self, _args)
287 PyObject *_self;
288 PyObject *_args;
289{
290 PyObject *_res = NULL;
291 if (!PyArg_ParseTuple(_args, ""))
292 return NULL;
293 SystemTask();
294 Py_INCREF(Py_None);
295 _res = Py_None;
296 return _res;
297}
Jack Jansenabf17032000-06-20 07:42:23 +0000298#endif
299
Jack Jansen74a1e632000-07-14 22:37:27 +0000300#if !TARGET_API_MAC_CARBON
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000301
302static PyObject *Evt_SystemEvent(_self, _args)
303 PyObject *_self;
304 PyObject *_args;
305{
306 PyObject *_res = NULL;
307 Boolean _rv;
308 EventRecord theEvent;
309 if (!PyArg_ParseTuple(_args, "O&",
310 PyMac_GetEventRecord, &theEvent))
311 return NULL;
312 _rv = SystemEvent(&theEvent);
313 _res = Py_BuildValue("b",
314 _rv);
315 return _res;
316}
Jack Jansenabf17032000-06-20 07:42:23 +0000317#endif
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000318
Jack Jansenf7d5aa62000-12-10 23:43:49 +0000319#if TARGET_API_MAC_CARBON
320
321static PyObject *Evt_GetGlobalMouse(_self, _args)
322 PyObject *_self;
323 PyObject *_args;
324{
325 PyObject *_res = NULL;
326 Point globalMouse;
327 if (!PyArg_ParseTuple(_args, ""))
328 return NULL;
329 GetGlobalMouse(&globalMouse);
330 _res = Py_BuildValue("O&",
331 PyMac_BuildPoint, globalMouse);
332 return _res;
333}
334#endif
335
336#if TARGET_API_MAC_CARBON
337
338static PyObject *Evt_GetCurrentKeyModifiers(_self, _args)
339 PyObject *_self;
340 PyObject *_args;
341{
342 PyObject *_res = NULL;
343 UInt32 _rv;
344 if (!PyArg_ParseTuple(_args, ""))
345 return NULL;
346 _rv = GetCurrentKeyModifiers();
347 _res = Py_BuildValue("l",
348 _rv);
349 return _res;
350}
351#endif
352
353#if TARGET_API_MAC_CARBON
354
355static PyObject *Evt_CheckEventQueueForUserCancel(_self, _args)
356 PyObject *_self;
357 PyObject *_args;
358{
359 PyObject *_res = NULL;
360 Boolean _rv;
361 if (!PyArg_ParseTuple(_args, ""))
362 return NULL;
363 _rv = CheckEventQueueForUserCancel();
364 _res = Py_BuildValue("b",
365 _rv);
366 return _res;
367}
368#endif
369
Jack Jansen91b5bed1998-09-17 15:28:58 +0000370static PyObject *Evt_WaitNextEvent(_self, _args)
371 PyObject *_self;
372 PyObject *_args;
373{
374 PyObject *_res = NULL;
375
376 Boolean _rv;
377 EventMask eventMask;
378 EventRecord theEvent;
379 UInt32 sleep;
380 Handle mouseregion = (Handle)0;
381
Jack Jansen4fb67f42000-07-24 19:59:17 +0000382 if (!PyArg_ParseTuple(_args, "Hl|O&",
Jack Jansen91b5bed1998-09-17 15:28:58 +0000383 &eventMask,
384 &sleep,
385 OptResObj_Convert, &mouseregion))
386 return NULL;
387 _rv = WaitNextEvent(eventMask,
388 &theEvent,
389 sleep,
390 (RgnHandle)mouseregion);
391 _res = Py_BuildValue("bO&",
392 _rv,
393 PyMac_BuildEventRecord, &theEvent);
394 return _res;
395
396}
397
Guido van Rossum17448e21995-01-30 11:53:55 +0000398static PyMethodDef Evt_methods[] = {
Guido van Rossum17448e21995-01-30 11:53:55 +0000399 {"GetMouse", (PyCFunction)Evt_GetMouse, 1,
400 "() -> (Point mouseLoc)"},
401 {"Button", (PyCFunction)Evt_Button, 1,
402 "() -> (Boolean _rv)"},
403 {"StillDown", (PyCFunction)Evt_StillDown, 1,
404 "() -> (Boolean _rv)"},
405 {"WaitMouseUp", (PyCFunction)Evt_WaitMouseUp, 1,
406 "() -> (Boolean _rv)"},
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000407 {"TickCount", (PyCFunction)Evt_TickCount, 1,
Jack Jansen7d0bc831995-06-09 20:56:31 +0000408 "() -> (UInt32 _rv)"},
Jack Jansen21f96871998-02-20 16:02:09 +0000409 {"GetCaretTime", (PyCFunction)Evt_GetCaretTime, 1,
410 "() -> (UInt32 _rv)"},
411 {"GetKeys", (PyCFunction)Evt_GetKeys, 1,
412 "() -> (KeyMap theKeys)"},
413 {"GetDblTime", (PyCFunction)Evt_GetDblTime, 1,
414 "() -> (UInt32 _rv)"},
415 {"SetEventMask", (PyCFunction)Evt_SetEventMask, 1,
416 "(EventMask value) -> None"},
417 {"GetNextEvent", (PyCFunction)Evt_GetNextEvent, 1,
418 "(EventMask eventMask) -> (Boolean _rv, EventRecord theEvent)"},
Jack Jansen21f96871998-02-20 16:02:09 +0000419 {"EventAvail", (PyCFunction)Evt_EventAvail, 1,
420 "(EventMask eventMask) -> (Boolean _rv, EventRecord theEvent)"},
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000421 {"PostEvent", (PyCFunction)Evt_PostEvent, 1,
Jack Jansend40f3c61995-10-09 23:12:22 +0000422 "(EventKind eventNum, UInt32 eventMsg) -> None"},
Jack Jansenabf17032000-06-20 07:42:23 +0000423
Jack Jansen74a1e632000-07-14 22:37:27 +0000424#if !TARGET_API_MAC_CARBON
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000425 {"OSEventAvail", (PyCFunction)Evt_OSEventAvail, 1,
Jack Jansend40f3c61995-10-09 23:12:22 +0000426 "(EventMask mask) -> (Boolean _rv, EventRecord theEvent)"},
Jack Jansenabf17032000-06-20 07:42:23 +0000427#endif
428
Jack Jansen74a1e632000-07-14 22:37:27 +0000429#if !TARGET_API_MAC_CARBON
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000430 {"GetOSEvent", (PyCFunction)Evt_GetOSEvent, 1,
Jack Jansend40f3c61995-10-09 23:12:22 +0000431 "(EventMask mask) -> (Boolean _rv, EventRecord theEvent)"},
Jack Jansenabf17032000-06-20 07:42:23 +0000432#endif
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000433 {"FlushEvents", (PyCFunction)Evt_FlushEvents, 1,
Jack Jansend40f3c61995-10-09 23:12:22 +0000434 "(EventMask whichMask, EventMask stopMask) -> None"},
Jack Jansenabf17032000-06-20 07:42:23 +0000435
Jack Jansen74a1e632000-07-14 22:37:27 +0000436#if !TARGET_API_MAC_CARBON
Guido van Rossume26c2631995-02-28 09:11:41 +0000437 {"SystemClick", (PyCFunction)Evt_SystemClick, 1,
438 "(EventRecord theEvent, WindowPtr theWindow) -> None"},
Jack Jansenabf17032000-06-20 07:42:23 +0000439#endif
440
Jack Jansen74a1e632000-07-14 22:37:27 +0000441#if !TARGET_API_MAC_CARBON
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000442 {"SystemTask", (PyCFunction)Evt_SystemTask, 1,
443 "() -> None"},
Jack Jansenabf17032000-06-20 07:42:23 +0000444#endif
445
Jack Jansen74a1e632000-07-14 22:37:27 +0000446#if !TARGET_API_MAC_CARBON
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000447 {"SystemEvent", (PyCFunction)Evt_SystemEvent, 1,
448 "(EventRecord theEvent) -> (Boolean _rv)"},
Jack Jansenabf17032000-06-20 07:42:23 +0000449#endif
Jack Jansenf7d5aa62000-12-10 23:43:49 +0000450
451#if TARGET_API_MAC_CARBON
452 {"GetGlobalMouse", (PyCFunction)Evt_GetGlobalMouse, 1,
453 "() -> (Point globalMouse)"},
454#endif
455
456#if TARGET_API_MAC_CARBON
457 {"GetCurrentKeyModifiers", (PyCFunction)Evt_GetCurrentKeyModifiers, 1,
458 "() -> (UInt32 _rv)"},
459#endif
460
461#if TARGET_API_MAC_CARBON
462 {"CheckEventQueueForUserCancel", (PyCFunction)Evt_CheckEventQueueForUserCancel, 1,
463 "() -> (Boolean _rv)"},
464#endif
Jack Jansen91b5bed1998-09-17 15:28:58 +0000465 {"WaitNextEvent", (PyCFunction)Evt_WaitNextEvent, 1,
466 "(EventMask eventMask, UInt32 sleep [,RegionHandle]) -> (Boolean _rv, EventRecord theEvent)"},
Guido van Rossum17448e21995-01-30 11:53:55 +0000467 {NULL, NULL, 0}
468};
469
470
471
472
473void initEvt()
474{
475 PyObject *m;
476 PyObject *d;
477
478
479
480
481 m = Py_InitModule("Evt", Evt_methods);
482 d = PyModule_GetDict(m);
483 Evt_Error = PyMac_GetOSErrException();
484 if (Evt_Error == NULL ||
485 PyDict_SetItemString(d, "Error", Evt_Error) != 0)
Jack Jansenf7d5aa62000-12-10 23:43:49 +0000486 return;
Guido van Rossum17448e21995-01-30 11:53:55 +0000487}
488
489/* ========================= End module Evt ========================= */
490