2
pygame - Python Game Library
3
Copyright (C) 2000-2001 Pete Shinners
5
This library is free software; you can redistribute it and/or
6
modify it under the terms of the GNU Library General Public
7
License as published by the Free Software Foundation; either
8
version 2 of the License, or (at your option) any later version.
10
This library is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
Library General Public License for more details.
15
You should have received a copy of the GNU Library General Public
16
License along with this library; if not, write to the Free
17
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30
/* mouse module functions */
32
/*DOC*/ static char doc_mouse_set_pos[] =
33
/*DOC*/ "pygame.mouse.set_pos(pos) -> None\n"
34
/*DOC*/ "moves the cursor position\n"
36
/*DOC*/ "Moves the mouse cursor to the specified position. This will\n"
37
/*DOC*/ "generate a MOUSEMOTION event on the input queue. The pos argument\n"
38
/*DOC*/ "is a 2-number-sequence containing the desired x and y position.\n"
41
static PyObject* mouse_set_pos(PyObject* self, PyObject* args)
45
if(!TwoShortsFromObj(args, &x, &y))
46
return RAISE(PyExc_TypeError, "invalid position argument for set_pos");
55
/*DOC*/ static char doc_mouse_get_pos[] =
56
/*DOC*/ "pygame.mouse.get_pos() -> x, y\n"
57
/*DOC*/ "gets the cursor position\n"
59
/*DOC*/ "Returns the current position of the mouse cursor. This is the\n"
60
/*DOC*/ "absolute mouse position inside your game window.\n"
63
static PyObject* mouse_get_pos(PyObject* self, PyObject* args)
67
if(!PyArg_ParseTuple(args, ""))
71
SDL_GetMouseState(&x, &y);
72
return Py_BuildValue("(ii)", x, y);
77
/*DOC*/ static char doc_mouse_get_rel[] =
78
/*DOC*/ "pygame.mouse.get_rel() -> x, y\n"
79
/*DOC*/ "gets the movement of the mouse\n"
81
/*DOC*/ "Returns the total distance the mouse has moved since your last\n"
82
/*DOC*/ "call to get_rel(). On the first call to get_rel the movement will\n"
83
/*DOC*/ "always be 0,0.\n"
85
/*DOC*/ "When the mouse is at the edges of the screen, the relative\n"
86
/*DOC*/ "movement will be stopped. See mouse_visible for a way to resolve\n"
90
static PyObject* mouse_get_rel(PyObject* self, PyObject* args)
94
if(!PyArg_ParseTuple(args, ""))
99
SDL_GetRelativeMouseState(&x, &y);
100
return Py_BuildValue("(ii)", x, y);
105
/*DOC*/ static char doc_mouse_get_pressed[] =
106
/*DOC*/ "pygame.mouse.get_pressed() -> button1, button2, button3\n"
107
/*DOC*/ "state of the mouse buttons\n"
109
/*DOC*/ "This will return a small sequence containing the pressed state of\n"
110
/*DOC*/ "each mouse button.\n"
113
static PyObject* mouse_get_pressed(PyObject* self, PyObject* args)
118
if(!PyArg_ParseTuple(args, ""))
123
state = SDL_GetMouseState(NULL, NULL);
124
if(!(tuple = PyTuple_New(3)))
127
PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong((state&SDL_BUTTON(1)) != 0));
128
PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong((state&SDL_BUTTON(2)) != 0));
129
PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong((state&SDL_BUTTON(3)) != 0));
136
/*DOC*/ static char doc_mouse_set_visible[] =
137
/*DOC*/ "pygame.mouse.set_visible(bool) -> bool\n"
138
/*DOC*/ "show or hide the mouse cursor\n"
140
/*DOC*/ "Shows or hides the mouse cursor. This will return the previous\n"
141
/*DOC*/ "visible state of the mouse cursor.\n"
143
/*DOC*/ "Note that when the cursor is hidden and the application has\n"
144
/*DOC*/ "grabbed the input. pygame will force the mouse to stay in the\n"
145
/*DOC*/ "center of the screen. Since the mouse is hidden it won't matter\n"
146
/*DOC*/ "that it's not moving, but it will keep the mouse from the edges\n"
147
/*DOC*/ "of the screen so the relative mouse position will always be true.\n"
150
static PyObject* mouse_set_visible(PyObject* self, PyObject* args)
154
if(!PyArg_ParseTuple(args, "i", &toggle))
158
toggle = SDL_ShowCursor(toggle);
159
return PyInt_FromLong(toggle);
164
/*DOC*/ static char doc_mouse_get_focused[] =
165
/*DOC*/ "pygame.mouse.get_focused() -> bool\n"
166
/*DOC*/ "state of mouse input focus\n"
168
/*DOC*/ "Returns true when the application is receiving the mouse input\n"
172
static PyObject* mouse_get_focused(PyObject* self, PyObject* args)
174
if(!PyArg_ParseTuple(args, ""))
179
return PyInt_FromLong((SDL_GetAppState()&SDL_APPMOUSEFOCUS) != 0);
184
/*DOC*/ static char doc_mouse_set_cursor[] =
185
/*DOC*/ "pygame.mouse.set_cursor(size, hotspot, xormasks, andmasks) -> None\n"
186
/*DOC*/ "state of shape of the mouse cursor\n"
188
/*DOC*/ "When the mouse cursor is visible, it will be displayed\n"
189
/*DOC*/ "as a black and white bitmap using the given bitmask arrays.\n"
190
/*DOC*/ "The size is a sequence containing the cursor width and height.\n"
191
/*DOC*/ "Hotspot is a sequence containing the cursor hotspot position.\n"
192
/*DOC*/ "xormasks is a sequence of bytes containing the cursor xor data\n"
193
/*DOC*/ "masks. Lastly is andmasks, a sequence of bytes containting the\n"
194
/*DOC*/ "cursor bitmask data.\n"
196
/*DOC*/ "Width must be a multiple of 8, and the mask arrays must be the\n"
197
/*DOC*/ "correct size for the given width and height. Otherwise an exception.\n"
200
static PyObject* mouse_set_cursor(PyObject* self, PyObject* args)
202
int w, h, spotx, spoty;
203
PyObject *xormask, *andmask;
204
Uint8 *xordata=NULL, *anddata=NULL;
205
int xorsize, andsize, loop;
207
SDL_Cursor *lastcursor, *cursor = NULL;
209
if(!PyArg_ParseTuple(args, "(ii)(ii)OO", &w, &h, &spotx, &spoty, &xormask, &andmask))
214
if(!PySequence_Check(xormask) || !PySequence_Check(andmask))
215
return RAISE(PyExc_TypeError, "xormask and andmask must be sequences");
218
return RAISE(PyExc_ValueError, "Cursor width must be divisible by 8.");
220
xorsize = PySequence_Length(xormask);
221
andsize = PySequence_Length(andmask);
223
if(xorsize != w*h/8 || andsize != w*h/8)
224
return RAISE(PyExc_ValueError, "bitmasks must be sized width*height/8");
226
xordata = (Uint8*)malloc(xorsize);
227
anddata = (Uint8*)malloc(andsize);
229
for(loop = 0; loop < xorsize; ++loop)
231
if(!ShortFromObjIndex(xormask, loop, &val))
233
xordata[loop] = (Uint8)val;
234
if(!ShortFromObjIndex(andmask, loop, &val))
236
anddata[loop] = (Uint8)val;
239
cursor = SDL_CreateCursor(xordata, anddata, w, h, spotx, spoty);
246
return RAISE(PyExc_SDLError, SDL_GetError());
248
lastcursor = SDL_GetCursor();
249
SDL_SetCursor(cursor);
250
SDL_FreeCursor(lastcursor);
255
if(xordata) free(xordata);
256
if(anddata) free(anddata);
257
return RAISE(PyExc_TypeError, "Invalid number in mask array");
261
/*DOC*/ static char doc_mouse_get_cursor[] =
262
/*DOC*/ "pygame.mouse.get_cursor() -> size, hotspot, xormasks, andmasks\n"
263
/*DOC*/ "get mouse cursor data\n"
265
/*DOC*/ "The mouse cursor data is the same as those passed into set_cursor.\n"
268
static PyObject* mouse_get_cursor(PyObject* self, PyObject* args)
270
SDL_Cursor *cursor = NULL;
271
PyObject* xordata, *anddata;
272
int size, loop, w, h, spotx, spoty;
274
if(!PyArg_ParseTuple(args, ""))
279
cursor = SDL_GetCursor();
281
return RAISE(PyExc_SDLError, SDL_GetError());
285
spotx = cursor->hot_x;
286
spoty = cursor->hot_y;
288
size = cursor->area.w * cursor->area.h / 8;
289
xordata = PyTuple_New(size);
292
anddata = PyTuple_New(size);
299
for(loop = 0; loop < size; ++loop)
301
PyTuple_SET_ITEM(xordata, loop, PyInt_FromLong(cursor->data[loop]));
302
PyTuple_SET_ITEM(anddata, loop, PyInt_FromLong(cursor->mask[loop]));
305
return Py_BuildValue("((ii)(ii)OO)", w, h, spotx, spoty, xordata, anddata);
311
static PyMethodDef mouse_builtins[] =
313
{ "set_pos", mouse_set_pos, 1, doc_mouse_set_pos },
314
{ "get_pos", mouse_get_pos, 1, doc_mouse_get_pos },
315
{ "get_rel", mouse_get_rel, 1, doc_mouse_get_rel },
316
{ "get_pressed", mouse_get_pressed, 1, doc_mouse_get_pressed },
317
{ "set_visible", mouse_set_visible, 1, doc_mouse_set_visible },
318
{ "get_focused", mouse_get_focused, 1, doc_mouse_get_focused },
319
{ "set_cursor", mouse_set_cursor, 1, doc_mouse_set_cursor },
320
{ "get_cursor", mouse_get_cursor, 1, doc_mouse_get_cursor },
327
/*DOC*/ static char doc_pygame_mouse_MODULE[] =
328
/*DOC*/ "Contains routines for dealing with the mouse. All mouse events\n"
329
/*DOC*/ "are retrieved through the pygame.event module. The mouse module\n"
330
/*DOC*/ "can be used to get the current state of the mouse. It can also be\n"
331
/*DOC*/ "used to set the state of the system cursor.\n"
333
/*DOC*/ "If you hide the mouse cursor with pygame.mouse.set_visible(0) and\n"
334
/*DOC*/ "lock the mouse focus to your game with pygame.event.set_grab(1),\n"
335
/*DOC*/ "the hidden mouse will be forced to the center of the screen. This\n"
336
/*DOC*/ "will help your relative mouse motions keep from getting stuck on\n"
337
/*DOC*/ "the edges of the screen.\n"
343
PyObject *module, *dict;
345
/* create the module */
346
module = Py_InitModule3("mouse", mouse_builtins, doc_pygame_mouse_MODULE);
347
dict = PyModule_GetDict(module);
349
/*imported needed apis*/
350
import_pygame_base();