1 /* xscreensaver, Copyright (c) 2006-2009 Jamie Zawinski <jwz@jwz.org>
3 * Permission to use, copy, modify, distribute, and sell this software and its
4 * documentation for any purpose is hereby granted without fee, provided that
5 * the above copyright notice appear in all copies and that both that
6 * copyright notice and this permission notice appear in supporting
7 * documentation. No representations are made about the suitability of this
8 * software for any purpose. It is provided "as is" without express or
12 /* This is a subclass of Apple's ScreenSaverView that knows how to run
13 xscreensaver programs without X11 via the dark magic of the "jwxyz"
14 library. In xscreensaver terminology, this is the replacement for
15 the "screenhack.c" module.
18 #import "XScreenSaverGLView.h"
19 #import "XScreenSaverConfigSheet.h"
20 #import "screenhackI.h"
21 #import "xlockmoreI.h"
23 #import <OpenGL/OpenGL.h>
25 /* used by the OpenGL screen savers
27 extern GLXContext *init_GL (ModeInfo *);
28 extern void glXSwapBuffers (Display *, Window);
29 extern void glXMakeCurrent (Display *, Window, GLXContext);
30 extern void clear_gl_error (void);
31 extern void check_gl_error (const char *type);
34 @implementation XScreenSaverGLView
39 /* #### Do we have to destroy ogl_ctx? */
47 [super stopAnimation];
49 // Without this, the GL frame stays on screen when switching tabs
50 // in System Preferences.
52 [NSOpenGLContext clearCurrentContext];
56 // #### maybe this could/should just be on 'lockFocus' instead?
57 - (void) prepareContext
60 [ogl_ctx makeCurrentContext];
61 // check_gl_error ("makeCurrentContext");
67 - (void) resizeContext
70 [ogl_ctx setView:self];
74 - (void)drawRect:(NSRect)rect
77 [super drawRect:rect];
81 - (NSOpenGLContext *) oglContext
87 - (void) setOglContext: (NSOpenGLContext *) ctx
96 /* Utility functions...
100 /* Called by OpenGL savers using the XLockmore API.
103 init_GL (ModeInfo *mi)
105 Window win = mi->window;
106 XScreenSaverGLView *view = (XScreenSaverGLView *) jwxyz_window_view (win);
107 NSOpenGLContext *ctx = [view oglContext];
111 NSOpenGLPixelFormatAttribute attrs[20];
113 attrs[i++] = NSOpenGLPFAColorSize; attrs[i++] = 24;
114 attrs[i++] = NSOpenGLPFAAlphaSize; attrs[i++] = 8;
115 attrs[i++] = NSOpenGLPFADepthSize; attrs[i++] = 16;
117 if (get_boolean_resource (mi->dpy, "doubleBuffer", "DoubleBuffer"))
118 attrs[i++] = NSOpenGLPFADoubleBuffer;
122 NSOpenGLPixelFormat *pixfmt = [[NSOpenGLPixelFormat alloc]
123 initWithAttributes:attrs];
125 ctx = [[NSOpenGLContext alloc]
126 initWithFormat:pixfmt
128 // [pixfmt release]; // #### ???
131 // Sync refreshes to the vertical blanking interval
133 [ctx setValues:&r forParameter:NSOpenGLCPSwapInterval];
134 check_gl_error ("NSOpenGLCPSwapInterval");
136 // #### "Build and Analyze" says that ctx leaks, because it doesn't
137 // seem to realize that makeCurrentContext retains it (right?)
138 // Not sure what to do to make this warning go away.
140 [ctx makeCurrentContext];
141 check_gl_error ("makeCurrentContext");
143 [view setOglContext:ctx];
145 // Clear frame buffer ASAP, else there are bits left over from other apps.
146 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
148 // glXSwapBuffers (mi->dpy, mi->window);
151 // Enable multi-threading, if possible. This runs most OpenGL commands
152 // and GPU management on a second CPU.
154 # ifndef kCGLCEMPEngine
155 # define kCGLCEMPEngine 313 // Added in MacOS 10.4.8 + XCode 2.4.
157 CGLContextObj cctx = CGLGetCurrentContext();
158 CGLError err = CGLEnable (cctx, kCGLCEMPEngine);
159 if (err != kCGLNoError) {
160 NSLog (@"enabling multi-threaded OpenGL failed: %d", err);
164 // Caller expects a pointer to an opaque struct... which it dereferences.
165 // Don't ask me, it's historical...
166 static int blort = -1;
167 return (void *) &blort;
171 /* Copy the back buffer to the front buffer.
174 glXSwapBuffers (Display *dpy, Window window)
176 XScreenSaverGLView *view = (XScreenSaverGLView *) jwxyz_window_view (window);
177 NSOpenGLContext *ctx = [view oglContext];
178 if (ctx) [ctx flushBuffer]; // despite name, this actually swaps
181 /* Does nothing - prepareContext already did the work.
184 glXMakeCurrent (Display *dpy, Window window, GLXContext context)
189 /* clear away any lingering error codes */
191 clear_gl_error (void)
193 while (glGetError() != GL_NO_ERROR)
198 /* report a GL error. */
200 check_gl_error (const char *type)
205 switch ((i = glGetError())) {
206 case GL_NO_ERROR: return;
207 case GL_INVALID_ENUM: e = "invalid enum"; break;
208 case GL_INVALID_VALUE: e = "invalid value"; break;
209 case GL_INVALID_OPERATION: e = "invalid operation"; break;
210 case GL_STACK_OVERFLOW: e = "stack overflow"; break;
211 case GL_STACK_UNDERFLOW: e = "stack underflow"; break;
212 case GL_OUT_OF_MEMORY: e = "out of memory"; break;
213 #ifdef GL_TABLE_TOO_LARGE_EXT
214 case GL_TABLE_TOO_LARGE_EXT: e = "table too large"; break;
216 #ifdef GL_TEXTURE_TOO_LARGE_EXT
217 case GL_TEXTURE_TOO_LARGE_EXT: e = "texture too large"; break;
220 e = buf; sprintf (buf, "unknown GL error %d", (int) i); break;
222 NSLog (@"%s GL error: %s", type, e);