File: linux-config.cpp

package info (click to toggle)
amoeba 1.1-31
  • links: PTS
  • area: contrib
  • in suites: bookworm, forky, sid, trixie
  • size: 1,492 kB
  • sloc: cpp: 8,384; makefile: 140
file content (439 lines) | stat: -rw-r--r-- 14,958 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*
 * A Linux GTK+ configuration dialog.
 *
 * FIXME: Should we support command line options even with GTK+?
 */

#ifndef G_DISABLE_CAST_CHECKS
#define G_DISABLE_CAST_CHECKS
#endif

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <dlfcn.h>
#include <errno.h>
#include <locale.h>

#include <GL/glx.h>
#include <X11/extensions/xf86vmode.h>
#include <X11/keysym.h>

#include <vector>
#include <string>
#include <utility>

#include "audio/linux_alsa.h"
#include "image/image.h"
#include "linux-config.h"
#include "exception.h"

int modeline_compare(const void *a, const void *b)
{
	XF86VidModeModeInfo *am = *((XF86VidModeModeInfo **)a);
	XF86VidModeModeInfo *bm = *((XF86VidModeModeInfo **)b);

	if (am->hdisplay < bm->hdisplay) return -1;
	if (am->hdisplay == bm->hdisplay) {
		if (am->vdisplay < bm->vdisplay) return -1;
		if (am->vdisplay == bm->vdisplay) return 0;
		return 1;
	}
	return 1;
}

LinuxConfig::LinuxConfig()
{
}

