Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*
  2.  * OpenTyrian: A modern cross-platform port of Tyrian
  3.  * Copyright (C) 2007-2009  The OpenTyrian Development Team
  4.  *
  5.  * This program is free software; you can redistribute it and/or
  6.  * modify it under the terms of the GNU General Public License
  7.  * as published by the Free Software Foundation; either version 2
  8.  * of the License, or (at your option) any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  18.  */
  19. #include "keyboard.h"
  20.  
  21. #include "joystick.h"
  22. #include "network.h"
  23. #include "opentyr.h"
  24. #include "video.h"
  25. #include "video_scale.h"
  26.  
  27. #include "SDL.h"
  28.  
  29. #include <stdio.h>
  30.  
  31.  
  32. JE_boolean ESCPressed;
  33.  
  34. JE_boolean newkey, newmouse, keydown, mousedown;
  35. SDLKey lastkey_sym;
  36. SDLMod lastkey_mod;
  37. unsigned char lastkey_char;
  38. Uint8 lastmouse_but;
  39. Uint16 lastmouse_x, lastmouse_y;
  40. JE_boolean mouse_pressed[3] = {false, false, false};
  41. Uint16 mouse_x, mouse_y;
  42.  
  43. Uint8 keysactive[SDLK_LAST];
  44.  
  45. #ifdef NDEBUG
  46. bool input_grab_enabled = true;
  47. #else
  48. bool input_grab_enabled = false;
  49. #endif
  50.  
  51.  
  52. void flush_events_buffer( void )
  53. {
  54.         SDL_Event ev;
  55.  
  56.         while (SDL_PollEvent(&ev));
  57. }
  58.  
  59. void wait_input( JE_boolean keyboard, JE_boolean mouse, JE_boolean joystick )
  60. {
  61.         service_SDL_events(false);
  62.         while (!((keyboard && keydown) || (mouse && mousedown) || (joystick && joydown)))
  63.         {
  64.                 uSDL_Delay(SDL_POLL_INTERVAL);
  65.                 push_joysticks_as_keyboard();
  66.                 service_SDL_events(false);
  67.                
  68. #ifdef WITH_NETWORK
  69.                 if (isNetworkGame)
  70.                         network_check();
  71. #endif
  72.         }
  73. }
  74.  
  75. void wait_noinput( JE_boolean keyboard, JE_boolean mouse, JE_boolean joystick )
  76. {
  77.         service_SDL_events(false);
  78.         while ((keyboard && keydown) || (mouse && mousedown) || (joystick && joydown))
  79.         {
  80.                 uSDL_Delay(SDL_POLL_INTERVAL);
  81.                 poll_joysticks();
  82.                 service_SDL_events(false);
  83.                
  84. #ifdef WITH_NETWORK
  85.                 if (isNetworkGame)
  86.                         network_check();
  87. #endif
  88.         }
  89. }
  90.  
  91. void init_keyboard( void )
  92. {
  93.         SDL_EnableKeyRepeat(500, 60);
  94.  
  95.         newkey = newmouse = false;
  96.         keydown = mousedown = false;
  97.  
  98.         SDL_EnableUNICODE(1);
  99. }
  100.  
  101. void input_grab( bool enable )
  102. {
  103. #if defined(TARGET_GP2X) || defined(TARGET_DINGUX)
  104.         enable = true;
  105. #endif
  106.        
  107.         input_grab_enabled = enable || fullscreen_enabled;
  108.        
  109.         SDL_ShowCursor(input_grab_enabled ? SDL_DISABLE : SDL_ENABLE);
  110. #ifdef NDEBUG
  111.         SDL_WM_GrabInput(input_grab_enabled ? SDL_GRAB_ON : SDL_GRAB_OFF);
  112. #endif
  113. }
  114.  
  115. JE_word JE_mousePosition( JE_word *mouseX, JE_word *mouseY )
  116. {
  117.         service_SDL_events(false);
  118.         *mouseX = mouse_x;
  119.         *mouseY = mouse_y;
  120.         return mousedown ? lastmouse_but : 0;
  121. }
  122.  
  123. void set_mouse_position( int x, int y )
  124. {
  125.         if (input_grab_enabled)
  126.         {
  127.                 SDL_WarpMouse(x * scalers[scaler].width / vga_width, y * scalers[scaler].height / vga_height);
  128.                 mouse_x = x;
  129.                 mouse_y = y;
  130.         }
  131. }
  132.  
  133. void service_SDL_events( JE_boolean clear_new )
  134. {
  135.         SDL_Event ev;
  136.        
  137.         if (clear_new)
  138.                 newkey = newmouse = false;
  139.        
  140.         while (SDL_PollEvent(&ev))
  141.         {
  142.                 switch (ev.type)
  143.                 {
  144.                         case SDL_ACTIVEEVENT:
  145.                                 if (ev.active.state == SDL_APPINPUTFOCUS && !ev.active.gain)
  146.                                         input_grab(false);
  147.                                 break;
  148.                        
  149.                         case SDL_MOUSEMOTION:
  150.                                 mouse_x = ev.motion.x * vga_width / scalers[scaler].width;
  151.                                 mouse_y = ev.motion.y * vga_height / scalers[scaler].height;
  152.                                 break;
  153.                         case SDL_KEYDOWN:
  154.                                 if (ev.key.keysym.mod & KMOD_CTRL)
  155.                                 {
  156.                                         /* <ctrl><bksp> emergency kill */
  157.                                         if (ev.key.keysym.sym == SDLK_BACKSPACE)
  158.                                         {
  159.                                                 puts("\n\n\nCtrl+Backspace pressed. Doing emergency quit.\n");
  160.                                                 SDL_Quit();
  161.                                                 exit(1);
  162.                                         }
  163.                                        
  164.                                         /* <ctrl><f10> toggle input grab */
  165.                                         if (ev.key.keysym.sym == SDLK_F10)
  166.                                         {
  167.                                                 input_grab(!input_grab_enabled);
  168.                                                 break;
  169.                                         }
  170.                                 }
  171.                                
  172.                                 if (ev.key.keysym.mod & KMOD_ALT)
  173.                                 {
  174.                                         /* <alt><enter> toggle fullscreen */
  175.                                         if (ev.key.keysym.sym == SDLK_RETURN)
  176.                                         {
  177.                                                 if (!init_scaler(scaler, !fullscreen_enabled) && // try new fullscreen state
  178.                                                     !init_any_scaler(!fullscreen_enabled) &&     // try any scaler in new fullscreen state
  179.                                                     !init_scaler(scaler, fullscreen_enabled))    // revert on fail
  180.                                                 {
  181.                                                         exit(EXIT_FAILURE);
  182.                                                 }
  183.                                                 break;
  184.                                         }
  185.                                        
  186.                                         /* <alt><tab> disable input grab and fullscreen */
  187.                                         if (ev.key.keysym.sym == SDLK_TAB)
  188.                                         {
  189.                                                 if (!init_scaler(scaler, false) &&             // try windowed
  190.                                                     !init_any_scaler(false) &&                 // try any scaler windowed
  191.                                                     !init_scaler(scaler, fullscreen_enabled))  // revert on fail
  192.                                                 {
  193.                                                         exit(EXIT_FAILURE);
  194.                                                 }
  195.                                                
  196.                                                 input_grab(false);
  197.                                                 break;
  198.                                         }
  199.                                 }
  200.  
  201.                                 keysactive[ev.key.keysym.sym] = 1;
  202.                                
  203.                                 newkey = true;
  204.                                 lastkey_sym = ev.key.keysym.sym;
  205.                                 lastkey_mod = ev.key.keysym.mod;
  206.                                 lastkey_char = ev.key.keysym.unicode;
  207.                                 keydown = true;
  208.                                 return;
  209.                         case SDL_KEYUP:
  210.                                 keysactive[ev.key.keysym.sym] = 0;
  211.                                 keydown = false;
  212.                                 return;
  213.                         case SDL_MOUSEBUTTONDOWN:
  214.                                 if (!input_grab_enabled)
  215.                                 {
  216.                                         input_grab(true);
  217.                                         break;
  218.                                 }
  219.                                 // fall through
  220.                         case SDL_MOUSEBUTTONUP:
  221.                                 if (ev.type == SDL_MOUSEBUTTONDOWN)
  222.                                 {
  223.                                         newmouse = true;
  224.                                         lastmouse_but = ev.button.button;
  225.                                         lastmouse_x = ev.button.x * vga_width / scalers[scaler].width;
  226.                                         lastmouse_y = ev.button.y * vga_height / scalers[scaler].height;
  227.                                         mousedown = true;
  228.                                 }
  229.                                 else
  230.                                 {
  231.                                         mousedown = false;
  232.                                 }
  233.                                 switch (ev.button.button)
  234.                                 {
  235.                                         case SDL_BUTTON_LEFT:
  236.                                                 mouse_pressed[0] = mousedown; break;
  237.                                         case SDL_BUTTON_RIGHT:
  238.                                                 mouse_pressed[1] = mousedown; break;
  239.                                         case SDL_BUTTON_MIDDLE:
  240.                                                 mouse_pressed[2] = mousedown; break;
  241.                                 }
  242.                                 break;
  243.                         case SDL_QUIT:
  244.                                 /* TODO: Call the cleanup code here. */
  245.                                 exit(0);
  246.                                 break;
  247.                 }
  248.         }
  249. }
  250.  
  251. void JE_clearKeyboard( void )
  252. {
  253.         // /!\ Doesn't seems important. I think. D:
  254. }
  255.  
  256.