1 /* xlock-gc.c --- xscreensaver compatibility layer for xlockmore GL modules.
2 * xscreensaver, Copyright (c) 1997, 1998 Jamie Zawinski <jwz@jwz.org>
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation. No representations are made about the suitability of this
9 * software for any purpose. It is provided "as is" without express or
12 * This file, along with xlockmore.h, make it possible to compile an xlockmore
13 * GL module into a standalone program, and thus use it with xscreensaver.
14 * By Jamie Zawinski <jwz@jwz.org> on 31-May-97.
18 #include "screenhack.h"
19 #include "xlockmoreI.h"
24 /* Gag -- we use this to turn X errors from glXCreateContext() into
25 something that will actually make sense to the user.
27 static XErrorHandler orig_ehandler = 0;
28 static Bool got_error = 0;
31 BadValue_ehandler (Display *dpy, XErrorEvent *error)
33 if (error->error_code == BadValue)
39 return orig_ehandler (dpy, error);
44 init_GL(ModeInfo * mi)
46 Display *dpy = mi->dpy;
47 Window window = mi->window;
48 Screen *screen = mi->xgwa.screen;
49 Visual *visual = mi->xgwa.visual;
50 GLXContext glx_context = 0;
51 XVisualInfo vi_in, *vi_out;
54 vi_in.screen = screen_number (screen);
55 vi_in.visualid = XVisualIDFromVisual (visual);
56 vi_out = XGetVisualInfo (dpy, VisualScreenMask|VisualIDMask,
58 if (! vi_out) abort ();
62 orig_ehandler = XSetErrorHandler (BadValue_ehandler);
63 glx_context = glXCreateContext (dpy, vi_out, 0, GL_TRUE);
65 XSetErrorHandler (orig_ehandler);
70 XFree((char *) vi_out);
74 fprintf(stderr, "%s: couldn't create GL context for visual 0x%x.\n",
75 progname, (unsigned int) XVisualIDFromVisual (visual));
79 glXMakeCurrent (dpy, window, glx_context);
82 GLboolean rgba_mode = 0;
83 glGetBooleanv(GL_RGBA_MODE, &rgba_mode);
86 glIndexi (WhitePixelOfScreen (screen));
87 glClearIndex (BlackPixelOfScreen (screen));
91 /* GLXContext is already a pointer type.
92 Why this function returns a pointer to a pointer, I have no idea...
95 GLXContext *ptr = (GLXContext *) malloc(sizeof(GLXContext));
103 get_gl_visual (Screen *screen, char *name, char *class)
105 char *string = get_string_resource (name, class);
107 Bool done_once = False;
110 if (!string || !*string ||
111 !strcmp (string, "best") ||
112 !strcmp (string, "color") ||
113 !strcmp (string, "default"))
115 Display *dpy = DisplayOfScreen (screen);
116 int screen_num = screen_number (screen);
119 Bool dbuf_p = !get_boolean_resource ("noBuffer", "NoBuffer");
123 attrs[i++] = GLX_RGBA;
124 attrs[i++] = GLX_RED_SIZE; attrs[i++] = 1;
125 attrs[i++] = GLX_GREEN_SIZE; attrs[i++] = 1;
126 attrs[i++] = GLX_BLUE_SIZE; attrs[i++] = 1;
127 attrs[i++] = GLX_DEPTH_SIZE; attrs[i++] = 1;
129 attrs[i++] = GLX_DOUBLEBUFFER;
132 vi = glXChooseVisual (dpy, screen_num, attrs);
135 /* Try without double-buffering. */
137 vi = glXChooseVisual (dpy, screen_num, attrs);
143 attrs[i++] = GLX_DOUBLEBUFFER;
145 vi = glXChooseVisual (dpy, screen_num, attrs);
148 /* Try mono without double-buffering. */
150 vi = glXChooseVisual (dpy, screen_num, attrs);
163 v = get_visual (screen, string, False, True);
167 v = DefaultVisualOfScreen (screen);