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 static const WCHAR comW
[] = {'c','o','m',0 };
51 /***********************************************************************
52 * DllMain [Internal] Initializes the internal 'SERIALUI.DLL'.
55 * hinstDLL [I] handle to the DLL's instance
57 * lpvReserved [I] reserved, must be NULL
64 BOOL WINAPI
DllMain (HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
66 TRACE("%p,%x,%p\n", hinstDLL
, fdwReason
, lpvReserved
);
69 case DLL_PROCESS_ATTACH
:
70 DisableThreadLibraryCalls(hinstDLL
);
71 SERIALUI_hModule
= hinstDLL
;
79 /***********************************************************************
80 * EnumPropPages (SERIALUI.2)
82 * Called by the device manager to add prop sheets in Control Panel ???
83 * Pointed to in Win98 registry by
84 * \System\CurrentControlSet\Services\Class\ports\0000\EnumPropPages =
85 * "serialui.dll,EnumPropPages"
87 typedef LPVOID LPDEVICE_INFO
;
88 typedef LPVOID LPFNADDPROPSHEETPAGE
;
89 BOOL WINAPI
EnumPropPages(LPDEVICE_INFO pdi
, LPFNADDPROPSHEETPAGE pfnAdd
, LPARAM lParam
)
91 FIXME("(%p %p %lx)\n",pdi
,pfnAdd
,lParam
);
96 * These data structures are convert from values used in fields of a DCB
97 * to strings used in the CommConfigDialog.
99 typedef struct tagPARAM2STRDATA
103 } PARAM2STRDATA
, *LPPARAM2STRDATA
;
105 typedef struct tagPARAM2STR
108 LPPARAM2STRDATA data
;
109 } PARAM2STR
, *LPPARAM2STR
;
110 typedef const PARAM2STR
*LPCPARAM2STR
;
112 static PARAM2STRDATA SERIALUI_Baud2StrData
[]={
113 {110, "110"}, {300, "300"}, {600, "600"}, {1200, "1200"},
114 {2400, "2400"}, {4800, "4800"}, {9600, "9600"}, {14400, "14400"},
115 {19200, "19200"}, {38400L, "38400"}, {56000L, "56000"}, {57600L, "57600"},
116 {115200L, "115200"}, {128000L, "128000"}, {256000L, "256000"}
118 static PARAM2STR SERIALUI_Baud2Str
={ ARRAY_SIZE(SERIALUI_Baud2StrData
), SERIALUI_Baud2StrData
};
120 static PARAM2STRDATA SERIALUI_Parity2StrData
[]={
121 {NOPARITY
,"None"}, {ODDPARITY
,"Odd"}, {EVENPARITY
,"Even"}, {MARKPARITY
,"Mark"},
122 {SPACEPARITY
,"Space"}
124 static PARAM2STR SERIALUI_Parity2Str
={ ARRAY_SIZE(SERIALUI_Parity2StrData
), SERIALUI_Parity2StrData
};
126 static PARAM2STRDATA SERIALUI_Stop2StrData
[]={
127 {ONESTOPBIT
,"1"}, {ONE5STOPBITS
,"1.5"}, {TWOSTOPBITS
,"2"}
129 static PARAM2STR SERIALUI_Stop2Str
={ ARRAY_SIZE(SERIALUI_Stop2StrData
), SERIALUI_Stop2StrData
};
131 static PARAM2STRDATA SERIALUI_Data2StrData
[]={
132 {5,"5"}, {6,"6"}, {7,"7"}, {8, "8"}, {16,"16"}
134 static PARAM2STR SERIALUI_Data2Str
={ ARRAY_SIZE(SERIALUI_Data2StrData
), SERIALUI_Data2StrData
};
136 static PARAM2STRDATA SERIALUI_Flow2StrData
[]={
137 {0,"None"}, {1,"Hardware (RTS/CTS)"}, {2,"Software (XON/XOFF)"}
139 static PARAM2STR SERIALUI_Flow2Str
={ ARRAY_SIZE(SERIALUI_Flow2StrData
), SERIALUI_Flow2StrData
};
142 * Add all the fields to a combo box and highlight the current value
144 static void SERIALUI_AddConfItems(HWND hDlg
, DWORD id
, LPCPARAM2STR table
, DWORD dwVal
)
148 HWND hControl
= GetDlgItem(hDlg
,id
);
153 for(i
=0; i
<table
->dwSize
; i
++)
155 n
= SendMessageA(hControl
, CB_ADDSTRING
, 0L, (LPARAM
)table
->data
[i
].name
);
156 if(dwVal
== table
->data
[i
].val
)
158 SendMessageA(hControl
, CB_SETCURSEL
, n
, 0);
164 * Get the current selection of the given combo box and set a DCB field to
165 * the value matching that selection.
167 static BOOL
SERIALUI_GetConfItems(HWND hDlg
, DWORD id
, LPCPARAM2STR table
, LPDWORD lpdwVal
)
171 HWND hControl
= GetDlgItem(hDlg
,id
);
173 if( (!hControl
) || (!lpdwVal
))
175 TRACE("Couldn't get window handle for item %x\n",id
);
179 if(!GetWindowTextA(hControl
, &lpEntry
[0], sizeof(lpEntry
)))
181 TRACE("Couldn't get window text for item %x\n",id
);
184 /* TRACE("%ld contains %s\n",id, lpEntry); */
186 for(i
=0; i
<table
->dwSize
; i
++)
188 if(!lstrcmpA(table
->data
[i
].name
,lpEntry
))
190 *lpdwVal
= table
->data
[i
].val
;
199 * Both the enumerated values CBR_XXXX and integer baud rates are valid
200 * dcb.BaudRate. This code is to convert back and forth between CBR_ style
201 * and integers. The dialog box uses integer values.
203 static const DWORD SERIALUI_BaudConvertTable
[] = {
204 CBR_110
, 110, CBR_300
, 300, CBR_600
, 600, CBR_1200
, 1200,
205 CBR_2400
, 2400, CBR_4800
, 4800, CBR_9600
, 9600, CBR_14400
, 14400,
206 CBR_19200
, 19200, CBR_38400
, 38400, CBR_56000
, 56000, CBR_57600
, 57600,
207 CBR_115200
, 115200, CBR_128000
, 128000, CBR_256000
, 256000
210 static BOOL
SERIALUI_MakeBaudDword(LPDWORD lpdwBaudRate
)
214 for(i
=0; i
<ARRAY_SIZE(SERIALUI_BaudConvertTable
); i
+=2)
216 if(*lpdwBaudRate
== SERIALUI_BaudConvertTable
[i
])
218 *lpdwBaudRate
= SERIALUI_BaudConvertTable
[i
+1];
225 static BOOL
SERIALUI_MakeBaudEnum(LPDWORD lpdwBaudRate
)
229 for(i
=0; i
<ARRAY_SIZE(SERIALUI_BaudConvertTable
); i
+=2)
231 if(*lpdwBaudRate
== SERIALUI_BaudConvertTable
[i
+1])
233 *lpdwBaudRate
= SERIALUI_BaudConvertTable
[i
];
240 typedef struct tagSERIALUI_DialogInfo
243 LPCOMMCONFIG lpCommConfig
;
244 BOOL bConvert
; /* baud rate was converted to a DWORD */
245 DWORD dwFlowControl
; /* old flow control */
246 } SERIALUI_DialogInfo
;
248 static void SERIALUI_DCBToDialogInfo(HWND hDlg
, SERIALUI_DialogInfo
*info
)
250 DWORD dwBaudRate
, dwStopBits
, dwParity
, dwByteSize
, dwFlowControl
;
251 LPDCB lpdcb
= &info
->lpCommConfig
->dcb
;
253 /* pass integer pointers to SERIALUI_ dialog config fns */
254 dwBaudRate
= lpdcb
->BaudRate
;
255 dwStopBits
= lpdcb
->StopBits
;
256 dwParity
= lpdcb
->Parity
;
257 dwByteSize
= lpdcb
->ByteSize
;
259 /* map flow control state, if it looks normal */
260 if((lpdcb
->fRtsControl
== RTS_CONTROL_HANDSHAKE
) ||
261 (lpdcb
->fOutxCtsFlow
)) {
263 } else if(lpdcb
->fOutX
|| lpdcb
->fInX
) {
269 info
->bConvert
= SERIALUI_MakeBaudDword(&dwBaudRate
);
271 SERIALUI_AddConfItems( hDlg
, IDC_BAUD
, &SERIALUI_Baud2Str
,dwBaudRate
);
272 SERIALUI_AddConfItems( hDlg
, IDC_STOP
, &SERIALUI_Stop2Str
,dwStopBits
);
273 SERIALUI_AddConfItems( hDlg
, IDC_PARITY
, &SERIALUI_Parity2Str
,dwParity
);
274 SERIALUI_AddConfItems( hDlg
, IDC_DATA
, &SERIALUI_Data2Str
,dwByteSize
);
275 SERIALUI_AddConfItems( hDlg
, IDC_FLOW
, &SERIALUI_Flow2Str
, dwFlowControl
);
277 info
->dwFlowControl
= dwFlowControl
;
280 static void SERIALUI_DialogInfoToDCB(HWND hDlg
, SERIALUI_DialogInfo
*info
)
282 DWORD dwBaudRate
, dwStopBits
, dwParity
, dwByteSize
, dwFlowControl
;
283 LPDCB lpdcb
= &info
->lpCommConfig
->dcb
;
285 SERIALUI_GetConfItems( hDlg
, IDC_BAUD
, &SERIALUI_Baud2Str
, &dwBaudRate
);
286 SERIALUI_GetConfItems( hDlg
, IDC_STOP
, &SERIALUI_Stop2Str
, &dwStopBits
);
287 SERIALUI_GetConfItems( hDlg
, IDC_PARITY
, &SERIALUI_Parity2Str
, &dwParity
);
288 SERIALUI_GetConfItems( hDlg
, IDC_DATA
, &SERIALUI_Data2Str
, &dwByteSize
);
289 SERIALUI_GetConfItems( hDlg
, IDC_FLOW
, &SERIALUI_Flow2Str
, &dwFlowControl
);
291 TRACE("baud=%d stop=%d parity=%d data=%d flow=%d\n",
292 dwBaudRate
, dwStopBits
, dwParity
, dwByteSize
, dwFlowControl
);
294 lpdcb
->BaudRate
= dwBaudRate
;
295 lpdcb
->StopBits
= dwStopBits
;
296 lpdcb
->Parity
= dwParity
;
297 lpdcb
->ByteSize
= dwByteSize
;
299 /* try not to change flow control if the user didn't change it */
300 if(info
->dwFlowControl
!= dwFlowControl
)
302 switch(dwFlowControl
)
305 lpdcb
->fOutxCtsFlow
= FALSE
;
306 lpdcb
->fOutxDsrFlow
= FALSE
;
307 lpdcb
->fDtrControl
= DTR_CONTROL_DISABLE
;
308 lpdcb
->fOutX
= FALSE
;
310 lpdcb
->fRtsControl
= RTS_CONTROL_DISABLE
;
312 case 1: /* CTS/RTS */
313 lpdcb
->fOutxCtsFlow
= TRUE
;
314 lpdcb
->fOutxDsrFlow
= FALSE
;
315 lpdcb
->fDtrControl
= DTR_CONTROL_DISABLE
;
316 lpdcb
->fOutX
= FALSE
;
318 lpdcb
->fRtsControl
= RTS_CONTROL_HANDSHAKE
;
321 lpdcb
->fOutxCtsFlow
= FALSE
;
322 lpdcb
->fOutxDsrFlow
= FALSE
;
323 lpdcb
->fDtrControl
= DTR_CONTROL_DISABLE
;
326 lpdcb
->fRtsControl
= RTS_CONTROL_DISABLE
;
332 SERIALUI_MakeBaudEnum(&lpdcb
->BaudRate
);
335 /***********************************************************************
336 * SERIALUI_ConfigDialogProc
338 * Shows a dialog for configuring a COMM port
340 static INT_PTR CALLBACK
SERIALUI_ConfigDialogProc(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
342 WCHAR szTitle
[128], format
[40];
343 SERIALUI_DialogInfo
*info
;
348 info
= (SERIALUI_DialogInfo
*) lParam
;
351 SetWindowLongPtrW(hWnd
, DWLP_USER
, lParam
);
352 GetWindowTextW(hWnd
, format
, ARRAY_SIZE(format
));
353 swprintf(szTitle
, ARRAY_SIZE(szTitle
), format
, info
->lpszDevice
);
354 SetWindowTextW(hWnd
, szTitle
);
355 SERIALUI_DCBToDialogInfo(hWnd
, info
);
360 WORD wID
= LOWORD(wParam
);
362 info
= (SERIALUI_DialogInfo
*) GetWindowLongPtrW(hWnd
, DWLP_USER
);
368 SERIALUI_DialogInfoToDCB(hWnd
,info
);
369 EndDialog(hWnd
, ERROR_SUCCESS
);
372 EndDialog(hWnd
, ERROR_CANCELLED
);
374 /* test code for Get/SetDefaultCommConfig begins */
377 DWORD r
,dwConfSize
= sizeof (COMMCONFIG
);
378 r
= GetDefaultCommConfigW(info
->lpszDevice
,
379 info
->lpCommConfig
, &dwConfSize
);
381 MessageBoxA(hWnd
,"Failed","GetDefaultCommConfig",MB_OK
);
383 SERIALUI_DCBToDialogInfo(hWnd
, info
);
388 SERIALUI_DialogInfoToDCB(hWnd
,info
);
389 r
= SetDefaultCommConfigW(info
->lpszDevice
,
390 info
->lpCommConfig
, sizeof (COMMCONFIG
));
392 MessageBoxA(hWnd
,"Failed","GetDefaultCommConfig",MB_OK
);
395 /* test code for Get/SetDefaultCommConfig ends */
403 static LPWSTR
SERIALUI_strdup( LPCSTR str
)
410 len
= MultiByteToWideChar( CP_ACP
, 0, str
, -1, NULL
, 0 );
411 strW
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
) );
412 MultiByteToWideChar( CP_ACP
, 0, str
, -1, strW
, len
);
416 static VOID
SERIALUI_strfree( LPWSTR strW
)
418 HeapFree( GetProcessHeap(), 0, strW
);
421 /***********************************************************************
422 * drvCommConfigDialogW (SERIALUI.@)
424 * Show a dialog for configuring a Serial Port.
427 DWORD WINAPI
drvCommConfigDialogW(LPCWSTR lpszName
, HWND hWndParent
, LPCOMMCONFIG lpCommConfig
)
429 SERIALUI_DialogInfo info
;
432 info
.lpCommConfig
= lpCommConfig
;
433 info
.lpszDevice
= lpszName
;
434 info
.bConvert
= FALSE
;
435 info
.dwFlowControl
= 0;
437 if ((!lpCommConfig
) || (!lpszName
))
438 return ERROR_INVALID_PARAMETER
;
440 if (lpCommConfig
->dwSize
< sizeof(COMMCONFIG
))
441 return ERROR_INSUFFICIENT_BUFFER
;
446 res
= DialogBoxParamW( SERIALUI_hModule
,
447 MAKEINTRESOURCEW(IDD_SERIALUICONFIG
),
449 SERIALUI_ConfigDialogProc
,
452 return (res
== -1) ? GetLastError() : res
;
455 /***********************************************************************
456 * drvCommConfigDialogA (SERIALUI.@)
458 DWORD WINAPI
drvCommConfigDialogA(LPCSTR lpszName
, HWND hWndParent
, LPCOMMCONFIG lpCommConfig
)
460 LPWSTR strW
= SERIALUI_strdup( lpszName
);
461 DWORD r
= drvCommConfigDialogW( strW
, hWndParent
, lpCommConfig
);
462 SERIALUI_strfree( strW
);
466 static const WCHAR lpszCommKey
[] = {
467 'S','y','s','t','e','m','\\',
468 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
469 'S','e','r','v','i','c','e','s','\\',
470 'C','l','a','s','s','\\','P','o','r','t','s',0
472 static const WCHAR lpszDCB
[] = {'D','C','B',0};
474 /***********************************************************************
475 * drvSetDefaultCommConfigW (SERIALUI.@)
477 * Used by Win98 KERNEL to set the default config for a COMM port
478 * FIXME: uses the wrong registry key... should use a digit, not
479 * the comm port name.
481 BOOL WINAPI
drvSetDefaultCommConfigW(
482 LPCWSTR lpszDevice
, LPCOMMCONFIG lpCommConfig
, DWORD dwSize
)
484 HKEY hKeyReg
=0, hKeyPort
=0;
485 WCHAR szKeyName
[100];
487 static const WCHAR fmt
[] = {'%','s','\\','%','s',0 };
489 TRACE("%p %p %x\n",lpszDevice
,lpCommConfig
,dwSize
);
494 if(dwSize
< sizeof (COMMCONFIG
))
497 r
= RegConnectRegistryW(NULL
, HKEY_LOCAL_MACHINE
, &hKeyReg
);
498 if(r
!= ERROR_SUCCESS
)
501 swprintf(szKeyName
, ARRAY_SIZE(szKeyName
), fmt
, lpszCommKey
, lpszDevice
);
502 r
= RegCreateKeyW(hKeyReg
, szKeyName
, &hKeyPort
);
503 if(r
== ERROR_SUCCESS
)
505 dwDCBSize
= sizeof (DCB
);
506 r
= RegSetValueExW( hKeyPort
, lpszDCB
, 0, REG_BINARY
,
507 (LPBYTE
)&lpCommConfig
->dcb
,dwDCBSize
);
508 TRACE("write key r=%d\n",r
);
509 RegCloseKey(hKeyPort
);
512 RegCloseKey(hKeyReg
);
514 return (r
==ERROR_SUCCESS
);
517 /***********************************************************************
518 * drvSetDefaultCommConfigA (SERIALUI.@)
520 BOOL WINAPI
drvSetDefaultCommConfigA(
521 LPCSTR lpszDevice
, LPCOMMCONFIG lpCommConfig
, DWORD dwSize
)
523 LPWSTR strW
= SERIALUI_strdup( lpszDevice
);
524 BOOL r
= drvSetDefaultCommConfigW( strW
, lpCommConfig
, dwSize
);
525 SERIALUI_strfree( strW
);
529 /***********************************************************************
530 * drvGetDefaultCommConfigW (SERIALUI.@)
532 * Used by Win9x KERNEL to get the default config for a COMM port
533 * FIXME: uses the wrong registry key... should use a digit, not
534 * the comm port name.
536 DWORD WINAPI
drvGetDefaultCommConfigW(
537 LPCWSTR lpszDevice
, LPCOMMCONFIG lpCommConfig
, LPDWORD lpdwSize
)
539 HKEY hKeyReg
, hKeyPort
;
540 WCHAR szKeyName
[100];
541 DWORD r
,dwSize
,dwType
;
542 static const WCHAR fmt
[] = {'%','s','\\','%','s',0 };
544 TRACE("(%s, %p, %p) *lpdwSize: %u\n", debugstr_w(lpszDevice
), lpCommConfig
, lpdwSize
, lpdwSize
? *lpdwSize
: 0);
546 if ((!lpszDevice
) || (!lpCommConfig
) || (!lpdwSize
)) {
547 return ERROR_INVALID_PARAMETER
;
550 if (*lpdwSize
< sizeof (COMMCONFIG
)) {
551 *lpdwSize
= sizeof (COMMCONFIG
);
552 return ERROR_INSUFFICIENT_BUFFER
;
555 /* only "com1" - "com9" is allowed */
556 r
= ARRAY_SIZE(comW
); /* len of "com\0" */
557 lstrcpynW(szKeyName
, lpszDevice
, r
); /* simulate a lstrcmpnW */
560 if( lstrcmpiW(szKeyName
, comW
) ||
561 (lpszDevice
[r
] < '1') || (lpszDevice
[r
] > '9') || lpszDevice
[r
+1]) {
565 *lpdwSize
= sizeof (COMMCONFIG
);
566 memset(lpCommConfig
, 0 , sizeof (COMMCONFIG
));
567 lpCommConfig
->dwSize
= sizeof (COMMCONFIG
);
568 lpCommConfig
->wVersion
= 1;
569 lpCommConfig
->dwProviderSubType
= PST_RS232
;
571 r
= RegConnectRegistryW(NULL
, HKEY_LOCAL_MACHINE
, &hKeyReg
);
572 if(r
!= ERROR_SUCCESS
) return r
;
574 swprintf(szKeyName
, ARRAY_SIZE(szKeyName
), fmt
, lpszCommKey
, lpszDevice
);
575 r
= RegOpenKeyW(hKeyReg
, szKeyName
, &hKeyPort
);
576 if(r
== ERROR_SUCCESS
)
578 dwSize
= sizeof (DCB
);
580 r
= RegQueryValueExW( hKeyPort
, lpszDCB
, NULL
,
581 &dwType
, (LPBYTE
)&lpCommConfig
->dcb
, &dwSize
);
583 RegCloseKey(hKeyPort
);
584 if ((r
!=ERROR_SUCCESS
) || (dwType
!= REG_BINARY
) || (dwSize
!= sizeof(DCB
))) {
585 RegCloseKey(hKeyReg
);
586 return ERROR_INVALID_PARAMETER
;
592 /* FIXME: default to a hardcoded commconfig */
593 lpCommConfig
->dcb
.DCBlength
= sizeof(DCB
);
594 lpCommConfig
->dcb
.BaudRate
= 9600;
595 lpCommConfig
->dcb
.fBinary
= TRUE
;
596 lpCommConfig
->dcb
.fParity
= FALSE
;
597 lpCommConfig
->dcb
.ByteSize
= 8;
598 lpCommConfig
->dcb
.Parity
= NOPARITY
;
599 lpCommConfig
->dcb
.StopBits
= ONESTOPBIT
;
600 return ERROR_SUCCESS
;
603 RegCloseKey(hKeyReg
);
608 /***********************************************************************
609 * drvGetDefaultCommConfigA (SERIALUI.@)
611 DWORD WINAPI
drvGetDefaultCommConfigA(
612 LPCSTR lpszDevice
, LPCOMMCONFIG lpCommConfig
, LPDWORD lpdwSize
)
614 LPWSTR strW
= SERIALUI_strdup( lpszDevice
);
615 DWORD r
= drvGetDefaultCommConfigW( strW
, lpCommConfig
, lpdwSize
);
616 SERIALUI_strfree( strW
);