- Better implementation for GetTopindex/GetOrigin/SetIconSpacing.
[wine] / dlls / serialui / confdlg.c
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
6  *
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.
11  *
12  * Basic structure copied from COMCTL32 code.
13  *
14  * Copyright 2000 Mike McCormack
15  *
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.
20  *
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.
25  *
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
29  */
30
31 #include "config.h"
32 #include "wine/port.h"
33
34 #include <string.h>
35 #include <stdio.h>
36
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"
44
45 WINE_DEFAULT_DEBUG_CHANNEL(comm);
46
47 HMODULE SERIALUI_hModule = 0;
48
49 /***********************************************************************
50  * SERIALUI_LibMain [Internal] Initializes the internal 'SERIALUI.DLL'.
51  *
52  * PARAMS
53  *     hinstDLL    [I] handle to the DLL's instance
54  *     fdwReason   [I]
55  *     lpvReserved [I] reserved, must be NULL
56  *
57  * RETURNS
58  *     Success: TRUE
59  *     Failure: FALSE
60  */
61
62 BOOL WINAPI
63 SERIALUI_LibMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
64 {
65     TRACE("%x,%lx,%p\n", hinstDLL, fdwReason, lpvReserved);
66
67     switch (fdwReason) {
68         case DLL_PROCESS_ATTACH:
69             SERIALUI_hModule = hinstDLL;
70             break;
71         case DLL_PROCESS_DETACH:
72             break;
73     }
74
75     return TRUE;
76 }
77
78
79 /***********************************************************************
80  * EnumPropPages (SERIALUI.2)
81  *
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"
86  */
87 typedef LPVOID LPDEVICE_INFO;
88 typedef LPVOID LPFNADDPROPSHEETPAGE;
89 BOOL WINAPI SERIALUI_EnumPropPages(LPDEVICE_INFO pdi, LPFNADDPROPSHEETPAGE pfnAdd, LPARAM lParam )
90 {
91     FIXME("(%p %p %lx)\n",pdi,pfnAdd,lParam);
92     return FALSE;
93 }
94
95 /*
96  * These data structures are convert from values used in fields of a DCB
97  * to strings used in the CommConfigDialog.
98  */
99 typedef struct tagPARAM2STRDATA
100 {
101     DWORD        val;
102     CONST CHAR  *name;
103 } PARAM2STRDATA, *LPPARAM2STRDATA;
104
105 typedef struct tagPARAM2STR
106 {
107     DWORD         dwSize;
108     LPPARAM2STRDATA data;
109 } PARAM2STR, *LPPARAM2STR;
110 typedef const LPPARAM2STR LPCPARAM2STR;
111
112 #define SERIALUI_TABLESIZE(x) ((sizeof (x))/(sizeof (x[0])))
113
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"}
119 };
120 static PARAM2STR SERIALUI_Baud2Str={ SERIALUI_TABLESIZE(SERIALUI_Baud2StrData),SERIALUI_Baud2StrData };
121
122 static PARAM2STRDATA SERIALUI_Parity2StrData[]={
123   {NOPARITY,"None"}, {ODDPARITY,"Odd"}, {EVENPARITY,"Even"}, {MARKPARITY,"Mark"},
124   {SPACEPARITY,"Space"}
125 };
126 static PARAM2STR SERIALUI_Parity2Str={ SERIALUI_TABLESIZE(SERIALUI_Parity2StrData),SERIALUI_Parity2StrData };
127
128 static PARAM2STRDATA SERIALUI_Stop2StrData[]={
129   {ONESTOPBIT,"1"}, {ONE5STOPBITS,"1.5"}, {TWOSTOPBITS,"2"}
130 };
131 static PARAM2STR SERIALUI_Stop2Str={ SERIALUI_TABLESIZE(SERIALUI_Stop2StrData),SERIALUI_Stop2StrData };
132
133 static PARAM2STRDATA SERIALUI_Data2StrData[]={
134   {5,"5"}, {6,"6"}, {7,"7"}, {8, "8"}, {16,"16"}
135 };
136 static PARAM2STR SERIALUI_Data2Str={ SERIALUI_TABLESIZE(SERIALUI_Data2StrData),SERIALUI_Data2StrData };
137
138 static PARAM2STRDATA SERIALUI_Flow2StrData[]={
139   {0,"None"}, {1,"Hardware (RTS/CTS)"}, {2,"Software (XON/XOFF)"}
140 };
141 static PARAM2STR SERIALUI_Flow2Str={ SERIALUI_TABLESIZE(SERIALUI_Flow2StrData),SERIALUI_Flow2StrData };
142
143 /*
144  * Add all the fields to a combo box and highlight the current value
145  */
146 static void SERIALUI_AddConfItems(HWND hDlg, DWORD id, LPCPARAM2STR table, DWORD dwVal)
147 {
148     int i,n;
149     HWND hControl = GetDlgItem(hDlg,id);
150
151     if(!hControl)
152         return;
153
154     for(i=0; i<table->dwSize; i++)
155     {
156         n = SendMessageA(hControl, CB_ADDSTRING, 0L, (LPARAM)table->data[i].name);
157         if(dwVal == table->data[i].val)
158         {
159             SendMessageA(hControl, CB_SETCURSEL, (WPARAM)n, (LPARAM)0);
160         }
161     }
162 }
163
164 /*
165  * Get the current sellection of the given combo box and set a DCB field to
166  * the value matching that selection.
167  */
168 static BOOL SERIALUI_GetConfItems(HWND hDlg, DWORD id, LPCPARAM2STR table, LPDWORD lpdwVal)
169 {
170     DWORD i;
171     CHAR lpEntry[20];
172     HWND hControl = GetDlgItem(hDlg,id);
173
174     if( (!hControl) || (!lpdwVal))
175     {
176         TRACE("Couldn't get window handle for item %lx\n",id);
177         return FALSE;
178     }
179
180     if(!GetWindowTextA(hControl, &lpEntry[0], sizeof lpEntry))
181     {
182         TRACE("Couldn't get window text for item %lx\n",id);
183         return FALSE;
184     }
185     /* TRACE("%ld contains %s\n",id, lpEntry); */
186
187     for(i=0; i<table->dwSize; i++)
188     {
189         if(!lstrcmpA(table->data[i].name,lpEntry))
190         {
191             *lpdwVal = table->data[i].val;
192             return TRUE;
193         }
194     }
195
196     return FALSE;
197 }
198
199 /*
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.
203  */
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
209 };
210
211 static BOOL SERIALUI_MakeBaudDword(LPDWORD lpdwBaudRate)
212 {
213     int i;
214
215     for(i=0; i<(sizeof(SERIALUI_BaudConvertTable)/sizeof(DWORD)); i+=2)
216     {
217         if(*lpdwBaudRate == SERIALUI_BaudConvertTable[i])
218         {
219             *lpdwBaudRate = SERIALUI_BaudConvertTable[i+1];
220             return TRUE;
221         }
222     }
223     return FALSE;
224 }
225
226 static BOOL SERIALUI_MakeBaudEnum(LPDWORD lpdwBaudRate)
227 {
228     int i;
229
230     for(i=0; i<(sizeof(SERIALUI_BaudConvertTable)/sizeof(DWORD)); i+=2)
231     {
232         if(*lpdwBaudRate == SERIALUI_BaudConvertTable[i+1])
233         {
234             *lpdwBaudRate = SERIALUI_BaudConvertTable[i];
235             return TRUE;
236         }
237     }
238     return FALSE;
239 }
240
241 typedef struct tagSERIALUI_DialogInfo
242 {
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;
248
249 static void SERIALUI_DCBToDialogInfo(HWND hDlg, SERIALUI_DialogInfo *info)
250 {
251     DWORD dwBaudRate, dwStopBits, dwParity, dwByteSize, dwFlowControl;
252     LPDCB lpdcb = &info->lpCommConfig->dcb;
253
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;
259
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;
268     }
269
270     info->bConvert = SERIALUI_MakeBaudDword(&dwBaudRate);
271
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 );
277
278     info->dwFlowControl = dwFlowControl;
279 }
280
281 static void SERIALUI_DialogInfoToDCB(HWND hDlg, SERIALUI_DialogInfo *info)
282 {
283     DWORD dwBaudRate, dwStopBits, dwParity, dwByteSize, dwFlowControl;
284     LPDCB lpdcb = &info->lpCommConfig->dcb;
285
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 );
291
292     TRACE("baud=%ld stop=%ld parity=%ld data=%ld flow=%ld\n",
293           dwBaudRate, dwStopBits, dwParity, dwByteSize, dwFlowControl);
294
295     lpdcb->BaudRate = dwBaudRate;
296     lpdcb->StopBits = dwStopBits;
297     lpdcb->Parity   = dwParity;
298     lpdcb->ByteSize = dwByteSize;
299
300     /* try not to change flow control if the user didn't change it */
301     if(info->dwFlowControl != dwFlowControl)
302     {
303         switch(dwFlowControl)
304         {
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;
329         }
330     }
331
332     if(info->bConvert)
333         SERIALUI_MakeBaudEnum(&lpdcb->BaudRate);
334 }
335
336 /***********************************************************************
337  * SERIALUI_ConfigDialogProc
338  *
339  * Shows a dialog for configuring a COMM port
340  */
341 BOOL WINAPI SERIALUI_ConfigDialogProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
342 {
343     CHAR szTitle[30];
344     SERIALUI_DialogInfo *info;
345
346     switch (uMsg)
347     {
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;
357
358     case WM_COMMAND:
359     {
360         WORD wID = LOWORD(wParam);
361
362         info = (SERIALUI_DialogInfo *) GetWindowLongA(hWnd, DWL_USER);
363         if(!info)
364             EndDialog(hWnd,0);
365         switch (wID)
366         {
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:
376             {
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);
382             }
383             SERIALUI_DCBToDialogInfo(hWnd, info);
384             break;
385         case ID_SETDEFAULT:
386             {
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);
393             }
394             break;
395 /* test code for Get/SetDefaultCommConfig ends */
396         }
397     }
398     default:
399         return FALSE;
400     }
401 }
402
403 /***********************************************************************
404  * drvCommConfigDialog (SERIALUI.3)
405  *
406  * Used by Win9x KERNEL to show a dialog for configuring a COMM port.
407  */
408 BOOL WINAPI SERIALUI_CommConfigDialog(
409         LPCSTR lpszName,
410         HWND hWndParent,
411         LPCOMMCONFIG lpCommConfig
412 ) {
413     SERIALUI_DialogInfo info;
414
415     info.lpCommConfig  = lpCommConfig;
416     info.lpszDevice    = lpszName;
417     info.bConvert      = FALSE;
418     info.dwFlowControl = 0;
419
420     if(!lpCommConfig)
421         return FALSE;
422
423     return DialogBoxParamA(SERIALUI_hModule,
424                            MAKEINTRESOURCEA(IDD_SERIALUICONFIG),
425                            hWndParent,
426                            (DLGPROC) SERIALUI_ConfigDialogProc,
427                            (LPARAM)&info);
428 }
429
430 static LPCSTR lpszCommKey = "System\\CurrentControlSet\\Services\\Class\\Ports";
431 static LPCSTR lpszDCB     = "DCB";
432
433 /***********************************************************************
434  * drvSetDefaultCommConfig (SERIALUI.4)
435  *
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.
439  */
440 BOOL WINAPI SERIALUI_SetDefaultCommConfig(
441         LPCSTR lpszDevice,
442         LPCOMMCONFIG lpCommConfig,
443         DWORD dwSize
444 ) {
445     HKEY hKeyReg=0, hKeyPort=0;
446     CHAR szKeyName[100];
447     DWORD r,dwDCBSize;
448
449     TRACE("%p %p %lx\n",lpszDevice,lpCommConfig,dwSize);
450
451     if(!lpCommConfig)
452         return FALSE;
453
454     if(dwSize < sizeof (COMMCONFIG))
455         return FALSE;
456
457     r = RegConnectRegistryA(NULL, HKEY_LOCAL_MACHINE, &hKeyReg);
458     if(r != ERROR_SUCCESS)
459         return FALSE;
460
461     snprintf(szKeyName, sizeof szKeyName, "%s\\%s", lpszCommKey ,lpszDevice);
462     r = RegCreateKeyA(hKeyReg, szKeyName, &hKeyPort);
463     if(r == ERROR_SUCCESS)
464     {
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);
470     }
471
472     RegCloseKey(hKeyReg);
473
474     return (r==ERROR_SUCCESS);
475 }
476
477 /***********************************************************************
478  * drvGetDefaultCommConfig (SERIALUI.5)
479  *
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.
483  */
484 BOOL WINAPI SERIALUI_GetDefaultCommConfig(
485         LPCSTR lpszDevice,
486         LPCOMMCONFIG lpCommConfig,
487         LPDWORD lpdwSize
488 ) {
489     HKEY hKeyReg, hKeyPort;
490     CHAR szKeyName[100];
491     DWORD r,dwSize,dwType;
492
493     TRACE("%p %p %p\n",lpszDevice,lpCommConfig,lpdwSize);
494
495     if(!lpCommConfig)
496         return FALSE;
497
498     if(!lpdwSize)
499         return FALSE;
500
501     if(*lpdwSize < sizeof (COMMCONFIG))
502         return FALSE;
503
504     *lpdwSize = sizeof (COMMCONFIG);
505     memset(lpCommConfig, 0 , sizeof (COMMCONFIG));
506     lpCommConfig->dwSize = sizeof (COMMCONFIG);
507     lpCommConfig->wVersion = 1;
508
509     r = RegConnectRegistryA(NULL, HKEY_LOCAL_MACHINE, &hKeyReg);
510     if(r != ERROR_SUCCESS)
511         return FALSE;
512
513     snprintf(szKeyName, sizeof szKeyName, "%s\\%s", lpszCommKey ,lpszDevice);
514     r = RegOpenKeyA(hKeyReg, szKeyName, &hKeyPort);
515     if(r == ERROR_SUCCESS)
516     {
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;
525
526         RegCloseKey(hKeyPort);
527     }
528     else
529     {
530         /* FIXME: default to a hardcoded commconfig */
531
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;
540     }
541
542     RegCloseKey(hKeyReg);
543
544     return (r==ERROR_SUCCESS);
545 }