|
|
1.1 root 1: //---------------------------------------------------------------------------
2: //
3: // Module: tty.c
4: //
5: // Purpose:
6: // The sample application demonstrates the usage of the COMM
7: // API. It implements the new COMM API of Windows 3.1.
8: //
9: // NOTE: no escape sequences are translated, only
10: // the necessary control codes (LF, CR, BS, etc.)
11: //
12: // Description of functions:
13: // Descriptions are contained in the function headers.
14: //
15: // Development Team:
16: // Bryan A. Woodruff
17: //
18: // History: Date Author Comment
19: //
20: // 5/ 8/91 BryanW Wrote it.
21: //
22: // 10/18/91 BryanW Sanitized code & comments.
23: //
24: // 10/22/91 BryanW Minor bug fixes.
25: //
26: // 12/31/91 BryanW Ported to Win32 (NT).
27: //
28: // 2/10/92 BryanW Fixed a problem with ReadCommBlock()
29: // and error detection. ReadComm() is
30: // now performed... if the result is < 0
31: // the error status is read using
32: // GetCommError() and displayed. The
33: // length is adjusted (*= -1).
34: //
35: // 2/13/92 BryanW Updated to provide the option to
36: // implement/handle CN_RECEIVE
37: // notifications.
38: //
39: // 2/13/92 BryanW Implemented forced DTR control. DTR
40: // is asserted on connect and dropped
41: // on disconnect.
42: //
43: // 2/21/92 BryanW Removed overhead of local memory
44: // functions - now uses LPTR for
45: // allocation from heap.
46: //
47: // 2/26/92 BryanW Fixed off by one problem in paint
48: // calculations.
49: //
50: // 6/15/92 BryanW Ported the "updated" version of TTY
51: // for Win 3.1 including bug fixes.
52: //
53: //---------------------------------------------------------------------------
54: //
55: // Written by Microsoft Product Support Services, Windows Developer Support.
56: // Copyright (c) 1991 Microsoft Corporation. All Rights Reserved.
57: //
58: //---------------------------------------------------------------------------
59:
60: #include "tty.h"
61:
62: //---------------------------------------------------------------------------
63: // int PASCAL WinMain( HANDLE hInstance, HANDLE hPrevInstance,
64: // LPSTR lpszCmdLine, int nCmdShow )
65: //
66: // Description:
67: // This is the main window loop!
68: //
69: // Parameters:
70: // As documented for all WinMain() functions.
71: //
72: // History: Date Author Comment
73: // 5/ 8/91 BryanW Wrote it.
74: //
75: //---------------------------------------------------------------------------
76:
77: int PASCAL WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
78: LPSTR lpszCmdLine, int nCmdShow )
79: {
80: HWND hTTYWnd ;
81: MSG msg ;
82:
83: if (!hPrevInstance)
84: if (!InitApplication( hInstance ))
85: return ( FALSE ) ;
86:
87: if (NULL == (hTTYWnd = InitInstance( hInstance, nCmdShow )))
88: return ( FALSE ) ;
89:
90: while (GetMessage( &msg, NULL, 0, 0 ))
91: {
92: if (!TranslateAccelerator( hTTYWnd, ghAccel, &msg ))
93: {
94: TranslateMessage( &msg ) ;
95: DispatchMessage( &msg ) ;
96: }
97: }
98: return ( (int) msg.wParam ) ;
99:
100: } // end of WinMain()
101:
102: //---------------------------------------------------------------------------
103: // BOOL NEAR InitApplication( HANDLE hInstance )
104: //
105: // Description:
106: // First time initialization stuff. This registers information
107: // such as window classes.
108: //
109: // Parameters:
110: // HANDLE hInstance
111: // Handle to this instance of the application.
112: //
113: // History: Date Author Comment
114: // 5/ 8/91 BryanW Wrote it.
115: // 10/22/91 BryanW Fixed background color problem.
116: //
117: //---------------------------------------------------------------------------
118:
119: BOOL NEAR InitApplication( HANDLE hInstance )
120: {
121: WNDCLASS wndclass ;
122:
123: // register tty window class
124:
125: wndclass.style = NULL ;
126: wndclass.lpfnWndProc = TTYWndProc ;
127: wndclass.cbClsExtra = 0 ;
128: wndclass.cbWndExtra = TTYEXTRABYTES ;
129: wndclass.hInstance = hInstance ;
130: wndclass.hIcon = LoadIcon( hInstance, MAKEINTRESOURCE( TTYICON ) );
131: wndclass.hCursor = LoadCursor( NULL, IDC_ARROW ) ;
132: wndclass.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1) ;
133: wndclass.lpszMenuName = MAKEINTRESOURCE( TTYMENU ) ;
134: wndclass.lpszClassName = gszTTYClass ;
135:
136: return( RegisterClass( &wndclass ) ) ;
137:
138: } // end of InitApplication()
139:
140: //---------------------------------------------------------------------------
141: // HWND NEAR InitInstance( HANDLE hInstance, int nCmdShow )
142: //
143: // Description:
144: // Initializes instance specific information.
145: //
146: // Parameters:
147: // HANDLE hInstance
148: // Handle to instance
149: //
150: // int nCmdShow
151: // How do we show the window?
152: //
153: // History: Date Author Comment
154: // 5/ 8/91 BryanW Wrote it.
155: //
156: //---------------------------------------------------------------------------
157:
158: HWND NEAR InitInstance( HANDLE hInstance, int nCmdShow )
159: {
160: HWND hTTYWnd ;
161:
162: // load accelerators
163: ghAccel = LoadAccelerators( hInstance, MAKEINTRESOURCE( TTYACCEL ) ) ;
164:
165: // create the TTY window
166: hTTYWnd = CreateWindow( gszTTYClass, gszAppName,
167: WS_OVERLAPPEDWINDOW,
168: CW_USEDEFAULT, CW_USEDEFAULT,
169: CW_USEDEFAULT, CW_USEDEFAULT,
170: NULL, NULL, hInstance, NULL ) ;
171:
172: if (NULL == hTTYWnd)
173: return ( NULL ) ;
174:
175: ShowWindow( hTTYWnd, nCmdShow ) ;
176: UpdateWindow( hTTYWnd ) ;
177:
178: return ( hTTYWnd ) ;
179:
180: } // end of InitInstance()
181:
182: //---------------------------------------------------------------------------
183: // LRESULT FAR PASCAL TTYWndProc( HWND hWnd, UINT uMsg,
184: // WPARAM wParam, LPARAM lParam )
185: //
186: // Description:
187: // This is the TTY Window Proc. This handles ALL messages
188: // to the tty window.
189: //
190: // Parameters:
191: // As documented for Window procedures.
192: //
193: // Win-32 Porting Issues:
194: // - WM_HSCROLL and WM_VSCROLL packing is different under Win-32.
195: // - Needed LOWORD() of wParam for WM_CHAR messages.
196: //
197: // History: Date Author Comment
198: // 5/ 9/91 BryanW Wrote it.
199: // 6/15/92 BryanW Kill focus before disconnecting.
200: // 6/15/92 BryanW Ported to Win-32.
201: //
202: //---------------------------------------------------------------------------
203:
204: LRESULT FAR PASCAL TTYWndProc( HWND hWnd, UINT uMsg,
205: WPARAM wParam, LPARAM lParam )
206: {
207: switch (uMsg)
208: {
209: case WM_CREATE:
210: return ( CreateTTYInfo( hWnd ) ) ;
211:
212: case WM_COMMAND:
213: {
214: switch ( LOWORD( wParam ) )
215: {
216: case IDM_CONNECT:
217: if (!OpenConnection( hWnd ))
218: MessageBox( hWnd, "Connection failed.", gszAppName,
219: MB_ICONEXCLAMATION ) ;
220: break ;
221:
222: case IDM_DISCONNECT:
223: KillTTYFocus( hWnd ) ;
224: CloseConnection( hWnd ) ;
225: break ;
226:
227: case IDM_SETTINGS:
228: {
229: NPTTYINFO npTTYInfo ;
230:
231: if (NULL == (npTTYInfo = GETNPTTYINFO( hWnd )))
232: return ( FALSE ) ;
233: GoModalDialogBoxParam( GETHINST( hWnd ),
234: MAKEINTRESOURCE( SETTINGSDLGBOX ), hWnd,
235: SettingsDlgProc,
236: (LPARAM) (LPSTR) npTTYInfo ) ;
237:
238: // if fConnected, set new COM parameters
239:
240: if (CONNECTED( npTTYInfo ))
241: {
242: if (!SetupConnection( hWnd ))
243: MessageBox( hWnd, "Settings failed!", gszAppName,
244: MB_ICONEXCLAMATION ) ;
245: }
246: }
247: break ;
248:
249: case IDM_ABOUT:
250: GoModalDialogBoxParam ( GETHINST( hWnd ),
251: MAKEINTRESOURCE( ABOUTDLGBOX ),
252: hWnd,
253: AboutDlgProc, NULL ) ;
254: break;
255:
256: case IDM_EXIT:
257: PostMessage( hWnd, WM_CLOSE, NULL, 0L ) ;
258: break ;
259: }
260: }
261: break ;
262:
263: case WM_COMMNOTIFY:
264: ProcessCOMMNotification( hWnd, wParam, lParam ) ;
265: break ;
266:
267: case WM_PAINT:
268: PaintTTY( hWnd ) ;
269: break ;
270:
271: case WM_SIZE:
272: SizeTTY( hWnd, HIWORD( lParam ), LOWORD( lParam ) ) ;
273: break ;
274:
275: case WM_HSCROLL:
276: #ifdef WIN32
277: ScrollTTYHorz( hWnd, LOWORD( wParam ), HIWORD( wParam ) ) ;
278: #else
279: ScrollTTYHorz( hWnd, (WORD) wParam, LOWORD( lParam ) ) ;
280: #endif
281: break ;
282:
283: case WM_VSCROLL:
284: #ifdef WIN32
285: ScrollTTYVert( hWnd, LOWORD( wParam ), HIWORD( wParam ) ) ;
286: #else
287: ScrollTTYVert( hWnd, (WORD) wParam, LOWORD( lParam ) ) ;
288: #endif
289: break ;
290:
291: case WM_CHAR:
292: #ifdef WIN32
293: ProcessTTYCharacter( hWnd, LOBYTE( LOWORD( wParam ) ) ) ;
294: #else
295: ProcessTTYCharacter( hWnd, LOBYTE( wParam ) ) ;
296: #endif
297: break ;
298:
299: case WM_SETFOCUS:
300: SetTTYFocus( hWnd ) ;
301: break ;
302:
303: case WM_KILLFOCUS:
304: KillTTYFocus( hWnd ) ;
305: break ;
306:
307: case WM_DESTROY:
308: DestroyTTYInfo( hWnd ) ;
309: PostQuitMessage( 0 ) ;
310: break ;
311:
312: case WM_CLOSE:
313: if (IDOK != MessageBox( hWnd, "OK to close window?", "TTY Sample",
314: MB_ICONQUESTION | MB_OKCANCEL ))
315: break ;
316:
317: // fall through
318:
319: default:
320: return( DefWindowProc( hWnd, uMsg, wParam, lParam ) ) ;
321: }
322: return 0L ;
323:
324: } // end of TTYWndProc()
325:
326: //---------------------------------------------------------------------------
327: // LRESULT NEAR CreateTTYInfo( HWND hWnd )
328: //
329: // Description:
330: // Creates the tty information structure and sets
331: // menu option availability. Returns -1 if unsuccessful.
332: //
333: // Parameters:
334: // HWND hWnd
335: // Handle to main window.
336: //
337: // Win-32 Porting Issues:
338: // - Needed to initialize TERMWND( npTTYInfo ) for secondary thread.
339: // - Needed to create/initialize overlapped structures used in reads &
340: // writes to COMM device.
341: //
342: // History: Date Author Comment
343: // 10/18/91 BryanW Pulled from tty window proc.
344: // 1/13/92 BryanW Fixed bug with invalid handle
345: // caused by WM_SIZE sent by
346: // ResetTTYScreen().
347: //
348: //---------------------------------------------------------------------------
349:
350: LRESULT NEAR CreateTTYInfo( HWND hWnd )
351: {
352: HMENU hMenu ;
353: NPTTYINFO npTTYInfo ;
354:
355: if (NULL == (npTTYInfo =
356: (NPTTYINFO) LocalAlloc( LPTR, sizeof( TTYINFO ) )))
357: return ( (LRESULT) -1 ) ;
358:
359: // initialize TTY info structure
360:
361: COMDEV( npTTYInfo ) = 0 ;
362: CONNECTED( npTTYInfo ) = FALSE ;
363: CURSORSTATE( npTTYInfo ) = CS_HIDE ;
364: LOCALECHO( npTTYInfo ) = FALSE ;
365: AUTOWRAP( npTTYInfo ) = TRUE ;
366: PORT( npTTYInfo ) = 1 ;
367: BAUDRATE( npTTYInfo ) = CBR_9600 ;
368: BYTESIZE( npTTYInfo ) = 8 ;
369: FLOWCTRL( npTTYInfo ) = FC_RTSCTS ;
370: PARITY( npTTYInfo ) = NOPARITY ;
371: STOPBITS( npTTYInfo ) = ONESTOPBIT ;
372: XONXOFF( npTTYInfo ) = FALSE ;
373: XSIZE( npTTYInfo ) = 0 ;
374: YSIZE( npTTYInfo ) = 0 ;
375: XSCROLL( npTTYInfo ) = 0 ;
376: YSCROLL( npTTYInfo ) = 0 ;
377: XOFFSET( npTTYInfo ) = 0 ;
378: YOFFSET( npTTYInfo ) = 0 ;
379: COLUMN( npTTYInfo ) = 0 ;
380: ROW( npTTYInfo ) = 0 ;
381: HTTYFONT( npTTYInfo ) = NULL ;
382: FGCOLOR( npTTYInfo ) = RGB( 0, 0, 0 ) ;
383: USECNRECEIVE( npTTYInfo ) = TRUE ;
384: DISPLAYERRORS( npTTYInfo ) = TRUE ;
385: #ifdef WIN32
386: TERMWND( npTTYInfo ) = hWnd ;
387:
388: // create I/O event used for overlapped reads / writes
389:
390: READ_OS( npTTYInfo ).hEvent = CreateEvent( NULL, // no security
391: TRUE, // explicit reset req
392: FALSE, // initial event reset
393: NULL ) ; // no name
394: if (READ_OS( npTTYInfo ).hEvent == NULL)
395: {
396: LocalFree( npTTYInfo ) ;
397: return ( -1 ) ;
398: }
399: WRITE_OS( npTTYInfo ).hEvent = CreateEvent( NULL, // no security
400: TRUE, // explicit reset req
401: FALSE, // initial event reset
402: NULL ) ; // no name
403: if (NULL == WRITE_OS( npTTYInfo ).hEvent)
404: {
405: CloseHandle( READ_OS( npTTYInfo ).hEvent ) ;
406: LocalFree( npTTYInfo ) ;
407: return ( -1 ) ;
408: }
409:
410: // create "posted notification" event
411:
412: POSTEVENT( npTTYInfo ) = CreateEvent( NULL, // no security
413: TRUE, // manual reset
414: TRUE, // initial event is set
415: NULL ) ; // no name
416:
417: if (POSTEVENT( npTTYInfo ) == NULL)
418: {
419: CloseHandle( READ_OS( npTTYInfo ).hEvent ) ;
420: CloseHandle( WRITE_OS( npTTYInfo ).hEvent ) ;
421: LocalFree( npTTYInfo ) ;
422: return ( -1 ) ;
423: }
424: #endif
425:
426: // clear screen space
427:
428: _fmemset( SCREEN( npTTYInfo ), ' ', MAXROWS * MAXCOLS ) ;
429:
430: // setup default font information
431:
432: LFTTYFONT( npTTYInfo ).lfHeight = 12 ;
433: LFTTYFONT( npTTYInfo ).lfWidth = 0 ;
434: LFTTYFONT( npTTYInfo ).lfEscapement = 0 ;
435: LFTTYFONT( npTTYInfo ).lfOrientation = 0 ;
436: LFTTYFONT( npTTYInfo ).lfWeight = 0 ;
437: LFTTYFONT( npTTYInfo ).lfItalic = 0 ;
438: LFTTYFONT( npTTYInfo ).lfUnderline = 0 ;
439: LFTTYFONT( npTTYInfo ).lfStrikeOut = 0 ;
440: LFTTYFONT( npTTYInfo ).lfCharSet = OEM_CHARSET ;
441: LFTTYFONT( npTTYInfo ).lfOutPrecision = OUT_DEFAULT_PRECIS ;
442: LFTTYFONT( npTTYInfo ).lfClipPrecision = CLIP_DEFAULT_PRECIS ;
443: LFTTYFONT( npTTYInfo ).lfQuality = DEFAULT_QUALITY ;
444: LFTTYFONT( npTTYInfo ).lfPitchAndFamily = FIXED_PITCH | FF_MODERN ;
445: LFTTYFONT( npTTYInfo ).lfFaceName[0] = NULL ;
446:
447: // set TTYInfo handle before any further message processing.
448:
449: SETNPTTYINFO( hWnd, npTTYInfo ) ;
450:
451: // reset the character information, etc.
452:
453: ResetTTYScreen( hWnd, npTTYInfo ) ;
454:
455: hMenu = GetMenu( hWnd ) ;
456: EnableMenuItem( hMenu, IDM_DISCONNECT,
457: MF_GRAYED | MF_DISABLED | MF_BYCOMMAND ) ;
458: EnableMenuItem( hMenu, IDM_CONNECT, MF_ENABLED | MF_BYCOMMAND ) ;
459:
460: return ( (LRESULT) TRUE ) ;
461:
462: } // end of CreateTTYInfo()
463:
464: //---------------------------------------------------------------------------
465: // BOOL NEAR DestroyTTYInfo( HWND hWnd )
466: //
467: // Description:
468: // Destroys block associated with TTY window handle.
469: //
470: // Parameters:
471: // HWND hWnd
472: // handle to TTY window
473: //
474: // Win-32 Porting Issues:
475: // - Needed to clean up event objects created during initialization.
476: //
477: // History: Date Author Comment
478: // 5/ 8/91 BryanW Wrote it.
479: // 6/15/92 BryanW Ported to Win-32.
480: //
481: //---------------------------------------------------------------------------
482:
483: BOOL NEAR DestroyTTYInfo( HWND hWnd )
484: {
485: NPTTYINFO npTTYInfo ;
486:
487: if (NULL == (npTTYInfo = GETNPTTYINFO( hWnd )))
488: return ( FALSE ) ;
489:
490: // force connection closed (if not already closed)
491:
492: if (CONNECTED( npTTYInfo ))
493: CloseConnection( hWnd ) ;
494:
495: #ifdef WIN32
496: // clean up event objects
497:
498: CloseHandle( READ_OS( npTTYInfo ).hEvent ) ;
499: CloseHandle( WRITE_OS( npTTYInfo ).hEvent ) ;
500: CloseHandle( POSTEVENT( npTTYInfo ) ) ;
501: #endif
502:
503: DeleteObject( HTTYFONT( npTTYInfo ) ) ;
504:
505: LocalFree( npTTYInfo ) ;
506: return ( TRUE ) ;
507:
508: } // end of DestroyTTYInfo()
509:
510: //---------------------------------------------------------------------------
511: // BOOL NEAR ResetTTYScreen( HWND hWnd, NPTTYINFO npTTYInfo )
512: //
513: // Description:
514: // Resets the TTY character information and causes the
515: // screen to resize to update the scroll information.
516: //
517: // Parameters:
518: // NPTTYINFO npTTYInfo
519: // pointer to TTY info structure
520: //
521: // History: Date Author Comment
522: // 10/20/91 BryanW Wrote it.
523: //
524: //---------------------------------------------------------------------------
525:
526: BOOL NEAR ResetTTYScreen( HWND hWnd, NPTTYINFO npTTYInfo )
527: {
528: HDC hDC ;
529: TEXTMETRIC tm ;
530: RECT rcWindow ;
531:
532: if (NULL == npTTYInfo)
533: return ( FALSE ) ;
534:
535: if (NULL != HTTYFONT( npTTYInfo ))
536: DeleteObject( HTTYFONT( npTTYInfo ) ) ;
537:
538: HTTYFONT( npTTYInfo ) = CreateFontIndirect( &LFTTYFONT( npTTYInfo ) ) ;
539:
540: hDC = GetDC( hWnd ) ;
541: SelectObject( hDC, HTTYFONT( npTTYInfo ) ) ;
542: GetTextMetrics( hDC, &tm ) ;
543: ReleaseDC( hWnd, hDC ) ;
544:
545: XCHAR( npTTYInfo ) = tm.tmAveCharWidth ;
546: YCHAR( npTTYInfo ) = tm.tmHeight + tm.tmExternalLeading ;
547:
548: // a slimy hack to force the scroll position, region to
549: // be recalculated based on the new character sizes
550:
551: GetWindowRect( hWnd, &rcWindow ) ;
552: SendMessage( hWnd, WM_SIZE, SIZENORMAL,
553: (LPARAM) MAKELONG( rcWindow.right - rcWindow.left,
554: rcWindow.bottom - rcWindow.top ) ) ;
555:
556: return ( TRUE ) ;
557:
558: } // end of ResetTTYScreen()
559:
560: //---------------------------------------------------------------------------
561: // BOOL NEAR PaintTTY( HWND hWnd )
562: //
563: // Description:
564: // Paints the rectangle determined by the paint struct of
565: // the DC.
566: //
567: // Parameters:
568: // HWND hWnd
569: // handle to TTY window (as always)
570: //
571: // History: Date Author Comment
572: // 5/ 9/91 BryanW Wrote it.
573: // 10/22/91 BryanW Problem with background color
574: // and "off by one" fixed.
575: //
576: // 2/25/92 BryanW Off-by-one not quite fixed...
577: // also resolved min/max problem
578: // for windows extended beyond
579: // the "TTY display".
580: //
581: //---------------------------------------------------------------------------
582:
583: BOOL NEAR PaintTTY( HWND hWnd )
584: {
585: int nRow, nCol, nEndRow, nEndCol, nCount, nHorzPos, nVertPos ;
586: HDC hDC ;
587: HFONT hOldFont ;
588: NPTTYINFO npTTYInfo ;
589: PAINTSTRUCT ps ;
590: RECT rect ;
591:
592: if (NULL == (npTTYInfo = GETNPTTYINFO( hWnd )))
593: return ( FALSE ) ;
594:
595: hDC = BeginPaint( hWnd, &ps ) ;
596: hOldFont = SelectObject( hDC, HTTYFONT( npTTYInfo ) ) ;
597: SetTextColor( hDC, FGCOLOR( npTTYInfo ) ) ;
598: SetBkColor( hDC, GetSysColor( COLOR_WINDOW ) ) ;
599: rect = ps.rcPaint ;
600: nRow =
601: min( MAXROWS - 1,
602: max( 0, (rect.top + YOFFSET( npTTYInfo )) / YCHAR( npTTYInfo ) ) ) ;
603: nEndRow =
604: min( MAXROWS - 1,
605: ((rect.bottom + YOFFSET( npTTYInfo ) - 1) / YCHAR( npTTYInfo ) ) ) ;
606: nCol =
607: min( MAXCOLS - 1,
608: max( 0, (rect.left + XOFFSET( npTTYInfo )) / XCHAR( npTTYInfo ) ) ) ;
609: nEndCol =
610: min( MAXCOLS - 1,
611: ((rect.right + XOFFSET( npTTYInfo ) - 1) / XCHAR( npTTYInfo ) ) ) ;
612: nCount = nEndCol - nCol + 1 ;
613: for (; nRow <= nEndRow; nRow++)
614: {
615: nVertPos = (nRow * YCHAR( npTTYInfo )) - YOFFSET( npTTYInfo ) ;
616: nHorzPos = (nCol * XCHAR( npTTYInfo )) - XOFFSET( npTTYInfo ) ;
617: rect.top = nVertPos ;
618: rect.bottom = nVertPos + YCHAR( npTTYInfo ) ;
619: rect.left = nHorzPos ;
620: rect.right = nHorzPos + XCHAR( npTTYInfo ) * nCount ;
621: SetBkMode( hDC, OPAQUE ) ;
622: ExtTextOut( hDC, nHorzPos, nVertPos, ETO_OPAQUE | ETO_CLIPPED, &rect,
623: (LPSTR)( SCREEN( npTTYInfo ) + nRow * MAXCOLS + nCol ),
624: nCount, NULL ) ;
625: }
626: SelectObject( hDC, hOldFont ) ;
627: EndPaint( hWnd, &ps ) ;
628: MoveTTYCursor( hWnd ) ;
629: return ( TRUE ) ;
630:
631: } // end of PaintTTY()
632:
633: //---------------------------------------------------------------------------
634: // BOOL NEAR SizeTTY( HWND hWnd, WORD wVertSize, WORD wHorzSize )
635: //
636: // Description:
637: // Sizes TTY and sets up scrolling regions.
638: //
639: // Parameters:
640: // HWND hWnd
641: // handle to TTY window
642: //
643: // WORD wVertSize
644: // new vertical size
645: //
646: // WORD wHorzSize
647: // new horizontal size
648: //
649: // History: Date Author Comment
650: // 5/ 8/ 91 BryanW Wrote it
651: //
652: //---------------------------------------------------------------------------
653:
654: BOOL NEAR SizeTTY( HWND hWnd, WORD wVertSize, WORD wHorzSize )
655: {
656: int nScrollAmt ;
657: NPTTYINFO npTTYInfo ;
658:
659: if (NULL == (npTTYInfo = GETNPTTYINFO( hWnd )))
660: return ( FALSE ) ;
661:
662: YSIZE( npTTYInfo ) = (int) wVertSize ;
663: YSCROLL( npTTYInfo ) = max( 0, (MAXROWS * YCHAR( npTTYInfo )) -
664: YSIZE( npTTYInfo ) ) ;
665: nScrollAmt = min( YSCROLL( npTTYInfo ), YOFFSET( npTTYInfo ) ) -
666: YOFFSET( npTTYInfo ) ;
667: ScrollWindow( hWnd, 0, -nScrollAmt, NULL, NULL ) ;
668:
669: YOFFSET( npTTYInfo ) = YOFFSET( npTTYInfo ) + nScrollAmt ;
670: SetScrollPos( hWnd, SB_VERT, YOFFSET( npTTYInfo ), FALSE ) ;
671: SetScrollRange( hWnd, SB_VERT, 0, YSCROLL( npTTYInfo ), TRUE ) ;
672:
673: XSIZE( npTTYInfo ) = (int) wHorzSize ;
674: XSCROLL( npTTYInfo ) = max( 0, (MAXCOLS * XCHAR( npTTYInfo )) -
675: XSIZE( npTTYInfo ) ) ;
676: nScrollAmt = min( XSCROLL( npTTYInfo ), XOFFSET( npTTYInfo )) -
677: XOFFSET( npTTYInfo ) ;
678: ScrollWindow( hWnd, 0, -nScrollAmt, NULL, NULL ) ;
679: XOFFSET( npTTYInfo ) = XOFFSET( npTTYInfo ) + nScrollAmt ;
680: SetScrollPos( hWnd, SB_HORZ, XOFFSET( npTTYInfo ), FALSE ) ;
681: SetScrollRange( hWnd, SB_HORZ, 0, XSCROLL( npTTYInfo ), TRUE ) ;
682:
683: InvalidateRect( hWnd, NULL, TRUE ) ;
684:
685: return ( TRUE ) ;
686:
687: } // end of SizeTTY()
688:
689: //---------------------------------------------------------------------------
690: // BOOL NEAR ScrollTTYVert( HWND hWnd, WORD wScrollCmd, WORD wScrollPos )
691: //
692: // Description:
693: // Scrolls TTY window vertically.
694: //
695: // Parameters:
696: // HWND hWnd
697: // handle to TTY window
698: //
699: // WORD wScrollCmd
700: // type of scrolling we're doing
701: //
702: // WORD wScrollPos
703: // scroll position
704: //
705: // History: Date Author Comment
706: // 5/ 8/91 BryanW Wrote it.
707: //
708: //---------------------------------------------------------------------------
709:
710: BOOL NEAR ScrollTTYVert( HWND hWnd, WORD wScrollCmd, WORD wScrollPos )
711: {
712: int nScrollAmt ;
713: NPTTYINFO npTTYInfo ;
714:
715: if (NULL == (npTTYInfo = GETNPTTYINFO( hWnd )))
716: return ( FALSE ) ;
717:
718: switch (wScrollCmd)
719: {
720: case SB_TOP:
721: nScrollAmt = -YOFFSET( npTTYInfo ) ;
722: break ;
723:
724: case SB_BOTTOM:
725: nScrollAmt = YSCROLL( npTTYInfo ) - YOFFSET( npTTYInfo ) ;
726: break ;
727:
728: case SB_PAGEUP:
729: nScrollAmt = -YSIZE( npTTYInfo ) ;
730: break ;
731:
732: case SB_PAGEDOWN:
733: nScrollAmt = YSIZE( npTTYInfo ) ;
734: break ;
735:
736: case SB_LINEUP:
737: nScrollAmt = -YCHAR( npTTYInfo ) ;
738: break ;
739:
740: case SB_LINEDOWN:
741: nScrollAmt = YCHAR( npTTYInfo ) ;
742: break ;
743:
744: case SB_THUMBPOSITION:
745: nScrollAmt = wScrollPos - YOFFSET( npTTYInfo ) ;
746: break ;
747:
748: default:
749: return ( FALSE ) ;
750: }
751: if ((YOFFSET( npTTYInfo ) + nScrollAmt) > YSCROLL( npTTYInfo ))
752: nScrollAmt = YSCROLL( npTTYInfo ) - YOFFSET( npTTYInfo ) ;
753: if ((YOFFSET( npTTYInfo ) + nScrollAmt) < 0)
754: nScrollAmt = -YOFFSET( npTTYInfo ) ;
755: ScrollWindow( hWnd, 0, -nScrollAmt, NULL, NULL ) ;
756: YOFFSET( npTTYInfo ) = YOFFSET( npTTYInfo ) + nScrollAmt ;
757: SetScrollPos( hWnd, SB_VERT, YOFFSET( npTTYInfo ), TRUE ) ;
758:
759: return ( TRUE ) ;
760:
761: } // end of ScrollTTYVert()
762:
763: //---------------------------------------------------------------------------
764: // BOOL NEAR ScrollTTYHorz( HWND hWnd, WORD wScrollCmd, WORD wScrollPos )
765: //
766: // Description:
767: // Scrolls TTY window horizontally.
768: //
769: // Parameters:
770: // HWND hWnd
771: // handle to TTY window
772: //
773: // WORD wScrollCmd
774: // type of scrolling we're doing
775: //
776: // WORD wScrollPos
777: // scroll position
778: //
779: // History: Date Author Comment
780: // 5/ 8/91 BryanW Wrote it.
781: //
782: //---------------------------------------------------------------------------
783:
784: BOOL NEAR ScrollTTYHorz( HWND hWnd, WORD wScrollCmd, WORD wScrollPos )
785: {
786: int nScrollAmt ;
787: NPTTYINFO npTTYInfo ;
788:
789: if (NULL == (npTTYInfo = GETNPTTYINFO( hWnd )))
790: return ( FALSE ) ;
791:
792: switch (wScrollCmd)
793: {
794: case SB_TOP:
795: nScrollAmt = -XOFFSET( npTTYInfo ) ;
796: break ;
797:
798: case SB_BOTTOM:
799: nScrollAmt = XSCROLL( npTTYInfo ) - XOFFSET( npTTYInfo ) ;
800: break ;
801:
802: case SB_PAGEUP:
803: nScrollAmt = -XSIZE( npTTYInfo ) ;
804: break ;
805:
806: case SB_PAGEDOWN:
807: nScrollAmt = XSIZE( npTTYInfo ) ;
808: break ;
809:
810: case SB_LINEUP:
811: nScrollAmt = -XCHAR( npTTYInfo ) ;
812: break ;
813:
814: case SB_LINEDOWN:
815: nScrollAmt = XCHAR( npTTYInfo ) ;
816: break ;
817:
818: case SB_THUMBPOSITION:
819: nScrollAmt = wScrollPos - XOFFSET( npTTYInfo ) ;
820: break ;
821:
822: default:
823: return ( FALSE ) ;
824: }
825: if ((XOFFSET( npTTYInfo ) + nScrollAmt) > XSCROLL( npTTYInfo ))
826: nScrollAmt = XSCROLL( npTTYInfo ) - XOFFSET( npTTYInfo ) ;
827: if ((XOFFSET( npTTYInfo ) + nScrollAmt) < 0)
828: nScrollAmt = -XOFFSET( npTTYInfo ) ;
829: ScrollWindow( hWnd, -nScrollAmt, 0, NULL, NULL ) ;
830: XOFFSET( npTTYInfo ) = XOFFSET( npTTYInfo ) + nScrollAmt ;
831: SetScrollPos( hWnd, SB_HORZ, XOFFSET( npTTYInfo ), TRUE ) ;
832:
833: return ( TRUE ) ;
834:
835: } // end of ScrollTTYHorz()
836:
837: //---------------------------------------------------------------------------
838: // BOOL NEAR SetTTYFocus( HWND hWnd )
839: //
840: // Description:
841: // Sets the focus to the TTY window also creates caret.
842: //
843: // Parameters:
844: // HWND hWnd
845: // handle to TTY window
846: //
847: // History: Date Author Comment
848: // 5/ 9/91 BryanW Wrote it.
849: //
850: //---------------------------------------------------------------------------
851:
852: BOOL NEAR SetTTYFocus( HWND hWnd )
853: {
854: NPTTYINFO npTTYInfo ;
855:
856: if (NULL == (npTTYInfo = GETNPTTYINFO( hWnd )))
857: return ( FALSE ) ;
858:
859: if (CONNECTED( npTTYInfo ) && (CURSORSTATE( npTTYInfo ) != CS_SHOW))
860: {
861: CreateCaret( hWnd, NULL, XCHAR( npTTYInfo ), YCHAR( npTTYInfo ) ) ;
862: ShowCaret( hWnd ) ;
863: CURSORSTATE( npTTYInfo ) = CS_SHOW ;
864: }
865: MoveTTYCursor( hWnd ) ;
866: return ( TRUE ) ;
867:
868: } // end of SetTTYFocus()
869:
870: //---------------------------------------------------------------------------
871: // BOOL NEAR KillTTYFocus( HWND hWnd )
872: //
873: // Description:
874: // Kills TTY focus and destroys the caret.
875: //
876: // Parameters:
877: // HWND hWnd
878: // handle to TTY window
879: //
880: // History: Date Author Comment
881: // 5/ 9/91 BryanW Wrote it.
882: //
883: //---------------------------------------------------------------------------
884:
885: BOOL NEAR KillTTYFocus( HWND hWnd )
886: {
887: NPTTYINFO npTTYInfo ;
888:
889: if (NULL == (npTTYInfo = GETNPTTYINFO( hWnd )))
890: return ( FALSE ) ;
891:
892: if (CONNECTED( npTTYInfo ) && (CURSORSTATE( npTTYInfo ) != CS_HIDE))
893: {
894: HideCaret( hWnd ) ;
895: DestroyCaret() ;
896: CURSORSTATE( npTTYInfo ) = CS_HIDE ;
897: }
898: return ( TRUE ) ;
899:
900: } // end of KillTTYFocus()
901:
902: //---------------------------------------------------------------------------
903: // BOOL NEAR MoveTTYCursor( HWND hWnd )
904: //
905: // Description:
906: // Moves caret to current position.
907: //
908: // Parameters:
909: // HWND hWnd
910: // handle to TTY window
911: //
912: // History: Date Author Comment
913: // 5/ 9/91 BryanW Wrote it.
914: //
915: //---------------------------------------------------------------------------
916:
917: BOOL NEAR MoveTTYCursor( HWND hWnd )
918: {
919: NPTTYINFO npTTYInfo ;
920:
921: if (NULL == (npTTYInfo = GETNPTTYINFO( hWnd )))
922: return ( FALSE ) ;
923:
924: if (CONNECTED( npTTYInfo ) && (CURSORSTATE( npTTYInfo ) & CS_SHOW))
925: SetCaretPos( (COLUMN( npTTYInfo ) * XCHAR( npTTYInfo )) -
926: XOFFSET( npTTYInfo ),
927: (ROW( npTTYInfo ) * YCHAR( npTTYInfo )) -
928: YOFFSET( npTTYInfo ) ) ;
929:
930: return ( TRUE ) ;
931:
932: } // end of MoveTTYCursor()
933:
934: //---------------------------------------------------------------------------
935: // BOOL NEAR ProcessCOMMNotification( HWND hWnd,
936: // WPARAM wParam, LPARAM lParam ) ;
937: //
938: // Description:
939: // Processes the WM_COMMNOTIFY message from the COMM.DRV.
940: //
941: // Parameters:
942: // HWND hWnd
943: // handle to TTY window
944: //
945: // WPARAM wParam
946: // specifes the device (nCid)
947: //
948: // LPARAM lParam
949: // LOWORD contains event trigger
950: // HIWORD is NULL
951: //
952: // Win-32 Porting Issues:
953: // - Function was constrained by WORD and LONG declarations.
954: // - Processing under Win-32 is much simpler and additionally
955: // requires the "posted message" flag to be cleared.
956: //
957: // History: Date Author Comment
958: // 5/10/91 BryanW Wrote it.
959: // 10/18/91 BryanW Updated to verify the event.
960: // 6/15/92 BryanW Removed WORD and LONG constraints.
961: // 6/15/92 BryanW Ported to Win-32.
962: //
963: //---------------------------------------------------------------------------
964:
965: BOOL NEAR ProcessCOMMNotification( HWND hWnd, WPARAM wParam, LPARAM lParam )
966: {
967: int nLength ;
968: BYTE abIn[ MAXBLOCK + 1] ;
969: NPTTYINFO npTTYInfo ;
970: MSG msg ;
971:
972: if (NULL == (npTTYInfo = GETNPTTYINFO( hWnd )))
973: return ( FALSE ) ;
974:
975: #ifdef WIN32
976: // verify that it is a COMM event sent by our thread
977:
978: if (CN_EVENT & LOWORD( lParam ) != CN_EVENT)
979: return ( FALSE ) ;
980:
981: // We loop here since it is highly likely that the buffer
982: // can been filled while we are reading this block. This
983: // is especially true when operating at high baud rates
984: // (e.g. >= 9600 baud).
985:
986: do
987: {
988: if (nLength = ReadCommBlock( hWnd, (LPSTR) abIn, MAXBLOCK ))
989: {
990: WriteTTYBlock( hWnd, (LPSTR) abIn, nLength ) ;
991:
992: // force a paint
993:
994: UpdateWindow( hWnd ) ;
995: }
996: }
997: while (!PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE) || (nLength > 0)) ;
998:
999: // clear our "posted notification" flag
1000:
1001: SetEvent( POSTEVENT( npTTYInfo ) ) ;
1002: #else
1003: if (!USECNRECEIVE( npTTYInfo ))
1004: {
1005: // verify that it is a COMM event specified by our mask
1006:
1007: if (CN_EVENT & LOWORD( lParam ) != CN_EVENT)
1008: return ( FALSE ) ;
1009:
1010: // For Windows 3.1, rested reset the event word so we are notified
1011: // when the next event occurs
1012:
1013: GetCommEventMask( COMDEV( npTTYInfo ), EV_RXCHAR ) ;
1014:
1015: // We loop here since it is highly likely that the buffer
1016: // can been filled while we are reading this block. This
1017: // is especially true when operating at high baud rates
1018: // (e.g. >= 9600 baud).
1019:
1020: do
1021: {
1022: if (nLength = ReadCommBlock( hWnd, (LPSTR) abIn, MAXBLOCK ))
1023: {
1024: WriteTTYBlock( hWnd, (LPSTR) abIn, nLength ) ;
1025:
1026: // force a paint
1027:
1028: UpdateWindow( hWnd ) ;
1029: }
1030: }
1031: while (!PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE) || (nLength > 0)) ;
1032: }
1033: else
1034: {
1035: // verify that it is a receive event
1036:
1037: if (CN_RECEIVE & LOWORD( lParam ) != CN_RECEIVE)
1038: return ( FALSE ) ;
1039:
1040: do
1041: {
1042: if (nLength = ReadCommBlock( hWnd, (LPSTR) abIn, MAXBLOCK ))
1043: {
1044: WriteTTYBlock( hWnd, (LPSTR) abIn, nLength ) ;
1045:
1046: // force a paint
1047:
1048: UpdateWindow( hWnd ) ;
1049: }
1050: }
1051: while ((!PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE )) ||
1052: (ComStat.cbInQue >= MAXBLOCK)) ;
1053: }
1054: #endif
1055:
1056: return ( TRUE ) ;
1057:
1058: } // end of ProcessCOMMNotification()
1059:
1060: //---------------------------------------------------------------------------
1061: // BOOL NEAR ProcessTTYCharacter( HWND hWnd, BYTE bOut )
1062: //
1063: // Description:
1064: // This simply writes a character to the port and echos it
1065: // to the TTY screen if fLocalEcho is set. Some minor
1066: // keyboard mapping could be performed here.
1067: //
1068: // Parameters:
1069: // HWND hWnd
1070: // handle to TTY window
1071: //
1072: // BYTE bOut
1073: // byte from keyboard
1074: //
1075: // History: Date Author Comment
1076: // 5/11/91 BryanW Wrote it.
1077: //
1078: //---------------------------------------------------------------------------
1079:
1080: BOOL NEAR ProcessTTYCharacter( HWND hWnd, BYTE bOut )
1081: {
1082: NPTTYINFO npTTYInfo ;
1083:
1084: if (NULL == (npTTYInfo = GETNPTTYINFO( hWnd )))
1085: return ( FALSE ) ;
1086:
1087: if (!CONNECTED( npTTYInfo ))
1088: return ( FALSE ) ;
1089:
1090: WriteCommByte( hWnd, bOut ) ;
1091: if (LOCALECHO( npTTYInfo ))
1092: WriteTTYBlock( hWnd, &bOut, 1 ) ;
1093:
1094: return ( TRUE ) ;
1095:
1096: } // end of ProcessTTYCharacter()
1097:
1098: //---------------------------------------------------------------------------
1099: // BOOL NEAR OpenConnection( HWND hWnd )
1100: //
1101: // Description:
1102: // Opens communication port specified in the TTYINFO struct.
1103: // It also sets the CommState and notifies the window via
1104: // the fConnected flag in the TTYINFO struct.
1105: //
1106: // Parameters:
1107: // HWND hWnd
1108: // handle to TTY window
1109: //
1110: // Win-32 Porting Issues:
1111: // - OpenComm() is not supported under Win-32. Use OpenFile()
1112: // and setup for OVERLAPPED_IO.
1113: // - Win-32 has specific communication timeout parameters.
1114: // - Created the secondary thread for event notification.
1115: //
1116: // History: Date Author Comment
1117: // 5/ 9/91 BryanW Wrote it.
1118: //
1119: //---------------------------------------------------------------------------
1120:
1121: BOOL NEAR OpenConnection( HWND hWnd )
1122: {
1123: char szPort[ 10 ], szTemp[ 10 ] ;
1124: BOOL fRetVal ;
1125: HCURSOR hOldCursor, hWaitCursor ;
1126: HMENU hMenu ;
1127: NPTTYINFO npTTYInfo ;
1128:
1129: #ifdef WIN32
1130: HANDLE hCommWatchThread ;
1131: DWORD dwThreadID ;
1132: COMMTIMEOUTS CommTimeOuts ;
1133: #endif
1134:
1135: if (NULL == (npTTYInfo = GETNPTTYINFO( hWnd )))
1136: return ( FALSE ) ;
1137:
1138: // show the hourglass cursor
1139: hWaitCursor = LoadCursor( NULL, IDC_WAIT ) ;
1140: hOldCursor = SetCursor( hWaitCursor ) ;
1141:
1142: // load the COM prefix string and append port number
1143:
1144: LoadString( GETHINST( hWnd ), IDS_COMPREFIX, szTemp, sizeof( szTemp ) ) ;
1145: wsprintf( szPort, "%s%d", (LPSTR) szTemp, PORT( npTTYInfo ) ) ;
1146:
1147: // open COMM device
1148:
1149: #ifdef WIN32
1150: if ((COMDEV( npTTYInfo ) =
1151: CreateFile( szPort, GENERIC_READ | GENERIC_WRITE,
1152: 0, // exclusive access
1153: NULL, // no security attrs
1154: OPEN_EXISTING,
1155: FILE_FLAG_OVERLAPPED, // overlapped I/O
1156: NULL )) == (HANDLE) -1 )
1157: return ( FALSE ) ;
1158: else
1159: {
1160: // purge any information in the buffer
1161:
1162: PurgeComm( COMDEV( npTTYInfo ), PURGE_TXABORT | PURGE_RXABORT |
1163: PURGE_TXCLEAR | PURGE_RXCLEAR ) ;
1164:
1165: // set up for overlapped non-blocking I/O
1166:
1167: CommTimeOuts.ReadIntervalTimeout = 0xFFFFFFFF ;
1168: CommTimeOuts.ReadTotalTimeoutMultiplier = 0 ;
1169: CommTimeOuts.ReadTotalTimeoutConstant = 0 ;
1170: CommTimeOuts.WriteTotalTimeoutMultiplier = 0 ;
1171: CommTimeOuts.WriteTotalTimeoutConstant = 0 ;
1172: SetCommTimeouts( COMDEV( npTTYInfo ), &CommTimeOuts ) ;
1173: }
1174: #else
1175: if ((COMDEV( npTTYInfo ) = OpenComm( szPort, RXQUEUE, TXQUEUE )) < 0)
1176: return ( FALSE ) ;
1177: #endif
1178:
1179: fRetVal = SetupConnection( hWnd ) ;
1180:
1181: if (fRetVal)
1182: {
1183: CONNECTED( npTTYInfo ) = TRUE ;
1184:
1185: #ifdef WIN32
1186: // In the case of Win32, we create a secondary thread
1187: // to watch for an event.
1188:
1189: if (NULL == (hCommWatchThread =
1190: CreateThread( (LPSECURITY_ATTRIBUTES) NULL,
1191: 0,
1192: (LPTHREAD_START_ROUTINE) CommWatchProc,
1193: (LPVOID) npTTYInfo,
1194: NULL, &dwThreadID )))
1195: {
1196: CONNECTED( npTTYInfo ) = FALSE ;
1197: CloseHandle( COMDEV( npTTYInfo ) ) ;
1198: fRetVal = FALSE ;
1199: }
1200: else
1201: {
1202: THREADID( npTTYInfo ) = dwThreadID ;
1203: HTHREAD( npTTYInfo ) = hCommWatchThread ;
1204:
1205: // assert DTR
1206:
1207: EscapeCommFunction( COMDEV( npTTYInfo ), SETDTR ) ;
1208:
1209:
1210: SetTTYFocus( hWnd ) ;
1211:
1212: hMenu = GetMenu( hWnd ) ;
1213: EnableMenuItem( hMenu, IDM_DISCONNECT,
1214: MF_ENABLED | MF_BYCOMMAND ) ;
1215: EnableMenuItem( hMenu, IDM_CONNECT,
1216: MF_GRAYED | MF_DISABLED | MF_BYCOMMAND ) ;
1217: }
1218: #else
1219: // Under Windows 3.1, we set up notifications from COMM.DRV
1220:
1221: if (!USECNRECEIVE( npTTYInfo ))
1222: {
1223: // In this case we really are only using the notifications
1224: // for the received characters - it could be expanded to
1225: // cover the changes in CD or other status lines.
1226:
1227: SetCommEventMask( COMDEV( npTTYInfo ), EV_RXCHAR ) ;
1228:
1229: // Enable notifications for events only.
1230:
1231: // NB: This method does not use the specific
1232: // in/out queue triggers.
1233:
1234: EnableCommNotification( COMDEV( npTTYInfo ), hWnd, -1, -1 ) ;
1235: }
1236: else
1237: {
1238: // Enable notification for CN_RECEIVE events.
1239:
1240: EnableCommNotification( COMDEV( npTTYInfo ), hWnd, MAXBLOCK, -1 ) ;
1241: }
1242:
1243: // assert DTR
1244:
1245: EscapeCommFunction( COMDEV( npTTYInfo ), SETDTR ) ;
1246:
1247:
1248: SetTTYFocus( hWnd ) ;
1249:
1250: hMenu = GetMenu( hWnd ) ;
1251: EnableMenuItem( hMenu, IDM_DISCONNECT,
1252: MF_ENABLED | MF_BYCOMMAND ) ;
1253: EnableMenuItem( hMenu, IDM_CONNECT,
1254: MF_GRAYED | MF_DISABLED | MF_BYCOMMAND ) ;
1255: #endif
1256: }
1257: else
1258: {
1259: CONNECTED( npTTYInfo ) = FALSE ;
1260:
1261: #ifdef WIN32
1262: CloseHandle( COMDEV( npTTYInfo ) ) ;
1263: #else
1264: CloseComm( COMDEV( npTTYInfo ) ) ;
1265: #endif
1266: }
1267:
1268: // restore cursor
1269:
1270: SetCursor( hOldCursor ) ;
1271:
1272: return ( fRetVal ) ;
1273:
1274: } // end of OpenConnection()
1275:
1276: //---------------------------------------------------------------------------
1277: // BOOL NEAR SetupConnection( HWND hWnd )
1278: //
1279: // Description:
1280: // This routines sets up the DCB based on settings in the
1281: // TTY info structure and performs a SetCommState().
1282: //
1283: // Parameters:
1284: // HWND hWnd
1285: // handle to TTY window
1286: //
1287: // Win-32 Porting Issues:
1288: // - Win-32 requires a slightly different processing of the DCB.
1289: // Changes were made for configuration of the hardware handshaking
1290: // lines.
1291: //
1292: // History: Date Author Comment
1293: // 5/ 9/91 BryanW Wrote it.
1294: //
1295: //---------------------------------------------------------------------------
1296:
1297: BOOL NEAR SetupConnection( HWND hWnd )
1298: {
1299: BOOL fRetVal ;
1300: BYTE bSet ;
1301: DCB dcb ;
1302: NPTTYINFO npTTYInfo ;
1303:
1304: if (NULL == (npTTYInfo = GETNPTTYINFO( hWnd )))
1305: return ( FALSE ) ;
1306:
1307: #ifdef WIN32
1308: dcb.DCBlength = sizeof( DCB ) ;
1309: #endif
1310:
1311: GetCommState( COMDEV( npTTYInfo ), &dcb ) ;
1312:
1313: dcb.BaudRate = BAUDRATE( npTTYInfo ) ;
1314: dcb.ByteSize = BYTESIZE( npTTYInfo ) ;
1315: dcb.Parity = PARITY( npTTYInfo ) ;
1316: dcb.StopBits = STOPBITS( npTTYInfo ) ;
1317:
1318: // setup hardware flow control
1319:
1320: bSet = (BYTE) ((FLOWCTRL( npTTYInfo ) & FC_DTRDSR) != 0) ;
1321: #ifdef WIN32
1322: dcb.fOutxDsrFlow = bSet ;
1323: if (bSet)
1324: dcb.fDtrControl = DTR_CONTROL_HANDSHAKE ;
1325: else
1326: dcb.fDtrControl = DTR_CONTROL_ENABLE ;
1327: #else
1328: dcb.fOutxDsrFlow = dcb.fDtrflow = bSet ;
1329: dcb.DsrTimeout = (bSet) ? 30 : 0 ;
1330: #endif
1331:
1332: bSet = (BYTE) ((FLOWCTRL( npTTYInfo ) & FC_RTSCTS) != 0) ;
1333: #ifdef WIN32
1334: dcb.fOutxCtsFlow = bSet ;
1335: if (bSet)
1336: dcb.fRtsControl = RTS_CONTROL_HANDSHAKE ;
1337: else
1338: dcb.fRtsControl = RTS_CONTROL_ENABLE ;
1339: #else
1340: dcb.fOutxCtsFlow = dcb.fRtsflow = bSet ;
1341: dcb.CtsTimeout = (bSet) ? 30 : 0 ;
1342: #endif
1343:
1344: // setup software flow control
1345:
1346: bSet = (BYTE) ((FLOWCTRL( npTTYInfo ) & FC_XONXOFF) != 0) ;
1347:
1348: dcb.fInX = dcb.fOutX = bSet ;
1349: dcb.XonChar = ASCII_XON ;
1350: dcb.XoffChar = ASCII_XOFF ;
1351: dcb.XonLim = 100 ;
1352: dcb.XoffLim = 100 ;
1353:
1354: // other various settings
1355:
1356: dcb.fBinary = TRUE ;
1357: dcb.fParity = TRUE ;
1358:
1359: #ifndef WIN32
1360: dcb.fRtsDisable = FALSE ;
1361: dcb.fDtrDisable = FALSE ;
1362: fRetVal = !(SetCommState( &dcb ) < 0) ;
1363: #else
1364: fRetVal = SetCommState( COMDEV( npTTYInfo ), &dcb ) ;
1365: #endif
1366:
1367: return ( fRetVal ) ;
1368:
1369: } // end of SetupConnection()
1370:
1371: //---------------------------------------------------------------------------
1372: // BOOL NEAR CloseConnection( HWND hWnd )
1373: //
1374: // Description:
1375: // Closes the connection to the port. Resets the connect flag
1376: // in the TTYINFO struct.
1377: //
1378: // Parameters:
1379: // HWND hWnd
1380: // handle to TTY window
1381: //
1382: // Win-32 Porting Issues:
1383: // - Needed to stop secondary thread. SetCommMask() will signal the
1384: // WaitCommEvent() event and the thread will halt when the
1385: // CONNECTED() flag is clear.
1386: // - Use new PurgeComm() API to clear communications driver before
1387: // closing device.
1388: //
1389: // History: Date Author Comment
1390: // 5/ 9/91 BryanW Wrote it.
1391: // 6/15/92 BryanW Ported to Win-32.
1392: //
1393: //---------------------------------------------------------------------------
1394:
1395: BOOL NEAR CloseConnection( HWND hWnd )
1396: {
1397: HMENU hMenu ;
1398: NPTTYINFO npTTYInfo ;
1399:
1400: if (NULL == (npTTYInfo = GETNPTTYINFO( hWnd )))
1401: return ( FALSE ) ;
1402:
1403: // set connected flag to FALSE
1404:
1405: CONNECTED( npTTYInfo ) = FALSE ;
1406:
1407: #ifdef WIN32
1408: // disable event notification and wait for thread
1409: // to halt
1410:
1411: SetCommMask( COMDEV( npTTYInfo ), NULL ) ;
1412:
1413: // block until thread has been halted
1414:
1415: while (THREADID( npTTYInfo ) != NULL) ;
1416: #else
1417: // Disable event notification. Using a NULL hWnd tells
1418: // the COMM.DRV to disable future notifications.
1419:
1420: EnableCommNotification( COMDEV( npTTYInfo ), NULL, -1, -1 ) ;
1421: #endif
1422:
1423: // kill the focus
1424:
1425: KillTTYFocus( hWnd ) ;
1426:
1427: // drop DTR
1428:
1429: EscapeCommFunction( COMDEV( npTTYInfo ), CLRDTR ) ;
1430:
1431: #ifdef WIN32
1432: // purge any outstanding reads/writes and close device handle
1433:
1434: PurgeComm( COMDEV( npTTYInfo ), PURGE_TXABORT | PURGE_RXABORT |
1435: PURGE_TXCLEAR | PURGE_RXCLEAR ) ;
1436: CloseHandle( COMDEV( npTTYInfo ) ) ;
1437: #else
1438: // close comm connection
1439:
1440: CloseComm( COMDEV( npTTYInfo ) ) ;
1441: #endif
1442:
1443: // change the selectable items in the menu
1444:
1445: hMenu = GetMenu( hWnd ) ;
1446: EnableMenuItem( hMenu, IDM_DISCONNECT,
1447: MF_GRAYED | MF_DISABLED | MF_BYCOMMAND ) ;
1448: EnableMenuItem( hMenu, IDM_CONNECT,
1449: MF_ENABLED | MF_BYCOMMAND ) ;
1450:
1451: return ( TRUE ) ;
1452:
1453: } // end of CloseConnection()
1454:
1455: //---------------------------------------------------------------------------
1456: // int NEAR ReadCommBlock( HWND hWnd, LPSTR lpszBlock, int nMaxLength )
1457: //
1458: // Description:
1459: // Reads a block from the COM port and stuffs it into
1460: // the provided block.
1461: //
1462: // Parameters:
1463: // HWND hWnd
1464: // handle to TTY window
1465: //
1466: // LPSTR lpszBlock
1467: // block used for storage
1468: //
1469: // int nMaxLength
1470: // max length of block to read
1471: //
1472: // Win-32 Porting Issues:
1473: // - ReadComm() has been replaced by ReadFile() in Win-32.
1474: // - Overlapped I/O has been implemented.
1475: //
1476: // History: Date Author Comment
1477: // 5/10/91 BryanW Wrote it.
1478: //
1479: //---------------------------------------------------------------------------
1480:
1481: int NEAR ReadCommBlock( HWND hWnd, LPSTR lpszBlock, int nMaxLength )
1482: {
1483: #ifdef WIN32
1484: BOOL fReadStat ;
1485: COMSTAT ComStat ;
1.1.1.2 ! root 1486: DWORD dwErrorFlags, dwLength ;
1.1 root 1487: #else
1.1.1.2 ! root 1488: int nError, nLength ;
1.1 root 1489: #endif
1490:
1491: char szError[ 10 ] ;
1492: NPTTYINFO npTTYInfo ;
1493:
1494: if (NULL == (npTTYInfo = GETNPTTYINFO( hWnd )))
1495: return ( FALSE ) ;
1496:
1497: #ifdef WIN32
1498: ClearCommError( COMDEV( npTTYInfo ), &dwErrorFlags, &ComStat ) ;
1499: if ((dwErrorFlags > 0) && DISPLAYERRORS( npTTYInfo ))
1500: {
1501: wsprintf( szError, "<CE-%u>", dwErrorFlags ) ;
1502: WriteTTYBlock( hWnd, szError, lstrlen( szError ) ) ;
1503: }
1.1.1.2 ! root 1504: dwLength = min( (DWORD) nMaxLength, ComStat.cbInQue ) ;
! 1505: if (dwLength > 0)
1.1 root 1506: {
1507: fReadStat = ReadFile( COMDEV( npTTYInfo ), lpszBlock,
1.1.1.2 ! root 1508: dwLength, &dwLength, &READ_OS( npTTYInfo ) ) ;
1.1 root 1509: if (!fReadStat)
1510: {
1511: if (GetLastError() == ERROR_IO_PENDING)
1512: {
1513: // wait for a second for this transmission to complete
1514:
1515: if (WaitForSingleObject( READ_OS( npTTYInfo ).hEvent, 1000 ))
1.1.1.2 ! root 1516: dwLength = 0 ;
1.1 root 1517: else
1518: {
1519: GetOverlappedResult( COMDEV( npTTYInfo ),
1520: &READ_OS( npTTYInfo ),
1.1.1.2 ! root 1521: &dwLength, FALSE ) ;
! 1522: READ_OS( npTTYInfo ).Offset += dwLength ;
1.1 root 1523: }
1524: }
1525: else
1526: // some other error occurred
1527:
1.1.1.2 ! root 1528: dwLength = 0 ;
1.1 root 1529: }
1530: }
1.1.1.2 ! root 1531: return ( dwLength ) ;
1.1 root 1532: #else
1533: nLength = ReadComm( COMDEV( npTTYInfo ), lpszBlock, nMaxLength ) ;
1534:
1535: if (nLength < 0)
1536: {
1537: nLength *= -1 ;
1538: while (nError = GetCommError( COMDEV( npTTYInfo ), NULL ))
1539: {
1540: if (DISPLAYERRORS( npTTYInfo ))
1541: {
1542: wsprintf( szError, "<CE-%d>", nError ) ;
1543: WriteTTYBlock( hWnd, szError, lstrlen( szError ) ) ;
1544: }
1545: }
1546: }
1.1.1.2 ! root 1547: return ( nLength ) ;
1.1 root 1548: #endif
1549:
1550:
1551: } // end of ReadCommBlock()
1552:
1553: //---------------------------------------------------------------------------
1554: // BOOL NEAR WriteCommByte( HWND hWnd, BYTE bByte )
1555: //
1556: // Description:
1557: // Writes a byte to the COM port specified in the associated
1558: // TTY info structure.
1559: //
1560: // Parameters:
1561: // HWND hWnd
1562: // handle to TTY window
1563: //
1564: // BYTE bByte
1565: // byte to write to port
1566: //
1567: // Win-32 Porting Issues:
1568: // - WriteComm() has been replaced by WriteFile() in Win-32.
1569: // - Overlapped I/O has been implemented.
1570: //
1571: // History: Date Author Comment
1572: // 5/10/91 BryanW Wrote it.
1573: //
1574: //---------------------------------------------------------------------------
1575:
1576: BOOL NEAR WriteCommByte( HWND hWnd, BYTE bByte )
1577: {
1578: #ifdef WIN32
1579: BOOL fWriteStat ;
1580: DWORD dwBytesWritten ;
1581: #endif
1582: NPTTYINFO npTTYInfo ;
1583:
1584: if (NULL == (npTTYInfo = GETNPTTYINFO( hWnd )))
1585: return ( FALSE ) ;
1586:
1587: #ifdef WIN32
1588: fWriteStat = WriteFile( COMDEV( npTTYInfo ), (LPSTR) &bByte, 1,
1589: &dwBytesWritten, &WRITE_OS( npTTYInfo ) ) ;
1590: if (!fWriteStat && (GetLastError() == ERROR_IO_PENDING))
1591: {
1592: // wait for a second for this transmission to complete
1593:
1594: if (WaitForSingleObject( WRITE_OS( npTTYInfo ).hEvent, 1000 ))
1595: dwBytesWritten = 0 ;
1596: else
1597: {
1598: GetOverlappedResult( COMDEV( npTTYInfo ),
1599: &WRITE_OS( npTTYInfo ),
1600: &dwBytesWritten, FALSE ) ;
1601: WRITE_OS( npTTYInfo ).Offset += dwBytesWritten ;
1602: }
1603: }
1604: #else
1605: WriteComm( COMDEV( npTTYInfo ), (LPSTR) &bByte, 1 ) ;
1606: #endif
1607: return ( TRUE ) ;
1608:
1609: } // end of WriteCommByte()
1610:
1611: //---------------------------------------------------------------------------
1612: // BOOL NEAR WriteTTYBlock( HWND hWnd, LPSTR lpBlock, int nLength )
1613: //
1614: // Description:
1615: // Writes block to TTY screen. Nothing fancy - just
1616: // straight TTY.
1617: //
1618: // Parameters:
1619: // HWND hWnd
1620: // handle to TTY window
1621: //
1622: // LPSTR lpBlock
1623: // far pointer to block of data
1624: //
1625: // int nLength
1626: // length of block
1627: //
1628: // History: Date Author Comment
1629: // 5/ 9/91 BryanW Wrote it.
1630: // 5/20/91 BryanW Modified... not character based,
1631: // block based now. It was processing
1632: // per char.
1633: //
1634: //---------------------------------------------------------------------------
1635:
1636: BOOL NEAR WriteTTYBlock( HWND hWnd, LPSTR lpBlock, int nLength )
1637: {
1638: int i ;
1639: NPTTYINFO npTTYInfo ;
1640: RECT rect ;
1641:
1642: if (NULL == (npTTYInfo = GETNPTTYINFO( hWnd )))
1643: return ( FALSE ) ;
1644:
1645: for (i = 0 ; i < nLength; i++)
1646: {
1647: switch (lpBlock[ i ])
1648: {
1649: case ASCII_BEL:
1650: // Bell
1651: MessageBeep( 0 ) ;
1652: break ;
1653:
1654: case ASCII_BS:
1655: // Backspace
1656: if (COLUMN( npTTYInfo ) > 0)
1657: COLUMN( npTTYInfo ) -- ;
1658: MoveTTYCursor( hWnd ) ;
1659: break ;
1660:
1661: case ASCII_CR:
1662: // Carriage return
1663: COLUMN( npTTYInfo ) = 0 ;
1664: MoveTTYCursor( hWnd ) ;
1665: if (!NEWLINE( npTTYInfo ))
1666: break;
1667:
1668: // fall through
1669:
1670: case ASCII_LF:
1671: // Line feed
1672: if (ROW( npTTYInfo )++ == MAXROWS - 1)
1673: {
1674: _fmemmove( (LPSTR) (SCREEN( npTTYInfo )),
1675: (LPSTR) (SCREEN( npTTYInfo ) + MAXCOLS),
1676: (MAXROWS - 1) * MAXCOLS ) ;
1677: _fmemset( (LPSTR) (SCREEN( npTTYInfo ) + (MAXROWS - 1) * MAXCOLS),
1678: ' ', MAXCOLS ) ;
1679: InvalidateRect( hWnd, NULL, FALSE ) ;
1680: ROW( npTTYInfo )-- ;
1681: }
1682: MoveTTYCursor( hWnd ) ;
1683: break ;
1684:
1685: default:
1686: *(SCREEN( npTTYInfo ) + ROW( npTTYInfo ) * MAXCOLS +
1687: COLUMN( npTTYInfo )) = lpBlock[ i ] ;
1688: rect.left = (COLUMN( npTTYInfo ) * XCHAR( npTTYInfo )) -
1689: XOFFSET( npTTYInfo ) ;
1690: rect.right = rect.left + XCHAR( npTTYInfo ) ;
1691: rect.top = (ROW( npTTYInfo ) * YCHAR( npTTYInfo )) -
1692: YOFFSET( npTTYInfo ) ;
1693: rect.bottom = rect.top + YCHAR( npTTYInfo ) ;
1694: InvalidateRect( hWnd, &rect, FALSE ) ;
1695:
1696: // Line wrap
1697: if (COLUMN( npTTYInfo ) < MAXCOLS - 1)
1698: COLUMN( npTTYInfo )++ ;
1699: else if (AUTOWRAP( npTTYInfo ))
1700: WriteTTYBlock( hWnd, "\r\n", 2 ) ;
1701: break;
1702: }
1703: }
1704: return ( TRUE ) ;
1705:
1706: } // end of WriteTTYBlock()
1707:
1708: //---------------------------------------------------------------------------
1709: // VOID NEAR GoModalDialogBoxParam( HINSTANCE hInstance,
1710: // LPCSTR lpszTemplate, HWND hWnd,
1711: // DLGPROC lpDlgProc, LPARAM lParam )
1712: //
1713: // Description:
1714: // It is a simple utility function that simply performs the
1715: // MPI and invokes the dialog box with a DWORD paramter.
1716: //
1717: // Parameters:
1718: // similar to that of DialogBoxParam() with the exception
1719: // that the lpDlgProc is not a procedure instance
1720: //
1721: // History: Date Author Comment
1722: // 5/10/91 BryanW Wrote it.
1723: //
1724: //---------------------------------------------------------------------------
1725:
1726: VOID NEAR GoModalDialogBoxParam( HINSTANCE hInstance, LPCSTR lpszTemplate,
1727: HWND hWnd, DLGPROC lpDlgProc, LPARAM lParam )
1728: {
1729: DLGPROC lpProcInstance ;
1730:
1731: lpProcInstance = (DLGPROC) MakeProcInstance( (FARPROC) lpDlgProc,
1732: hInstance ) ;
1733: DialogBoxParam( hInstance, lpszTemplate, hWnd, lpProcInstance, lParam ) ;
1734: FreeProcInstance( (FARPROC) lpProcInstance ) ;
1735:
1736: } // end of GoModalDialogBoxParam()
1737:
1738: //---------------------------------------------------------------------------
1739: // BOOL FAR PASCAL AboutDlgProc( HWND hDlg, UINT uMsg,
1740: // WPARAM wParam, LPARAM lParam )
1741: //
1742: // Description:
1743: // Simulates the Windows System Dialog Box.
1744: //
1745: // Parameters:
1746: // Same as standard dialog procedures.
1747: //
1748: // History: Date Author Comment
1749: // 10/19/91 BryanW Added this little extra.
1750: //
1751: //---------------------------------------------------------------------------
1752:
1753: BOOL FAR PASCAL AboutDlgProc( HWND hDlg, UINT uMsg,
1754: WPARAM wParam, LPARAM lParam )
1755: {
1756: switch (uMsg)
1757: {
1758: case WM_INITDIALOG:
1759: {
1760: #ifdef WIN32
1761: char szBuffer[ MAXLEN_TEMPSTR ], szTemp[ MAXLEN_TEMPSTR ];
1762: WORD wRevision, wVersion ;
1763: #else
1764: int idModeString ;
1765: char szBuffer[ MAXLEN_TEMPSTR ], szTemp[ MAXLEN_TEMPSTR ] ;
1766: DWORD dwFreeMemory, dwWinFlags ;
1767: WORD wFreeResources, wRevision, wVersion ;
1768: #endif
1769:
1770: #ifdef ABOUTDLG_USEBITMAP
1771: // if we are using the bitmap, hide the icon
1772:
1773: ShowWindow( GetDlgItem( hDlg, IDD_ABOUTICON ), SW_HIDE ) ;
1774: #endif
1775: // sets up the version number for Windows
1776:
1777: wVersion = LOWORD( GetVersion() ) ;
1778: wRevision = HIBYTE( wVersion ) ;
1779: wVersion = LOBYTE( wVersion ) ;
1780:
1781: GetDlgItemText( hDlg, IDD_TITLELINE, szTemp, sizeof( szTemp ) ) ;
1782: wsprintf( szBuffer, szTemp, wVersion, wRevision ) ;
1783: SetDlgItemText( hDlg, IDD_TITLELINE, szBuffer ) ;
1784:
1785: // sets up version number for TTY
1786:
1787: GetDlgItemText( hDlg, IDD_VERSION, szTemp, sizeof( szTemp ) ) ;
1788: wsprintf( szBuffer, szTemp, VER_MAJOR, VER_MINOR, VER_BUILD ) ;
1789: SetDlgItemText( hDlg, IDD_VERSION, (LPSTR) szBuffer ) ;
1790:
1791: // get by-line
1792:
1793: LoadString( GETHINST( hDlg ), IDS_BYLINE, szBuffer,
1794: sizeof( szBuffer ) ) ;
1795: SetDlgItemText( hDlg, IDD_BYLINE, szBuffer ) ;
1796:
1797: #ifndef WIN32
1798: // set windows mode information
1799:
1800: dwWinFlags = GetWinFlags() ;
1801: if (dwWinFlags & WF_ENHANCED)
1802: idModeString = IDS_MODE_ENHANCED ;
1803: else if (dwWinFlags & WF_STANDARD)
1804: idModeString = IDS_MODE_STANDARD ;
1805: else if (dwWinFlags & WF_WLO)
1806: idModeString = IDS_MODE_WLO ;
1807: else
1808: idModeString = IDS_MODE_UNDEF ;
1809:
1810: LoadString( GETHINST( hDlg ), idModeString, szBuffer,
1811: sizeof( szBuffer ) ) ;
1812: SetDlgItemText( hDlg, IDD_WINDOWSMODE, szBuffer ) ;
1813: #else
1814: SetDlgItemText( hDlg, IDD_WINDOWSMODE, "NT Mode" ) ;
1815: #endif
1816:
1.1.1.2 ! root 1817: #ifndef WIN32
1.1 root 1818: // get free memory information
1819:
1820: dwFreeMemory = GetFreeSpace( 0 ) / 1024L ;
1821: GetDlgItemText( hDlg, IDD_FREEMEM, szTemp, sizeof( szTemp ) ) ;
1822: wsprintf( szBuffer, szTemp, dwFreeMemory ) ;
1823: SetDlgItemText( hDlg, IDD_FREEMEM, (LPSTR) szBuffer ) ;
1824:
1825: // get free resources information
1826:
1827: wFreeResources = GetFreeSystemResources( 0 ) ;
1828: GetDlgItemText( hDlg, IDD_RESOURCES, szTemp, sizeof( szTemp ) ) ;
1829: wsprintf( szBuffer, szTemp, wFreeResources ) ;
1830: SetDlgItemText( hDlg, IDD_RESOURCES, (LPSTR) szBuffer ) ;
1831: #endif
1832: }
1833: return ( TRUE ) ;
1834:
1835: #ifdef ABOUTDLG_USEBITMAP
1836: // used to paint the bitmap
1837:
1838: case WM_PAINT:
1839: {
1840: HBITMAP hBitMap ;
1841: HDC hDC, hMemDC ;
1842: PAINTSTRUCT ps ;
1843:
1844: // load bitmap and display it
1845:
1846: hDC = BeginPaint( hDlg, &ps ) ;
1847: if (NULL != (hMemDC = CreateCompatibleDC( hDC )))
1848: {
1849: hBitMap = LoadBitmap( GETHINST( hDlg ),
1850: MAKEINTRESOURCE( TTYBITMAP ) ) ;
1851: hBitMap = SelectObject( hMemDC, hBitMap ) ;
1852: BitBlt( hDC, 10, 10, 64, 64, hMemDC, 0, 0, SRCCOPY ) ;
1853: DeleteObject( SelectObject( hMemDC, hBitMap ) ) ;
1854: DeleteDC( hMemDC ) ;
1855: }
1856: EndPaint( hDlg, &ps ) ;
1857: }
1858: break ;
1859: #endif
1860:
1861: case WM_COMMAND:
1862: if (LOWORD( wParam ) == IDD_OK)
1863: {
1864: EndDialog( hDlg, TRUE ) ;
1865: return ( TRUE ) ;
1866: }
1867: break;
1868: }
1869: return ( FALSE ) ;
1870:
1871: } // end of AboutDlgProc()
1872:
1873: //---------------------------------------------------------------------------
1874: // VOID NEAR FillComboBox( HINSTANCE hInstance, HWND hCtrlWnd, int nIDString,
1875: // WORD NEAR *npTable, WORD wTableLen,
1876: // WORD wCurrentSetting )
1877: //
1878: // Description:
1879: // Fills the given combo box with strings from the resource
1880: // table starting at nIDString. Associated items are
1881: // added from given table. The combo box is notified of
1882: // the current setting.
1883: //
1884: // Parameters:
1885: // HINSTANCE hInstance
1886: // handle to application instance
1887: //
1888: // HWND hCtrlWnd
1889: // handle to combo box control
1890: //
1891: // int nIDString
1892: // first resource string id
1893: //
1894: // DWORD NEAR *npTable
1895: // near point to table of associated values
1896: //
1897: // WORD wTableLen
1898: // length of table
1899: //
1900: // DWORD dwCurrentSetting
1901: // current setting (for combo box selection)
1902: //
1903: // History: Date Author Comment
1904: // 10/20/91 BryanW Pulled from the init procedure.
1905: //
1906: //---------------------------------------------------------------------------
1907:
1908: VOID NEAR FillComboBox( HINSTANCE hInstance, HWND hCtrlWnd, int nIDString,
1909: DWORD NEAR *npTable, WORD wTableLen,
1910: DWORD dwCurrentSetting )
1911: {
1912: char szBuffer[ MAXLEN_TEMPSTR ] ;
1913: WORD wCount, wPosition ;
1914:
1915: for (wCount = 0; wCount < wTableLen; wCount++)
1916: {
1917: // load the string from the string resources and
1918: // add it to the combo box
1919:
1920: LoadString( hInstance, nIDString + wCount, szBuffer, sizeof( szBuffer ) ) ;
1921: wPosition = LOWORD( SendMessage( hCtrlWnd, CB_ADDSTRING, NULL,
1922: (LPARAM) (LPSTR) szBuffer ) ) ;
1923:
1924: // use item data to store the actual table value
1925:
1926: SendMessage( hCtrlWnd, CB_SETITEMDATA, (WPARAM) wPosition,
1927: (LPARAM) *(npTable + wCount) ) ;
1928:
1929: // if this is our current setting, select it
1930:
1931: if (*(npTable + wCount) == dwCurrentSetting)
1932: SendMessage( hCtrlWnd, CB_SETCURSEL, (WPARAM) wPosition, NULL ) ;
1933: }
1934:
1935: } // end of FillComboBox()
1936:
1937: //---------------------------------------------------------------------------
1938: // BOOL NEAR SettingsDlgInit( HWND hDlg )
1939: //
1940: // Description:
1941: // Puts current settings into dialog box (via CheckRadioButton() etc.)
1942: //
1943: // Parameters:
1944: // HWND hDlg
1945: // handle to dialog box
1946: //
1947: // Win-32 Porting Issues:
1948: // - Constants require DWORD arrays for baud rate table, etc.
1949: // - There is no "MAXCOM" function in Win-32. Number of COM ports
1950: // is assumed to be 4.
1951: //
1952: // History: Date Author Comment
1953: // 5/11/91 BryanW Wrote it.
1954: // 10/20/91 BryanW Dialog revision.
1955: // 10/24/91 BryanW Fixed bug with EscapeCommFunction().
1956: // 6/15/92 BryanW Ported to Win-32.
1957: //
1958: //---------------------------------------------------------------------------
1959:
1960: BOOL NEAR SettingsDlgInit( HWND hDlg )
1961: {
1962: char szBuffer[ MAXLEN_TEMPSTR ], szTemp[ MAXLEN_TEMPSTR ] ;
1963: NPTTYINFO npTTYInfo ;
1964: WORD wCount, wMaxCOM, wPosition ;
1965:
1966: if (NULL == (npTTYInfo = (NPTTYINFO) GET_PROP( hDlg, ATOM_TTYINFO )))
1967: return ( FALSE ) ;
1968:
1969: #ifdef WIN32
1970: wMaxCOM = 4 ;
1971: #else
1972: wMaxCOM = LOWORD( EscapeCommFunction( NULL, GETMAXCOM ) ) + 1 ;
1973: #endif
1974:
1975: // load the COM prefix from resources
1976:
1977: LoadString( GETHINST( hDlg ), IDS_COMPREFIX, szTemp, sizeof( szTemp ) ) ;
1978:
1979: // fill port combo box and make initial selection
1980:
1981: for (wCount = 0; wCount < wMaxCOM; wCount++)
1982: {
1983: wsprintf( szBuffer, "%s%d", (LPSTR) szTemp, wCount + 1 ) ;
1984: SendDlgItemMessage( hDlg, IDD_PORTCB, CB_ADDSTRING, NULL,
1985: (LPARAM) (LPSTR) szBuffer ) ;
1986: }
1987: SendDlgItemMessage( hDlg, IDD_PORTCB, CB_SETCURSEL,
1988: (WPARAM) (PORT( npTTYInfo ) - 1), NULL ) ;
1989:
1990: // disable COM port combo box if connection has already been
1991: // established (e.g. OpenComm() already successful)
1992:
1993: EnableWindow( GetDlgItem( hDlg, IDD_PORTCB ), !CONNECTED( npTTYInfo ) ) ;
1994:
1995: // fill baud combo box and make initial selection
1996:
1997: FillComboBox( GETHINST( hDlg ), GetDlgItem( hDlg, IDD_BAUDCB ),
1998: IDS_BAUD110, BaudTable,
1999: sizeof( BaudTable ) / sizeof( BaudTable[ 0 ] ),
2000: BAUDRATE( npTTYInfo ) ) ;
2001:
2002: // fill data bits combo box and make initial selection
2003:
2004: for (wCount = 5; wCount < 9; wCount++)
2005: {
2006: wsprintf( szBuffer, "%d", wCount ) ;
2007: wPosition = LOWORD( SendDlgItemMessage( hDlg, IDD_DATABITSCB,
2008: CB_ADDSTRING, NULL,
2009: (LPARAM) (LPSTR) szBuffer ) ) ;
2010:
2011: // if current selection, tell the combo box
2012:
2013: if (wCount == BYTESIZE( npTTYInfo ))
2014: SendDlgItemMessage( hDlg, IDD_DATABITSCB, CB_SETCURSEL,
2015: (WPARAM) wPosition, NULL ) ;
2016: }
2017:
2018: // fill parity combo box and make initial selection
2019:
2020: FillComboBox( GETHINST( hDlg ), GetDlgItem( hDlg, IDD_PARITYCB ),
2021: IDS_PARITYNONE, ParityTable,
2022: sizeof( ParityTable ) / sizeof( ParityTable[ 0 ] ),
2023: PARITY( npTTYInfo ) ) ;
2024:
2025: // fill stop bits combo box and make initial selection
2026:
2027: FillComboBox( GETHINST( hDlg ), GetDlgItem( hDlg, IDD_STOPBITSCB ),
2028: IDS_ONESTOPBIT, StopBitsTable,
2029: sizeof( StopBitsTable ) / sizeof ( StopBitsTable ),
2030: STOPBITS( npTTYInfo ) ) ;
2031:
2032: // initalize the flow control settings
2033:
2034: CheckDlgButton( hDlg, IDD_DTRDSR,
2035: (FLOWCTRL( npTTYInfo ) & FC_DTRDSR) > 0 ) ;
2036: CheckDlgButton( hDlg, IDD_RTSCTS,
2037: (FLOWCTRL( npTTYInfo ) & FC_RTSCTS) > 0 ) ;
2038: CheckDlgButton( hDlg, IDD_XONXOFF,
2039: (FLOWCTRL( npTTYInfo ) & FC_XONXOFF) > 0 ) ;
2040:
2041: // other TTY settings
2042:
2043: CheckDlgButton( hDlg, IDD_AUTOWRAP, AUTOWRAP( npTTYInfo ) ) ;
2044: CheckDlgButton( hDlg, IDD_NEWLINE, NEWLINE( npTTYInfo ) ) ;
2045: CheckDlgButton( hDlg, IDD_LOCALECHO, LOCALECHO( npTTYInfo ) ) ;
2046:
2047: // control options
2048:
2049: #ifdef WIN32
2050: // "Use CN_RECEIVE" is not valid under Win-32
2051:
2052: EnableWindow( GetDlgItem( hDlg, IDD_USECNRECEIVE ), FALSE ) ;
2053: #else
2054: CheckDlgButton( hDlg, IDD_USECNRECEIVE, USECNRECEIVE( npTTYInfo ) ) ;
2055:
2056: // disable Use CN_RECEIVE option if connection has already been
2057: // established (e.g. OpenComm() already successful)
2058:
2059: EnableWindow( GetDlgItem( hDlg, IDD_USECNRECEIVE ),
2060: !CONNECTED( npTTYInfo ) ) ;
2061: #endif
2062:
2063: CheckDlgButton( hDlg, IDD_DISPLAYERRORS, DISPLAYERRORS( npTTYInfo ) ) ;
2064:
2065: return ( TRUE ) ;
2066:
2067: } // end of SettingsDlgInit()
2068:
2069: //---------------------------------------------------------------------------
2070: // BOOL NEAR SelectTTYFont( HWND hDlg )
2071: //
2072: // Description:
2073: // Selects the current font for the TTY screen.
2074: // Uses the Common Dialog ChooseFont() API.
2075: //
2076: // Parameters:
2077: // HWND hDlg
2078: // handle to settings dialog
2079: //
2080: // History: Date Author Comment
2081: // 10/20/91 BryanW Wrote it.
2082: //
2083: //---------------------------------------------------------------------------
2084:
2085: BOOL NEAR SelectTTYFont( HWND hDlg )
2086: {
2087: CHOOSEFONT cfTTYFont ;
2088: NPTTYINFO npTTYInfo ;
2089:
2090: if (NULL == (npTTYInfo = (NPTTYINFO) GET_PROP( hDlg, ATOM_TTYINFO )))
2091: return ( FALSE ) ;
2092:
2093: cfTTYFont.lStructSize = sizeof( CHOOSEFONT ) ;
2094: cfTTYFont.hwndOwner = hDlg ;
2095: cfTTYFont.hDC = NULL ;
2096: cfTTYFont.rgbColors = FGCOLOR( npTTYInfo ) ;
2097: cfTTYFont.lpLogFont = &LFTTYFONT( npTTYInfo ) ;
2098: cfTTYFont.Flags = CF_SCREENFONTS | CF_FIXEDPITCHONLY |
2099: CF_EFFECTS | CF_INITTOLOGFONTSTRUCT ;
2100: cfTTYFont.lCustData = NULL ;
2101: cfTTYFont.lpfnHook = NULL ;
2102: cfTTYFont.lpTemplateName = NULL ;
2103: cfTTYFont.hInstance = GETHINST( hDlg ) ;
2104:
2105: if (ChooseFont( &cfTTYFont ))
2106: {
2107: FGCOLOR( npTTYInfo ) = cfTTYFont.rgbColors ;
2108: ResetTTYScreen( GetParent( hDlg ), npTTYInfo ) ;
2109: }
2110:
2111: return ( TRUE ) ;
2112:
2113: } // end of SelectTTYFont()
2114:
2115: //---------------------------------------------------------------------------
2116: // BOOL NEAR SettingsDlgTerm( HWND hDlg )
2117: //
2118: // Description:
2119: // Puts dialog contents into TTY info structure.
2120: //
2121: // Parameters:
2122: // HWND hDlg
2123: // handle to settings dialog
2124: //
2125: // Win-32 Porting Issues:
2126: // - Baud rate requires DWORD values.
2127: //
2128: // History: Date Author Comment
2129: // 5/11/91 BryanW Wrote it.
2130: // 6/15/92 BryanW Ported to Win-32.
2131: //
2132: //---------------------------------------------------------------------------
2133:
2134: BOOL NEAR SettingsDlgTerm( HWND hDlg )
2135: {
2136: NPTTYINFO npTTYInfo ;
2137: WORD wSelection ;
2138:
2139: if (NULL == (npTTYInfo = (NPTTYINFO) GET_PROP( hDlg, ATOM_TTYINFO )))
2140: return ( FALSE ) ;
2141:
2142: // get port selection
2143:
2144: PORT( npTTYInfo ) =
2145: LOBYTE( LOWORD( SendDlgItemMessage( hDlg, IDD_PORTCB,
2146: CB_GETCURSEL,
2147: NULL, NULL ) ) + 1 ) ;
2148: // get baud rate selection
2149:
2150: wSelection =
2151: LOWORD( SendDlgItemMessage( hDlg, IDD_BAUDCB, CB_GETCURSEL,
2152: NULL, NULL ) ) ;
2153: #ifdef WIN32
2154: BAUDRATE( npTTYInfo ) =
2155: SendDlgItemMessage( hDlg, IDD_BAUDCB, CB_GETITEMDATA,
2156: (WPARAM) wSelection, NULL ) ;
2157: #else
2158: BAUDRATE( npTTYInfo ) =
2159: LOWORD( SendDlgItemMessage( hDlg, IDD_BAUDCB, CB_GETITEMDATA,
2160: (WPARAM) wSelection, NULL ) ) ;
2161: #endif
2162:
2163: // get data bits selection
2164:
2165: BYTESIZE( npTTYInfo ) =
2166: LOBYTE( LOWORD( SendDlgItemMessage( hDlg, IDD_DATABITSCB,
2167: CB_GETCURSEL,
2168: NULL, NULL ) ) + 5 ) ;
2169:
2170: // get parity selection
2171:
2172: wSelection =
2173: LOWORD( SendDlgItemMessage( hDlg, IDD_PARITYCB, CB_GETCURSEL,
2174: NULL, NULL ) ) ;
2175: PARITY( npTTYInfo ) =
2176: LOBYTE( LOWORD( SendDlgItemMessage( hDlg, IDD_PARITYCB,
2177: CB_GETITEMDATA,
2178: (WPARAM) wSelection,
2179: NULL ) ) ) ;
2180:
2181: // get stop bits selection
2182:
2183: wSelection =
2184: LOWORD( SendDlgItemMessage( hDlg, IDD_STOPBITSCB, CB_GETCURSEL,
2185: NULL, NULL ) ) ;
2186: STOPBITS( npTTYInfo ) =
2187: LOBYTE( LOWORD( SendDlgItemMessage( hDlg, IDD_STOPBITSCB,
2188: CB_GETITEMDATA,
2189: (WPARAM) wSelection, NULL ) ) ) ;
2190:
2191: // get flow control settings
2192:
2193: FLOWCTRL( npTTYInfo ) = 0 ;
2194: if (IsDlgButtonChecked( hDlg, IDD_DTRDSR ))
2195: FLOWCTRL( npTTYInfo ) |= FC_DTRDSR ;
2196: if (IsDlgButtonChecked( hDlg, IDD_RTSCTS ))
2197: FLOWCTRL( npTTYInfo ) |= FC_RTSCTS ;
2198: if (IsDlgButtonChecked( hDlg, IDD_XONXOFF ))
2199: FLOWCTRL( npTTYInfo ) |= FC_XONXOFF ;
2200:
2201: // get other various settings
2202:
2203: AUTOWRAP( npTTYInfo ) = IsDlgButtonChecked( hDlg, IDD_AUTOWRAP ) ;
2204: NEWLINE( npTTYInfo ) = IsDlgButtonChecked( hDlg, IDD_NEWLINE ) ;
2205: LOCALECHO( npTTYInfo ) = IsDlgButtonChecked( hDlg, IDD_LOCALECHO ) ;
2206:
2207: // control options
2208:
2209: USECNRECEIVE( npTTYInfo ) = IsDlgButtonChecked( hDlg, IDD_USECNRECEIVE ) ;
2210: DISPLAYERRORS( npTTYInfo ) = IsDlgButtonChecked( hDlg, IDD_DISPLAYERRORS ) ;
2211:
2212: return ( TRUE ) ;
2213:
2214: } // end of SettingsDlgTerm()
2215:
2216: //---------------------------------------------------------------------------
2217: // BOOL FAR PASCAL SettingsDlgProc( HWND hDlg, UINT uMsg,
2218: // WPARAM wParam, LPARAM lParam )
2219: //
2220: // Description:
2221: // This handles all of the user preference settings for
2222: // the TTY.
2223: //
2224: // Parameters:
2225: // same as all dialog procedures
2226: //
2227: // Win-32 Porting Issues:
2228: // - npTTYInfo is a DWORD in Win-32.
2229: //
2230: // History: Date Author Comment
2231: // 5/10/91 BryanW Wrote it.
2232: // 10/20/91 BryanW Now uses window properties to
2233: // store TTYInfo handle. Also added
2234: // font selection.
2235: // 6/15/92 BryanW Ported to Win-32.
2236: //
2237: //---------------------------------------------------------------------------
2238:
2239: BOOL FAR PASCAL SettingsDlgProc( HWND hDlg, UINT uMsg,
2240: WPARAM wParam, LPARAM lParam )
2241: {
2242: switch (uMsg)
2243: {
2244: case WM_INITDIALOG:
2245: {
2246: NPTTYINFO npTTYInfo ;
2247:
2248: // get & save pointer to TTY info structure
2249:
2250: #ifdef WIN32
2251: npTTYInfo = (NPTTYINFO) lParam ;
2252: #else
2253: npTTYInfo = (NPTTYINFO) LOWORD( lParam ) ;
2254: #endif
2255:
2256: SET_PROP( hDlg, ATOM_TTYINFO, (HANDLE) npTTYInfo ) ;
2257:
2258: return ( SettingsDlgInit( hDlg ) ) ;
2259: }
2260:
2261: case WM_COMMAND:
2262: switch ( LOWORD( wParam ))
2263: {
2264: case IDD_FONT:
2265: return ( SelectTTYFont( hDlg ) ) ;
2266:
2267: case IDD_OK:
2268: // Copy stuff into structure
2269: SettingsDlgTerm( hDlg ) ;
2270: EndDialog( hDlg, TRUE ) ;
2271: return ( TRUE ) ;
2272:
2273: case IDD_CANCEL:
2274: // Just end
2275: EndDialog( hDlg, TRUE ) ;
2276: return ( TRUE ) ;
2277: }
2278: break;
2279:
2280: case WM_DESTROY:
2281: REMOVE_PROP( hDlg, ATOM_TTYINFO ) ;
2282: break ;
2283: }
2284: return ( FALSE ) ;
2285:
2286: } // end of SettingsDlgProc()
2287:
2288: #ifdef WIN32
2289:
2290: //************************************************************************
2291: // DWORD FAR PASCAL CommWatchProc( LPSTR lpData )
2292: //
2293: // Description:
2294: // A secondary thread that will watch for COMM events.
2295: //
2296: // Parameters:
2297: // LPSTR lpData
2298: // 32-bit pointer argument
2299: //
2300: // Win-32 Porting Issues:
2301: // - Added this thread to watch the communications device and
2302: // post notifications to the associated window.
2303: //
2304: // History: Date Author Comment
2305: // 12/31/91 BryanW Wrote it.
2306: // 6/12/92 BryanW CommWaitEvent() now uses
2307: // an overlapped structure.
2308: //
2309: //************************************************************************
2310:
2311: DWORD FAR PASCAL CommWatchProc( LPSTR lpData )
2312: {
1.1.1.2 ! root 2313: DWORD dwTransfer, dwEvtMask ;
1.1 root 2314: NPTTYINFO npTTYInfo = (NPTTYINFO) lpData ;
2315: OVERLAPPED os ;
2316:
2317: memset( &os, 0, sizeof( OVERLAPPED ) ) ;
2318:
2319: // create I/O event used for overlapped read
2320:
2321: os.hEvent = CreateEvent( NULL, // no security
2322: TRUE, // explicit reset req
2323: FALSE, // initial event reset
2324: NULL ) ; // no name
2325: if (os.hEvent == NULL)
2326: {
2327: MessageBox( NULL, "Failed to create event for thread!", "TTY Error!",
2328: MB_ICONEXCLAMATION | MB_OK ) ;
2329: return ( FALSE ) ;
2330: }
2331:
2332: if (!SetCommMask( COMDEV( npTTYInfo ), EV_RXCHAR ))
2333: return ( FALSE ) ;
2334:
2335: while ( CONNECTED( npTTYInfo ) )
2336: {
2337: dwEvtMask = NULL ;
2338:
2339: if (!WaitCommEvent( COMDEV( npTTYInfo ), &dwEvtMask, &os ))
2340: {
2341: if (ERROR_IO_PENDING == GetLastError())
2342: {
1.1.1.2 ! root 2343: GetOverlappedResult( COMDEV( npTTYInfo ), &os, &dwTransfer, TRUE ) ;
! 2344: os.Offset += dwTransfer ;
1.1 root 2345: }
2346: }
2347:
2348: if ((dwEvtMask & EV_RXCHAR) == EV_RXCHAR)
2349: {
2350: // wait for "posted notification" flag to clear
2351:
2352: WaitForSingleObject( POSTEVENT( npTTYInfo ), 0xFFFFFFFF ) ;
2353:
2354: // reset event
2355:
2356: ResetEvent( POSTEVENT( npTTYInfo ) ) ;
2357:
2358: // last message was processed, O.K. to post
2359:
2360: PostMessage( TERMWND( npTTYInfo ), WM_COMMNOTIFY,
2361: (WPARAM) COMDEV( npTTYInfo ),
2362: MAKELONG( CN_EVENT, 0 ) ) ;
2363: }
2364: }
2365:
2366: // get rid of event handle
2367:
2368: CloseHandle( os.hEvent ) ;
2369:
2370: // clear information in structure (kind of a "we're done flag")
2371:
2372: THREADID( npTTYInfo ) = NULL ;
2373: HTHREAD( npTTYInfo ) = NULL ;
2374:
2375: return( TRUE ) ;
2376:
2377: } // end of CommWatchProc()
2378:
2379: #endif
2380:
2381: //---------------------------------------------------------------------------
2382: // End of File: tty.c
2383: //---------------------------------------------------------------------------
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.