Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

mouse_x11.cpp

Go to the documentation of this file.
00001 /*
00002         $Id: mouse_x11.cpp,v 1.1 2001/03/06 15:09:22 mbn Exp $
00003 
00004         ------------------------------------------------------------------------
00005         ClanLib, the platform independent game SDK.
00006 
00007         This library is distributed under the GNU LIBRARY GENERAL PUBLIC LICENSE
00008         version 2. See COPYING for details.
00009 
00010         For a total list of contributers see CREDITS.
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 "API/Core/System/cl_assert.h"
00027 
00028 #include "mouse_x11.h"
00029 
00030 #include <API/Display/Input/input.h>
00031 #include <API/Display/Input/inputaxis.h>
00032 #include <API/Display/Input/inputbutton.h>
00033 #include <API/Display/Input/inputcursor.h>
00034 #include <API/Display/Input/key.h>
00035 #include <API/Display/Input/inputhat.h>
00036 #include <Display/Input/X11/mouse_x11.h>
00037 #include <Display/Display/X11/display_xwindow.h>
00038 
00039 #define NUM_MOUSE_BUTTONS 3
00040 
00041 /*************************************
00042   CL_Mouse_XWin
00043 *************************************/
00044 
00045 CL_Mouse_XWin::CL_Mouse_XWin(CL_XWindow_CompatibleCard *_card)
00046 {
00047         card = _card;
00048 
00049         cursor = new CL_InputCursor_Mouse_XWin(card);
00050         axes = new CL_InputAxis_Mouse_XWin[2]; 
00051 
00052         buttons = new CL_InputButton_Mouse_XWin*[NUM_MOUSE_BUTTONS];
00053         for (int i=0; i<NUM_MOUSE_BUTTONS; i++)
00054                 buttons[i] = new CL_InputButton_Mouse_XWin();
00055         have_focus = false;
00056 }
00057 
00058 CL_Mouse_XWin::~CL_Mouse_XWin()
00059 {
00060         delete cursor;
00061 
00062         for (int i=0; i<NUM_MOUSE_BUTTONS; i++) delete buttons[i];
00063         delete[] buttons;
00064 }
00065 
00066 
00067 int CL_Mouse_XWin::get_num_buttons() const
00068 {
00069         return 3;
00070 }
00071 
00072 CL_InputButton *CL_Mouse_XWin::get_button(int button_num)
00073 {
00074         if (button_num < 0 || button_num >= NUM_MOUSE_BUTTONS) return NULL;
00075 
00076         return buttons[button_num];
00077 }
00078 
00079 int CL_Mouse_XWin::get_num_axes() const
00080 {
00081         return 2;
00082 }
00083 
00084 CL_InputAxis *CL_Mouse_XWin::get_axis(int axis_num)
00085 {
00086         cl_assert(((unsigned int) axis_num)<2);
00087         return &axes[axis_num];
00088 }
00089 
00090 int CL_Mouse_XWin::get_num_hats() const
00091 {
00092         return 0;
00093 }
00094 
00095 CL_InputHat *CL_Mouse_XWin::get_hat(int /*hat_num*/)
00096 {
00097         return NULL;
00098 }
00099 
00100 int CL_Mouse_XWin::get_num_cursors() const
00101 {
00102         return 1;
00103 }
00104 
00105 void CL_Mouse_XWin::keep_alive()
00106 {
00107         if (card->is_initialized() == false) return;
00108 
00109         CL_Key key;
00110         Window child;
00111         Window root;
00112         int root_x;
00113         int root_y;
00114         int win_x;
00115         int win_y;
00116         unsigned int mask;
00117 
00118         axes[0].center = (card->get_width())/2.0;
00119         axes[1].center = (card->get_height())/2.0;
00120 
00121         // Check if window has focus right now:
00122         Window focus_win;
00123         int focus_state;
00124         XGetInputFocus(
00125                 card->get_display(),
00126                 &focus_win,
00127                 &focus_state);
00128 
00129         XQueryPointer(card->get_display(),
00130                       card->get_window(),
00131                       &root,
00132                       &child,
00133                       &root_x,
00134                       &root_y,
00135                       &win_x,
00136                       &win_y,
00137                       &mask);
00138 
00139         // Cutting the mouse position to the effective window size
00140         if (win_x < 0) win_x = 0;
00141         if (win_y < 0) win_y = 0;
00142         
00143         if (win_x >= cursor->get_max_x()) win_x = (int)cursor->get_max_x() - 1;
00144         if (win_y >= cursor->get_max_y()) win_y = (int)cursor->get_max_y() - 1;
00145 
00146         // get_window() returns NULL if in DGA mode.    
00147         if (card->get_window() == 0 || card->get_window() == focus_win)
00148         {
00149                 axes[0].pos = cursor->x;
00150                 axes[1].pos = cursor->y;
00151                 
00152                 if (cursor->x != win_x || cursor->y != win_y) 
00153                 {
00154                         //the mouse is on our display:
00155                         cursor->x = win_x;
00156                         cursor->y = win_y;
00157                         
00158                         CL_Input::sig_mouse_move(this, cursor->x, cursor->y);
00159                 }
00160                 
00161                 // Handle the key events
00162                 if (mask & 0x100)
00163                 {
00164                         if (buttons[0]->button_state == false)
00165                         {
00166                                 buttons[0]->button_state = true;
00167                                 CL_Input::sig_button_press(
00168                                         this,
00169                                         CL_Key(
00170                                                 CL_MOUSE_LEFTBUTTON,
00171                                                 CL_Key::Pressed,
00172                                                 -1,
00173                                                 cursor->x,
00174                                                 cursor->y));
00175                         }
00176                 }
00177                 else
00178                 {
00179                         if (buttons[0]->button_state == true)
00180                         {
00181                                 buttons[0]->button_state = false;
00182                                 CL_Input::sig_button_release(
00183                                         this,
00184                                         CL_Key(
00185                                                 CL_MOUSE_LEFTBUTTON,
00186                                                 CL_Key::Released,
00187                                                 -1,
00188                                                 cursor->x,
00189                                                 cursor->y));
00190                         }
00191                 }
00192                 
00193                 if (mask & 0x200)
00194                 {
00195                         if (buttons[1]->button_state == false)
00196                         {
00197                                 buttons[1]->button_state = true;
00198                                 CL_Input::sig_button_press(
00199                                         this,
00200                                         CL_Key(
00201                                                 CL_MOUSE_MIDDLEBUTTON,
00202                                                 CL_Key::Pressed,
00203                                                 -1,
00204                                                 cursor->x,
00205                                                 cursor->y));
00206                         }
00207                 }
00208                 else
00209                 {
00210                         if (buttons[1]->button_state == true)
00211                         {
00212                                 buttons[1]->button_state = false;
00213                                 CL_Input::sig_button_release(
00214                                         this,
00215                                         CL_Key(
00216                                                 CL_MOUSE_MIDDLEBUTTON,
00217                                                 CL_Key::Released,
00218                                                 -1,
00219                                                 cursor->x,
00220                                                 cursor->y));
00221                         }
00222                 }
00223 
00224                 if(mask & 0x400) 
00225                 {
00226                         if (buttons[2]->button_state == false)
00227                         {
00228                                 buttons[2]->button_state = true;
00229                                 CL_Input::sig_button_press(
00230                                         this,
00231                                         CL_Key(
00232                                                 CL_MOUSE_RIGHTBUTTON,
00233                                                 CL_Key::Pressed,
00234                                                 -1,
00235                                                 cursor->x,
00236                                                 cursor->y));
00237                         }
00238                 }
00239                 else
00240                 {
00241                         if (buttons[2]->button_state == true)
00242                         {
00243                                 buttons[2]->button_state = false;
00244                                 CL_Input::sig_button_release(
00245                                         this,
00246                                         CL_Key(
00247                                                 CL_MOUSE_RIGHTBUTTON,
00248                                                 CL_Key::Released,
00249                                                 -1,
00250                                                 cursor->x,
00251                                                 cursor->y));
00252                         }
00253                 }
00254 
00255                 if (!have_focus)
00256                 {
00257                         have_focus = true;
00258 //                      CL_Input::sig_mouse_enter(this);
00259                 }
00260         }
00261         else // we don't have window focus. keep last coordinates and release buttons
00262         {
00263                 XQueryPointer(card->get_display(),
00264                               card->get_window(),
00265                               &root,
00266                               &child,
00267                               &root_x,
00268                               &root_y,
00269                               &win_x,
00270                               &win_y,
00271                               &mask);
00272 
00273                 cursor->x = win_x;
00274                 cursor->y = win_y;
00275         
00276                 if (have_focus)
00277                 {
00278                         have_focus = false;
00279 //                      CL_Input::sig_mouse_exit(this);
00280                 }
00281                 
00282                 // Handle the key events
00283                 key.state = CL_Key::Released;     
00284 
00285                 if (buttons[0]->button_state == true) 
00286                 {
00287                         key.id = CL_MOUSE_LEFTBUTTON;
00288                         buttons[0]->button_state = false;
00289                         CL_Input::sig_button_release(this, key);
00290                 }
00291           
00292                 if (buttons[1]->button_state == true)
00293                 {
00294                         key.id = CL_MOUSE_MIDDLEBUTTON;
00295                         buttons[1]->button_state = false;
00296                         CL_Input::sig_button_release(this, key);
00297                 }
00298 
00299                 if (buttons[2]->button_state == true)
00300                 {
00301                         key.id = CL_MOUSE_RIGHTBUTTON;
00302                         buttons[2]->button_state = false;
00303                         CL_Input::sig_button_release(this, key);
00304                 }         
00305         }
00306         
00307         if (card->is_fullscreen())
00308         {
00309                 int w = card->get_win_width();
00310                 int h = card->get_win_height();
00311 
00312                 cursor->x -= (w - card->get_width())/2;
00313                 cursor->y -= (h - card->get_height())/2;
00314 
00315                 if (cursor->x < 0)
00316                         cursor->x = 0;
00317                 if (cursor->x > card->get_width())
00318                         cursor->x = card->get_width();
00319                 if (cursor->y < 0)
00320                         cursor->y = 0;
00321                 if (cursor->y > card->get_height())
00322                         cursor->y = card->get_height();
00323         }
00324 }
00325 
00326 CL_InputCursor *CL_Mouse_XWin::get_cursor(int /*cursor_num*/)
00327 {
00328         return cursor;
00329 }
00330 
00331 /*******************************
00332   CL_InputButton_Mouse_XWin
00333 *******************************/
00334 
00335 CL_InputButton_Mouse_XWin::CL_InputButton_Mouse_XWin()
00336 {
00337         button_state = false;
00338 }
00339 
00340 CL_InputButton_Mouse_XWin::~CL_InputButton_Mouse_XWin()
00341 {
00342 }
00343 
00344 bool CL_InputButton_Mouse_XWin::is_pressed()
00345 {
00346         return button_state;
00347 }
00348 
00349 /*******************************
00350   CL_InputCursor_Mouse_XWin
00351 *******************************/
00352 
00353 CL_InputCursor_Mouse_XWin::CL_InputCursor_Mouse_XWin(
00354         CL_XWindow_CompatibleCard *_card)
00355 {
00356         card = _card;
00357         x = 0;
00358         y = 0;
00359 }
00360 
00361 CL_InputCursor_Mouse_XWin::~CL_InputCursor_Mouse_XWin()
00362 {
00363 }
00364 
00365 
00366 float CL_InputCursor_Mouse_XWin::get_x()
00367 {
00368         return x;
00369 }
00370 
00371 float CL_InputCursor_Mouse_XWin::get_y()
00372 {
00373         return y;
00374 }
00375 
00376 float CL_InputCursor_Mouse_XWin::get_max_x()
00377 {
00378         return card->get_width();
00379 }
00380 
00381 float CL_InputCursor_Mouse_XWin::get_max_y()
00382 {
00383         return card->get_height();
00384 }
00385 
00386 /*******************************
00387   CL_InputAxis_Mouse_XWin
00388 *******************************/
00389 
00390 CL_InputAxis_Mouse_XWin::CL_InputAxis_Mouse_XWin()
00391 {
00392         center = 1;
00393         pos = 0;
00394 }
00395 
00396 CL_InputAxis_Mouse_XWin::~CL_InputAxis_Mouse_XWin()
00397 {
00398 }
00399 
00400 
00401 float CL_InputAxis_Mouse_XWin::get_pos()
00402 {
00403         return (pos - center) / center;
00404 }
00405 
00406 #endif

Generated at Wed Apr 4 19:54:01 2001 for ClanLib by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001