[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] How to catch SpaceMouse events?



Simon Wittber wrote:
I may be wrong, but I doubt you will ever get SDL to receive and
re-post unusual win32 events.

You will probably need to use something from the win32 API to capture
and use these events.


I digged a bit in the sources of SDL and pygame and found out how I can get the SpaceMouse events. However, to be able to process the events I had to modify pygame slightly. So is it possible that the below patch is incorporated into cvs?

The first thing I had to do is to enable the system events by calling

  pygame.event.set_allowed(SYSWMEVENT)

After that I received the SpaceMouse events as system events (by the way, the SYSWMEVENT isn't mentioned in the pygame documentation, I only found it in the SDL docs. I'd suggest to update the pygame docs as the attribute and the event is actually already wrapped).
Now the problem with the system events is that the event object doesn't contain any data. You only know that an event has occured but you have no chance of processing it. That's where I had to modify pygame.


In the dict_from_event() function in event.c I added the following to the switch statement:

	case SDL_SYSWMEVENT:
	        #ifdef WIN32
		insobj(dict, "hwnd", PyInt_FromLong((long)(event->syswm.msg->hwnd)));
		insobj(dict, "msg", PyInt_FromLong(event->syswm.msg->msg));
		insobj(dict, "wparam", PyInt_FromLong(event->syswm.msg->wParam));
		insobj(dict, "lparam", PyInt_FromLong(event->syswm.msg->lParam));
		#endif
		break;

And at the top of the file I had to include the following header:

#include "SDL_syswm.h"

With these changes, it's possible to access the actual message data in Python (i.e. the msg attribute which contains the message ID and the actual data in form of the wParam and lParam attributes).

On Linux, SDL also provides the actual XEvent structure. Unfortunately, this structure is not as simple as a Windows message (it's a big union of several other structures). So in this case, I'd suggest to make the entire XEvent available as a raw binary string. Then you could either dissect it yourself in Python using the struct module or you could just pass it on to other C functions that expect a pointer to the structure (that's what the SpaceMouse SDK wants). So the code would look something like this (untested!):

#ifdef ... // a proper symbol for Linux/Unix (maybe just as in SDL)
insobj(dict, "event", PyString_FromStringAndSize((char*)(&(event->syswm.event.xevent)), sizeof(XEvent)));
// error checking here
#endif


If anybode wants to check out how SDL processes the events, see the file SDL_events.h where the actual system event structure is defined:

typedef struct SDL_SysWMEvent {
	Uint8 type;
	SDL_SysWMmsg *msg;
} SDL_SysWMEvent;

The SDL_SysWMmsg structure is defined in SDL_syswm.h and is dependent on the actual platform. For WIN32 it's defined as:

struct SDL_SysWMmsg {
	SDL_version version;
	HWND hwnd;			/* The window for the message */
	UINT msg;			/* The type of message */
	WPARAM wParam;			/* WORD message parameter */
	LPARAM lParam;			/* LONG message parameter */
};

and for Unix it's:

struct SDL_SysWMmsg {
	SDL_version version;
	SDL_SYSWM_TYPE subsystem;
	union {
	    XEvent xevent;
	} event;
};


I'd be grateful if anybody who's in charge of the source code would incorporate the above modifications, so that the system event objects also make the event data available.


Thanks,

- Matthias -