Main Page   Class Hierarchy   Alphabetical List   Data Structures   File List   Data Fields   Globals   Related Pages  

o_input.cpp

Go to the documentation of this file.
00001 // OMICRON ENGINE HEADER FILE
00002 //
00003 // --------------------------------------------------------------------------
00004 // Copyright (C) 2001-2002 by Bjoern Paetzel <kolrabi@gmx.de>
00005 //
00006 // This file is part of the Omicron Engine.
00007 //
00008 // The Omicron Engine is free software; you can redistribute it and/or modify
00009 // it under the terms of the  GNU General Public License  as published by the
00010 // Free Software Foundation;  either version  2  of the License,  or (at your
00011 // option) any later version.
00012 //
00013 // The Omicron Engine  is distributed in the hope that it will be useful, but
00014 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
00015 // or  FITNESS FOR A PARTICULAR PURPOSE.  See the  GNU General Public License
00016 // for more details.
00017 //
00018 // You should have  received a copy of the  GNU General Public License  along
00019 // with The Omicron Engine;  if not,  write to the  Free Software Foundation,
00020 // Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
00021 //
00022 // --------------------------------------------------------------------------
00023 // Last modified:       $Date: 2002/12/13 12:29:26 $
00024 // By           :       $Author: kolrabi $
00025 // $Id: o_input.cpp,v 1.3 2002/12/13 12:29:26 kolrabi Exp $ 
00026 
00027 /*
00028 
00029   $Log: o_input.cpp,v $
00030   Revision 1.3  2002/12/13 12:29:26  kolrabi
00031   fixed some bugs
00032 
00033   Revision 1.2  2002/12/12 13:13:39  kolrabi
00034   made the fullscreen work under X11
00035 
00036   Revision 1.1.1.1  2002/12/07 19:02:06  kolrabi
00037   initial release
00038 
00039 
00040 */
00041 
00046 #include            <string.h>
00047 
00048 #include            "omicron/internal.h"
00049 #include            "omicron/input.h"
00050 
00051 /**************************************************************************** 
00052  **************************************************************************** 
00053  * CONSTANTS **************************************************************** 
00054  **************************************************************************** 
00055  ****************************************************************************/ 
00056 
00057 static char *keyrow_de[] =
00058 { 
00059     "1234567890ß´", 
00060     "qwertzuiopü+", 
00061     "asdfghjklöä#\n", 
00062     "yxcvbnm,.-" 
00063 }; 
00064  
00065 static char *skeyrow_de[] =
00066 { 
00067     "!\"§$%&/()=?`", 
00068     "QWERTZUIOPÜ*", 
00069     "ASDFGHJKLÖÄ'\n", 
00070     "YXCVBNM;:_" 
00071 }; 
00072  
00073 /**************************************************************************** 
00074  **************************************************************************** 
00075  * VARIABLES **************************************************************** 
00076  **************************************************************************** 
00077  ****************************************************************************/ 
00078  
00079 static ushort       lastx     = 0, lasty = 0; 
00080 
00081 static char         **keyrow  = keyrow_de;
00082 static char         **skeyrow = skeyrow_de;
00083  
00084 /**************************************************************************** 
00085  **************************************************************************** 
00086  * INPUT ROUTINES *********************************************************** 
00087  **************************************************************************** 
00088  ****************************************************************************/ 
00089  
00090 /****************************************************************************
00091  * _get_key_ascii                                    convert a key to ascii *
00092  ****************************************************************************/
00093 ushort _get_key_ascii(uchar key) 
00094 { 
00095     switch(key) 
00096     { 
00097         case OK_SPACE: 
00098             return ' '; 
00099     }
00100 
00101     if (gv.kbdstate[OK_RSHIFT] || gv.kbdstate[OK_LSHIFT]) 
00102     { 
00103         if (key>=2 && key<=13) 
00104             return skeyrow[0][key-2]; 
00105 
00106         if (key>=16&& key<=27) 
00107             return skeyrow[1][key-16]; 
00108 
00109         if (key>=30&& key<=41) 
00110             return skeyrow[2][key-30]; 
00111 
00112         if (key>=44&& key<=53) 
00113             return skeyrow[3][key-44]; 
00114     } 
00115     else 
00116     { 
00117         if (key>=2 && key<=13) 
00118             return keyrow[0][key-2]; 
00119 
00120         if (key>=16&& key<=27) 
00121             return keyrow[1][key-16]; 
00122 
00123         if (key>=30&& key<=41) 
00124             return keyrow[2][key-30]; 
00125 
00126         if (key>=44&& key<=53) 
00127             return keyrow[3][key-44]; 
00128     } 
00129 
00130     return 0; 
00131 } 
00132  
00133 static ulong _input_getkeycodeforvk(ulong key) 
00134 { 
00135 #if defined(WIN32) 
00136  
00137     /* most of keycodes are identical, but some differ */ 
00138  
00139     switch(key) 
00140     { 
00141         case VK_BACK        :   return OK_BACKSPACE;
00142         case VK_TAB         :   return OK_TAB;
00143         case VK_RETURN      :   return OK_RETURN;
00144 
00145         case VK_PAUSE       :   return OK_PAUSE;
00146         case VK_CAPITAL     :   return OK_CAPSLOCK;
00147 
00148         case VK_ESCAPE      :   return OK_ESCAPE;
00149         case VK_SPACE       :   return OK_SPACE;
00150         case VK_PRIOR       :   return OK_PGUP;
00151         case VK_NEXT        :   return OK_PGDN;
00152         case VK_END         :   return OK_END;
00153         case VK_HOME        :   return OK_HOME;
00154         case VK_LEFT        :   return OK_LEFT;
00155         case VK_UP          :   return OK_UP;
00156         case VK_RIGHT       :   return OK_RIGHT;
00157         case VK_DOWN        :   return OK_DOWN;
00158         case VK_SNAPSHOT    :   return OK_PRINT;
00159         case VK_INSERT      :   return OK_INSERT;
00160         case VK_DELETE      :   return OK_DELETE;
00161 
00162         case VK_NUMPAD0     :   return OK_NUM0;
00163         case VK_NUMPAD1     :   return OK_NUM1;
00164         case VK_NUMPAD2     :   return OK_NUM2;
00165         case VK_NUMPAD3     :   return OK_NUM3;
00166         case VK_NUMPAD4     :   return OK_NUM4;
00167         case VK_NUMPAD5     :   return OK_NUM5;
00168         case VK_NUMPAD6     :   return OK_NUM6;
00169         case VK_NUMPAD7     :   return OK_NUM7;
00170         case VK_NUMPAD8     :   return OK_NUM8;
00171         case VK_NUMPAD9     :   return OK_NUM9;
00172 
00173         case '0'            :   return OK_0;
00174         case '1'            :   return OK_1;
00175         case '2'            :   return OK_2;
00176         case '3'            :   return OK_3;
00177         case '4'            :   return OK_4;
00178         case '5'            :   return OK_5;
00179         case '6'            :   return OK_6;
00180         case '7'            :   return OK_7;
00181         case '8'            :   return OK_8;
00182         case '9'            :   return OK_9;
00183 
00184         case 'A'            :   return OK_A;
00185         case 'B'            :   return OK_B;
00186         case 'C'            :   return OK_C;
00187         case 'D'            :   return OK_D;
00188         case 'E'            :   return OK_E;
00189         case 'F'            :   return OK_F;
00190         case 'G'            :   return OK_G;
00191         case 'H'            :   return OK_H;
00192         case 'I'            :   return OK_I;
00193         case 'J'            :   return OK_J;
00194         case 'K'            :   return OK_K;
00195         case 'L'            :   return OK_L;
00196         case 'M'            :   return OK_M;
00197         case 'N'            :   return OK_N;
00198         case 'O'            :   return OK_O;
00199         case 'P'            :   return OK_P;
00200         case 'Q'            :   return OK_Q;
00201         case 'R'            :   return OK_R;
00202         case 'S'            :   return OK_S;
00203         case 'T'            :   return OK_T;
00204         case 'U'            :   return OK_U;
00205         case 'V'            :   return OK_V;
00206         case 'W'            :   return OK_W;
00207         case 'X'            :   return OK_X;
00208         case 'Y'            :   return OK_Y;
00209         case 'Z'            :   return OK_Z;
00210 
00211         case VK_MULTIPLY    :   return OK_NUMMULT;
00212         case VK_ADD         :   return OK_NUMADD;
00213         case VK_DECIMAL     :   return OK_NUMPOINT;
00214         case VK_SUBTRACT    :   return OK_NUMMINUS;
00215         case VK_DIVIDE      :   return OK_NUMDIV;
00216 
00217         case VK_F1          :   return OK_F1;
00218         case VK_F2          :   return OK_F2;
00219         case VK_F3          :   return OK_F3;
00220         case VK_F4          :   return OK_F4;
00221         case VK_F5          :   return OK_F5;
00222         case VK_F6          :   return OK_F6;
00223         case VK_F7          :   return OK_F7;
00224         case VK_F8          :   return OK_F8;
00225         case VK_F9          :   return OK_F9;
00226         case VK_F10         :   return OK_F10;
00227         case VK_F11         :   return OK_F11;
00228         case VK_F12         :   return OK_F12;
00229 
00230         case VK_NUMLOCK     :   return OK_NUMLOCK;
00231         case VK_SCROLL      :   return OK_ROLL; 
00232 
00233         case VK_LSHIFT      :   return OK_LSHIFT; 
00234         case VK_RSHIFT      :   return OK_RSHIFT; 
00235         case VK_LCONTROL    :   return OK_LCTRL;
00236         case VK_RCONTROL    :   return OK_RCTRL;
00237         case VK_LMENU       :   return OK_LALT; 
00238         case VK_RMENU       :   return OK_RALT; 
00239 
00240         default             :   return 0; 
00241     }
00242  
00243 #else 
00244  
00245 #endif 
00246   return key; 
00247 } 
00248  
00249 /**************************************************************************** 
00250  * _input_init                                            initializes input * 
00251  ****************************************************************************/ 
00252 ulong               _input_init         // returns 0 if failed 
00253 ( 
00254 ) 
00255 { 
00256     _log_printf(MSG_DEBUG, "- _input_init() ------------------------"); 
00257 
00258     gv.init.noinput = 1; 
00259 
00260 #if defined(WIN32) 
00261 
00262     POINT pt;
00263     GetCursorPos(&pt);
00264 
00265     lastx = (ushort)pt.x;
00266     lasty = (ushort)pt.y;
00267 
00268     if (gv.init.fullscreen)
00269     {
00270         lastx = gv.init.hres/2;
00271         lasty = gv.init.vres/2;
00272 
00273         SetCursorPos(lastx, lasty);
00274     }
00275 #else
00276 
00277     lastx = gv.init.hres/2;
00278     lasty = gv.init.vres/2;
00279     XWarpPointer(gv.init.dpy, None, gv.init.wnd, 0, 0, 0, 0, lastx, lasty);
00280 
00281 #endif 
00282  
00283     memset(gv.kbdstate, 0, 256); 
00284  
00285     gv.init.noinput = 0; 
00286  
00287     _log_printf(MSG_DEBUG, "----------------------------------------"); 
00288     return 1; 
00289 } // _input_init 
00290  
00291  
00292  
00293 /**************************************************************************** 
00294  * _input_deinit                                        deinitializes input * 
00295  ****************************************************************************/ 
00296 void            _input_deinit() 
00297 { 
00298     /* not much to do here ;) */
00299 } 
00300 
00301 
00302 
00303 /**************************************************************************** 
00304  * _input_update                                         process input data * 
00305  ****************************************************************************/ 
00306 void            _input_update() 
00307 { 
00308     if (gv.init.noinput) 
00309         return;
00310 
00311     gv.mouse_dx = 0; 
00312     gv.mouse_dy = 0; 
00313     gv.mouse_lb = gv.mouse_b; 
00314  
00315     memcpy(gv.lkbdstate, gv.kbdstate, 256); 
00316 
00317 #if defined(WIN32) 
00318 
00319     uchar tmpstate[256];
00320 
00321     GetKeyboardState(tmpstate);
00322 
00323     gv.mouse_b =    (tmpstate[VK_LBUTTON]&0xFE?1:0)|
00324                     (tmpstate[VK_RBUTTON]&0xFE?2:0)|
00325                     (tmpstate[VK_MBUTTON]&0xFE?4:0);
00326 
00327     for (ulong i=0; i<256; i++)
00328     {
00329         ulong kc = _input_getkeycodeforvk(i);
00330         gv.kbdstate[kc] = tmpstate[i]&0xFE?1:0;
00331     }
00332 
00333     POINT pt;
00334     GetCursorPos(&pt);
00335 
00336     if (gv.active)
00337     {
00338         gv.mouse_dx = (sshort)(pt.x-lastx);
00339         gv.mouse_dy = (sshort)(pt.y-lasty);
00340     }
00341     else
00342     {
00343         gv.mouse_dx = gv.mouse_dy = gv.mouse_dz = 0;
00344     }
00345 
00346     lastx = (ushort)pt.x;
00347     lasty = (ushort)pt.y;
00348 
00349     if (gv.active && gv.init.fullscreen)
00350     {
00351         lastx = gv.init.hres/2;
00352         lasty = gv.init.vres/2;
00353 
00354         SetCursorPos(lastx, lasty);
00355     }
00356 #else 
00357  
00358     int x,y, tmp;
00359     unsigned int mask; 
00360     Window root, child; 
00361   
00362     XQueryPointer(gv.init.dpy, gv.init.wnd, &root, &child, 
00363       &x, &y, &tmp, &tmp, &mask); 
00364  
00365     gv.mouse_dx = x - lastx; 
00366     gv.mouse_dy = y - lasty;
00367 
00368     lastx = (ushort)x;
00369     lasty = (ushort)y;
00370 
00371     if (gv.active && gv.init.fullscreen)
00372     {
00373         lastx = gv.init.hres/2;
00374         lasty = gv.init.vres/2;
00375 
00376         XWarpPointer(gv.init.dpy, None, gv.init.wnd, 0, 0, 0, 0, lastx, lasty);
00377     }
00378 
00379     gv.mouse_lb = gv.mouse_b; 
00380  
00381     gv.mouse_b = 0; 
00382 
00383     if (mask&0x100) 
00384       gv.mouse_b |= 1; 
00385 
00386     if (mask&0x400) 
00387       gv.mouse_b |= 2; 
00388 
00389     if (mask&0x200) 
00390       gv.mouse_b |= 4; 
00391 
00392     lastx = x; lasty = y; 
00393  
00394     char kbd[32]; 
00395  
00396     XQueryKeymap(gv.init.dpy, kbd); 
00397  
00398     ulong i; 
00399  
00400     for (i=8; i<256; i++) 
00401       gv.kbdstate[i-8] = (kbd[i>>3]&(1<<(i%8)))?1:0; 
00402 
00403 #endif 
00404 }

Generated on Wed Dec 18 15:48:47 2002 for omicron engine by doxygen1.2.18