Main Page | Modules | Data Structures | File List | Data Fields

SDL_hashevent.c

00001 #include <SDL/SDL.h>
00002 #include <stdio.h>
00003 #include <string.h>
00004 #include "SDL_eventmap.h"
00005 #include "SDL_eventmap_internal.h"
00006 
00007 char hashname[300]={'\0'};
00008 
00009 #define JOYAXIS_SNAP 1600
00010 #define JOYBALL_SNAP 1600
00011 #define MOUSE_SNAP   20
00012 
00013 Uint8 MotionDirection(Sint16 xrel, Sint16 yrel, Sint16 snap);
00014 
00015 const char *SDLH_HashName(Uint32 hash)
00016 {
00017   SDL_EventHash ehash;
00018 
00019   ehash.hash=hash;
00020 
00021   switch(ehash.values[0])
00022   {
00023   char buf[256];
00024   Uint8 tmp;
00025   const char *tstr;
00026   case SDL_MOUSEBUTTONDOWN:
00027     sprintf(hashname,"[MOUSEDN]\t[%d]\t[0x%02x]",ehash.values[1],ehash.values[2]);
00028     break;
00029 
00030   case SDL_MOUSEBUTTONUP:
00031     sprintf(hashname,"[MOUSEUP]\t[%d]\t[0x%02x]",ehash.values[1],ehash.values[2]);
00032     break;
00033 
00034   case SDL_MOUSEMOTION:
00035     tmp=ehash.values[1];
00036     buf[0]='\0';
00037 
00038     if(tmp&SDL_HAT_UP)        strcat(buf,"UP ");
00039     else if(tmp&SDL_HAT_DOWN) strcat(buf,"DOWN ");
00040 
00041     if(tmp&SDL_HAT_LEFT)      strcat(buf,"LEFT ");
00042     else if(tmp&SDL_HAT_RIGHT)strcat(buf,"RIGHT ");
00043 
00044     sprintf(hashname,"[MOUSEMV]\t[%s]\t[0x%02x]",buf,ehash.values[2]);    
00045     break;
00046 
00047   case SDL_KEYDOWN:
00048     if(ehash.lvalues[1]>=SDLK_LAST)
00049     {
00050       sprintf(buf,"U0x%04x",ehash.lvalues[1]-SDLK_LAST);
00051       tstr=buf;
00052     }
00053     else
00054       tstr=SDL_GetKeyName((SDLKey)ehash.lvalues[1]);
00055 
00056     sprintf(hashname,"[KEYDOWN]\t[M0x%02x]\t[%s]",ehash.values[1],tstr);
00057     break;
00058 
00059   case SDL_KEYUP:
00060     if(ehash.lvalues[1]>=SDLK_LAST)
00061     {
00062       sprintf(buf,"U0x%04x",ehash.lvalues[1]-SDLK_LAST);
00063       tstr=buf;
00064     }
00065     else
00066       tstr=SDL_GetKeyName((SDLKey)ehash.lvalues[1]);
00067 
00068     sprintf(hashname,"[KEYUP  ]\t[M0x%02x]\t[%s]",ehash.values[1],tstr);
00069     break;
00070 
00071   case SDL_JOYAXISMOTION:
00072     tmp=ehash.values[3];
00073     sprintf(hashname,"[JOYAXIS]\t[Joy#%d]\t[Axis#%d]\t[",ehash.values[1],ehash.values[2]);
00074 
00075     if(tmp&SDL_HAT_LEFT)       strcat(hashname," NEG");
00076     else if(tmp&SDL_HAT_RIGHT) strcat(hashname," POS");
00077     else                       strcat(hashname," CENTER");
00078 
00079     strcat(hashname," ]");
00080     break;
00081 
00082   case SDL_JOYHATMOTION:
00083     tmp=ehash.values[3];
00084     buf[0]='\0';
00085 
00086     if(tmp&SDL_HAT_UP)        strcat(buf,"UP ");
00087     else if(tmp&SDL_HAT_DOWN) strcat(buf,"DOWN ");
00088 
00089     if(tmp&SDL_HAT_LEFT)      strcat(buf,"LEFT ");
00090     else if(tmp&SDL_HAT_RIGHT)strcat(buf,"RIGHT ");
00091 
00092     sprintf(hashname,"[JOYHAT]\t[Joy#%d]\t[Hat#%d]\t[%s]",ehash.values[1],ehash.values[2],buf);
00093     break;
00094 
00095   case SDL_JOYBALLMOTION:
00096     tmp=ehash.values[3];
00097     buf[0]='\0';
00098 
00099     if(tmp&SDL_HAT_UP)        strcat(buf,"UP ");
00100     else if(tmp&SDL_HAT_DOWN) strcat(buf,"DOWN ");
00101 
00102     if(tmp&SDL_HAT_LEFT)      strcat(buf,"LEFT ");
00103     else if(tmp&SDL_HAT_RIGHT)strcat(buf,"RIGHT ");
00104     sprintf(hashname,"[JOYBAL]\t[Joy#%d]\t[Ball#%d]\t[%s]",ehash.values[1],ehash.values[2],buf);
00105     break;
00106 
00107   case SDL_JOYBUTTONDOWN:
00108     sprintf(hashname,"[JOYBDN]\t[Joy#%d]\t[But#%d]",ehash.values[1],ehash.values[2]);
00109     break;
00110 
00111   case SDL_JOYBUTTONUP:
00112     sprintf(hashname,"[JOYBUP]\t[Joy#%d]\t[But#%d]",ehash.values[1],ehash.values[2]);
00113     break;
00114 
00115   case 0:
00116     sprintf(hashname,"Invalid hash");
00117     break;
00118   
00119   default:
00120     hashname[0]='\0';
00121   }
00122 
00123   return(hashname);
00124 }
00125 
00126 Uint32 SDLH_HashEvent(SDL_Event *event, Uint32 flags)
00127 {
00128   SDL_EventHash hash;
00129   if(event==NULL) return(0);
00130 
00131   hash.hash=0;
00132 
00133   switch(event->type)
00134   {
00135   case SDL_KEYUP:
00136     if(flags&FILTER_NOKEYUP) break;
00137     /* else fall through to SDL_KEYDOWN case */
00138   case SDL_KEYDOWN:
00139     hash.values[0]=event->type;
00140 
00141     /* If SDL doesn't recognize the key, use the keycode instead */
00142     if(event->key.keysym.sym==SDLK_UNKNOWN)
00143       hash.lvalues[1]=event->key.keysym.scancode+SDLK_LAST;
00144     else
00145       hash.lvalues[1]=event->key.keysym.sym;
00146 
00147     if(flags&FILTER_NOKEYMOD)
00148       hash.values[1]=0;
00149     else
00150       hash.values[1]=SDL_GetModState();
00151     break;
00152   
00153   case SDL_MOUSEMOTION:
00154     hash.values[0]=event->type;
00155     hash.values[1]=MotionDirection(event->motion.xrel,-event->motion.yrel,MOUSE_SNAP);
00156     hash.values[2]=event->motion.state;
00157     if((flags&FILTER_NOCENTER)&&(hash.values[1]==0)) hash.hash=0;
00158     break;
00159 
00160   case SDL_MOUSEBUTTONUP:
00161     if(flags&FILTER_NOMOUSEUP) break;
00162     /* else fall through to SDL_MOUSEBUTTONDOWN */
00163   case SDL_MOUSEBUTTONDOWN:
00164     hash.values[0]=event->type;
00165     hash.values[1]=event->button.button;
00166 
00167     if(flags&FILTER_NOMOUSEMOD)
00168       hash.values[2]=0;
00169     else
00170       hash.values[2]=SDL_GetMouseState(NULL,NULL);
00171     break;
00172 
00173   case SDL_JOYAXISMOTION:
00174     hash.values[0]=event->type;
00175     hash.values[1]=event->jaxis.which;
00176     hash.values[2]=event->jaxis.axis;
00177     hash.values[3]=MotionDirection(event->jaxis.value,0,JOYAXIS_SNAP);
00178     if((flags&FILTER_NOCENTER)&&(hash.values[3]==0))
00179       hash.hash=0;
00180     break;
00181 
00182   case SDL_JOYBALLMOTION:
00183     hash.values[0]=event->type;
00184     hash.values[1]=event->jball.which;
00185     hash.values[2]=event->jball.ball;
00186     hash.values[3]=MotionDirection(event->jball.xrel,-event->jball.yrel,JOYBALL_SNAP);
00187     if((flags&FILTER_NOCENTER)&&(hash.values[3]==0))
00188       hash.hash=0;
00189     break;
00190 
00191   case SDL_JOYHATMOTION:
00192     hash.values[0]=event->type;
00193     hash.values[1]=event->jhat.which;
00194     hash.values[2]=event->jhat.hat;
00195     hash.values[3]=event->jhat.value;
00196     if((flags&FILTER_NOCENTER)&&(hash.values[3]==0))
00197       hash.hash=0;
00198 
00199     break;
00200 
00201   case SDL_JOYBUTTONUP:
00202     if(flags&FILTER_NOJOYBUP) break;
00203     /* else fall through to SDL_JOYBUTTONDOWN */
00204   case SDL_JOYBUTTONDOWN:
00205     hash.values[0]=event->type;
00206     hash.values[1]=event->jbutton.which;
00207     hash.values[2]=event->jbutton.button;
00208     break;
00209 
00210   default:
00211     break;
00212   }
00213 
00214   return(hash.hash);
00215 }
00216 
00217 Uint8 MotionDirection(Sint16 xrel, Sint16 yrel, Sint16 snap)
00218 {
00219   Uint8 val=0;
00220 
00221   if(xrel>snap)
00222     val|=SDL_HAT_RIGHT;
00223   else if(xrel<-snap)
00224     val|=SDL_HAT_LEFT;
00225 
00226   if(yrel>snap)
00227     val|=SDL_HAT_UP;
00228   else if(yrel<-snap)
00229     val|=SDL_HAT_DOWN;
00230 
00231   if(val==0) val|=SDL_HAT_CENTERED;
00232 
00233   return(val);
00234 }

Generated on Tue Jun 22 16:25:25 2004 by doxygen 1.3.5