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 HMODULE SERIALUI_hModule = 0;
52 /***********************************************************************
53 * DllMain [Internal] Initializes the internal 'SERIALUI.DLL'.
56 * hinstDLL [I] handle to the DLL's instance
58 * lpvReserved [I] reserved, must be NULL
65 BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
67 TRACE("%p,%x,%p\n", hinstDLL, fdwReason, lpvReserved);
70 case DLL_PROCESS_ATTACH:
71 DisableThreadLibraryCalls(hinstDLL);
72 SERIALUI_hModule = hinstDLL;
74 case DLL_PROCESS_DETACH:
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 LPPARAM2STR LPCPARAM2STR;
115 #define SERIALUI_TABLESIZE(x) ((sizeof (x))/(sizeof (x[0])))
117 static PARAM2STRDATA SERIALUI_Baud2StrData[]={
118 {110, "110"}, {300, "300"}, {600, "600"}, {1200, "1200"},
119 {2400, "2400"}, {4800, "4800"}, {9600, "9600"}, {14400, "14400"},
120 {19200, "19200"}, {38400L, "38400"}, {56000L, "56000"}, {57600L, "57600"},
121 {115200L, "115200"}, {128000L, "128000"}, {256000L, "256000"}
123 static PARAM2STR SERIALUI_Baud2Str={ SERIALUI_TABLESIZE(SERIALUI_Baud2StrData),SERIALUI_Baud2StrData };
125 static PARAM2STRDATA SERIALUI_Parity2StrData[]={
126 {NOPARITY,"None"}, {ODDPARITY,"Odd"}, {EVENPARITY,"Even"}, {MARKPARITY,"Mark"},
127 {SPACEPARITY,"Space"}
129 static PARAM2STR SERIALUI_Parity2Str={ SERIALUI_TABLESIZE(SERIALUI_Parity2StrData),SERIALUI_Parity2StrData };
131 static PARAM2STRDATA SERIALUI_Stop2StrData[]={
132 {ONESTOPBIT,"1"}, {ONE5STOPBITS,"1.5"}, {TWOSTOPBITS,"2"}
134 static PARAM2STR SERIALUI_Stop2Str={ SERIALUI_TABLESIZE(SERIALUI_Stop2StrData),SERIALUI_Stop2StrData };
136 static PARAM2STRDATA SERIALUI_Data2StrData[]={
137 {5,"5"}, {6,"6"}, {7,"7"}, {8, "8"}, {16,"16"}
139 static PARAM2STR SERIALUI_Data2Str={ SERIALUI_TABLESIZE(SERIALUI_Data2StrData),SERIALUI_Data2StrData };
141 static PARAM2STRDATA SERIALUI_Flow2StrData[]={
142 {0,"None"}, {1,"Hardware (RTS/CTS)"}, {2,"Software (XON/XOFF)"}
144 static PARAM2STR SERIALUI_Flow2Str={ SERIALUI_TABLESIZE(SERIALUI_Flow2StrData),SERIALUI_Flow2StrData };
147 * Add all the fields to a combo box and highlight the current value
149 static void SERIALUI_AddConfItems(HWND hDlg, DWORD id, LPCPARAM2STR table, DWORD dwVal)
153 HWND hControl = GetDlgItem(hDlg,id);
158 for(i=0; i<table->dwSize; i++)
160 n = SendMessageA(hControl, CB_ADDSTRING, 0L, (LPARAM)table->data[i].name);
161 if(dwVal == table->data[i].val)
163 SendMessageA(hControl, CB_SETCURSEL, (WPARAM)n, (LPARAM)0);
169 * Get the current sellection of the given combo box and set a DCB field to
170 * the value matching that selection.
172 static BOOL SERIALUI_GetConfItems(HWND hDlg, DWORD id, LPCPARAM2STR table, LPDWORD lpdwVal)
176 HWND hControl = GetDlgItem(hDlg,id);
178 if( (!hControl) || (!lpdwVal))
180 TRACE("Couldn't get window handle for item %x\n",id);
184 if(!GetWindowTextA(hControl, &lpEntry[0], sizeof(lpEntry)))
186 TRACE("Couldn't get window text for item %x\n",id);
189 /* TRACE("%ld contains %s\n",id, lpEntry); */
191 for(i=0; i<table->dwSize; i++)
193 if(!lstrcmpA(table->data[i].name,lpEntry))
195 *lpdwVal = table->data[i].val;
204 * Both the enumerated values CBR_XXXX and integer baud rates are valid
205 * dcb.BaudRate. This code is to convert back and forth between CBR_ style
206 * and integers. The dialog box uses integer values.
208 static DWORD SERIALUI_BaudConvertTable[] = {
209 CBR_110, 110, CBR_300, 300, CBR_600, 600, CBR_1200, 1200,
210 CBR_2400, 2400, CBR_4800, 4800, CBR_9600, 9600, CBR_14400, 14400,
211 CBR_19200, 19200, CBR_38400, 38400, CBR_56000, 56000, CBR_57600, 57600,
212 CBR_115200, 115200, CBR_128000, 128000, CBR_256000, 256000
215 static BOOL SERIALUI_MakeBaudDword(LPDWORD lpdwBaudRate)
219 for(i=0; i<(sizeof(SERIALUI_BaudConvertTable)/sizeof(DWORD)); i+=2)
221 if(*lpdwBaudRate == SERIALUI_BaudConvertTable[i])
223 *lpdwBaudRate = SERIALUI_BaudConvertTable[i+1];
230 static BOOL SERIALUI_MakeBaudEnum(LPDWORD lpdwBaudRate)
234 for(i=0; i<(sizeof(SERIALUI_BaudConvertTable)/sizeof(DWORD)); i+=2)
236 if(*lpdwBaudRate == SERIALUI_BaudConvertTable[i+1])
238 *lpdwBaudRate = SERIALUI_BaudConvertTable[i];
245 typedef struct tagSERIALUI_DialogInfo
248 LPCOMMCONFIG lpCommConfig;
249 BOOL bConvert; /* baud rate was converted to a DWORD */
250 DWORD dwFlowControl; /* old flow control */
251 } SERIALUI_DialogInfo;
253 static void SERIALUI_DCBToDialogInfo(HWND hDlg, SERIALUI_DialogInfo *info)
255 DWORD dwBaudRate, dwStopBits, dwParity, dwByteSize, dwFlowControl;
256 LPDCB lpdcb = &info->lpCommConfig->dcb;
258 /* pass integer pointers to SERIALUI_ dialog config fns */
259 dwBaudRate = lpdcb->BaudRate;
260 dwStopBits = lpdcb->StopBits;
261 dwParity = lpdcb->Parity;
262 dwByteSize = lpdcb->ByteSize;
264 /* map flow control state, if it looks normal */
265 if((lpdcb->fRtsControl == RTS_CONTROL_HANDSHAKE) ||
266 (lpdcb->fOutxCtsFlow)) {
268 } else if(lpdcb->fOutX || lpdcb->fInX) {
274 info->bConvert = SERIALUI_MakeBaudDword(&dwBaudRate);
276 SERIALUI_AddConfItems( hDlg, IDC_BAUD, &SERIALUI_Baud2Str ,dwBaudRate);
277 SERIALUI_AddConfItems( hDlg, IDC_STOP, &SERIALUI_Stop2Str ,dwStopBits);
278 SERIALUI_AddConfItems( hDlg, IDC_PARITY, &SERIALUI_Parity2Str ,dwParity);
279 SERIALUI_AddConfItems( hDlg, IDC_DATA, &SERIALUI_Data2Str ,dwByteSize);
280 SERIALUI_AddConfItems( hDlg, IDC_FLOW, &SERIALUI_Flow2Str, dwFlowControl );
282 info->dwFlowControl = dwFlowControl;
285 static void SERIALUI_DialogInfoToDCB(HWND hDlg, SERIALUI_DialogInfo *info)
287 DWORD dwBaudRate, dwStopBits, dwParity, dwByteSize, dwFlowControl;
288 LPDCB lpdcb = &info->lpCommConfig->dcb;
290 SERIALUI_GetConfItems( hDlg, IDC_BAUD, &SERIALUI_Baud2Str, &dwBaudRate);
291 SERIALUI_GetConfItems( hDlg, IDC_STOP, &SERIALUI_Stop2Str, &dwStopBits);
292 SERIALUI_GetConfItems( hDlg, IDC_PARITY, &SERIALUI_Parity2Str, &dwParity);
293 SERIALUI_GetConfItems( hDlg, IDC_DATA, &SERIALUI_Data2Str, &dwByteSize);
294 SERIALUI_GetConfItems( hDlg, IDC_FLOW, &SERIALUI_Flow2Str, &dwFlowControl );
296 TRACE("baud=%d stop=%d parity=%d data=%d flow=%d\n",
297 dwBaudRate, dwStopBits, dwParity, dwByteSize, dwFlowControl);
299 lpdcb->BaudRate = dwBaudRate;
300 lpdcb->StopBits = dwStopBits;
301 lpdcb->Parity = dwParity;
302 lpdcb->ByteSize = dwByteSize;
304 /* try not to change flow control if the user didn't change it */
305 if(info->dwFlowControl != dwFlowControl)
307 switch(dwFlowControl)
310 lpdcb->fOutxCtsFlow = FALSE;
311 lpdcb->fOutxDsrFlow = FALSE;
312 lpdcb->fDtrControl = DTR_CONTROL_DISABLE;
313 lpdcb->fOutX = FALSE;
315 lpdcb->fRtsControl = RTS_CONTROL_DISABLE;
317 case 1: /* CTS/RTS */
318 lpdcb->fOutxCtsFlow = TRUE;
319 lpdcb->fOutxDsrFlow = FALSE;
320 lpdcb->fDtrControl = DTR_CONTROL_DISABLE;
321 lpdcb->fOutX = FALSE;
323 lpdcb->fRtsControl = RTS_CONTROL_HANDSHAKE;
326 lpdcb->fOutxCtsFlow = FALSE;
327 lpdcb->fOutxDsrFlow = FALSE;
328 lpdcb->fDtrControl = DTR_CONTROL_DISABLE;
331 lpdcb->fRtsControl = RTS_CONTROL_DISABLE;
337 SERIALUI_MakeBaudEnum(&lpdcb->BaudRate);
340 /***********************************************************************
341 * SERIALUI_ConfigDialogProc
343 * Shows a dialog for configuring a COMM port
345 INT_PTR CALLBACK SERIALUI_ConfigDialogProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
347 static const WCHAR szSettings[] = {
348 'S','e','t','t','i','n','g','s',' ','f','o','r',' ',0
351 SERIALUI_DialogInfo *info;
356 info = (SERIALUI_DialogInfo*) lParam;
359 SetWindowLongPtrW(hWnd, DWLP_USER, lParam);
360 strcpyW( szTitle, szSettings );
361 strcatW( szTitle, info->lpszDevice );
362 SetWindowTextW(hWnd, szTitle);
363 SERIALUI_DCBToDialogInfo(hWnd, info);
368 WORD wID = LOWORD(wParam);
370 info = (SERIALUI_DialogInfo *) GetWindowLongPtrW(hWnd, DWLP_USER);
376 SERIALUI_DialogInfoToDCB(hWnd,info);
382 /* test code for Get/SetDefaultCommConfig begins */
385 DWORD r,dwConfSize = sizeof (COMMCONFIG);
386 r = GetDefaultCommConfigW(info->lpszDevice,
387 info->lpCommConfig, &dwConfSize);
389 MessageBoxA(hWnd,"Failed","GetDefaultCommConfig",MB_OK);
391 SERIALUI_DCBToDialogInfo(hWnd, info);
396 SERIALUI_DialogInfoToDCB(hWnd,info);
397 r = SetDefaultCommConfigW(info->lpszDevice,
398 info->lpCommConfig, sizeof (COMMCONFIG));
400 MessageBoxA(hWnd,"Failed","GetDefaultCommConfig",MB_OK);
403 /* test code for Get/SetDefaultCommConfig ends */
411 static LPWSTR SERIALUI_strdup( LPCSTR str )
418 len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
419 strW = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
420 MultiByteToWideChar( CP_ACP, 0, str, -1, strW, len );
424 static VOID SERIALUI_strfree( LPWSTR strW )
426 HeapFree( GetProcessHeap(), 0, strW );
429 /***********************************************************************
430 * drvCommConfigDialogW (SERIALUI.@)
432 * Used by Win9x KERNEL to show a dialog for configuring a COMM port.
434 BOOL WINAPI drvCommConfigDialogW(
437 LPCOMMCONFIG lpCommConfig
439 SERIALUI_DialogInfo info;
441 info.lpCommConfig = lpCommConfig;
442 info.lpszDevice = lpszName;
443 info.bConvert = FALSE;
444 info.dwFlowControl = 0;
449 return DialogBoxParamW(SERIALUI_hModule,
450 MAKEINTRESOURCEW(IDD_SERIALUICONFIG),
452 SERIALUI_ConfigDialogProc,
456 /***********************************************************************
457 * drvCommConfigDialogA (SERIALUI.@)
459 BOOL WINAPI drvCommConfigDialogA(
460 LPCSTR lpszName, HWND hWndParent, LPCOMMCONFIG lpCommConfig )
462 LPWSTR strW = SERIALUI_strdup( lpszName );
463 BOOL r = drvCommConfigDialogW( strW, hWndParent, lpCommConfig );
464 SERIALUI_strfree( strW );
468 static const WCHAR lpszCommKey[] = {
469 'S','y','s','t','e','m','\\',
470 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
471 'S','e','r','v','i','c','e','s','\\',
472 'C','l','a','s','s','\\','P','o','r','t','s',0
474 static const WCHAR lpszDCB[] = {'D','C','B',0};
476 /***********************************************************************
477 * drvSetDefaultCommConfigW (SERIALUI.@)
479 * Used by Win98 KERNEL to set the default config for a COMM port
480 * FIXME: uses the wrong registry key... should use a digit, not
481 * the comm port name.
483 BOOL WINAPI drvSetDefaultCommConfigW(
484 LPCWSTR lpszDevice, LPCOMMCONFIG lpCommConfig, DWORD dwSize)
486 HKEY hKeyReg=0, hKeyPort=0;
487 WCHAR szKeyName[100];
489 static const WCHAR fmt[] = {'%','s','\\','%','s',0 };
491 TRACE("%p %p %x\n",lpszDevice,lpCommConfig,dwSize);
496 if(dwSize < sizeof (COMMCONFIG))
499 r = RegConnectRegistryW(NULL, HKEY_LOCAL_MACHINE, &hKeyReg);
500 if(r != ERROR_SUCCESS)
503 snprintfW(szKeyName, sizeof(szKeyName)/sizeof(WCHAR), fmt, lpszCommKey ,lpszDevice);
504 r = RegCreateKeyW(hKeyReg, szKeyName, &hKeyPort);
505 if(r == ERROR_SUCCESS)
507 dwDCBSize = sizeof (DCB);
508 r = RegSetValueExW( hKeyPort, lpszDCB, 0, REG_BINARY,
509 (LPBYTE)&lpCommConfig->dcb,dwDCBSize);
510 TRACE("write key r=%d\n",r);
511 RegCloseKey(hKeyPort);
514 RegCloseKey(hKeyReg);
516 return (r==ERROR_SUCCESS);
519 /***********************************************************************
520 * drvSetDefaultCommConfigA (SERIALUI.@)
522 BOOL WINAPI drvSetDefaultCommConfigA(
523 LPCSTR lpszDevice, LPCOMMCONFIG lpCommConfig, DWORD dwSize)
525 LPWSTR strW = SERIALUI_strdup( lpszDevice );
526 BOOL r = drvSetDefaultCommConfigW( strW, lpCommConfig, dwSize );
527 SERIALUI_strfree( strW );
531 /***********************************************************************
532 * drvGetDefaultCommConfigW (SERIALUI.@)
534 * Used by Win9x KERNEL to get the default config for a COMM port
535 * FIXME: uses the wrong registry key... should use a digit, not
536 * the comm port name.
538 BOOL WINAPI drvGetDefaultCommConfigW(
539 LPCWSTR lpszDevice, LPCOMMCONFIG lpCommConfig, LPDWORD lpdwSize)
541 HKEY hKeyReg, hKeyPort;
542 WCHAR szKeyName[100];
543 DWORD r,dwSize,dwType;
544 static const WCHAR fmt[] = {'%','s','\\','%','s',0 };
546 TRACE("%p %p %p\n",lpszDevice,lpCommConfig,lpdwSize);
554 if(*lpdwSize < sizeof (COMMCONFIG))
557 *lpdwSize = sizeof (COMMCONFIG);
558 memset(lpCommConfig, 0 , sizeof (COMMCONFIG));
559 lpCommConfig->dwSize = sizeof (COMMCONFIG);
560 lpCommConfig->wVersion = 1;
562 r = RegConnectRegistryW(NULL, HKEY_LOCAL_MACHINE, &hKeyReg);
563 if(r != ERROR_SUCCESS)
566 snprintfW(szKeyName, sizeof(szKeyName)/sizeof(WCHAR), fmt, lpszCommKey ,lpszDevice);
567 r = RegOpenKeyW(hKeyReg, szKeyName, &hKeyPort);
568 if(r == ERROR_SUCCESS)
570 dwSize = sizeof (DCB);
572 r = RegQueryValueExW( hKeyPort, lpszDCB, NULL,
573 &dwType, (LPBYTE)&lpCommConfig->dcb, &dwSize);
574 if ((r==ERROR_SUCCESS) && (dwType != REG_BINARY))
576 if ((r==ERROR_SUCCESS) && (dwSize != sizeof(DCB)))
579 RegCloseKey(hKeyPort);
583 /* FIXME: default to a hardcoded commconfig */
585 lpCommConfig->dcb.DCBlength = sizeof(DCB);
586 lpCommConfig->dcb.BaudRate = 9600;
587 lpCommConfig->dcb.fBinary = TRUE;
588 lpCommConfig->dcb.fParity = FALSE;
589 lpCommConfig->dcb.ByteSize = 8;
590 lpCommConfig->dcb.Parity = NOPARITY;
591 lpCommConfig->dcb.StopBits = ONESTOPBIT;
595 RegCloseKey(hKeyReg);
597 return (r==ERROR_SUCCESS);
600 /***********************************************************************
601 * drvGetDefaultCommConfigA (SERIALUI.@)
603 BOOL WINAPI drvGetDefaultCommConfigA(
604 LPCSTR lpszDevice, LPCOMMCONFIG lpCommConfig, LPDWORD lpdwSize)
606 LPWSTR strW = SERIALUI_strdup( lpszDevice );
607 BOOL r = drvGetDefaultCommConfigW( strW, lpCommConfig, lpdwSize );
608 SERIALUI_strfree( strW );