2 * This DLL contains the user interface for the serial driver.
3 * a dialog box to configure the specified COMM port
4 * an interface to the control panel (??)
5 * functions to load and save default configuration
7 * Eventually the 32 bit comm port driver could be moved into here
8 * and interfaced to KERNEL32 using the WIN95 or WINNT comm driver interface.
9 * This way, different driver DLLS could be written to support other
10 * serial interfaces, such as X.25, etc.
12 * Basic structure copied from COMCTL32 code.
14 * Copyright 2000, 2004 Mike McCormack
16 * This library is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU Lesser General Public
18 * License as published by the Free Software Foundation; either
19 * version 2.1 of the License, or (at your option) any later version.
21 * This library is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * Lesser General Public License for more details.
26 * You should have received a copy of the GNU Lesser General Public
27 * License along with this library; if not, write to the Free Software
28 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
32 #include "wine/port.h"
33 #include "wine/unicode.h"
44 #include "wine/debug.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(comm
);
50 static HMODULE SERIALUI_hModule
;
52 static const WCHAR comW
[] = {'c','o','m',0 };
54 /***********************************************************************
55 * DllMain [Internal] Initializes the internal 'SERIALUI.DLL'.
58 * hinstDLL [I] handle to the DLL's instance
60 * lpvReserved [I] reserved, must be NULL
67 BOOL WINAPI
DllMain (HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
69 TRACE("%p,%x,%p\n", hinstDLL
, fdwReason
, lpvReserved
);
72 case DLL_PROCESS_ATTACH
:
73 DisableThreadLibraryCalls(hinstDLL
);
74 SERIALUI_hModule
= hinstDLL
;
82 /***********************************************************************
83 * EnumPropPages (SERIALUI.2)
85 * Called by the device manager to add prop sheets in Control Panel ???
86 * Pointed to in Win98 registry by
87 * \System\CurrentControlSet\Services\Class\ports\0000\EnumPropPages =
88 * "serialui.dll,EnumPropPages"
90 typedef LPVOID LPDEVICE_INFO
;
91 typedef LPVOID LPFNADDPROPSHEETPAGE
;
92 BOOL WINAPI
EnumPropPages(LPDEVICE_INFO pdi
, LPFNADDPROPSHEETPAGE pfnAdd
, LPARAM lParam
)
94 FIXME("(%p %p %lx)\n",pdi
,pfnAdd
,lParam
);
99 * These data structures are convert from values used in fields of a DCB
100 * to strings used in the CommConfigDialog.
102 typedef struct tagPARAM2STRDATA
106 } PARAM2STRDATA
, *LPPARAM2STRDATA
;
108 typedef struct tagPARAM2STR
111 LPPARAM2STRDATA data
;
112 } PARAM2STR
, *LPPARAM2STR
;
113 typedef const PARAM2STR
*LPCPARAM2STR
;
115 static PARAM2STRDATA SERIALUI_Baud2StrData
[]={
116 {110, "110"}, {300, "300"}, {600, "600"}, {1200, "1200"},
117 {2400, "2400"}, {4800, "4800"}, {9600, "9600"}, {14400, "14400"},
118 {19200, "19200"}, {38400L, "38400"}, {56000L, "56000"}, {57600L, "57600"},
119 {115200L, "115200"}, {128000L, "128000"}, {256000L, "256000"}
121 static PARAM2STR SERIALUI_Baud2Str
={ ARRAY_SIZE(SERIALUI_Baud2StrData
), SERIALUI_Baud2StrData
};
123 static PARAM2STRDATA SERIALUI_Parity2StrData
[]={
124 {NOPARITY
,"None"}, {ODDPARITY
,"Odd"}, {EVENPARITY
,"Even"}, {MARKPARITY
,"Mark"},
125 {SPACEPARITY
,"Space"}
127 static PARAM2STR SERIALUI_Parity2Str
={ ARRAY_SIZE(SERIALUI_Parity2StrData
), SERIALUI_Parity2StrData
};
129 static PARAM2STRDATA SERIALUI_Stop2StrData
[]={
130 {ONESTOPBIT
,"1"}, {ONE5STOPBITS
,"1.5"}, {TWOSTOPBITS
,"2"}
132 static PARAM2STR SERIALUI_Stop2Str
={ ARRAY_SIZE(SERIALUI_Stop2StrData
), SERIALUI_Stop2StrData
};
134 static PARAM2STRDATA SERIALUI_Data2StrData
[]={
135 {5,"5"}, {6,"6"}, {7,"7"}, {8, "8"}, {16,"16"}
137 static PARAM2STR SERIALUI_Data2Str
={ ARRAY_SIZE(SERIALUI_Data2StrData
), SERIALUI_Data2StrData
};
139 static PARAM2STRDATA SERIALUI_Flow2StrData
[]={
140 {0,"None"}, {1,"Hardware (RTS/CTS)"}, {2,"Software (XON/XOFF)"}
142 static PARAM2STR SERIALUI_Flow2Str
={ ARRAY_SIZE(SERIALUI_Flow2StrData
), SERIALUI_Flow2StrData
};
145 * Add all the fields to a combo box and highlight the current value
147 static void SERIALUI_AddConfItems(HWND hDlg
, DWORD id
, LPCPARAM2STR table
, DWORD dwVal
)
151 HWND hControl
= GetDlgItem(hDlg
,id
);
156 for(i
=0; i
<table
->dwSize
; i
++)
158 n
= SendMessageA(hControl
, CB_ADDSTRING
, 0L, (LPARAM
)table
->data
[i
].name
);
159 if(dwVal
== table
->data
[i
].val
)
161 SendMessageA(hControl
, CB_SETCURSEL
, n
, 0);
167 * Get the current selection of the given combo box and set a DCB field to
168 * the value matching that selection.
170 static BOOL
SERIALUI_GetConfItems(HWND hDlg
, DWORD id
, LPCPARAM2STR table
, LPDWORD lpdwVal
)
174 HWND hControl
= GetDlgItem(hDlg
,id
);
176 if( (!hControl
) || (!lpdwVal
))
178 TRACE("Couldn't get window handle for item %x\n",id
);
182 if(!GetWindowTextA(hControl
, &lpEntry
[0], sizeof(lpEntry
)))
184 TRACE("Couldn't get window text for item %x\n",id
);
187 /* TRACE("%ld contains %s\n",id, lpEntry); */
189 for(i
=0; i
<table
->dwSize
; i
++)
191 if(!lstrcmpA(table
->data
[i
].name
,lpEntry
))
193 *lpdwVal
= table
->data
[i
].val
;
202 * Both the enumerated values CBR_XXXX and integer baud rates are valid
203 * dcb.BaudRate. This code is to convert back and forth between CBR_ style
204 * and integers. The dialog box uses integer values.
206 static const DWORD SERIALUI_BaudConvertTable
[] = {
207 CBR_110
, 110, CBR_300
, 300, CBR_600
, 600, CBR_1200
, 1200,
208 CBR_2400
, 2400, CBR_4800
, 4800, CBR_9600
, 9600, CBR_14400
, 14400,
209 CBR_19200
, 19200, CBR_38400
, 38400, CBR_56000
, 56000, CBR_57600
, 57600,
210 CBR_115200
, 115200, CBR_128000
, 128000, CBR_256000
, 256000
213 static BOOL
SERIALUI_MakeBaudDword(LPDWORD lpdwBaudRate
)
217 for(i
=0; i
<ARRAY_SIZE(SERIALUI_BaudConvertTable
); i
+=2)
219 if(*lpdwBaudRate
== SERIALUI_BaudConvertTable
[i
])
221 *lpdwBaudRate
= SERIALUI_BaudConvertTable
[i
+1];
228 static BOOL
SERIALUI_MakeBaudEnum(LPDWORD lpdwBaudRate
)
232 for(i
=0; i
<ARRAY_SIZE(SERIALUI_BaudConvertTable
); i
+=2)
234 if(*lpdwBaudRate
== SERIALUI_BaudConvertTable
[i
+1])
236 *lpdwBaudRate
= SERIALUI_BaudConvertTable
[i
];
243 typedef struct tagSERIALUI_DialogInfo
246 LPCOMMCONFIG lpCommConfig
;
247 BOOL bConvert
; /* baud rate was converted to a DWORD */
248 DWORD dwFlowControl
; /* old flow control */
249 } SERIALUI_DialogInfo
;
251 static void SERIALUI_DCBToDialogInfo(HWND hDlg
, SERIALUI_DialogInfo
*info
)
253 DWORD dwBaudRate
, dwStopBits
, dwParity
, dwByteSize
, dwFlowControl
;
254 LPDCB lpdcb
= &info
->lpCommConfig
->dcb
;
256 /* pass integer pointers to SERIALUI_ dialog config fns */
257 dwBaudRate
= lpdcb
->BaudRate
;
258 dwStopBits
= lpdcb
->StopBits
;
259 dwParity
= lpdcb
->Parity
;
260 dwByteSize
= lpdcb
->ByteSize
;
262 /* map flow control state, if it looks normal */
263 if((lpdcb
->fRtsControl
== RTS_CONTROL_HANDSHAKE
) ||
264 (lpdcb
->fOutxCtsFlow
)) {
266 } else if(lpdcb
->fOutX
|| lpdcb
->fInX
) {
272 info
->bConvert
= SERIALUI_MakeBaudDword(&dwBaudRate
);
274 SERIALUI_AddConfItems( hDlg
, IDC_BAUD
, &SERIALUI_Baud2Str
,dwBaudRate
);
275 SERIALUI_AddConfItems( hDlg
, IDC_STOP
, &SERIALUI_Stop2Str
,dwStopBits
);
276 SERIALUI_AddConfItems( hDlg
, IDC_PARITY
, &SERIALUI_Parity2Str
,dwParity
);
277 SERIALUI_AddConfItems( hDlg
, IDC_DATA
, &SERIALUI_Data2Str
,dwByteSize
);
278 SERIALUI_AddConfItems( hDlg
, IDC_FLOW
, &SERIALUI_Flow2Str
, dwFlowControl
);
280 info
->dwFlowControl
= dwFlowControl
;
283 static void SERIALUI_DialogInfoToDCB(HWND hDlg
, SERIALUI_DialogInfo
*info
)
285 DWORD dwBaudRate
, dwStopBits
, dwParity
, dwByteSize
, dwFlowControl
;
286 LPDCB lpdcb
= &info
->lpCommConfig
->dcb
;
288 SERIALUI_GetConfItems( hDlg
, IDC_BAUD
, &SERIALUI_Baud2Str
, &dwBaudRate
);
289 SERIALUI_GetConfItems( hDlg
, IDC_STOP
, &SERIALUI_Stop2Str
, &dwStopBits
);
290 SERIALUI_GetConfItems( hDlg
, IDC_PARITY
, &SERIALUI_Parity2Str
, &dwParity
);
291 SERIALUI_GetConfItems( hDlg
, IDC_DATA
, &SERIALUI_Data2Str
, &dwByteSize
);
292 SERIALUI_GetConfItems( hDlg
, IDC_FLOW
, &SERIALUI_Flow2Str
, &dwFlowControl
);
294 TRACE("baud=%d stop=%d parity=%d data=%d flow=%d\n",
295 dwBaudRate
, dwStopBits
, dwParity
, dwByteSize
, dwFlowControl
);
297 lpdcb
->BaudRate
= dwBaudRate
;
298 lpdcb
->StopBits
= dwStopBits
;
299 lpdcb
->Parity
= dwParity
;
300 lpdcb
->ByteSize
= dwByteSize
;
302 /* try not to change flow control if the user didn't change it */
303 if(info
->dwFlowControl
!= dwFlowControl
)
305 switch(dwFlowControl
)
308 lpdcb
->fOutxCtsFlow
= FALSE
;
309 lpdcb
->fOutxDsrFlow
= FALSE
;
310 lpdcb
->fDtrControl
= DTR_CONTROL_DISABLE
;
311 lpdcb
->fOutX
= FALSE
;
313 lpdcb
->fRtsControl
= RTS_CONTROL_DISABLE
;
315 case 1: /* CTS/RTS */
316 lpdcb
->fOutxCtsFlow
= TRUE
;
317 lpdcb
->fOutxDsrFlow
= FALSE
;
318 lpdcb
->fDtrControl
= DTR_CONTROL_DISABLE
;
319 lpdcb
->fOutX
= FALSE
;
321 lpdcb
->fRtsControl
= RTS_CONTROL_HANDSHAKE
;
324 lpdcb
->fOutxCtsFlow
= FALSE
;
325 lpdcb
->fOutxDsrFlow
= FALSE
;
326 lpdcb
->fDtrControl
= DTR_CONTROL_DISABLE
;
329 lpdcb
->fRtsControl
= RTS_CONTROL_DISABLE
;
335 SERIALUI_MakeBaudEnum(&lpdcb
->BaudRate
);
338 /***********************************************************************
339 * SERIALUI_ConfigDialogProc
341 * Shows a dialog for configuring a COMM port
343 static INT_PTR CALLBACK
SERIALUI_ConfigDialogProc(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
345 WCHAR szTitle
[128], format
[40];
346 SERIALUI_DialogInfo
*info
;
351 info
= (SERIALUI_DialogInfo
*) lParam
;
354 SetWindowLongPtrW(hWnd
, DWLP_USER
, lParam
);
355 GetWindowTextW(hWnd
, format
, ARRAY_SIZE(format
));
356 snprintfW(szTitle
, ARRAY_SIZE(szTitle
), format
, info
->lpszDevice
);
357 SetWindowTextW(hWnd
, szTitle
);
358 SERIALUI_DCBToDialogInfo(hWnd
, info
);
363 WORD wID
= LOWORD(wParam
);
365 info
= (SERIALUI_DialogInfo
*) GetWindowLongPtrW(hWnd
, DWLP_USER
);
371 SERIALUI_DialogInfoToDCB(hWnd
,info
);
372 EndDialog(hWnd
, ERROR_SUCCESS
);
375 EndDialog(hWnd
, ERROR_CANCELLED
);
377 /* test code for Get/SetDefaultCommConfig begins */
380 DWORD r
,dwConfSize
= sizeof (COMMCONFIG
);
381 r
= GetDefaultCommConfigW(info
->lpszDevice
,
382 info
->lpCommConfig
, &dwConfSize
);
384 MessageBoxA(hWnd
,"Failed","GetDefaultCommConfig",MB_OK
);
386 SERIALUI_DCBToDialogInfo(hWnd
, info
);
391 SERIALUI_DialogInfoToDCB(hWnd
,info
);
392 r
= SetDefaultCommConfigW(info
->lpszDevice
,
393 info
->lpCommConfig
, sizeof (COMMCONFIG
));
395 MessageBoxA(hWnd
,"Failed","GetDefaultCommConfig",MB_OK
);
398 /* test code for Get/SetDefaultCommConfig ends */
406 static LPWSTR
SERIALUI_strdup( LPCSTR str
)
413 len
= MultiByteToWideChar( CP_ACP
, 0, str
, -1, NULL
, 0 );
414 strW
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
) );
415 MultiByteToWideChar( CP_ACP
, 0, str
, -1, strW
, len
);
419 static VOID
SERIALUI_strfree( LPWSTR strW
)
421 HeapFree( GetProcessHeap(), 0, strW
);
424 /***********************************************************************
425 * drvCommConfigDialogW (SERIALUI.@)
427 * Show a dialog for configuring a Serial Port.
430 DWORD WINAPI
drvCommConfigDialogW(LPCWSTR lpszName
, HWND hWndParent
, LPCOMMCONFIG lpCommConfig
)
432 SERIALUI_DialogInfo info
;
435 info
.lpCommConfig
= lpCommConfig
;
436 info
.lpszDevice
= lpszName
;
437 info
.bConvert
= FALSE
;
438 info
.dwFlowControl
= 0;
440 if ((!lpCommConfig
) || (!lpszName
))
441 return ERROR_INVALID_PARAMETER
;
443 if (lpCommConfig
->dwSize
< sizeof(COMMCONFIG
))
444 return ERROR_INSUFFICIENT_BUFFER
;
449 res
= DialogBoxParamW( SERIALUI_hModule
,
450 MAKEINTRESOURCEW(IDD_SERIALUICONFIG
),
452 SERIALUI_ConfigDialogProc
,
455 return (res
== -1) ? GetLastError() : res
;
458 /***********************************************************************
459 * drvCommConfigDialogA (SERIALUI.@)
461 DWORD WINAPI
drvCommConfigDialogA(LPCSTR lpszName
, HWND hWndParent
, LPCOMMCONFIG lpCommConfig
)
463 LPWSTR strW
= SERIALUI_strdup( lpszName
);
464 DWORD r
= drvCommConfigDialogW( strW
, hWndParent
, lpCommConfig
);
465 SERIALUI_strfree( strW
);
469 static const WCHAR lpszCommKey
[] = {
470 'S','y','s','t','e','m','\\',
471 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
472 'S','e','r','v','i','c','e','s','\\',
473 'C','l','a','s','s','\\','P','o','r','t','s',0
475 static const WCHAR lpszDCB
[] = {'D','C','B',0};
477 /***********************************************************************
478 * drvSetDefaultCommConfigW (SERIALUI.@)
480 * Used by Win98 KERNEL to set the default config for a COMM port
481 * FIXME: uses the wrong registry key... should use a digit, not
482 * the comm port name.
484 BOOL WINAPI
drvSetDefaultCommConfigW(
485 LPCWSTR lpszDevice
, LPCOMMCONFIG lpCommConfig
, DWORD dwSize
)
487 HKEY hKeyReg
=0, hKeyPort
=0;
488 WCHAR szKeyName
[100];
490 static const WCHAR fmt
[] = {'%','s','\\','%','s',0 };
492 TRACE("%p %p %x\n",lpszDevice
,lpCommConfig
,dwSize
);
497 if(dwSize
< sizeof (COMMCONFIG
))
500 r
= RegConnectRegistryW(NULL
, HKEY_LOCAL_MACHINE
, &hKeyReg
);
501 if(r
!= ERROR_SUCCESS
)
504 snprintfW(szKeyName
, ARRAY_SIZE(szKeyName
), fmt
, lpszCommKey
, lpszDevice
);
505 r
= RegCreateKeyW(hKeyReg
, szKeyName
, &hKeyPort
);
506 if(r
== ERROR_SUCCESS
)
508 dwDCBSize
= sizeof (DCB
);
509 r
= RegSetValueExW( hKeyPort
, lpszDCB
, 0, REG_BINARY
,
510 (LPBYTE
)&lpCommConfig
->dcb
,dwDCBSize
);
511 TRACE("write key r=%d\n",r
);
512 RegCloseKey(hKeyPort
);
515 RegCloseKey(hKeyReg
);
517 return (r
==ERROR_SUCCESS
);
520 /***********************************************************************
521 * drvSetDefaultCommConfigA (SERIALUI.@)
523 BOOL WINAPI
drvSetDefaultCommConfigA(
524 LPCSTR lpszDevice
, LPCOMMCONFIG lpCommConfig
, DWORD dwSize
)
526 LPWSTR strW
= SERIALUI_strdup( lpszDevice
);
527 BOOL r
= drvSetDefaultCommConfigW( strW
, lpCommConfig
, dwSize
);
528 SERIALUI_strfree( strW
);
532 /***********************************************************************
533 * drvGetDefaultCommConfigW (SERIALUI.@)
535 * Used by Win9x KERNEL to get the default config for a COMM port
536 * FIXME: uses the wrong registry key... should use a digit, not
537 * the comm port name.
539 DWORD WINAPI
drvGetDefaultCommConfigW(
540 LPCWSTR lpszDevice
, LPCOMMCONFIG lpCommConfig
, LPDWORD lpdwSize
)
542 HKEY hKeyReg
, hKeyPort
;
543 WCHAR szKeyName
[100];
544 DWORD r
,dwSize
,dwType
;
545 static const WCHAR fmt
[] = {'%','s','\\','%','s',0 };
547 TRACE("(%s, %p, %p) *lpdwSize: %u\n", debugstr_w(lpszDevice
), lpCommConfig
, lpdwSize
, lpdwSize
? *lpdwSize
: 0);
549 if ((!lpszDevice
) || (!lpCommConfig
) || (!lpdwSize
)) {
550 return ERROR_INVALID_PARAMETER
;
553 if (*lpdwSize
< sizeof (COMMCONFIG
)) {
554 *lpdwSize
= sizeof (COMMCONFIG
);
555 return ERROR_INSUFFICIENT_BUFFER
;
558 /* only "com1" - "com9" is allowed */
559 r
= ARRAY_SIZE(comW
); /* len of "com\0" */
560 lstrcpynW(szKeyName
, lpszDevice
, r
); /* simulate a lstrcmpnW */
563 if( lstrcmpiW(szKeyName
, comW
) ||
564 (lpszDevice
[r
] < '1') || (lpszDevice
[r
] > '9') || lpszDevice
[r
+1]) {
568 *lpdwSize
= sizeof (COMMCONFIG
);
569 memset(lpCommConfig
, 0 , sizeof (COMMCONFIG
));
570 lpCommConfig
->dwSize
= sizeof (COMMCONFIG
);
571 lpCommConfig
->wVersion
= 1;
572 lpCommConfig
->dwProviderSubType
= PST_RS232
;
574 r
= RegConnectRegistryW(NULL
, HKEY_LOCAL_MACHINE
, &hKeyReg
);
575 if(r
!= ERROR_SUCCESS
) return r
;
577 snprintfW(szKeyName
, ARRAY_SIZE(szKeyName
), fmt
, lpszCommKey
, lpszDevice
);
578 r
= RegOpenKeyW(hKeyReg
, szKeyName
, &hKeyPort
);
579 if(r
== ERROR_SUCCESS
)
581 dwSize
= sizeof (DCB
);
583 r
= RegQueryValueExW( hKeyPort
, lpszDCB
, NULL
,
584 &dwType
, (LPBYTE
)&lpCommConfig
->dcb
, &dwSize
);
586 RegCloseKey(hKeyPort
);
587 if ((r
!=ERROR_SUCCESS
) || (dwType
!= REG_BINARY
) || (dwSize
!= sizeof(DCB
))) {
588 RegCloseKey(hKeyReg
);
589 return ERROR_INVALID_PARAMETER
;
595 /* FIXME: default to a hardcoded commconfig */
596 lpCommConfig
->dcb
.DCBlength
= sizeof(DCB
);
597 lpCommConfig
->dcb
.BaudRate
= 9600;
598 lpCommConfig
->dcb
.fBinary
= TRUE
;
599 lpCommConfig
->dcb
.fParity
= FALSE
;
600 lpCommConfig
->dcb
.ByteSize
= 8;
601 lpCommConfig
->dcb
.Parity
= NOPARITY
;
602 lpCommConfig
->dcb
.StopBits
= ONESTOPBIT
;
603 return ERROR_SUCCESS
;
606 RegCloseKey(hKeyReg
);
611 /***********************************************************************
612 * drvGetDefaultCommConfigA (SERIALUI.@)
614 DWORD WINAPI
drvGetDefaultCommConfigA(
615 LPCSTR lpszDevice
, LPCOMMCONFIG lpCommConfig
, LPDWORD lpdwSize
)
617 LPWSTR strW
= SERIALUI_strdup( lpszDevice
);
618 DWORD r
= drvGetDefaultCommConfigW( strW
, lpCommConfig
, lpdwSize
);
619 SERIALUI_strfree( strW
);