/* * Copyright (c) 2001 Dan Gudmundsson * See the file "license.terms" for information on usage and redistribution * of this file, and for a DISCLAIMER OF ALL WARRANTIES. * * * $Id: esdl_events.c,v 1.2 2005/06/29 14:45:38 dgud Exp $ */ /* * Map erl esdl_events calls to C sdl calls */ #include "esdl.h" #include #define MAX_EVENT_SIZE 13 /* Foward decls */ static char* encode_event(const SDL_Event * ev, char * bp); /* API */ void es_pumpEvents(sdl_data *sd, int len,char *buff) { SDL_PumpEvents(); } void es_peepEvents(sdl_data *sd, int len, char *bp) { SDL_Event events[256]; int numevents, res; Uint32 mask; char *start; if (len == 0) { mask = SDL_ALLEVENTS; numevents = 16; } else { mask = * (Uint32 *) bp; bp += sizeof(Uint32); numevents = *bp++; } SDL_PumpEvents(); res = SDL_PeepEvents(events, numevents, SDL_GETEVENT, mask); if (res > 0) { int sendlen, i; bp = start = sdl_get_temp_buff(sd, res*MAX_EVENT_SIZE); for (i = 0; i < res; i++) { bp = encode_event(&(events[i]), bp); } sendlen = bp - start; sdl_send(sd, sendlen); } } void es_pollEvent(sdl_data *sd, int len, char *buff) { SDL_Event event; if (SDL_PollEvent(&event)) { int sendlen; char *bp, *start; bp = start = sdl_get_temp_buff(sd, MAX_EVENT_SIZE); bp = encode_event(&event, bp); sendlen = bp - start; sdl_send(sd, sendlen); } } void es_waitEvent(sdl_data *sd, int len,char *buff) { SDL_Event event; int sendlen; char *bp, *start; bp = start = sdl_get_temp_buff(sd, MAX_EVENT_SIZE); SDL_WaitEvent(&event); bp = encode_event(&event, bp); if (*start != SDL_NOEVENT) { sendlen = bp - start; sdl_send(sd, sendlen); } } void es_eventState(sdl_data *sd, int len, char *bp) { Uint8 type, res; int sendlen, state; char *start; type = get8(bp); state = get8(bp); res = SDL_EventState(type, state); bp = start = sdl_get_temp_buff(sd, 1); put8(bp, res); sendlen = bp - start; sdl_send(sd, sendlen); } /* API from the other files e.g. mouse keyboard active */ void es_getAppState(sdl_data *sd, int len,char *buff) { int sendlen; char *bp, *start; Uint8 state; bp = start = sdl_get_temp_buff(sd, 1); state = SDL_GetAppState(); put8(bp, state); sendlen = bp - start; sdl_send(sd, sendlen); } void es_enableUNICODE(sdl_data *sd, int len,char *buff) { int sendlen; char *bp, *start; Uint8 enable; bp = buff; enable = get8(bp); bp = start = sdl_get_temp_buff(sd, 1); enable = SDL_EnableUNICODE(enable); put8(bp, enable); sendlen = bp - start; sdl_send(sd, sendlen); } void es_enableKeyRepeat(sdl_data *sd, int len,char *buff) { int sendlen; char *bp, *start; Uint16 delay, intv, res; bp = buff; delay = get16be(bp); intv = get16be(bp); bp = start = sdl_get_temp_buff(sd, 1); res = SDL_EnableKeyRepeat(delay, intv); put8(bp, res); sendlen = bp - start; sdl_send(sd, sendlen); } void es_getKeyName(sdl_data *sd, int len,char *buff) { int sendlen; char *bp, *start, *name; Uint16 key; bp = buff; key = get16be(bp); bp = start = sdl_get_temp_buff(sd, 128); name = SDL_GetKeyName(key); while(*name != '\0') { put8(bp, *name); name++; } sendlen = bp - start; sdl_send(sd, sendlen); } void es_getKeyState(sdl_data *sd, int len,char *buff) { Uint8 * keys; char *bp, *start; int length, sendlen, i; keys = SDL_GetKeyState(&length); bp = start = sdl_get_temp_buff(sd, length); for(i=0; itype) { case SDL_ACTIVEEVENT: /* Application loses/gains visibility */ put8(bp, SDL_ACTIVEEVENT); put8(bp, ev->active.gain); put8(bp, ev->active.state); break; case SDL_KEYDOWN: /* Keys pressed */ case SDL_KEYUP: /* Keys released */ put8(bp, SDL_KEYDOWN); /* Using same for both */ put8(bp, ev->key.which); put8(bp, ev->key.state); put8(bp, ev->key.keysym.scancode); put16be(bp, ev->key.keysym.sym); put16be(bp, ev->key.keysym.mod); put16be(bp, ev->key.keysym.unicode); break; case SDL_MOUSEMOTION: /* Mouse moved */ put8(bp, SDL_MOUSEMOTION); put8(bp, ev->motion.which); put8(bp, ev->motion.state); put16be(bp, SDL_GetModState()); put16be(bp, ev->motion.x); put16be(bp, ev->motion.y); put16be(bp, ev->motion.xrel); put16be(bp, ev->motion.yrel); break; case SDL_MOUSEBUTTONDOWN:/* Mouse button pressed */ case SDL_MOUSEBUTTONUP:/* Mouse button released */ put8(bp, SDL_MOUSEBUTTONDOWN); put8(bp, ev->button.which); put8(bp, ev->button.button); put8(bp, ev->button.state); put16be(bp, SDL_GetModState()); put16be(bp, ev->button.x); put16be(bp, ev->button.y); break; case SDL_JOYAXISMOTION:/* Joystick axis motion */ put8(bp, SDL_JOYAXISMOTION); put8(bp, ev->jaxis.which); put8(bp, ev->jaxis.axis); put16be(bp, ev->jaxis.value); break; case SDL_JOYBALLMOTION:/* Joystick trackball motion */ put8(bp, SDL_JOYBALLMOTION); put8(bp, ev->jball.which); put8(bp, ev->jball.ball); put16be(bp, ev->jball.xrel); put16be(bp, ev->jball.yrel); break; case SDL_JOYHATMOTION:/* Joystick hat position change */ put8(bp, SDL_JOYHATMOTION); put8(bp, ev->jhat.which); put8(bp, ev->jhat.hat); put16be(bp, ev->jhat.value); break; case SDL_JOYBUTTONDOWN:/* Joystick button pressed */ case SDL_JOYBUTTONUP: /* Joystick button released */ put8(bp, SDL_JOYBUTTONDOWN); put8(bp, ev->jbutton.which); put8(bp, ev->jbutton.button); put8(bp, ev->jbutton.state); break; case SDL_QUIT: /* User-requested quit */ put8(bp, SDL_QUIT); break; case SDL_VIDEORESIZE: put8(bp, SDL_VIDEORESIZE); put16be(bp, ev->resize.w); put16be(bp, ev->resize.h); break; case SDL_VIDEOEXPOSE: put8(bp, SDL_VIDEOEXPOSE); break; case SDL_SYSWMEVENT: /* System specific event */ break; default: /* Forward compatible */ fprintf(stderr, "ESDL received unsupported event type %x \n", ev->type); put8(bp, SDL_NOEVENT); break; } return bp; }