~ubuntu-branches/ubuntu/gutsy/pygame/gutsy

« back to all changes in this revision

Viewing changes to src/mouse.c

  • Committer: Bazaar Package Importer
  • Author(s): Ed Boraas
  • Date: 2002-02-20 06:39:24 UTC
  • Revision ID: james.westby@ubuntu.com-20020220063924-amlzj7tqkeods4eq
Tags: upstream-1.4
ImportĀ upstreamĀ versionĀ 1.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    pygame - Python Game Library
 
3
    Copyright (C) 2000-2001  Pete Shinners
 
4
 
 
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.
 
9
 
 
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.
 
14
 
 
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
 
18
 
 
19
    Pete Shinners
 
20
    pete@shinners.org
 
21
*/
 
22
 
 
23
/*
 
24
 *  pygame mouse module
 
25
 */
 
26
#include "pygame.h"
 
27
 
 
28
 
 
29
 
 
30
/* mouse module functions */
 
31
 
 
32
    /*DOC*/ static char doc_mouse_set_pos[] =
 
33
    /*DOC*/    "pygame.mouse.set_pos(pos) -> None\n"
 
34
    /*DOC*/    "moves the cursor position\n"
 
35
    /*DOC*/    "\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"
 
39
    /*DOC*/ ;
 
40
 
 
41
static PyObject* mouse_set_pos(PyObject* self, PyObject* args)
 
42
{
 
43
        short x, y;
 
44
 
 
45
        if(!TwoShortsFromObj(args, &x, &y))
 
46
                return RAISE(PyExc_TypeError, "invalid position argument for set_pos");
 
47
 
 
48
        VIDEO_INIT_CHECK();
 
49
 
 
50
        SDL_WarpMouse(x, y);
 
51
 
 
52
        RETURN_NONE
 
53
}
 
54
 
 
55
    /*DOC*/ static char doc_mouse_get_pos[] =
 
56
    /*DOC*/    "pygame.mouse.get_pos() -> x, y\n"
 
57
    /*DOC*/    "gets the cursor position\n"
 
58
    /*DOC*/    "\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"
 
61
    /*DOC*/ ;
 
62
 
 
63
static PyObject* mouse_get_pos(PyObject* self, PyObject* args)
 
64
{
 
65
        int x, y;
 
66
 
 
67
        if(!PyArg_ParseTuple(args, ""))
 
68
                return NULL;
 
69
        VIDEO_INIT_CHECK();
 
70
 
 
71
        SDL_GetMouseState(&x, &y);
 
72
        return Py_BuildValue("(ii)", x, y);
 
73
}
 
74
 
 
75
 
 
76
 
 
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"
 
80
    /*DOC*/    "\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"
 
84
    /*DOC*/    "\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"
 
87
    /*DOC*/    "this.\n"
 
88
    /*DOC*/ ;
 
89
 
 
90
static PyObject* mouse_get_rel(PyObject* self, PyObject* args)
 
91
{
 
92
        int x, y;
 
93
 
 
94
        if(!PyArg_ParseTuple(args, ""))
 
95
                return NULL;
 
96
 
 
97
        VIDEO_INIT_CHECK();
 
98
 
 
99
        SDL_GetRelativeMouseState(&x, &y);
 
100
        return Py_BuildValue("(ii)", x, y);
 
101
}
 
102
 
 
103
 
 
104
 
 
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"
 
108
    /*DOC*/    "\n"
 
109
    /*DOC*/    "This will return a small sequence containing the pressed state of\n"
 
110
    /*DOC*/    "each mouse button.\n"
 
111
    /*DOC*/ ;
 
112
 
 
113
static PyObject* mouse_get_pressed(PyObject* self, PyObject* args)
 
