winejack.drv only supports 16bit sound so we can simplify the sound
[wine] / programs / winecfg / drive.c
1 /*
2  * Drive management UI code
3  *
4  * Copyright 2003 Mark Westcott
5  * Copyright 2003 Mike Hearn
6  * Copyright 2004 Chris Morgan
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23
24 /* TODO: */
25 /* - Support for devices(not sure what this means) */
26 /* - Various autodetections */
27
28 #include <assert.h>
29 #include <stdarg.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33
34 #include <windef.h>
35 #include <winbase.h>
36 #include <winreg.h>
37 #include <wine/debug.h>
38 #include <shellapi.h>
39 #include <objbase.h>
40 #include <shlguid.h>
41 #include <shlwapi.h>
42 #include <shlobj.h>
43
44 #include "winecfg.h"
45 #include "resource.h"
46
47
48 WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
49
50 typedef struct drive_entry_s
51 {
52     char letter;
53     char *unixpath;
54     char *label;
55     char *serial;
56     uint type;
57
58     BOOL in_use;
59 } drive_entry_t;
60
61 static BOOL updatingUI = FALSE;
62 static drive_entry_t* editDriveEntry;
63 static int lastSel = 0; /* the last drive selected in the property sheet */
64 drive_entry_t drives[26]; /* one for each drive letter */
65
66 int getDrive(char letter)
67 {
68     return (toupper(letter) - 'A');
69 }
70
71 BOOL addDrive(char letter, char *targetpath, char *label, char *serial, uint type)
72 {
73     if(drives[getDrive(letter)].in_use)
74         return FALSE;
75
76     WINE_TRACE("letter == '%c', unixpath == '%s', label == '%s', serial == '%s', type == %d\n",
77                letter, targetpath, label, serial, type);
78
79     drives[getDrive(letter)].letter = toupper(letter);
80     drives[getDrive(letter)].unixpath = strdup(targetpath);
81     drives[getDrive(letter)].label = strdup(label);
82     drives[getDrive(letter)].serial = strdup(serial);
83     drives[getDrive(letter)].type = type;
84     drives[getDrive(letter)].in_use = TRUE;
85
86     return TRUE;
87 }
88
89 /* frees up the memory associated with a drive and returns the */
90 /* pNext of the given drive entry */
91 void freeDrive(drive_entry_t *pDrive)
92 {
93     free(pDrive->unixpath);
94     free(pDrive->label);
95     free(pDrive->serial);
96
97     pDrive->in_use = FALSE;
98 }
99
100 void setDriveLabel(drive_entry_t *pDrive, char *label)
101 {
102     WINE_TRACE("pDrive->letter '%c', label = '%s'\n", pDrive->letter, label);
103     free(pDrive->label);
104     pDrive->label = strdup(label);
105 }
106
107 void setDriveSerial(drive_entry_t *pDrive, char *serial)
108 {
109     WINE_TRACE("pDrive->letter '%c', serial = '%s'\n", pDrive->letter, serial);
110     free(pDrive->serial);
111     pDrive->serial = strdup(serial);
112 }
113
114 void setDrivePath(drive_entry_t *pDrive, char *path)
115 {
116     WINE_TRACE("pDrive->letter '%c', path = '%s'\n", pDrive->letter, path);
117     free(pDrive->unixpath);
118     pDrive->unixpath = strdup(path);
119 }
120
121 BOOL copyDrive(drive_entry_t *pSrc, drive_entry_t *pDst)
122 {
123     if(pDst->in_use)
124     {
125         WINE_TRACE("pDst already in use\n");
126         return FALSE;
127     }
128
129     if(!pSrc->unixpath) WINE_TRACE("!pSrc->unixpath\n");
130     if(!pSrc->label) WINE_TRACE("!pSrc->label\n");
131     if(!pSrc->serial) WINE_TRACE("!pSrc->serial\n");
132
133     pDst->unixpath = strdup(pSrc->unixpath);
134     pDst->label = strdup(pSrc->label);
135     pDst->serial = strdup(pSrc->serial);
136     pDst->type = pSrc->type;
137     pDst->in_use = TRUE;
138
139     return TRUE;
140 }
141
142 BOOL moveDrive(drive_entry_t *pSrc, drive_entry_t *pDst)
143 {
144     WINE_TRACE("pSrc->letter == %c, pDst->letter == %c\n", pSrc->letter, pDst->letter);
145
146     if(!copyDrive(pSrc, pDst))
147     {
148         WINE_TRACE("copyDrive failed\n");
149         return FALSE;
150     }
151
152     freeDrive(pSrc);
153     return TRUE;
154 }
155
156 int refreshDriveDlg (HWND dialog)
157 {
158   int driveCount = 0;
159   int doesDriveCExist = FALSE;
160   int i;
161
162   WINE_TRACE("\n");
163
164   updatingUI = TRUE;
165
166   /* Clear the listbox */
167   SendMessageA(GetDlgItem(dialog, IDC_LIST_DRIVES), LB_RESETCONTENT, 0, 0);
168
169   for(i = 0; i < 26; i++)
170   {
171       char *title = 0;
172       int titleLen;
173       int itemIndex;
174
175       /* skip over any unused drives */
176       if(!drives[i].in_use)
177           continue;
178
179       if(drives[i].letter == 'C')
180           doesDriveCExist = TRUE;
181
182       titleLen = snprintf(title, 0, "Drive %c:\\ %s", 'A' + i,
183                           drives[i].unixpath);
184       titleLen++; /* add a byte for the trailing null */
185
186       title = malloc(titleLen);
187
188       /* the %s in the item label will be replaced by the drive letter, so -1, then
189          -2 for the second %s which will be expanded to the label, finally + 1 for terminating #0 */
190       snprintf(title, titleLen, "Drive %c:\\ %s", 'A' + i,
191                drives[i].unixpath);
192
193       WINE_TRACE("title is '%s'\n", title);
194
195       /* the first SendMessage call adds the string and returns the index, the second associates that index with it */
196       itemIndex = SendMessageA(GetDlgItem(dialog, IDC_LIST_DRIVES), LB_ADDSTRING ,(WPARAM) -1, (LPARAM) title);
197       SendMessageA(GetDlgItem(dialog, IDC_LIST_DRIVES), LB_SETITEMDATA, itemIndex, (LPARAM) &drives[i]);
198
199       free(title);
200       driveCount++;
201   }
202
203   WINE_TRACE("loaded %d drives\n", driveCount);
204   SendDlgItemMessage(dialog, IDC_LIST_DRIVES, LB_SETSEL, TRUE, lastSel);
205
206   /* show the warning if there is no Drive C */
207   if (!doesDriveCExist)
208     ShowWindow(GetDlgItem(dialog, IDS_DRIVE_NO_C), SW_NORMAL);
209   else
210     ShowWindow(GetDlgItem(dialog, IDS_DRIVE_NO_C), SW_HIDE);
211   
212   /* disable or enable controls depending on whether we are editing global vs app specific config */
213   if (appSettings == EDITING_GLOBAL) {
214     WINE_TRACE("enabling controls\n");
215     enable(IDC_LIST_DRIVES);
216     enable(IDC_BUTTON_ADD);
217     enable(IDC_BUTTON_REMOVE);
218     enable(IDC_BUTTON_EDIT);
219     enable(IDC_BUTTON_AUTODETECT);
220     
221   } else {
222     WINE_TRACE("disabling controls\n");
223     disable(IDC_LIST_DRIVES);
224     disable(IDC_BUTTON_ADD);
225     disable(IDC_BUTTON_REMOVE);
226     disable(IDC_BUTTON_EDIT);
227     disable(IDC_BUTTON_AUTODETECT);    
228   }
229
230   
231   updatingUI = FALSE;
232   return driveCount;
233 }
234
235 /******************************************************************************/
236 /*  The Drive Editing Dialog                                                  */
237 /******************************************************************************/
238 #define DRIVE_MASK_BIT(B) 1<<(toupper(B)-'A')
239
240 typedef struct {
241   const uint sCode;
242   const char *sDesc;
243 } code_desc_pair;
244
245 static code_desc_pair type_pairs[] = {
246   {DRIVE_FIXED,      "Local hard disk"},
247   {DRIVE_REMOTE,     "Network share" },
248   {DRIVE_REMOVABLE,  "Floppy disk"},
249   {DRIVE_CDROM,      "CD-ROM"}
250 };
251 #define DRIVE_TYPE_DEFAULT 1
252
253
254 void fill_drive_droplist(long mask, char currentLetter, HWND hDlg)
255 {
256   int i;
257   int selection;
258   int count;
259   int next_letter;
260   char sName[4] = "A:";
261
262   for( i=0, count=0, selection=-1, next_letter=-1; i <= 'Z'-'A'; ++i ) {
263     if( mask & DRIVE_MASK_BIT('A'+i) ) {
264       int index;
265       
266       sName[0] = 'A' + i;
267       index = SendDlgItemMessage( hDlg, IDC_COMBO_LETTER, CB_ADDSTRING, 0, (LPARAM) sName );
268                           
269       if( toupper(currentLetter) == 'A' + i ) {
270           selection = count;
271       }
272       
273       if( i >= 2 && next_letter == -1){ /*default drive is first one of C-Z */
274           next_letter = count;
275       }
276       
277       count++;
278     }
279   }
280   
281   if( selection == -1 ) {
282     selection = next_letter;
283   }
284   
285   SendDlgItemMessage( hDlg, IDC_COMBO_LETTER, CB_SETCURSEL, selection, 0 );
286 }
287
288 #define BOX_MODE_CD_ASSIGN 1
289 #define BOX_MODE_CD_AUTODETECT 2
290 #define BOX_MODE_NONE 3
291 #define BOX_MODE_NORMAL 4
292 void enable_labelserial_box(HWND dialog, int mode)
293 {
294   WINE_TRACE("mode=%d\n", mode);
295   switch (mode) {
296       case BOX_MODE_CD_ASSIGN:
297 /*      enable(IDC_RADIO_AUTODETECT); */
298         enable(IDC_RADIO_ASSIGN);
299         disable(IDC_EDIT_DEVICE);
300         disable(IDC_BUTTON_BROWSE_DEVICE);
301         enable(IDC_EDIT_SERIAL);
302         enable(IDC_EDIT_LABEL);
303         enable(IDC_STATIC_SERIAL);
304         enable(IDC_STATIC_LABEL);
305         break;
306         
307       case BOX_MODE_CD_AUTODETECT:
308 /*      enable(IDC_RADIO_AUTODETECT); */
309         enable(IDC_RADIO_ASSIGN);
310         enable(IDC_EDIT_DEVICE);
311         enable(IDC_BUTTON_BROWSE_DEVICE);
312         disable(IDC_EDIT_SERIAL);
313         disable(IDC_EDIT_LABEL);
314         disable(IDC_STATIC_SERIAL);
315         disable(IDC_STATIC_LABEL);
316         break;
317
318       case BOX_MODE_NONE:
319         disable(IDC_RADIO_AUTODETECT);
320         disable(IDC_RADIO_ASSIGN);
321         disable(IDC_EDIT_DEVICE);
322         disable(IDC_BUTTON_BROWSE_DEVICE);
323         disable(IDC_EDIT_SERIAL);
324         disable(IDC_EDIT_LABEL);
325         disable(IDC_STATIC_SERIAL);
326         disable(IDC_STATIC_LABEL);
327         break;
328
329       case BOX_MODE_NORMAL:
330         disable(IDC_RADIO_AUTODETECT);
331         enable(IDC_RADIO_ASSIGN);
332         disable(IDC_EDIT_DEVICE);
333         disable(IDC_BUTTON_BROWSE_DEVICE);
334         enable(IDC_EDIT_SERIAL);
335         enable(IDC_EDIT_LABEL);
336         enable(IDC_STATIC_SERIAL);
337         enable(IDC_STATIC_LABEL);
338         break;  
339   }
340 }
341
342 /* This function produces a mask for each drive letter that isn't currently used. Each bit of the long result
343  * represents a letter, with A being the least significant bit, and Z being the most significant.
344  *
345  * To calculate this, we loop over each letter, and see if we can get a drive entry for it. If so, we
346  * set the appropriate bit. At the end, we flip each bit, to give the desired result.
347  *
348  * The letter parameter is always marked as being available. This is so the edit dialog can display the
349  * currently used drive letter alongside the available ones.
350  */
351 long drive_available_mask(char letter)
352 {
353   long result = 0;
354   int i;
355
356   WINE_TRACE("\n");
357
358   
359   for(i = 0; i < 26; i++)
360   {
361       if(!drives[i].in_use) continue;
362       result |= (1 << (toupper(drives[i].letter) - 'A'));
363   }
364
365   result = ~result;
366   if (letter) result |= DRIVE_MASK_BIT(letter);
367   
368   WINE_TRACE( "finished drive letter loop with %lx\n", result );
369   return result;
370 }
371
372 void advancedDriveEditDialog(HWND hDlg, BOOL showAdvanced)
373 {
374 #define ADVANCED_DELTA      120
375
376     static RECT okpos;
377     static BOOL got_initial_ok_position = FALSE;
378
379     static RECT windowpos; /* we only use the height of this rectangle */
380     static BOOL got_initial_window_position = FALSE;
381
382     static RECT current_window;
383
384     INT state;
385     INT offset;
386     char *text;
387
388     if(!got_initial_ok_position)
389     {
390         POINT pt;
391         GetWindowRect(GetDlgItem(hDlg, ID_BUTTON_OK), &okpos);
392         pt.x = okpos.left;
393         pt.y = okpos.top;
394         ScreenToClient(hDlg, &pt);
395         okpos.right+= (pt.x - okpos.left);
396         okpos.bottom+= (pt.y - okpos.top);
397         okpos.left = pt.x;
398         okpos.top = pt.y;
399         got_initial_ok_position = TRUE;
400     }
401
402     if(!got_initial_window_position)
403     {
404         GetWindowRect(hDlg, &windowpos);
405         got_initial_window_position = TRUE;
406     }
407           
408     if(showAdvanced)
409     {
410         state = SW_NORMAL;
411         offset = 0;
412         text = "Hide Advanced";
413     } else
414     {
415         state = SW_HIDE;
416         offset = ADVANCED_DELTA;
417         text = "Show Advanced";
418     }
419
420     ShowWindow(GetDlgItem(hDlg, IDC_STATIC_TYPE), state);
421     ShowWindow(GetDlgItem(hDlg, IDC_COMBO_TYPE), state);
422     ShowWindow(GetDlgItem(hDlg, IDC_BOX_LABELSERIAL), state);
423     ShowWindow(GetDlgItem(hDlg, IDC_RADIO_AUTODETECT), state);
424     ShowWindow(GetDlgItem(hDlg, IDC_RADIO_ASSIGN), state);
425     ShowWindow(GetDlgItem(hDlg, IDC_EDIT_LABEL), state);
426     ShowWindow(GetDlgItem(hDlg, IDC_EDIT_DEVICE), state);
427     ShowWindow(GetDlgItem(hDlg, IDC_STATIC_LABEL), state);
428     ShowWindow(GetDlgItem(hDlg, IDC_BUTTON_BROWSE_DEVICE), state);
429     SetWindowPos(GetDlgItem(hDlg, ID_BUTTON_OK),
430                  HWND_TOP,
431                  okpos.left, okpos.top - offset, okpos.right - okpos.left,
432                  okpos.bottom - okpos.top,
433                  0);
434
435     /* resize the parent window */
436     GetWindowRect(hDlg, &current_window);
437     SetWindowPos(hDlg, 
438                  HWND_TOP,
439                  current_window.left,
440                  current_window.top,
441                  windowpos.right - windowpos.left,
442                  windowpos.bottom - windowpos.top - offset,
443                  0);
444
445     /* update the button text based on the state */
446     SetWindowText(GetDlgItem(hDlg, IDC_BUTTON_SHOW_HIDE_ADVANCED),
447                   text);
448 }
449
450
451 void refreshDriveEditDialog(HWND dialog) {
452   char *path;
453   uint type;
454   char *label;
455   char *serial;
456   char *device;
457   int i, selection = -1;
458
459   updatingUI = TRUE;
460
461   WINE_TRACE("\n");
462   
463   /* Drive letters */
464   fill_drive_droplist( drive_available_mask( editDriveEntry->letter ), editDriveEntry->letter, dialog );
465
466   /* path */
467   path = editDriveEntry->unixpath;
468   if (path) {
469     WINE_TRACE("set path control text to '%s'\n", path);
470     SetWindowText(GetDlgItem(dialog, IDC_EDIT_PATH), path);
471   } else WINE_WARN("no Path field?\n");
472   
473   /* drive type */
474   type = editDriveEntry->type;
475   if (type) {
476     for(i = 0; i < sizeof(type_pairs)/sizeof(code_desc_pair); i++) {
477       SendDlgItemMessage(dialog, IDC_COMBO_TYPE, CB_ADDSTRING, 0,
478                          (LPARAM) type_pairs[i].sDesc);
479       if(type_pairs[i].sCode ==  type){
480           selection = i;
481       }
482     }
483   
484     if( selection == -1 ) selection = DRIVE_TYPE_DEFAULT;
485     SendDlgItemMessage(dialog, IDC_COMBO_TYPE, CB_SETCURSEL, selection, 0);
486   } else WINE_WARN("no Type field?\n");
487
488   
489   /* removeable media properties */
490   label = editDriveEntry->label;
491   if (label) {
492     SendDlgItemMessage(dialog, IDC_EDIT_LABEL, WM_SETTEXT, 0,(LPARAM)label);
493   } else WINE_WARN("no Label field?\n");
494
495   /* set serial edit text */
496   serial = editDriveEntry->serial;
497   if (serial) {
498     SendDlgItemMessage(dialog, IDC_EDIT_SERIAL, WM_SETTEXT, 0,(LPARAM)serial);
499   } else WINE_WARN("no Serial field?\n");
500
501   /* TODO: get the device here to put into the edit box */
502   device = "Not implemented yet";
503   if (device) {
504     SendDlgItemMessage(dialog, IDC_EDIT_DEVICE, WM_SETTEXT, 0,(LPARAM)device);
505   } else WINE_WARN("no Device field?\n");
506
507   selection = IDC_RADIO_ASSIGN;
508   if ((type == DRIVE_CDROM) || (type == DRIVE_REMOVABLE)) {
509 #if 0
510     if (device) {
511       selection = IDC_RADIO_AUTODETECT;
512       enable_labelserial_box(dialog, BOX_MODE_CD_AUTODETECT);
513     } else {
514 #endif
515       selection = IDC_RADIO_ASSIGN;
516       enable_labelserial_box(dialog, BOX_MODE_CD_ASSIGN);
517 #if 0
518     }
519 #endif
520   } else {
521     enable_labelserial_box(dialog, BOX_MODE_NORMAL);
522     selection = IDC_RADIO_ASSIGN;
523   }
524
525   CheckRadioButton( dialog, IDC_RADIO_AUTODETECT, IDC_RADIO_ASSIGN, selection );
526   
527   updatingUI = FALSE;
528   
529   return;
530 }
531
532 /* storing the drive propsheet HWND here is a bit ugly, but the simplest solution for now */
533 static HWND driveDlgHandle;
534
535 void onEditChanged(HWND hDlg, WORD controlID) {
536   WINE_TRACE("controlID=%d\n", controlID);
537   switch (controlID) {
538       case IDC_EDIT_LABEL: {
539         char *label = getDialogItemText(hDlg, controlID);
540         if(!label) label = strdup("");
541         setDriveLabel(editDriveEntry, label);
542         refreshDriveDlg(driveDlgHandle);
543         if (label) free(label);
544         break;
545       }
546       case IDC_EDIT_PATH: {
547           char *path = getDialogItemText(hDlg, controlID);
548           if (!path) path = strdup("fake_windows"); /* default to assuming fake_windows in the .wine directory */
549           WINE_TRACE("got path from control of '%s'\n", path);
550           setDrivePath(editDriveEntry, path);
551           if(path) free(path);
552           break;
553       }
554       case IDC_EDIT_SERIAL: {
555           char *serial = getDialogItemText(hDlg, controlID);
556           if(!serial) serial = strdup("");
557           setDriveSerial(editDriveEntry, serial);
558           if (serial) free (serial);
559           break;
560       }
561       case IDC_EDIT_DEVICE: {
562           char *device = getDialogItemText(hDlg,controlID);
563           /* TODO: handle device if/when it makes sense to do so.... */
564           if (device) free(device);
565           refreshDriveDlg(driveDlgHandle);
566           break;
567       }
568   }
569 }
570
571 /* edit a drive entry */
572 INT_PTR CALLBACK DriveEditDlgProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
573 {
574   int selection;
575   static BOOL advanced = FALSE;
576
577   switch (uMsg)  {
578       case WM_CLOSE:
579             EndDialog(hDlg, wParam);
580             return TRUE;
581
582       case WM_INITDIALOG: {
583           enable_labelserial_box(hDlg, BOX_MODE_NORMAL);
584           advancedDriveEditDialog(hDlg, advanced);
585           editDriveEntry = (drive_entry_t*)lParam;
586           refreshDriveEditDialog(hDlg);
587       }
588
589     case WM_COMMAND:
590       switch (LOWORD(wParam)) {
591           case IDC_COMBO_TYPE:
592             if (HIWORD(wParam) != CBN_SELCHANGE) break;
593             selection = SendDlgItemMessage( hDlg, IDC_COMBO_TYPE, CB_GETCURSEL, 0, 0);
594             if( selection == 2 || selection == 3 ) { /* cdrom or floppy */
595               if (IsDlgButtonChecked(hDlg, IDC_RADIO_AUTODETECT))
596               enable_labelserial_box(hDlg, BOX_MODE_CD_AUTODETECT);
597               else
598               enable_labelserial_box(hDlg, BOX_MODE_CD_ASSIGN);
599             }
600             else {
601               enable_labelserial_box( hDlg, BOX_MODE_NORMAL );
602             }
603         editDriveEntry->type = type_pairs[selection].sCode;
604             break;
605
606           case IDC_COMBO_LETTER: {
607             int item = SendDlgItemMessage(hDlg, IDC_COMBO_LETTER, CB_GETCURSEL, 0, 0);
608             char newLetter;
609             SendDlgItemMessage(hDlg, IDC_COMBO_LETTER, CB_GETLBTEXT, item, (LPARAM) &newLetter);
610             
611             if (HIWORD(wParam) != CBN_SELCHANGE) break;
612             if (newLetter == editDriveEntry->letter) break;
613                     
614             WINE_TRACE("changing drive letter to %c\n", newLetter);
615         moveDrive(editDriveEntry, &drives[getDrive(newLetter)]);
616         editDriveEntry = &drives[getDrive(newLetter)];
617             refreshDriveDlg(driveDlgHandle);
618             break;
619           }
620
621           case IDC_BUTTON_BROWSE_PATH:
622             WRITEME(hDlg);
623             break;
624
625           case IDC_RADIO_AUTODETECT: {
626         /* TODO: */
627         WINE_FIXME("Implement autodetection\n");
628             enable_labelserial_box(hDlg, BOX_MODE_CD_AUTODETECT);
629         refreshDriveDlg(driveDlgHandle);
630             break;
631           }
632             
633           case IDC_RADIO_ASSIGN:
634       {
635         char *edit, *serial;
636         edit = getDialogItemText(hDlg, IDC_EDIT_LABEL);
637         if(!edit) edit = strdup("");
638         setDriveLabel(editDriveEntry, edit);
639         free(edit);
640
641         serial = getDialogItemText(hDlg, IDC_EDIT_SERIAL);
642         if(!serial) serial = strdup("");
643         setDriveSerial(editDriveEntry, serial);
644         free(serial);
645
646         /* TODO: we don't have a device at this point */
647 /*      setDriveValue(editWindowLetter, "Device", NULL); */
648             enable_labelserial_box(hDlg, BOX_MODE_CD_ASSIGN);
649             refreshDriveDlg(driveDlgHandle);
650             break;
651       }
652             
653       case IDC_BUTTON_SHOW_HIDE_ADVANCED:
654           advanced = (advanced == TRUE) ? FALSE : TRUE; /* toggle state */
655           advancedDriveEditDialog(hDlg, advanced);
656           break;
657
658           case ID_BUTTON_OK:      
659             EndDialog(hDlg, wParam);
660             return TRUE;
661
662       }
663       if (HIWORD(wParam) == EN_CHANGE) onEditChanged(hDlg, LOWORD(wParam));
664       break;
665   }
666   return FALSE;
667 }
668
669 void onAddDriveClicked(HWND hDlg) {
670   /* we should allocate a drive letter automatically. We also need some way to let the user choose the mapping point,
671      for now we will just force them to enter a path automatically, with / being the default. In future we should
672      be able to temporarily map / then invoke the directory chooser dialog. */
673   
674   char newLetter = 'C'; /* we skip A and B, they are historically floppy drives */
675   long mask = ~drive_available_mask(0); /* the mask is now which drives aren't available */
676   
677   while (mask & (1 << (newLetter - 'A'))) {
678     newLetter++;
679     if (newLetter > 'Z') {
680       MessageBox(NULL, "You cannot add any more drives.\n\nEach drive must have a letter, from A to Z, so you cannot have more than 26", "", MB_OK | MB_ICONEXCLAMATION);
681       return;
682     }
683   }
684   WINE_TRACE("allocating drive letter %c\n", newLetter);
685
686   if(newLetter == 'C') {
687       addDrive(newLetter, "fake_windows", "System Drive", "", DRIVE_FIXED);
688   } else {
689       addDrive(newLetter, "/", "", "", DRIVE_FIXED);
690   }
691
692   refreshDriveDlg(driveDlgHandle);
693
694   DialogBoxParam(NULL, MAKEINTRESOURCE(IDD_DRIVE_EDIT), NULL, (DLGPROC) DriveEditDlgProc, (LPARAM) &(drives[getDrive(newLetter)]));
695 }
696
697 void onDriveInitDialog(void)
698 {
699   char *pDevices;
700   int ret;
701   int i;
702   int retval;
703
704   WINE_TRACE("\n");
705
706   /* setup the drives array */
707   pDevices = (char*)malloc(512);
708   ret = GetLogicalDriveStrings(512, pDevices);
709
710   /* make all devices unused */
711   for(i = 0; i < 26; i++)
712   {
713       drives[i].letter = 'A' + i;
714       drives[i].in_use = FALSE;
715   }
716
717   i = 0;
718
719   while(ret)
720   {
721     CHAR volumeNameBuffer[512];
722     DWORD serialNumber;
723     CHAR serialNumberString[256];
724     DWORD maxComponentLength;
725     DWORD fileSystemFlags;
726     CHAR fileSystemName[128];
727     char rootpath[256];
728     char simplepath[3];
729     int pathlen;
730     char targetpath[256];
731
732     *pDevices = toupper(*pDevices);
733
734     WINE_TRACE("pDevices == '%s'\n", pDevices);
735
736     volumeNameBuffer[0] = 0;
737     
738     retval = GetVolumeInformation(pDevices,
739                          volumeNameBuffer,
740                          sizeof(volumeNameBuffer),
741                          &serialNumber,
742                          &maxComponentLength,
743                          &fileSystemFlags,
744                          fileSystemName,
745                          sizeof(fileSystemName));
746     if(!retval)
747     {
748         WINE_TRACE("GetVolumeInformation() for '%s' failed, setting serialNumber to 0\n", pDevices);
749         PRINTERROR();
750         serialNumber = 0;
751     }
752
753     WINE_TRACE("serialNumber: '0x%lX'\n", serialNumber);
754
755     /* build rootpath for GetDriveType() */
756     strncpy(rootpath, pDevices, sizeof(rootpath));
757     pathlen = strlen(rootpath);
758     /* ensure that we have a backslash on the root path */
759     if((rootpath[pathlen - 1] != '\\') &&
760        (pathlen < sizeof(rootpath)))
761      {
762          rootpath[pathlen] = '\\';
763          rootpath[pathlen + 1] = 0;
764      }
765
766     strncpy(simplepath, pDevices, 2); /* QueryDosDevice() requires no trailing backslash */
767     simplepath[2] = 0;
768     QueryDosDevice(simplepath, targetpath, sizeof(targetpath)); 
769     
770     snprintf(serialNumberString, sizeof(serialNumberString), "%lX", serialNumber);
771     WINE_TRACE("serialNumberString: '%s'\n", serialNumberString);
772     addDrive(*pDevices, targetpath, volumeNameBuffer, serialNumberString, GetDriveType(rootpath));
773
774     ret-=strlen(pDevices);
775     pDevices+=strlen(pDevices);
776
777     /* skip over any nulls */
778     while((*pDevices == 0) && (ret))
779     {
780         ret--;
781         pDevices++;
782     }
783
784     i++;
785   }
786
787   WINE_TRACE("found %d drives\n", i);
788
789   free(pDevices);
790 }
791
792
793 void applyDriveChanges(void)
794 {
795     int i;
796     CHAR devicename[4];
797     CHAR targetpath[256];
798     BOOL foundDrive;
799     CHAR volumeNameBuffer[512];
800     DWORD serialNumber;
801     DWORD maxComponentLength;
802     DWORD fileSystemFlags;
803     CHAR fileSystemName[128];
804     int retval;
805     BOOL defineDevice;
806
807     WINE_TRACE("\n");
808
809     /* add each drive and remove as we go */
810     for(i = 0; i < 26; i++)
811     {
812         defineDevice = FALSE;
813         foundDrive = FALSE;
814         snprintf(devicename, sizeof(devicename), "%c:", 'A' + i); 
815
816         /* get a drive */
817         if(QueryDosDevice(devicename, targetpath, sizeof(targetpath)))
818         {
819             foundDrive = TRUE;
820         }
821
822         /* if we found a drive and have a drive then compare things */
823         if(foundDrive && drives[i].in_use)
824         {
825             char newSerialNumberText[256];
826
827             volumeNameBuffer[0] = 0;
828
829             WINE_TRACE("drives[i].letter: '%c'\n", drives[i].letter);
830
831             snprintf(devicename, sizeof(devicename), "%c:\\", 'A' + i);
832             retval = GetVolumeInformation(devicename,
833                          volumeNameBuffer,
834                          sizeof(volumeNameBuffer),
835                          &serialNumber,
836                          &maxComponentLength,
837                          &fileSystemFlags,
838                          fileSystemName,
839                          sizeof(fileSystemName));
840             if(!retval)
841             {
842                 WINE_TRACE("  GetVolumeInformation() for '%s' failed\n", devicename);
843                 WINE_TRACE("  Skipping this drive\n");
844                 PRINTERROR();
845                 continue; /* skip this drive */
846             }
847
848             snprintf(newSerialNumberText, sizeof(newSerialNumberText), "%lX", serialNumber);
849
850             WINE_TRACE("  current path:   '%s', new path:   '%s'\n",
851                        targetpath, drives[i].unixpath);
852             WINE_TRACE("  current label:  '%s', new label:  '%s'\n",
853                        volumeNameBuffer, drives[i].label);
854             WINE_TRACE("  current serial: '%s', new serial: '%s'\n",
855                        newSerialNumberText, drives[i].serial);
856
857             /* compare to what we have */
858             /* do we have the same targetpath? */
859             if(strcmp(drives[i].unixpath, targetpath) ||
860                strcmp(drives[i].label, volumeNameBuffer) ||
861                strcmp(drives[i].serial, newSerialNumberText))
862             {
863                 defineDevice = TRUE;
864                 WINE_TRACE("  making changes to drive '%s'\n", devicename);
865             } else
866             {
867                 WINE_TRACE("  no changes to drive '%s'\n", devicename);
868             }
869         } else if(foundDrive && !drives[i].in_use)
870         {
871             /* remove this drive */
872             if(!DefineDosDevice(DDD_REMOVE_DEFINITION, devicename, drives[i].unixpath))
873             {
874                 WINE_ERR("unable to remove devicename of '%s', targetpath of '%s'\n",
875                     devicename, drives[i].unixpath);
876                 PRINTERROR();
877             } else
878             {
879                 WINE_TRACE("removed devicename of '%s', targetpath of '%s'\n",
880                            devicename, drives[i].unixpath);
881             }
882         } else if(drives[i].in_use) /* foundDrive must be false from the above check */
883         {
884             defineDevice = TRUE;
885         }
886
887         /* adding and modifying are the same steps */
888         if(defineDevice)
889         {
890             char filename[256];
891             HANDLE hFile;
892
893             HKEY hKey;
894             char *typeText;
895             char driveValue[256];
896
897             /* define this drive */
898             /* DefineDosDevice() requires that NO trailing slash be present */
899             snprintf(devicename, sizeof(devicename), "%c:", 'A' + i); 
900             if(!DefineDosDevice(DDD_RAW_TARGET_PATH, devicename, drives[i].unixpath))
901             {
902                 WINE_ERR("  unable to define devicename of '%s', targetpath of '%s'\n",
903                     devicename, drives[i].unixpath);
904                 PRINTERROR();
905             } else
906             {
907                 WINE_TRACE("  added devicename of '%s', targetpath of '%s'\n",
908                            devicename, drives[i].unixpath);
909                 
910                 /* SetVolumeLabel() requires a trailing slash */
911                 snprintf(devicename, sizeof(devicename), "%c:\\", 'A' + i);
912                 if(!SetVolumeLabel(devicename, drives[i].label))
913                 {
914                     WINE_ERR("unable to set volume label for devicename of '%s', label of '%s'\n",
915                         devicename, drives[i].label);
916                     PRINTERROR();
917                 } else
918                 {
919                     WINE_TRACE("  set volume label for devicename of '%s', label of '%s'\n",
920                         devicename, drives[i].label);
921                 }
922             }
923
924             /* Set the drive type in the registry */
925             if(drives[i].type == DRIVE_FIXED)
926                 typeText = "hd";
927             else if(drives[i].type == DRIVE_REMOTE)
928                 typeText = "network";
929             else if(drives[i].type == DRIVE_REMOVABLE)
930                 typeText = "floppy";
931             else /* must be DRIVE_CDROM */
932                 typeText = "cdrom";
933             
934
935             snprintf(driveValue, sizeof(driveValue), "%c:", toupper(drives[i].letter));
936
937             retval = RegOpenKey(HKEY_LOCAL_MACHINE,
938                        "Software\\Wine\\Drives",
939                        &hKey);
940
941             if(retval != ERROR_SUCCESS)
942             {
943                 WINE_TRACE("  Unable to open '%s'\n", "Software\\Wine\\Drives");
944             } else
945             {
946                 retval = RegSetValueEx(
947                               hKey,
948                               driveValue,
949                               0,
950                               REG_SZ,
951                               typeText,
952                               strlen(typeText) + 1);
953                 if(retval != ERROR_SUCCESS)
954                 {
955                     WINE_TRACE("  Unable to set value of '%s' to '%s'\n",
956                                driveValue, typeText);
957                 } else
958                 {
959                     WINE_TRACE("  Finished setting value of '%s' to '%s'\n",
960                                driveValue, typeText);
961                     RegCloseKey(hKey);
962                 }
963             }
964
965             /* Set the drive serial number via a .windows-serial file in */
966             /* the targetpath directory */
967             snprintf(filename, sizeof(filename), "%c:\\.windows-serial", drives[i].letter);
968             WINE_TRACE("  Putting serial number of '%ld' into file '%s'\n",
969                        serialNumber, filename);
970             hFile = CreateFile(filename,
971                        GENERIC_WRITE,
972                        FILE_SHARE_READ,
973                        NULL,
974                        CREATE_ALWAYS,
975                        FILE_ATTRIBUTE_NORMAL,
976                        NULL);
977             if(hFile)
978             {
979                 WINE_TRACE("  writing serial number of '%s'\n", drives[i].serial);
980                 WriteFile(hFile,
981                           drives[i].serial,
982                           strlen(drives[i].serial),
983                           NULL,
984                           NULL); 
985                 WriteFile(hFile,
986                           "\n",
987                           strlen("\n"),
988                           NULL,
989                           NULL); 
990                 CloseHandle(hFile);
991             } else
992             {
993                 WINE_TRACE("  CreateFile() error with file '%s'\n", filename);
994             }
995         }
996
997         /* if this drive is in use we should free it up */
998         if(drives[i].in_use)
999         {
1000             freeDrive(&drives[i]); /* free up the string memory */
1001         }
1002     }
1003 }
1004
1005
1006 INT_PTR CALLBACK
1007 DriveDlgProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
1008 {
1009   int nItem;
1010   drive_entry_t *pDrive;
1011
1012   switch (uMsg) {
1013       case WM_INITDIALOG:
1014           onDriveInitDialog();
1015           break;
1016       case WM_COMMAND:
1017         switch (LOWORD(wParam)) {
1018             case IDC_LIST_DRIVES:
1019               /* double click should open the edit window for the chosen drive */
1020               if (HIWORD(wParam) == LBN_DBLCLK)
1021                 SendMessageA(hDlg, WM_COMMAND, IDC_BUTTON_EDIT, 0);
1022
1023               if (HIWORD(wParam) == LBN_SELCHANGE) lastSel = SendDlgItemMessage(hDlg, IDC_LIST_DRIVES, LB_GETCURSEL, 0, 0);
1024               break;
1025
1026             case IDC_BUTTON_ADD:
1027               onAddDriveClicked(hDlg);
1028               break;
1029
1030             case IDC_BUTTON_REMOVE:
1031               if (HIWORD(wParam) != BN_CLICKED) break;
1032               nItem = SendDlgItemMessage(hDlg, IDC_LIST_DRIVES,  LB_GETCURSEL, 0, 0);
1033               pDrive = (drive_entry_t*)SendDlgItemMessage(hDlg, IDC_LIST_DRIVES, LB_GETITEMDATA, nItem, 0);
1034           freeDrive(pDrive);
1035               refreshDriveDlg(driveDlgHandle);
1036               break;
1037               
1038             case IDC_BUTTON_EDIT:
1039               if (HIWORD(wParam) != BN_CLICKED) break;
1040               nItem = SendMessage(GetDlgItem(hDlg, IDC_LIST_DRIVES),  LB_GETCURSEL, 0, 0);
1041               pDrive = (drive_entry_t*)SendMessage(GetDlgItem(hDlg, IDC_LIST_DRIVES), LB_GETITEMDATA, nItem, 0);
1042               DialogBoxParam(NULL, MAKEINTRESOURCE(IDD_DRIVE_EDIT), NULL, (DLGPROC) DriveEditDlgProc, (LPARAM) pDrive);
1043               break;
1044
1045             case IDC_BUTTON_AUTODETECT:
1046               WRITEME(hDlg);
1047               break;
1048         }
1049         break;
1050         
1051       case WM_NOTIFY: switch(((LPNMHDR)lParam)->code) {
1052             case PSN_KILLACTIVE:
1053           WINE_TRACE("PSN_KILLACTIVE\n");
1054               SetWindowLong(hDlg, DWL_MSGRESULT, FALSE);
1055               break;
1056             case PSN_APPLY:
1057           applyDriveChanges();
1058               SetWindowLong(hDlg, DWL_MSGRESULT, PSNRET_NOERROR);
1059               break;
1060             case PSN_SETACTIVE:
1061               driveDlgHandle = hDlg;
1062               refreshDriveDlg (driveDlgHandle);
1063               break;
1064         }
1065         break;
1066   }
1067
1068   return FALSE;
1069 }