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"
25 #include <SDL/SDL_mixer.h>
27 PyObject
* PythonInput::create_module()
29 static PyMethodDef NumptyMethods
[] = {
30 {"post_event", post_event
, METH_O
, "Post a new mouse event"},
31 {NULL
, NULL
, 0, NULL
} /* Sentinel */
35 module
= Py_InitModule("numptyphysics", NumptyMethods
);
37 PyModule_AddIntConstant(module
, "START_STROKE", SDL_NP_START_STROKE
);
38 PyModule_AddIntConstant(module
, "APPEND_STROKE", SDL_NP_APPEND_STROKE
);
39 PyModule_AddIntConstant(module
, "FINISH_STROKE", SDL_NP_FINISH_STROKE
);
40 PyModule_AddIntConstant(module
, "START_ROPE", SDL_NP_START_ROPE
);
41 PyModule_AddIntConstant(module
, "APPEND_ROPE", SDL_NP_APPEND_ROPE
);
42 PyModule_AddIntConstant(module
, "FINISH_ROPE", SDL_NP_FINISH_ROPE
);
43 PyModule_AddIntConstant(module
, "PREVIEW_CURSOR", SDL_NP_PREVIEW_CURSOR
);
44 PyModule_AddIntConstant(module
, "CANCEL_DRAW", SDL_NP_CANCEL_DRAW
);
45 PyModule_AddIntConstant(module
, "START_DRAG", SDL_NP_START_DRAG
);
46 PyModule_AddIntConstant(module
, "DRAG", SDL_NP_DRAG
);
47 PyModule_AddIntConstant(module
, "END_DRAG", SDL_NP_END_DRAG
);
48 PyModule_AddIntConstant(module
, "PAN", SDL_NP_PAN
);
49 PyModule_AddIntConstant(module
, "ZOOM", SDL_NP_ZOOM
);
50 PyModule_AddIntConstant(module
, "DELETE", SDL_NP_DELETE
);
52 PyModule_AddIntConstant(module
, "WIDTH", m_width
);
53 PyModule_AddIntConstant(module
, "HEIGHT", m_height
);
54 PyModule_AddIntConstant(module
, "MAX_CURSORS", MT_MAX_CURSORS
);
59 PyObject
* PythonInput::post_event(PyObject
* self
, PyObject
* event
)
61 static Mix_Chunk
* sDraw
= Mix_LoadWAV("draw.ogg");
62 static Mix_Chunk
* sDrag
= Mix_LoadWAV("drag.ogg");
63 static Mix_Chunk
* sDelete
= Mix_LoadWAV("delete.ogg");
65 int xpos
= -1, ypos
= -1;
71 if(sDrag
== NULL
|| sDraw
== NULL
|| sDelete
== NULL
) {
72 fprintf(stderr
, "Error: %s\n", Mix_GetError());
79 o
= PyObject_GetAttrString(event
, "x");
81 xpos
= (int)PyInt_AsLong(o
);
83 fprintf(stderr
, "x not a number: ");
84 PyObject_Print(o
, stderr
, 0);
85 fprintf(stderr
, "\n");
91 o
= PyObject_GetAttrString(event
, "y");
93 ypos
= (int)PyInt_AsLong(o
);
95 fprintf(stderr
, "y not a number: ");
96 PyObject_Print(o
, stderr
, 0);
97 fprintf(stderr
, "\n");
103 o
= PyObject_GetAttrString(event
, "event_type");
104 if (PyInt_Check(o
)) {
105 type
= (int)PyInt_AsLong(o
);
107 fprintf(stderr
, "event_type is not an int: ");
108 PyObject_Print(o
, stderr
, 0);
115 o
= PyObject_GetAttrString(event
, "cursor_id");
116 if (PyInt_Check(o
)) {
117 cursor_id
= (int)PyInt_AsLong(o
);
119 fprintf(stderr
, "cursor_id is not an int: ");
120 PyObject_Print(o
, stderr
, 0);
126 if (type
== SDL_NP_START_STROKE
) {
127 Mix_PlayChannel(-1, sDraw
, 0);
128 queueStartStrokeEvent(cursor_id
, xpos
, ypos
);
129 } else if (type
== SDL_NP_FINISH_STROKE
) {
130 queueFinishStrokeEvent(cursor_id
, xpos
, ypos
);
131 } else if (type
== SDL_NP_APPEND_STROKE
) {
132 queueAppendStrokeEvent(cursor_id
, xpos
, ypos
);
133 } else if (type
== SDL_NP_START_DRAG
) {
134 Mix_PlayChannel(-1, sDrag
, 0);
135 queueStartDragEvent(cursor_id
, xpos
, ypos
);
136 } else if (type
== SDL_NP_END_DRAG
) {
137 Mix_PlayChannel(-1, sDrag
, 0);
138 queueEndDragEvent(cursor_id
, xpos
, ypos
);
139 } else if (type
== SDL_NP_DRAG
) {
140 queueDragEvent(cursor_id
, xpos
, ypos
);
141 } else if (type
== SDL_NP_PREVIEW_CURSOR
) {
142 queuePreviewCursorEvent(cursor_id
, xpos
, ypos
);
143 } else if (type
== SDL_NP_DELETE
) {
144 Mix_PlayChannel(-1, sDelete
, 0);
145 queueDeleteEvent(xpos
, ypos
);
146 } else if (type
== SDL_NP_CANCEL_DRAW
) {
147 queueCancelDrawEvent(cursor_id
);
149 fprintf(stderr
, "Warning: unknown event type %d.\n", type
);
153 //return PyErr_Format(PyExc_TypeError, "This function needs a bot class to work");
157 #endif /* HAVE_PYTHON */