PCem

view src/allegro-gui-hdconf.c @ 154:d0d530adce12

Initial port to Linux (using Allegro). 64-bit fixes. Some changes to aid portability. A few other tweaks.
author TomW
date Thu Sep 04 21:07:24 2014 +0100
parents
children
line source
1 #define _LARGEFILE_SOURCE
2 #define _LARGEFILE64_SOURCE
3 #define _GNU_SOURCE
5 #include <stdio.h>
6 #include "ibm.h"
7 #include "ide.h"
8 #include "allegro-main.h"
9 #include "allegro-gui.h"
11 static char hd_path[2][260];
12 static char hd_sectors[2][10];
13 static char hd_heads[2][10];
14 static char hd_cylinders[2][10];
15 static char hd_size[2][20];
17 static char hd_path_new[260];
18 static char hd_sectors_new[10];
19 static char hd_heads_new[10];
20 static char hd_cylinders_new[10];
21 static char hd_size_new[20];
23 static PcemHDC hdc_new[2];
25 static DIALOG hdparams_dialog[]=
26 {
27 {d_shadow_box_proc, 0, 0, 194*2,86,0,0xffffff,0,0, 0,0,0,0,0}, // 0
29 {d_button_proc, 126, 66, 50, 14, 0, 0xffffff, 0, D_EXIT, 0, 0, "OK", 0, 0}, // 1
30 {d_button_proc, 196, 66, 50, 16, 0, 0xffffff, 0, D_EXIT, 0, 0, "Cancel", 0, 0}, // 2
32 {d_text_proc, 7*2, 6, 170, 10, 0, 0xffffff, 0, 0, 0, 0, "Initial settings are based on file size", 0, 0},
34 {d_text_proc, 7*2, 22, 27, 10, 0, 0xffffff, 0, 0, 0, 0, "Sectors:", 0, 0},
35 {d_text_proc, 63*2, 22, 29, 8, 0, 0xffffff, 0, 0, 0, 0, "Heads:", 0, 0},
36 {d_text_proc, 120*2, 22, 28, 12, 0, 0xffffff, 0, 0, 0, 0, "Cylinders:", 0, 0},
37 {d_edit_proc, 44*2, 22, 16*2, 12, 0, 0xffffff, 0, 0, 2, 0, hd_sectors_new, 0, 0},
38 {d_edit_proc, 92*2, 22, 16*2, 12, 0, 0xffffff, 0, 0, 3, 0, hd_heads_new, 0, 0},
39 {d_edit_proc, 168*2, 22, 24*2, 12, 0, 0xffffff, 0, 0, 5, 0, hd_cylinders_new, 0, 0},
40 {d_text_proc, 7*2, 54, 136, 12, 0, 0xffffff, 0, 0, 0, 0, hd_size_new, 0, 0},
42 {0,0,0,0,0,0,0,0,0,0,0,NULL,NULL,NULL}
43 };
45 static int hdconf_open(int msg, DIALOG *d, int c)
46 {
47 int drv = d->d2;
48 int ret = d_button_proc(msg, d, c);
50 if (ret == D_EXIT)
51 {
52 char fn[260];
53 int xsize = SCREEN_W - 32, ysize = SCREEN_H - 64;
55 strcpy(fn, hd_path[drv]);
56 ret = file_select_ex("Please choose a disc image", fn, "IMG", 260, xsize, ysize);
57 if (ret)
58 {
59 uint64_t sz;
60 FILE *f = fopen64(fn, "rb");
61 if (!f)
62 {
63 return D_REDRAW;
64 }
65 fseeko64(f, -1, SEEK_END);
66 sz = ftello64(f) + 1;
67 fclose(f);
68 sprintf(hd_sectors_new, "63");
69 sprintf(hd_heads_new, "16");
70 sprintf(hd_cylinders_new, "%i", (int)((sz / 512) / 16) / 63);
72 while (1)
73 {
74 position_dialog(hdparams_dialog, SCREEN_W/2 - 186, SCREEN_H/2 - 86/2);
76 ret = popup_dialog(hdparams_dialog, 1);
78 position_dialog(hdparams_dialog, -(SCREEN_W/2 - 186), -(SCREEN_H/2 - 86/2));
80 if (ret == 1)
81 {
82 int spt, hpc, cyl;
83 sscanf(hd_sectors_new, "%i", &spt);
84 sscanf(hd_heads_new, "%i", &hpc);
85 sscanf(hd_cylinders_new, "%i", &cyl);
87 if (spt > 63)
88 {
89 alert("Drive has too many sectors (maximum is 63)", NULL, NULL, "OK", NULL, 0, 0);
90 continue;
91 }
92 if (hpc > 128)
93 {
94 alert("Drive has too many heads (maximum is 128)", NULL, NULL, "OK", NULL, 0, 0);
95 continue;
96 }
97 if (cyl > 16383)
98 {
99 alert("Drive has too many cylinders (maximum is 16383)", NULL, NULL, "OK", NULL, 0, 0);
100 continue;
101 }
103 hdc_new[drv].spt = spt;
104 hdc_new[drv].hpc = hpc;
105 hdc_new[drv].tracks = cyl;
107 strcpy(hd_path[drv], fn);
108 sprintf(hd_sectors[drv], "%i", hdc_new[drv].spt);
109 sprintf(hd_heads[drv], "%i", hdc_new[drv].hpc);
110 sprintf(hd_cylinders[drv], "%i", hdc_new[drv].tracks);
111 sprintf(hd_size[drv], "Size : %imb", (((((uint64_t)hdc_new[drv].tracks*(uint64_t)hdc_new[drv].hpc)*(uint64_t)hdc_new[drv].spt)*512)/1024)/1024);
113 return D_REDRAW;
114 }
116 if (ret == 2)
117 break;
118 }
119 }
121 return D_REDRAW;
122 }
124 return ret;
125 }
127 static int hdconf_new_file(int msg, DIALOG *d, int c)
128 {
129 int ret = d_button_proc(msg, d, c);
131 if (ret == D_EXIT)
132 {
133 char fn[260];
134 int xsize = SCREEN_W - 32, ysize = SCREEN_H - 64;
136 strcpy(fn, hd_path_new);
137 ret = file_select_ex("Please choose a disc image", fn, "IMG", 260, xsize, ysize);
138 if (ret)
139 strcpy(hd_path_new, fn);
141 return D_REDRAW;
142 }
144 return ret;
145 }
147 static DIALOG hdnew_dialog[]=
148 {
149 {d_shadow_box_proc, 0, 0, 194*2,86,0,0xffffff,0,0, 0,0,0,0,0}, // 0
151 {d_button_proc, 126, 66, 50, 14, 0, 0xffffff, 0, D_EXIT, 0, 0, "OK", 0, 0}, // 1
152 {d_button_proc, 196, 66, 50, 16, 0, 0xffffff, 0, D_EXIT, 0, 0, "Cancel", 0, 0}, // 2
154 {d_edit_proc, 7*2, 6, 136*2, 10, 0, 0xffffff, 0, 0, 0, 0, hd_path_new, 0, 0},
155 {hdconf_new_file, 143*2, 6, 16*2, 14, 0, 0xffffff, 0, D_EXIT, 0, 0, "...", 0, 0},
157 {d_text_proc, 7*2, 22, 27, 10, 0, 0xffffff, 0, 0, 0, 0, "Sectors:", 0, 0},
158 {d_text_proc, 63*2, 22, 29, 8, 0, 0xffffff, 0, 0, 0, 0, "Heads:", 0, 0},
159 {d_text_proc, 120*2, 22, 28, 12, 0, 0xffffff, 0, 0, 0, 0, "Cylinders:", 0, 0},
160 {d_edit_proc, 44*2, 22, 16*2, 12, 0, 0xffffff, 0, 0, 2, 0, hd_sectors_new, 0, 0},
161 {d_edit_proc, 92*2, 22, 16*2, 12, 0, 0xffffff, 0, 0, 3, 0, hd_heads_new, 0, 0},
162 {d_edit_proc, 168*2, 22, 24*2, 12, 0, 0xffffff, 0, 0, 5, 0, hd_cylinders_new, 0, 0},
163 // {d_text_proc, 7*2, 54, 136, 12, 0, -1, 0, 0, 0, 0, hd_size_new, 0, 0},
165 {0,0,0,0,0,0,0,0,0,0,0,NULL,NULL,NULL}
166 };
168 static int create_hd(char *fn, int cyl, int hpc, int spt)
169 {
170 int c;
171 int e;
172 uint8_t buf[512];
173 FILE *f = fopen64(hd_path_new, "wb");
174 e = errno;
175 if (!f)
176 {
177 alert("Can't open file for write", NULL, NULL, "OK", NULL, 0, 0);
178 return -1;
179 }
180 memset(buf, 0, 512);
181 for (c = 0; c < (cyl * hpc * spt); c++)
182 {
183 fwrite(buf, 512, 1, f);
184 }
185 fclose(f);
186 }
188 static int hdconf_new(int msg, DIALOG *d, int c)
189 {
190 int drv = d->d2;
191 int ret = d_button_proc(msg, d, c);
193 if (ret == D_EXIT)
194 {
195 sprintf(hd_sectors_new, "63");
196 sprintf(hd_heads_new, "16");
197 sprintf(hd_cylinders_new, "511");
198 strcpy(hd_path_new, "");
200 while (1)
201 {
202 position_dialog(hdnew_dialog, SCREEN_W/2 - 186, SCREEN_H/2 - 86/2);
204 ret = popup_dialog(hdnew_dialog, 1);
206 position_dialog(hdnew_dialog, -(SCREEN_W/2 - 186), -(SCREEN_H/2 - 86/2));
208 if (ret == 1)
209 {
210 int spt, hpc, cyl;
211 int c, d;
212 FILE *f;
213 uint8_t *buf;
215 sscanf(hd_sectors_new, "%i", &spt);
216 sscanf(hd_heads_new, "%i", &hpc);
217 sscanf(hd_cylinders_new, "%i", &cyl);
219 if (spt > 63)
220 {
221 alert("Drive has too many sectors (maximum is 63)", NULL, NULL, "OK", NULL, 0, 0);
222 continue;
223 }
224 if (hpc > 128)
225 {
226 alert("Drive has too many heads (maximum is 128)", NULL, NULL, "OK", NULL, 0, 0);
227 continue;
228 }
229 if (cyl > 16383)
230 {
231 alert("Drive has too many cylinders (maximum is 16383)", NULL, NULL, "OK", NULL, 0, 0);
232 continue;
233 }
234 if (create_hd(hd_path_new, cyl, hpc, spt))
235 return D_REDRAW;
237 alert("Remember to partition and format the new drive", NULL, NULL, "OK", NULL, 0, 0);
239 hdc_new[drv].spt = spt;
240 hdc_new[drv].hpc = hpc;
241 hdc_new[drv].tracks = cyl;
243 strcpy(hd_path[drv], hd_path_new);
244 sprintf(hd_sectors[drv], "%i", hdc_new[drv].spt);
245 sprintf(hd_heads[drv], "%i", hdc_new[drv].hpc);
246 sprintf(hd_cylinders[drv], "%i", hdc_new[drv].tracks);
247 sprintf(hd_size[drv], "Size : %imb", (((((uint64_t)hdc_new[drv].tracks*(uint64_t)hdc_new[drv].hpc)*(uint64_t)hdc_new[drv].spt)*512)/1024)/1024);
249 return D_REDRAW;
250 }
252 if (ret == 2)
253 break;
254 }
256 return D_REDRAW;
257 }
259 return ret;
260 }
262 static int hdconf_eject(int msg, DIALOG *d, int c)
263 {
264 int drv = d->d2;
265 int ret = d_button_proc(msg, d, c);
267 if (ret == D_EXIT)
268 {
269 hdc_new[drv].spt = 0;
270 hdc_new[drv].hpc = 0;
271 hdc_new[drv].tracks = 0;
272 strcpy(hd_path[drv], "");
273 sprintf(hd_sectors[drv], "%i", hdc_new[drv].spt);
274 sprintf(hd_heads[drv], "%i", hdc_new[drv].hpc);
275 sprintf(hd_cylinders[drv], "%i", hdc_new[drv].tracks);
276 sprintf(hd_size[drv], "Size : %imb", (((((uint64_t)hdc_new[drv].tracks*(uint64_t)hdc_new[drv].hpc)*(uint64_t)hdc_new[drv].spt)*512)/1024)/1024);
278 return D_REDRAW;
279 }
281 return ret;
282 }
284 static DIALOG hdconf_dialog[]=
285 {
286 {d_shadow_box_proc, 0, 0, 210*2,172,0,0xffffff,0,0, 0,0,0,0,0}, // 0
288 {d_button_proc, 150, 152, 50, 14, 0, 0xffffff, 0, D_EXIT, 0, 0, "OK", 0, 0}, // 1
289 {d_button_proc, 220, 152, 50, 16, 0, 0xffffff, 0, D_EXIT, 0, 0, "Cancel", 0, 0}, // 2
291 {d_text_proc, 7*2, 6, 27, 10, 0, 0xffffff, 0, 0, 0, 0, "C:", 0, 0},
292 {d_edit_proc, 7*2, 22, 136*2, 12, 0, 0xffffff, 0, D_DISABLED, 0, 0, hd_path[0], 0, 0},
293 {hdconf_open, 143*2, 22, 16*2, 14, 0, 0xffffff, 0, D_EXIT, 0, 0, "...", 0, 0},
294 {hdconf_new, 159*2, 22, 24*2, 14, 0, 0xffffff, 0, D_EXIT, 0, 0, "New", 0, 0},
295 {hdconf_eject, 183*2, 22, 24*2, 14, 0, 0xffffff, 0, D_EXIT, 0, 0, "Eject", 0, 0},
297 {d_text_proc, 7*2, 38, 27, 10, 0, 0xffffff, 0, 0, 0, 0, "Sectors:", 0, 0},
298 {d_text_proc, 63*2, 38, 29, 8, 0, 0xffffff, 0, 0, 0, 0, "Heads:", 0, 0},
299 {d_text_proc, 120*2, 38, 28, 12, 0, 0xffffff, 0, 0, 0, 0, "Cylinders:", 0, 0},
300 {d_edit_proc, 44*2, 38, 16*2, 12, 0, 0xffffff, 0, D_DISABLED, 0, 0, hd_sectors[0], 0, 0},
301 {d_edit_proc, 92*2, 38, 16*2, 12, 0, 0xffffff, 0, D_DISABLED, 0, 0, hd_heads[0], 0, 0},
302 {d_edit_proc, 168*2, 38, 24*2, 12, 0, 0xffffff, 0, D_DISABLED, 0, 0, hd_cylinders[0], 0, 0},
303 {d_text_proc, 7*2, 54, 136, 12, 0, 0xffffff, 0, 0, 0, 0, hd_size[0], 0, 0},
305 {d_text_proc, 7*2, 76, 27, 10, 0, 0xffffff, 0, 0, 0, 0, "D:", 0, 0},
306 {d_edit_proc, 7*2, 92, 136*2, 12, 0, 0xffffff, 0, D_DISABLED, 0, 0, hd_path[1], 0, 0},
307 {hdconf_open, 143*2, 92, 16*2, 14, 0, 0xffffff, 0, D_EXIT, 0, 1, "...", 0, 0},
308 {hdconf_new, 159*2, 92, 24*2, 14, 0, 0xffffff, 0, D_EXIT, 0, 1, "New", 0, 0},
309 {hdconf_eject, 183*2, 92, 24*2, 14, 0, 0xffffff, 0, D_EXIT, 0, 1, "Eject", 0, 0},
311 {d_edit_proc, 44*2, 108, 16*2, 12, 0, 0xffffff, 0, D_DISABLED, 0, 0, hd_sectors[1], 0, 0},
312 {d_edit_proc, 92*2, 108, 16*2, 12, 0, 0xffffff, 0, D_DISABLED, 0, 0, hd_heads[1], 0, 0},
313 {d_edit_proc, 168*2, 108, 24*2, 12, 0, 0xffffff, 0, D_DISABLED, 0, 0, hd_cylinders[1], 0, 0},
314 {d_text_proc, 7*2, 108, 27, 10, 0, 0xffffff, 0, 0, 0, 0, "Sectors:", 0, 0},
315 {d_text_proc, 63*2, 108, 29, 8, 0, 0xffffff, 0, 0, 0, 0, "Heads:", 0, 0},
316 {d_text_proc, 120*2, 108, 32, 12, 0, 0xffffff, 0, 0, 0, 0, "Cylinders:", 0, 0},
317 {d_text_proc, 7*2, 124, 136, 12, 0, 0xffffff, 0, 0, 0, 0, hd_size[1], 0, 0},
319 {0,0,0,0,0,0,0,0,0,0,0,NULL,NULL,NULL}
320 };
322 int disc_hdconf()
323 {
324 int c;
325 int changed=0;
327 hdc_new[0] = hdc[0];
328 hdc_new[1] = hdc[1];
329 strcpy(hd_path[0], ide_fn[0]);
330 strcpy(hd_path[1], ide_fn[1]);
331 sprintf(hd_sectors[0], "%i", hdc[0].spt);
332 sprintf(hd_sectors[1], "%i", hdc[1].spt);
333 sprintf(hd_heads[0], "%i", hdc[0].hpc);
334 sprintf(hd_heads[1], "%i", hdc[1].hpc);
335 sprintf(hd_cylinders[0], "%i", hdc[0].tracks);
336 sprintf(hd_cylinders[1], "%i", hdc[1].tracks);
337 sprintf(hd_size[0], "Size : %imb", (((((uint64_t)hdc[0].tracks*(uint64_t)hdc[0].hpc)*(uint64_t)hdc[0].spt)*512)/1024)/1024);
338 sprintf(hd_size[1], "Size : %imb", (((((uint64_t)hdc[1].tracks*(uint64_t)hdc[1].hpc)*(uint64_t)hdc[1].spt)*512)/1024)/1024);
340 while (1)
341 {
342 position_dialog(hdconf_dialog, SCREEN_W/2 - 210, SCREEN_H/2 - 172/2);
344 c = popup_dialog(hdconf_dialog, 1);
346 position_dialog(hdconf_dialog, -(SCREEN_W/2 - 210), -(SCREEN_H/2 - 172/2));
348 if (c == 1)
349 {
350 if (alert("This will reset PCem!", "Okay to continue?", NULL, "OK", "Cancel", 0, 0) == 1)
351 {
352 hdc[0] = hdc_new[0];
353 hdc[1] = hdc_new[1];
355 strcpy(ide_fn[0], hd_path[0]);
356 strcpy(ide_fn[1], hd_path[1]);
358 saveconfig();
360 resetpchard();
362 return D_O_K;
363 }
364 }
365 if (c == 2)
366 return D_O_K;
367 }
369 return D_O_K;
370 }