Fix some types and function prototypes according to Platform SDK
[wine.git] / dlls / serialui / confdlg.c
blob35732b9195917cf38cc942e7e4730853bf6dabe5
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 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include "config.h"
32 #include "wine/port.h"
34 #include <string.h>
35 #include <stdio.h>
37 #include "winbase.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 HMODULE SERIALUI_hModule = 0;
49 /***********************************************************************
50 * SERIALUI_LibMain [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
63 SERIALUI_LibMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
65 TRACE("%p,%lx,%p\n", hinstDLL, fdwReason, lpvReserved);
67 switch (fdwReason) {
68 case DLL_PROCESS_ATTACH:
69 SERIALUI_hModule = hinstDLL;
70 break;
71 case DLL_PROCESS_DETACH:
72 break;
75 return TRUE;
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 SERIALUI_EnumPropPages(LPDEVICE_INFO pdi, LPFNADDPROPSHEETPAGE pfnAdd, LPARAM lParam )
91 FIXME("(%p %p %lx)\n",pdi,pfnAdd,lParam);
92 return FALSE;
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
101 DWORD val;
102 CONST CHAR *name;
103 } PARAM2STRDATA, *LPPARAM2STRDATA;
105 typedef struct tagPARAM2STR
107 DWORD dwSize;
108 LPPARAM2STRDATA data;
109 } PARAM2STR, *LPPARAM2STR;
110 typedef const LPPARAM2STR LPCPARAM2STR;
112 #define SERIALUI_TABLESIZE(x) ((sizeof (x))/(sizeof (x[0])))
114 static PARAM2STRDATA SERIALUI_Baud2StrData[]={
115 {110, "110"}, {300, "300"}, {600, "600"}, {1200, "1200"},
116 {2400, "2400"}, {4800, "4800"}, {9600, "9600"}, {14400, "14400"},
117 {19200, "19200"}, {38400L, "38400"}, {56000L, "56000"}, {57600L, "57600"},
118 {115200L, "115200"}, {128000L, "128000"}, {256000L, "256000"}
120 static PARAM2STR SERIALUI_Baud2Str={ SERIALUI_TABLESIZE(SERIALUI_Baud2StrData),SERIALUI_Baud2StrData };
122 static PARAM2STRDATA SERIALUI_Parity2StrData[]={
123 {NOPARITY,"None"}, {ODDPARITY,"Odd"}, {EVENPARITY,"Even"}, {MARKPARITY,"Mark"},
124 {SPACEPARITY,"Space"}
126 static PARAM2STR SERIALUI_Parity2Str={ SERIALUI_TABLESIZE(SERIALUI_Parity2StrData),SERIALUI_Parity2StrData };
128 static PARAM2STRDATA SERIALUI_Stop2StrData[]={
129 {ONESTOPBIT,"1"}, {ONE5STOPBITS,"1.5"}, {TWOSTOPBITS,"2"}
131 static PARAM2STR SERIALUI_Stop2Str={ SERIALUI_TABLESIZE(SERIALUI_Stop2StrData),SERIALUI_Stop2StrData };
133 static PARAM2STRDATA SERIALUI_Data2StrData[]={
134 {5,"5"}, {6,"6"}, {7,"7"}, {8, "8"}, {16,"16"}
136 static PARAM2STR SERIALUI_Data2Str={ SERIALUI_TABLESIZE(SERIALUI_Data2StrData),SERIALUI_Data2StrData };
138 static PARAM2STRDATA SERIALUI_Flow2StrData[]={
139 {0,"None"}, {1,"Hardware (RTS/CTS)"}, {2,"Software (XON/XOFF)"}
141 static PARAM2STR SERIALUI_Flow2Str={ SERIALUI_TABLESIZE(SERIALUI_Flow2StrData),SERIALUI_Flow2StrData };
144 * Add all the fields to a combo box and highlight the current value
146 static void SERIALUI_AddConfItems(HWND hDlg, DWORD id, LPCPARAM2STR table, DWORD dwVal)
148 int i,n;
149 HWND hControl = GetDlgItem(hDlg,id);
151 if(!hControl)
152 return;
154 for(i=0; i<table->dwSize; i++)
156 n = SendMessageA(hControl, CB_ADDSTRING, 0L, (LPARAM)table->data[i].name);
157 if(dwVal == table->data[i].val)
159 SendMessageA(hControl, CB_SETCURSEL, (WPARAM)n, (LPARAM)0);
165 * Get the current sellection of the given combo box and set a DCB field to
166 * the value matching that selection.
168 static BOOL SERIALUI_GetConfItems(HWND hDlg, DWORD id, LPCPARAM2STR table, LPDWORD lpdwVal)
170 DWORD i;
171 CHAR lpEntry[20];
172 HWND hControl = GetDlgItem(hDlg,id);
174 if( (!hControl) || (!lpdwVal))
176 TRACE("Couldn't get window handle for item %lx\n",id);
177 return FALSE;
180 if(!GetWindowTextA(hControl, &lpEntry[0], sizeof lpEntry))
182 TRACE("Couldn't get window text for item %lx\n",id);
183 return FALSE;
185 /* TRACE("%ld contains %s\n",id, lpEntry); */
187 for(i=0; i<table->dwSize; i++)
189 if(!lstrcmpA(table->data[i].name,lpEntry))
191 *lpdwVal = table->data[i].val;
192 return TRUE;
196 return FALSE;
200 * Both the enumerated values CBR_XXXX and integer baud rates are valid
201 * dcb.BaudRate. This code is to convert back and forth between CBR_ style
202 * and integers. The dialog box uses integer values.
204 static DWORD SERIALUI_BaudConvertTable[] = {
205 CBR_110, 110, CBR_300, 300, CBR_600, 600, CBR_1200, 1200,
206 CBR_2400, 2400, CBR_4800, 4800, CBR_9600, 9600, CBR_14400, 14400,
207 CBR_19200, 19200, CBR_38400, 38400, CBR_56000, 56000, CBR_57600, 57600,
208 CBR_115200, 115200, CBR_128000, 128000, CBR_256000, 256000
211 static BOOL SERIALUI_MakeBaudDword(LPDWORD lpdwBaudRate)
213 int i;
215 for(i=0; i<(sizeof(SERIALUI_BaudConvertTable)/sizeof(DWORD)); i+=2)
217 if(*lpdwBaudRate == SERIALUI_BaudConvertTable[i])
219 *lpdwBaudRate = SERIALUI_BaudConvertTable[i+1];
220 return TRUE;
223 return FALSE;
226 static BOOL SERIALUI_MakeBaudEnum(LPDWORD lpdwBaudRate)
228 int i;
230 for(i=0; i<(sizeof(SERIALUI_BaudConvertTable)/sizeof(DWORD)); i+=2)
232 if(*lpdwBaudRate == SERIALUI_BaudConvertTable[i+1])
234 *lpdwBaudRate = SERIALUI_BaudConvertTable[i];
235 return TRUE;
238 return FALSE;
241 typedef struct tagSERIALUI_DialogInfo
243 LPCSTR lpszDevice;
244 LPCOMMCONFIG lpCommConfig;
245 BOOL bConvert; /* baud rate was converted to a DWORD */
246 DWORD dwFlowControl; /* old flow control */
247 } SERIALUI_DialogInfo;
249 static void SERIALUI_DCBToDialogInfo(HWND hDlg, SERIALUI_DialogInfo *info)
251 DWORD dwBaudRate, dwStopBits, dwParity, dwByteSize, dwFlowControl;
252 LPDCB lpdcb = &info->lpCommConfig->dcb;
254 /* pass integer pointers to SERIALUI_ dialog config fns */
255 dwBaudRate = lpdcb->BaudRate;
256 dwStopBits = lpdcb->StopBits;
257 dwParity = lpdcb->Parity;
258 dwByteSize = lpdcb->ByteSize;
260 /* map flow control state, if it looks normal */
261 if((lpdcb->fRtsControl == RTS_CONTROL_HANDSHAKE) ||
262 (lpdcb->fOutxCtsFlow == TRUE)) {
263 dwFlowControl = 1;
264 } else if(lpdcb->fOutX || lpdcb->fInX) {
265 dwFlowControl = 2;
266 } else {
267 dwFlowControl = 0;
270 info->bConvert = SERIALUI_MakeBaudDword(&dwBaudRate);
272 SERIALUI_AddConfItems( hDlg, IDC_BAUD, &SERIALUI_Baud2Str ,dwBaudRate);
273 SERIALUI_AddConfItems( hDlg, IDC_STOP, &SERIALUI_Stop2Str ,dwStopBits);
274 SERIALUI_AddConfItems( hDlg, IDC_PARITY, &SERIALUI_Parity2Str ,dwParity);
275 SERIALUI_AddConfItems( hDlg, IDC_DATA, &SERIALUI_Data2Str ,dwByteSize);
276 SERIALUI_AddConfItems( hDlg, IDC_FLOW, &SERIALUI_Flow2Str, dwFlowControl );
278 info->dwFlowControl = dwFlowControl;
281 static void SERIALUI_DialogInfoToDCB(HWND hDlg, SERIALUI_DialogInfo *info)
283 DWORD dwBaudRate, dwStopBits, dwParity, dwByteSize, dwFlowControl;
284 LPDCB lpdcb = &info->lpCommConfig->dcb;
286 SERIALUI_GetConfItems( hDlg, IDC_BAUD, &SERIALUI_Baud2Str, &dwBaudRate);
287 SERIALUI_GetConfItems( hDlg, IDC_STOP, &SERIALUI_Stop2Str, &dwStopBits);
288 SERIALUI_GetConfItems( hDlg, IDC_PARITY, &SERIALUI_Parity2Str, &dwParity);
289 SERIALUI_GetConfItems( hDlg, IDC_DATA, &SERIALUI_Data2Str, &dwByteSize);
290 SERIALUI_GetConfItems( hDlg, IDC_FLOW, &SERIALUI_Flow2Str, &dwFlowControl );
292 TRACE("baud=%ld stop=%ld parity=%ld data=%ld flow=%ld\n",
293 dwBaudRate, dwStopBits, dwParity, dwByteSize, dwFlowControl);
295 lpdcb->BaudRate = dwBaudRate;
296 lpdcb->StopBits = dwStopBits;
297 lpdcb->Parity = dwParity;
298 lpdcb->ByteSize = dwByteSize;
300 /* try not to change flow control if the user didn't change it */
301 if(info->dwFlowControl != dwFlowControl)
303 switch(dwFlowControl)
305 case 0:
306 lpdcb->fOutxCtsFlow = FALSE;
307 lpdcb->fOutxDsrFlow = FALSE;
308 lpdcb->fDtrControl = DTR_CONTROL_DISABLE;
309 lpdcb->fOutX = FALSE;
310 lpdcb->fInX = FALSE;
311 lpdcb->fRtsControl = RTS_CONTROL_DISABLE;
312 break;
313 case 1: /* CTS/RTS */
314 lpdcb->fOutxCtsFlow = TRUE;
315 lpdcb->fOutxDsrFlow = FALSE;
316 lpdcb->fDtrControl = DTR_CONTROL_DISABLE;
317 lpdcb->fOutX = FALSE;
318 lpdcb->fInX = FALSE;
319 lpdcb->fRtsControl = RTS_CONTROL_HANDSHAKE;
320 break;
321 case 2:
322 lpdcb->fOutxCtsFlow = FALSE;
323 lpdcb->fOutxDsrFlow = FALSE;
324 lpdcb->fDtrControl = DTR_CONTROL_DISABLE;
325 lpdcb->fOutX = TRUE;
326 lpdcb->fInX = TRUE;
327 lpdcb->fRtsControl = RTS_CONTROL_DISABLE;
328 break;
332 if(info->bConvert)
333 SERIALUI_MakeBaudEnum(&lpdcb->BaudRate);
336 /***********************************************************************
337 * SERIALUI_ConfigDialogProc
339 * Shows a dialog for configuring a COMM port
341 INT_PTR CALLBACK SERIALUI_ConfigDialogProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
343 CHAR szTitle[30];
344 SERIALUI_DialogInfo *info;
346 switch (uMsg)
348 case WM_INITDIALOG:
349 info = (SERIALUI_DialogInfo*) lParam;
350 if(!info)
351 return FALSE;
352 SetWindowLongA(hWnd, DWL_USER, lParam);
353 snprintf(szTitle, sizeof szTitle, "Settings for %s", info->lpszDevice);
354 SetWindowTextA(hWnd, szTitle);
355 SERIALUI_DCBToDialogInfo(hWnd, info);
356 return TRUE;
358 case WM_COMMAND:
360 WORD wID = LOWORD(wParam);
362 info = (SERIALUI_DialogInfo *) GetWindowLongA(hWnd, DWL_USER);
363 if(!info)
364 EndDialog(hWnd,0);
365 switch (wID)
367 case IDOK:
368 SERIALUI_DialogInfoToDCB(hWnd,info);
369 EndDialog(hWnd,1);
370 return TRUE;
371 case IDCANCEL:
372 EndDialog(hWnd,0);
373 return TRUE;
374 /* test code for Get/SetDefaultCommConfig begins */
375 case ID_GETDEFAULT:
377 DWORD r,dwConfSize = sizeof (COMMCONFIG);
378 r = GetDefaultCommConfigA(info->lpszDevice,
379 info->lpCommConfig, &dwConfSize);
380 if(!r)
381 MessageBoxA(hWnd,"Failed","GetDefaultCommConfig",MB_OK);
383 SERIALUI_DCBToDialogInfo(hWnd, info);
384 break;
385 case ID_SETDEFAULT:
387 DWORD r;
388 SERIALUI_DialogInfoToDCB(hWnd,info);
389 r = SetDefaultCommConfigA(info->lpszDevice,
390 info->lpCommConfig, sizeof (COMMCONFIG));
391 if(!r)
392 MessageBoxA(hWnd,"Failed","GetDefaultCommConfig",MB_OK);
394 break;
395 /* test code for Get/SetDefaultCommConfig ends */
398 default:
399 return FALSE;
403 /***********************************************************************
404 * drvCommConfigDialog (SERIALUI.3)
406 * Used by Win9x KERNEL to show a dialog for configuring a COMM port.
408 BOOL WINAPI SERIALUI_CommConfigDialog(
409 LPCSTR lpszName,
410 HWND hWndParent,
411 LPCOMMCONFIG lpCommConfig
413 SERIALUI_DialogInfo info;
415 info.lpCommConfig = lpCommConfig;
416 info.lpszDevice = lpszName;
417 info.bConvert = FALSE;
418 info.dwFlowControl = 0;
420 if(!lpCommConfig)
421 return FALSE;
423 return DialogBoxParamA(SERIALUI_hModule,
424 MAKEINTRESOURCEA(IDD_SERIALUICONFIG),
425 hWndParent,
426 SERIALUI_ConfigDialogProc,
427 (LPARAM)&info);
430 static LPCSTR lpszCommKey = "System\\CurrentControlSet\\Services\\Class\\Ports";
431 static LPCSTR lpszDCB = "DCB";
433 /***********************************************************************
434 * drvSetDefaultCommConfig (SERIALUI.4)
436 * Used by Win98 KERNEL to set the default config for a COMM port
437 * FIXME: uses the wrong registry key... should use a digit, not
438 * the comm port name.
440 BOOL WINAPI SERIALUI_SetDefaultCommConfig(
441 LPCSTR lpszDevice,
442 LPCOMMCONFIG lpCommConfig,
443 DWORD dwSize
445 HKEY hKeyReg=0, hKeyPort=0;
446 CHAR szKeyName[100];
447 DWORD r,dwDCBSize;
449 TRACE("%p %p %lx\n",lpszDevice,lpCommConfig,dwSize);
451 if(!lpCommConfig)
452 return FALSE;
454 if(dwSize < sizeof (COMMCONFIG))
455 return FALSE;
457 r = RegConnectRegistryA(NULL, HKEY_LOCAL_MACHINE, &hKeyReg);
458 if(r != ERROR_SUCCESS)
459 return FALSE;
461 snprintf(szKeyName, sizeof szKeyName, "%s\\%s", lpszCommKey ,lpszDevice);
462 r = RegCreateKeyA(hKeyReg, szKeyName, &hKeyPort);
463 if(r == ERROR_SUCCESS)
465 dwDCBSize = sizeof (DCB);
466 r = RegSetValueExA( hKeyPort, lpszDCB, 0, REG_BINARY,
467 (LPSTR)&lpCommConfig->dcb,dwDCBSize);
468 TRACE("write key r=%ld\n",r);
469 RegCloseKey(hKeyPort);
472 RegCloseKey(hKeyReg);
474 return (r==ERROR_SUCCESS);
477 /***********************************************************************
478 * drvGetDefaultCommConfig (SERIALUI.5)
480 * Used by Win9x KERNEL to get 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 SERIALUI_GetDefaultCommConfig(
485 LPCSTR lpszDevice,
486 LPCOMMCONFIG lpCommConfig,
487 LPDWORD lpdwSize
489 HKEY hKeyReg, hKeyPort;
490 CHAR szKeyName[100];
491 DWORD r,dwSize,dwType;
493 TRACE("%p %p %p\n",lpszDevice,lpCommConfig,lpdwSize);
495 if(!lpCommConfig)
496 return FALSE;
498 if(!lpdwSize)
499 return FALSE;
501 if(*lpdwSize < sizeof (COMMCONFIG))
502 return FALSE;
504 *lpdwSize = sizeof (COMMCONFIG);
505 memset(lpCommConfig, 0 , sizeof (COMMCONFIG));
506 lpCommConfig->dwSize = sizeof (COMMCONFIG);
507 lpCommConfig->wVersion = 1;
509 r = RegConnectRegistryA(NULL, HKEY_LOCAL_MACHINE, &hKeyReg);
510 if(r != ERROR_SUCCESS)
511 return FALSE;
513 snprintf(szKeyName, sizeof szKeyName, "%s\\%s", lpszCommKey ,lpszDevice);
514 r = RegOpenKeyA(hKeyReg, szKeyName, &hKeyPort);
515 if(r == ERROR_SUCCESS)
517 dwSize = sizeof (DCB);
518 dwType = 0;
519 r = RegQueryValueExA( hKeyPort, lpszDCB, NULL,
520 &dwType, (LPSTR)&lpCommConfig->dcb,&dwSize);
521 if ((r==ERROR_SUCCESS) && (dwType != REG_BINARY))
522 r = 1;
523 if ((r==ERROR_SUCCESS) && (dwSize != sizeof(DCB)))
524 r = 1;
526 RegCloseKey(hKeyPort);
528 else
530 /* FIXME: default to a hardcoded commconfig */
532 lpCommConfig->dcb.DCBlength = sizeof(DCB);
533 lpCommConfig->dcb.BaudRate = 9600;
534 lpCommConfig->dcb.fBinary = TRUE;
535 lpCommConfig->dcb.fParity = FALSE;
536 lpCommConfig->dcb.ByteSize = 8;
537 lpCommConfig->dcb.Parity = NOPARITY;
538 lpCommConfig->dcb.StopBits = ONESTOPBIT;
539 return TRUE;
542 RegCloseKey(hKeyReg);
544 return (r==ERROR_SUCCESS);