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.

435 lines
18 KiB

  1. /*----------------------------------------------------------------------------*\
  2. | |
  3. | dialog.c - Dialog functions for Timer Device Driver Test Application |
  4. | |
  5. | |
  6. | History: |
  7. | Created Glenn Steffler (w-GlennS) 29-Jan-1990 |
  8. | |
  9. \*----------------------------------------------------------)-----------------*/
  10. /*----------------------------------------------------------------------------*\
  11. | |
  12. | i n c l u d e f i l e s |
  13. | |
  14. \*----------------------------------------------------------------------------*/
  15. #include <windows.h>
  16. #include <mmsystem.h>
  17. #include <port1632.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include "ta.h"
  21. #define abs(x) ( (x) < 0 ? -(x) : (x) )
  22. /*----------------------------------------------------------------------------*\
  23. | |
  24. | g l o b a l v a r i a b l e s |
  25. | |
  26. \*----------------------------------------------------------------------------*/
  27. extern LPTIMECALLBACK lpTimeCallback; // call back function entry point
  28. extern BOOL bHandlerHit;
  29. extern WORD wHandlerError;
  30. extern HWND hwndApp;
  31. static char *szPerOne[] = { "One","Per" };
  32. HWND hdlgModeless = NULL;
  33. HWND hdlgDelay = NULL;
  34. int nEvents = 0;
  35. EVENTLIST EventList[MAXEVENTS];
  36. /*----------------------------------------------------------------------------*\
  37. | ErrMsg - Opens a Message box with a error message in it. The user can |
  38. | select the OK button to continue |
  39. \*----------------------------------------------------------------------------*/
  40. void PASCAL ErrMsg(char *sz)
  41. {
  42. MessageBox(NULL,sz,NULL,MB_YESNO|MB_ICONEXCLAMATION|MB_DEFBUTTON2|MB_SYSTEMMODAL);
  43. }
  44. /*----------------------------------------------------------------------------*\
  45. | fnFileDlg (hDlg, uiMessage, wParam, lParam) |
  46. | |
  47. | Description: |
  48. | |
  49. | This function handles the messages for the delay dialog box. When |
  50. | the ADD, or REMOVE options are chosen, a timer event is either added |
  51. | or removed from the system respectively. |
  52. | |
  53. | Arguments: |
  54. | hDlg window handle of dialog window |
  55. | uiMessage message number |
  56. | wParam message-dependent |
  57. | lParam message-dependent |
  58. | |
  59. | Returns: |
  60. | TRUE if message has been processed, else FALSE |
  61. | |
  62. \*----------------------------------------------------------------------------*/
  63. BOOL FAR PASCAL
  64. fnFileDlg( HWND hDlg, unsigned uiMessage, UINT wParam, LONG lParam )
  65. {
  66. switch (uiMessage) {
  67. case WM_COMMAND:
  68. switch (wParam) {
  69. case IDOK:
  70. EndDialog(hDlg,TRUE);
  71. break;
  72. case IDCANCEL:
  73. EndDialog(hDlg,FALSE);
  74. break;
  75. }
  76. break;
  77. case WM_INITDIALOG:
  78. // SetDlgItemText(hDlg,IDOK,szText);
  79. return TRUE;
  80. }
  81. return FALSE;
  82. }
  83. /*----------------------------------------------------------------------------*\
  84. | fnDelayDlg (hDlg, uiMessage, wParam, lParam) |
  85. | |
  86. | Description: |
  87. | |
  88. | This function handles the messages for the delay dialog box. When |
  89. | the ADD, or REMOVE options are chosen, a timer event is either added |
  90. | or removed from the system respectively. |
  91. | |
  92. | Arguments: |
  93. | hDlg window handle of dialog window |
  94. | uiMessage message number |
  95. | wParam message-dependent |
  96. | lParam message-dependent |
  97. | |
  98. | Returns: |
  99. | TRUE if message has been processed, else FALSE |
  100. | |
  101. \*----------------------------------------------------------------------------*/
  102. BOOL FAR PASCAL
  103. fnDelayDlg( HWND hDlg, unsigned uiMessage, UINT wParam, LONG lParam )
  104. {
  105. static int piTabs[5] = { 0, 5*4, 10*4, 15*4, 20*4 };
  106. HWND hWnd;
  107. int nID,i,i1;
  108. char szTemp[100];
  109. EVENTLIST *pEventList;
  110. TIMEREVENT *pEvent;
  111. WORD error;
  112. switch (uiMessage) {
  113. case MM_TIMEEVENT:
  114. if( EventList[LOWORD(lParam)].bPeriodic ) {
  115. bHandlerHit = TRUE;
  116. EventList[LOWORD(lParam)].dwCount++;
  117. EventList[LOWORD(lParam)].bHit = TRUE;
  118. }
  119. else {
  120. i = (int)EventList[LOWORD(lParam)].teEvent.wDelay;
  121. error = (int)EventList[LOWORD(lParam)].dtime - i;
  122. if (error < 0)
  123. wsprintf(szTemp,"%dms OneShot was %dms early",-i,error);
  124. else if (error > 0)
  125. wsprintf(szTemp,"%dms OneShot was %dms late",i,error);
  126. else
  127. wsprintf(szTemp,"%dms OneShot was on time",i);
  128. MessageBox(NULL,szTemp,NULL,MB_OK|MB_ICONEXCLAMATION|MB_TASKMODAL);
  129. hWnd = GetDlgItem(hDlg,LB_DELAY);
  130. /* events in listbox */
  131. i1 = (int)SendMessage(hWnd,LB_GETCOUNT,0,0l);
  132. for( i=1; i < i1; i++ ) {
  133. SendMessage(hWnd,LB_GETTEXT,i,(LONG)(LPSTR)szTemp);
  134. sscanf( szTemp+strlen(szTemp)-4, "%x", &nID );
  135. if( nID == wParam ) { // compare ID's - kill if same
  136. nEvents--;
  137. bHandlerHit = TRUE;
  138. EventList[LOWORD(lParam)].bActive = FALSE;
  139. SendMessage(hWnd,LB_DELETESTRING,i,0l);
  140. break;
  141. }
  142. }
  143. }
  144. return TRUE;
  145. case WM_COMMAND:
  146. DelayDlgCmd( hDlg, wParam, lParam );
  147. break;
  148. case WM_CLOSE:
  149. DestroyWindow( hDlg );
  150. hdlgModeless = NULL;
  151. hdlgDelay = NULL;
  152. return TRUE;
  153. case WM_INITDIALOG:
  154. hdlgDelay = hDlg;
  155. hWnd = GetDlgItem(hDlg,LB_DELAY);
  156. SendMessage(hWnd,LB_SETTABSTOPS,
  157. sizeof(piTabs)/sizeof(int),(LONG)(LPSTR)piTabs);
  158. SendMessage(hWnd,LB_RESETCONTENT,0,0l);
  159. SendMessage(hWnd,LB_ADDSTRING,0,(LONG)(LPSTR)"[none]");
  160. for( pEventList=EventList,i=0; i < MAXEVENTS ; i++,pEventList++) {
  161. if( pEventList->bActive ) {
  162. pEvent = &( pEventList->teEvent );
  163. wsprintf(szTemp,"%d\t%d\t%d\t %s : %4x",
  164. pEventList->wStart,
  165. pEvent->wDelay,
  166. pEvent->wResolution,
  167. (LPSTR)szPerOne[pEventList->bPeriodic],
  168. pEventList->nID);
  169. SendMessage(hWnd,LB_ADDSTRING,0,(LONG)(LPSTR)szTemp);
  170. }
  171. }
  172. SendMessage(hWnd,LB_SETCURSEL,0,0l);
  173. return TRUE;
  174. }
  175. return FALSE;
  176. }
  177. /*----------------------------------------------------------------------------*\
  178. | DelayDlgCmd( hDlg, wParam, lParam ) |
  179. | |
  180. | Description: |
  181. | |
  182. | Process the WM_COMMAND messages for the fnDelayDlg DIALOG. |
  183. | |
  184. | Arguments: |
  185. | hDlg window handle of dialog window |
  186. | wParam message-dependent |
  187. | lParam message-dependent |
  188. | |
  189. | Returns: |
  190. | abso-poso-litilly-nothin |
  191. | |
  192. \*----------------------------------------------------------------------------*/
  193. void PASCAL DelayDlgCmd( HWND hDlg, UINT wParam, LONG lParam )
  194. {
  195. HWND hWnd;
  196. int nStartEdit,nDelayEdit,nResolEdit,nID,i;
  197. BOOL b1,b2,b3,bPeriodic;
  198. char szTemp[100],szTemp2[100];
  199. TIMEREVENT *pEvent;
  200. switch (wParam) {
  201. case ID_REMOVE:
  202. hWnd = GetDlgItem(hDlg,LB_DELAY);
  203. if( (i = (int)SendMessage(hWnd,LB_GETCURSEL,0,0l)) > 0 ) {
  204. SendMessage(hWnd,LB_GETTEXT,i,(LONG)(LPSTR)szTemp);
  205. sscanf( szTemp+strlen(szTemp)-4, "%x", &nID );
  206. if( nID != 0 ) {
  207. SendMessage(hWnd,LB_DELETESTRING,i,0l);
  208. for( i=0; i < MAXEVENTS; i++ ) {
  209. if( EventList[i].nID == nID ) {
  210. if( EventList[i].bActive ) {
  211. EventList[i].bActive = FALSE;
  212. timeKillEvent(nID);
  213. nEvents--;
  214. break;
  215. }
  216. else {
  217. ErrMsg( "Isn't registered in Queue!" );
  218. break;
  219. }
  220. }
  221. }
  222. SendMessage(hWnd,LB_SETCURSEL,0,0l);
  223. PostMessage(hDlg,WM_COMMAND,LB_DELAY,
  224. MAKELONG(hWnd,LBN_SELCHANGE));
  225. break;
  226. }
  227. }
  228. MessageBeep( NULL );
  229. break;
  230. case IDOK:
  231. case ID_ADD:
  232. hWnd = GetDlgItem(hDlg,LB_DELAY);
  233. nStartEdit = GetDlgItemInt( hDlg, ID_STARTEDIT, &b1, FALSE );
  234. nDelayEdit = GetDlgItemInt( hDlg, ID_DELAYEDIT, &b2, FALSE );
  235. nResolEdit = GetDlgItemInt( hDlg, ID_RESOLEDIT, &b3, FALSE );
  236. bPeriodic = (int)SendDlgItemMessage(hDlg,ID_PERIODIC,BM_GETCHECK,0,0l);
  237. if( nID = (b1 && b2 && b3) ) {
  238. for( i=0; i < MAXEVENTS; i++ ) {
  239. if( !EventList[i].bActive )
  240. break;
  241. }
  242. if( i >= MAXEVENTS ) {
  243. nID = 0;
  244. break;
  245. }
  246. pEvent = &( EventList[i].teEvent );
  247. pEvent->wDelay = nDelayEdit;
  248. pEvent->wResolution = nResolEdit;
  249. pEvent->dwUser = i;
  250. pEvent->wFlags = ((bPeriodic)?(TIME_PERIODIC):(TIME_ONESHOT));
  251. pEvent->lpFunction = lpTimeCallback;
  252. EventList[i].bActive = TRUE;
  253. EventList[i].bPeriodic = bPeriodic;
  254. EventList[i].bHit = FALSE;
  255. EventList[i].dwCount = 0l;
  256. EventList[i].dwError = 0l;
  257. EventList[i].wStart = nStartEdit;
  258. EventList[i].dtimeMin = 0x7FFFFFFF;
  259. EventList[i].dtimeMax = 0;
  260. EventList[i].dtime = 0;
  261. EventList[i].time = timeGetTime();
  262. if( (nID = timeSetEvent(pEvent->wDelay, pEvent->wResolution,
  263. pEvent->lpFunction, pEvent->dwUser, pEvent->wFlags)) == NULL )
  264. {
  265. EventList[i].bActive = FALSE;
  266. break;
  267. }
  268. // SetTimer call must return non-NULL ID to signify a valid event
  269. nEvents++;
  270. EventList[i].nID = nID;
  271. wsprintf(szTemp,"%d\t%d\t%d\t %s : %4x",
  272. nStartEdit,nDelayEdit,nResolEdit,(LPSTR)szPerOne[bPeriodic],nID);
  273. SendMessage(hWnd,LB_ADDSTRING,0,(LONG)(LPSTR)szTemp);
  274. SendMessage(hWnd,LB_SETCURSEL,0,0l);
  275. PostMessage(hDlg,WM_COMMAND,LB_DELAY,MAKELONG(hWnd,LBN_SELCHANGE));
  276. }
  277. if( wParam == IDOK ) {
  278. PostMessage( hDlg, WM_CLOSE, 0, 0l );
  279. break;
  280. }
  281. if( !nID ) {
  282. MessageBeep( NULL );
  283. }
  284. break;
  285. case LB_DELAY:
  286. switch( HIWORD(lParam) ) {
  287. case LBN_SELCHANGE:
  288. i = (int)SendMessage(LOWORD(lParam),LB_GETCURSEL,0,0l);
  289. if( i == 0 ) {
  290. SetDlgItemText(hDlg,ID_STARTEDIT,"");
  291. SetDlgItemText(hDlg,ID_DELAYEDIT,"");
  292. SetDlgItemText(hDlg,ID_RESOLEDIT,"");
  293. SendDlgItemMessage(hDlg,ID_PERIODIC,BM_SETCHECK,FALSE,0l);
  294. }
  295. else if( i != LB_ERR ) {
  296. SendMessage(LOWORD(lParam),LB_GETTEXT,i,(LONG)(LPSTR)szTemp);
  297. sscanf( szTemp, "%d %d %d %s : %x",
  298. &nStartEdit,&nDelayEdit,&nResolEdit,szTemp2,&nID);
  299. bPeriodic = (lstrcmp( szTemp2, szPerOne[1] ) == 0);
  300. SetDlgItemInt(hDlg,ID_STARTEDIT,nStartEdit,FALSE);
  301. SetDlgItemInt(hDlg,ID_DELAYEDIT,nDelayEdit,FALSE);
  302. SetDlgItemInt(hDlg,ID_RESOLEDIT,nResolEdit,FALSE);
  303. SendDlgItemMessage(hDlg,ID_PERIODIC,BM_SETCHECK,bPeriodic,0l);
  304. }
  305. else
  306. break;
  307. break;
  308. }
  309. break;
  310. case IDCANCEL:
  311. PostMessage( hDlg, WM_CLOSE, 0, 0l );
  312. break;
  313. }
  314. return;
  315. }
  316. /*----------------------------------------------------------------------------*\
  317. | KillAllEvents(void ) |
  318. | |
  319. | Description: |
  320. | Kills all outstanding events when windows exits |
  321. | |
  322. | Arguments: |
  323. | |
  324. | Returns: |
  325. | Absolutely nothing |
  326. | |
  327. \*----------------------------------------------------------------------------*/
  328. void PASCAL KillAllEvents( void )
  329. {
  330. int i;
  331. for( i=0; i < MAXEVENTS; i++ ) {
  332. if( EventList[i].bActive ) {
  333. timeKillEvent( (UINT)EventList[i].nID );
  334. }
  335. }
  336. }
  337. /*----------------------------------------------------------------------------*\
  338. | TimeCallback( wID, dwUser ) |
  339. | |
  340. | Description: |
  341. | The routine which processes the timer interrupt call backs. |
  342. | Simply adds one to the long int corresponding to lParam in the |
  343. | table of call back function counts. This essentionally means the |
  344. | number of times this routine is called for the certain interrupts |
  345. | is recorded. |
  346. | |
  347. | Arguments: |
  348. | wID ID returned from timeSetTimerEvent call |
  349. | dwUser value passed in event structure to said function. |
  350. | |
  351. | Returns: |
  352. | Absolutely nothing |
  353. | |
  354. \*----------------------------------------------------------------------------*/
  355. void FAR PASCAL TimeCallback( UINT wID, UINT msg, DWORD dwUser, DWORD dwTime, DWORD dw2)
  356. {
  357. #define i (LOWORD(dwUser))
  358. DWORD time;
  359. time = timeGetTime();
  360. EventList[i].dtime = time - EventList[i].time;
  361. EventList[i].dtimeMin = min(EventList[i].dtime,EventList[i].dtimeMin);
  362. EventList[i].dtimeMax = max(EventList[i].dtime,EventList[i].dtimeMax);
  363. EventList[i].time = time;
  364. if( !EventList[i].bActive ) {
  365. wHandlerError++;
  366. }
  367. else if( EventList[i].bPeriodic ) {
  368. bHandlerHit = TRUE;
  369. EventList[i].dwCount++;
  370. if (abs((int)EventList[i].dtime-(int)EventList[i].teEvent.wDelay) > (int)EventList[i].teEvent.wResolution)
  371. EventList[i].dwError++;
  372. EventList[i].bHit = TRUE;
  373. }
  374. else if( hdlgDelay != NULL ) {
  375. PostMessage(hdlgDelay,MM_TIMEEVENT,wID,dwUser);
  376. }
  377. else {
  378. nEvents--;
  379. bHandlerHit = TRUE;
  380. EventList[i].bActive = FALSE;
  381. }
  382. #undef i
  383. }