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
41 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(comm
);
47 static HMODULE SERIALUI_hModule
;
49 /***********************************************************************
50 * DllMain [Internal] Initializes the internal 'SERIALUI.DLL'.
53 * hinstDLL [I] handle to the DLL's instance
55 * lpvReserved [I] reserved, must be NULL
62 BOOL WINAPI
DllMain (HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
64 TRACE("%p,%lx,%p\n", hinstDLL
, fdwReason
, lpvReserved
);
67 case DLL_PROCESS_ATTACH
:
68 DisableThreadLibraryCalls(hinstDLL
);
69 SERIALUI_hModule
= hinstDLL
;
77 /***********************************************************************
78 * EnumPropPages (SERIALUI.2)
80 * Called by the device manager to add prop sheets in Control Panel ???
81 * Pointed to in Win98 registry by
82 * \System\CurrentControlSet\Services\Class\ports\0000\EnumPropPages =
83 * "serialui.dll,EnumPropPages"
85 typedef LPVOID LPDEVICE_INFO
;
86 typedef LPVOID LPFNADDPROPSHEETPAGE
;
87 BOOL WINAPI
EnumPropPages(LPDEVICE_INFO pdi
, LPFNADDPROPSHEETPAGE pfnAdd
, LPARAM lParam
)
89 FIXME("(%p %p %Ix)\n",pdi
,pfnAdd
,lParam
);
94 * These data structures are convert from values used in fields of a DCB
95 * to strings used in the CommConfigDialog.
97 typedef struct tagPARAM2STRDATA
101 } PARAM2STRDATA
, *LPPARAM2STRDATA
;
103 typedef struct tagPARAM2STR
106 LPPARAM2STRDATA data
;
107 } PARAM2STR
, *LPPARAM2STR
;
108 typedef const PARAM2STR
*LPCPARAM2STR
;
110 static PARAM2STRDATA SERIALUI_Baud2StrData
[]={
111 {110, "110"}, {300, "300"}, {600, "600"}, {1200, "1200"},
112 {2400, "2400"}, {4800, "4800"}, {9600, "9600"}, {14400, "14400"},
113 {19200, "19200"}, {38400L, "38400"}, {56000L, "56000"}, {57600L, "57600"},
114 {115200L, "115200"}, {128000L, "128000"}, {256000L, "256000"}
116 static PARAM2STR SERIALUI_Baud2Str
={ ARRAY_SIZE(SERIALUI_Baud2StrData
), SERIALUI_Baud2StrData
};
118 static PARAM2STRDATA SERIALUI_Parity2StrData
[]={
119 {NOPARITY
,"None"}, {ODDPARITY
,"Odd"}, {EVENPARITY
,"Even"}, {MARKPARITY
,"Mark"},
120 {SPACEPARITY
,"Space"}
122 static PARAM2STR SERIALUI_Parity2Str
={ ARRAY_SIZE(SERIALUI_Parity2StrData
), SERIALUI_Parity2StrData
};
124 static PARAM2STRDATA SERIALUI_Stop2StrData
[]={
125 {ONESTOPBIT
,"1"}, {ONE5STOPBITS
,"1.5"}, {TWOSTOPBITS
,"2"}
127 static PARAM2STR SERIALUI_Stop2Str
={ ARRAY_SIZE(SERIALUI_Stop2StrData
), SERIALUI_Stop2StrData
};
129 static PARAM2STRDATA SERIALUI_Data2StrData
[]={
130 {5,"5"}, {6,"6"}, {7,"7"}, {8, "8"}, {16,"16"}
132 static PARAM2STR SERIALUI_Data2Str
={ ARRAY_SIZE(SERIALUI_Data2StrData
), SERIALUI_Data2StrData
};
134 static PARAM2STRDATA SERIALUI_Flow2StrData
[]={
135 {0,"None"}, {1,"Hardware (RTS/CTS)"}, {2,"Software (XON/XOFF)"}
137 static PARAM2STR SERIALUI_Flow2Str
={ ARRAY_SIZE(SERIALUI_Flow2StrData
), SERIALUI_Flow2StrData
};
140 * Add all the fields to a combo box and highlight the current value
142 static void SERIALUI_AddConfItems(HWND hDlg
, DWORD id
, LPCPARAM2STR table
, DWORD dwVal
)
146 HWND hControl
= GetDlgItem(hDlg
,id
);
151 for(i
=0; i
<table
->dwSize
; i
++)
153 n
= SendMessageA(hControl
, CB_ADDSTRING
, 0L, (LPARAM
)table
->data
[i
].name
);
154 if(dwVal
== table
->data
[i
].val
)
156 SendMessageA(hControl
, CB_SETCURSEL
, n
, 0);
162 * Get the current selection of the given combo box and set a DCB field to
163 * the value matching that selection.
165 static BOOL
SERIALUI_GetConfItems(HWND hDlg
, DWORD id
, LPCPARAM2STR table
, LPDWORD lpdwVal
)
169 HWND hControl
= GetDlgItem(hDlg
,id
);
171 if( (!hControl
) || (!lpdwVal
))
173 TRACE("Couldn't get window handle for item %lx\n",id
);
177 if(!GetWindowTextA(hControl
, &lpEntry
[0], sizeof(lpEntry
)))
179 TRACE("Couldn't get window text for item %lx\n",id
);
182 /* TRACE("%ld contains %s\n",id, lpEntry); */
184 for(i
=0; i
<table
->dwSize
; i
++)
186 if(!lstrcmpA(table
->data
[i
].name
,lpEntry
))
188 *lpdwVal
= table
->data
[i
].val
;
197 * Both the enumerated values CBR_XXXX and integer baud rates are valid
198 * dcb.BaudRate. This code is to convert back and forth between CBR_ style
199 * and integers. The dialog box uses integer values.
201 static const DWORD SERIALUI_BaudConvertTable
[] = {
202 CBR_110
, 110, CBR_300
, 300, CBR_600
, 600, CBR_1200
, 1200,
203 CBR_2400
, 2400, CBR_4800
, 4800, CBR_9600
, 9600, CBR_14400
, 14400,
204 CBR_19200
, 19200, CBR_38400
, 38400, CBR_56000
, 56000, CBR_57600
, 57600,
205 CBR_115200
, 115200, CBR_128000
, 128000, CBR_256000
, 256000
208 static BOOL
SERIALUI_MakeBaudDword(LPDWORD lpdwBaudRate
)
212 for(i
=0; i
<ARRAY_SIZE(SERIALUI_BaudConvertTable
); i
+=2)
214 if(*lpdwBaudRate
== SERIALUI_BaudConvertTable
[i
])
216 *lpdwBaudRate
= SERIALUI_BaudConvertTable
[i
+1];
223 static BOOL
SERIALUI_MakeBaudEnum(LPDWORD lpdwBaudRate
)
227 for(i
=0; i
<ARRAY_SIZE(SERIALUI_BaudConvertTable
); i
+=2)
229 if(*lpdwBaudRate
== SERIALUI_BaudConvertTable
[i
+1])
231 *lpdwBaudRate
= SERIALUI_BaudConvertTable
[i
];
238 typedef struct tagSERIALUI_DialogInfo
241 LPCOMMCONFIG lpCommConfig
;
242 BOOL bConvert
; /* baud rate was converted to a DWORD */
243 DWORD dwFlowControl
; /* old flow control */
244 } SERIALUI_DialogInfo
;
246 static void SERIALUI_DCBToDialogInfo(HWND hDlg
, SERIALUI_DialogInfo
*info
)
248 DWORD dwBaudRate
, dwStopBits
, dwParity
, dwByteSize
, dwFlowControl
;
249 LPDCB lpdcb
= &info
->lpCommConfig
->dcb
;
251 /* pass integer pointers to SERIALUI_ dialog config fns */
252 dwBaudRate
= lpdcb
->BaudRate
;
253 dwStopBits
= lpdcb
->StopBits
;
254 dwParity
= lpdcb
->Parity
;
255 dwByteSize
= lpdcb
->ByteSize
;
257 /* map flow control state, if it looks normal */
258 if((lpdcb
->fRtsControl
== RTS_CONTROL_HANDSHAKE
) ||
259 (lpdcb
->fOutxCtsFlow
)) {
261 } else if(lpdcb
->fOutX
|| lpdcb
->fInX
) {
267 info
->bConvert
= SERIALUI_MakeBaudDword(&dwBaudRate
);
269 SERIALUI_AddConfItems( hDlg
, IDC_BAUD
, &SERIALUI_Baud2Str
,dwBaudRate
);
270 SERIALUI_AddConfItems( hDlg
, IDC_STOP
, &SERIALUI_Stop2Str
,dwStopBits
);
271 SERIALUI_AddConfItems( hDlg
, IDC_PARITY
, &SERIALUI_Parity2Str
,dwParity
);
272 SERIALUI_AddConfItems( hDlg
, IDC_DATA
, &SERIALUI_Data2Str
,dwByteSize
);
273 SERIALUI_AddConfItems( hDlg
, IDC_FLOW
, &SERIALUI_Flow2Str
, dwFlowControl
);
275 info
->dwFlowControl
= dwFlowControl
;
278 static void SERIALUI_DialogInfoToDCB(HWND hDlg
, SERIALUI_DialogInfo
*info
)
280 DWORD dwBaudRate
, dwStopBits
, dwParity
, dwByteSize
, dwFlowControl
;
281 LPDCB lpdcb
= &info
->lpCommConfig
->dcb
;
283 SERIALUI_GetConfItems( hDlg
, IDC_BAUD
, &SERIALUI_Baud2Str
, &dwBaudRate
);
284 SERIALUI_GetConfItems( hDlg
, IDC_STOP
, &SERIALUI_Stop2Str
, &dwStopBits
);
285 SERIALUI_GetConfItems( hDlg
, IDC_PARITY
, &SERIALUI_Parity2Str
, &dwParity
);
286 SERIALUI_GetConfItems( hDlg
, IDC_DATA
, &SERIALUI_Data2Str
, &dwByteSize
);
287 SERIALUI_GetConfItems( hDlg
, IDC_FLOW
, &SERIALUI_Flow2Str
, &dwFlowControl
);
289 TRACE("baud=%ld stop=%ld parity=%ld data=%ld flow=%ld\n",
290 dwBaudRate
, dwStopBits
, dwParity
, dwByteSize
, dwFlowControl
);
292 lpdcb
->BaudRate
= dwBaudRate
;
293 lpdcb
->StopBits
= dwStopBits
;
294 lpdcb
->Parity
= dwParity
;
295 lpdcb
->ByteSize
= dwByteSize
;
297 /* try not to change flow control if the user didn't change it */
298 if(info
->dwFlowControl
!= dwFlowControl
)
300 switch(dwFlowControl
)
303 lpdcb
->fOutxCtsFlow
= FALSE
;
304 lpdcb
->fOutxDsrFlow
= FALSE
;
305 lpdcb
->fDtrControl
= DTR_CONTROL_DISABLE
;
306 lpdcb
->fOutX
= FALSE
;
308 lpdcb
->fRtsControl
= RTS_CONTROL_DISABLE
;
310 case 1: /* CTS/RTS */
311 lpdcb
->fOutxCtsFlow
= TRUE
;
312 lpdcb
->fOutxDsrFlow
= FALSE
;
313 lpdcb
->fDtrControl
= DTR_CONTROL_DISABLE
;
314 lpdcb
->fOutX
= FALSE
;
316 lpdcb
->fRtsControl
= RTS_CONTROL_HANDSHAKE
;
319 lpdcb
->fOutxCtsFlow
= FALSE
;
320 lpdcb
->fOutxDsrFlow
= FALSE
;
321 lpdcb
->fDtrControl
= DTR_CONTROL_DISABLE
;
324 lpdcb
->fRtsControl
= RTS_CONTROL_DISABLE
;
330 SERIALUI_MakeBaudEnum(&lpdcb
->BaudRate
);
333 /***********************************************************************
334 * SERIALUI_ConfigDialogProc
336 * Shows a dialog for configuring a COMM port
338 static INT_PTR CALLBACK
SERIALUI_ConfigDialogProc(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
340 WCHAR szTitle
[128], format
[40];
341 SERIALUI_DialogInfo
*info
;
346 info
= (SERIALUI_DialogInfo
*) lParam
;
349 SetWindowLongPtrW(hWnd
, DWLP_USER
, lParam
);
350 GetWindowTextW(hWnd
, format
, ARRAY_SIZE(format
));
351 swprintf(szTitle
, ARRAY_SIZE(szTitle
), format
, info
->lpszDevice
);
352 SetWindowTextW(hWnd
, szTitle
);
353 SERIALUI_DCBToDialogInfo(hWnd
, info
);
358 WORD wID
= LOWORD(wParam
);
360 info
= (SERIALUI_DialogInfo
*) GetWindowLongPtrW(hWnd
, DWLP_USER
);
366 SERIALUI_DialogInfoToDCB(hWnd
,info
);
367 EndDialog(hWnd
, ERROR_SUCCESS
);
370 EndDialog(hWnd
, ERROR_CANCELLED
);
372 /* test code for Get/SetDefaultCommConfig begins */
375 DWORD r
,dwConfSize
= sizeof (COMMCONFIG
);
376 r
= GetDefaultCommConfigW(info
->lpszDevice
,
377 info
->lpCommConfig
, &dwConfSize
);
379 MessageBoxA(hWnd
,"Failed","GetDefaultCommConfig",MB_OK
);
381 SERIALUI_DCBToDialogInfo(hWnd
, info
);
386 SERIALUI_DialogInfoToDCB(hWnd
,info
);
387 r
= SetDefaultCommConfigW(info
->lpszDevice
,
388 info
->lpCommConfig
, sizeof (COMMCONFIG
));
390 MessageBoxA(hWnd
,"Failed","GetDefaultCommConfig",MB_OK
);
393 /* test code for Get/SetDefaultCommConfig ends */
401 static LPWSTR
SERIALUI_strdup( LPCSTR str
)
408 len
= MultiByteToWideChar( CP_ACP
, 0, str
, -1, NULL
, 0 );
409 strW
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
) );
410 MultiByteToWideChar( CP_ACP
, 0, str
, -1, strW
, len
);
414 static VOID
SERIALUI_strfree( LPWSTR strW
)
416 HeapFree( GetProcessHeap(), 0, strW
);
419 /***********************************************************************
420 * drvCommConfigDialogW (SERIALUI.@)
422 * Show a dialog for configuring a Serial Port.
425 DWORD WINAPI
drvCommConfigDialogW(LPCWSTR lpszName
, HWND hWndParent
, LPCOMMCONFIG lpCommConfig
)
427 SERIALUI_DialogInfo info
;
430 info
.lpCommConfig
= lpCommConfig
;
431 info
.lpszDevice
= lpszName
;
432 info
.bConvert
= FALSE
;
433 info
.dwFlowControl
= 0;
435 if ((!lpCommConfig
) || (!lpszName
))
436 return ERROR_INVALID_PARAMETER
;
438 if (lpCommConfig
->dwSize
< sizeof(COMMCONFIG
))
439 return ERROR_INSUFFICIENT_BUFFER
;
444 res
= DialogBoxParamW( SERIALUI_hModule
,
445 MAKEINTRESOURCEW(IDD_SERIALUICONFIG
),
447 SERIALUI_ConfigDialogProc
,
450 return (res
== -1) ? GetLastError() : res
;
453 /***********************************************************************
454 * drvCommConfigDialogA (SERIALUI.@)
456 DWORD WINAPI
drvCommConfigDialogA(LPCSTR lpszName
, HWND hWndParent
, LPCOMMCONFIG lpCommConfig
)
458 LPWSTR strW
= SERIALUI_strdup( lpszName
);
459 DWORD r
= drvCommConfigDialogW( strW
, hWndParent
, lpCommConfig
);
460 SERIALUI_strfree( strW
);
464 static const WCHAR lpszCommKey
[] = L
"System\\CurrentControlSet\\Services\\Class\\Ports";
466 /***********************************************************************
467 * drvSetDefaultCommConfigW (SERIALUI.@)
469 * Used by Win98 KERNEL to set the default config for a COMM port
470 * FIXME: uses the wrong registry key... should use a digit, not
471 * the comm port name.
473 BOOL WINAPI
drvSetDefaultCommConfigW(
474 LPCWSTR lpszDevice
, LPCOMMCONFIG lpCommConfig
, DWORD dwSize
)
476 HKEY hKeyReg
=0, hKeyPort
=0;
477 WCHAR szKeyName
[100];
480 TRACE("%p %p %lx\n",lpszDevice
,lpCommConfig
,dwSize
);
485 if(dwSize
< sizeof (COMMCONFIG
))
488 r
= RegConnectRegistryW(NULL
, HKEY_LOCAL_MACHINE
, &hKeyReg
);
489 if(r
!= ERROR_SUCCESS
)
492 swprintf(szKeyName
, ARRAY_SIZE(szKeyName
), L
"%s\\%s", lpszCommKey
, lpszDevice
);
493 r
= RegCreateKeyW(hKeyReg
, szKeyName
, &hKeyPort
);
494 if(r
== ERROR_SUCCESS
)
496 dwDCBSize
= sizeof (DCB
);
497 r
= RegSetValueExW(hKeyPort
, L
"DCB", 0, REG_BINARY
, (BYTE
*)&lpCommConfig
->dcb
, dwDCBSize
);
498 TRACE("write key r=%ld\n",r
);
499 RegCloseKey(hKeyPort
);
502 RegCloseKey(hKeyReg
);
504 return (r
==ERROR_SUCCESS
);
507 /***********************************************************************
508 * drvSetDefaultCommConfigA (SERIALUI.@)
510 BOOL WINAPI
drvSetDefaultCommConfigA(
511 LPCSTR lpszDevice
, LPCOMMCONFIG lpCommConfig
, DWORD dwSize
)
513 LPWSTR strW
= SERIALUI_strdup( lpszDevice
);
514 BOOL r
= drvSetDefaultCommConfigW( strW
, lpCommConfig
, dwSize
);
515 SERIALUI_strfree( strW
);
519 /***********************************************************************
520 * drvGetDefaultCommConfigW (SERIALUI.@)
522 * Used by Win9x KERNEL to get the default config for a COMM port
523 * FIXME: uses the wrong registry key... should use a digit, not
524 * the comm port name.
526 DWORD WINAPI
drvGetDefaultCommConfigW(
527 LPCWSTR lpszDevice
, LPCOMMCONFIG lpCommConfig
, LPDWORD lpdwSize
)
529 HKEY hKeyReg
, hKeyPort
;
530 WCHAR szKeyName
[100];
531 DWORD r
,dwSize
,dwType
;
533 TRACE("(%s, %p, %p) *lpdwSize: %lu\n", debugstr_w(lpszDevice
), lpCommConfig
, lpdwSize
, lpdwSize
? *lpdwSize
: 0);
535 if ((!lpszDevice
) || (!lpCommConfig
) || (!lpdwSize
)) {
536 return ERROR_INVALID_PARAMETER
;
539 if (*lpdwSize
< sizeof (COMMCONFIG
)) {
540 *lpdwSize
= sizeof (COMMCONFIG
);
541 return ERROR_INSUFFICIENT_BUFFER
;
544 /* only "com1" - "com9" is allowed */
545 r
= ARRAY_SIZE(L
"com");
546 lstrcpynW(szKeyName
, lpszDevice
, r
); /* simulate a lstrcmpnW */
549 if (lstrcmpiW(szKeyName
, L
"com") ||
550 (lpszDevice
[r
] < '1') || (lpszDevice
[r
] > '9') || lpszDevice
[r
+1]) {
554 *lpdwSize
= sizeof (COMMCONFIG
);
555 memset(lpCommConfig
, 0 , sizeof (COMMCONFIG
));
556 lpCommConfig
->dwSize
= sizeof (COMMCONFIG
);
557 lpCommConfig
->wVersion
= 1;
558 lpCommConfig
->dwProviderSubType
= PST_RS232
;
560 r
= RegConnectRegistryW(NULL
, HKEY_LOCAL_MACHINE
, &hKeyReg
);
561 if(r
!= ERROR_SUCCESS
) return r
;
563 swprintf(szKeyName
, ARRAY_SIZE(szKeyName
), L
"%s\\%s", lpszCommKey
, lpszDevice
);
564 r
= RegOpenKeyW(hKeyReg
, szKeyName
, &hKeyPort
);
565 if(r
== ERROR_SUCCESS
)
567 dwSize
= sizeof (DCB
);
569 r
= RegQueryValueExW(hKeyPort
, L
"DCB", NULL
, &dwType
, (BYTE
*)&lpCommConfig
->dcb
, &dwSize
);
571 RegCloseKey(hKeyPort
);
572 if ((r
!=ERROR_SUCCESS
) || (dwType
!= REG_BINARY
) || (dwSize
!= sizeof(DCB
))) {
573 RegCloseKey(hKeyReg
);
574 return ERROR_INVALID_PARAMETER
;
580 /* FIXME: default to a hardcoded commconfig */
581 lpCommConfig
->dcb
.DCBlength
= sizeof(DCB
);
582 lpCommConfig
->dcb
.BaudRate
= 9600;
583 lpCommConfig
->dcb
.fBinary
= TRUE
;
584 lpCommConfig
->dcb
.fParity
= FALSE
;
585 lpCommConfig
->dcb
.ByteSize
= 8;
586 lpCommConfig
->dcb
.Parity
= NOPARITY
;
587 lpCommConfig
->dcb
.StopBits
= ONESTOPBIT
;
588 return ERROR_SUCCESS
;
591 RegCloseKey(hKeyReg
);
596 /***********************************************************************
597 * drvGetDefaultCommConfigA (SERIALUI.@)
599 DWORD WINAPI
drvGetDefaultCommConfigA(
600 LPCSTR lpszDevice
, LPCOMMCONFIG lpCommConfig
, LPDWORD lpdwSize
)
602 LPWSTR strW
= SERIALUI_strdup( lpszDevice
);
603 DWORD r
= drvGetDefaultCommConfigW( strW
, lpCommConfig
, lpdwSize
);
604 SERIALUI_strfree( strW
);