114
{
 
115
        PyObject* tuple;
 
116
        int state;
 
117
 
 
118
        if(!PyArg_ParseTuple(args, ""))
 
119
                return NULL;
 
120
 
 
121
        VIDEO_INIT_CHECK();
 
122
 
 
123
        state = SDL_GetMouseState(NULL, NULL);
 
124
        if(!(tuple = PyTuple_New(3)))
 
125
                return NULL;
 
126
 
 
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));
 
130
 
 
131
        return tuple;
 
132
}
 
133
 
 
134
 
 
135
 
 
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"
 
139
    /*DOC*/    "\n"
 
140
    /*DOC*/    "Shows or hides the mouse cursor. This will return the previous\n"
 
141
    /*DOC*/    "visible state of the mouse cursor.\n"
 
142
    /*DOC*/    "\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"
 
148
    /*DOC*/ ;
 
149
 
 
150
static PyObject* mouse_set_visible(PyObject* self, PyObject* args)
 
151
{
 
152
        int toggle;
 
153
 
 
154
        if(!PyArg_ParseTuple(args, "i", &toggle))
 
155
                return NULL;
 
156
        VIDEO_INIT_CHECK();
 
157
 
 
158
        toggle = SDL_ShowCursor(toggle);
 
159
        return PyInt_FromLong(toggle);
 
160
}
 
161
 
 
162
 
 
163
 
 
164
    /*DOC*/ static char doc_mouse_get_focused[] =
 
165
    /*DOC*/    "pygame.mouse.get_focused() -> bool\n"
 
166
    /*DOC*/    "state of mouse input focus\n"
 
167
    /*DOC*/    "\n"
 
168
    /*DOC*/    "Returns true when the application is receiving the mouse input\n"
 
169
    /*DOC*/    "focus.\n"
 
170
    /*DOC*/ ;
 
171
 
 
172
static PyObject* mouse_get_focused(PyObject* self, PyObject* args)
 
173
{
 
174
        if(!PyArg_ParseTuple(args, ""))
 
175
                return NULL;
 
176
 
 
177
        VIDEO_INIT_CHECK();
 
178
 
 
179
        return PyInt_FromLong((SDL_GetAppState()&SDL_APPMOUSEFOCUS) != 0);
 
180
}
 
181
 
 
182
 
 
183
 
 
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"
 
187
    /*DOC*/    "\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"
 
195
    /*DOC*/    "\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"
 
198
    /*DOC*/ ;
 
199
 
 
200
static PyObject* mouse_set_cursor(PyObject* self, PyObject* args)
 
201
{
 
202
        int w, h, spotx, spoty;
 
203
        PyObject *xormask, *andmask;
 
204
        Uint8 *xordata=NULL, *anddata=NULL;
 
205
        int xorsize, andsize, loop;
 
206
        short val;
 
207
        SDL_Cursor *lastcursor, *cursor = NULL;
 
208
 
 
209
        if(!PyArg_ParseTuple(args, "(ii)(ii)OO", &w, &h, &spotx, &spoty, &xormask, &andmask))
 
210
                return NULL;
 
211
 
 
212
        VIDEO_INIT_CHECK();
 
213
 
 
214
        if(!PySequence_Check(xormask) || !PySequence_Check(andmask))
 
215
                return RAISE(PyExc_TypeError, "xormask and andmask must be sequences");
 
216
 
 
217
        if(w % 8)
 
218
                return RAISE(PyExc_ValueError, "Cursor width must be divisible by 8.");
 
219
 
 
220
        xorsize = PySequence_Length(xormask);
 
221
        andsize = PySequence_Length(andmask);
 
222
 
 
223
        if(xorsize != w*h/8 || andsize != w*h/8)
 
224
                return RAISE(PyExc_ValueError, "bitmasks must be sized width*height/8");
 
225
 
 
226
        xordata = (Uint8*)malloc(xorsize);
 
227
        anddata = (Uint8*)malloc(andsize);
 
228
 
 
229
        for(loop = 0; loop < xorsize; ++loop)
 
230
        {
 
231
                if(!ShortFromObjIndex(xormask, loop, &val))
 
232
                        goto interror;
 
233
                xordata[loop] = (Uint8)val;
 
234
                if(!ShortFromObjIndex(andmask, loop, &val))
 
235
                        goto interror;
 
236
                anddata[loop] = (Uint8)val;
 
237
        }
 
238
 
 
239
        cursor = SDL_CreateCursor(xordata, anddata, w, h, spotx, spoty);
 
240
        free(xordata);
 
241
        free(anddata);
 
242
        xordata = NULL;
 
243
        anddata = NULL;
 
244
 
 
245
        if(!cursor)
 
246
                return RAISE(PyExc_SDLError, SDL_GetError());
 
247
 
 
248
        lastcursor = SDL_GetCursor();
 
249
        SDL_SetCursor(cursor);  
 
250
        SDL_FreeCursor(lastcursor);
 
251
 
 
252
        RETURN_NONE;
 
253
 
 
254
interror:
 
255
        if(xordata) free(xordata);
 
256
        if(anddata) free(anddata);
 
257
        return RAISE(PyExc_TypeError, "Invalid number in mask array");
 
258
}
 