/* this is largely GLADE-generated code, but modified for the dl */
void LinuxConfig::show(int *argc, char ***argv, Hashtable *attr_hash)
{
	/*
	 * open an X connection to enumerate resolutions and visuals
	 * (code duplication from glwindow.cpp, not good)
	 */
	XF86VidModeModeInfo **modes;
	Display *dpy = XOpenDisplay(NULL);
	if (dpy == NULL)
		throw new FatalException("Can't connect to X server!");
	int screen = DefaultScreen(dpy);
	
	gtk_init(argc, argv);

	GtkWidget *config;
	GtkWidget *alignment1;
	GtkWidget *vbox2;
	GtkWidget *hbox2;
	GtkWidget *pixmap1;
	GtkWidget *table2;
	GtkWidget *mode_label;
	GtkWidget *vis_label;
	GtkWidget *res_menu;
	GtkWidget *fullscreenmenu;
	GtkWidget *visualmenu;
	GtkWidget *sound_label;
	GtkWidget *soundmenu;
	GtkWidget *res_label;
	GtkWidget *spacefiller1;
	GtkWidget *spacefiller2;
	GtkWidget *hbox4;
	GtkWidget *ok;
	GdkPixbuf *gdkpixbuf;
	Image *logo;
	int w, h, bpp;
	unsigned char *ptr;
	
	config = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	g_object_set_data(G_OBJECT (config), "config", config);
	gtk_window_set_title(GTK_WINDOW (config), "Excess demo configuration (GLX/X11)");
	gtk_window_set_position(GTK_WINDOW (config), GTK_WIN_POS_CENTER);
	gtk_window_set_modal(GTK_WINDOW (config), TRUE);
	gtk_window_set_resizable(GTK_WINDOW (config), FALSE);

	alignment1 = gtk_alignment_new(0.5, 0.5, 1, 1);
	g_object_ref(alignment1);
	g_object_set_data_full(G_OBJECT (config), "alignment1", alignment1,
	                       (GDestroyNotify) (g_object_unref));
	gtk_widget_show(alignment1);
	gtk_container_add(GTK_CONTAINER (config), alignment1);

	vbox2 = gtk_vbox_new(FALSE, 0);
	g_object_ref(vbox2);
	g_object_set_data_full(G_OBJECT (config), "vbox2", vbox2,
	                       (GDestroyNotify) (g_object_unref));
	gtk_widget_show(vbox2);
	gtk_container_add(GTK_CONTAINER (alignment1), vbox2);

	hbox2 = gtk_hbox_new(FALSE, 10);
	g_object_ref(hbox2);
	g_object_set_data_full(G_OBJECT (config), "hbox2", hbox2,
	                       (GDestroyNotify) (g_object_unref));
	gtk_widget_show(hbox2);
	gtk_box_pack_start(GTK_BOX (vbox2), hbox2, TRUE, TRUE, 0);
	gtk_container_set_border_width(GTK_CONTAINER (hbox2), 5);

	/* Get the logo from a PNG */
	logo = load_image("launcherlogo.png");
	w = logo->get_width();
	h = logo->get_height();
	bpp = logo->get_bpp();
	ptr = logo->get_pixel_data();
	if (bpp != 32)
		throw new NonFatalException("Launcher logo must be 32bpp");

	gdkpixbuf = gdk_pixbuf_new_from_data(ptr, GDK_COLORSPACE_RGB, TRUE, 8, w, h, w * 4, NULL, NULL);
	pixmap1 = gtk_image_new_from_pixbuf(gdkpixbuf);

	// Just leak the logo, instead of bothering with a callback.

	g_object_ref(pixmap1);
	g_object_set_data_full(G_OBJECT (config), "pixmap1", pixmap1,
	                       (GDestroyNotify) (g_object_unref));
	gtk_widget_show(pixmap1);
	gtk_box_pack_start(GTK_BOX (hbox2), pixmap1, FALSE, TRUE, 0);

	table2 = gtk_table_new(6, 2, FALSE);
	g_object_ref(table2);
	g_object_set_data_full(G_OBJECT (config), "table2", table2,
	                       (GDestroyNotify) (g_object_unref));
	gtk_widget_show(table2);
	gtk_box_pack_start(GTK_BOX (hbox2), table2, TRUE, FALSE, 0);
	gtk_table_set_col_spacings(GTK_TABLE (table2), 7);

	mode_label = gtk_label_new("Window mode:");
	g_object_ref(mode_label);
	g_object_set_data_full(G_OBJECT (config), "mode_label", mode_label,
	                       (GDestroyNotify) (g_object_unref));
	gtk_widget_show(mode_label);
	gtk_table_attach(GTK_TABLE (table2), mode_label, 0, 1, 2, 3,
	                  (GtkAttachOptions) (GTK_FILL),
	                  (GtkAttachOptions) (0), 0, 0);
	gtk_label_set_justify(GTK_LABEL (mode_label), GTK_JUSTIFY_LEFT);
	gtk_misc_set_alignment(GTK_MISC (mode_label), 0, 0.5);

	vis_label = gtk_label_new("OpenGL visual:");
	g_object_ref(vis_label);
	g_object_set_data_full(G_OBJECT (config), "vis_label", vis_label,
	                       (GDestroyNotify) (g_object_unref));
	gtk_widget_show(vis_label);
	gtk_table_attach(GTK_TABLE (table2), vis_label, 0, 1, 3, 4,
	                  (GtkAttachOptions) (GTK_FILL),
	                  (GtkAttachOptions) (0), 0, 0);
	gtk_label_set_justify(GTK_LABEL (vis_label), GTK_JUSTIFY_LEFT);
	gtk_misc_set_alignment(GTK_MISC (vis_label), 0, 0.5);

	res_menu = gtk_combo_box_text_new();
	g_object_ref(res_menu);
	g_object_set_data_full(G_OBJECT (config), "res_menu", res_menu,
	                       (GDestroyNotify) (g_object_unref));
	gtk_widget_show(res_menu);
	gtk_table_attach(GTK_TABLE (table2), res_menu, 1, 2, 1, 2,
	                  (GtkAttachOptions) (GTK_FILL),
	                  (GtkAttachOptions) (0), 0, 0);

	/*
	 * enumerate through all the resolutions, then sort
	 */
	std::vector<std::pair<std::string, std::string> > resolutions;
	{
		int modeNum;
		int selected = 0;
		
		XF86VidModeGetAllModeLines(dpy, screen, &modeNum, &modes);
		qsort(modes, modeNum, sizeof(XF86VidModeModeInfo *), modeline_compare);
		
		for (int i = 0; i < modeNum; i++) {
			char buf[32];
			sprintf(buf, "%ux%u", modes[i]->hdisplay, modes[i]->vdisplay);
			gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT (res_menu), buf);

			char xres[32], yres[32];
			sprintf(xres, "%u", modes[i]->hdisplay);
			sprintf(yres, "%u", modes[i]->vdisplay);
			resolutions.push_back(std::make_pair(std::string(xres), std::string(yres)));
			
			if (modes[i]->hdisplay == 640 &&
			    modes[i]->vdisplay == 480) {
				selected = i;
			}
		}
		gtk_combo_box_set_active(GTK_COMBO_BOX (res_menu), selected);

		XFree(modes);
	}
	
	fullscreenmenu = gtk_combo_box_text_new();
	g_object_ref(fullscreenmenu);
	g_object_set_data_full(G_OBJECT (config), "fullscreenmenu", fullscreenmenu,
	                       (GDestroyNotify) (g_object_unref));
	gtk_widget_show(fullscreenmenu);
	gtk_table_attach(GTK_TABLE (table2), fullscreenmenu, 1, 2, 2, 3,
	                  (GtkAttachOptions) (GTK_FILL),
	                  (GtkAttachOptions) (0), 0, 0);

	gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT (fullscreenmenu), "Fullscreen");
	gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT (fullscreenmenu), "Windowed");

	gtk_combo_box_set_active(GTK_COMBO_BOX (fullscreenmenu), 0);

	/*
	 * now find all the visuals available, sort out the useless ones, and
	 * show them in the dialog
	 */
	visualmenu = gtk_combo_box_text_new();
	g_object_ref(visualmenu);
	g_object_set_data_full(G_OBJECT (config), "visualmenu", visualmenu,
	                       (GDestroyNotify) (g_object_unref));
	gtk_widget_show(visualmenu);
	gtk_table_attach(GTK_TABLE (table2), visualmenu, 1, 2, 3, 4,
	                  (GtkAttachOptions) (GTK_FILL),
	                  (GtkAttachOptions) (0), 0, 0);

	std::vector<std::string> visual_ids;
	{
		XVisualInfo tmplate;
		int nret, usable = 0;
		int bestid = 0, bestbpp = 0, bestdepth = 0;
		
		tmplate.screen = screen;
		XVisualInfo *xvi = XGetVisualInfo(dpy, VisualScreenMask, &tmplate, &nret);
		if (xvi == NULL)
			throw new FatalException("No usable visuals!");

		for (int i = 0; i < nret; i++) {
			int has_gl, has_db, stencil_bpp, bpp, depth;
			char buf[256];

			/* first check for doublebuffered GL */
			if (glXGetConfig(dpy, &(xvi[i]), GLX_USE_GL, &has_gl) != 0 ||
			    !has_gl)
				continue;
			if (glXGetConfig(dpy, &(xvi[i]), GLX_DOUBLEBUFFER, &has_db) != 0 ||
			    !has_db)
				continue;

			/* we also need at least 4bpp stencil */
			if (glXGetConfig(dpy, &(xvi[i]), GLX_STENCIL_SIZE, &stencil_bpp) != 0 ||
			    stencil_bpp < 4)
				continue;
			
			if (glXGetConfig(dpy, &(xvi[i]), GLX_BUFFER_SIZE, &bpp) != 0)
				continue;
			if (glXGetConfig(dpy, &(xvi[i]), GLX_DEPTH_SIZE, &depth) != 0)
				continue;
		
			sprintf(buf, "%dbpp, %d-bits Z-buffer", bpp, depth);

			/*
			 * verify that there aren't already identical (for our purposes --
			 * the first usable will be selected anyhow by glXChooseVisual
			 * so we don't have to do any better guesses, I think ;-) )
			 * modes in the list... a bit ineffective, but doesn't matter either.
			 * if we really want to optimize, we could make our own array
			 * or something :-)
			 */
			bool dup = false;
			for (int j = 0; j < i; j++) {
				int has_gl2, has_db2, stencil_bpp2, bpp2, depth2;
				if (glXGetConfig(dpy, &(xvi[j]), GLX_USE_GL, &has_gl2) != 0 ||
				    !has_gl2)
					continue;
				if (glXGetConfig(dpy, &(xvi[j]), GLX_DOUBLEBUFFER, &has_db2) != 0 ||
				    !has_db2)
					continue;
				if (glXGetConfig(dpy, &(xvi[j]), GLX_STENCIL_SIZE, &stencil_bpp2) != 0 ||
				    stencil_bpp2 < 4)
					continue;
				if (glXGetConfig(dpy, &(xvi[j]), GLX_BUFFER_SIZE, &bpp2) != 0 ||
				    bpp2 != bpp)
					continue;
				if (glXGetConfig(dpy, &(xvi[j]), GLX_DEPTH_SIZE, &depth2) != 0 ||
				    depth2 != depth)
					continue;

				dup = true;
				break;
			}
			
			if (dup)
				continue;
			
			gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT (visualmenu), buf);
			sprintf(buf, "%lu", xvi[i].visualid);
			visual_ids.push_back(buf);
			
			if (bpp > bestbpp || bpp == bestbpp && depth >= bestdepth) {
				bestid = usable;
				bestbpp = bpp;
				bestdepth = depth;
			}
			
			usable++;
		}
		if (usable == 0)
			throw new FatalException("No usable visuals! (Try 32bpp mode if you are not already)");
		gtk_combo_box_set_active(GTK_COMBO_BOX (visualmenu), bestid);
	}
	XCloseDisplay(dpy);

	sound_label = gtk_label_new("Sound device:");
	g_object_ref(sound_label);
	g_object_set_data_full(G_OBJECT (config), "sound_label", sound_label,
	                       (GDestroyNotify) (g_object_unref));
	gtk_widget_show(sound_label);
	gtk_table_attach(GTK_TABLE (table2), sound_label, 0, 1, 4, 5,
	                  (GtkAttachOptions) (GTK_FILL),
	                  (GtkAttachOptions) (0), 0, 0);
	gtk_label_set_justify(GTK_LABEL (sound_label), GTK_JUSTIFY_LEFT);
	gtk_misc_set_alignment(GTK_MISC (sound_label), 0, 0.5);

	soundmenu = gtk_combo_box_text_new();
	g_object_ref(soundmenu);
	g_object_set_data_full(G_OBJECT (config), "soundmenu", soundmenu,
	                       (GDestroyNotify) (g_object_unref));
	gtk_widget_show(soundmenu);
	gtk_table_attach(GTK_TABLE (table2), soundmenu, 1, 2, 4, 5,
	                  (GtkAttachOptions) (GTK_FILL),
	                  (GtkAttachOptions) (0), 0, 0);

	std::vector<std::string> sound_paths;

	if (access("/dev/dsp", R_OK | W_OK) == 0) {
		gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT (soundmenu), "OSS: Default sound device (/dev/dsp)");
		sound_paths.push_back("/dev/dsp");
	}

