foo
[instimg] / src / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <ctype.h>
4 #include <windows.h>
5 #include "widgets.h"
6 #include "rawdisk.h"
7
8 static struct wgt_window *win;
9 static struct wgt_widget *lb_instto, *bn_inst, *bn_cancel, *cb_devs, *ck_usbonly;
10
11 static struct rawdisk_device rawdev[64];
12 static const char *items[64];
13 static int num_rawdev;
14
15
16 static void update_disks(void);
17 static void onclick(struct wgt_widget *w);
18 static void onmodify(struct wgt_widget *w);
19
20
21 int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hprevinst, char *cmdline, int showcmd)
22 {
23         int x, y;
24         MSG msg;
25
26
27         if(!(win = wgt_window("256boss USB stick installer", 400, 300))) {
28                 return 1;
29         }
30         lb_instto = wgt_label(win, "Install to device:", 10, 10);
31
32         x = wgt_xpos_after(lb_instto, WGT_AUTO);
33         cb_devs = wgt_combo(win, items, num_rawdev, 0, x, 10, 200, WGT_AUTO, onmodify);
34
35         y = wgt_ypos_after(cb_devs, 16);
36         ck_usbonly = wgt_checkbox(win, "only show USB devices", 1, x, y,
37                         WGT_AUTO, WGT_AUTO, 0);
38
39         y = wgt_ypos_after(ck_usbonly, 16);
40         bn_inst = wgt_button(win, "Install", 10, y, WGT_AUTO, WGT_AUTO, onclick);
41
42         x = wgt_xpos_after(bn_inst, WGT_AUTO);
43         bn_cancel = wgt_button(win, "Cancel", x, y, WGT_AUTO, WGT_AUTO, onclick);
44
45         update_disks();
46
47         while(GetMessage(&msg, 0, 0, 0)) {
48                 TranslateMessage(&msg);
49                 DispatchMessage(&msg);
50         }
51
52         wgt_destroy_window(win);
53         return 0;
54 }
55
56 static int isusbdev(struct rawdisk_device *dev)
57 {
58         char *ptr = dev->path;
59         while(*ptr) {
60                 int i;
61                 char buf[3];
62                 for(i=0; i<3; i++) {
63                         buf[i] = tolower(ptr[i]);
64                 }
65                 if(memcmp(buf, "usb", 3) == 0) {
66                         return 1;
67                 }
68                 ptr++;
69         }
70         return 0;
71 }
72
73 static void update_disks(void)
74 {
75         int i;
76
77         for(i=0; i<num_rawdev; i++) {
78                 free(rawdev[i].name);
79                 free(rawdev[i].path);
80         }
81
82         if((num_rawdev = rawdisk_detect(rawdev, sizeof rawdev / sizeof *rawdev)) == -1) {
83                 MessageBox(0, "Failed to detect storage devices!", 0, MB_OK);
84                 exit(1);
85         }
86
87         if(wgt_checkbox_checked(ck_usbonly)) {
88                 for(i=0; i<num_rawdev; i++) {
89                         if(!isusbdev(rawdev + i)) {
90                                 free(rawdev[i].name);
91                                 free(rawdev[i].path);
92                                 rawdev[i] = rawdev[--num_rawdev];
93                         }
94                 }
95         }
96
97         for(i=0; i<num_rawdev; i++) {
98                 items[i] = rawdev[i].name;
99         }
100
101         wgt_combo_setitems(cb_devs, items, num_rawdev);
102
103         if(num_rawdev) {
104                 wgt_enable_widget(cb_devs);
105                 wgt_enable_widget(bn_inst);
106         } else {
107                 wgt_disable_widget(cb_devs);
108                 wgt_disable_widget(bn_inst);
109         }
110 }
111
112 static void onclick(struct wgt_widget *w)
113 {
114         if(w == bn_inst) {
115                 MessageBox(0, "clicked", "clicked", MB_OK);
116         } else if(w == bn_cancel) {
117                 PostQuitMessage(0);
118         } else if(w == ck_usbonly) {
119                 update_disks();
120         }
121 }
122
123 static void onmodify(struct wgt_widget *w)
124 {
125         int sel = wgt_combo_selected(w);
126         if(sel >= 0) {
127                 const char *selstr = wgt_get_combo_item(w, sel);
128                 MessageBox(0, selstr, "selected item", MB_OK);
129         }
130 }