Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

339 lines
8.5 KiB

  1. //****************************************************************************
  2. //
  3. // File: joytest.c
  4. // Content: Joystick test dialog
  5. // History:
  6. // Date By Reason
  7. // ==== == ======
  8. // 11-dec-94 craige split out of joycpl.c; some tweaks
  9. // 15-dec-94 craige allow N joysticks
  10. //
  11. // Copyright (c) Microsoft Corporation 1994-1995
  12. //
  13. //****************************************************************************
  14. #include "joycpl.h"
  15. /*
  16. * variables used by test process
  17. */
  18. typedef struct {
  19. LPGLOBALVARS pgv;
  20. MMRESULT mmr_capture;
  21. HWND hwnd;
  22. BOOL bHasTimer;
  23. BOOL bUseTimer;
  24. int iButtonShift;
  25. JOYINFOEX ji;
  26. } test_vars, *LPTESTVARS;
  27. /*
  28. * fillButton - light up a specific button
  29. */
  30. static void fillButton( LPGLOBALVARS pgv, HWND hwnd, int id, BOOL isup )
  31. {
  32. HWND hwb;
  33. RECT r;
  34. HDC hdc;
  35. hwb = GetDlgItem( hwnd, id );
  36. if( hwb == NULL ) {
  37. return;
  38. }
  39. hdc = GetDC( hwb );
  40. if( hdc == NULL ) {
  41. return;
  42. }
  43. GetClientRect( hwb, &r );
  44. if( isup ) {
  45. FillRect( hdc, &r, pgv->pjd->hbUp );
  46. } else {
  47. FillRect( hdc, &r, pgv->pjd->hbDown );
  48. }
  49. ReleaseDC( hwb, hdc );
  50. } /* fillButton */
  51. /*
  52. * doTestButton - try to light the relevant buttons
  53. */
  54. static void doTestButton( LPTESTVARS ptv, HWND hwnd, LPJOYINFOEX pji )
  55. {
  56. if( (ptv->ji.dwButtons & JOY_BUTTON1) != (pji->dwButtons & JOY_BUTTON1) ) {
  57. fillButton( ptv->pgv, hwnd, IDC_JOYB1+ptv->iButtonShift,
  58. pji->dwButtons & JOY_BUTTON1 );
  59. }
  60. if( (ptv->ji.dwButtons & JOY_BUTTON2) != (pji->dwButtons & JOY_BUTTON2) ) {
  61. fillButton( ptv->pgv, hwnd, IDC_JOYB2+ptv->iButtonShift,
  62. pji->dwButtons & JOY_BUTTON2 );
  63. }
  64. if( ptv->iButtonShift == 0 ) {
  65. if( (ptv->ji.dwButtons & JOY_BUTTON3) != (pji->dwButtons & JOY_BUTTON3) ) {
  66. fillButton( ptv->pgv, hwnd, IDC_JOYB3, pji->dwButtons & JOY_BUTTON3 );
  67. }
  68. if( (ptv->ji.dwButtons & JOY_BUTTON4) != (pji->dwButtons & JOY_BUTTON4) ) {
  69. fillButton( ptv->pgv, hwnd, IDC_JOYB4, pji->dwButtons & JOY_BUTTON4 );
  70. }
  71. }
  72. ptv->ji.dwButtons = pji->dwButtons;
  73. } /* doTestButton */
  74. /*
  75. * doTestPOV - try to light the POV indicators
  76. */
  77. static void doTestPOV( LPTESTVARS ptv, HWND hwnd, LPJOYINFOEX pji )
  78. {
  79. int idi;
  80. if( ptv->ji.dwPOV != pji->dwPOV ) {
  81. idi = IDI_JOYPOV_NONE;
  82. if( pji->dwPOV == JOY_POVFORWARD ) {
  83. idi = IDI_JOYPOV_UP;
  84. } else if( pji->dwPOV == JOY_POVBACKWARD ) {
  85. idi = IDI_JOYPOV_DOWN;
  86. } else if( pji->dwPOV == JOY_POVLEFT ) {
  87. idi = IDI_JOYPOV_LEFT;
  88. } else if( pji->dwPOV == JOY_POVRIGHT ) {
  89. idi = IDI_JOYPOV_RIGHT;
  90. }
  91. ChangeIcon( hwnd, idi, IDC_JOYPOV );
  92. }
  93. ptv->ji.dwPOV = pji->dwPOV;
  94. } /* doTestPOV */
  95. /*
  96. * joyTestInitDialog - init the testing dialog
  97. */
  98. static BOOL joyTestInitDialog( HWND hwnd, LPARAM lParam)
  99. {
  100. HINSTANCE hinst;
  101. char str[MAX_STR];
  102. LPJOYREGHWCONFIG pcfg;
  103. LPTESTVARS ptv;
  104. LPGLOBALVARS pgv;
  105. /*
  106. * create test vars
  107. */
  108. ptv = DoAlloc( sizeof( test_vars ) );
  109. SetWindowLong( hwnd, DWL_USER, (LONG) ptv );
  110. if( ptv == NULL ) {
  111. return FALSE;
  112. }
  113. pgv = (LPGLOBALVARS) lParam;
  114. ptv->pgv = pgv;
  115. ptv->hwnd = hwnd;
  116. /*
  117. * set dialog text based on OEM strings
  118. */
  119. SetOEMText( pgv, hwnd, TRUE );
  120. /*
  121. * customize test dialog's button display
  122. */
  123. pcfg = &pgv->joyHWCurr;
  124. if( pcfg->hws.dwNumButtons <= 2 ) {
  125. ptv->iButtonShift = 1;
  126. ShowWindow( GetDlgItem( hwnd, IDC_JOYB1 ), SW_HIDE );
  127. ShowWindow( GetDlgItem( hwnd, IDC_JOYB4 ), SW_HIDE );
  128. ShowWindow( GetDlgItem( hwnd, IDC_JOYB1_LABEL ), SW_HIDE );
  129. ShowWindow( GetDlgItem( hwnd, IDC_JOYB4_LABEL ), SW_HIDE );
  130. hinst = GetWindowInstance( hwnd );
  131. if( LoadString( hinst, IDS_JOYBUTTON1, str, sizeof( str ) ) ) {
  132. SetWindowText( GetDlgItem( hwnd, IDC_JOYB2_LABEL ), str );
  133. }
  134. if( LoadString( hinst, IDS_JOYBUTTON2, str, sizeof( str ) ) ) {
  135. SetWindowText( GetDlgItem( hwnd, IDC_JOYB3_LABEL ), str );
  136. }
  137. } else {
  138. ptv->iButtonShift = 0;
  139. }
  140. ShowControls( pcfg, hwnd );
  141. /*
  142. * other misc setup
  143. */
  144. ptv->bHasTimer = SetTimer( hwnd, TIMER_ID, JOYPOLLTIME, NULL );
  145. ptv->bUseTimer = TRUE;
  146. if( !ptv->bHasTimer ) {
  147. DPF( "No timer for joystick test!\r\n" );
  148. return FALSE;
  149. }
  150. return TRUE;
  151. } /* joyTestInitDialog */
  152. /*
  153. * context help for the test dialog
  154. */
  155. const static DWORD aTestHelpIDs[] = { // Context Help IDs
  156. IDC_GROUPBOX, IDH_JOYSTICK_GROUPBOX,
  157. IDC_JOYLIST1_LABEL, IDH_JOYSTICK_TEST_RANGE,
  158. IDC_JOYLIST1, IDH_JOYSTICK_TEST_RANGE,
  159. IDC_JOYLIST2_LABEL, IDH_JOYSTICK_TEST_THROTTLE,
  160. IDC_JOYLIST2, IDH_JOYSTICK_TEST_THROTTLE,
  161. IDC_JOYLIST3_LABEL, IDH_JOYSTICK_TEST_RUDDER,
  162. IDC_JOYLIST3, IDH_JOYSTICK_TEST_RUDDER,
  163. IDC_JOYPOV_LABEL, IDH_JOYSTICK_TEST_POV,
  164. IDC_JOYPOV, IDH_JOYSTICK_TEST_POV,
  165. IDC_TEXT_1, NO_HELP,
  166. IDC_TEXT_2, NO_HELP,
  167. IDC_ICON_1, NO_HELP,
  168. IDC_GROUPBOX_2, IDH_JOYSTICK_TEST_BUTTONS,
  169. IDC_JOYB1_LABEL, IDH_JOYSTICK_TEST_BUTTONS,
  170. IDC_JOYB2_LABEL, IDH_JOYSTICK_TEST_BUTTONS,
  171. IDC_JOYB3_LABEL, IDH_JOYSTICK_TEST_BUTTONS,
  172. IDC_JOYB4_LABEL, IDH_JOYSTICK_TEST_BUTTONS,
  173. IDC_JOYB1, IDH_JOYSTICK_TEST_BUTTONS,
  174. IDC_JOYB2, IDH_JOYSTICK_TEST_BUTTONS,
  175. IDC_JOYB3, IDH_JOYSTICK_TEST_BUTTONS,
  176. IDC_JOYB4, IDH_JOYSTICK_TEST_BUTTONS,
  177. IDC_JOYLIST4_LABEL, IDH_JOYSTICK_TEST_THROTTLE,
  178. IDC_JOYLIST4, IDH_JOYSTICK_TEST_THROTTLE,
  179. IDC_JOYLIST5_LABEL, NO_HELP,
  180. IDC_JOYLIST5, NO_HELP,
  181. 0, 0
  182. };
  183. /*
  184. * TestProc - callback procedure for joystick test dialog
  185. */
  186. BOOL CALLBACK TestProc( HWND hwnd, UINT umsg, WPARAM wParam, LPARAM lParam)
  187. {
  188. BOOL rc;
  189. switch( umsg ) {
  190. case WM_TIMER:
  191. {
  192. LPTESTVARS ptv;
  193. ptv = (LPTESTVARS) GetWindowLong( hwnd, DWL_USER );
  194. if( ptv->bUseTimer ) {
  195. JOYINFOEX ji;
  196. MMRESULT rc;
  197. ptv->bUseTimer = FALSE;
  198. ji.dwSize = sizeof( ji );
  199. // ji.dwFlags = JOY_RETURNALL | JOY_USEDEADZONE | JOY_RETURNCENTERED;
  200. ji.dwFlags = JOY_RETURNALL | JOY_RETURNCENTERED;
  201. rc = joyGetPosEx( ptv->pgv->iJoyId, &ji );
  202. if( rc == JOYERR_NOERROR ) {
  203. DoJoyMove( ptv->pgv, hwnd, &ji, &ptv->ji, JOYMOVE_DRAWALL );
  204. doTestButton( ptv, hwnd, &ji );
  205. doTestPOV( ptv, hwnd, &ji );
  206. ptv->bUseTimer = TRUE;
  207. } else {
  208. if( JoyError( hwnd ) ) {
  209. ptv->bUseTimer = TRUE;
  210. }
  211. }
  212. }
  213. break;
  214. }
  215. case WM_DESTROY:
  216. {
  217. LPTESTVARS ptv;
  218. ptv = (LPTESTVARS) GetWindowLong( hwnd, DWL_USER );
  219. DoFree( ptv );
  220. break;
  221. }
  222. case WM_INITDIALOG:
  223. rc = joyTestInitDialog( hwnd, lParam );
  224. if( !rc ) {
  225. EndDialog( hwnd, 0 );
  226. }
  227. return FALSE;
  228. case WM_PAINT:
  229. {
  230. LPTESTVARS ptv;
  231. ptv = (LPTESTVARS) GetWindowLong( hwnd, DWL_USER );
  232. CauseRedraw( &ptv->ji, TRUE );
  233. return FALSE;
  234. }
  235. case WM_HELP:
  236. WinHelp((HWND)((LPHELPINFO) lParam)->hItemHandle, cszHelpFile,
  237. HELP_WM_HELP, (DWORD)(LPSTR) aTestHelpIDs);
  238. return TRUE;
  239. case WM_CONTEXTMENU:
  240. WinHelp((HWND) wParam, cszHelpFile, HELP_CONTEXTMENU,
  241. (DWORD)(LPVOID) aTestHelpIDs);
  242. return TRUE;
  243. case WM_COMMAND:
  244. {
  245. int id;
  246. LPTESTVARS ptv;
  247. ptv = (LPTESTVARS) GetWindowLong( hwnd, DWL_USER );
  248. id = GET_WM_COMMAND_ID(wParam, lParam);
  249. switch( id ) {
  250. case IDCANCEL:
  251. case IDOK:
  252. if( ptv->bHasTimer ) {
  253. KillTimer( hwnd, TIMER_ID );
  254. }
  255. EndDialog(hwnd, (id == IDOK));
  256. break;
  257. default:
  258. break;
  259. }
  260. break;
  261. }
  262. default:
  263. break;
  264. }
  265. return FALSE;
  266. } /* TestProc */
  267. /*
  268. * DoTest - do the test dialog
  269. */
  270. void DoTest( LPGLOBALVARS pgv, HWND hwnd, LPUPDCFGFN pupdcfgfn, LPVOID pparm )
  271. {
  272. JOYREGHWCONFIG save_joycfg;
  273. int id;
  274. /*
  275. * save the current config, and then update config if required
  276. */
  277. save_joycfg = pgv->joyHWCurr;
  278. if( pupdcfgfn != NULL ) {
  279. pupdcfgfn( pparm );
  280. }
  281. /*
  282. * update the registry with our new joystick info
  283. */
  284. RegSaveCurrentJoyHW( pgv );
  285. RegistryUpdated( pgv );
  286. /*
  287. * process the test dialog
  288. */
  289. if( pgv->joyHWCurr.hws.dwFlags & (JOY_HWS_HASU|JOY_HWS_HASV) ) {
  290. id = IDD_JOYTEST1;
  291. } else {
  292. id = IDD_JOYTEST;
  293. }
  294. DialogBoxParam( (HINSTANCE)GetWindowLong( hwnd, GWL_HINSTANCE ),
  295. MAKEINTRESOURCE( id ), hwnd, TestProc, (LONG) pgv );
  296. /*
  297. * restore the old registry info
  298. */
  299. pgv->joyHWCurr = save_joycfg;
  300. RegSaveCurrentJoyHW( pgv );
  301. RegistryUpdated( pgv );
  302. } /* DoTest */