~ubuntu-branches/ubuntu/trusty/argyll/trusty-proposed

« back to all changes in this revision

Viewing changes to libusbw/src/inf_wizard.c

  • Committer: Package Import Robot
  • Author(s): Artur Rona
  • Date: 2014-02-12 00:35:39 UTC
  • mfrom: (13.1.24 sid)
  • Revision ID: package-import@ubuntu.com-20140212003539-24tautzlitsiz61w
Tags: 1.5.1-5ubuntu1
* Merge from Debian unstable. (LP: #1275572) Remaining changes:
  - debian/control:
    + Build-depend on libtiff-dev rather than libtiff4-dev.
  - debian/control, debian/patches/06_fix_udev_rule.patch:
    + Fix udev rules to actually work; ENV{ACL_MANAGE} has
      stopped working ages ago, and with logind it's now the
      "uaccess" tag. Dropping also consolekit from Recommends.
  - debian/patches/drop-usb-db.patch:
    + Use hwdb builtin, instead of the obsolete usb-db
      in the udev rules.
* debian/patches/05_ftbfs-underlinkage.diff:
  - Dropped change, no needed anymore.
* Refresh the patches.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* LIBUSB-WIN32, Generic Windows USB Library
2
 
 * Copyright (c) 2002-2006 Stephan Meyer <ste_meyer@web.de>
3
 
 *
4
 
 * This program is free software; you can redistribute it and/or modify
5
 
 * it under the terms of the GNU General Public License as published by
6
 
 * the Free Software Foundation; either version 2 of the License, or
7
 
 * (at your option) any later version.
8
 
 *
9
 
 * This program is distributed in the hope that it will be useful,
10
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
 * GNU General Public License for more details.
13
 
 *
14
 
 * You should have received a copy of the GNU General Public License
15
 
 * along with this program; if not, write to the Free Software
16
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
 
 */
18
 
 
19
 
#ifdef __GNUC__
20
 
#define _WIN32_IE 0x0400
21
 
#define WINVER 0x0500
22
 
#endif
23
 
 
24
 
#define INITGUID
25
 
 
26
 
#include <windows.h>
27
 
#include <commdlg.h>
28
 
#include <dbt.h>
29
 
#include <stdio.h>
30
 
#include <stdlib.h>
31
 
#include <initguid.h>
32
 
#include <commctrl.h>
33
 
#include <setupapi.h>
34
 
 
35
 
#include "registry.h"
36
 
 
37
 
#define __INF_WIZARD_C__
38
 
#include "inf_wizard_rc.rc"
39
 
 
40
 
 
41
 
#define _STRINGIFY(x) #x
42
 
#define STRINGIFY(x) _STRINGIFY(x)
43
 
 
44
 
 
45
 
DEFINE_GUID(GUID_DEVINTERFACE_USB_HUB, 0xf18a0e88, 0xc30c, 0x11d0, 0x88, \
46
 
            0x15, 0x00, 0xa0, 0xc9, 0x06, 0xbe, 0xd8);
47
 
 
48
 
DEFINE_GUID(GUID_DEVINTERFACE_USB_DEVICE, 0xA5DCBF10L, 0x6530, 0x11D2, \
49
 
            0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED);
50
 
 
51
 
const char cat_file_content[] =
52
 
"This file will contain the digital signature of the files to be installed\n"
53
 
"on the system.\n"
54
 
"This file will be provided by Microsoft upon certification of your "
55
 
"drivers.\n";
56
 
 
57
 
const char info_text_0[] = 
58
 
"This program will create an .inf file for your device.\n\n"
59
 
"Before clicking \"Next\" make sure that your device is connected to the "
60
 
"system.\n";
61
 
 
62
 
const char info_text_1[] = 
63
 
"An .inf and .cat file has been created successfully for the following "
64
 
"device:\n\n";
65
 
 
66
 
const char list_header_text[] = 
67
 
"Select your device from the list of detected devices below.\n"
68
 
"If your device isn't listed then either connect it or just click \"Next\"\n"
69
 
"and enter your device description manually\n";
70
 
 
71
 
const char inf_header[] = 
72
 
"[Version]\n"
73
 
"Signature = \"$Chicago$\"\n"
74
 
"provider  = %manufacturer%\n"
75
 
"DriverVer = " STRINGIFY(INF_DATE) "," STRINGIFY(INF_VERSION) "\n";
76
 
 
77
 
const char inf_body[] = 
78
 
"Class = LibUsbDevices\n"
79
 
"ClassGUID = {EB781AAF-9C70-4523-A5DF-642A87ECA567}\n"
80
 
"\n"
81
 
"[ClassInstall]\n"
82
 
"AddReg=libusb_class_install_add_reg\n"
83
 
"\n"
84
 
"[ClassInstall32]\n"
85
 
"AddReg=libusb_class_install_add_reg\n"
86
 
"\n"
87
 
"[libusb_class_install_add_reg]\n"
88
 
"HKR,,,,\"LibUSB-Win32 Devices\"\n"
89
 
"HKR,,Icon,,\"-20\"\n"
90
 
"\n"
91
 
"[Manufacturer]\n"
92
 
"%manufacturer%=Devices,NT,NTAMD64\n"
93
 
"\n"
94
 
";--------------------------------------------------------------------------\n"
95
 
"; Files\n"
96
 
";--------------------------------------------------------------------------\n"
97
 
"\n"
98
 
"[SourceDisksNames]\n"
99
 
"1 = \"Libusb-Win32 Driver Installation Disk\",,\n"
100
 
"\n"
101
 
"[SourceDisksFiles]\n"
102
 
"libusb0.sys = 1,,\n"
103
 
"libusb0.dll = 1,,\n"
104
 
"libusb0_x64.sys = 1,,\n"
105
 
"libusb0_x64.dll = 1,,\n"
106
 
"\n"
107
 
"[DestinationDirs]\n"
108
 
"libusb_files_sys = 10,system32\\drivers\n"
109
 
"libusb_files_sys_x64 = 10,system32\\drivers\n"
110
 
"libusb_files_dll = 10,system32\n"
111
 
"libusb_files_dll_wow64 = 10,syswow64\n"
112
 
"libusb_files_dll_x64 = 10,system32\n"
113
 
"\n"
114
 
"[libusb_files_sys]\n"
115
 
"libusb0.sys\n"
116
 
"\n"
117
 
"[libusb_files_sys_x64]\n"
118
 
"libusb0.sys,libusb0_x64.sys\n"
119
 
"\n"
120
 
"[libusb_files_dll]\n"
121
 
"libusb0.dll\n"
122
 
"\n"
123
 
"[libusb_files_dll_wow64]\n"
124
 
"libusb0.dll\n"
125
 
"\n"
126
 
"[libusb_files_dll_x64]\n"
127
 
"libusb0.dll,libusb0_x64.dll\n"
128
 
"\n"
129
 
";--------------------------------------------------------------------------\n"
130
 
"; Device driver\n"
131
 
";--------------------------------------------------------------------------\n"
132
 
"\n"
133
 
"[LIBUSB_DEV]\n"
134
 
"CopyFiles = libusb_files_sys, libusb_files_dll\n"
135
 
"AddReg    = libusb_add_reg\n"
136
 
"\n"
137
 
"[LIBUSB_DEV.NT]\n"
138
 
"CopyFiles = libusb_files_sys, libusb_files_dll\n"
139
 
"\n"
140
 
"[LIBUSB_DEV.NTAMD64]\n"
141
 
"CopyFiles = libusb_files_sys_x64, libusb_files_dll_wow64, libusb_files_dll_x64\n"
142
 
"\n"
143
 
"[LIBUSB_DEV.HW]\n"
144
 
"DelReg = libusb_del_reg_hw\n"
145
 
"AddReg = libusb_add_reg_hw\n"
146
 
"\n"
147
 
"[LIBUSB_DEV.NT.HW]\n"
148
 
"DelReg = libusb_del_reg_hw\n"
149
 
"AddReg = libusb_add_reg_hw\n"
150
 
"\n"
151
 
"[LIBUSB_DEV.NTAMD64.HW]\n"
152
 
"DelReg = libusb_del_reg_hw\n"
153
 
"AddReg = libusb_add_reg_hw\n"
154
 
"\n"
155
 
"[LIBUSB_DEV.NT.Services]\n"
156
 
"AddService = libusb0, 0x00000002, libusb_add_service\n"
157
 
"\n"
158
 
"[LIBUSB_DEV.NTAMD64.Services]\n"
159
 
"AddService = libusb0, 0x00000002, libusb_add_service\n"
160
 
"\n"
161
 
"[libusb_add_reg]\n"
162
 
"HKR,,DevLoader,,*ntkern\n"
163
 
"HKR,,NTMPDriver,,libusb0.sys\n"
164
 
"\n"
165
 
"; Older versions of this .inf file installed filter drivers. They are not\n"
166
 
"; needed any more and must be removed\n"
167
 
"[libusb_del_reg_hw]\n"
168
 
"HKR,,LowerFilters\n"
169
 
"HKR,,UpperFilters\n"
170
 
"\n"
171
 
"; Device properties\n"
172
 
"[libusb_add_reg_hw]\n"
173
 
"HKR,,SurpriseRemovalOK, 0x00010001, 1\n"
174
 
"\n"
175
 
";--------------------------------------------------------------------------\n"
176
 
"; Services\n"
177
 
";--------------------------------------------------------------------------\n"
178
 
"\n"
179
 
"[libusb_add_service]\n"
180
 
"DisplayName    = \"LibUsb-Win32 - Kernel Driver " 
181
 
STRINGIFY(INF_DATE) ", " STRINGIFY(INF_VERSION) "\"\n"
182
 
"ServiceType    = 1\n"
183
 
"StartType      = 3\n"
184
 
"ErrorControl   = 0\n"
185
 
"ServiceBinary  = %12%\\libusb0.sys\n"
186
 
"\n"
187
 
";--------------------------------------------------------------------------\n"
188
 
"; Devices\n"
189
 
";--------------------------------------------------------------------------\n"
190
 
"\n";
191
 
 
192
 
const char strings_header[] =
193
 
"\n"
194
 
";--------------------------------------------------------------------------\n"
195
 
"; Strings\n"
196
 
";--------------------------------------------------------------------------\n"
197
 
"\n"
198
 
"[Strings]\n";
199
 
 
200
 
typedef struct {
201
 
  int vid;
202
 
  int pid;
203
 
  char description[MAX_PATH];
204
 
  char manufacturer[MAX_PATH];
205
 
} device_context_t;
206
 
 
207
 
 
208
 
BOOL CALLBACK dialog_proc_0(HWND dialog, UINT message, 
209
 
                            WPARAM w_param, LPARAM l_param);
210
 
BOOL CALLBACK dialog_proc_1(HWND dialog, UINT message, 
211
 
                            WPARAM w_param, LPARAM l_param);
212
 
BOOL CALLBACK dialog_proc_2(HWND dialog, UINT message, 
213
 
                            WPARAM w_param, LPARAM l_param);
214
 
BOOL CALLBACK dialog_proc_3(HWND dialog, UINT message, 
215
 
                            WPARAM w_param, LPARAM l_param);
216
 
 
217
 
static void device_list_init(HWND list);
218
 
static void device_list_refresh(HWND list);
219
 
static void device_list_add(HWND list, device_context_t *device);
220
 
static void device_list_clean(HWND list);
221
 
 
222
 
static int save_file(HWND dialog, device_context_t *device);
223
 
 
224
 
int APIENTRY WinMain(HINSTANCE instance, HINSTANCE prev_instance,
225
 
                     LPSTR cmd_line, int cmd_show)
226
 
{
227
 
  int next_dialog;
228
 
  device_context_t device;
229
 
 
230
 
  LoadLibrary("comctl32.dll");
231
 
  InitCommonControls();
232
 
 
233
 
  memset(&device, 0, sizeof(device));
234
 
 
235
 
  next_dialog = ID_DIALOG_0;
236
 
 
237
 
  while(next_dialog)
238
 
    {
239
 
      switch(next_dialog)
240
 
        {
241
 
          case ID_DIALOG_0:
242
 
            next_dialog = (int)DialogBoxParam(instance, 
243
 
                                              MAKEINTRESOURCE(next_dialog), 
244
 
                                              NULL, (DLGPROC)dialog_proc_0,
245
 
                                              (LPARAM)&device);
246
 
 
247
 
            break;
248
 
          case ID_DIALOG_1:
249
 
            next_dialog = (int)DialogBoxParam(instance, 
250
 
                                              MAKEINTRESOURCE(next_dialog), 
251
 
                                              NULL, (DLGPROC)dialog_proc_1,
252
 
                                              (LPARAM)&device);
253
 
            break;
254
 
          case ID_DIALOG_2:
255
 
            next_dialog = (int)DialogBoxParam(instance, 
256
 
                                              MAKEINTRESOURCE(next_dialog), 
257
 
                                              NULL, (DLGPROC)dialog_proc_2,
258
 
                                              (LPARAM)&device);
259
 
            break;
260
 
          case ID_DIALOG_3:
261
 
            next_dialog = (int)DialogBoxParam(instance, 
262
 
                                              MAKEINTRESOURCE(next_dialog), 
263
 
                                              NULL, (DLGPROC)dialog_proc_3,
264
 
                                              (LPARAM)&device);
265
 
            break;
266
 
        default:
267
 
          ;
268
 
        }
269
 
    }
270
 
 
271
 
  return 0;
272
 
}
273
 
 
274
 
 
275
 
BOOL CALLBACK dialog_proc_0(HWND dialog, UINT message, 
276
 
                               WPARAM w_param, LPARAM l_param)
277
 
{
278
 
  switch(message)
279
 
    {
280
 
    case WM_INITDIALOG:
281
 
      SetWindowText(GetDlgItem(dialog, ID_INFO_TEXT), info_text_0);
282
 
      return TRUE;
283
 
      
284
 
    case WM_COMMAND:
285
 
      switch(LOWORD(w_param))
286
 
        {
287
 
        case ID_BUTTON_NEXT:
288
 
          EndDialog(dialog, ID_DIALOG_1);
289
 
          return TRUE ;
290
 
        case ID_BUTTON_CANCEL:
291
 
        case IDCANCEL:
292
 
          EndDialog(dialog, 0);
293
 
          return TRUE ;
294
 
        }
295
 
    }
296
 
  
297
 
  return FALSE;
298
 
}
299
 
 
300
 
BOOL CALLBACK dialog_proc_1(HWND dialog, UINT message, 
301
 
                            WPARAM w_param, LPARAM l_param)
302
 
{
303
 
  static HDEVNOTIFY notification_handle_hub = NULL;
304
 
  static HDEVNOTIFY notification_handle_dev = NULL;
305
 
  DEV_BROADCAST_HDR *hdr = (DEV_BROADCAST_HDR *) l_param;
306
 
  DEV_BROADCAST_DEVICEINTERFACE dev_if;
307
 
  static device_context_t *device = NULL;
308
 
  HWND list = GetDlgItem(dialog, ID_LIST);
309
 
  LVITEM item;
310
 
 
311
 
  switch(message)
312
 
    {
313
 
    case WM_INITDIALOG:
314
 
      device = (device_context_t *)l_param;
315
 
      memset(device, 0, sizeof(*device));
316
 
 
317
 
      SetWindowText(GetDlgItem(dialog, ID_LIST_HEADER_TEXT), list_header_text);
318
 
      device_list_init(list);
319
 
      device_list_refresh(list);
320
 
 
321
 
      dev_if.dbcc_size = sizeof(dev_if);
322
 
      dev_if.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
323
 
      
324
 
      dev_if.dbcc_classguid = GUID_DEVINTERFACE_USB_HUB;
325
 
      notification_handle_hub = RegisterDeviceNotification(dialog, &dev_if, 0);
326
 
      
327
 
      dev_if.dbcc_classguid = GUID_DEVINTERFACE_USB_DEVICE;
328
 
      notification_handle_dev = RegisterDeviceNotification(dialog, &dev_if, 0);
329
 
 
330
 
      return TRUE;
331
 
      
332
 
    case WM_DEVICECHANGE:
333
 
      switch(w_param)
334
 
        {
335
 
        case DBT_DEVICEREMOVECOMPLETE:
336
 
          if(hdr->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE) 
337
 
            device_list_refresh(list);
338
 
          break;
339
 
        case DBT_DEVICEARRIVAL:
340
 
          if(hdr->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE)
341
 
            device_list_refresh(list);
342
 
          break;
343
 
        default:
344
 
          ;
345
 
        }
346
 
      return TRUE;
347
 
 
348
 
    case WM_COMMAND:
349
 
      switch(LOWORD(w_param))
350
 
        {
351
 
        case ID_BUTTON_NEXT:
352
 
          if(notification_handle_hub)
353
 
            UnregisterDeviceNotification(notification_handle_hub);
354
 
          if(notification_handle_dev)
355
 
            UnregisterDeviceNotification(notification_handle_dev);
356
 
 
357
 
          memset(&item, 0, sizeof(item));
358
 
          item.mask = LVIF_TEXT | LVIF_PARAM; 
359
 
          item.iItem = ListView_GetNextItem(list, -1, LVNI_SELECTED);
360
 
          
361
 
          memset(device, 0, sizeof(*device));
362
 
 
363
 
          if(item.iItem >= 0)
364
 
            {
365
 
              if(ListView_GetItem(list, &item))
366
 
                {
367
 
                  if(item.lParam)
368
 
                    {
369
 
                      memcpy(device, (void *)item.lParam, sizeof(*device));
370
 
                    }
371
 
                }
372
 
            }
373
 
 
374
 
          if(!device->vid)
375
 
            {
376
 
              device->vid = 0x12AB;
377
 
              device->pid = 0x12AB;
378
 
            }
379
 
 
380
 
          if(!device->manufacturer[0])
381
 
            strcpy(device->manufacturer, "Insert manufacturer name");
382
 
          if(!device->description[0])
383
 
            strcpy(device->description,  "Insert device description");
384
 
 
385
 
          if(notification_handle_hub)
386
 
            UnregisterDeviceNotification(notification_handle_hub);
387
 
          if(notification_handle_dev)
388
 
            UnregisterDeviceNotification(notification_handle_dev);
389
 
 
390
 
          device_list_clean(list);
391
 
 
392
 
          EndDialog(dialog, ID_DIALOG_2);
393
 
          return TRUE;
394
 
 
395
 
        case ID_BUTTON_BACK:
396
 
          device_list_clean(list);
397
 
          if(notification_handle_hub)
398
 
            UnregisterDeviceNotification(notification_handle_hub);
399
 
          if(notification_handle_dev)
400
 
            UnregisterDeviceNotification(notification_handle_dev);
401
 
          EndDialog(dialog, ID_DIALOG_0);
402
 
          return TRUE ;
403
 
 
404
 
        case ID_BUTTON_CANCEL:
405
 
        case IDCANCEL:
406
 
          device_list_clean(list);
407
 
          if(notification_handle_hub)
408
 
            UnregisterDeviceNotification(notification_handle_hub);
409
 
          if(notification_handle_dev)
410
 
            UnregisterDeviceNotification(notification_handle_dev);
411
 
          EndDialog(dialog, 0);
412
 
          return TRUE ;
413
 
        }
414
 
    }
415
 
  
416
 
  return FALSE;
417
 
}
418
 
 
419
 
BOOL CALLBACK dialog_proc_2(HWND dialog, UINT message, 
420
 
                             WPARAM w_param, LPARAM l_param)
421
 
{
422
 
  static device_context_t *device = NULL;
423
 
  char tmp[MAX_PATH];
424
 
 
425
 
  switch(message)
426
 
    {
427
 
    case WM_INITDIALOG:
428
 
      device = (device_context_t *)l_param;
429
 
 
430
 
      if(device)
431
 
        {
432
 
          memset(tmp, 0, sizeof(tmp));
433
 
          sprintf(tmp, "0x%04X", device->vid);
434
 
          SetWindowText(GetDlgItem(dialog, ID_TEXT_VID), tmp);
435
 
 
436
 
          memset(tmp, 0, sizeof(tmp));
437
 
          sprintf(tmp, "0x%04X", device->pid);
438
 
          SetWindowText(GetDlgItem(dialog, ID_TEXT_PID), tmp);
439
 
 
440
 
          SetWindowText(GetDlgItem(dialog, ID_TEXT_MANUFACTURER), 
441
 
                        device->manufacturer);
442
 
          SetWindowText(GetDlgItem(dialog, ID_TEXT_DEV_NAME),
443
 
                        device->description);
444
 
        }
445
 
      return TRUE;
446
 
      
447
 
    case WM_COMMAND:
448
 
      switch(LOWORD(w_param))
449
 
        {
450
 
        case ID_BUTTON_NEXT:
451
 
          memset(device, 0, sizeof(*device));
452
 
 
453
 
          GetWindowText(GetDlgItem(dialog, ID_TEXT_MANUFACTURER), 
454
 
                        device->manufacturer, sizeof(tmp));
455
 
          GetWindowText(GetDlgItem(dialog, ID_TEXT_DEV_NAME),
456
 
                        device->description, sizeof(tmp));
457
 
 
458
 
          GetWindowText(GetDlgItem(dialog, ID_TEXT_VID), tmp, sizeof(tmp));
459
 
          sscanf(tmp, "0x%04x", &device->vid);
460
 
 
461
 
          GetWindowText(GetDlgItem(dialog, ID_TEXT_PID), tmp, sizeof(tmp));
462
 
          sscanf(tmp, "0x%04x", &device->pid);
463
 
 
464
 
          if(save_file(dialog, device))
465
 
            EndDialog(dialog, ID_DIALOG_3);
466
 
          return TRUE ;
467
 
        case ID_BUTTON_BACK:
468
 
          EndDialog(dialog, ID_DIALOG_1);
469
 
          return TRUE ;
470
 
        case ID_BUTTON_CANCEL:
471
 
        case IDCANCEL:
472
 
          EndDialog(dialog, 0);
473
 
          return TRUE ;
474
 
        }
475
 
    }
476
 
  
477
 
  return FALSE;
478
 
}
479
 
 
480
 
BOOL CALLBACK dialog_proc_3(HWND dialog, UINT message, 
481
 
                            WPARAM w_param, LPARAM l_param)
482
 
{
483
 
  static device_context_t *device = NULL;
484
 
  char tmp[MAX_PATH];
485
 
 
486
 
  switch(message)
487
 
    {
488
 
    case WM_INITDIALOG:
489
 
      device = (device_context_t *)l_param;
490
 
 
491
 
      sprintf(tmp,
492
 
              "%s\n"
493
 
              "Vendor ID:\t\t 0x%04X\n\n"
494
 
              "Product ID:\t\t 0x%04X\n\n"
495
 
              "Device description:\t %s\n\n"
496
 
              "Manufacturer:\t\t %s\n\n",
497
 
              info_text_1,
498
 
              device->vid,
499
 
              device->pid,
500
 
              device->description,
501
 
              device->manufacturer);
502
 
 
503
 
      SetWindowText(GetDlgItem(dialog, ID_INFO_TEXT), tmp);
504
 
      return TRUE;
505
 
      
506
 
    case WM_COMMAND:
507
 
      switch(LOWORD(w_param))
508
 
        {
509
 
        case ID_BUTTON_NEXT:
510
 
        case IDCANCEL:
511
 
          EndDialog(dialog, 0);
512
 
          return TRUE ;
513
 
        }
514
 
    }
515
 
  
516
 
  return FALSE;
517
 
}
518
 
 
519
 
static void device_list_init(HWND list)
520
 
{
521
 
  LVCOLUMN lvc; 
522
 
 
523
 
  ListView_SetExtendedListViewStyle(list, LVS_EX_FULLROWSELECT);
524
 
 
525
 
  memset(&lvc, 0, sizeof(lvc));
526
 
 
527
 
  lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT; 
528
 
  lvc.fmt = LVCFMT_LEFT; 
529
 
 
530
 
  lvc.cx = 70; 
531
 
  lvc.iSubItem = 0;
532
 
  lvc.pszText = "Vendor ID";
533
 
  ListView_InsertColumn(list, 1, &lvc); 
534
 
 
535
 
  lvc.cx = 70; 
536
 
  lvc.iSubItem = 1;
537
 
  lvc.pszText = "Product ID";
538
 
  ListView_InsertColumn(list, 2, &lvc); 
539
 
 
540
 
  lvc.cx = 250; 
541
 
  lvc.iSubItem = 2;
542
 
  lvc.pszText = "Description";
543
 
  ListView_InsertColumn(list, 3, &lvc); 
544
 
}
545
 
 
546
 
static void device_list_refresh(HWND list)
547
 
{
548
 
  HDEVINFO dev_info;
549
 
  SP_DEVINFO_DATA dev_info_data;
550
 
  int dev_index = 0;
551
 
  device_context_t *device;
552
 
  char tmp[MAX_PATH];
553
 
 
554
 
  device_list_clean(list);
555
 
 
556
 
  dev_info_data.cbSize = sizeof(SP_DEVINFO_DATA);
557
 
  dev_index = 0;
558
 
 
559
 
  dev_info = SetupDiGetClassDevs(NULL, "USB", NULL,
560
 
                                 DIGCF_ALLCLASSES | DIGCF_PRESENT);
561
 
  
562
 
  if(dev_info == INVALID_HANDLE_VALUE)
563
 
    {
564
 
      return;
565
 
    }
566
 
  
567
 
  while(SetupDiEnumDeviceInfo(dev_info, dev_index, &dev_info_data))
568
 
    {
569
 
      if(usb_registry_match(dev_info, &dev_info_data))
570
 
        {
571
 
          device = (device_context_t *) malloc(sizeof(device_context_t));
572
 
          memset(device, 0, sizeof(*device));
573
 
        
574
 
          usb_registry_get_property(SPDRP_HARDWAREID, dev_info, 
575
 
                                    &dev_info_data,
576
 
                                    tmp, sizeof(tmp) - 1);
577
 
 
578
 
          sscanf(tmp + sizeof("USB\\VID_") - 1, "%04x", &device->vid);
579
 
          sscanf(tmp + sizeof("USB\\VID_XXXX&PID_") - 1, "%04x", &device->pid);
580
 
                    
581
 
          usb_registry_get_property(SPDRP_DEVICEDESC, dev_info, 
582
 
                                    &dev_info_data,
583
 
                                    tmp, sizeof(tmp) - 1);
584
 
          strcpy(device->description, tmp);
585
 
 
586
 
          usb_registry_get_property(SPDRP_MFG, dev_info, 
587
 
                                    &dev_info_data,
588
 
                                    tmp, sizeof(tmp) - 1);
589
 
          strcpy(device->manufacturer, tmp);
590
 
 
591
 
          device_list_add(list, device);
592
 
        }
593
 
 
594
 
      dev_index++;
595
 
    }
596
 
  
597
 
  SetupDiDestroyDeviceInfoList(dev_info);
598
 
}
599
 
 
600
 
static void device_list_add(HWND list, device_context_t *device)
601
 
{
602
 
  LVITEM item;
603
 
  char vid[32];
604
 
  char pid[32];
605
 
 
606
 
  memset(&item, 0, sizeof(item));
607
 
  memset(vid, 0, sizeof(vid));
608
 
  memset(pid, 0, sizeof(pid));
609
 
 
610
 
  sprintf(vid, "0x%04X", device->vid);
611
 
  sprintf(pid, "0x%04X", device->pid);
612
 
 
613
 
  item.mask = LVIF_TEXT | LVIF_PARAM; 
614
 
  item.lParam = (LPARAM)device;
615
 
 
616
 
  ListView_InsertItem(list, &item);                  
617
 
 
618
 
  ListView_SetItemText(list, 0, 0, vid);
619
 
  ListView_SetItemText(list, 0, 1, pid);
620
 
  ListView_SetItemText(list, 0, 2, device->description);
621
 
}
622
 
 
623
 
static void device_list_clean(HWND list)
624
 
{
625
 
  LVITEM item;
626
 
 
627
 
  memset(&item, 0, sizeof(LVITEM));
628
 
 
629
 
  while(ListView_GetItem(list, &item))
630
 
    {
631
 
      if(item.lParam)
632
 
        free((void *)item.lParam);
633
 
      
634
 
      ListView_DeleteItem(list, 0);
635
 
      memset(&item, 0, sizeof(LVITEM));
636
 
    }
637
 
}
638
 
 
639
 
static int save_file(HWND dialog, device_context_t *device)
640
 
{
641
 
  OPENFILENAME open_file;
642
 
  char inf_name[MAX_PATH];
643
 
  char inf_path[MAX_PATH];
644
 
 
645
 
  char cat_name[MAX_PATH];
646
 
  char cat_path[MAX_PATH];
647
 
 
648
 
  char cat_name_x64[MAX_PATH];
649
 
  char cat_path_x64[MAX_PATH];
650
 
 
651
 
  char error[MAX_PATH];
652
 
  FILE *file;
653
 
 
654
 
  memset(&open_file, 0, sizeof(open_file));
655
 
  strcpy(inf_path, "your_file.inf");
656
 
 
657
 
  open_file.lStructSize = sizeof(OPENFILENAME);
658
 
  open_file.hwndOwner = dialog;
659
 
  open_file.lpstrFile = inf_path;
660
 
  open_file.nMaxFile = sizeof(inf_path);
661
 
  open_file.lpstrFilter = "*.inf\0*.inf\0";
662
 
  open_file.nFilterIndex = 1;
663
 
  open_file.lpstrFileTitle = inf_name;
664
 
  open_file.nMaxFileTitle = sizeof(inf_name);
665
 
  open_file.lpstrInitialDir = NULL;
666
 
  open_file.Flags = OFN_PATHMUSTEXIST;
667
 
  open_file.lpstrDefExt = "inf";
668
 
  
669
 
  if(GetSaveFileName(&open_file))
670
 
    {
671
 
      strcpy(cat_path, inf_path);
672
 
      strcpy(cat_name, inf_name);
673
 
      strcpy(cat_path_x64, inf_path);
674
 
      strcpy(cat_name_x64, inf_name);
675
 
 
676
 
      strcpy(strstr(cat_path, ".inf"), ".cat");
677
 
      strcpy(strstr(cat_name, ".inf"), ".cat");
678
 
      strcpy(strstr(cat_path_x64, ".inf"), "_x64.cat");
679
 
      strcpy(strstr(cat_name_x64, ".inf"), "_x64.cat");
680
 
 
681
 
      file = fopen(inf_path, "w");
682
 
 
683
 
      if(file)
684
 
        {
685
 
          fprintf(file, "%s", inf_header);
686
 
          fprintf(file, "CatalogFile = %s\n", cat_name);
687
 
          fprintf(file, "CatalogFile.NT = %s\n", cat_name);
688
 
          fprintf(file, "CatalogFile.NTAMD64 = %s\n\n", cat_name_x64);
689
 
          fprintf(file, "%s", inf_body);
690
 
 
691
 
          fprintf(file, "[Devices]\n");
692
 
          fprintf(file, "\"%s\"=LIBUSB_DEV, USB\\VID_%04x&PID_%04x\n\n", 
693
 
                  device->description,
694
 
                  device->vid, device->pid);
695
 
          fprintf(file, "[Devices.NT]\n");
696
 
          fprintf(file, "\"%s\"=LIBUSB_DEV, USB\\VID_%04x&PID_%04x\n\n", 
697
 
                  device->description,
698
 
                  device->vid, device->pid);
699
 
          fprintf(file, "[Devices.NTAMD64]\n");
700
 
          fprintf(file, "\"%s\"=LIBUSB_DEV, USB\\VID_%04x&PID_%04x\n\n", 
701
 
                  device->description,
702
 
                  device->vid, device->pid);
703
 
 
704
 
          fprintf(file, strings_header);
705
 
          fprintf(file, "manufacturer = \"%s\"\n", device->manufacturer);
706
 
 
707
 
          fclose(file);
708
 
        }
709
 
      else
710
 
        {
711
 
          sprintf(error, "Error: unable to open file: %s", inf_name);
712
 
          MessageBox(dialog, error, "Error",
713
 
                     MB_OK | MB_APPLMODAL | MB_ICONWARNING);
714
 
        }
715
 
 
716
 
 
717
 
      file = fopen(cat_path, "w");
718
 
 
719
 
      if(file)
720
 
        {
721
 
          fprintf(file, "%s", cat_file_content);
722
 
          fclose(file);
723
 
        }
724
 
      else
725
 
        {
726
 
          sprintf(error, "Error: unable to open file: %s", cat_name);
727
 
          MessageBox(dialog, error, "Error",
728
 
                     MB_OK | MB_APPLMODAL | MB_ICONWARNING);
729
 
        }
730
 
 
731
 
      file = fopen(cat_path_x64, "w");
732
 
 
733
 
      if(file)
734
 
        {
735
 
          fprintf(file, "%s", cat_file_content);
736
 
          fclose(file);
737
 
        }
738
 
      else
739
 
        {
740
 
          sprintf(error, "Error: unable to open file: %s", cat_name_x64);
741
 
          MessageBox(dialog, error, "Error",
742
 
                     MB_OK | MB_APPLMODAL | MB_ICONWARNING);
743
 
        }
744
 
 
745
 
      return TRUE;
746
 
    }
747
 
  return FALSE;
748
 
}