OMOUSEGE.cpp 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656
  1. /*
  2. * Seven Kingdoms: Ancient Adversaries
  3. *
  4. * Copyright 1997,1998 Enlight Software Ltd.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. //Filename : OMOUSE.CPP
  21. //Description : Mouse handling Object
  22. #ifdef GERMAN
  23. #include <ALL.h>
  24. #include <OSYS.h>
  25. #include <OVGA.h>
  26. #include <OPOWER.h>
  27. #include <OMOUSE.h>
  28. #include <OMOUSECR.h>
  29. #include <OMOUSE2.h>
  30. #include <KEY.h>
  31. #include <OVGALOCK.h>
  32. #ifdef WIN32
  33. #include <cctype>
  34. #endif
  35. //--- define the size of a buffer for real-time vga screen updating ---//
  36. #define VGA_UPDATE_BUF_SIZE (100*100) // 100 x 100
  37. // define the graphic key scan code
  38. #define CDIK_GRAPH 0x68
  39. //--------- Define Click Threshold -----------//
  40. //
  41. // Clock tick is incremented 1000 times per second or once per millisecond.
  42. //
  43. // The minimum time interval between consequent mouse click is set to
  44. // = 0.3 seconds
  45. //
  46. //--------------------------------------------//
  47. static DWORD click_threshold = (LONG)(0.3 * 1000);
  48. static unsigned vk_graph = 0; // virtual key code for Graph key
  49. //------- Define static functions -----------//
  50. LRESULT CALLBACK key_hook_proc(int nCode, WORD wParam, LONG lParam);
  51. // static unsigned translate_key(unsigned scanCode, unsigned short skeyState);
  52. //------- define constant --------//
  53. #define MOUSE_BUFFER_SIZE 100
  54. #define KEYB_BUFFER_SIZE 16
  55. #define DOUBLE_SPEED_THREHOLD 8
  56. //------- define static storage -----------//
  57. static DIDEVICEOBJECTDATA mouse_data[MOUSE_BUFFER_SIZE];
  58. static DIDEVICEOBJECTDATA keyb_data[KEYB_BUFFER_SIZE];
  59. static int update_x1, update_y1, update_x2, update_y2; // coordination of the last double-buffer update area
  60. //--------- Start of Mouse::Mouse ---------//
  61. //
  62. Mouse::Mouse()
  63. {
  64. memset( this, 0, sizeof(Mouse) );
  65. double_speed_threshold = DEFAULT_DOUBLE_SPEED_THRESHOLD;
  66. triple_speed_threshold = DEFAULT_TRIPLE_SPEED_THRESHOLD;
  67. }
  68. //---------- End of Mouse::Mouse ---------//
  69. //---------- Begin of Mouse::~Mouse --------//
  70. //
  71. // Deinitialize the mouse driver, reset event handler
  72. //
  73. Mouse::~Mouse()
  74. {
  75. deinit();
  76. }
  77. //------------ End of Mouse::~Mouse --------//
  78. //------------ Start of Mouse::init ------------//
  79. //
  80. void Mouse::init(HINSTANCE hinst, HWND hwnd, LPDIRECTINPUT createdDirectInput)
  81. {
  82. //-------- set starting position ---------//
  83. POINT pt;
  84. GetCursorPos(&pt);
  85. cur_x = pt.x;
  86. cur_y = pt.y;
  87. /*
  88. //------ install keyboard hook ---------//
  89. key_hook_handle = SetWindowsHookEx(WH_KEYBOARD, (HOOKPROC) key_hook_proc, sys.app_hinstance, NULL);
  90. if( !key_hook_handle )
  91. err.run( "Failed installing keyboard hook." );
  92. */
  93. //-------- initialize DirectInput Mouse device--------//
  94. /*
  95. direct_mouse_handle = DMouseOpen();
  96. if( !direct_mouse_handle )
  97. err.run( "Failed installing direct mouse." );
  98. */
  99. HRESULT hr;
  100. if( createdDirectInput )
  101. {
  102. direct_input = createdDirectInput;
  103. hr = direct_input->AddRef();
  104. }
  105. else
  106. {
  107. hr = DirectInputCreate(hinst, DIRECTINPUT_VERSION, &direct_input, NULL);
  108. }
  109. if(hr)
  110. err.run( "Failed creating DirectInput");
  111. // ####### begin Gilbert 12/9 #######//
  112. {
  113. VgaFrontLock vgaLock;
  114. hr = direct_input->CreateDevice(GUID_SysMouse,&di_mouse_device,NULL);
  115. }
  116. // ####### end Gilbert 12/9 #######//
  117. if(hr)
  118. err.run( "Failed creating mouse interface from DirectInput");
  119. // ------- set cooperative level --------//
  120. hr = di_mouse_device->SetCooperativeLevel(hwnd, DISCL_FOREGROUND | DISCL_EXCLUSIVE);
  121. // ------- set data format ---------//
  122. if(!hr)
  123. hr = di_mouse_device->SetDataFormat(&c_dfDIMouse);
  124. // ------- set relative coordinate mode --------//
  125. // DirectInput default is relative
  126. if(!hr)
  127. {
  128. DIPROPDWORD propDword;
  129. propDword.diph.dwSize = sizeof(DIPROPDWORD);
  130. propDword.diph.dwHeaderSize = sizeof(DIPROPHEADER);
  131. propDword.diph.dwHow = DIPH_DEVICE; // Entire device
  132. propDword.diph.dwObj = 0; // Entire device, so zero
  133. propDword.dwData = DIPROPAXISMODE_REL;
  134. hr = di_mouse_device->SetProperty(DIPROP_AXISMODE, &propDword.diph);
  135. }
  136. // ------- set buffer size --------//
  137. if(!hr)
  138. {
  139. DIPROPDWORD propDword;
  140. propDword.diph.dwSize = sizeof(DIPROPDWORD);
  141. propDword.diph.dwHeaderSize = sizeof(DIPROPHEADER);
  142. propDword.diph.dwHow = DIPH_DEVICE; // Entire device
  143. propDword.diph.dwObj = 0; // Entire device, so zero
  144. propDword.dwData = MOUSE_BUFFER_SIZE * sizeof(DIDEVICEOBJECTDATA);
  145. hr = di_mouse_device->SetProperty(DIPROP_BUFFERSIZE, &propDword.diph);
  146. }
  147. if(hr)
  148. err.run( "Failed initializing mouse interface");
  149. // ------- create direct input keyboard interface --------- //
  150. // ####### begin Gilbert 12/9 #######//
  151. {
  152. VgaFrontLock vgaLock;
  153. hr = direct_input->CreateDevice(GUID_SysKeyboard,&di_keyb_device,NULL);
  154. }
  155. // ####### end Gilbert 12/9 #######//
  156. if(hr)
  157. err.run( "Failed creating keyboard interface from DirectInput");
  158. // ------- set cooperative level --------//
  159. hr = di_keyb_device->SetCooperativeLevel(hwnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);
  160. // ------- set data format ---------//
  161. if(!hr)
  162. hr = di_keyb_device->SetDataFormat(&c_dfDIKeyboard);
  163. // ------- set buffer size --------//
  164. if(!hr)
  165. {
  166. DIPROPDWORD propDword;
  167. propDword.diph.dwSize = sizeof(DIPROPDWORD);
  168. propDword.diph.dwHeaderSize = sizeof(DIPROPHEADER);
  169. propDword.diph.dwHow = DIPH_DEVICE; // Entire device
  170. propDword.diph.dwObj = 0; // Entire device, so zero
  171. propDword.dwData = KEYB_BUFFER_SIZE * sizeof(DIDEVICEOBJECTDATA);
  172. hr = di_keyb_device->SetProperty(DIPROP_BUFFERSIZE, &propDword.diph);
  173. }
  174. if(hr)
  175. err.run( "Failed initializing keyboard interface");
  176. // ------- get initial keyboard state ----------//
  177. skey_state = 0;
  178. if(GetKeyState(VK_LSHIFT) < 0)
  179. skey_state |= LEFT_SHIFT_KEY_MASK;
  180. if(GetKeyState(VK_RSHIFT) < 0)
  181. skey_state |= RIGHT_SHIFT_KEY_MASK;
  182. if(GetKeyState(VK_LCONTROL) < 0)
  183. skey_state |= LEFT_CONTROL_KEY_MASK;
  184. if(GetKeyState(VK_RCONTROL) < 0)
  185. skey_state |= RIGHT_CONTROL_KEY_MASK;
  186. if(GetKeyState(VK_LMENU) < 0)
  187. skey_state |= LEFT_ALT_KEY_MASK;
  188. if(GetKeyState(VK_RMENU) < 0)
  189. skey_state |= RIGHT_ALT_KEY_MASK;
  190. if(GetKeyState(VK_NUMLOCK) & 1)
  191. skey_state |= NUM_LOCK_STATE_MASK;
  192. if(GetKeyState(VK_CAPITAL) & 1)
  193. skey_state |= CAP_LOCK_STATE_MASK;
  194. if(GetKeyState(VK_SCROLL) & 1)
  195. skey_state |= SCROLL_LOCK_STATE_MASK;
  196. skey_state |= INSERT_STATE_MASK; // enable insert mode by default
  197. // find the virtual key code of graph key
  198. vk_graph = MapVirtualKey(CDIK_GRAPH, 3 ); // convert scan code to virtual key code
  199. if( vk_graph )
  200. {
  201. if(GetKeyState(vk_graph) < 0)
  202. skey_state |= GRAPH_KEY_MASK;
  203. }
  204. //------- initialize VGA update buffer -------//
  205. vga_update_buf = mem_add( VGA_UPDATE_BUF_SIZE );
  206. // ------ initialize mouse boundary ---------//
  207. reset_boundary();
  208. // ------- initialize event queue ---------//
  209. head_ptr = tail_ptr = 0;
  210. //--------------------------------------------//
  211. init_flag = 1;
  212. }
  213. //------------- End of Mouse::init -------------//
  214. //------------ Start of Mouse::deinit ------------//
  215. //
  216. void Mouse::deinit()
  217. {
  218. if( init_flag )
  219. {
  220. // UnhookWindowsHookEx(key_hook_handle);
  221. // DMouseClose(direct_mouse_handle);
  222. init_flag = 0;
  223. }
  224. if( vga_update_buf )
  225. {
  226. mem_del(vga_update_buf);
  227. vga_update_buf = NULL;
  228. }
  229. if( di_keyb_device )
  230. {
  231. di_keyb_device->Unacquire();
  232. di_keyb_device->Release();
  233. di_keyb_device = NULL;
  234. }
  235. if( di_mouse_device )
  236. {
  237. di_mouse_device->Unacquire();
  238. di_mouse_device->Release();
  239. di_mouse_device = NULL;
  240. }
  241. if( direct_input )
  242. {
  243. direct_input->Release();
  244. direct_input = NULL;
  245. }
  246. }
  247. //------------- End of Mouse::deinit -------------//
  248. //--------- Start of Mouse::hide -------//
  249. //
  250. // Suspend the mouse function, use resume() to resume to function
  251. //
  252. void Mouse::hide()
  253. {
  254. mouse_cursor.hide_all_flag=1;
  255. mouse_cursor.process(cur_x, cur_y);
  256. }
  257. //---------- End of Mouse::hide --------//
  258. //--------- Start of Mouse::show -------//
  259. //
  260. // Resume the mouse function which is previously hideed by hide()
  261. //
  262. void Mouse::show()
  263. {
  264. mouse_cursor.hide_all_flag=0;
  265. mouse_cursor.process(cur_x, cur_y);
  266. }
  267. //---------- End of Mouse::show --------//
  268. //--------- Begin of Mouse::hide_area ----------//
  269. //
  270. void Mouse::hide_area(int x1, int y1, int x2, int y2)
  271. {
  272. mouse_cursor.hide_area_flag++;
  273. if( mouse_cursor.hide_area_flag!=1 ) // only process for the first call of hide_area()
  274. return;
  275. mouse_cursor.hide_x1 = x1;
  276. mouse_cursor.hide_y1 = y1;
  277. mouse_cursor.hide_x2 = x2;
  278. mouse_cursor.hide_y2 = y2;
  279. int curX = cur_x - mouse_cursor.hot_spot_x;
  280. int curY = cur_y - mouse_cursor.hot_spot_y;
  281. if( m.is_touch( x1, y1, x2, y2, curX, curY,
  282. curX+mouse_cursor.icon_width-1,
  283. curY+mouse_cursor.icon_height-1 ) )
  284. {
  285. if( handle_flicking )
  286. {
  287. update_x1 = min(x1, curX);
  288. update_y1 = min(y1, curY);
  289. update_x2 = max(x2, curX+mouse_cursor.icon_width-1);
  290. update_y2 = max(y2, curY+mouse_cursor.icon_height-1);
  291. update_x1 = max(0, update_x1);
  292. update_y1 = max(0, update_y1);
  293. update_x2 = min(VGA_WIDTH-1 , update_x2);
  294. update_y2 = min(VGA_HEIGHT-1, update_y2);
  295. err_when( (update_x2-update_x1+1) * (update_y2-update_y1+1) > VGA_UPDATE_BUF_SIZE );
  296. //---- save the update area of the back buf to a temp buffer ----//
  297. vga_back.read_bitmap( update_x1, update_y1, update_x2, update_y2, vga_update_buf );
  298. //--- copy the update area from the front buf to the back buf ---//
  299. IMGcopy( vga_back.buf_ptr(), vga_back.buf_pitch(), vga_front.buf_ptr(), vga_front.buf_pitch(),
  300. update_x1, update_y1, update_x2, update_y2 );
  301. //-- redirect the front VGA buffer pointer to the back VGA buffer --//
  302. vga_front.set_buf_ptr( vga_back.buf_ptr() );
  303. }
  304. //------ hide up the mouse cursor --------//
  305. mouse_cursor.process(cur_x, cur_y);
  306. }
  307. }
  308. //--------- End of Mouse::hide_area --------------//
  309. //--------- Begin of Mouse::show_area ----------//
  310. //
  311. void Mouse::show_area()
  312. {
  313. mouse_cursor.hide_area_flag--;
  314. if( mouse_cursor.hide_area_flag!=0 ) // only process for the first call of hide_area()
  315. return;
  316. int curX = cur_x - mouse_cursor.hot_spot_x;
  317. int curY = cur_y - mouse_cursor.hot_spot_y;
  318. if( m.is_touch( mouse_cursor.hide_x1, mouse_cursor.hide_y1,
  319. mouse_cursor.hide_x2, mouse_cursor.hide_y2,
  320. curX, curY, curX+mouse_cursor.icon_width-1,
  321. curY+mouse_cursor.icon_height-1 ) )
  322. {
  323. //----- redisplay the mouse cursor ------//
  324. mouse_cursor.process(cur_x, cur_y);
  325. if( handle_flicking )
  326. {
  327. //--- copy the update area from the back buf to the front buf ---//
  328. IMGcopy( vga_front.buf_ptr(), vga_front.buf_pitch(), vga_back.buf_ptr(), vga_back.buf_pitch(),
  329. update_x1, update_y1, update_x2, update_y2 );
  330. //--- restore the update area of the back buf with the temp buffer ---//
  331. vga_back.fast_put_bitmap( update_x1, update_y1, vga_update_buf );
  332. //--- restore the VGA front buffer's buf ptr ---//
  333. vga_front.set_default_buf_ptr();
  334. }
  335. }
  336. }
  337. //--------- End of Mouse::show_area --------------//
  338. //--------- Start of Mouse::add_event ---------//
  339. //
  340. // Called by handler interrupt to procss the state
  341. //
  342. void Mouse::add_event(MouseEvent *mouseEvent)
  343. {
  344. //---- call the game object to see if the mouse cursor icon needs to be changed, or if the nation selection square needs to be activated ----//
  345. power.mouse_handler();
  346. //--------- update the mouse cursor ----------//
  347. mouse_cursor.process(cur_x, cur_y); // repaint mouse cursor
  348. //-------- save state into the event queue --------//
  349. if((head_ptr == tail_ptr-1) || // see if the buffer is full
  350. (head_ptr == EVENT_BUFFER_SIZE-1 && tail_ptr == 0))
  351. {
  352. return;
  353. }
  354. event_buffer[head_ptr] = *mouseEvent;
  355. if(++head_ptr >= EVENT_BUFFER_SIZE) // increment the head ptr
  356. head_ptr = 0;
  357. }
  358. //----------- End of Mouse::add_event ----------//
  359. //--------- Start of Mouse::add_key_event ---------//
  360. //
  361. // Called by key handler to save the key pressed
  362. //
  363. void Mouse::add_key_event(unsigned scanCode, DWORD timeStamp)
  364. {
  365. if((head_ptr == tail_ptr-1) || // see if the buffer is full
  366. (head_ptr == EVENT_BUFFER_SIZE-1 && tail_ptr == 0))
  367. {
  368. return;
  369. }
  370. MouseEvent *ev = event_buffer + head_ptr;
  371. ev->event_type = KEY_PRESS;
  372. ev->scan_code = scanCode;
  373. ev->skey_state = skey_state;
  374. ev->time = timeStamp;
  375. // put mouse state
  376. // ev->state = 0; //ev->state = left_press | right_press;
  377. ev->x = cur_x;
  378. ev->y = cur_y;
  379. if(++head_ptr >= EVENT_BUFFER_SIZE) // increment the head ptr
  380. head_ptr = 0;
  381. }
  382. //----------- End of Mouse::add_key_event ----------//
  383. //--------- Start of Mouse::get_event ---------//
  384. //
  385. // Get next event from the event buffer
  386. //
  387. // return : <int> 1 - event fetched from the event queue
  388. // 0 - not event remained in the buffer
  389. //
  390. // to know what type of event :
  391. // 1. check is_mouse_event() or is_key_event()
  392. // 2. if is_mouse_event(), check mouse_event_type
  393. // if( LEFT_BUTTON or LEFT_BUTTON_RELEASE, read click_buffer[LEFT_BUTTON]
  394. // if( RIGHT_BUTTON or RIGHT_BUTTON_RELEASE, read click_buffer[RIGHT_BUTTON]
  395. // 3. if is_key_event(), check event_skey_state, scan_code and key_code
  396. //
  397. int Mouse::get_event()
  398. {
  399. if(head_ptr == tail_ptr) // no event queue left in the buffer
  400. {
  401. scan_code =0; // no keyboard event
  402. key_code =0;
  403. has_mouse_event=0; // no mouse event
  404. return 0;
  405. }
  406. //--------- get event from queue ---------//
  407. MouseEvent* eptr = event_buffer + tail_ptr;
  408. MouseClick* cptr;
  409. event_skey_state = eptr->skey_state;
  410. mouse_event_type = eptr->event_type;
  411. switch( eptr->event_type )
  412. {
  413. case LEFT_BUTTON:
  414. case RIGHT_BUTTON:
  415. // set count of other button to zero
  416. click_buffer[LEFT_BUTTON+RIGHT_BUTTON-eptr->event_type].count = 0;
  417. cptr = click_buffer + eptr->event_type;
  418. if( //eptr->event_type == LEFT_BUTTON && // left button has double click
  419. eptr->time - cptr->time < click_threshold )
  420. cptr->count++;
  421. else
  422. cptr->count = 1;
  423. cptr->time = eptr->time;
  424. cptr->x = eptr->x;
  425. cptr->y = eptr->y;
  426. scan_code = 0;
  427. key_code = 0;
  428. has_mouse_event = 1;
  429. break;
  430. case KEY_PRESS:
  431. scan_code = eptr->scan_code;
  432. key_code = mouse.is_key(mouse.scan_code, mouse.event_skey_state, (WORD) 0, K_CHAR_KEY);
  433. has_mouse_event = 0;
  434. break;
  435. case LEFT_BUTTON_RELEASE:
  436. case RIGHT_BUTTON_RELEASE:
  437. cptr = click_buffer + eptr->event_type - LEFT_BUTTON_RELEASE;
  438. cptr->release_time = eptr->time;
  439. cptr->release_x = eptr->x;
  440. cptr->release_y = eptr->y;
  441. scan_code = 0;
  442. key_code = 0;
  443. has_mouse_event = 1;
  444. break;
  445. case KEY_RELEASE:
  446. // no action
  447. break;
  448. default:
  449. err_here();
  450. }
  451. if(++tail_ptr >= EVENT_BUFFER_SIZE)
  452. tail_ptr = 0;
  453. return 1;
  454. }
  455. //----------- End of Mouse::get_event ----------//
  456. //--------- Begin of Mouse::in_area ----------//
  457. //
  458. // <Real-time access>
  459. //
  460. // Detect whether mouse cursor is in the specified area
  461. //
  462. // <int> x1,y1,x2,y2 = the coordinations of the area
  463. //
  464. // Return : 1 - if the mouse cursor is in the area
  465. // 0 - if not
  466. //
  467. int Mouse::in_area(int x1, int y1, int x2, int y2)
  468. {
  469. return( cur_x >= x1 && cur_y >= y1 && cur_x <= x2 && cur_y <= y2 );
  470. }
  471. //--------- End of Mouse::in_area --------------//
  472. //--------- Begin of Mouse::press_area ----------//
  473. //
  474. // <Real-time access>
  475. //
  476. // Detect whether the specified area has been pressed by mouse
  477. //
  478. // <int> x1,y1,x2,y2 = the coordinations of the area
  479. // <int> buttonId = which button ( 0=left, 1-right, 2-left or right )
  480. //
  481. // Return : 1 - if the area has been pressed (left button)
  482. // 1 - if the area has been pressed (right button)
  483. // 0 - if not
  484. //
  485. int Mouse::press_area(int x1, int y1, int x2, int y2, int buttonId)
  486. {
  487. if( cur_x >= x1 && cur_y >= y1 && cur_x <= x2 && cur_y <= y2 )
  488. {
  489. if( left_press && (buttonId==0 || buttonId==2) ) // Left button
  490. return 1;
  491. if( right_press && (buttonId==1 || buttonId==2) ) // Right button
  492. return 2;
  493. }
  494. return 0;
  495. }
  496. //--------- End of Mouse::press_area --------------//
  497. //--------- Begin of Mouse::set_boundary ----------//
  498. //
  499. // for each parameter, put -1 to mean unchange
  500. //
  501. void Mouse::set_boundary(int x1, int y1, int x2, int y2)
  502. {
  503. if( x1 >= 0)
  504. bound_x1 = x1;
  505. if( y1 >= 0)
  506. bound_y1 = y1;
  507. if( x2 >= 0)
  508. bound_x2 = x2 > MOUSE_X_UPPER_LIMIT ? MOUSE_X_UPPER_LIMIT : x2;
  509. if( y2 >= 0)
  510. bound_y2 = y2 > MOUSE_Y_UPPER_LIMIT ? MOUSE_Y_UPPER_LIMIT : y2;
  511. }
  512. //--------- End of Mouse::set_boundary ----------//
  513. //--------- Begin of Mouse::reset_boundary ----------//
  514. void Mouse::reset_boundary()
  515. {
  516. bound_x1 = 0;
  517. bound_y1 = 0;
  518. bound_x2 = MOUSE_X_UPPER_LIMIT;
  519. bound_y2 = MOUSE_Y_UPPER_LIMIT;
  520. }
  521. //--------- End of Mouse::set_boundary ----------//
  522. //--------- Begin of Mouse::single_click ----------//
  523. //
  524. // <Event queue access>
  525. //
  526. // Detect whether the specified area has been single clicked by mouse
  527. //
  528. // <int> x1,y1,x2,y2 = the coordinations of the area
  529. // [int] buttonId = which button ( 0=left, 1-right, 2-both)
  530. // (default:0)
  531. //
  532. // Return : 1 - if the area has been clicked (left click)
  533. // 2 - if the area has been clicked (right click)
  534. // 0 - if not
  535. //
  536. int Mouse::single_click(int x1, int y1, int x2, int y2,int buttonId)
  537. {
  538. if( !has_mouse_event )
  539. return 0;
  540. MouseClick* cptr;
  541. if( buttonId==0 || buttonId==2 ) // left button
  542. {
  543. cptr = click_buffer+LEFT_BUTTON;
  544. if( mouse_event_type == LEFT_BUTTON
  545. && cptr->count == 1
  546. && cptr->x >= x1 && cptr->y >= y1
  547. && cptr->x <= x2 && cptr->y <= y2 )
  548. {
  549. return 1;
  550. }
  551. }
  552. if( buttonId==1 || buttonId==2 ) // right button
  553. {
  554. cptr = click_buffer+RIGHT_BUTTON;
  555. if( mouse_event_type == RIGHT_BUTTON
  556. && cptr->count == 1
  557. && cptr->x >= x1 && cptr->y >= y1
  558. && cptr->x <= x2 && cptr->y <= y2 )
  559. {
  560. return 2;
  561. }
  562. }
  563. return 0;
  564. }
  565. //--------- End of Mouse::single_click --------------//
  566. //--------- Begin of Mouse::double_click ----------//
  567. //
  568. // <Event queue access>
  569. //
  570. // Detect whether the specified area has been double clicked by mouse
  571. //
  572. // Note : when a mouse double click, it will FIRST generate a SINGLE
  573. // click signal and then a DOUBLE click signal.
  574. // Because a double click is consisted of two single click
  575. //
  576. // <int> x1,y1,x2,y2 = the coordinations of the area
  577. // [int] buttonId = which button ( 0=left, 1-right, 2-left or right)
  578. // (default:0)
  579. //
  580. // Return : 1 - if the area has been clicked
  581. // 0 - if not
  582. //
  583. int Mouse::double_click(int x1, int y1, int x2, int y2,int buttonId)
  584. {
  585. if( !has_mouse_event )
  586. return 0;
  587. MouseClick* cptr;
  588. if( buttonId==0 || buttonId==2 ) // left button
  589. {
  590. cptr = click_buffer+LEFT_BUTTON;
  591. if( mouse_event_type == LEFT_BUTTON
  592. && cptr->count == 2
  593. && cptr->x >= x1 && cptr->y >= y1
  594. && cptr->x <= x2 && cptr->y <= y2 )
  595. {
  596. return 1;
  597. }
  598. }
  599. if( buttonId==1 || buttonId==2 ) // right button
  600. {
  601. cptr = click_buffer+RIGHT_BUTTON;
  602. if( mouse_event_type == RIGHT_BUTTON
  603. && cptr->count == 2
  604. && cptr->x >= x1 && cptr->y >= y1
  605. && cptr->x <= x2 && cptr->y <= y2 )
  606. {
  607. return 1;
  608. }
  609. }
  610. return 0;
  611. }
  612. //--------- End of Mouse::double_click --------------//
  613. //--------- Begin of Mouse::any_click ----------//
  614. //
  615. // <Event queue access>
  616. //
  617. // Detect whether the specified area has been single or double clicked by mouse
  618. //
  619. // <int> x1,y1,x2,y2 = the coordinations of the area
  620. // [int] buttonId = which button ( 0=left, 1-right, 2-left or right)
  621. // (default:0)
  622. //
  623. // Return : >0 - the no. of click if the area has been clicked
  624. // 0 - if not
  625. //
  626. int Mouse::any_click(int x1, int y1, int x2, int y2,int buttonId)
  627. {
  628. if( !has_mouse_event )
  629. return 0;
  630. MouseClick* cptr;
  631. if( buttonId==0 || buttonId==2 ) // left button
  632. {
  633. cptr = click_buffer+LEFT_BUTTON;
  634. if( mouse_event_type == LEFT_BUTTON
  635. && cptr->count >= 1
  636. && cptr->x >= x1 && cptr->y >= y1
  637. && cptr->x <= x2 && cptr->y <= y2 )
  638. {
  639. return cptr->count;
  640. }
  641. }
  642. if( buttonId==1 || buttonId==2 ) // right button
  643. {
  644. cptr = click_buffer+RIGHT_BUTTON;
  645. if( mouse_event_type == RIGHT_BUTTON
  646. && cptr->count >= 1
  647. && cptr->x >= x1 && cptr->y >= y1
  648. && cptr->x <= x2 && cptr->y <= y2 )
  649. {
  650. return cptr->count;
  651. }
  652. }
  653. return 0;
  654. }
  655. //--------- End of Mouse::any_click --------------//
  656. //--------- Begin of Mouse::any_click ----------//
  657. //
  658. // <Event queue access>
  659. //
  660. // Detect whether the specified area has been single or double clicked by mouse
  661. // Only check button, don't check mouse coordination
  662. //
  663. // [int] buttonId = which button ( 0=left, 1-right, 2-left or right)
  664. // (default:0)
  665. //
  666. // Return : >0 - the no. of click if the area has been clicked
  667. // 0 - if not
  668. //
  669. int Mouse::any_click(int buttonId)
  670. {
  671. if( !has_mouse_event )
  672. return 0;
  673. MouseClick* cptr;
  674. if( buttonId==0 || buttonId==2 ) // left button
  675. {
  676. cptr = click_buffer+LEFT_BUTTON;
  677. if( mouse_event_type == LEFT_BUTTON && cptr->count >= 1 )
  678. return cptr->count;
  679. }
  680. if( buttonId==1 || buttonId==2 ) // right button
  681. {
  682. cptr = click_buffer+RIGHT_BUTTON;
  683. if( mouse_event_type == RIGHT_BUTTON && cptr->count >= 1 )
  684. return cptr->count;
  685. }
  686. return 0;
  687. }
  688. //--------- End of Mouse::any_click --------------//
  689. //--------- Begin of Mouse::release_click ----------//
  690. //
  691. // <Event queue access>
  692. //
  693. // Detect whether the specified area has been released button by mouse
  694. //
  695. // <int> x1,y1,x2,y2 = the coordinations of the area
  696. // [int] buttonId = which button ( 0=left, 1-right, 2-both)
  697. // (default:0)
  698. //
  699. // Return : 1 - if the area has been clicked (left click)
  700. // 2 - if the area has been clicked (right click)
  701. // 0 - if not
  702. //
  703. int Mouse::release_click(int x1, int y1, int x2, int y2,int buttonId)
  704. {
  705. if( !has_mouse_event )
  706. return 0;
  707. MouseClick* cptr;
  708. if( buttonId==0 || buttonId==2 ) // left button
  709. {
  710. cptr = click_buffer+LEFT_BUTTON;
  711. if( mouse_event_type == LEFT_BUTTON_RELEASE
  712. && cptr->release_x >= x1 && cptr->release_y >= y1
  713. && cptr->release_x <= x2 && cptr->release_y <= y2 )
  714. {
  715. return 1;
  716. }
  717. }
  718. if( buttonId==1 || buttonId==2 ) // right button
  719. {
  720. cptr = click_buffer+RIGHT_BUTTON;
  721. if( mouse_event_type == RIGHT_BUTTON_RELEASE
  722. && cptr->release_x >= x1 && cptr->release_y >= y1
  723. && cptr->release_x <= x2 && cptr->release_y <= y2 )
  724. {
  725. return 2;
  726. }
  727. }
  728. return 0;
  729. }
  730. //--------- End of Mouse::release_click --------------//
  731. //------- Begin of function key_hook_proc --------//
  732. LRESULT CALLBACK key_hook_proc(int nCode, WORD wParam, LONG lParam)
  733. {
  734. if (nCode >= 0)
  735. mouse.add_key_event(wParam, m.get_time());
  736. return CallNextHookEx(mouse.key_hook_handle, nCode, wParam, lParam);
  737. }
  738. //-------- End of function key_hook_proc --------//
  739. //--------- Begin of Mouse::poll_event ----------//
  740. //
  741. // Poll mouse events from the direct mouse VXD.
  742. //
  743. void Mouse::poll_event()
  744. {
  745. if(!init_flag)
  746. return;
  747. // ---- acquire mouse device and count the message queued ----//
  748. HRESULT hr;
  749. if( (hr = di_mouse_device->Acquire()) != DI_OK && hr != S_FALSE)
  750. return;
  751. if( (hr = di_keyb_device->Acquire()) != DI_OK && hr != S_FALSE)
  752. return;
  753. // HRESULT hr;
  754. DWORD mouseLen, keybLen;
  755. int moveFlag = 0;
  756. while( 1 )
  757. {
  758. mouseLen = MOUSE_BUFFER_SIZE;
  759. keybLen = KEYB_BUFFER_SIZE;
  760. if( (hr = di_mouse_device->GetDeviceData( sizeof(DIDEVICEOBJECTDATA),
  761. mouse_data, &mouseLen, 0)) != DI_OK && hr != S_FALSE )
  762. mouseLen = 0;
  763. if( (hr = di_keyb_device->GetDeviceData( sizeof(DIDEVICEOBJECTDATA),
  764. keyb_data, &keybLen, 0)) != DI_OK && hr != S_FALSE)
  765. keybLen = 0;
  766. if( !mouseLen && !keybLen )
  767. break;
  768. DIDEVICEOBJECTDATA *mouseMsg = mouse_data, *keybMsg = keyb_data;
  769. while( mouseLen > 0 || keybLen > 0)
  770. {
  771. // merge mouse event and keyboard event
  772. MouseEvent ev;
  773. int earlyDevice = 0; // 1 = mouse, 2 = keyboard
  774. if( mouseLen > 0 && keybLen > 0 )
  775. {
  776. if( DISEQUENCE_COMPARE(mouseMsg->dwSequence, <=, keybMsg->dwSequence) )
  777. earlyDevice = 1;
  778. else
  779. earlyDevice = 2;
  780. }
  781. else if( mouseLen > 0)
  782. {
  783. earlyDevice = 1;
  784. }
  785. else if( keybLen > 0)
  786. {
  787. earlyDevice = 2;
  788. }
  789. if( earlyDevice == 1 )
  790. {
  791. if (mouseMsg->dwOfs == DIMOFS_BUTTON0)
  792. {
  793. if( mouseMsg->dwData & 0x80)
  794. {
  795. // mouse button pressed
  796. //ev.state = left_press = LEFT_BUTTON_MASK;
  797. left_press = LEFT_BUTTON_MASK;
  798. ev.event_type = LEFT_BUTTON;
  799. ev.x = cur_x;
  800. ev.y = cur_y;
  801. ev.time = mouseMsg->dwTimeStamp;
  802. ev.scan_code = 0;
  803. ev.skey_state = skey_state;
  804. add_event(&ev);
  805. }
  806. else
  807. {
  808. // mouse button released
  809. left_press = 0;
  810. ev.event_type = LEFT_BUTTON_RELEASE;
  811. ev.x = cur_x;
  812. ev.y = cur_y;
  813. ev.time = mouseMsg->dwTimeStamp;
  814. ev.scan_code = 0;
  815. ev.skey_state = skey_state;
  816. add_event(&ev);
  817. reset_boundary(); // reset_boundary whenever left button is released
  818. }
  819. }
  820. else if (mouseMsg->dwOfs == DIMOFS_BUTTON1)
  821. {
  822. if( mouseMsg->dwData & 0x80)
  823. {
  824. // mouse button pressed
  825. right_press = RIGHT_BUTTON_MASK;
  826. ev.event_type = RIGHT_BUTTON;
  827. ev.x = cur_x;
  828. ev.y = cur_y;
  829. ev.time = mouseMsg->dwTimeStamp;
  830. ev.scan_code = 0;
  831. ev.skey_state = skey_state;
  832. add_event(&ev);
  833. }
  834. else
  835. {
  836. // mouse button released
  837. right_press = 0;
  838. ev.event_type = RIGHT_BUTTON_RELEASE;
  839. ev.x = cur_x;
  840. ev.y = cur_y;
  841. ev.time = mouseMsg->dwTimeStamp;
  842. ev.scan_code = 0;
  843. ev.skey_state = skey_state;
  844. add_event(&ev);
  845. }
  846. }
  847. else if (mouseMsg->dwOfs == DIMOFS_BUTTON2)
  848. {
  849. // not interested
  850. }
  851. else if (mouseMsg->dwOfs == DIMOFS_BUTTON3)
  852. {
  853. // not interested
  854. }
  855. else if (mouseMsg->dwOfs == DIMOFS_X)
  856. {
  857. cur_x += micky_to_displacement(mouseMsg->dwData);
  858. if(cur_x < bound_x1)
  859. cur_x = bound_x1;
  860. if(cur_x > bound_x2)
  861. cur_x = bound_x2;
  862. moveFlag = 1;
  863. }
  864. else if (mouseMsg->dwOfs == DIMOFS_Y)
  865. {
  866. cur_y += micky_to_displacement(mouseMsg->dwData);
  867. if(cur_y < bound_y1)
  868. cur_y = bound_y1;
  869. if(cur_y > bound_y2)
  870. cur_y = bound_y2;
  871. moveFlag = 1;
  872. }
  873. else if (mouseMsg->dwOfs == DIMOFS_Z)
  874. {
  875. // not interested
  876. }
  877. --mouseLen;
  878. ++mouseMsg;
  879. }
  880. else if( earlyDevice == 2 )
  881. {
  882. // trap keys, such as shift, ctrl, alt, numlock, caplock and scrolllock
  883. // add to event if the key is none of the above
  884. // for english to emulate <alt graph> key
  885. //if( keybMsg->dwOfs == DIK_RMENU )
  886. // keybMsg->dwOfs = CDIK_GRAPH;
  887. switch( keybMsg->dwOfs )
  888. {
  889. case DIK_LSHIFT:
  890. if( keybMsg->dwData & 0x80 )
  891. skey_state |= LEFT_SHIFT_KEY_MASK;
  892. else
  893. skey_state &= ~LEFT_SHIFT_KEY_MASK;
  894. break;
  895. case DIK_RSHIFT:
  896. if( keybMsg->dwData & 0x80 )
  897. skey_state |= RIGHT_SHIFT_KEY_MASK;
  898. else
  899. skey_state &= ~RIGHT_SHIFT_KEY_MASK;
  900. break;
  901. case DIK_LCONTROL:
  902. if( keybMsg->dwData & 0x80 )
  903. skey_state |= LEFT_CONTROL_KEY_MASK;
  904. else
  905. skey_state &= ~LEFT_CONTROL_KEY_MASK;
  906. break;
  907. case DIK_RCONTROL:
  908. if( keybMsg->dwData & 0x80 )
  909. skey_state |= RIGHT_CONTROL_KEY_MASK;
  910. else
  911. skey_state &= ~RIGHT_CONTROL_KEY_MASK;
  912. break;
  913. case DIK_LMENU:
  914. if( keybMsg->dwData & 0x80 )
  915. skey_state |= LEFT_ALT_KEY_MASK;
  916. else
  917. skey_state &= ~LEFT_ALT_KEY_MASK;
  918. break;
  919. case DIK_RMENU:
  920. if( keybMsg->dwData & 0x80 )
  921. skey_state |= RIGHT_ALT_KEY_MASK;
  922. else
  923. skey_state &= ~RIGHT_ALT_KEY_MASK;
  924. break;
  925. case DIK_CAPITAL:
  926. if(keybMsg->dwData & 0x80)
  927. skey_state ^= CAP_LOCK_STATE_MASK;
  928. break;
  929. case DIK_NUMLOCK:
  930. if(keybMsg->dwData & 0x80)
  931. skey_state ^= NUM_LOCK_STATE_MASK;
  932. break;
  933. case DIK_SCROLL:
  934. if(keybMsg->dwData & 0x80)
  935. skey_state ^= SCROLL_LOCK_STATE_MASK;
  936. break;
  937. case DIK_INSERT:
  938. if(keybMsg->dwData & 0x80)
  939. {
  940. // insert is a special case, it toggle skey_state and
  941. // append event queue
  942. skey_state ^= INSERT_STATE_MASK;
  943. add_key_event(keybMsg->dwOfs, keybMsg->dwTimeStamp);
  944. }
  945. break;
  946. case CDIK_GRAPH: // graphic key
  947. if( keybMsg->dwData & 0x80 )
  948. skey_state |= GRAPH_KEY_MASK;
  949. else
  950. skey_state &= ~GRAPH_KEY_MASK;
  951. break;
  952. default:
  953. if( keybMsg->dwData & 0x80 )
  954. {
  955. add_key_event(keybMsg->dwOfs, keybMsg->dwTimeStamp);
  956. // ####### begin Gilbert 29/8 ######//
  957. // capture screen
  958. if(keybMsg->dwOfs == DIK_F11 && (skey_state & CONTROL_KEY_MASK))
  959. {
  960. sys.capture_screen();
  961. }
  962. // ####### end Gilbert 29/8 ######//
  963. }
  964. }
  965. --keybLen;
  966. ++keybMsg;
  967. }
  968. }
  969. }
  970. // ####### begin Gilbert 12/4 ########//
  971. if(moveFlag)
  972. {
  973. mouse_cursor.process(cur_x, cur_y); // repaint mouse cursor
  974. power.mouse_handler();
  975. }
  976. // ####### end Gilbert 12/4 ########//
  977. }
  978. //--------- End of Mouse::poll_event --------------//
  979. // ####### begin Gilbert 31/10 #########//
  980. //--------- Begin of Mouse::update_skey_state ----------//
  981. // called after task switch to get the lastest state of ctrl/alt/shift key
  982. void Mouse::update_skey_state()
  983. {
  984. // ------- get initial keyboard state ----------//
  985. skey_state = 0;
  986. if(GetKeyState(VK_LSHIFT) < 0)
  987. skey_state |= LEFT_SHIFT_KEY_MASK;
  988. if(GetKeyState(VK_RSHIFT) < 0)
  989. skey_state |= RIGHT_SHIFT_KEY_MASK;
  990. if(GetKeyState(VK_LCONTROL) < 0)
  991. skey_state |= LEFT_CONTROL_KEY_MASK;
  992. if(GetKeyState(VK_RCONTROL) < 0)
  993. skey_state |= RIGHT_CONTROL_KEY_MASK;
  994. if(GetKeyState(VK_LMENU) < 0)
  995. skey_state |= LEFT_ALT_KEY_MASK;
  996. if(GetKeyState(VK_RMENU) < 0)
  997. skey_state |= RIGHT_ALT_KEY_MASK;
  998. if(GetKeyState(VK_NUMLOCK) & 1)
  999. skey_state |= NUM_LOCK_STATE_MASK;
  1000. if(GetKeyState(VK_CAPITAL) & 1)
  1001. skey_state |= CAP_LOCK_STATE_MASK;
  1002. if(GetKeyState(VK_SCROLL) & 1)
  1003. skey_state |= SCROLL_LOCK_STATE_MASK;
  1004. skey_state |= INSERT_STATE_MASK; // enable insert mode by default
  1005. if( vk_graph )
  1006. {
  1007. if(GetKeyState(vk_graph) < 0)
  1008. skey_state |= GRAPH_KEY_MASK;
  1009. }
  1010. }
  1011. //--------- End of Mouse::update_skey_state ----------//
  1012. // ####### end Gilbert 31/10 #########//
  1013. //--------- Begin of Mouse::wait_press ----------//
  1014. //
  1015. // Wait until one of the mouse buttons is pressed.
  1016. //
  1017. // [int] timeOutSecond - no. of seconds to time out. If not
  1018. // given, there will be no time out.
  1019. //
  1020. // return: <int> 1-left mouse button
  1021. // 2-right mouse button
  1022. //
  1023. int Mouse::wait_press(int timeOutSecond)
  1024. {
  1025. while( mouse.left_press || mouse.any_click() || mouse.key_code ) // avoid repeat clicking
  1026. {
  1027. sys.yield();
  1028. mouse.get_event();
  1029. }
  1030. int rc=0;
  1031. unsigned int timeOutTime = m.get_time() + timeOutSecond*1000;
  1032. while(1)
  1033. {
  1034. sys.yield();
  1035. mouse.get_event();
  1036. if( sys.debug_session )
  1037. sys.blt_virtual_buf();
  1038. if( right_press || mouse.key_code==KEY_ESC )
  1039. {
  1040. rc = 2;
  1041. break;
  1042. }
  1043. if( left_press || mouse.key_code )
  1044. {
  1045. rc = 1;
  1046. break;
  1047. }
  1048. if( timeOutSecond && m.get_time() > timeOutTime )
  1049. break;
  1050. }
  1051. while( mouse.left_press || mouse.any_click() || mouse.key_code ) // avoid repeat clicking
  1052. {
  1053. sys.yield();
  1054. mouse.get_event();
  1055. }
  1056. return rc;
  1057. }
  1058. //--------- End of Mouse::wait_press --------------//
  1059. //--------- Begin of Mouse::reset_click ----------//
  1060. //
  1061. // Reset queued mouse clicks.
  1062. //
  1063. void Mouse::reset_click()
  1064. {
  1065. click_buffer[0].count=0;
  1066. click_buffer[1].count=0;
  1067. }
  1068. //--------- End of Mouse::reset_click --------------//
  1069. // ------ Begin of Mouse::mickey_to_displacment -------//
  1070. long Mouse::micky_to_displacement(DWORD w)
  1071. {
  1072. long d = (long)w ;
  1073. // long a = abs(d);
  1074. // return a >= double_speed_threshold ? (a >= triple_speed_threshold ? 3 * d : 2 * d) : d;
  1075. return abs(d) >= double_speed_threshold ? d+d : d;
  1076. }
  1077. // ------ End of Mouse::mickey_to_displacment -------//
  1078. // ------ Begin of Mouse::is_key -------//
  1079. // compare key with key code
  1080. // e.g. to test a key is alt-a,
  1081. // call mouse.is_key(mouse.scan_code, mouse.event_skey_state, 'a', K_CHAR_KEY | K_IS_ALT)
  1082. //
  1083. // pass 0 as charValue to disable checking in charValue
  1084. // e.g pressed key is 'a'
  1085. // mouse.is_key(mouse.scan_code, mouse.event_skey_state, (WORD) 0, K_CHAR_KEY) returns 'a'
  1086. // but if key pressed is alt-a
  1087. // the same function call returns 0
  1088. // use mouse.is_key(mouse.scan_code, mouse.event_skey_state, (WORD) 0, K_CHAR_KEY | K_IS_ALT ) instead
  1089. //
  1090. int Mouse::is_key(unsigned scanCode, unsigned short skeyState, unsigned short charValue, unsigned flags)
  1091. {
  1092. unsigned short priChar = 0, shiftChar = 0, capitalChar = 0, altChar = 0;
  1093. unsigned onNumPad = 0;
  1094. switch(scanCode)
  1095. {
  1096. case DIK_ESCAPE: priChar = shiftChar = capitalChar = KEY_ESC; break;
  1097. case DIK_1: priChar = capitalChar = '1'; shiftChar = '!'; break;
  1098. case DIK_2: priChar = capitalChar = '2'; shiftChar = '\"'; altChar = (UCHAR)'²'; break;
  1099. case DIK_3: priChar = capitalChar = '3'; shiftChar = (UCHAR)'§'; altChar = (UCHAR)'³'; break;
  1100. case DIK_4: priChar = capitalChar = '4'; shiftChar = '$'; break;
  1101. case DIK_5: priChar = capitalChar = '5'; shiftChar = '%'; break;
  1102. case DIK_6: priChar = capitalChar = '6'; shiftChar = '&'; break;
  1103. case DIK_7: priChar = capitalChar = '7'; shiftChar = '/'; altChar = '{'; break;
  1104. case DIK_8: priChar = capitalChar = '8'; shiftChar = '('; altChar = '['; break;
  1105. case DIK_9: priChar = capitalChar = '9'; shiftChar = ')'; altChar = ']'; break;
  1106. case DIK_0: priChar = capitalChar = '0'; shiftChar = '='; altChar = '}'; break;
  1107. // case DIK_MINUS: priChar = capitalChar = '-'; shiftChar = '_'; break;
  1108. case 0x0c: priChar = capitalChar = (UCHAR)'ß'; shiftChar = '\?'; altChar = '\\'; break;
  1109. // case DIK_EQUALS: priChar = capitalChar = '='; shiftChar = '+'; break;
  1110. case 0x0d: priChar = capitalChar = (UCHAR)'´'; shiftChar = (UCHAR)'`'; break;
  1111. case DIK_BACK: priChar = capitalChar = shiftChar = KEY_BACK_SPACE; break; // backspace
  1112. case DIK_TAB: priChar = capitalChar = shiftChar = KEY_TAB; break;
  1113. case DIK_Q: priChar = 'q'; capitalChar = shiftChar = 'Q'; altChar = '@'; break;
  1114. case DIK_W: priChar = 'w'; capitalChar = shiftChar = 'W'; break;
  1115. case DIK_E: priChar = 'e'; capitalChar = shiftChar = 'E'; break;
  1116. case DIK_R: priChar = 'r'; capitalChar = shiftChar = 'R'; break;
  1117. case DIK_T: priChar = 't'; capitalChar = shiftChar = 'T'; break;
  1118. // case DIK_Y: priChar = 'y'; capitalChar = shiftChar = 'Y'; break;
  1119. case 0x15: priChar = 'z'; capitalChar = shiftChar = 'Z'; break;
  1120. case DIK_U: priChar = 'u'; capitalChar = shiftChar = 'U'; break;
  1121. case DIK_I: priChar = 'i'; capitalChar = shiftChar = 'I'; break;
  1122. case DIK_O: priChar = 'o'; capitalChar = shiftChar = 'O'; break;
  1123. case DIK_P: priChar = 'p'; capitalChar = shiftChar = 'P'; break;
  1124. // case DIK_LBRACKET: priChar = capitalChar = '['; shiftChar = '{'; break;
  1125. case 0x1a: priChar = (UCHAR)'ü'; capitalChar = shiftChar = (UCHAR)'Ü'; break;
  1126. // case DIK_RBRACKET: priChar = capitalChar = ']'; shiftChar = '}'; break;
  1127. case 0x1b: priChar = capitalChar = shiftChar = '+'; altChar = '~'; break;
  1128. case DIK_NUMPADENTER: // Enter on numeric keypad
  1129. onNumPad = 1; // fall through
  1130. case DIK_RETURN: priChar = capitalChar = shiftChar = KEY_RETURN; break;
  1131. case DIK_A: priChar = 'a'; capitalChar = shiftChar = 'A'; break;
  1132. case DIK_S: priChar = 's'; capitalChar = shiftChar = 'S'; break;
  1133. case DIK_D: priChar = 'd'; capitalChar = shiftChar = 'D'; break;
  1134. case DIK_F: priChar = 'f'; capitalChar = shiftChar = 'F'; break;
  1135. case DIK_G: priChar = 'g'; capitalChar = shiftChar = 'G'; break;
  1136. case DIK_H: priChar = 'h'; capitalChar = shiftChar = 'H'; break;
  1137. case DIK_J: priChar = 'j'; capitalChar = shiftChar = 'J'; break;
  1138. case DIK_K: priChar = 'k'; capitalChar = shiftChar = 'K'; break;
  1139. case DIK_L: priChar = 'l'; capitalChar = shiftChar = 'L'; break;
  1140. // case DIK_SEMICOLON: priChar = capitalChar = ';'; shiftChar = ':'; break;
  1141. case 0x27: priChar = (UCHAR)'ö'; capitalChar = shiftChar = (UCHAR)'Ö'; break;
  1142. // case DIK_APOSTROPHE: priChar = capitalChar = '\''; shiftChar = '\"'; break;
  1143. case 0x28: priChar = (UCHAR)'ä'; capitalChar = shiftChar = (UCHAR)'Ä'; break;
  1144. // case DIK_GRAVE: priChar = capitalChar = '`'; shiftChar = '~'; break;
  1145. case 0x29: priChar = capitalChar = '^'; shiftChar = (UCHAR)'°'; break;
  1146. // case DIK_BACKSLASH: priChar = capitalChar = '\\'; shiftChar = '|'; break;
  1147. case 0x2b: priChar = capitalChar = '#'; shiftChar = '\''; break;
  1148. // case DIK_Z: priChar = 'z'; capitalChar = shiftChar = 'Z'; break;
  1149. case 0x2c: priChar = 'y'; capitalChar = shiftChar = 'Y'; break;
  1150. case DIK_X: priChar = 'x'; capitalChar = shiftChar = 'X'; break;
  1151. case DIK_C: priChar = 'c'; capitalChar = shiftChar = 'C'; break;
  1152. case DIK_V: priChar = 'v'; capitalChar = shiftChar = 'V'; break;
  1153. case DIK_B: priChar = 'b'; capitalChar = shiftChar = 'B'; break;
  1154. case DIK_N: priChar = 'n'; capitalChar = shiftChar = 'N'; break;
  1155. case DIK_M: priChar = 'm'; capitalChar = shiftChar = 'M'; altChar = (UCHAR)'µ'; break;
  1156. // case DIK_COMMA: priChar = capitalChar = ','; shiftChar = '<'; break;
  1157. case DIK_COMMA: priChar = capitalChar = ','; shiftChar = ';'; break;
  1158. // case DIK_PERIOD: priChar = capitalChar = '.'; shiftChar = '>'; break;
  1159. case DIK_PERIOD: priChar = capitalChar = '.'; shiftChar = ':'; break;
  1160. // case DIK_SLASH: priChar = capitalChar = '/'; shiftChar = '\?'; break;
  1161. case 0x35: priChar = capitalChar = '-'; shiftChar = '_'; break;
  1162. case DIK_MULTIPLY: priChar = capitalChar = shiftChar = '*'; onNumPad = 1; break; // * on numeric keypad
  1163. case DIK_SPACE: priChar = capitalChar = shiftChar = ' '; break;
  1164. case DIK_ADD: priChar = capitalChar = shiftChar = '+'; onNumPad = 1; break; // + on numeric keypad
  1165. case DIK_DIVIDE: priChar = capitalChar = shiftChar = '/'; onNumPad = 1; break; // / on numeric keypad
  1166. case DIK_SUBTRACT: priChar = capitalChar = shiftChar = '-'; onNumPad = 1; break; // - on numeric keypad
  1167. case 0x56: priChar = capitalChar = '<'; shiftChar = '>'; altChar = '|'; break;
  1168. case DIK_NUMPAD7: priChar = shiftChar = capitalChar = '7'; onNumPad = 1; break;
  1169. case DIK_NUMPAD8: priChar = shiftChar = capitalChar = '8'; onNumPad = 1; break;
  1170. case DIK_NUMPAD9: priChar = shiftChar = capitalChar = '9'; onNumPad = 1; break;
  1171. case DIK_NUMPAD4: priChar = shiftChar = capitalChar = '4'; onNumPad = 1; break;
  1172. case DIK_NUMPAD5: priChar = shiftChar = capitalChar = '5'; onNumPad = 1; break;
  1173. case DIK_NUMPAD6: priChar = shiftChar = capitalChar = '6'; onNumPad = 1; break;
  1174. case DIK_NUMPAD1: priChar = shiftChar = capitalChar = '1'; onNumPad = 1; break;
  1175. case DIK_NUMPAD2: priChar = shiftChar = capitalChar = '2'; onNumPad = 1; break;
  1176. case DIK_NUMPAD3: priChar = shiftChar = capitalChar = '3'; onNumPad = 1; break;
  1177. case DIK_NUMPAD0: priChar = shiftChar = capitalChar = '0'; onNumPad = 1; break;
  1178. case DIK_DECIMAL: priChar = shiftChar = capitalChar = '.'; onNumPad = 1; break;
  1179. // function keys
  1180. case DIK_F1: priChar = shiftChar = capitalChar = KEY_F1; break;
  1181. case DIK_F2: priChar = shiftChar = capitalChar = KEY_F2; break;
  1182. case DIK_F3: priChar = shiftChar = capitalChar = KEY_F3; break;
  1183. case DIK_F4: priChar = shiftChar = capitalChar = KEY_F4; break;
  1184. case DIK_F5: priChar = shiftChar = capitalChar = KEY_F5; break;
  1185. case DIK_F6: priChar = shiftChar = capitalChar = KEY_F6; break;
  1186. case DIK_F7: priChar = shiftChar = capitalChar = KEY_F7; break;
  1187. case DIK_F8: priChar = shiftChar = capitalChar = KEY_F8; break;
  1188. case DIK_F9: priChar = shiftChar = capitalChar = KEY_F9; break;
  1189. case DIK_F10: priChar = shiftChar = capitalChar = KEY_F10; break;
  1190. case DIK_F11: priChar = shiftChar = capitalChar = KEY_F11; break;
  1191. case DIK_F12: priChar = shiftChar = capitalChar = KEY_F12; break;
  1192. // arrow keys
  1193. case 0x67: // fall through, German keyboard called "Pos 1"
  1194. case DIK_HOME: priChar = shiftChar = capitalChar = KEY_HOME; break;
  1195. case DIK_UP: priChar = shiftChar = capitalChar = KEY_UP; break;
  1196. case DIK_PRIOR: priChar = shiftChar = capitalChar = KEY_PGUP; break;
  1197. case DIK_LEFT: priChar = shiftChar = capitalChar = KEY_LEFT; break;
  1198. case DIK_RIGHT: priChar = shiftChar = capitalChar = KEY_RIGHT; break;
  1199. case DIK_END: priChar = shiftChar = capitalChar = KEY_END; break;
  1200. case DIK_DOWN: priChar = shiftChar = capitalChar = KEY_DOWN; break;
  1201. case DIK_NEXT: priChar = shiftChar = capitalChar = KEY_PGDN; break;
  1202. case DIK_INSERT: priChar = shiftChar = capitalChar = KEY_INS; break;
  1203. case DIK_DELETE: priChar = shiftChar = capitalChar = KEY_DEL; break;
  1204. // other keys found in Japanese keyboard
  1205. case DIK_NUMPADCOMMA: priChar = shiftChar = capitalChar = ','; break;
  1206. case DIK_NUMPADEQUALS: priChar = shiftChar = capitalChar = '='; break;
  1207. case DIK_AT: priChar = shiftChar = capitalChar = '@'; break;
  1208. case DIK_COLON: priChar = shiftChar = capitalChar = ':'; break;
  1209. case DIK_UNDERLINE: priChar = shiftChar = capitalChar = '_'; break;
  1210. }
  1211. // BUGHERE : numpad key is not translated when numlock is off
  1212. // check flags
  1213. int retFlag = 1;
  1214. // check shift key state
  1215. if( !(flags & K_IGNORE_SHIFT) )
  1216. {
  1217. if( flags & K_IS_SHIFT )
  1218. {
  1219. if( !(skeyState & SHIFT_KEY_MASK) )
  1220. retFlag = 0;
  1221. }
  1222. else
  1223. {
  1224. if( skeyState & SHIFT_KEY_MASK )
  1225. retFlag = 0;
  1226. }
  1227. }
  1228. // check contrl key state
  1229. if( !(flags & K_IGNORE_CTRL) )
  1230. {
  1231. if( flags & K_IS_CTRL )
  1232. {
  1233. if( !(skeyState & CONTROL_KEY_MASK) )
  1234. retFlag = 0;
  1235. }
  1236. else
  1237. {
  1238. if( skeyState & CONTROL_KEY_MASK )
  1239. retFlag = 0;
  1240. }
  1241. }
  1242. // check alt key state
  1243. if( !(flags & K_IGNORE_ALT) )
  1244. {
  1245. if( flags & K_IS_ALT )
  1246. {
  1247. if( !(skeyState & ALT_KEY_MASK) )
  1248. retFlag = 0;
  1249. }
  1250. else
  1251. {
  1252. if( skeyState & ALT_KEY_MASK )
  1253. retFlag = 0;
  1254. }
  1255. }
  1256. // check numpad state
  1257. if( !(flags & K_IGNORE_NUMPAD) )
  1258. {
  1259. if( flags & K_ON_NUMPAD )
  1260. {
  1261. if( !onNumPad )
  1262. retFlag = 0;
  1263. }
  1264. else
  1265. {
  1266. if( onNumPad )
  1267. retFlag = 0;
  1268. }
  1269. }
  1270. unsigned outChar = priChar;
  1271. if( flags & K_TRANSLATE_KEY )
  1272. {
  1273. if( (skeyState & GRAPH_KEY_MASK) && altChar )
  1274. {
  1275. outChar = altChar;
  1276. }
  1277. else
  1278. {
  1279. if( priChar == capitalChar )
  1280. {
  1281. // non-letter
  1282. outChar = skeyState & SHIFT_KEY_MASK ? shiftChar : priChar;
  1283. }
  1284. else
  1285. {
  1286. // letter
  1287. outChar = skeyState & CAP_LOCK_STATE_MASK ?
  1288. (skeyState & SHIFT_KEY_MASK ? priChar : capitalChar) :
  1289. (skeyState & SHIFT_KEY_MASK ? shiftChar : priChar) ;
  1290. }
  1291. }
  1292. }
  1293. if(!retFlag)
  1294. return 0;
  1295. int retFlag2 = (charValue == 0) || outChar == charValue
  1296. || ((flags & K_IGNORE_SHIFT) && shiftChar == charValue)
  1297. || ((flags & K_IGNORE_CAP_LOCK) && capitalChar == charValue)
  1298. #ifdef WIN32
  1299. || ((flags & K_CASE_INSENSITIVE) && outChar == (unsigned short) std::tolower(charValue));
  1300. #else
  1301. || ((flags & K_CASE_INSENSITIVE) && outChar == (unsigned short) tolower(charValue));
  1302. #endif
  1303. if(retFlag2)
  1304. return outChar;
  1305. else
  1306. return 0;
  1307. }
  1308. // ------ End of Mouse::is_key -------//
  1309. // ------ Begin of Mouse::is_key -------//
  1310. int Mouse::is_key(unsigned scanCode, unsigned short skeyState, char *keyStr, unsigned flags)
  1311. {
  1312. int len = strlen(keyStr);
  1313. if( len == 0)
  1314. return 0;
  1315. if( len == 1)
  1316. return is_key(scanCode, skeyState, keyStr[0], flags);
  1317. char *priChar = NULL;
  1318. char *numLockChar = NULL;
  1319. int onNumPad = 0;
  1320. switch(scanCode)
  1321. {
  1322. case DIK_F1: numLockChar = priChar = "F1"; break;
  1323. case DIK_F2: numLockChar = priChar = "F2"; break;
  1324. case DIK_F3: numLockChar = priChar = "F3"; break;
  1325. case DIK_F4: numLockChar = priChar = "F4"; break;
  1326. case DIK_F5: numLockChar = priChar = "F5"; break;
  1327. case DIK_F6: numLockChar = priChar = "F6"; break;
  1328. case DIK_F7: numLockChar = priChar = "F7"; break;
  1329. case DIK_F8: numLockChar = priChar = "F8"; break;
  1330. case DIK_F9: numLockChar = priChar = "F9"; break;
  1331. case DIK_F10: numLockChar = priChar = "F10"; break;
  1332. case DIK_F11: numLockChar = priChar = "F11"; break;
  1333. case DIK_F12: numLockChar = priChar = "F12"; break;
  1334. case DIK_NUMPAD7: priChar = "HOME"; numLockChar = "7"; onNumPad = 1; break;
  1335. case DIK_NUMPAD8: priChar = "UP"; numLockChar = "8"; onNumPad = 1; break;
  1336. case DIK_NUMPAD9: priChar = "PAGE UP"; numLockChar = "9"; onNumPad = 1; break;
  1337. case DIK_NUMPAD4: priChar = "LEFT"; numLockChar = "4"; onNumPad = 1; break;
  1338. case DIK_NUMPAD5: priChar = "CENTER"; numLockChar = "5"; onNumPad = 1; break;
  1339. case DIK_NUMPAD6: priChar = "RIGHT"; numLockChar = "6"; onNumPad = 1; break;
  1340. case DIK_NUMPAD1: priChar = "END"; numLockChar = "1"; onNumPad = 1; break;
  1341. case DIK_NUMPAD2: priChar = "DOWN"; numLockChar = "2"; onNumPad = 1; break;
  1342. case DIK_NUMPAD3: priChar = "PAGE DOWN"; numLockChar = "3"; onNumPad = 1; break;
  1343. case DIK_NUMPAD0: priChar = "INSERT"; numLockChar = "0"; onNumPad = 1; break;
  1344. case DIK_DECIMAL: priChar = "DELETE"; numLockChar = "."; onNumPad = 1; break;
  1345. // keys above arrow keys
  1346. case DIK_HOME: priChar = numLockChar = "HOME"; break;
  1347. case DIK_UP: priChar = numLockChar = "UP"; break;
  1348. case DIK_PRIOR: priChar = numLockChar = "PAGE UP"; break;
  1349. case DIK_LEFT: priChar = numLockChar = "LEFT"; break;
  1350. case DIK_RIGHT: priChar = numLockChar = "RIGHT"; break;
  1351. case DIK_END: priChar = numLockChar = "END"; break;
  1352. case DIK_DOWN: priChar = numLockChar = "DOWN"; break;
  1353. case DIK_NEXT: priChar = numLockChar = "PAGE DOWN"; break;
  1354. case DIK_INSERT: priChar = numLockChar = "INSERT"; break;
  1355. case DIK_DELETE: priChar = numLockChar = "DELETE"; break;
  1356. // 104-key only
  1357. case DIK_LWIN: priChar = numLockChar = "LEFT WINDOW"; break;
  1358. case DIK_RWIN: priChar = numLockChar = "RIGHT WINDOW"; break;
  1359. case DIK_APPS: priChar = numLockChar = "APP MENU"; break;
  1360. }
  1361. // check flags
  1362. int retFlag = 1;
  1363. // check shift key state
  1364. if( !(flags & K_IGNORE_SHIFT) )
  1365. {
  1366. if( flags & K_IS_SHIFT )
  1367. {
  1368. if( !(skeyState & SHIFT_KEY_MASK) )
  1369. retFlag = 0;
  1370. }
  1371. else
  1372. {
  1373. if( skeyState & SHIFT_KEY_MASK )
  1374. retFlag = 0;
  1375. }
  1376. }
  1377. // check contrl key state
  1378. if( !(flags & K_IGNORE_CTRL) )
  1379. {
  1380. if( flags & K_IS_CTRL )
  1381. {
  1382. if( !(skeyState & CONTROL_KEY_MASK) )
  1383. retFlag = 0;
  1384. }
  1385. else
  1386. {
  1387. if( skeyState & CONTROL_KEY_MASK )
  1388. retFlag = 0;
  1389. }
  1390. }
  1391. // check alt key state
  1392. if( !(flags & K_IGNORE_ALT) )
  1393. {
  1394. if( flags & K_IS_ALT )
  1395. {
  1396. if( !(skeyState & ALT_KEY_MASK) )
  1397. retFlag = 0;
  1398. }
  1399. else
  1400. {
  1401. if( skeyState & ALT_KEY_MASK )
  1402. retFlag = 0;
  1403. }
  1404. }
  1405. // check numpad state
  1406. if( !(flags & K_IGNORE_NUMPAD) )
  1407. {
  1408. if( flags & K_ON_NUMPAD )
  1409. {
  1410. if( !onNumPad )
  1411. retFlag = 0;
  1412. }
  1413. else
  1414. {
  1415. if( onNumPad )
  1416. retFlag = 0;
  1417. }
  1418. }
  1419. char *outChar = skeyState & NUM_LOCK_STATE_MASK ? numLockChar : priChar;
  1420. int retFlag2 = outChar ? !strcmp(outChar, keyStr) : 0;
  1421. return retFlag && retFlag2;
  1422. }
  1423. // ------ End of Mouse::is_key -------//
  1424. #endif