Logo
  • Main Page
  • Related Pages
  • Modules
  • Classes
  • Files

mmsinputx11handler.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2005-2007 Stefan Schwarzer, Jens Schneider,             *
00003  *                           Matthias Hardt, Guido Madaus                  *
00004  *                                                                         *
00005  *   Copyright (C) 2007-2008 BerLinux Solutions GbR                        *
00006  *                           Stefan Schwarzer & Guido Madaus               *
00007  *                                                                         *
00008  *   Copyright (C) 2009-2013 BerLinux Solutions GmbH                       *
00009  *                                                                         *
00010  *   Authors:                                                              *
00011  *      Stefan Schwarzer   <stefan.schwarzer@diskohq.org>,                 *
00012  *      Matthias Hardt     <matthias.hardt@diskohq.org>,                   *
00013  *      Jens Schneider     <jens.schneider@diskohq.org>,                   *
00014  *      Guido Madaus       <guido.madaus@diskohq.org>,                     *
00015  *      Patrick Helterhoff <patrick.helterhoff@diskohq.org>,               *
00016  *      René Bählkow       <rene.baehlkow@diskohq.org>                     *
00017  *                                                                         *
00018  *   This library is free software; you can redistribute it and/or         *
00019  *   modify it under the terms of the GNU Lesser General Public            *
00020  *   License version 2.1 as published by the Free Software Foundation.     *
00021  *                                                                         *
00022  *   This library is distributed in the hope that it will be useful,       *
00023  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00024  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00025  *   Lesser General Public License for more details.                       *
00026  *                                                                         *
00027  *   You should have received a copy of the GNU Lesser General Public      *
00028  *   License along with this library; if not, write to the                 *
00029  *   Free Software Foundation, Inc.,                                       *
00030  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
00031  **************************************************************************/
00032 
00033 #include "mmsinput/mmsinputx11handler.h"
00034 #include "mmsgui/fb/mmsfb.h"
00035 #include <typeinfo>
00036 
00037 #ifdef __HAVE_XLIB__
00038 #include <X11/keysym.h>
00039 
00040 static MMSKeySymbol getKeyFromX11(KeySym xSymbol) {
00041 
00042     switch(xSymbol) {
00043         case XK_a                : return MMSKEY_SMALL_A;
00044         case XK_b                : return MMSKEY_SMALL_B;
00045         case XK_c                : return MMSKEY_SMALL_C;
00046         case XK_d                : return MMSKEY_SMALL_D;
00047         case XK_e                : return MMSKEY_SMALL_E;
00048         case XK_f                : return MMSKEY_SMALL_F;
00049         case XK_g                : return MMSKEY_SMALL_G;
00050         case XK_h                : return MMSKEY_SMALL_H;
00051         case XK_i                : return MMSKEY_SMALL_I;
00052         case XK_j                : return MMSKEY_SMALL_J;
00053         case XK_k                : return MMSKEY_SMALL_K;
00054         case XK_l                : return MMSKEY_SMALL_L;
00055         case XK_m                : return MMSKEY_SMALL_M;
00056         case XK_n                : return MMSKEY_SMALL_N;
00057         case XK_o                : return MMSKEY_SMALL_O;
00058         case XK_p                : return MMSKEY_SMALL_P;
00059         case XK_q                : return MMSKEY_SMALL_Q;
00060         case XK_r                : return MMSKEY_SMALL_R;
00061         case XK_s                : return MMSKEY_SMALL_S;
00062         case XK_t                : return MMSKEY_SMALL_T;
00063         case XK_u                : return MMSKEY_SMALL_U;
00064         case XK_v                : return MMSKEY_SMALL_V;
00065         case XK_w                : return MMSKEY_SMALL_W;
00066         case XK_x                : return MMSKEY_SMALL_X;
00067         case XK_y                : return MMSKEY_SMALL_Y;
00068         case XK_z                : return MMSKEY_SMALL_Z;
00069         case XK_0                : return MMSKEY_0;
00070         case XK_1                : return MMSKEY_1;
00071         case XK_2                : return MMSKEY_2;
00072         case XK_3                : return MMSKEY_3;
00073         case XK_4                : return MMSKEY_4;
00074         case XK_5                : return MMSKEY_5;
00075         case XK_6                : return MMSKEY_6;
00076         case XK_7                : return MMSKEY_7;
00077         case XK_8                : return MMSKEY_8;
00078         case XK_9                : return MMSKEY_9;
00079         case XK_F1               : return MMSKEY_F1;
00080         case XK_F2               : return MMSKEY_F2;
00081         case XK_F3               : return MMSKEY_F3;
00082         case XK_F4               : return MMSKEY_F4;
00083         case XK_F5               : return MMSKEY_F5;
00084         case XK_F6               : return MMSKEY_F6;
00085         case XK_F7               : return MMSKEY_F7;
00086         case XK_F8               : return MMSKEY_F8;
00087         case XK_F9               : return MMSKEY_F9;
00088         case XK_F10              : return MMSKEY_F10;
00089         case XK_F11              : return MMSKEY_F11;
00090         case XK_F12              : return MMSKEY_F12;
00091         case XK_Caps_Lock        : return MMSKEY_CAPS_LOCK;
00092         case XK_Num_Lock         : return MMSKEY_NUM_LOCK;
00093         case XK_Scroll_Lock      : return MMSKEY_SCROLL_LOCK;
00094         case XK_Escape           : return MMSKEY_ESCAPE;
00095         case XK_Left             : return MMSKEY_CURSOR_LEFT;
00096         case XK_Right            : return MMSKEY_CURSOR_RIGHT;
00097         case XK_Up               : return MMSKEY_CURSOR_UP;
00098         case XK_Down             : return MMSKEY_CURSOR_DOWN;
00099         case XK_Tab              : return MMSKEY_TAB;
00100         case XK_ISO_Left_Tab     : return MMSKEY_TAB;
00101         case XK_Return           : return MMSKEY_RETURN;
00102         case XK_space            : return MMSKEY_SPACE;
00103         case XK_BackSpace        : return MMSKEY_BACKSPACE;
00104         case XK_Insert           : return MMSKEY_INSERT;
00105         case XK_Delete           : return MMSKEY_DELETE;
00106         case XK_Home             : return MMSKEY_HOME;
00107         case XK_End              : return MMSKEY_END;
00108         case XK_Page_Up          : return MMSKEY_PAGE_UP;
00109         case XK_Page_Down        : return MMSKEY_PAGE_DOWN;
00110         case XK_Print            : return MMSKEY_PRINT;
00111         case XK_Pause            : return MMSKEY_PAUSE;
00112         case XK_minus            : return MMSKEY_MINUS_SIGN;
00113         case XK_plus             : return MMSKEY_PLUS_SIGN;
00114         case XK_equal            : return MMSKEY_EQUALS_SIGN;
00115         case XK_backslash        : return MMSKEY_BACKSLASH;
00116         case XK_semicolon        : return MMSKEY_SEMICOLON;
00117         case XK_comma            : return MMSKEY_COMMA;
00118         case XK_period           : return MMSKEY_PERIOD;
00119         case XK_slash            : return MMSKEY_SLASH;
00120         case XK_Shift_L          : return MMSKEY_SHIFT;
00121         case XK_Shift_R          : return MMSKEY_SHIFT;
00122         case XK_Control_L        : return MMSKEY_CONTROL;
00123         case XK_Control_R        : return MMSKEY_CONTROL;
00124         case XK_Alt_L            : return MMSKEY_ALT;
00125         case XK_Alt_R            : return MMSKEY_ALT;
00126         case XK_Meta_L           : return MMSKEY_META;
00127         case XK_Meta_R           : return MMSKEY_META;
00128         case XK_Super_L          : return MMSKEY_SUPER;
00129         case XK_Super_R          : return MMSKEY_SUPER;
00130         case XK_Hyper_L          : return MMSKEY_HYPER;
00131         case XK_Hyper_R          : return MMSKEY_HYPER;
00132         case XK_Mode_switch      : return MMSKEY_ALTGR;
00133         case XK_KP_Divide        : return MMSKEY_SLASH;
00134         case XK_KP_Multiply      : return MMSKEY_ASTERISK;
00135         case XK_KP_Subtract      : return MMSKEY_MINUS_SIGN;
00136         case XK_KP_Add           : return MMSKEY_PLUS_SIGN;
00137         case XK_KP_Enter         : return MMSKEY_RETURN;
00138         case XK_KP_Space         : return MMSKEY_SPACE;
00139         case XK_KP_Tab           : return MMSKEY_TAB;
00140         case XK_KP_F1            : return MMSKEY_F1;
00141         case XK_KP_F2            : return MMSKEY_F2;
00142         case XK_KP_F3            : return MMSKEY_F3;
00143         case XK_KP_F4            : return MMSKEY_F4;
00144         case XK_KP_Equal         : return MMSKEY_EQUALS_SIGN;
00145         case XK_KP_Separator     : return MMSKEY_COLON;
00146         case XK_KP_Delete        : return MMSKEY_DELETE;
00147         case XK_KP_Insert        : return MMSKEY_INSERT;
00148         case XK_KP_End           : return MMSKEY_END;
00149         case XK_KP_Down          : return MMSKEY_CURSOR_DOWN;
00150         case XK_KP_Page_Down     : return MMSKEY_PAGE_DOWN;
00151         case XK_KP_Left          : return MMSKEY_CURSOR_LEFT;
00152         case XK_KP_Begin         : return MMSKEY_BEGIN;
00153         case XK_KP_Right         : return MMSKEY_CURSOR_RIGHT;
00154         case XK_KP_Home          : return MMSKEY_HOME;
00155         case XK_KP_Up            : return MMSKEY_CURSOR_UP;
00156         case XK_KP_Page_Up       : return MMSKEY_PAGE_UP;
00157         case XK_KP_Decimal       : return MMSKEY_PERIOD;
00158         case XK_KP_0             : return MMSKEY_0;
00159         case XK_KP_1             : return MMSKEY_1;
00160         case XK_KP_2             : return MMSKEY_2;
00161         case XK_KP_3             : return MMSKEY_3;
00162         case XK_KP_4             : return MMSKEY_4;
00163         case XK_KP_5             : return MMSKEY_5;
00164         case XK_KP_6             : return MMSKEY_6;
00165         case XK_KP_7             : return MMSKEY_7;
00166         case XK_KP_8             : return MMSKEY_8;
00167         case XK_KP_9             : return MMSKEY_9;
00168 
00169 
00170 
00171         default:    return MMSKEY_UNKNOWN;
00172     }
00173 }
00174 #endif
00175 
00176 MMSInputX11Handler::MMSInputX11Handler(MMS_INPUT_DEVICE device) {
00177 #ifdef __HAVE_XLIB__
00178     this->window = *((Window*) mmsfb->getX11Window());
00179     this->display = (Display *)mmsfb->getX11Display();
00180     //printf("\nwindow %d, %x\n",window, display);
00181     lastmotion = 0;
00182 
00183 #else
00184     throw MMSError(0,(string)typeid(this).name() + " is empty. compile X11 support!");
00185 #endif
00186 }
00187 
00188 MMSInputX11Handler::~MMSInputX11Handler() {
00189 #ifdef __HAVE_XLIB__
00190 #endif
00191 }
00192 
00193 void MMSInputX11Handler::grabEvents(MMSInputEvent *inputevent) {
00194 #ifdef __HAVE_XLIB__
00195     XEvent event;
00196     Bool ret;
00197     while(1) {
00198         //
00199         XLockDisplay(mmsfb->x_display);
00200         ret=XCheckWindowEvent(this->display,  this->window, KeyPressMask|KeyReleaseMask|ExposureMask|ButtonPressMask|ButtonReleaseMask|PointerMotionMask|EnterWindowMask|ResizeRedirectMask|StructureNotifyMask, &event);
00201         XUnlockDisplay(mmsfb->x_display);
00202         if(ret==False) {
00203             usleep(10000);
00204             continue;
00205         }
00206 
00207 //      XWindowEvent(this->display,  this->window, KeyPressMask|KeyReleaseMask|ExposureMask|ButtonPressMask|ButtonReleaseMask|PointerMotionMask|EnterWindowMask|ResizeRedirectMask, &event);
00208 
00209         //printf("have event\n");
00210         //printf("\n%d",event.type);
00211         //XNextEvent(this->display, &event);
00212         if(event.type==ResizeRequest) {
00213             mmsfb->resized=true;
00214             mmsfb->target_window_w = event.xresizerequest.width;
00215             mmsfb->target_window_h = event.xresizerequest.height;
00216             mmsfb->resizeWindow();
00217         }
00218         if(event.type==KeyPress) {
00219             inputevent->type = MMSINPUTEVENTTYPE_KEYPRESS;
00220             XLockDisplay(mmsfb->x_display);
00221             KeySym xSymbol = XKeycodeToKeysym(this->display , event.xkey.keycode,0 );
00222             XUnlockDisplay(mmsfb->x_display);
00223             inputevent->key = getKeyFromX11(xSymbol);
00224             return;
00225         }
00226 
00227         if(event.type==KeyRelease) {
00228             inputevent->type = MMSINPUTEVENTTYPE_KEYRELEASE;
00229             XLockDisplay(mmsfb->x_display);
00230             KeySym xSymbol = XKeycodeToKeysym(this->display , event.xkey.keycode,0 );
00231             XUnlockDisplay(mmsfb->x_display);
00232             inputevent->key = getKeyFromX11(xSymbol);
00233             return;
00234         }
00235         if(event.type==ButtonPress) {
00236         //discard all buttons but the left */
00237         if(event.xbutton.button != 1)
00238             return;
00239             inputevent->type = MMSINPUTEVENTTYPE_BUTTONPRESS;
00240             if (mmsfb->fullscreen == MMSFB_FSM_TRUE || mmsfb->fullscreen == MMSFB_FSM_ASPECT_RATIO) {
00241                 inputevent->posx = (int)((double)((double)event.xbutton.x / (double)mmsfb->display_w ) * mmsfb->x11_win_rect.w);
00242                 inputevent->posy =  (int)((double)((double)event.xbutton.y / (double)mmsfb->display_h ) * mmsfb->x11_win_rect.h);
00243             }
00244             else {
00245                 inputevent->posx = event.xbutton.x;
00246                 inputevent->posy = event.xbutton.y;
00247             }
00248             /*printf("press x: %d y: %d \n",event.xbutton.x,event.xbutton.y);
00249             printf("rect x: %d y: %d \n\n",inputevent->posx,inputevent->posy);*/
00250             return;
00251         }
00252         if(event.type==ButtonRelease) {
00253         //discard all buttons but the left */
00254         if(event.xbutton.button != 1)
00255             return;
00256             inputevent->type = MMSINPUTEVENTTYPE_BUTTONRELEASE;
00257             if (mmsfb->fullscreen == MMSFB_FSM_TRUE || mmsfb->fullscreen == MMSFB_FSM_ASPECT_RATIO) {
00258                 inputevent->posx = (int)((double)((double)event.xbutton.x / (double)mmsfb->display_w ) * mmsfb->x11_win_rect.w);
00259                 inputevent->posy =  (int)((double)((double)event.xbutton.y / (double)mmsfb->display_h ) * mmsfb->x11_win_rect.h);
00260             }
00261             else {
00262                 inputevent->posx = event.xbutton.x;
00263                 inputevent->posy = event.xbutton.y;
00264             }
00265 /*          printf("release x: %d y: %d \n",event.xbutton.x,event.xbutton.y);
00266             printf("rect x: %d y: %d \n\n",inputevent->posx,inputevent->posy);*/
00267             return;
00268         }
00269         if(event.type==MotionNotify) {
00270 
00271 #if 0
00272             if(lastmotion!=0) {
00273                 if(lastmotion+15 > event.xbutton.time) {
00274                     return;
00275                 }
00276             }
00277             lastmotion=event.xbutton.time;
00278 #endif
00279 
00280             inputevent->type = MMSINPUTEVENTTYPE_AXISMOTION;
00281             if (mmsfb->fullscreen == MMSFB_FSM_TRUE || mmsfb->fullscreen == MMSFB_FSM_ASPECT_RATIO || mmsfb->resized) {
00282                 inputevent->posx = (int)((double)((double)event.xbutton.x / (double)mmsfb->display_w ) * mmsfb->x11_win_rect.w);
00283                 inputevent->posy =  (int)((double)((double)event.xbutton.y / (double)mmsfb->display_h ) * mmsfb->x11_win_rect.h);
00284             }
00285             else {
00286                 inputevent->posx = event.xbutton.x;
00287                 inputevent->posy = event.xbutton.y;
00288             }
00289 /*          printf("release x: %d y: %d \n",event.xbutton.x,event.xbutton.y);
00290             printf("rect x: %d y: %d \n\n",inputevent->posx,inputevent->posy);*/
00291             return;
00292         }
00293 
00294         if(event.type==Expose) {
00295             mmsfb->refresh();
00296         }
00297         if(event.type == MapNotify) {
00298             /*
00299             for(int i=0; ;i++) {
00300                 if(mmsfb->x_windows[i]==0)
00301                     break;
00302                 else if(mmsfb->x_windows[i]!=mmsfb->input_window) {
00303                     XLowerWindow(mmsfb->x_display, mmsfb->x_windows[i]);
00304                     XMapWindow(mmsfb->x_display, mmsfb->x_windows[i]);
00305                     XRaiseWindow(mmsfb->x_display, mmsfb->input_window);
00306                     printf("mapping layer %d\n", i);
00307                     XSync(mmsfb->x_display,False);
00308                 }
00309             }*/
00310         }
00311         if(event.type == UnmapNotify) {
00312             for(int i=0; ;i++) {
00313                 if(mmsfb->x_windows[i]==0)
00314                     break;
00315                 else if(mmsfb->x_windows[i]!=mmsfb->input_window) {
00316                     //fill layer window with default root
00317                     MMSFBLayer *layer ;
00318                     mmsfb->getLayer(i, &layer);
00319                     X11_IMPL *impl = (X11_IMPL *)layer->getImplementation();
00320                     XLockDisplay(mmsfb->x_display);
00321                     XPutImage(mmsfb->x_display, mmsfb->x_windows[i], impl->x_gc, mmsfb->rootimage, 0,0, 0, 0, mmsfb->display_w,
00322                               mmsfb->display_h);
00323 
00324                     //XUnmapWindow(mmsfb->x_display, mmsfb->x_windows[i]);
00325                     printf("unmapping layer %d\n", i);
00326                     XSync(mmsfb->x_display,False);
00327                     XUnlockDisplay(mmsfb->x_display);
00328                 }
00329             }
00330         }
00331 
00332     }
00333 
00334 #else
00335     throw MMSError(0,(string)typeid(this).name() + " is empty. compile X11 support!");
00336 #endif
00337 
00338 }

Generated by doxygen