winepulse: Wrap unix call parameters in structs.
[wine.git] / dlls / serialui / confdlg.c
blob23c9eb68c2abd93e4351b25941b05461e6ce0730
1 /*
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
31 #include <string.h>
32 #include <stdarg.h>
33 #include <stdio.h>
35 #include "windef.h"
36 #include "winbase.h"
37 #include "winnls.h"
38 #include "winreg.h"
39 #include "wingdi.h"
40 #include "winuser.h"
41 #include "wine/debug.h"
42 #include "serialui.h"
43 #include "winerror.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(comm);
47 static HMODULE SERIALUI_hModule;
49 /***********************************************************************
50 * DllMain [Internal] Initializes the internal 'SERIALUI.DLL'.
52 * PARAMS
53 * hinstDLL [I] handle to the DLL's instance
54 * fdwReason [I]
55 * lpvReserved [I] reserved, must be NULL
57 * RETURNS
58 * Success: TRUE
59 * Failure: FALSE
62 BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
64 TRACE("%p,%x,%p\n", hinstDLL, fdwReason, lpvReserved);
66 switch (fdwReason) {
67 case DLL_PROCESS_ATTACH:
68 DisableThreadLibraryCalls(hinstDLL);
69 SERIALUI_hModule = hinstDLL;
70 break;
73 return TRUE;
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 %lx)\n",pdi,pfnAdd,lParam);
90 return FALSE;
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
99 DWORD val;
100 const CHAR *name;
101 } PARAM2STRDATA, *LPPARAM2STRDATA;
103 typedef struct tagPARAM2STR
105 DWORD dwSize;
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)
144 unsigned int i;
145 int n;
146 HWND hControl = GetDlgItem(hDlg,id);
148 if(!hControl)
149 return;
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)
167 DWORD i;
168 CHAR lpEntry[20];
169 HWND hControl = GetDlgItem(hDlg,id);
171 if( (!hControl) || (!lpdwVal))
173 TRACE("Couldn't get window handle for item %x\n",id);
174 return FALSE;
177 if(!GetWindowTextA(hControl, &lpEntry[0], sizeof(lpEntry)))
179 TRACE("Couldn't get window text for item %x\n",id);
180 return FALSE;
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;
189 return TRUE;
193 return FALSE;
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)
210 unsigned int i;
212 for(i=0; i<ARRAY_SIZE(SERIALUI_BaudConvertTable); i+=2)
214 if(*lpdwBaudRate == SERIALUI_BaudConvertTable[i])
216 *lpdwBaudRate = SERIALUI_BaudConvertTable[i+1];
217 return TRUE;
220 return FALSE;
223 static BOOL SERIALUI_MakeBaudEnum(LPDWORD lpdwBaudRate)
225 unsigned int i;
227 for(i=0; i<ARRAY_SIZE(SERIALUI_BaudConvertTable); i+=2)
229 if(*lpdwBaudRate == SERIALUI_BaudConvertTable[i+1])
231 *lpdwBaudRate = SERIALUI_BaudConvertTable[i];
232 return TRUE;
235 return FALSE;
238 typedef struct tagSERIALUI_DialogInfo
240 LPCWSTR lpszDevice;
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)) {
260 dwFlowControl = 1;
261 } else if(lpdcb->fOutX || lpdcb->fInX) {
262 dwFlowControl = 2;
263 } else {
264 dwFlowControl = 0;
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=%d stop=%d parity=%d data=%d flow=%d\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)
302 case 0:
303 lpdcb->fOutxCtsFlow = FALSE;
304 lpdcb->fOutxDsrFlow = FALSE;
305 lpdcb->fDtrControl = DTR_CONTROL_DISABLE;
306 lpdcb->fOutX = FALSE;
307 lpdcb->fInX = FALSE;
308 lpdcb->fRtsControl = RTS_CONTROL_DISABLE;
309 break;
310 case 1: /* CTS/RTS */
311 lpdcb->fOutxCtsFlow = TRUE;
312 lpdcb->fOutxDsrFlow = FALSE;
313 lpdcb->fDtrControl = DTR_CONTROL_DISABLE;
314 lpdcb->fOutX = FALSE;
315 lpdcb->fInX = FALSE;
316 lpdcb->fRtsControl = RTS_CONTROL_HANDSHAKE;
317 break;
318 case 2:
319 lpdcb->fOutxCtsFlow = FALSE;
320 lpdcb->fOutxDsrFlow = FALSE;
321 lpdcb->fDtrControl = DTR_CONTROL_DISABLE;
322 lpdcb->fOutX = TRUE;
323 lpdcb->fInX = TRUE;
324 lpdcb->fRtsControl = RTS_CONTROL_DISABLE;
325 break;
329 if(info->bConvert)
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;
343 switch (uMsg)
345 case WM_INITDIALOG:
346 info = (SERIALUI_DialogInfo*) lParam;
347 if(!info)
348 return FALSE;
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);
354 return TRUE;
356 case WM_COMMAND:
358 WORD wID = LOWORD(wParam);
360 info = (SERIALUI_DialogInfo *) GetWindowLongPtrW(hWnd, DWLP_USER);
361 if(!info)
362 EndDialog(hWnd,0);
363 switch (wID)
365 case IDOK:
366 SERIALUI_DialogInfoToDCB(hWnd,info);
367 EndDialog(hWnd, ERROR_SUCCESS);
368 return TRUE;
369 case IDCANCEL:
370 EndDialog(hWnd, ERROR_CANCELLED);
371 return TRUE;
372 /* test code for Get/SetDefaultCommConfig begins */
373 case ID_GETDEFAULT:
375 DWORD r,dwConfSize = sizeof (COMMCONFIG);
376 r = GetDefaultCommConfigW(info->lpszDevice,
377 info->lpCommConfig, &dwConfSize);
378 if(!r)
379 MessageBoxA(hWnd,"Failed","GetDefaultCommConfig",MB_OK);
381 SERIALUI_DCBToDialogInfo(hWnd, info);
382 break;
383 case ID_SETDEFAULT:
385 DWORD r;
386 SERIALUI_DialogInfoToDCB(hWnd,info);
387 r = SetDefaultCommConfigW(info->lpszDevice,
388 info->lpCommConfig, sizeof (COMMCONFIG));
389 if(!r)
390 MessageBoxA(hWnd,"Failed","GetDefaultCommConfig",MB_OK);
392 break;
393 /* test code for Get/SetDefaultCommConfig ends */
396 default:
397 return FALSE;
401 static LPWSTR SERIALUI_strdup( LPCSTR str )
403 DWORD len;
404 LPWSTR strW;
406 if (!str)
407 return NULL;
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 );
411 return strW;
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;
428 INT res;
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;
441 if (!lpszName[0])
442 return ERROR_BADKEY;
444 res = DialogBoxParamW( SERIALUI_hModule,
445 MAKEINTRESOURCEW(IDD_SERIALUICONFIG),
446 hWndParent,
447 SERIALUI_ConfigDialogProc,
448 (LPARAM)&info);
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 );
461 return r;
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];
478 DWORD r,dwDCBSize;
480 TRACE("%p %p %x\n",lpszDevice,lpCommConfig,dwSize);
482 if(!lpCommConfig)
483 return FALSE;
485 if(dwSize < sizeof (COMMCONFIG))
486 return FALSE;
488 r = RegConnectRegistryW(NULL, HKEY_LOCAL_MACHINE, &hKeyReg);
489 if(r != ERROR_SUCCESS)
490 return FALSE;
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=%d\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 );
516 return r;
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: %u\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 */
547 r--;
549 if (lstrcmpiW(szKeyName, L"com") ||
550 (lpszDevice[r] < '1') || (lpszDevice[r] > '9') || lpszDevice[r+1]) {
551 return ERROR_BADKEY;
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);
568 dwType = 0;
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;
578 else
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);
593 return r;
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 );
605 return r;