ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/prefs_editor_gtk.cpp
Revision: 1.6
Committed: 1999-10-19T17:41:36Z (25 years, 1 month ago) by cebix
Branch: MAIN
Changes since 1.5: +5 -1 lines
Log Message:
- added external file system
- moved most init/deinit code to InitAll()/ExitAll() in main.cpp

File Contents

# User Rev Content
1 cebix 1.1 /*
2     * prefs_editor_gtk.cpp - Preferences editor, Unix implementation using GTK+
3     *
4     * Basilisk II (C) 1997-1999 Christian Bauer
5     *
6     * This program is free software; you can redistribute it and/or modify
7     * it under the terms of the GNU General Public License as published by
8     * the Free Software Foundation; either version 2 of the License, or
9     * (at your option) any later version.
10     *
11     * This program is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with this program; if not, write to the Free Software
18     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19     */
20    
21     #include "sysdeps.h"
22    
23     #include <gtk/gtk.h>
24     #include <stdlib.h>
25     #include <dirent.h>
26     #include <sys/socket.h>
27     #include <sys/ioctl.h>
28     #include <net/if.h>
29     #include <net/if_arp.h>
30    
31     #include "user_strings.h"
32     #include "version.h"
33     #include "cdrom.h"
34     #include "xpram.h"
35     #include "prefs.h"
36     #include "prefs_editor.h"
37    
38    
39     // Global variables
40     static GtkWidget *win; // Preferences window
41     static bool start_clicked = true; // Return value of PrefsEditor() function
42    
43    
44     // Prototypes
45     static void create_volumes_pane(GtkWidget *top);
46     static void create_scsi_pane(GtkWidget *top);
47     static void create_graphics_pane(GtkWidget *top);
48     static void create_serial_pane(GtkWidget *top);
49     static void create_memory_pane(GtkWidget *top);
50     static void read_settings(void);
51    
52    
53     /*
54     * Utility functions
55     */
56    
57     struct opt_desc {
58     int label_id;
59     GtkSignalFunc func;
60     };
61    
62     static void add_menu_item(GtkWidget *menu, int label_id, GtkSignalFunc func)
63     {
64     GtkWidget *item = gtk_menu_item_new_with_label(GetString(label_id));
65     gtk_widget_show(item);
66     gtk_signal_connect(GTK_OBJECT(item), "activate", func, NULL);
67     gtk_menu_append(GTK_MENU(menu), item);
68     }
69    
70     static GtkWidget *make_pane(GtkWidget *notebook, int title_id)
71     {
72     GtkWidget *frame, *label, *box;
73    
74     frame = gtk_frame_new(NULL);
75     gtk_widget_show(frame);
76     gtk_container_border_width(GTK_CONTAINER(frame), 4);
77    
78     label = gtk_label_new(GetString(title_id));
79     gtk_notebook_append_page(GTK_NOTEBOOK(notebook), frame, label);
80    
81     box = gtk_vbox_new(FALSE, 4);
82     gtk_widget_show(box);
83     gtk_container_set_border_width(GTK_CONTAINER(box), 4);
84     gtk_container_add(GTK_CONTAINER(frame), box);
85     return box;
86     }
87    
88     static GtkWidget *make_button_box(GtkWidget *top, int border, const opt_desc *buttons)
89     {
90     GtkWidget *bb, *button;
91    
92     bb = gtk_hbutton_box_new();
93     gtk_widget_show(bb);
94     gtk_container_set_border_width(GTK_CONTAINER(bb), border);
95     gtk_button_box_set_layout(GTK_BUTTON_BOX(bb), GTK_BUTTONBOX_DEFAULT_STYLE);
96     gtk_button_box_set_spacing(GTK_BUTTON_BOX(bb), 4);
97     gtk_box_pack_start(GTK_BOX(top), bb, FALSE, FALSE, 0);
98    
99     while (buttons->label_id) {
100     button = gtk_button_new_with_label(GetString(buttons->label_id));
101     gtk_widget_show(button);
102     gtk_signal_connect_object(GTK_OBJECT(button), "clicked", buttons->func, NULL);
103     gtk_box_pack_start(GTK_BOX(bb), button, TRUE, TRUE, 0);
104     buttons++;
105     }
106     return bb;
107     }
108    
109     static GtkWidget *make_separator(GtkWidget *top)
110     {
111     GtkWidget *sep = gtk_hseparator_new();
112     gtk_box_pack_start(GTK_BOX(top), sep, FALSE, FALSE, 0);
113     gtk_widget_show(sep);
114     return sep;
115     }
116    
117     static GtkWidget *make_table(GtkWidget *top, int x, int y)
118     {
119     GtkWidget *table = gtk_table_new(x, y, FALSE);
120     gtk_widget_show(table);
121     gtk_box_pack_start(GTK_BOX(top), table, FALSE, FALSE, 0);
122     return table;
123     }
124    
125     static GtkWidget *make_option_menu(GtkWidget *top, int label_id, const opt_desc *options, int active)
126     {
127     GtkWidget *box, *label, *opt, *menu;
128    
129     box = gtk_hbox_new(FALSE, 4);
130     gtk_widget_show(box);
131     gtk_box_pack_start(GTK_BOX(top), box, FALSE, FALSE, 0);
132    
133     label = gtk_label_new(GetString(label_id));
134     gtk_widget_show(label);
135     gtk_box_pack_start(GTK_BOX(box), label, FALSE, FALSE, 0);
136    
137     opt = gtk_option_menu_new();
138     gtk_widget_show(opt);
139     menu = gtk_menu_new();
140    
141     while (options->label_id) {
142     add_menu_item(menu, options->label_id, options->func);
143     options++;
144     }
145     gtk_menu_set_active(GTK_MENU(menu), active);
146    
147     gtk_option_menu_set_menu(GTK_OPTION_MENU(opt), menu);
148     gtk_box_pack_start(GTK_BOX(box), opt, FALSE, FALSE, 0);
149     return menu;
150     }
151    
152     static GtkWidget *make_entry(GtkWidget *top, int label_id, const char *prefs_item)
153     {
154     GtkWidget *box, *label, *entry;
155    
156     box = gtk_hbox_new(FALSE, 4);
157     gtk_widget_show(box);
158     gtk_box_pack_start(GTK_BOX(top), box, FALSE, FALSE, 0);
159    
160     label = gtk_label_new(GetString(label_id));
161     gtk_widget_show(label);
162     gtk_box_pack_start(GTK_BOX(box), label, FALSE, FALSE, 0);
163    
164     entry = gtk_entry_new();
165     gtk_widget_show(entry);
166     const char *str = PrefsFindString(prefs_item);
167     if (str == NULL)
168     str = "";
169     gtk_entry_set_text(GTK_ENTRY(entry), str);
170     gtk_box_pack_start(GTK_BOX(box), entry, TRUE, TRUE, 0);
171     return entry;
172     }
173    
174     static GtkWidget *make_checkbox(GtkWidget *top, int label_id, const char *prefs_item, GtkSignalFunc func)
175     {
176     GtkWidget *button = gtk_check_button_new_with_label(GetString(label_id));
177     gtk_widget_show(button);
178     gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(button), PrefsFindBool(prefs_item));
179     gtk_signal_connect(GTK_OBJECT(button), "toggled", func, button);
180     gtk_box_pack_start(GTK_BOX(top), button, FALSE, FALSE, 0);
181     return button;
182     }
183    
184    
185     /*
186     * Show preferences editor
187     * Returns true when user clicked on "Start", false otherwise
188     */
189    
190     // Window closed
191     static gint window_closed(void)
192     {
193     return FALSE;
194     }
195    
196     // Window destroyed
197     static void window_destroyed(void)
198     {
199     gtk_main_quit();
200     }
201    
202     // "Start" button clicked
203     static void cb_start(...)
204     {
205     start_clicked = true;
206     read_settings();
207     SavePrefs();
208     gtk_widget_destroy(win);
209     }
210    
211     // "Quit" button clicked
212     static void cb_quit(...)
213     {
214     start_clicked = false;
215     gtk_widget_destroy(win);
216     }
217    
218     // "OK" button of "About" dialog clicked
219     static void dl_quit(GtkWidget *dialog)
220     {
221     gtk_widget_destroy(dialog);
222     }
223    
224     // "About" selected
225     static void mn_about(...)
226     {
227     GtkWidget *dialog, *label, *button;
228    
229     char str[256];
230     sprintf(str, GetString(STR_ABOUT_TEXT1), VERSION_MAJOR, VERSION_MINOR);
231     strncat(str, "\n", 255);
232     strncat(str, GetString(STR_ABOUT_TEXT2), 255);
233    
234     dialog = gtk_dialog_new();
235     gtk_widget_set_usize(GTK_WIDGET(dialog), strlen(GetString(STR_ABOUT_TEXT2)) + 200, 120);
236     gtk_window_set_title(GTK_WINDOW(dialog), GetString(STR_ABOUT_TITLE));
237     gtk_container_border_width(GTK_CONTAINER(dialog), 5);
238     gtk_widget_set_uposition(GTK_WIDGET(dialog), 100, 150);
239    
240     label = gtk_label_new(str);
241     gtk_widget_show(label);
242     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), label, TRUE, TRUE, 0);
243    
244     button = gtk_button_new_with_label(GetString(STR_OK_BUTTON));
245     gtk_widget_show(button);
246     gtk_signal_connect_object(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(dl_quit), GTK_OBJECT(dialog));
247     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area), button, FALSE, FALSE, 0);
248     GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
249     gtk_widget_grab_default(button);
250     gtk_widget_show(dialog);
251     }
252    
253     // "Zap PRAM" selected
254     static void mn_zap_pram(...)
255     {
256     ZapPRAM();
257     }
258    
259     // Menu item descriptions
260     static GtkItemFactoryEntry menu_items[] = {
261 cebix 1.4 {(gchar *)GetString(STR_PREFS_MENU_FILE_GTK), NULL, NULL, 0, "<Branch>"},
262     {(gchar *)GetString(STR_PREFS_ITEM_START_GTK), NULL, GTK_SIGNAL_FUNC(cb_start), 0, NULL},
263     {(gchar *)GetString(STR_PREFS_ITEM_ZAP_PRAM_GTK), NULL, GTK_SIGNAL_FUNC(mn_zap_pram), 0, NULL},
264     {(gchar *)GetString(STR_PREFS_ITEM_SEPL_GTK), NULL, NULL, 0, "<Separator>"},
265     {(gchar *)GetString(STR_PREFS_ITEM_QUIT_GTK), "<control>Q", GTK_SIGNAL_FUNC(cb_quit), 0, NULL},
266     {(gchar *)GetString(STR_HELP_MENU_GTK), NULL, NULL, 0, "<LastBranch>"},
267     {(gchar *)GetString(STR_HELP_ITEM_ABOUT_GTK), NULL, GTK_SIGNAL_FUNC(mn_about), 0, NULL}
268 cebix 1.1 };
269    
270     bool PrefsEditor(void)
271     {
272     // Create window
273     win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
274     gtk_window_set_title(GTK_WINDOW(win), GetString(STR_PREFS_TITLE));
275     gtk_signal_connect(GTK_OBJECT(win), "delete_event", GTK_SIGNAL_FUNC(window_closed), NULL);
276     gtk_signal_connect(GTK_OBJECT(win), "destroy", GTK_SIGNAL_FUNC(window_destroyed), NULL);
277    
278     // Create window contents
279     GtkWidget *box = gtk_vbox_new(FALSE, 4);
280     gtk_widget_show(box);
281     gtk_container_add(GTK_CONTAINER(win), box);
282    
283     GtkAccelGroup *accel_group = gtk_accel_group_new();
284     GtkItemFactory *item_factory = gtk_item_factory_new(GTK_TYPE_MENU_BAR, "<main>", accel_group);
285     gtk_item_factory_create_items(item_factory, sizeof(menu_items) / sizeof(menu_items[0]), menu_items, NULL);
286     gtk_accel_group_attach(accel_group, GTK_OBJECT(win));
287     GtkWidget *menu_bar = gtk_item_factory_get_widget(item_factory, "<main>");
288     gtk_widget_show(menu_bar);
289     gtk_box_pack_start(GTK_BOX(box), menu_bar, FALSE, TRUE, 0);
290    
291     GtkWidget *notebook = gtk_notebook_new();
292     gtk_widget_show(notebook);
293     gtk_notebook_set_tab_pos(GTK_NOTEBOOK(notebook), GTK_POS_TOP);
294     gtk_notebook_set_scrollable(GTK_NOTEBOOK(notebook), TRUE);
295     gtk_box_pack_start(GTK_BOX(box), notebook, TRUE, TRUE, 0);
296    
297     create_volumes_pane(notebook);
298     create_scsi_pane(notebook);
299     create_graphics_pane(notebook);
300     create_serial_pane(notebook);
301     create_memory_pane(notebook);
302    
303     static const opt_desc buttons[] = {
304     {STR_START_BUTTON, GTK_SIGNAL_FUNC(cb_start)},
305     {STR_QUIT_BUTTON, GTK_SIGNAL_FUNC(cb_quit)},
306     {0, NULL}
307     };
308     make_button_box(box, 4, buttons);
309    
310     // Show window and enter main loop
311     gtk_widget_show(win);
312     gtk_main();
313     return start_clicked;
314     }
315    
316    
317     /*
318     * "Volumes" pane
319     */
320    
321 cebix 1.6 static GtkWidget *volume_list, *w_extfs;
322 cebix 1.1 static int selected_volume;
323    
324     // Volume in list selected
325     static void cl_selected(GtkWidget *list, int row, int column)
326     {
327     selected_volume = row;
328     }
329    
330     struct file_req_assoc {
331     file_req_assoc(GtkWidget *r, GtkWidget *e) : req(r), entry(e) {}
332     GtkWidget *req;
333     GtkWidget *entry;
334     };
335    
336     // Volume selected for addition
337     static void add_volume_ok(GtkWidget *button, file_req_assoc *assoc)
338     {
339     char *file = gtk_file_selection_get_filename(GTK_FILE_SELECTION(assoc->req));
340     gtk_clist_append(GTK_CLIST(volume_list), &file);
341     gtk_widget_destroy(assoc->req);
342     delete assoc;
343     }
344    
345     // Volume selected for creation
346     static void create_volume_ok(GtkWidget *button, file_req_assoc *assoc)
347     {
348     char *file = gtk_file_selection_get_filename(GTK_FILE_SELECTION(assoc->req));
349    
350     char *str = gtk_entry_get_text(GTK_ENTRY(assoc->entry));
351     int size = atoi(str);
352    
353     char cmd[1024];
354     sprintf(cmd, "dd if=/dev/zero \"of=%s\" bs=1024k count=%d", file, size);
355     int ret = system(cmd);
356     if (ret == 0)
357     gtk_clist_append(GTK_CLIST(volume_list), &file);
358     gtk_widget_destroy(GTK_WIDGET(assoc->req));
359     delete assoc;
360     }
361    
362     // "Add Volume" button clicked
363     static void cb_add_volume(...)
364     {
365     GtkWidget *req = gtk_file_selection_new(GetString(STR_ADD_VOLUME_TITLE));
366     gtk_signal_connect_object(GTK_OBJECT(req), "delete_event", GTK_SIGNAL_FUNC(gtk_widget_destroy), GTK_OBJECT(req));
367     gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(req)->ok_button), "clicked", GTK_SIGNAL_FUNC(add_volume_ok), new file_req_assoc(req, NULL));
368     gtk_signal_connect_object(GTK_OBJECT(GTK_FILE_SELECTION(req)->cancel_button), "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy), GTK_OBJECT(req));
369     gtk_widget_show(req);
370     }
371    
372     // "Create Hardfile" button clicked
373     static void cb_create_volume(...)
374     {
375     GtkWidget *req = gtk_file_selection_new(GetString(STR_CREATE_VOLUME_TITLE));
376    
377     GtkWidget *box = gtk_hbox_new(FALSE, 4);
378     gtk_widget_show(box);
379     GtkWidget *label = gtk_label_new(GetString(STR_HARDFILE_SIZE_CTRL));
380     gtk_widget_show(label);
381     GtkWidget *entry = gtk_entry_new();
382     gtk_widget_show(entry);
383     char str[32];
384     sprintf(str, "%d", 40);
385     gtk_entry_set_text(GTK_ENTRY(entry), str);
386     gtk_box_pack_start(GTK_BOX(box), label, FALSE, FALSE, 0);
387     gtk_box_pack_start(GTK_BOX(box), entry, FALSE, FALSE, 0);
388     gtk_box_pack_start(GTK_BOX(GTK_FILE_SELECTION(req)->main_vbox), box, FALSE, FALSE, 0);
389    
390     gtk_signal_connect_object(GTK_OBJECT(req), "delete_event", GTK_SIGNAL_FUNC(gtk_widget_destroy), GTK_OBJECT(req));
391     gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(req)->ok_button), "clicked", GTK_SIGNAL_FUNC(create_volume_ok), new file_req_assoc(req, entry));
392     gtk_signal_connect_object(GTK_OBJECT(GTK_FILE_SELECTION(req)->cancel_button), "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy), GTK_OBJECT(req));
393     gtk_widget_show(req);
394     }
395    
396     // "Remove Volume" button clicked
397     static void cb_remove_volume(...)
398     {
399     gtk_clist_remove(GTK_CLIST(volume_list), selected_volume);
400     }
401    
402     // "Boot From" selected
403     static void mn_boot_any(...) {PrefsReplaceInt16("bootdriver", 0);}
404     static void mn_boot_cdrom(...) {PrefsReplaceInt16("bootdriver", CDROMRefNum);}
405    
406     // "No CD-ROM Driver" button toggled
407     static void tb_nocdrom(GtkWidget *widget)
408     {
409     PrefsReplaceBool("nocdrom", GTK_TOGGLE_BUTTON(widget)->active);
410     }
411    
412     // Read settings from widgets and set preferences
413     static void read_volumes_settings(void)
414     {
415     while (PrefsFindString("disk"))
416     PrefsRemoveItem("disk");
417    
418     for (int i=0; i<GTK_CLIST(volume_list)->rows; i++) {
419     char *str;
420     gtk_clist_get_text(GTK_CLIST(volume_list), i, 0, &str);
421     PrefsAddString("disk", str);
422     }
423 cebix 1.6
424     PrefsReplaceString("extfs", gtk_entry_get_text(GTK_ENTRY(w_extfs)));
425 cebix 1.1 }
426    
427     // Create "Volumes" pane
428     static void create_volumes_pane(GtkWidget *top)
429     {
430     GtkWidget *box, *scroll, *menu;
431    
432     box = make_pane(top, STR_VOLUMES_PANE_TITLE);
433    
434     scroll = gtk_scrolled_window_new(NULL, NULL);
435     gtk_widget_show(scroll);
436     gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
437     volume_list = gtk_clist_new(1);
438     gtk_widget_show(volume_list);
439     gtk_clist_set_selection_mode(GTK_CLIST(volume_list), GTK_SELECTION_SINGLE);
440     gtk_clist_set_shadow_type(GTK_CLIST(volume_list), GTK_SHADOW_NONE);
441 cebix 1.5 gtk_clist_set_reorderable(GTK_CLIST(volume_list), true);
442 cebix 1.1 gtk_signal_connect(GTK_OBJECT(volume_list), "select_row", GTK_SIGNAL_FUNC(cl_selected), NULL);
443     char *str;
444     int32 index = 0;
445     while ((str = (char *)PrefsFindString("disk", index++)) != NULL)
446     gtk_clist_append(GTK_CLIST(volume_list), &str);
447     gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scroll), volume_list);
448     gtk_box_pack_start(GTK_BOX(box), scroll, TRUE, TRUE, 0);
449     selected_volume = 0;
450    
451     static const opt_desc buttons[] = {
452     {STR_ADD_VOLUME_BUTTON, GTK_SIGNAL_FUNC(cb_add_volume)},
453     {STR_CREATE_VOLUME_BUTTON, GTK_SIGNAL_FUNC(cb_create_volume)},
454     {STR_REMOVE_VOLUME_BUTTON, GTK_SIGNAL_FUNC(cb_remove_volume)},
455     {0, NULL},
456     };
457     make_button_box(box, 0, buttons);
458     make_separator(box);
459 cebix 1.6
460     w_extfs = make_entry(box, STR_EXTFS_CTRL, "extfs");
461 cebix 1.1
462     static const opt_desc options[] = {
463     {STR_BOOT_ANY_LAB, GTK_SIGNAL_FUNC(mn_boot_any)},
464     {STR_BOOT_CDROM_LAB, GTK_SIGNAL_FUNC(mn_boot_cdrom)},
465     {0, NULL}
466     };
467     int bootdriver = PrefsFindInt16("bootdriver"), active = 0;
468     switch (bootdriver) {
469     case 0: active = 0; break;
470     case CDROMRefNum: active = 1; break;
471     }
472     menu = make_option_menu(box, STR_BOOTDRIVER_CTRL, options, active);
473    
474     make_checkbox(box, STR_NOCDROM_CTRL, "nocdrom", GTK_SIGNAL_FUNC(tb_nocdrom));
475     }
476    
477    
478     /*
479     * "SCSI" pane
480     */
481    
482     static GtkWidget *w_scsi[7];
483    
484     // Read settings from widgets and set preferences
485     static void read_scsi_settings(void)
486     {
487     for (int id=0; id<7; id++) {
488     char prefs_name[32];
489     sprintf(prefs_name, "scsi%d", id);
490     const char *str = gtk_entry_get_text(GTK_ENTRY(w_scsi[id]));
491     if (str && strlen(str))
492     PrefsReplaceString(prefs_name, str);
493     else
494     PrefsRemoveItem(prefs_name);
495     }
496     }
497    
498     // Create "SCSI" pane
499     static void create_scsi_pane(GtkWidget *top)
500     {
501     GtkWidget *box;
502    
503     box = make_pane(top, STR_SCSI_PANE_TITLE);
504    
505     for (int id=0; id<7; id++) {
506     char prefs_name[32];
507     sprintf(prefs_name, "scsi%d", id);
508     w_scsi[id] = make_entry(box, STR_SCSI_ID_0 + id, prefs_name);
509     }
510     }
511    
512    
513     /*
514     * "Graphics/Sound" pane
515     */
516    
517     // Display types
518     enum {
519     DISPLAY_WINDOW,
520     DISPLAY_SCREEN
521     };
522    
523     static GtkWidget *w_frameskip, *w_display_x, *w_display_y;
524     static GtkWidget *l_frameskip, *l_display_x, *l_display_y;
525     static int display_type;
526     static int dis_width, dis_height;
527    
528     // Hide/show graphics widgets
529     static void hide_show_graphics_widgets(void)
530     {
531     switch (display_type) {
532     case DISPLAY_WINDOW:
533     gtk_widget_show(w_frameskip); gtk_widget_show(l_frameskip);
534     break;
535     case DISPLAY_SCREEN:
536     gtk_widget_hide(w_frameskip); gtk_widget_hide(l_frameskip);
537     break;
538     }
539     }
540    
541     // "Window" video type selected
542     static void mn_window(...)
543     {
544     display_type = DISPLAY_WINDOW;
545     hide_show_graphics_widgets();
546     }
547    
548     // "Fullscreen" video type selected
549     static void mn_fullscreen(...)
550     {
551     display_type = DISPLAY_SCREEN;
552     hide_show_graphics_widgets();
553     }
554    
555     // "5 Hz".."60Hz" selected
556     static void mn_5hz(...) {PrefsReplaceInt32("frameskip", 12);}
557     static void mn_7hz(...) {PrefsReplaceInt32("frameskip", 8);}
558     static void mn_10hz(...) {PrefsReplaceInt32("frameskip", 6);}
559     static void mn_15hz(...) {PrefsReplaceInt32("frameskip", 4);}
560     static void mn_30hz(...) {PrefsReplaceInt32("frameskip", 2);}
561     static void mn_60hz(...) {PrefsReplaceInt32("frameskip", 1);}
562    
563     // "Disable Sound Output" button toggled
564     static void tb_nosound(GtkWidget *widget)
565     {
566     PrefsReplaceBool("nosound", GTK_TOGGLE_BUTTON(widget)->active);
567     }
568    
569     // Read graphics preferences
570     static void parse_graphics_prefs(void)
571     {
572     display_type = DISPLAY_WINDOW;
573     dis_width = 512;
574     dis_height = 384;
575    
576     const char *str = PrefsFindString("screen");
577     if (str) {
578     if (sscanf(str, "win/%d/%d", &dis_width, &dis_height) == 2)
579     display_type = DISPLAY_WINDOW;
580 cebix 1.2 else if (sscanf(str, "dga/%d/%d", &dis_width, &dis_height) == 2)
581 cebix 1.1 display_type = DISPLAY_SCREEN;
582     }
583     }
584    
585     // Read settings from widgets and set preferences
586     static void read_graphics_settings(void)
587     {
588     const char *str;
589    
590     str = gtk_entry_get_text(GTK_ENTRY(w_display_x));
591     dis_width = atoi(str);
592    
593     str = gtk_entry_get_text(GTK_ENTRY(w_display_y));
594     dis_height = atoi(str);
595    
596     char pref[256];
597     switch (display_type) {
598     case DISPLAY_WINDOW:
599     sprintf(pref, "win/%d/%d", dis_width, dis_height);
600     break;
601     case DISPLAY_SCREEN:
602 cebix 1.2 sprintf(pref, "dga/%d/%d", dis_width, dis_height);
603 cebix 1.1 break;
604     default:
605     PrefsRemoveItem("screen");
606     return;
607     }
608     PrefsReplaceString("screen", pref);
609     }
610    
611     // Create "Graphics/Sound" pane
612     static void create_graphics_pane(GtkWidget *top)
613     {
614 cebix 1.2 GtkWidget *box, *table, *label, *opt, *menu, *combo;
615 cebix 1.1 char str[32];
616    
617     parse_graphics_prefs();
618    
619     box = make_pane(top, STR_GRAPHICS_SOUND_PANE_TITLE);
620     table = make_table(box, 2, 4);
621    
622     label = gtk_label_new(GetString(STR_VIDEO_TYPE_CTRL));
623     gtk_widget_show(label);
624     gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, 1, (GtkAttachOptions)0, (GtkAttachOptions)0, 4, 4);
625    
626     opt = gtk_option_menu_new();
627     gtk_widget_show(opt);
628     menu = gtk_menu_new();
629     add_menu_item(menu, STR_WINDOW_LAB, GTK_SIGNAL_FUNC(mn_window));
630     add_menu_item(menu, STR_FULLSCREEN_LAB, GTK_SIGNAL_FUNC(mn_fullscreen));
631     switch (display_type) {
632     case DISPLAY_WINDOW:
633     gtk_menu_set_active(GTK_MENU(menu), 0);
634     break;
635     case DISPLAY_SCREEN:
636     gtk_menu_set_active(GTK_MENU(menu), 1);
637     break;
638     }
639     gtk_option_menu_set_menu(GTK_OPTION_MENU(opt), menu);
640     gtk_table_attach(GTK_TABLE(table), opt, 1, 2, 0, 1, (GtkAttachOptions)GTK_FILL, (GtkAttachOptions)0, 4, 4);
641    
642     l_frameskip = gtk_label_new(GetString(STR_FRAMESKIP_CTRL));
643     gtk_widget_show(l_frameskip);
644     gtk_table_attach(GTK_TABLE(table), l_frameskip, 0, 1, 1, 2, (GtkAttachOptions)0, (GtkAttachOptions)0, 4, 4);
645    
646     w_frameskip = gtk_option_menu_new();
647     gtk_widget_show(w_frameskip);
648     menu = gtk_menu_new();
649     add_menu_item(menu, STR_REF_5HZ_LAB, GTK_SIGNAL_FUNC(mn_5hz));
650     add_menu_item(menu, STR_REF_7_5HZ_LAB, GTK_SIGNAL_FUNC(mn_7hz));
651     add_menu_item(menu, STR_REF_10HZ_LAB, GTK_SIGNAL_FUNC(mn_10hz));
652     add_menu_item(menu, STR_REF_15HZ_LAB, GTK_SIGNAL_FUNC(mn_15hz));
653     add_menu_item(menu, STR_REF_30HZ_LAB, GTK_SIGNAL_FUNC(mn_30hz));
654     add_menu_item(menu, STR_REF_60HZ_LAB, GTK_SIGNAL_FUNC(mn_60hz));
655     int frameskip = PrefsFindInt32("frameskip");
656     switch (frameskip) {
657     case 12:
658     gtk_menu_set_active(GTK_MENU(menu), 0);
659     break;
660     case 8:
661     gtk_menu_set_active(GTK_MENU(menu), 1);
662     break;
663     case 6:
664     gtk_menu_set_active(GTK_MENU(menu), 2);
665     break;
666     case 4:
667     gtk_menu_set_active(GTK_MENU(menu), 3);
668     break;
669     case 2:
670     gtk_menu_set_active(GTK_MENU(menu), 4);
671     break;
672     case 1:
673     gtk_menu_set_active(GTK_MENU(menu), 5);
674     break;
675     }
676     gtk_option_menu_set_menu(GTK_OPTION_MENU(w_frameskip), menu);
677     gtk_table_attach(GTK_TABLE(table), w_frameskip, 1, 2, 1, 2, (GtkAttachOptions)GTK_FILL, (GtkAttachOptions)0, 4, 4);
678    
679     l_display_x = gtk_label_new(GetString(STR_DISPLAY_X_CTRL));
680     gtk_widget_show(l_display_x);
681     gtk_table_attach(GTK_TABLE(table), l_display_x, 0, 1, 2, 3, (GtkAttachOptions)0, (GtkAttachOptions)0, 4, 4);
682    
683 cebix 1.2 combo = gtk_combo_new();
684     gtk_widget_show(combo);
685     GList *glist1 = NULL;
686 cebix 1.4 glist1 = g_list_append(glist1, (void *)GetString(STR_SIZE_512_LAB));
687     glist1 = g_list_append(glist1, (void *)GetString(STR_SIZE_640_LAB));
688     glist1 = g_list_append(glist1, (void *)GetString(STR_SIZE_800_LAB));
689     glist1 = g_list_append(glist1, (void *)GetString(STR_SIZE_1024_LAB));
690     glist1 = g_list_append(glist1, (void *)GetString(STR_SIZE_MAX_LAB));
691 cebix 1.2 gtk_combo_set_popdown_strings(GTK_COMBO(combo), glist1);
692     if (dis_width)
693     sprintf(str, "%d", dis_width);
694     else
695 cebix 1.3 strcpy(str, GetString(STR_SIZE_MAX_LAB));
696 cebix 1.2 gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(combo)->entry), str);
697     gtk_table_attach(GTK_TABLE(table), combo, 1, 2, 2, 3, (GtkAttachOptions)GTK_FILL, (GtkAttachOptions)0, 4, 4);
698     w_display_x = GTK_COMBO(combo)->entry;
699 cebix 1.1
700     l_display_y = gtk_label_new(GetString(STR_DISPLAY_Y_CTRL));
701     gtk_widget_show(l_display_y);
702     gtk_table_attach(GTK_TABLE(table), l_display_y, 0, 1, 3, 4, (GtkAttachOptions)0, (GtkAttachOptions)0, 4, 4);
703    
704 cebix 1.2 combo = gtk_combo_new();
705     gtk_widget_show(combo);
706     GList *glist2 = NULL;
707 cebix 1.4 glist2 = g_list_append(glist2, (void *)GetString(STR_SIZE_384_LAB));
708     glist2 = g_list_append(glist2, (void *)GetString(STR_SIZE_480_LAB));
709     glist2 = g_list_append(glist2, (void *)GetString(STR_SIZE_600_LAB));
710     glist2 = g_list_append(glist2, (void *)GetString(STR_SIZE_768_LAB));
711     glist2 = g_list_append(glist2, (void *)GetString(STR_SIZE_MAX_LAB));
712 cebix 1.2 gtk_combo_set_popdown_strings(GTK_COMBO(combo), glist2);
713     if (dis_height)
714     sprintf(str, "%d", dis_height);
715     else
716 cebix 1.3 strcpy(str, GetString(STR_SIZE_MAX_LAB));
717 cebix 1.2 gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(combo)->entry), str);
718     gtk_table_attach(GTK_TABLE(table), combo, 1, 2, 3, 4, (GtkAttachOptions)GTK_FILL, (GtkAttachOptions)0, 4, 4);
719     w_display_y = GTK_COMBO(combo)->entry;
720 cebix 1.1
721     make_checkbox(box, STR_NOSOUND_CTRL, "nosound", GTK_SIGNAL_FUNC(tb_nosound));
722    
723     hide_show_graphics_widgets();
724     }
725    
726    
727     /*
728     * "Serial/Network" pane
729     */
730    
731     static GtkWidget *w_seriala, *w_serialb, *w_ether;
732    
733     // Read settings from widgets and set preferences
734     static void read_serial_settings(void)
735     {
736     const char *str;
737    
738     str = gtk_entry_get_text(GTK_ENTRY(w_seriala));
739     PrefsReplaceString("seriala", str);
740    
741     str = gtk_entry_get_text(GTK_ENTRY(w_serialb));
742     PrefsReplaceString("serialb", str);
743    
744     str = gtk_entry_get_text(GTK_ENTRY(w_ether));
745     if (str && strlen(str))
746     PrefsReplaceString("ether", str);
747     else
748     PrefsRemoveItem("ether");
749     }
750    
751     // Add names of serial devices
752     static gint gl_str_cmp(gconstpointer a, gconstpointer b)
753     {
754     return strcmp((char *)a, (char *)b);
755     }
756    
757     static GList *add_serial_names(void)
758     {
759     GList *glist = NULL;
760    
761     // Search /dev for ttyS* and lp*
762     DIR *d = opendir("/dev");
763     if (d) {
764     struct dirent *de;
765     while ((de = readdir(d)) != NULL) {
766     #if defined(__linux__)
767     if (strncmp(de->d_name, "ttyS", 4) == 0 || strncmp(de->d_name, "lp", 2) == 0) {
768     #elif defined(__FreeBSD__)
769     if (strncmp(de->d_name, "cuaa", 4) == 0 || strncmp(de->d_name, "lpt", 3) == 0) {
770 cebix 1.4 #elif defined(__NetBSD__)
771     if (strncmp(de->d_name, "tty0", 4) == 0 || strncmp(de->d_name, "lpt", 3) == 0) {
772 cebix 1.1 #elif defined(sgi)
773     if (strncmp(de->d_name, "ttyf", 4) == 0 || strncmp(de->d_name, "plp", 3) == 0) {
774     #else
775     if (false) {
776     #endif
777     char *str = new char[64];
778     sprintf(str, "/dev/%s", de->d_name);
779     glist = g_list_append(glist, str);
780     }
781     }
782     closedir(d);
783     }
784     if (glist)
785     g_list_sort(glist, gl_str_cmp);
786     else
787 cebix 1.4 glist = g_list_append(glist, (void *)GetString(STR_NONE_LAB));
788 cebix 1.1 return glist;
789     }
790    
791     // Add names of ethernet interfaces
792     static GList *add_ether_names(void)
793     {
794     GList *glist = NULL;
795    
796     // Get list of all Ethernet interfaces
797     int s = socket(PF_INET, SOCK_DGRAM, 0);
798     if (s >= 0) {
799     char inbuf[8192];
800     struct ifconf ifc;
801     ifc.ifc_len = sizeof(inbuf);
802     ifc.ifc_buf = inbuf;
803     if (ioctl(s, SIOCGIFCONF, &ifc) == 0) {
804     struct ifreq req, *ifr = ifc.ifc_req;
805     for (int i=0; i<ifc.ifc_len; i+=sizeof(ifreq), ifr++) {
806     req = *ifr;
807 cebix 1.4 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(sgi)
808 cebix 1.1 if (ioctl(s, SIOCGIFADDR, &req) == 0 && (req.ifr_addr.sa_family == ARPHRD_ETHER || req.ifr_addr.sa_family == ARPHRD_ETHER+1)) {
809 cebix 1.4 #elif defined(__linux__)
810     if (ioctl(s, SIOCGIFHWADDR, &req) == 0 && req.ifr_hwaddr.sa_family == ARPHRD_ETHER) {
811 cebix 1.1 #else
812 cebix 1.4 if (false) {
813 cebix 1.1 #endif
814     char *str = new char[64];
815     strncpy(str, ifr->ifr_name, 63);
816     glist = g_list_append(glist, str);
817     }
818     }
819     }
820     close(s);
821     }
822     if (glist)
823     g_list_sort(glist, gl_str_cmp);
824     else
825 cebix 1.4 glist = g_list_append(glist, (void *)GetString(STR_NONE_LAB));
826 cebix 1.1 return glist;
827     }
828    
829     // Create "Serial/Network" pane
830     static void create_serial_pane(GtkWidget *top)
831     {
832     GtkWidget *box, *table, *label, *combo;
833     GList *glist = add_serial_names();
834    
835     box = make_pane(top, STR_SERIAL_NETWORK_PANE_TITLE);
836     table = make_table(box, 2, 3);
837    
838     label = gtk_label_new(GetString(STR_SERIALA_CTRL));
839     gtk_widget_show(label);
840     gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, 1, (GtkAttachOptions)0, (GtkAttachOptions)0, 4, 4);
841    
842     combo = gtk_combo_new();
843     gtk_widget_show(combo);
844     gtk_combo_set_popdown_strings(GTK_COMBO(combo), glist);
845     const char *str = PrefsFindString("seriala");
846     if (str == NULL)
847     str = "";
848     gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(combo)->entry), str);
849     gtk_table_attach(GTK_TABLE(table), combo, 1, 2, 0, 1, (GtkAttachOptions)(GTK_FILL | GTK_EXPAND), (GtkAttachOptions)0, 4, 4);
850     w_seriala = GTK_COMBO(combo)->entry;
851    
852     label = gtk_label_new(GetString(STR_SERIALB_CTRL));
853     gtk_widget_show(label);
854     gtk_table_attach(GTK_TABLE(table), label, 0, 1, 1, 2, (GtkAttachOptions)0, (GtkAttachOptions)0, 4, 4);
855    
856     combo = gtk_combo_new();
857     gtk_widget_show(combo);
858     gtk_combo_set_popdown_strings(GTK_COMBO(combo), glist);
859     str = PrefsFindString("serialb");
860     if (str == NULL)
861     str = "";
862     gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(combo)->entry), str);
863     gtk_table_attach(GTK_TABLE(table), combo, 1, 2, 1, 2, (GtkAttachOptions)(GTK_FILL | GTK_EXPAND), (GtkAttachOptions)0, 4, 4);
864     w_serialb = GTK_COMBO(combo)->entry;
865    
866     label = gtk_label_new(GetString(STR_ETHERNET_IF_CTRL));
867     gtk_widget_show(label);
868     gtk_table_attach(GTK_TABLE(table), label, 0, 1, 2, 3, (GtkAttachOptions)0, (GtkAttachOptions)0, 4, 4);
869    
870     glist = add_ether_names();
871     combo = gtk_combo_new();
872     gtk_widget_show(combo);
873     gtk_combo_set_popdown_strings(GTK_COMBO(combo), glist);
874     str = PrefsFindString("ether");
875     if (str == NULL)
876     str = "";
877     gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(combo)->entry), str);
878     gtk_table_attach(GTK_TABLE(table), combo, 1, 2, 2, 3, (GtkAttachOptions)(GTK_FILL | GTK_EXPAND), (GtkAttachOptions)0, 4, 4);
879     w_ether = GTK_COMBO(combo)->entry;
880     }
881    
882    
883     /*
884     * "Memory/Misc" pane
885     */
886    
887     static GtkObject *w_ramsize_adj;
888     static GtkWidget *w_rom_file;
889     static GtkWidget *w_keycode_file;
890    
891     // Model ID selected
892     static void mn_modelid_5(...) {PrefsReplaceInt32("modelid", 5);}
893     static void mn_modelid_14(...) {PrefsReplaceInt32("modelid", 14);}
894    
895     // "Use Raw Keycodes" button toggled
896     static void tb_keycodes(GtkWidget *widget)
897     {
898     PrefsReplaceBool("keycodes", GTK_TOGGLE_BUTTON(widget)->active);
899     }
900    
901     // Read settings from widgets and set preferences
902     static void read_memory_settings(void)
903     {
904     PrefsReplaceInt32("ramsize", int(GTK_ADJUSTMENT(w_ramsize_adj)->value) << 20);
905    
906     const char *str = gtk_entry_get_text(GTK_ENTRY(w_rom_file));
907     if (str && strlen(str))
908     PrefsReplaceString("rom", str);
909     else
910     PrefsRemoveItem("rom");
911    
912     str = gtk_entry_get_text(GTK_ENTRY(w_keycode_file));
913     if (str && strlen(str))
914     PrefsReplaceString("keycodefile", str);
915     else
916     PrefsRemoveItem("keycodefile");
917     }
918    
919     // Create "Memory/Misc" pane
920     static void create_memory_pane(GtkWidget *top)
921     {
922 cebix 1.4 GtkWidget *box, *vbox, *hbox, *hbox2, *label, *scale, *menu;
923 cebix 1.1
924     box = make_pane(top, STR_MEMORY_MISC_PANE_TITLE);
925    
926     hbox = gtk_hbox_new(FALSE, 4);
927     gtk_widget_show(hbox);
928    
929     label = gtk_label_new(GetString(STR_RAMSIZE_SLIDER));
930     gtk_widget_show(label);
931     gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
932    
933     vbox = gtk_vbox_new(FALSE, 4);
934     gtk_widget_show(vbox);
935    
936     gfloat min, max;
937     min = 1;
938     max = 1024;
939     w_ramsize_adj = gtk_adjustment_new(min, min, max, 1, 16, 0);
940     gtk_adjustment_set_value(GTK_ADJUSTMENT(w_ramsize_adj), PrefsFindInt32("ramsize") >> 20);
941    
942     scale = gtk_hscale_new(GTK_ADJUSTMENT(w_ramsize_adj));
943     gtk_widget_show(scale);
944     gtk_scale_set_digits(GTK_SCALE(scale), 0);
945     gtk_box_pack_start(GTK_BOX(vbox), scale, TRUE, TRUE, 0);
946    
947     hbox2 = gtk_hbox_new(FALSE, 4);
948     gtk_widget_show(hbox2);
949    
950     char val[32];
951     sprintf(val, GetString(STR_RAMSIZE_FMT), int(min));
952     label = gtk_label_new(val);
953     gtk_widget_show(label);
954     gtk_box_pack_start(GTK_BOX(hbox2), label, FALSE, FALSE, 0);
955    
956     sprintf(val, GetString(STR_RAMSIZE_FMT), int(max));
957     label = gtk_label_new(val);
958     gtk_widget_show(label);
959     gtk_box_pack_end(GTK_BOX(hbox2), label, FALSE, FALSE, 0);
960     gtk_box_pack_start(GTK_BOX(vbox), hbox2, TRUE, TRUE, 0);
961     gtk_box_pack_start(GTK_BOX(hbox), vbox, TRUE, TRUE, 0);
962     gtk_box_pack_start(GTK_BOX(box), hbox, FALSE, FALSE, 0);
963    
964     static const opt_desc options[] = {
965     {STR_MODELID_5_LAB, GTK_SIGNAL_FUNC(mn_modelid_5)},
966     {STR_MODELID_14_LAB, GTK_SIGNAL_FUNC(mn_modelid_14)},
967     {0, NULL}
968     };
969     int modelid = PrefsFindInt32("modelid"), active = 0;
970     switch (modelid) {
971     case 5: active = 0; break;
972     case 14: active = 1; break;
973     }
974     menu = make_option_menu(box, STR_MODELID_CTRL, options, active);
975    
976     w_rom_file = make_entry(box, STR_ROM_FILE_CTRL, "rom");
977    
978     make_checkbox(box, STR_KEYCODES_CTRL, "keycodes", GTK_SIGNAL_FUNC(tb_keycodes));
979     w_keycode_file = make_entry(box, STR_KEYCODE_FILE_CTRL, "keycodefile");
980     }
981    
982    
983     /*
984     * Read settings from widgets and set preferences
985     */
986    
987     static void read_settings(void)
988     {
989     read_volumes_settings();
990     read_scsi_settings();
991     read_graphics_settings();
992     read_serial_settings();
993     read_memory_settings();
994     }