00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "Core/precomp.h"
00016
00017 #ifdef USE_X11
00018
00019 #include <stdlib.h>
00020 #include <stdio.h>
00021 #include <string.h>
00022 #include <iostream>
00023
00024 #include <X11/Xlib.h>
00025 #include <X11/Xutil.h>
00026 #include <X11/keysym.h>
00027
00028 #include "keyboard_x11.h"
00029
00030 #include <API/Display/Input/input.h>
00031 #include <API/Display/Input/inputaxis.h>
00032 #include <API/Display/Input/key.h>
00033 #include <API/Display/Input/inputbutton.h>
00034 #include <API/Display/Input/inputcursor.h>
00035 #include <API/Display/Input/inputhat.h>
00036 #include <API/Display/Input/keyboard.h>
00037 #include "API/Core/System/cl_assert.h"
00038
00039 #include <Core/System/Unix/implementation_xwindow.h>
00040 #include <Display/Input/X11/keyboard_x11.h>
00041 #include <Display/Display/X11/display_xwindow.h>
00042
00043 #include "Core/System/Generic/string_asm.h"
00044
00045
00046
00047
00048
00049 CL_XWindowKeyboard::CL_XWindowKeyboard(
00050 CL_XWindow_CompatibleCard *_card)
00051 {
00052 card = _card;
00053
00054 for (int j=0; j<X_KEYBOARD_BUFFERSIZE; j++) keys_return[j]=0;
00055
00056 buttons = new CL_InputButton_XKeyboard*[CL_NUM_KEYS];
00057 for (int i=0; i<CL_NUM_KEYS; i++) buttons[i] = NULL;
00058
00059 slot_xevent = CL_XWindow_CompatibleCard::sig_xevent.connect(
00060 CL_CreateSlot(this, &CL_XWindowKeyboard::on_xevent));
00061 }
00062
00063 CL_XWindowKeyboard::~CL_XWindowKeyboard()
00064 {
00065 CL_Implementation_XWindow::clean_up();
00066
00067 for (int i=0; i<CL_NUM_KEYS; i++) delete buttons[i];
00068 delete[] buttons;
00069 }
00070
00071 void CL_XWindowKeyboard::on_xevent(XEvent &e)
00072 {
00073 if (e.type != KeyPress && e.type != KeyRelease) return;
00074
00075 KeySym sym = XLookupKeysym(&e.xkey,0);
00076
00077 CL_Key key;
00078
00079 key.x = key.y = -1;
00080
00081 key.id = map_keysym_to_id(sym);
00082 key.ascii = -1;
00083
00084
00085 char buf[11];
00086 buf[10]=0;
00087 XLookupString(&e.xkey, buf, 10, NULL, NULL);
00088 if (strlen(buf) == 1) key.ascii = buf[0];
00089
00090 if (e.type == KeyRelease)
00091 {
00092 key.state = CL_Key::Released;
00093 CL_Input::sig_button_release(this, key);
00094 }
00095 else
00096 {
00097 key.state = CL_Key::Pressed;
00098 CL_Input::sig_button_press(this, key);
00099 }
00100 }
00101
00102 void CL_XWindowKeyboard::keep_alive()
00103 {
00104
00105 Window focus_win;
00106 int focus_state;
00107 XGetInputFocus(
00108 card->get_display(),
00109 &focus_win,
00110 &focus_state);
00111
00112
00113 if (card->get_window() == 0 ||
00114 card->get_window() == focus_win)
00115 {
00116 XQueryKeymap(card->get_display(), keys_return);
00117 }
00118 else
00119 {
00120
00121 for (int j=0; j<X_KEYBOARD_BUFFERSIZE; j++) keys_return[j]=0;
00122 }
00123 }
00124
00125 char *CL_XWindowKeyboard::get_name() const
00126 {
00127 return "XWindow keyboard";
00128 }
00129
00130 int CL_XWindowKeyboard::get_num_buttons() const
00131 {
00132 return CL_NUM_KEYS;
00133 }
00134
00135 CL_InputButton *CL_XWindowKeyboard::get_button(int button_num)
00136 {
00137 if (buttons[button_num] != NULL) return buttons[button_num];
00138
00139 int translate_key = map_id_to_keysym(button_num);
00140 if (translate_key == -1) return NULL;
00141
00142 buttons[button_num] =
00143 new CL_InputButton_XKeyboard(translate_key, keys_return);
00144
00145 return buttons[button_num];
00146 }
00147
00148 int CL_XWindowKeyboard::get_num_axes() const
00149 {
00150 return 0;
00151 }
00152
00153 CL_InputAxis *CL_XWindowKeyboard::get_axis(int )
00154 {
00155 return NULL;
00156 }
00157
00158 int CL_XWindowKeyboard::get_num_hats() const
00159 {
00160 return 0;
00161 }
00162
00163 CL_InputHat *CL_XWindowKeyboard::get_hat(int )
00164 {
00165 return NULL;
00166 }
00167
00168 int CL_XWindowKeyboard::get_num_cursors() const
00169 {
00170 return 0;
00171 }
00172
00173 CL_InputCursor *CL_XWindowKeyboard::get_cursor(int )
00174 {
00175 return NULL;
00176 }
00177
00178 int CL_XWindowKeyboard::map_keysym_to_id(int keysym)
00179 {
00180 switch (keysym)
00181 {
00182 case XK_BackSpace: return CL_KEY_BACKSPACE;
00183 case XK_Tab: return CL_KEY_TAB;
00184
00185 case XK_Return: return CL_KEY_ENTER;
00186 case XK_Pause: return CL_KEY_PAUSE;
00187
00188 case XK_Escape: return CL_KEY_ESCAPE;
00189 case XK_Delete: return CL_KEY_DELETE;
00190
00191 case XK_Home: return CL_KEY_HOME;
00192 case XK_Left: return CL_KEY_LEFT;
00193 case XK_Up: return CL_KEY_UP;
00194 case XK_Right: return CL_KEY_RIGHT;
00195 case XK_Down: return CL_KEY_DOWN;
00196 case XK_Page_Up: return CL_KEY_PAGEUP;
00197 case XK_Page_Down: return CL_KEY_PAGEDOWN;
00198 case XK_End: return CL_KEY_END;
00199 case XK_Print: return CL_KEY_PRINT;
00200 case XK_Insert: return CL_KEY_INSERT;
00201 case XK_Num_Lock: return CL_KEY_NUMLOCK;
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212 case XK_F1: return CL_KEY_F1;
00213 case XK_F2: return CL_KEY_F2;
00214 case XK_F3: return CL_KEY_F3;
00215 case XK_F4: return CL_KEY_F4;
00216 case XK_F5: return CL_KEY_F5;
00217 case XK_F6: return CL_KEY_F6;
00218 case XK_F7: return CL_KEY_F7;
00219 case XK_F8: return CL_KEY_F8;
00220 case XK_F9: return CL_KEY_F9;
00221 case XK_F10: return CL_KEY_F10;
00222 case XK_F11: return CL_KEY_F11;
00223 case XK_F12: return CL_KEY_F12;
00224 case XK_Shift_L: return CL_KEY_LSHIFT;
00225 case XK_Shift_R: return CL_KEY_RSHIFT;
00226 case XK_Control_L: return CL_KEY_LCTRL;
00227 case XK_Control_R: return CL_KEY_RCTRL;
00228 case XK_Caps_Lock: return CL_KEY_CAPSLOCK;
00229 case XK_Meta_L: return CL_KEY_ALT;
00230 case XK_Meta_R: return CL_KEY_ALTGR;
00231 case XK_Alt_L: return CL_KEY_ALT;
00232 case XK_Alt_R: return CL_KEY_ALTGR;
00233
00234 case XK_space: return CL_KEY_SPACE;
00235
00236
00237
00238
00239 case XK_0: return CL_KEY_0;
00240 case XK_1: return CL_KEY_1;
00241 case XK_2: return CL_KEY_2;
00242 case XK_3: return CL_KEY_3;
00243 case XK_4: return CL_KEY_4;
00244 case XK_5: return CL_KEY_5;
00245 case XK_6: return CL_KEY_6;
00246 case XK_7: return CL_KEY_7;
00247 case XK_8: return CL_KEY_8;
00248 case XK_9: return CL_KEY_9;
00249
00250
00251
00252
00253
00254 case XK_a: return CL_KEY_A;
00255 case XK_b: return CL_KEY_B;
00256 case XK_c: return CL_KEY_C;
00257 case XK_d: return CL_KEY_D;
00258 case XK_e: return CL_KEY_E;
00259 case XK_f: return CL_KEY_F;
00260 case XK_g: return CL_KEY_G;
00261 case XK_h: return CL_KEY_H;
00262 case XK_i: return CL_KEY_I;
00263 case XK_j: return CL_KEY_J;
00264 case XK_k: return CL_KEY_K;
00265 case XK_l: return CL_KEY_L;
00266 case XK_m: return CL_KEY_M;
00267 case XK_n: return CL_KEY_N;
00268 case XK_o: return CL_KEY_O;
00269 case XK_p: return CL_KEY_P;
00270 case XK_q: return CL_KEY_Q;
00271 case XK_r: return CL_KEY_R;
00272 case XK_s: return CL_KEY_S;
00273 case XK_t: return CL_KEY_T;
00274 case XK_u: return CL_KEY_U;
00275 case XK_v: return CL_KEY_V;
00276 case XK_w: return CL_KEY_W;
00277 case XK_x: return CL_KEY_X;
00278 case XK_y: return CL_KEY_Y;
00279 case XK_z: return CL_KEY_Z;
00280 }
00281
00282 return CL_KEY_NONE_OF_THE_ABOVE;
00283 }
00284
00285 int CL_XWindowKeyboard::map_id_to_keysym(int id)
00286 {
00287 switch (id)
00288 {
00289 case CL_KEY_A: return 38;
00290 case CL_KEY_B: return 56;
00291 case CL_KEY_C: return 54;
00292 case CL_KEY_D: return 40;
00293 case CL_KEY_E: return 26;
00294 case CL_KEY_F: return 41;
00295 case CL_KEY_G: return 42;
00296 case CL_KEY_H: return 43;
00297 case CL_KEY_I: return 31;
00298 case CL_KEY_J: return 44;
00299 case CL_KEY_K: return 45;
00300 case CL_KEY_L: return 46;
00301 case CL_KEY_M: return 58;
00302 case CL_KEY_N: return 57;
00303 case CL_KEY_O: return 32;
00304 case CL_KEY_P: return 33;
00305 case CL_KEY_Q: return 24;
00306 case CL_KEY_R: return 27;
00307 case CL_KEY_S: return 39;
00308 case CL_KEY_T: return 28;
00309 case CL_KEY_U: return 30;
00310 case CL_KEY_V: return 55;
00311 case CL_KEY_W: return 25;
00312 case CL_KEY_X: return 53;
00313 case CL_KEY_Y: return 29;
00314 case CL_KEY_Z: return 52;
00315
00316 case CL_KEY_0: return 19;
00317 case CL_KEY_1: return 10;
00318 case CL_KEY_2: return 11;
00319 case CL_KEY_3: return 12;
00320 case CL_KEY_4: return 13;
00321 case CL_KEY_5: return 14;
00322 case CL_KEY_6: return 15;
00323 case CL_KEY_7: return 16;
00324 case CL_KEY_8: return 17;
00325 case CL_KEY_9: return 18;
00326
00327 case CL_KEY_F1: return 67;
00328 case CL_KEY_F2: return 68;
00329 case CL_KEY_F3: return 69;
00330 case CL_KEY_F4: return 70;
00331 case CL_KEY_F5: return 71;
00332 case CL_KEY_F6: return 72;
00333 case CL_KEY_F7: return 73;
00334 case CL_KEY_F8: return 74;
00335 case CL_KEY_F9: return 75;
00336 case CL_KEY_F10: return 76;
00337 case CL_KEY_F11: return 95;
00338 case CL_KEY_F12: return 96;
00339
00340 case CL_KEY_ESCAPE: return 9;
00341 case CL_KEY_LEFT: return 100;
00342 case CL_KEY_RIGHT: return 102;
00343 case CL_KEY_UP: return 98;
00344 case CL_KEY_DOWN: return 104;
00345 case CL_KEY_LCTRL: return 37;
00346 case CL_KEY_RCTRL: return 109;
00347 case CL_KEY_LSHIFT: return 50;
00348 case CL_KEY_RSHIFT: return 62;
00349 case CL_KEY_ALT: return 64;
00350 case CL_KEY_ALTGR: return 113;
00351 case CL_KEY_TAB: return 23;
00352 case CL_KEY_ENTER: return 36;
00353 case CL_KEY_SPACE: return 65;
00354 case CL_KEY_BACKSPACE: return 22;
00355 case CL_KEY_INSERT: return 106;
00356 case CL_KEY_DELETE: return 107;
00357 case CL_KEY_HOME: return 97;
00358 case CL_KEY_END: return 98;
00359 case CL_KEY_PAGEUP: return 99;
00360 case CL_KEY_PAGEDOWN: return 105;
00361 case CL_KEY_CAPSLOCK: return 66;
00362 case CL_KEY_NUMLOCK: return 77;
00363 case CL_KEY_SCRLOCK: return 78;
00364 case CL_KEY_PRINT: return 111;
00365 case CL_KEY_PAUSE: return 110;
00366 case CL_KEY_KP_DIV: return 112;
00367 case CL_KEY_KP_MULT: return 63;
00368 case CL_KEY_KP_MINUS: return 82;
00369 case CL_KEY_KP_PLUS: return 86;
00370 case CL_KEY_KP_ENTER: return 108;
00371 }
00372
00373 return -1;
00374 }
00375
00376
00377
00378
00379
00380 CL_InputButton_XKeyboard::CL_InputButton_XKeyboard(
00381 int _key, char *_keys_return)
00382 {
00383 key = _key;
00384 keys_return = _keys_return;
00385 }
00386
00387 bool CL_InputButton_XKeyboard::is_pressed()
00388 {
00389 int byte = key/8;
00390 int bit = key%8;
00391
00392 return (keys_return[byte] & (1<<bit)) ? true : false;
00393 }
00394
00395 #endif