blob: edd8bc7a22c0f3b6004aef256025eabc5e28656b [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 Jansenb81cf9d1995-06-06 13:08:40 +0000170 if (!PyArg_ParseTuple(_args, "h",
171 &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;
187 if (!PyArg_ParseTuple(_args, "h",
188 &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
198static PyObject *Evt_WaitNextEvent(_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 Jansen7d0bc831995-06-09 20:56:31 +0000206 UInt32 sleep;
Guido van Rossum17448e21995-01-30 11:53:55 +0000207 if (!PyArg_ParseTuple(_args, "hl",
208 &eventMask,
209 &sleep))
210 return NULL;
211 _rv = WaitNextEvent(eventMask,
212 &theEvent,
213 sleep,
214 (RgnHandle)0);
215 _res = Py_BuildValue("bO&",
216 _rv,
217 PyMac_BuildEventRecord, &theEvent);
218 return _res;
219}
220
221static PyObject *Evt_EventAvail(_self, _args)
222 PyObject *_self;
223 PyObject *_args;
224{
225 PyObject *_res = NULL;
226 Boolean _rv;
Jack Jansend40f3c61995-10-09 23:12:22 +0000227 EventMask eventMask;
Guido van Rossum17448e21995-01-30 11:53:55 +0000228 EventRecord theEvent;
229 if (!PyArg_ParseTuple(_args, "h",
230 &eventMask))
231 return NULL;
232 _rv = EventAvail(eventMask,
233 &theEvent);
234 _res = Py_BuildValue("bO&",
235 _rv,
236 PyMac_BuildEventRecord, &theEvent);
237 return _res;
238}
239
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000240static PyObject *Evt_PostEvent(_self, _args)
241 PyObject *_self;
242 PyObject *_args;
243{
244 PyObject *_res = NULL;
245 OSErr _err;
Jack Jansend40f3c61995-10-09 23:12:22 +0000246 EventKind eventNum;
Jack Jansen7d0bc831995-06-09 20:56:31 +0000247 UInt32 eventMsg;
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000248 if (!PyArg_ParseTuple(_args, "hl",
249 &eventNum,
250 &eventMsg))
251 return NULL;
252 _err = PostEvent(eventNum,
253 eventMsg);
254 if (_err != noErr) return PyMac_Error(_err);
255 Py_INCREF(Py_None);
256 _res = Py_None;
257 return _res;
258}
259
260static PyObject *Evt_OSEventAvail(_self, _args)
261 PyObject *_self;
262 PyObject *_args;
263{
264 PyObject *_res = NULL;
265 Boolean _rv;
Jack Jansend40f3c61995-10-09 23:12:22 +0000266 EventMask mask;
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000267 EventRecord theEvent;
268 if (!PyArg_ParseTuple(_args, "h",
269 &mask))
270 return NULL;
271 _rv = OSEventAvail(mask,
272 &theEvent);
273 _res = Py_BuildValue("bO&",
274 _rv,
275 PyMac_BuildEventRecord, &theEvent);
276 return _res;
277}
278
279static PyObject *Evt_GetOSEvent(_self, _args)
280 PyObject *_self;
281 PyObject *_args;
282{
283 PyObject *_res = NULL;
284 Boolean _rv;
Jack Jansend40f3c61995-10-09 23:12:22 +0000285 EventMask mask;
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000286 EventRecord theEvent;
287 if (!PyArg_ParseTuple(_args, "h",
288 &mask))
289 return NULL;
290 _rv = GetOSEvent(mask,
291 &theEvent);
292 _res = Py_BuildValue("bO&",
293 _rv,
294 PyMac_BuildEventRecord, &theEvent);
295 return _res;
296}
297
298static PyObject *Evt_FlushEvents(_self, _args)
299 PyObject *_self;
300 PyObject *_args;
301{
302 PyObject *_res = NULL;
Jack Jansend40f3c61995-10-09 23:12:22 +0000303 EventMask whichMask;
304 EventMask stopMask;
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000305 if (!PyArg_ParseTuple(_args, "hh",
306 &whichMask,
307 &stopMask))
308 return NULL;
309 FlushEvents(whichMask,
310 stopMask);
311 Py_INCREF(Py_None);
312 _res = Py_None;
313 return _res;
314}
315
Guido van Rossume26c2631995-02-28 09:11:41 +0000316static PyObject *Evt_SystemClick(_self, _args)
Guido van Rossum17448e21995-01-30 11:53:55 +0000317 PyObject *_self;
318 PyObject *_args;
319{
320 PyObject *_res = NULL;
Guido van Rossume26c2631995-02-28 09:11:41 +0000321 EventRecord theEvent;
322 WindowPtr theWindow;
323 if (!PyArg_ParseTuple(_args, "O&O&",
324 PyMac_GetEventRecord, &theEvent,
325 WinObj_Convert, &theWindow))
Guido van Rossum17448e21995-01-30 11:53:55 +0000326 return NULL;
Guido van Rossume26c2631995-02-28 09:11:41 +0000327 SystemClick(&theEvent,
328 theWindow);
329 Py_INCREF(Py_None);
330 _res = Py_None;
Guido van Rossum17448e21995-01-30 11:53:55 +0000331 return _res;
332}
333
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000334static PyObject *Evt_SystemTask(_self, _args)
335 PyObject *_self;
336 PyObject *_args;
337{
338 PyObject *_res = NULL;
339 if (!PyArg_ParseTuple(_args, ""))
340 return NULL;
341 SystemTask();
342 Py_INCREF(Py_None);
343 _res = Py_None;
344 return _res;
345}
346
347static PyObject *Evt_SystemEvent(_self, _args)
348 PyObject *_self;
349 PyObject *_args;
350{
351 PyObject *_res = NULL;
352 Boolean _rv;
353 EventRecord theEvent;
354 if (!PyArg_ParseTuple(_args, "O&",
355 PyMac_GetEventRecord, &theEvent))
356 return NULL;
357 _rv = SystemEvent(&theEvent);
358 _res = Py_BuildValue("b",
359 _rv);
360 return _res;
361}
362
Guido van Rossum17448e21995-01-30 11:53:55 +0000363static PyMethodDef Evt_methods[] = {
Guido van Rossum17448e21995-01-30 11:53:55 +0000364 {"GetMouse", (PyCFunction)Evt_GetMouse, 1,
365 "() -> (Point mouseLoc)"},
366 {"Button", (PyCFunction)Evt_Button, 1,
367 "() -> (Boolean _rv)"},
368 {"StillDown", (PyCFunction)Evt_StillDown, 1,
369 "() -> (Boolean _rv)"},
370 {"WaitMouseUp", (PyCFunction)Evt_WaitMouseUp, 1,
371 "() -> (Boolean _rv)"},
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000372 {"TickCount", (PyCFunction)Evt_TickCount, 1,
Jack Jansen7d0bc831995-06-09 20:56:31 +0000373 "() -> (UInt32 _rv)"},
Jack Jansen21f96871998-02-20 16:02:09 +0000374 {"GetCaretTime", (PyCFunction)Evt_GetCaretTime, 1,
375 "() -> (UInt32 _rv)"},
376 {"GetKeys", (PyCFunction)Evt_GetKeys, 1,
377 "() -> (KeyMap theKeys)"},
378 {"GetDblTime", (PyCFunction)Evt_GetDblTime, 1,
379 "() -> (UInt32 _rv)"},
380 {"SetEventMask", (PyCFunction)Evt_SetEventMask, 1,
381 "(EventMask value) -> None"},
382 {"GetNextEvent", (PyCFunction)Evt_GetNextEvent, 1,
383 "(EventMask eventMask) -> (Boolean _rv, EventRecord theEvent)"},
384 {"WaitNextEvent", (PyCFunction)Evt_WaitNextEvent, 1,
385 "(EventMask eventMask, UInt32 sleep) -> (Boolean _rv, EventRecord theEvent)"},
386 {"EventAvail", (PyCFunction)Evt_EventAvail, 1,
387 "(EventMask eventMask) -> (Boolean _rv, EventRecord theEvent)"},
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000388 {"PostEvent", (PyCFunction)Evt_PostEvent, 1,
Jack Jansend40f3c61995-10-09 23:12:22 +0000389 "(EventKind eventNum, UInt32 eventMsg) -> None"},
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000390 {"OSEventAvail", (PyCFunction)Evt_OSEventAvail, 1,
Jack Jansend40f3c61995-10-09 23:12:22 +0000391 "(EventMask mask) -> (Boolean _rv, EventRecord theEvent)"},
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000392 {"GetOSEvent", (PyCFunction)Evt_GetOSEvent, 1,
Jack Jansend40f3c61995-10-09 23:12:22 +0000393 "(EventMask mask) -> (Boolean _rv, EventRecord theEvent)"},
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"},
Guido van Rossume26c2631995-02-28 09:11:41 +0000396 {"SystemClick", (PyCFunction)Evt_SystemClick, 1,
397 "(EventRecord theEvent, WindowPtr theWindow) -> None"},
Jack Jansenb81cf9d1995-06-06 13:08:40 +0000398 {"SystemTask", (PyCFunction)Evt_SystemTask, 1,
399 "() -> None"},
400 {"SystemEvent", (PyCFunction)Evt_SystemEvent, 1,
401 "(EventRecord theEvent) -> (Boolean _rv)"},
Guido van Rossum17448e21995-01-30 11:53:55 +0000402 {NULL, NULL, 0}
403};
404
405
406
407
408void initEvt()
409{
410 PyObject *m;
411 PyObject *d;
412
413
414
415
416 m = Py_InitModule("Evt", Evt_methods);
417 d = PyModule_GetDict(m);
418 Evt_Error = PyMac_GetOSErrException();
419 if (Evt_Error == NULL ||
420 PyDict_SetItemString(d, "Error", Evt_Error) != 0)
421 Py_FatalError("can't initialize Evt.Error");
422}
423
424/* ========================= End module Evt ========================= */
425