259
 
 
260
 
 
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"
 
264
    /*DOC*/    "\n"
 
265
    /*DOC*/    "The mouse cursor data is the same as those passed into set_cursor.\n"
 
266
    /*DOC*/ ;
 
267
 
 
268
static PyObject* mouse_get_cursor(PyObject* self, PyObject* args)
 
269
{
 
270
        SDL_Cursor *cursor = NULL;
 
271
        PyObject* xordata, *anddata;
 
272
        int size, loop, w, h, spotx, spoty;
 
273
 
 
274
        if(!PyArg_ParseTuple(args, ""))
 
275
                return NULL;
 
276
 
 
277
        VIDEO_INIT_CHECK();
 
278
 
 
279
        cursor = SDL_GetCursor();
 
280
        if(!cursor)
 
281
                return RAISE(PyExc_SDLError, SDL_GetError());
 
282
 
 
283
        w = cursor->area.w;
 
284
        h = cursor->area.h;
 
285
        spotx = cursor->hot_x;
 
286
        spoty = cursor->hot_y;
 
287
 
 
288
        size = cursor->area.w * cursor->area.h / 8;
 
289
        xordata = PyTuple_New(size);
 
290
        if(!xordata)
 
291
                return NULL;
 
292
        anddata = PyTuple_New(size);
 
293
        if(!anddata)
 
294
        {
 
295
                Py_DECREF(anddata);
 
296
                return NULL;
 
297
        }
 
298
 
 
299
        for(loop = 0; loop < size; ++loop)
 
300
        {
 
301
                PyTuple_SET_ITEM(xordata, loop, PyInt_FromLong(cursor->data[loop]));
 
302
                PyTuple_SET_ITEM(anddata, loop, PyInt_FromLong(cursor->mask[loop]));
 
303
        }
 
304
 
 
305
        return Py_BuildValue("((ii)(ii)OO)", w, h, spotx, spoty, xordata, anddata);
 
306
}
 
307
 
 
308
 
 
309
 
 
310
 
 
311
static PyMethodDef mouse_builtins[] =
 
312
{
 
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 },
 
321
 
 
322
        { NULL, NULL }
 
323
};
 
324
 
 
325
 
 
326
 
 
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"
 
332
    /*DOC*/    "\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"
 
338
    /*DOC*/ ;
 
339
 
 
340
PYGAME_EXPORT
 
341
void initmouse(void)
 
342
{
 
343
        PyObject *module, *dict;
 
344
 
 
345
    /* create the module */
 
346
        module = Py_InitModule3("mouse", mouse_builtins, doc_pygame_mouse_MODULE);
 
347
        dict = PyModule_GetDict(module);
 
348
 
 
349
        /*imported needed apis*/
 
350
        import_pygame_base();
 
351
}
 
352