ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/prefs_editor_gtk.cpp
Revision: 1.2
Committed: 1999-10-03T19:43:28Z (25 years, 1 month ago) by cebix
Branch: MAIN
CVS Tags: release-0_7-2
Changes since 1.1: +37 -19 lines
Log Message:
- screen prefs "dga" changed to "dga/<width>/<height>", with zero width/height
  meaning "maximum" (also for window mode)
- Caps Lock now behaves as expected

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