-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
touch.c
164 lines (138 loc) 路 4.6 KB
/
touch.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
pygame - Python Game Library
Copyright (C) 2019 David L枚nnhager
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "../pygame.h"
#include "../pgcompat.h"
#include "../doc/touch_doc.h"
#if PY3
#define INT_CHECK(o) PyLong_Check(o)
#else
#define INT_CHECK(o) (PyInt_Check(o) || PyLong_Check(o))
#endif
static PyObject *
pg_touch_num_devices(PyObject *self, PyObject *args)
{
return PyLong_FromLong(SDL_GetNumTouchDevices());
}
static PyObject *
pg_touch_get_device(PyObject *self, PyObject *index)
{
SDL_TouchID touchid;
if (!INT_CHECK(index)) {
return RAISE(PyExc_TypeError,
"index must be an integer "
"specifying a device to get the ID for");
}
touchid = SDL_GetTouchDevice(PyLong_AsLong(index));
if (touchid == 0) {
/* invalid index */
return RAISE(pgExc_SDLError, SDL_GetError());
}
return PyLong_FromLongLong(touchid);
}
static PyObject *
pg_touch_num_fingers(PyObject *self, PyObject *device_id)
{
int fingercount;
if (!INT_CHECK(device_id)) {
return RAISE(PyExc_TypeError,
"device_id must be an integer "
"specifying a touch device");
}
VIDEO_INIT_CHECK();
fingercount =
SDL_GetNumTouchFingers(PyLong_AsLongLong(device_id));
if (fingercount == 0) {
return RAISE(pgExc_SDLError, SDL_GetError());
}
return PyLong_FromLong(fingercount);
}
/* Helper for adding objects to dictionaries. Check for errors with
PyErr_Occurred() */
static void
_pg_insobj(PyObject *dict, char *name, PyObject *v)
{
if (v) {
PyDict_SetItemString(dict, name, v);
Py_DECREF(v);
}
}
static PyObject *
pg_touch_get_finger(PyObject *self, PyObject *args, PyObject *kwargs)
{
char* keywords[] = {"touchid", "index", NULL};
SDL_TouchID touchid;
int index;
SDL_Finger *finger;
PyObject *fingerobj;
if (!PyArg_ParseTupleAndKeywords(args, kwargs,
"Li", keywords,
&touchid, &index))
{
return NULL;
}
VIDEO_INIT_CHECK();
if (!(finger = SDL_GetTouchFinger(touchid, index))) {
Py_RETURN_NONE;
}
fingerobj = PyDict_New();
if (!fingerobj)
return NULL;
_pg_insobj(fingerobj, "id", PyLong_FromLongLong(finger->id));
_pg_insobj(fingerobj, "x", PyFloat_FromDouble(finger->x));
_pg_insobj(fingerobj, "y", PyFloat_FromDouble(finger->y));
_pg_insobj(fingerobj, "pressure", PyFloat_FromDouble(finger->pressure));
if (PyErr_Occurred()) {
Py_DECREF(fingerobj);
return NULL;
}
return fingerobj;
}
static PyMethodDef _touch_methods[] = {
{"get_num_devices", pg_touch_num_devices, METH_NOARGS, DOC_PYGAMESDL2TOUCHGETNUMDEVICES},
{"get_device", pg_touch_get_device, METH_O, DOC_PYGAMESDL2TOUCHGETDEVICE},
{"get_num_fingers", pg_touch_num_fingers, METH_O, DOC_PYGAMESDL2TOUCHGETNUMFINGERS},
{"get_finger", (PyCFunction)pg_touch_get_finger, METH_VARARGS | METH_KEYWORDS, DOC_PYGAMESDL2TOUCHGETFINGER},
{NULL, NULL, 0, NULL}};
MODINIT_DEFINE(touch)
{
PyObject *module;
#if PY3
static struct PyModuleDef _module = {PyModuleDef_HEAD_INIT,
"touch",
DOC_PYGAMESDL2TOUCH,
-1,
_touch_methods,
NULL,
NULL,
NULL,
NULL};
#endif
import_pygame_base();
if (PyErr_Occurred()) {
MODINIT_ERROR;
}
/* create the module */
#if PY3
module = PyModule_Create(&_module);
#else
module = Py_InitModule3(MODPREFIX "touch", _touch_methods,
DOC_PYGAMESDL2TOUCH);
#endif
if (module == NULL) {
MODINIT_ERROR;
}
MODINIT_RETURN(module);
}