ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/prefs_editor_gtk.cpp
Revision: 1.5
Committed: 1999-10-19T13:22:11Z (25 years, 1 month ago) by cebix
Branch: MAIN
Changes since 1.4: +1 -0 lines
Log Message:
- volume list is reorderable

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     static GtkWidget *volume_list;
322     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     }
424    
425     // Create "Volumes" pane
426     static void create_volumes_pane(GtkWidget *top)
427     {
428     GtkWidget *box, *scroll, *menu;
429    
430     box = make_pane(top, STR_VOLUMES_PANE_TITLE);
431    
432     scroll = gtk_scrolled_window_new(NULL, NULL);
433     gtk_widget_show(scroll);
434     gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
435     volume_list = gtk_clist_new(1);
436     gtk_widget_show(volume_list);
437     gtk_clist_set_selection_mode(GTK_CLIST(volume_list), GTK_SELECTION_SINGLE);
438     gtk_clist_set_shadow_type(GTK_CLIST(volume_list), GTK_SHADOW_NONE);
439 cebix 1.5 gtk_clist_set_reorderable(GTK_CLIST(volume_list), true);
440 cebix 1.1 gtk_signal_connect(GTK_OBJECT(volume_list), "select_row", GTK_SIGNAL_FUNC(cl_selected), NULL);
441     char *str;
442     int32 index = 0;
443     while ((str = (char *)PrefsFindString("disk", index++)) != NULL)
444     gtk_clist_append(GTK_CLIST(volume_list), &str);
445     gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scroll), volume_list);
446     gtk_box_pack_start(GTK_BOX(box), scroll, TRUE, TRUE, 0);
447     selected_volume = 0;
448    
449     static const opt_desc buttons[] = {
450     {STR_ADD_VOLUME_BUTTON, GTK_SIGNAL_FUNC(cb_add_volume)},
451     {STR_CREATE_VOLUME_BUTTON, GTK_SIGNAL_FUNC(cb_create_volume)},
452     {STR_REMOVE_VOLUME_BUTTON, GTK_SIGNAL_FUNC(cb_remove_volume)},
453     {0, NULL},
454     };
455     make_button_box(box, 0, buttons);
456     make_separator(box);
457    
458     static const opt_desc options[] = {
459     {STR_BOOT_ANY_LAB, GTK_SIGNAL_FUNC(mn_boot_any)},
460     {STR_BOOT_CDROM_LAB, GTK_SIGNAL_FUNC(mn_boot_cdrom)},
461     {0, NULL}
462     };
463     int bootdriver = PrefsFindInt16("bootdriver"), active = 0;
464     switch (bootdriver) {
465     case 0: active = 0; break;
466     case CDROMRefNum: active = 1; break;
467     }
468     menu = make_option_menu(box, STR_BOOTDRIVER_CTRL, options, active);
469    
470     make_checkbox(box, STR_NOCDROM_CTRL, "nocdrom", GTK_SIGNAL_FUNC(tb_nocdrom));
471     }
472    
473    
474     /*
475     * "SCSI" pane
476     */
477    
478     static GtkWidget *w_scsi[7];
479    
480     // Read settings from widgets and set preferences
481     static void read_scsi_settings(void)
482     {
483     for (int id=0; id<7; id++) {
484     char prefs_name[32];
485     sprintf(prefs_name, "scsi%d", id);
486     const char *str = gtk_entry_get_text(GTK_ENTRY(w_scsi[id]));
487     if (str && strlen(str))
488     PrefsReplaceString(prefs_name, str);
489     else
490     PrefsRemoveItem(prefs_name);
491     }
492     }
493    
494     // Create "SCSI" pane
495     static void create_scsi_pane(GtkWidget *top)
496     {
497     GtkWidget *box;
498    
499     box = make_pane(top, STR_SCSI_PANE_TITLE);
500    
501     for (int id=0; id<7; id++) {
502     char prefs_name[32];
503     sprintf(prefs_name, "scsi%d", id);
504     w_scsi[id] = make_entry(box, STR_SCSI_ID_0 + id, prefs_name);
505     }
506     }
507    
508    
509     /*
510     * "Graphics/Sound" pane
511     */
512    
513     // Display types
514     enum {
515     DISPLAY_WINDOW,
516     DISPLAY_SCREEN
517     };
518    
519     static GtkWidget *w_frameskip, *w_display_x, *w_display_y;
520     static GtkWidget *l_frameskip, *l_display_x, *l_display_y;
521     static int display_type;
522     static int dis_width, dis_height;
523    
524     // Hide/show graphics widgets
525     static void hide_show_graphics_widgets(void)
526     {
527     switch (display_type) {
528     case DISPLAY_WINDOW:
529     gtk_widget_show(w_frameskip); gtk_widget_show(l_frameskip);
530     break;
531     case DISPLAY_SCREEN:
532     gtk_widget_hide(w_frameskip); gtk_widget_hide(l_frameskip);
533     break;
534     }
535     }
536    
537     // "Window" video type selected
538     static void mn_window(...)
539     {
540     display_type = DISPLAY_WINDOW;
541     hide_show_graphics_widgets();
542     }
543    
544     // "Fullscreen" video type selected
545     static void mn_fullscreen(...)
546     {
547     display_type = DISPLAY_SCREEN;
548     hide_show_graphics_widgets();
549     }
550    
551     // "5 Hz".."60Hz" selected
552     static void mn_5hz(...) {PrefsReplaceInt32("frameskip", 12);}
553     static void mn_7hz(...) {PrefsReplaceInt32("frameskip", 8);}
554     static void mn_10hz(...) {PrefsReplaceInt32("frameskip", 6);}
555     static void mn_15hz(...) {PrefsReplaceInt32("frameskip", 4);}
556     static void mn_30hz(...) {PrefsReplaceInt32("frameskip", 2);}
557     static void mn_60hz(...) {PrefsReplaceInt32("frameskip", 1);}
558    
559     // "Disable Sound Output" button toggled
560     static void tb_nosound(GtkWidget *widget)
561     {
562     PrefsReplaceBool("nosound", GTK_TOGGLE_BUTTON(widget)->active);
563     }
564    
565     // Read graphics preferences
566     static void parse_graphics_prefs(void)
567     {
568     display_type = DISPLAY_WINDOW;
569     dis_width = 512;
570     dis_height = 384;
571    
572     const char *str = PrefsFindString("screen");
573     if (str) {
574     if (sscanf(str, "win/%d/%d", &dis_width, &dis_height) == 2)
575     display_type = DISPLAY_WINDOW;
576 cebix 1.2 else if (sscanf(str, "dga/%d/%d", &dis_width, &dis_height) == 2)
577 cebix 1.1 display_type = DISPLAY_SCREEN;
578     }
579     }
580    
581     // Read settings from widgets and set preferences
582     static void read_graphics_settings(void)
583     {
584     const char *str;
585    
586     str = gtk_entry_get_text(GTK_ENTRY(w_display_x));
587     dis_width = atoi(str);
588    
589     str = gtk_entry_get_text(GTK_ENTRY(w_display_y));
590     dis_height = atoi(str);
591    
592     char pref[256];
593     switch (display_type) {
594     case DISPLAY_WINDOW:
595     sprintf(pref, "win/%d/%d", dis_width, dis_height);
596     break;
597     case DISPLAY_SCREEN:
598 cebix 1.2 sprintf(pref, "dga/%d/%d", dis_width, dis_height);
599 cebix 1.1 break;
600     default:
601     PrefsRemoveItem("screen");
602     return;
603     }
604     PrefsReplaceString("screen", pref);
605     }
606    
607     // Create "Graphics/Sound" pane
608     static void create_graphics_pane(GtkWidget *top)
609     {
610 cebix 1.2 GtkWidget *box, *table, *label, *opt, *menu, *combo;
611 cebix 1.1 char str[32];
612    
613     parse_graphics_prefs();
614    
615     box = make_pane(top, STR_GRAPHICS_SOUND_PANE_TITLE);
616     table = make_table(box, 2, 4);
617    
618     label = gtk_label_new(GetString(STR_VIDEO_TYPE_CTRL));
619     gtk_widget_show(label);
620     gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, 1, (GtkAttachOptions)0, (GtkAttachOptions)0, 4, 4);
621    
622     opt = gtk_option_menu_new();
623     gtk_widget_show(opt);
624     menu = gtk_menu_new();
625     add_menu_item(menu, STR_WINDOW_LAB, GTK_SIGNAL_FUNC(mn_window));
626     add_menu_item(menu, STR_FULLSCREEN_LAB, GTK_SIGNAL_FUNC(mn_fullscreen));
627     switch (display_type) {
628     case DISPLAY_WINDOW:
629     gtk_menu_set_active(GTK_MENU(menu), 0);
630     break;
631     case DISPLAY_SCREEN:
632     gtk_menu_set_active(GTK_MENU(menu), 1);
633     break;
634     }
635     gtk_option_menu_set_menu(GTK_OPTION_MENU(opt), menu);
636     gtk_table_attach(GTK_TABLE(table), opt, 1, 2, 0, 1, (GtkAttachOptions)GTK_FILL, (GtkAttachOptions)0, 4, 4);
637    
638     l_frameskip = gtk_label_new(GetString(STR_FRAMESKIP_CTRL));
639     gtk_widget_show(l_frameskip);
640     gtk_table_attach(GTK_TABLE(table), l_frameskip, 0, 1, 1, 2, (GtkAttachOptions)0, (GtkAttachOptions)0, 4, 4);
641    
642     w_frameskip = gtk_option_menu_new();
643     gtk_widget_show(w_frameskip);
644     menu = gtk_menu_new();
645     add_menu_item(menu, STR_REF_5HZ_LAB, GTK_SIGNAL_FUNC(mn_5hz));
646     add_menu_item(menu, STR_REF_7_5HZ_LAB, GTK_SIGNAL_FUNC(mn_7hz));
647     add_menu_item(menu, STR_REF_10HZ_LAB, GTK_SIGNAL_FUNC(mn_10hz));
648     add_menu_item(menu, STR_REF_15HZ_LAB, GTK_SIGNAL_FUNC(mn_15hz));
649     add_menu_item(menu, STR_REF_30HZ_LAB, GTK_SIGNAL_FUNC(mn_30hz));
650     add_menu_item(menu, STR_REF_60HZ_LAB, GTK_SIGNAL_FUNC(mn_60hz));
651     int frameskip = PrefsFindInt32("frameskip");
652     switch (frameskip) {
653     case 12:
654     gtk_menu_set_active(GTK_MENU(menu), 0);
655     break;
656     case 8:
657     gtk_menu_set_active(GTK_MENU(menu), 1);
658     break;
659     case 6:
660     gtk_menu_set_active(GTK_MENU(menu), 2);
661     break;
662     case 4:
663     gtk_menu_set_active(GTK_MENU(menu), 3);
664     break;
665     case 2:
666     gtk_menu_set_active(GTK_MENU(menu), 4);
667     break;
668     case 1:
669     gtk_menu_set_active(GTK_MENU(menu), 5);
670     break;
671     }
672     gtk_option_menu_set_menu(GTK_OPTION_MENU(w_frameskip), menu);
673     gtk_table_attach(GTK_TABLE(table), w_frameskip, 1, 2, 1, 2, (GtkAttachOptions)GTK_FILL, (GtkAttachOptions)0, 4, 4);
674    
675     l_display_x = gtk_label_new(GetString(STR_DISPLAY_X_CTRL));
676     gtk_widget_show(l_display_x);
677     gtk_table_attach(GTK_TABLE(table), l_display_x, 0, 1, 2, 3, (GtkAttachOptions)0, (GtkAttachOptions)0, 4, 4);
678    
679 cebix 1.2 combo = gtk_combo_new();
680     gtk_widget_show(combo);
681     GList *glist1 = NULL;
682 cebix 1.4 glist1 = g_list_append(glist1, (void *)GetString(STR_SIZE_512_LAB));
683     glist1 = g_list_append(glist1, (void *)GetString(STR_SIZE_640_LAB));
684     glist1 = g_list_append(glist1, (void *)GetString(STR_SIZE_800_LAB));
685     glist1 = g_list_append(glist1, (void *)GetString(STR_SIZE_1024_LAB));
686     glist1 = g_list_append(glist1, (void *)GetString(STR_SIZE_MAX_LAB));
687 cebix 1.2 gtk_combo_set_popdown_strings(GTK_COMBO(combo), glist1);
688     if (dis_width)
689     sprintf(str, "%d", dis_width);
690     else
691 cebix 1.3 strcpy(str, GetString(STR_SIZE_MAX_LAB));
692 cebix 1.2 gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(combo)->entry), str);
693     gtk_table_attach(GTK_TABLE(table), combo, 1, 2, 2, 3, (GtkAttachOptions)GTK_FILL, (GtkAttachOptions)0, 4, 4);
694     w_display_x = GTK_COMBO(combo)->entry;
695 cebix 1.1
696     l_display_y = gtk_label_new(GetString(STR_DISPLAY_Y_CTRL));
697     gtk_widget_show(l_display_y);
698     gtk_table_attach(GTK_TABLE(table), l_display_y, 0, 1, 3, 4, (GtkAttachOptions)0, (GtkAttachOptions)0, 4, 4);
699    
700 cebix 1.2 combo = gtk_combo_new();
701     gtk_widget_show(combo);
702     GList *glist2 = NULL;
703 cebix 1.4 glist2 = g_list_append(glist2, (void *)GetString(STR_SIZE_384_LAB));
704     glist2 = g_list_append(glist2, (void *)GetString(STR_SIZE_480_LAB));
705     glist2 = g_list_append(glist2, (void *)GetString(STR_SIZE_600_LAB));
706     glist2 = g_list_append(glist2, (void *)GetString(STR_SIZE_768_LAB));
707     glist2 = g_list_append(glist2, (void *)GetString(STR_SIZE_MAX_LAB));
708 cebix 1.2 gtk_combo_set_popdown_strings(GTK_COMBO(combo), glist2);
709     if (dis_height)
710     sprintf(str, "%d", dis_height);
711     else
712 cebix 1.3 strcpy(str, GetString(STR_SIZE_MAX_LAB));
713 cebix 1.2 gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(combo)->entry), str);
714     gtk_table_attach(GTK_TABLE(table), combo, 1, 2, 3, 4, (GtkAttachOptions)GTK_FILL, (GtkAttachOptions)0, 4, 4);
715     w_display_y = GTK_COMBO(combo)->entry;
716 cebix 1.1
717     make_checkbox(box, STR_NOSOUND_CTRL, "nosound", GTK_SIGNAL_FUNC(tb_nosound));
718    
719     hide_show_graphics_widgets();
720     }
721    
722    
723     /*
724     * "Serial/Network" pane
725     */
726    
727     static GtkWidget *w_seriala, *w_serialb, *w_ether;
728    
729     // Read settings from widgets and set preferences
730     static void read_serial_settings(void)
731     {
732     const char *str;
733    
734     str = gtk_entry_get_text(GTK_ENTRY(w_seriala));
735     PrefsReplaceString("seriala", str);
736    
737     str = gtk_entry_get_text(GTK_ENTRY(w_serialb));
738     PrefsReplaceString("serialb", str);
739    
740     str = gtk_entry_get_text(GTK_ENTRY(w_ether));
741     if (str && strlen(str))
742     PrefsReplaceString("ether", str);
743     else
744     PrefsRemoveItem("ether");
745     }
746    
747     // Add names of serial devices
748     static gint gl_str_cmp(gconstpointer a, gconstpointer b)
749     {
750     return strcmp((char *)a, (char *)b);
751     }
752    
753     static GList *add_serial_names(void)
754     {
755     GList *glist = NULL;
756    
757     // Search /dev for ttyS* and lp*
758     DIR *d = opendir("/dev");
759     if (d) {
760     struct dirent *de;
761     while ((de = readdir(d)) != NULL) {
762     #if defined(__linux__)
763     if (strncmp(de->d_name, "ttyS", 4) == 0 || strncmp(de->d_name, "lp", 2) == 0) {
764     #elif defined(__FreeBSD__)
765     if (strncmp(de->d_name, "cuaa", 4) == 0 || strncmp(de->d_name, "lpt", 3) == 0) {
766 cebix 1.4 #elif defined(__NetBSD__)
767     if (strncmp(de->d_name, "tty0", 4) == 0 || strncmp(de->d_name, "lpt", 3) == 0) {
768 cebix 1.1 #elif defined(sgi)
769     if (strncmp(de->d_name, "ttyf", 4) == 0 || strncmp(de->d_name, "plp", 3) == 0) {
770     #else
771     if (false) {
772     #endif
773     char *str = new char[64];
774     sprintf(str, "/dev/%s", de->d_name);
775     glist = g_list_append(glist, str);
776     }
777     }
778     closedir(d);
779     }
780     if (glist)
781     g_list_sort(glist, gl_str_cmp);
782     else
783 cebix 1.4 glist = g_list_append(glist, (void *)GetString(STR_NONE_LAB));
784 cebix 1.1 return glist;
785     }
786    
787     // Add names of ethernet interfaces
788     static GList *add_ether_names(void)
789     {
790     GList *glist = NULL;
791    
792     // Get list of all Ethernet interfaces
793     int s = socket(PF_INET, SOCK_DGRAM, 0);
794     if (s >= 0) {
795     char inbuf[8192];
796     struct ifconf ifc;
797     ifc.ifc_len = sizeof(inbuf);
798     ifc.ifc_buf = inbuf;
799     if (ioctl(s, SIOCGIFCONF, &ifc) == 0) {
800     struct ifreq req, *ifr = ifc.ifc_req;
801     for (int i=0; i<ifc.ifc_len; i+=sizeof(ifreq), ifr++) {
802     req = *ifr;
803 cebix 1.4 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(sgi)
804 cebix 1.1 if (ioctl(s, SIOCGIFADDR, &req) == 0 && (req.ifr_addr.sa_family == ARPHRD_ETHER || req.ifr_addr.sa_family == ARPHRD_ETHER+1)) {
805 cebix 1.4 #elif defined(__linux__)
806     if (ioctl(s, SIOCGIFHWADDR, &req) == 0 && req.ifr_hwaddr.sa_family == ARPHRD_ETHER) {
807 cebix 1.1 #else
808 cebix 1.4 if (false) {
809 cebix 1.1 #endif
810     char *str = new char[64];
811     strncpy(str, ifr->ifr_name, 63);
812     glist = g_list_append(glist, str);
813     }
814     }
815     }
816     close(s);
817     }
818     if (glist)
819     g_list_sort(glist, gl_str_cmp);
820     else
821 cebix 1.4 glist = g_list_append(glist, (void *)GetString(STR_NONE_LAB));
822 cebix 1.1 return glist;
823     }
824    
825     // Create "Serial/Network" pane
826     static void create_serial_pane(GtkWidget *top)
827     {
828     GtkWidget *box, *table, *label, *combo;
829     GList *glist = add_serial_names();
830    
831     box = make_pane(top, STR_SERIAL_NETWORK_PANE_TITLE);
832     table = make_table(box, 2, 3);
833    
834     label = gtk_label_new(GetString(STR_SERIALA_CTRL));
835     gtk_widget_show(label);
836     gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, 1, (GtkAttachOptions)0, (GtkAttachOptions)0, 4, 4);
837    
838     combo = gtk_combo_new();
839     gtk_widget_show(combo);
840     gtk_combo_set_popdown_strings(GTK_COMBO(combo), glist);
841     const char *str = PrefsFindString("seriala");
842     if (str == NULL)
843     str = "";
844     gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(combo)->entry), str);
845     gtk_table_attach(GTK_TABLE(table), combo, 1, 2, 0, 1, (GtkAttachOptions)(GTK_FILL | GTK_EXPAND), (GtkAttachOptions)0, 4, 4);
846     w_seriala = GTK_COMBO(combo)->entry;
847    
848     label = gtk_label_new(GetString(STR_SERIALB_CTRL));
849     gtk_widget_show(label);
850     gtk_table_attach(GTK_TABLE(table), label, 0, 1, 1, 2, (GtkAttachOptions)0, (GtkAttachOptions)0, 4, 4);
851    
852     combo = gtk_combo_new();
853     gtk_widget_show(combo);
854     gtk_combo_set_popdown_strings(GTK_COMBO(combo), glist);
855     str = PrefsFindString("serialb");
856     if (str == NULL)
857     str = "";
858     gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(combo)->entry), str);
859     gtk_table_attach(GTK_TABLE(table), combo, 1, 2, 1, 2, (GtkAttachOptions)(GTK_FILL | GTK_EXPAND), (GtkAttachOptions)0, 4, 4);
860     w_serialb = GTK_COMBO(combo)->entry;
861    
862     label = gtk_label_new(GetString(STR_ETHERNET_IF_CTRL));
863     gtk_widget_show(label);
864     gtk_table_attach(GTK_TABLE(table), label, 0, 1, 2, 3, (GtkAttachOptions)0, (GtkAttachOptions)0, 4, 4);
865    
866     glist = add_ether_names();
867     combo = gtk_combo_new();
868     gtk_widget_show(combo);
869     gtk_combo_set_popdown_strings(GTK_COMBO(combo), glist);
870     str = PrefsFindString("ether");
871     if (str == NULL)
872     str = "";
873     gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(combo)->entry), str);
874     gtk_table_attach(GTK_TABLE(table), combo, 1, 2, 2, 3, (GtkAttachOptions)(GTK_FILL | GTK_EXPAND), (GtkAttachOptions)0, 4, 4);
875     w_ether = GTK_COMBO(combo)->entry;
876     }
877    
878    
879     /*
880     * "Memory/Misc" pane
881     */
882    
883     static GtkObject *w_ramsize_adj;
884     static GtkWidget *w_rom_file;
885     static GtkWidget *w_keycode_file;
886    
887     // Model ID selected
888     static void mn_modelid_5(...) {PrefsReplaceInt32("modelid", 5);}
889     static void mn_modelid_14(...) {PrefsReplaceInt32("modelid", 14);}
890    
891     // "Use Raw Keycodes" button toggled
892     static void tb_keycodes(GtkWidget *widget)
893     {
894     PrefsReplaceBool("keycodes", GTK_TOGGLE_BUTTON(widget)->active);
895     }
896    
897     // Read settings from widgets and set preferences
898     static void read_memory_settings(void)
899     {
900     PrefsReplaceInt32("ramsize", int(GTK_ADJUSTMENT(w_ramsize_adj)->value) << 20);
901    
902     const char *str = gtk_entry_get_text(GTK_ENTRY(w_rom_file));
903     if (str && strlen(str))
904     PrefsReplaceString("rom", str);
905     else
906     PrefsRemoveItem("rom");
907    
908     str = gtk_entry_get_text(GTK_ENTRY(w_keycode_file));
909     if (str && strlen(str))
910     PrefsReplaceString("keycodefile", str);
911     else
912     PrefsRemoveItem("keycodefile");
913     }
914    
915     // Create "Memory/Misc" pane
916     static void create_memory_pane(GtkWidget *top)
917     {
918 cebix 1.4 GtkWidget *box, *vbox, *hbox, *hbox2, *label, *scale, *menu;
919 cebix 1.1
920     box = make_pane(top, STR_MEMORY_MISC_PANE_TITLE);
921    
922     hbox = gtk_hbox_new(FALSE, 4);
923     gtk_widget_show(hbox);
924    
925     label = gtk_label_new(GetString(STR_RAMSIZE_SLIDER));
926     gtk_widget_show(label);
927     gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
928    
929     vbox = gtk_vbox_new(FALSE, 4);
930     gtk_widget_show(vbox);
931    
932     gfloat min, max;
933     min = 1;
934     max = 1024;
935     w_ramsize_adj = gtk_adjustment_new(min, min, max, 1, 16, 0);
936     gtk_adjustment_set_value(GTK_ADJUSTMENT(w_ramsize_adj), PrefsFindInt32("ramsize") >> 20);
937    
938     scale = gtk_hscale_new(GTK_ADJUSTMENT(w_ramsize_adj));
939     gtk_widget_show(scale);
940     gtk_scale_set_digits(GTK_SCALE(scale), 0);
941     gtk_box_pack_start(GTK_BOX(vbox), scale, TRUE, TRUE, 0);
942    
943     hbox2 = gtk_hbox_new(FALSE, 4);
944     gtk_widget_show(hbox2);
945    
946     char val[32];
947     sprintf(val, GetString(STR_RAMSIZE_FMT), int(min));
948     label = gtk_label_new(val);
949     gtk_widget_show(label);
950     gtk_box_pack_start(GTK_BOX(hbox2), label, FALSE, FALSE, 0);
951    
952     sprintf(val, GetString(STR_RAMSIZE_FMT), int(max));
953     label = gtk_label_new(val);
954     gtk_widget_show(label);
955     gtk_box_pack_end(GTK_BOX(hbox2), label, FALSE, FALSE, 0);
956     gtk_box_pack_start(GTK_BOX(vbox), hbox2, TRUE, TRUE, 0);
957     gtk_box_pack_start(GTK_BOX(hbox), vbox, TRUE, TRUE, 0);
958     gtk_box_pack_start(GTK_BOX(box), hbox, FALSE, FALSE, 0);
959    
960     static const opt_desc options[] = {
961     {STR_MODELID_5_LAB, GTK_SIGNAL_FUNC(mn_modelid_5)},
962     {STR_MODELID_14_LAB, GTK_SIGNAL_FUNC(mn_modelid_14)},
963     {0, NULL}
964     };
965     int modelid = PrefsFindInt32("modelid"), active = 0;
966     switch (modelid) {
967     case 5: active = 0; break;
968     case 14: active = 1; break;
969     }
970     menu = make_option_menu(box, STR_MODELID_CTRL, options, active);
971    
972     w_rom_file = make_entry(box, STR_ROM_FILE_CTRL, "rom");
973    
974     make_checkbox(box, STR_KEYCODES_CTRL, "keycodes", GTK_SIGNAL_FUNC(tb_keycodes));
975     w_keycode_file = make_entry(box, STR_KEYCODE_FILE_CTRL, "keycodefile");
976     }
977    
978    
979     /*
980     * Read settings from widgets and set preferences
981     */
982    
983     static void read_settings(void)
984     {
985     read_volumes_settings();
986     read_scsi_settings();
987     read_graphics_settings();
988     read_serial_settings();
989     read_memory_settings();
990     }