3 * Python Input Module for NumptyPhysics
4 * Copyright (c) 2009 Thomas Perl <thpinfo.com>
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"
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 */
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
);
58 PyObject
* PythonInput::post_event(PyObject
* self
, PyObject
* event
)
60 int xpos
= -1, ypos
= -1;
69 o
= PyObject_GetAttrString(event
, "x");
71 xpos
= (int)PyInt_AsLong(o
);
73 fprintf(stderr
, "x not a number: ");
74 PyObject_Print(o
, stderr
, 0);
75 fprintf(stderr
, "\n");
81 o
= PyObject_GetAttrString(event
, "y");
83 ypos
= (int)PyInt_AsLong(o
);
85 fprintf(stderr
, "y not a number: ");
86 PyObject_Print(o
, stderr
, 0);
87 fprintf(stderr
, "\n");
93 o
= PyObject_GetAttrString(event
, "event_type");
95 type
= (int)PyInt_AsLong(o
);
97 fprintf(stderr
, "event_type is not an int: ");
98 PyObject_Print(o
, stderr
, 0);
105 o
= PyObject_GetAttrString(event
, "cursor_id");
106 if (PyInt_Check(o
)) {
107 cursor_id
= (int)PyInt_AsLong(o
);
109 fprintf(stderr
, "cursor_id is not an int: ");
110 PyObject_Print(o
, stderr
, 0);
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
);
135 fprintf(stderr
, "Warning: unknown event type %d.\n", type
);
139 //return PyErr_Format(PyExc_TypeError, "This function needs a bot class to work");
143 #endif /* HAVE_PYTHON */