blob: 7d5d2f207eb338c5ae2f00bae6fc220dc0f75b8b [file] [log] [blame]
Guido van Rossum17448e21995-01-30 11:53:55 +00001
2/* =========================== Module Evt =========================== */
3
4#include "Python.h"
5
6
7
8#define SystemSevenOrLater 1
9
10#include "macglue.h"
11#include <Memory.h>
12#include <Dialogs.h>
13#include <Menus.h>
14#include <Controls.h>
15
16extern PyObject *ResObj_New(Handle);
17extern int ResObj_Convert(PyObject *, Handle *);
Jack Jansen425e9eb1995-12-12 15:02:03 +000018extern PyObject *OptResObj_New(Handle);
19extern int OptResObj_Convert(PyObject *, Handle *);
Guido van Rossum17448e21995-01-30 11:53:55 +000020
21extern PyObject *WinObj_New(WindowPtr);
22extern int WinObj_Convert(PyObject *, WindowPtr *);
Jack Jansen425e9eb1995-12-12 15:02:03 +000023extern PyTypeObject Window_Type;
24#define WinObj_Check(x) ((x)->ob_type == &Window_Type)
Guido van Rossum17448e21995-01-30 11:53:55 +000025
26extern PyObject *DlgObj_New(DialogPtr);
27extern int DlgObj_Convert(PyObject *, DialogPtr *);
28extern PyTypeObject Dialog_Type;
29#define DlgObj_Check(x) ((x)->ob_type == &Dialog_Type)
30
31extern PyObject *MenuObj_New(MenuHandle);
32extern int MenuObj_Convert(PyObject *, MenuHandle *);
33
34extern PyObject *CtlObj_New(ControlHandle);
35extern int CtlObj_Convert(PyObject *, ControlHandle *);
36
Jack Jansen425e9eb1995-12-12 15:02:03 +000037extern PyObject *GrafObj_New(GrafPtr);
38extern int GrafObj_Convert(PyObject *, GrafPtr *);
39
40extern PyObject *BMObj_New(BitMapPtr);
41extern int BMObj_Convert(PyObject *, BitMapPtr *);
42
Guido van Rossume26c2631995-02-28 09:11:41 +000043extern PyObject *WinObj_WhichWindow(WindowPtr);
44
Guido van Rossum17448e21995-01-30 11:53:55 +000045#include <Events.h>
46
47#define resNotFound -192 /* Can't include <Errors.h> because of Python's "errors.h" */
48
49static PyObject *Evt_Error;
50
Jack Jansen21f96871998-02-20 16:02:09 +000051static PyObject *Evt_GetMouse(_self, _args)
52 PyObject *_self;
53 PyObject *_args;
54{
55 PyObject *_res = NULL;
56 Point mouseLoc;
57 if (!PyArg_ParseTuple(_args, ""))
58 return NULL;
59 GetMouse(&mouseLoc);
60 _res = Py_BuildValue("O&",
61 PyMac_BuildPoint, mouseLoc);
62 return _res;
63}
64
65static PyObject *Evt_Button(_self, _args)
66 PyObject *_self;
67 PyObject *_args;
68{
69 PyObject *_res = NULL;
70 Boolean _rv;
71 if (!PyArg_ParseTuple(_args, ""))
72 return NULL;
73 _rv = Button();
74 _res = Py_BuildValue("b",
75 _rv);
76 return _res;
77}
78
79static PyObject *Evt_StillDown(_self, _args)
80 PyObject *_self;
81 PyObject *_args;
82{
83 PyObject *_res = NULL;
84 Boolean _rv;
85 if (!PyArg_ParseTuple(_args, ""))
86 return NULL;
87 _rv = StillDown();
88 _res = Py_BuildValue("b",
89 _rv);
90 return _res;
91}
92
93static PyObject *Evt_WaitMouseUp(_self, _args)
94 PyObject *_self;
95 PyObject *_args;
96{
97 PyObject *_res = NULL;
98 Boolean _rv;
99 if (!PyArg_ParseTuple(_args, ""))
100 return NULL;
101 _rv = WaitMouseUp();
102 _res = Py_BuildValue("b",
103 _rv);
104 return _res;
105}
106
107static PyObject *Evt_TickCount(_self, _args)
108 PyObject *_self;
109 PyObject *_args;
110{
111 PyObject *_res = NULL;
112 UInt32 _rv;
113 if (!PyArg_ParseTuple(_args, ""))
114 return NULL;
115 _rv = TickCount();
116 _res = Py_BuildValue("l",
117 _rv);
118 return _res;
119}
120
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000121static PyObject *Evt_GetCaretTime(_self, _args)
122 PyObject *_self;
123 PyObject *_args;
124{
125 PyObject *_res = NULL;
Jack Jansen7d0bc831995-06-09 20:56:31 +0000126 UInt32 _rv;
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000127 if (!PyArg_ParseTuple(_args, ""))
128 return NULL;
129 _rv = GetCaretTime();
130 _res = Py_BuildValue("l",
131 _rv);
132 return _res;
133}
134
Jack Jansen21f96871998-02-20 16:02:09 +0000135static PyObject *Evt_GetKeys(_self, _args)
136 PyObject *_self;
137 PyObject *_args;
138{
139 PyObject *_res = NULL;
140 KeyMap theKeys__out__;
141 if (!PyArg_ParseTuple(_args, ""))
142 return NULL;
143 GetKeys(theKeys__out__);
144 _res = Py_BuildValue("s#",
145 (char *)&theKeys__out__, (int)sizeof(KeyMap));
146 theKeys__error__: ;
147 return _res;
148}
149
150static PyObject *Evt_GetDblTime(_self, _args)
151 PyObject *_self;
152 PyObject *_args;
153{
154 PyObject *_res = NULL;
155 UInt32 _rv;
156 if (!PyArg_ParseTuple(_args, ""))
157 return NULL;
158 _rv = GetDblTime();
159 _res = Py_BuildValue("l",
160 _rv);
161 return _res;
162}
163
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000164static PyObject *Evt_SetEventMask(_self, _args)
165 PyObject *_self;
166 PyObject *_args;
167{
168 PyObject *_res = NULL;
Jack Jansend40f3c61995-10-09 23:12:22 +0000169 EventMask value;
Jack Jansen0b13e7c2000-07-07 13:09:35 +0000170 if (!PyArg_ParseTuple(_args, "H",
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000171 &value))
172 return NULL;
173 SetEventMask(value);
174 Py_INCREF(Py_None);
175 _res = Py_None;
176 return _res;
177}
178
Guido van Rossum17448e21995-01-30 11:53:55 +0000179static PyObject *Evt_GetNextEvent(_self, _args)
180 PyObject *_self;
181 PyObject *_args;
182{
183 PyObject *_res = NULL;
184 Boolean _rv;
Jack Jansend40f3c61995-10-09 23:12:22 +0000185 EventMask eventMask;
Guido van Rossum17448e21995-01-30 11:53:55 +0000186 EventRecord theEvent;
Jack Jansen0b13e7c2000-07-07 13:09:35 +0000187 if (!PyArg_ParseTuple(_args, "H",
Guido van Rossum17448e21995-01-30 11:53:55 +0000188 &eventMask))
189 return NULL;
190 _rv = GetNextEvent(eventMask,
191 &theEvent);
192 _res = Py_BuildValue("bO&",
193 _rv,
194 PyMac_BuildEventRecord, &theEvent);
195 return _res;
196}
197
Guido van Rossum17448e21995-01-30 11:53:55 +0000198static PyObject *Evt_EventAvail(_self, _args)
199 PyObject *_self;
200 PyObject *_args;
201{
202 PyObject *_res = NULL;
203 Boolean _rv;
Jack Jansend40f3c61995-10-09 23:12:22 +0000204 EventMask eventMask;
Guido van Rossum17448e21995-01-30 11:53:55 +0000205 EventRecord theEvent;
Jack Jansen0b13e7c2000-07-07 13:09:35 +0000206 if (!PyArg_ParseTuple(_args, "H",
Guido van Rossum17448e21995-01-30 11:53:55 +0000207 &eventMask))
208 return NULL;
209 _rv = EventAvail(eventMask,
210 &theEvent);
211 _res = Py_BuildValue("bO&",
212 _rv,
213 PyMac_BuildEventRecord, &theEvent);
214 return _res;
215}
216
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000217static PyObject *Evt_PostEvent(_self, _args)
218 PyObject *_self;
219 PyObject *_args;
220{
221 PyObject *_res = NULL;
222 OSErr _err;
Jack Jansend40f3c61995-10-09 23:12:22 +0000223 EventKind eventNum;
Jack Jansen7d0bc831995-06-09 20:56:31 +0000224 UInt32 eventMsg;
Jack Jansen0b13e7c2000-07-07 13:09:35 +0000225 if (!PyArg_ParseTuple(_args, "Hl",
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000226 &eventNum,
227 &eventMsg))
228 return NULL;
229 _err = PostEvent(eventNum,
230 eventMsg);
231 if (_err != noErr) return PyMac_Error(_err);
232 Py_INCREF(Py_None);
233 _res = Py_None;
234 return _res;
235}
236
Jack Jansenabf17032000-06-20 07:42:23 +0000237#ifndef TARGET_API_MAC_CARBON
238
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000239static PyObject *Evt_OSEventAvail(_self, _args)
240 PyObject *_self;
241 PyObject *_args;
242{
243 PyObject *_res = NULL;
244 Boolean _rv;
Jack Jansend40f3c61995-10-09 23:12:22 +0000245 EventMask mask;
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000246 EventRecord theEvent;
Jack Jansen0b13e7c2000-07-07 13:09:35 +0000247 if (!PyArg_ParseTuple(_args, "H",
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000248 &mask))
249 return NULL;
250 _rv = OSEventAvail(mask,
251 &theEvent);
252 _res = Py_BuildValue("bO&",
253 _rv,
254 PyMac_BuildEventRecord, &theEvent);
255 return _res;
256}
Jack Jansenabf17032000-06-20 07:42:23 +0000257#endif
258
259#ifndef TARGET_API_MAC_CARBON
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000260
261static PyObject *Evt_GetOSEvent(_self, _args)
262 PyObject *_self;
263 PyObject *_args;
264{
265 PyObject *_res = NULL;
266 Boolean _rv;
Jack Jansend40f3c61995-10-09 23:12:22 +0000267 EventMask mask;
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000268 EventRecord theEvent;
Jack Jansen0b13e7c2000-07-07 13:09:35 +0000269 if (!PyArg_ParseTuple(_args, "H",
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000270 &mask))
271 return NULL;
272 _rv = GetOSEvent(mask,
273 &theEvent);
274 _res = Py_BuildValue("bO&",
275 _rv,
276 PyMac_BuildEventRecord, &theEvent);
277 return _res;
278}
Jack Jansenabf17032000-06-20 07:42:23 +0000279#endif
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000280
281static PyObject *Evt_FlushEvents(_self, _args)
282 PyObject *_self;
283 PyObject *_args;
284{
285 PyObject *_res = NULL;
Jack Jansend40f3c61995-10-09 23:12:22 +0000286 EventMask whichMask;
287 EventMask stopMask;
Jack Jansen0b13e7c2000-07-07 13:09:35 +0000288 if (!PyArg_ParseTuple(_args, "HH",
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000289 &whichMask,
290 &stopMask))
291 return NULL;
292 FlushEvents(whichMask,
293 stopMask);
294 Py_INCREF(Py_None);
295 _res = Py_None;
296 return _res;
297}
298
Jack Jansenabf17032000-06-20 07:42:23 +0000299#ifndef TARGET_API_MAC_CARBON
300
Guido van Rossume26c2631995-02-28 09:11:41 +0000301static PyObject *Evt_SystemClick(_self, _args)
Guido van Rossum17448e21995-01-30 11:53:55 +0000302 PyObject *_self;
303 PyObject *_args;
304{
305 PyObject *_res = NULL;
Guido van Rossume26c2631995-02-28 09:11:41 +0000306 EventRecord theEvent;
307 WindowPtr theWindow;
308 if (!PyArg_ParseTuple(_args, "O&O&",
309 PyMac_GetEventRecord, &theEvent,
310 WinObj_Convert, &theWindow))
Guido van Rossum17448e21995-01-30 11:53:55 +0000311 return NULL;
Guido van Rossume26c2631995-02-28 09:11:41 +0000312 SystemClick(&theEvent,
313 theWindow);
314 Py_INCREF(Py_None);
315 _res = Py_None;
Guido van Rossum17448e21995-01-30 11:53:55 +0000316 return _res;
317}
Jack Jansenabf17032000-06-20 07:42:23 +0000318#endif
319
320#ifndef TARGET_API_MAC_CARBON
Guido van Rossum17448e21995-01-30 11:53:55 +0000321
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000322static PyObject *Evt_SystemTask(_self, _args)
323 PyObject *_self;
324 PyObject *_args;
325{
326 PyObject *_res = NULL;
327 if (!PyArg_ParseTuple(_args, ""))
328 return NULL;
329 SystemTask();
330 Py_INCREF(Py_None);
331 _res = Py_None;
332 return _res;
333}
Jack Jansenabf17032000-06-20 07:42:23 +0000334#endif
335
336#ifndef TARGET_API_MAC_CARBON
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000337
338static PyObject *Evt_SystemEvent(_self, _args)
339 PyObject *_self;
340 PyObject *_args;
341{
342 PyObject *_res = NULL;
343 Boolean _rv;
344 EventRecord theEvent;
345 if (!PyArg_ParseTuple(_args, "O&",
346 PyMac_GetEventRecord, &theEvent))
347 return NULL;
348 _rv = SystemEvent(&theEvent);
349 _res = Py_BuildValue("b",
350 _rv);
351 return _res;
352}
Jack Jansenabf17032000-06-20 07:42:23 +0000353#endif
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000354
Jack Jansen91b5bed1998-09-17 15:28:58 +0000355static PyObject *Evt_WaitNextEvent(_self, _args)
356 PyObject *_self;
357 PyObject *_args;
358{
359 PyObject *_res = NULL;
360
361 Boolean _rv;
362 EventMask eventMask;
363 EventRecord theEvent;
364 UInt32 sleep;
365 Handle mouseregion = (Handle)0;
366
367 if (!PyArg_ParseTuple(_args, "hl|O&",
368 &eventMask,
369 &sleep,
370 OptResObj_Convert, &mouseregion))
371 return NULL;
372 _rv = WaitNextEvent(eventMask,
373 &theEvent,
374 sleep,
375 (RgnHandle)mouseregion);
376 _res = Py_BuildValue("bO&",
377 _rv,
378 PyMac_BuildEventRecord, &theEvent);
379 return _res;
380
381}
382
Guido van Rossum17448e21995-01-30 11:53:55 +0000383static PyMethodDef Evt_methods[] = {
Guido van Rossum17448e21995-01-30 11:53:55 +0000384 {"GetMouse", (PyCFunction)Evt_GetMouse, 1,
385 "() -> (Point mouseLoc)"},
386 {"Button", (PyCFunction)Evt_Button, 1,
387 "() -> (Boolean _rv)"},
388 {"StillDown", (PyCFunction)Evt_StillDown, 1,
389 "() -> (Boolean _rv)"},
390 {"WaitMouseUp", (PyCFunction)Evt_WaitMouseUp, 1,
391 "() -> (Boolean _rv)"},
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000392 {"TickCount", (PyCFunction)Evt_TickCount, 1,
Jack Jansen7d0bc831995-06-09 20:56:31 +0000393 "() -> (UInt32 _rv)"},
Jack Jansen21f96871998-02-20 16:02:09 +0000394 {"GetCaretTime", (PyCFunction)Evt_GetCaretTime, 1,
395 "() -> (UInt32 _rv)"},
396 {"GetKeys", (PyCFunction)Evt_GetKeys, 1,
397 "() -> (KeyMap theKeys)"},
398 {"GetDblTime", (PyCFunction)Evt_GetDblTime, 1,
399 "() -> (UInt32 _rv)"},
400 {"SetEventMask", (PyCFunction)Evt_SetEventMask, 1,
401 "(EventMask value) -> None"},
402 {"GetNextEvent", (PyCFunction)Evt_GetNextEvent, 1,
403 "(EventMask eventMask) -> (Boolean _rv, EventRecord theEvent)"},
Jack Jansen21f96871998-02-20 16:02:09 +0000404 {"EventAvail", (PyCFunction)Evt_EventAvail, 1,
405 "(EventMask eventMask) -> (Boolean _rv, EventRecord theEvent)"},
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000406 {"PostEvent", (PyCFunction)Evt_PostEvent, 1,
Jack Jansend40f3c61995-10-09 23:12:22 +0000407 "(EventKind eventNum, UInt32 eventMsg) -> None"},
Jack Jansenabf17032000-06-20 07:42:23 +0000408
409#ifndef TARGET_API_MAC_CARBON
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000410 {"OSEventAvail", (PyCFunction)Evt_OSEventAvail, 1,
Jack Jansend40f3c61995-10-09 23:12:22 +0000411 "(EventMask mask) -> (Boolean _rv, EventRecord theEvent)"},
Jack Jansenabf17032000-06-20 07:42:23 +0000412#endif
413
414#ifndef TARGET_API_MAC_CARBON
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000415 {"GetOSEvent", (PyCFunction)Evt_GetOSEvent, 1,
Jack Jansend40f3c61995-10-09 23:12:22 +0000416 "(EventMask mask) -> (Boolean _rv, EventRecord theEvent)"},
Jack Jansenabf17032000-06-20 07:42:23 +0000417#endif
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000418 {"FlushEvents", (PyCFunction)Evt_FlushEvents, 1,
Jack Jansend40f3c61995-10-09 23:12:22 +0000419 "(EventMask whichMask, EventMask stopMask) -> None"},
Jack Jansenabf17032000-06-20 07:42:23 +0000420
421#ifndef TARGET_API_MAC_CARBON
Guido van Rossume26c2631995-02-28 09:11:41 +0000422 {"SystemClick", (PyCFunction)Evt_SystemClick, 1,
423 "(EventRecord theEvent, WindowPtr theWindow) -> None"},
Jack Jansenabf17032000-06-20 07:42:23 +0000424#endif
425
426#ifndef TARGET_API_MAC_CARBON
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000427 {"SystemTask", (PyCFunction)Evt_SystemTask, 1,
428 "() -> None"},
Jack Jansenabf17032000-06-20 07:42:23 +0000429#endif
430
431#ifndef TARGET_API_MAC_CARBON
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000432 {"SystemEvent", (PyCFunction)Evt_SystemEvent, 1,
433 "(EventRecord theEvent) -> (Boolean _rv)"},
Jack Jansenabf17032000-06-20 07:42:23 +0000434#endif
Jack Jansen91b5bed1998-09-17 15:28:58 +0000435 {"WaitNextEvent", (PyCFunction)Evt_WaitNextEvent, 1,
436 "(EventMask eventMask, UInt32 sleep [,RegionHandle]) -> (Boolean _rv, EventRecord theEvent)"},
Guido van Rossum17448e21995-01-30 11:53:55 +0000437 {NULL, NULL, 0}
438};
439
440
441
442
443void initEvt()
444{
445 PyObject *m;
446 PyObject *d;
447
448
449
450
451 m = Py_InitModule("Evt", Evt_methods);
452 d = PyModule_GetDict(m);
453 Evt_Error = PyMac_GetOSErrException();
454 if (Evt_Error == NULL ||
455 PyDict_SetItemString(d, "Error", Evt_Error) != 0)
456 Py_FatalError("can't initialize Evt.Error");
457}
458
459/* ========================= End module Evt ========================= */
460