Various fixes for the TUIO input module
[numtypysics.git] / PythonInput.cpp
blob6b5840024ed8f2021e695ca41700e6d7cb5a2171
2 /*
3 * Python Input Module for NumptyPhysics
4 * Copyright (c) 2009 Thomas Perl <thpinfo.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 3 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
18 #include "PythonInput.h"
19 #include "Multitouch.h"
21 #ifdef HAVE_PYTHON
23 #include <Python.h>
24 #include "SDL/SDL.h"
26 PyObject* PythonInput::create_module()
28 static PyMethodDef NumptyMethods[] = {
29 {"post_event", post_event, METH_O, "Post a new mouse event"},
30 {NULL, NULL, 0, NULL} /* Sentinel */
33 PyObject* module;
34 module = Py_InitModule("numptyphysics", NumptyMethods);
36 PyModule_AddIntConstant(module, "START_STROKE", SDL_NP_START_STROKE);
37 PyModule_AddIntConstant(module, "APPEND_STROKE", SDL_NP_APPEND_STROKE);
38 PyModule_AddIntConstant(module, "FINISH_STROKE", SDL_NP_FINISH_STROKE);
39 PyModule_AddIntConstant(module, "START_ROPE", SDL_NP_START_ROPE);
40 PyModule_AddIntConstant(module, "APPEND_ROPE", SDL_NP_APPEND_ROPE);
41 PyModule_AddIntConstant(module, "FINISH_ROPE", SDL_NP_FINISH_ROPE);
42 PyModule_AddIntConstant(module, "PREVIEW_CURSOR", SDL_NP_PREVIEW_CURSOR);
43 PyModule_AddIntConstant(module, "CANCEL_DRAW", SDL_NP_CANCEL_DRAW);
44 PyModule_AddIntConstant(module, "START_DRAG", SDL_NP_START_DRAG);
45 PyModule_AddIntConstant(module, "DRAG", SDL_NP_DRAG);
46 PyModule_AddIntConstant(module, "END_DRAG", SDL_NP_END_DRAG);
47 PyModule_AddIntConstant(module, "PAN", SDL_NP_PAN);
48 PyModule_AddIntConstant(module, "ZOOM", SDL_NP_ZOOM);
49 PyModule_AddIntConstant(module, "DELETE", SDL_NP_DELETE);
51 PyModule_AddIntConstant(module, "WIDTH", m_width);
52 PyModule_AddIntConstant(module, "HEIGHT", m_height);
53 PyModule_AddIntConstant(module, "MAX_CURSORS", MT_MAX_CURSORS);
55 return module;
58 PyObject* PythonInput::post_event(PyObject* self, PyObject* event)
60 int xpos = -1, ypos = -1;
61 int type = -1;
62 int cursor_id = -1;
63 PyObject *o = NULL;
64 SDL_Event e = {0};
66 assert(self == NULL);
68 /* Get X position */
69 o = PyObject_GetAttrString(event, "x");
70 if (PyInt_Check(o)) {
71 xpos = (int)PyInt_AsLong(o);
72 } else {
73 fprintf(stderr, "x not a number: ");
74 PyObject_Print(o, stderr, 0);
75 fprintf(stderr, "\n");
76 exit(1);
78 Py_DECREF(o);
80 /* Get Y position */
81 o = PyObject_GetAttrString(event, "y");
82 if (PyInt_Check(o)) {
83 ypos = (int)PyInt_AsLong(o);
84 } else {
85 fprintf(stderr, "y not a number: ");
86 PyObject_Print(o, stderr, 0);
87 fprintf(stderr, "\n");
88 exit(1);
90 Py_DECREF(o);
92 /* Get type */
93 o = PyObject_GetAttrString(event, "event_type");
94 if (PyInt_Check(o)) {
95 type = (int)PyInt_AsLong(o);
96 } else {
97 fprintf(stderr, "event_type is not an int: ");
98 PyObject_Print(o, stderr, 0);
99 fputc('\n', stderr);
100 exit(1);
102 Py_DECREF(o);
104 /* Get cursor_id */
105 o = PyObject_GetAttrString(event, "cursor_id");
106 if (PyInt_Check(o)) {
107 cursor_id = (int)PyInt_AsLong(o);
108 } else {
109 fprintf(stderr, "cursor_id is not an int: ");
110 PyObject_Print(o, stderr, 0);
111 fputc('\n', stderr);
112 exit(1);
114 Py_DECREF(o);
116 if (type == SDL_NP_START_STROKE) {
117 queueStartStrokeEvent(cursor_id, xpos, ypos);
118 } else if (type == SDL_NP_FINISH_STROKE) {
119 queueFinishStrokeEvent(cursor_id, xpos, ypos);
120 } else if (type == SDL_NP_APPEND_STROKE) {
121 queueAppendStrokeEvent(cursor_id, xpos, ypos);
122 } else if (type == SDL_NP_START_DRAG) {
123 queueStartDragEvent(cursor_id, xpos, ypos);
124 } else if (type == SDL_NP_END_DRAG) {
125 queueEndDragEvent(cursor_id, xpos, ypos);
126 } else if (type == SDL_NP_DRAG) {
127 queueDragEvent(cursor_id, xpos, ypos);
128 } else if (type == SDL_NP_PREVIEW_CURSOR) {
129 queuePreviewCursorEvent(cursor_id, xpos, ypos);
130 } else if (type == SDL_NP_DELETE) {
131 queueDeleteEvent(xpos, ypos);
132 } else if (type == SDL_NP_CANCEL_DRAW) {
133 queueCancelDrawEvent(cursor_id);
134 } else {
135 fprintf(stderr, "Warning: unknown event type %d.\n", type);
136 exit(1);
139 //return PyErr_Format(PyExc_TypeError, "This function needs a bot class to work");
140 Py_RETURN_NONE;
143 #endif /* HAVE_PYTHON */