#if DEMOLIB_USE_ALSA
	std::vector<ALSAAudioDriver::ALSASoundCard> alsa_cards = ALSAAudioDriver::enumerate_sound_cards();
	for (unsigned i = 0; i < alsa_cards.size(); ++i) {
		gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT (soundmenu), alsa_cards[i].description.c_str());
		sound_paths.push_back(alsa_cards[i].device);
	}
#endif

	gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT (soundmenu), "No sound");	
	sound_paths.push_back("");

	gtk_combo_box_set_active(GTK_COMBO_BOX (soundmenu), 0);

	res_label = gtk_label_new("Screen resolution:");
	g_object_ref(res_label);
	g_object_set_data_full(G_OBJECT (config), "res_label", res_label,
	                       (GDestroyNotify) (g_object_unref));
	gtk_widget_show(res_label);
	gtk_table_attach(GTK_TABLE (table2), res_label, 0, 1, 1, 2,
	                  (GtkAttachOptions) (GTK_FILL),
	                  (GtkAttachOptions) (0), 0, 0);
	gtk_label_set_justify(GTK_LABEL (res_label), GTK_JUSTIFY_LEFT);
	gtk_misc_set_alignment(GTK_MISC (res_label), 0, 0.5);

	spacefiller1 = gtk_label_new("");
	g_object_ref(spacefiller1);
	g_object_set_data_full(G_OBJECT (config), "spacefiller1", spacefiller1,
	                       (GDestroyNotify) (g_object_unref));
	gtk_widget_show(spacefiller1);
	gtk_table_attach(GTK_TABLE (table2), spacefiller1, 0, 1, 0, 1,
	                  (GtkAttachOptions) (0),
	                  (GtkAttachOptions) (GTK_EXPAND | GTK_FILL), 0, 0);
	gtk_misc_set_alignment(GTK_MISC (spacefiller1), 0, 0.5);

	spacefiller2 = gtk_label_new("");
	g_object_ref(spacefiller2);
	g_object_set_data_full(G_OBJECT (config), "spacefiller2", spacefiller2,
	                       (GDestroyNotify) (g_object_unref));
	gtk_widget_show(spacefiller2);
	gtk_table_attach(GTK_TABLE (table2), spacefiller2, 0, 1, 5, 6,
	                  (GtkAttachOptions) (0),
	                  (GtkAttachOptions) (GTK_EXPAND), 0, 0);
	gtk_misc_set_alignment(GTK_MISC (spacefiller2), 0, 0.5);

	hbox4 = gtk_hbox_new(FALSE, 0);
	g_object_ref(hbox4);
	g_object_set_data_full(G_OBJECT (config), "hbox4", hbox4,
	                       (GDestroyNotify) (g_object_unref));
	gtk_widget_show(hbox4);
	gtk_box_pack_start(GTK_BOX (vbox2), hbox4, FALSE, FALSE, 0);
	gtk_container_set_border_width(GTK_CONTAINER (hbox4), 4);

	ok = gtk_button_new_with_label("Ninja!");
	g_object_ref(ok);
	g_object_set_data_full(G_OBJECT (config), "ok", ok,
	                       (GDestroyNotify) (g_object_unref));
	g_signal_connect(G_OBJECT (ok), "clicked", G_CALLBACK (gtk_main_quit), NULL);

	gtk_widget_show(ok);
	gtk_box_pack_start(GTK_BOX (hbox4), ok, TRUE, TRUE, 1);
	
	int dest_signal = g_signal_connect(G_OBJECT (config), "destroy", G_CALLBACK (exit), NULL);
	gtk_widget_show(config);
	
	gtk_main();

	/* get the parameters that were set */
	int resolution = gtk_combo_box_get_active(GTK_COMBO_BOX (res_menu));
	attr_hash->insert("xres", resolutions[resolution].first.c_str());
	attr_hash->insert("yres", resolutions[resolution].second.c_str());

	if (gtk_combo_box_get_active(GTK_COMBO_BOX (fullscreenmenu)) == 0) {	
		attr_hash->insert("fullscreen", "true");
	} else {
		attr_hash->insert("fullscreen", "false");
	}

	int visual_id = gtk_combo_box_get_active(GTK_COMBO_BOX (visualmenu));
	attr_hash->insert("visual_id", visual_ids[visual_id].c_str());

	int sound = gtk_combo_box_get_active(GTK_COMBO_BOX (soundmenu));
	attr_hash->insert("sound", sound_paths[sound].c_str());

	g_signal_handler_disconnect(G_OBJECT (config), dest_signal);
	gtk_widget_hide(config);
	gtk_widget_destroy(config);

	while (gtk_events_pending())
		gtk_main_iteration_do(false);

	setlocale(LC_ALL, "C");
}