-
Notifications
You must be signed in to change notification settings - Fork 0
/
view.c
451 lines (377 loc) · 10.8 KB
/
view.c
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
440
441
442
443
444
445
446
447
448
449
450
451
/* StonerView: An eccentric visual toy.
Copyright 1998-2001 by Andrew Plotkin (erkyrath@eblong.com)
http://www.eblong.com/zarf/stonerview.html
This program is distributed under the GPL.
See main.c, the Copying document, or the above URL for details.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <X11/keysym.h>
#include <GL/gl.h>
#include <GL/glx.h>
#include "general.h"
#include "osc.h"
#include "view.h"
#include "vroot.h"
#include "move.h"
static char *progclass = "StonerView";
static char *progname = NULL;
static GLfloat view_rotx = -45.0, view_roty = 0.0, view_rotz = 0.0;
static GLfloat view_scale = 4.0;
static void setup_window(void);
static void win_reshape(int width, int height);
static void handle_events(void);
static Display *dpy;
static Window window;
static int wireframe = FALSE;
static int addedges = FALSE;
static Atom XA_WM_PROTOCOLS, XA_WM_DELETE_WINDOW;
static void usage(void)
{
fprintf(stderr,
"usage: %s [--geom =WxH+X+Y | --fullscreen | --root] [--wire]\n",
progname);
exit(1);
}
static int visual_depth(Display *dpy, int screen, Visual *visual)
{
XVisualInfo vi_in, *vi_out;
int out_count, depth;
vi_in.screen = screen;
vi_in.visualid = XVisualIDFromVisual(visual);
vi_out = XGetVisualInfo(dpy, (VisualScreenMask | VisualIDMask),
&vi_in, &out_count);
if (!vi_out)
return -1;
depth = vi_out[0].depth;
XFree(vi_out);
return depth;
}
#define UNDEF (-65536)
int init_view(int *argc, char *argv[])
{
int ix;
int fullscreen = FALSE;
int on_root = FALSE;
int x = UNDEF, y = UNDEF;
int depth = -1;
int w = 400, h = 400;
char *dpystr = (char *)getenv("DISPLAY");
char *geom = NULL;
int screen;
Visual *visual = NULL;
XWindowAttributes xgwa;
XSetWindowAttributes xswa;
unsigned long xswa_mask = 0;
XSizeHints hints;
GLXContext glx_context = 0;
memset(&hints, 0, sizeof(hints));
progname = argv[0];
for (ix=1; ix<*argc; ix++) {
if (argv[ix][0] == '-' && argv[ix][1] == '-')
argv[ix]++;
if (!strcmp(argv[ix], "-geometry") ||
!strcmp(argv[ix], "-geom")) {
if (on_root || fullscreen) usage();
geom = argv[++ix];
}
else if (!strcmp(argv[ix], "-display") ||
!strcmp(argv[ix], "-disp") ||
!strcmp(argv[ix], "-dpy"))
dpystr = argv[++ix];
else if (!strcmp(argv[ix], "-root")) {
if (geom || fullscreen) usage();
on_root = TRUE;
}
else if (!strcmp(argv[ix], "-fullscreen") ||
!strcmp(argv[ix], "-full")) {
if (on_root || geom) usage();
fullscreen = TRUE;
}
else if (!strcmp(argv[ix], "-wireframe") ||
!strcmp(argv[ix], "-wire")) {
wireframe = TRUE;
}
else if (!strcmp(argv[ix], "-edges") ||
!strcmp(argv[ix], "-edge")) {
addedges = TRUE;
}
else {
usage();
}
}
dpy = XOpenDisplay (dpystr);
if (!dpy) {
fprintf(stderr, "%s: unable to open display %s\n",
progname, dpystr);
return FALSE;
}
screen = DefaultScreen (dpy);
XA_WM_PROTOCOLS = XInternAtom (dpy, "WM_PROTOCOLS", False);
XA_WM_DELETE_WINDOW = XInternAtom (dpy, "WM_DELETE_WINDOW", False);
if (on_root) {
window = RootWindow (dpy, screen);
XGetWindowAttributes (dpy, window, &xgwa);
visual = xgwa.visual;
w = xgwa.width;
h = xgwa.height;
}
else {
int ww = WidthOfScreen (DefaultScreenOfDisplay (dpy));
int hh = HeightOfScreen (DefaultScreenOfDisplay (dpy));
if (fullscreen) {
w = ww;
h = hh;
}
else if (geom) {
char c;
if (4 == sscanf (geom, "=%dx%d+%d+%d%c", &w, &h, &x, &y, &c))
;
else if (4 == sscanf (geom, "=%dx%d-%d+%d%c", &w, &h, &x, &y, &c))
x = ww-w-x;
else if (4 == sscanf (geom, "=%dx%d+%d-%d%c", &w, &h, &x, &y, &c))
y = hh-h-y;
else if (4 == sscanf (geom, "=%dx%d-%d-%d%c", &w, &h, &x, &y, &c))
x = ww-w-x, y = hh-h-y;
else if (2 == sscanf (geom, "=%dx%d%c", &w, &h, &c))
;
else if (2 == sscanf (geom, "+%d+%d%c", &x, &y, &c))
;
else if (2 == sscanf (geom, "-%d+%d%c", &x, &y, &c))
x = ww-w-x;
else if (2 == sscanf (geom, "+%d-%d%c", &x, &y, &c))
y = hh-h-y;
else if (2 == sscanf (geom, "-%d-%d%c", &x, &y, &c))
x = ww-w-x, y = hh-h-y;
else {
fprintf(stderr, "%s: unparsable geometry: %s\n",
progname, geom);
return FALSE;
}
hints.flags = USSize;
hints.width = w;
hints.height = h;
if (x != UNDEF && y != UNDEF) {
hints.flags |= USPosition;
hints.x = x;
hints.y = y;
}
}
/* Pick a good GL visual */
{
#define R GLX_RED_SIZE
#define G GLX_GREEN_SIZE
#define B GLX_BLUE_SIZE
#define D GLX_DEPTH_SIZE
#define I GLX_BUFFER_SIZE
#define DB GLX_DOUBLEBUFFER
int attrs[][20] = {
{ GLX_RGBA, R, 8, G, 8, B, 8, D, 8, DB, 0 }, /* rgb double */
{ GLX_RGBA, R, 4, G, 4, B, 4, D, 4, DB, 0 },
{ GLX_RGBA, R, 2, G, 2, B, 2, D, 2, DB, 0 },
{ GLX_RGBA, R, 8, G, 8, B, 8, D, 8, 0 }, /* rgb single */
{ GLX_RGBA, R, 4, G, 4, B, 4, D, 4, 0 },
{ GLX_RGBA, R, 2, G, 2, B, 2, D, 2, 0 },
{ I, 8, D, 8, DB, 0 }, /* cmap double */
{ I, 4, D, 4, DB, 0 },
{ I, 8, D, 8, 0 }, /* cmap single */
{ I, 4, D, 4, 0 },
{ GLX_RGBA, R, 1, G, 1, B, 1, D, 1, 0 } /* monochrome */
};
int i;
for (i = 0; i < sizeof(attrs)/sizeof(*attrs); i++) {
XVisualInfo *vi = glXChooseVisual (dpy, screen, attrs[i]);
if (vi) {
visual = vi->visual;
XFree (vi);
break;
}
}
if (!visual) {
fprintf (stderr, "%s: unable to find a GL visual\n", progname);
return FALSE;
}
}
if (x == UNDEF) x = 0;
if (y == UNDEF) y = 0;
xswa_mask = (CWEventMask | CWColormap |
CWBackPixel | CWBackingPixel | CWBorderPixel );
xswa.colormap = XCreateColormap (dpy, RootWindow (dpy, screen),
visual, AllocNone);
xswa.background_pixel = BlackPixel (dpy, screen);
xswa.backing_pixel = xswa.background_pixel;
xswa.border_pixel = xswa.background_pixel;
xswa.event_mask = (KeyPressMask | ButtonPressMask | StructureNotifyMask);
depth = visual_depth (dpy, screen, visual);
if (depth < 0)
return FALSE;
window = XCreateWindow(dpy, RootWindow(dpy, screen),
x, y, w, h, 0,
depth,
InputOutput, visual,
xswa_mask, &xswa);
{
XTextProperty tp;
XStringListToTextProperty (&progclass, 1, &tp);
XSetWMProperties (dpy, window, &tp, &tp, argv, *argc, &hints, 0, 0);
}
XChangeProperty (dpy, window, XA_WM_PROTOCOLS, XA_ATOM, 32,
PropModeReplace,
(unsigned char *)&XA_WM_DELETE_WINDOW, 1);
XMapRaised (dpy, window);
XSync (dpy, False);
}
/* Now hook up to GLX */
{
XVisualInfo vi_in, *vi_out;
int out_count;
vi_in.screen = screen;
vi_in.visualid = XVisualIDFromVisual (visual);
vi_out = XGetVisualInfo (dpy, VisualScreenMask|VisualIDMask,
&vi_in, &out_count);
if (!vi_out)
return FALSE;
glx_context = glXCreateContext (dpy, vi_out, 0, GL_TRUE);
XFree(vi_out);
if (!glx_context) {
fprintf(stderr, "%s: couldn't create GL context for root window.\n",
progname);
return FALSE;
}
glXMakeCurrent (dpy, window, glx_context);
}
setup_window();
win_reshape(w, h);
return TRUE;
}
static void setup_window()
{
glEnable(GL_CULL_FACE);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
}
/* callback: draw everything */
void win_draw(void)
{
int ix;
static GLfloat white[] = { 1.0, 1.0, 1.0, 1.0 };
static GLfloat grey[] = { 0.6, 0.6, 0.6, 1.0 };
glDrawBuffer(GL_BACK);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glScalef(view_scale, view_scale, view_scale);
glRotatef(view_rotx, 1.0, 0.0, 0.0);
glRotatef(view_roty, 0.0, 1.0, 0.0);
glRotatef(view_rotz, 0.0, 0.0, 1.0);
glShadeModel(GL_FLAT);
for (ix=0; ix<NUM_ELS; ix++) {
elem_t *el = &elist[ix];
glNormal3f(0.0, 0.0, 1.0);
if (addedges || wireframe) {
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE,
(wireframe ? white : grey));
glBegin(GL_LINE_LOOP);
glVertex3f(el->pos[0] - el->vervec[0], el->pos[1] - el->vervec[1],
el->pos[2]);
glVertex3f(el->pos[0] + el->vervec[1], el->pos[1] - el->vervec[0],
el->pos[2]);
glVertex3f(el->pos[0] + el->vervec[0], el->pos[1] + el->vervec[1],
el->pos[2]);
glVertex3f(el->pos[0] - el->vervec[1], el->pos[1] + el->vervec[0],
el->pos[2]);
glEnd();
}
if (!wireframe) {
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE,
el->col);
glBegin(GL_QUADS);
glVertex3f(el->pos[0] - el->vervec[0], el->pos[1] - el->vervec[1],
el->pos[2]);
glVertex3f(el->pos[0] + el->vervec[1], el->pos[1] - el->vervec[0],
el->pos[2]);
glVertex3f(el->pos[0] + el->vervec[0], el->pos[1] + el->vervec[1],
el->pos[2]);
glVertex3f(el->pos[0] - el->vervec[1], el->pos[1] + el->vervec[0],
el->pos[2]);
glEnd();
}
}
glPopMatrix();
glFinish();
glXSwapBuffers(dpy, window);
handle_events();
}
/* callback: new window size or exposure */
static void win_reshape(int width, int height)
{
GLfloat h = (GLfloat) height / (GLfloat) width;
glViewport(0, 0, (GLint) width, (GLint) height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0, 1.0, -h, h, 5.0, 60.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -40.0);
}
static void handle_events(void)
{
while (XPending(dpy)) {
XEvent evstruct;
XEvent *event = &evstruct;
XNextEvent (dpy, event);
switch (event->xany.type) {
case ConfigureNotify:
{
XWindowAttributes xgwa;
XGetWindowAttributes (dpy, window, &xgwa);
win_reshape (xgwa.width, xgwa.height);
break;
}
case KeyPress:
{
KeySym keysym;
char c = 0;
XLookupString (&event->xkey, &c, 1, &keysym, 0);
if (c == 'q' ||
c == 'Q' ||
c == 3 || /* ^C */
c == 27) /* ESC */
exit (0);
else if (! (keysym >= XK_Shift_L && keysym <= XK_Hyper_R))
XBell (dpy, 0); /* beep for non-chord keys */
}
break;
case ButtonPress:
XBell (dpy, 0);
break;
case ClientMessage:
{
if (event->xclient.message_type != XA_WM_PROTOCOLS) {
char *s = XGetAtomName(dpy, event->xclient.message_type);
if (!s) s = "(null)";
fprintf (stderr, "%s: unknown ClientMessage %s received!\n",
progname, s);
}
else if (event->xclient.data.l[0] != XA_WM_DELETE_WINDOW) {
char *s1 = XGetAtomName(dpy, event->xclient.message_type);
char *s2 = XGetAtomName(dpy, event->xclient.data.l[0]);
if (!s1) s1 = "(null)";
if (!s2) s2 = "(null)";
fprintf (stderr,"%s: unknown ClientMessage %s[%s] received!\n",
progname, s1, s2);
}
else {
exit (0);
}
}
break;
}
}
}