diff options
author | Robert <krzmbrzl@gmail.com> | 2020-09-11 19:29:33 +0300 |
---|---|---|
committer | Robert <krzmbrzl@gmail.com> | 2020-09-11 19:29:33 +0300 |
commit | af7dac72f4063dd8d4dac71973ea51c25896089a (patch) | |
tree | 35558695aea94d48fd9e77d164d63fd8b7c76e90 /overlay_gl | |
parent | 40b28b03c150b453e00c6bc4f8d6957caea59c51 (diff) |
FORMAT: Run clang-format 10 on all C/CXX source-files
Diffstat (limited to 'overlay_gl')
-rw-r--r-- | overlay_gl/avail_mac.h | 66 | ||||
-rw-r--r-- | overlay_gl/init_mac.c | 44 | ||||
-rw-r--r-- | overlay_gl/init_unix.c | 90 | ||||
-rw-r--r-- | overlay_gl/overlay.c | 396 |
4 files changed, 278 insertions, 318 deletions
diff --git a/overlay_gl/avail_mac.h b/overlay_gl/avail_mac.h index 1c8d63610..7b876d4fd 100644 --- a/overlay_gl/avail_mac.h +++ b/overlay_gl/avail_mac.h @@ -8,57 +8,19 @@ // This file was auto-generated by 'avail_mac.pl'. Do not touch by hand. -#define AVAIL_ALL_GLSYM ( \ - AVAIL(glActiveTexture) && \ - AVAIL(glAttachShader) && \ - AVAIL(glBindBuffer) && \ - AVAIL(glBindTexture) && \ - AVAIL(glBlendFunc) && \ - AVAIL(glColorMaterial) && \ - AVAIL(glCompileShader) && \ - AVAIL(glCreateProgram) && \ - AVAIL(glCreateShader) && \ - AVAIL(glDeleteTextures) && \ - AVAIL(glDisable) && \ - AVAIL(glDisableClientState) && \ - AVAIL(glDisableVertexAttribArray) && \ - AVAIL(glDrawArrays) && \ - AVAIL(glEnable) && \ - AVAIL(glEnableClientState) && \ - AVAIL(glEnableVertexAttribArray) && \ - AVAIL(glGenTextures) && \ - AVAIL(glGetBooleanv) && \ - AVAIL(glGetError) && \ - AVAIL(glGetIntegerv) && \ - AVAIL(glGetShaderInfoLog) && \ - AVAIL(glGetString) && \ - AVAIL(glGetTexParameterfv) && \ - AVAIL(glGetUniformLocation) && \ - AVAIL(glGetVertexAttribiv) && \ - AVAIL(glIsTexture) && \ - AVAIL(glLinkProgram) && \ - AVAIL(glLoadIdentity) && \ - AVAIL(glMatrixMode) && \ - AVAIL(glOrtho) && \ - AVAIL(glPixelStorei) && \ - AVAIL(glPopAttrib) && \ - AVAIL(glPopClientAttrib) && \ - AVAIL(glPopMatrix) && \ - AVAIL(glPushAttrib) && \ - AVAIL(glPushClientAttrib) && \ - AVAIL(glPushMatrix) && \ - AVAIL(glRenderMode) && \ - AVAIL(glShaderSource) && \ - AVAIL(glTexCoordPointer) && \ - AVAIL(glTexEnvi) && \ - AVAIL(glTexImage2D) && \ - AVAIL(glTexParameterfv) && \ - AVAIL(glTexParameteri) && \ - AVAIL(glTexSubImage2D) && \ - AVAIL(glUniform1i) && \ - AVAIL(glUseProgram) && \ - AVAIL(glVertexPointer) && \ - AVAIL(glViewport) && \ - 1) +#define AVAIL_ALL_GLSYM \ + (AVAIL(glActiveTexture) && AVAIL(glAttachShader) && AVAIL(glBindBuffer) && AVAIL(glBindTexture) \ + && AVAIL(glBlendFunc) && AVAIL(glColorMaterial) && AVAIL(glCompileShader) && AVAIL(glCreateProgram) \ + && AVAIL(glCreateShader) && AVAIL(glDeleteTextures) && AVAIL(glDisable) && AVAIL(glDisableClientState) \ + && AVAIL(glDisableVertexAttribArray) && AVAIL(glDrawArrays) && AVAIL(glEnable) && AVAIL(glEnableClientState) \ + && AVAIL(glEnableVertexAttribArray) && AVAIL(glGenTextures) && AVAIL(glGetBooleanv) && AVAIL(glGetError) \ + && AVAIL(glGetIntegerv) && AVAIL(glGetShaderInfoLog) && AVAIL(glGetString) && AVAIL(glGetTexParameterfv) \ + && AVAIL(glGetUniformLocation) && AVAIL(glGetVertexAttribiv) && AVAIL(glIsTexture) && AVAIL(glLinkProgram) \ + && AVAIL(glLoadIdentity) && AVAIL(glMatrixMode) && AVAIL(glOrtho) && AVAIL(glPixelStorei) && AVAIL(glPopAttrib) \ + && AVAIL(glPopClientAttrib) && AVAIL(glPopMatrix) && AVAIL(glPushAttrib) && AVAIL(glPushClientAttrib) \ + && AVAIL(glPushMatrix) && AVAIL(glRenderMode) && AVAIL(glShaderSource) && AVAIL(glTexCoordPointer) \ + && AVAIL(glTexEnvi) && AVAIL(glTexImage2D) && AVAIL(glTexParameterfv) && AVAIL(glTexParameteri) \ + && AVAIL(glTexSubImage2D) && AVAIL(glUniform1i) && AVAIL(glUseProgram) && AVAIL(glVertexPointer) \ + && AVAIL(glViewport) && 1) #endif diff --git a/overlay_gl/init_mac.c b/overlay_gl/init_mac.c index aabda7312..ae904b21d 100644 --- a/overlay_gl/init_mac.c +++ b/overlay_gl/init_mac.c @@ -7,7 +7,7 @@ // Mac-specific overlay initialization. @implementation NSOpenGLContext (MumbleOverlay) -- (void) overlayFlushBuffer { +- (void)overlayFlushBuffer { ods("[NSOpenGLContext flushBuffer] %p %p", self, [self CGLContextObj]); Context *c = contexts; @@ -24,10 +24,10 @@ ods("malloc failure"); return; } - c->next = contexts; - c->nsctx = (NSOpenGLContext *)self; + c->next = contexts; + c->nsctx = (NSOpenGLContext *) self; c->cglctx = (CGLContextObj)[self CGLContextObj]; - contexts = c; + contexts = c; newContext(c); } @@ -35,12 +35,12 @@ int width = 0, height = 0; if (v) { NSRect r = [v bounds]; - width = (int)r.size.width; - height = (int)r.size.height; + width = (int) r.size.width; + height = (int) r.size.height; } else { GLint viewport[4]; glGetIntegerv(GL_VIEWPORT, viewport); - width = viewport[2]; + width = viewport[2]; height = viewport[3]; } @@ -85,16 +85,16 @@ void CGLFlushDrawableOverride(CGLContextObj ctx) { return; } memset(c, 0, sizeof(Context)); - c->next = contexts; + c->next = contexts; c->cglctx = ctx; - c->nsctx = NULL; - contexts = c; + c->nsctx = NULL; + contexts = c; newContext(c); } GLint viewport[4]; glGetIntegerv(GL_VIEWPORT, viewport); - int width = viewport[2]; + int width = viewport[2]; int height = viewport[3]; /* Are the viewport values crazy? Skip them in that case. */ if (height < 0 || width < 0 || height > 5000 || width > 5000) { @@ -117,8 +117,7 @@ CGError CGDisplayHideCursorOverride(CGDirectDisplayID display) { return oCGDisplayHideCursor(display); } -__attribute__((constructor)) -static void initializeLibrary() { +__attribute__((constructor)) static void initializeLibrary() { struct stat buf; bDebug = (stat("/Library/Application Support/.mumble_overlay_debug", &buf) == 0); @@ -127,7 +126,7 @@ static void initializeLibrary() { void *nsgl = NULL, *cgl = NULL; nsgl = dlsym(RTLD_DEFAULT, "NSClassFromString"); - cgl = dlsym(RTLD_DEFAULT, "CGLFlushDrawable"); + cgl = dlsym(RTLD_DEFAULT, "CGLFlushDrawable"); /* Check for GL symbol availability */ if (!(AVAIL_ALL_GLSYM)) { @@ -141,9 +140,10 @@ static void initializeLibrary() { Class c = NSClassFromString(@"NSOpenGLContext"); if (c) { Method orig = class_getInstanceMethod(c, @selector(flushBuffer)); - Method new = class_getInstanceMethod(c, @selector(overlayFlushBuffer)); + Method new = class_getInstanceMethod(c, @selector(overlayFlushBuffer)); if (class_addMethod(c, @selector(flushBuffer), method_getImplementation(new), method_getTypeEncoding(new))) - class_replaceMethod(c, @selector(overlayFlushBuffer), method_getImplementation(orig), method_getTypeEncoding(orig)); + class_replaceMethod(c, @selector(overlayFlushBuffer), method_getImplementation(orig), + method_getTypeEncoding(orig)); else method_exchangeImplementations(orig, new); } @@ -152,7 +152,9 @@ static void initializeLibrary() { /* CGL */ if (AVAIL(CGLFlushDrawable)) { ods("Attempting to hook CGL"); - if (mach_override_ptr(dlsym(RTLD_DEFAULT, "CGLFlushDrawable"), CGLFlushDrawableOverride, (void **) &oCGLFlushDrawable) != 0) { + if (mach_override_ptr(dlsym(RTLD_DEFAULT, "CGLFlushDrawable"), CGLFlushDrawableOverride, + (void **) &oCGLFlushDrawable) + != 0) { ods("CGLFlushDrawable override failed."); } else ods("Up running."); @@ -161,10 +163,14 @@ static void initializeLibrary() { } if (AVAIL(CGDisplayHideCursor) && AVAIL(CGDisplayShowCursor)) { - if (mach_override_ptr(dlsym(RTLD_DEFAULT, "CGDisplayHideCursor"), CGDisplayHideCursorOverride, (void **) &oCGDisplayHideCursor) != 0) { + if (mach_override_ptr(dlsym(RTLD_DEFAULT, "CGDisplayHideCursor"), CGDisplayHideCursorOverride, + (void **) &oCGDisplayHideCursor) + != 0) { ods("CGDisplayHideCursor override failed"); } - if (mach_override_ptr(dlsym(RTLD_DEFAULT, "CGDisplayShowCursor"), CGDisplayShowCursorOverride, (void **) &oCGDisplayShowCursor) != 0) { + if (mach_override_ptr(dlsym(RTLD_DEFAULT, "CGDisplayShowCursor"), CGDisplayShowCursorOverride, + (void **) &oCGDisplayShowCursor) + != 0) { ods("CGDisplayShowCursor override failed"); } ods("Hooked CGDisplayShowCursor and CGDisplayHideCursor"); diff --git a/overlay_gl/init_unix.c b/overlay_gl/init_unix.c index 48954d12b..349611c3c 100644 --- a/overlay_gl/init_unix.c +++ b/overlay_gl/init_unix.c @@ -8,16 +8,18 @@ static void initializeLibrary(); -#define RESOLVE(x) if (! o##x) o##x = (__typeof__(&x)) odlsym(RTLD_NEXT, #x) +#define RESOLVE(x) \ + if (!o##x) \ + o##x = (__typeof__(&x)) odlsym(RTLD_NEXT, #x) static void resolveOpenGL() { RESOLVE(glXSwapBuffers); - if (! oglXSwapBuffers) { + if (!oglXSwapBuffers) { void *lib = dlopen("libGL.so.1", RTLD_GLOBAL | RTLD_NOLOAD); - if (! lib) + if (!lib) return; RESOLVE(glXSwapBuffers); - if (! oglXSwapBuffers) { + if (!oglXSwapBuffers) { dlclose(lib); } } @@ -26,8 +28,7 @@ static void resolveOpenGL() { RESOLVE(glXGetProcAddress); } -__attribute__((visibility("default"))) -void glXSwapBuffers(Display * dpy, GLXDrawable draw) { +__attribute__((visibility("default"))) void glXSwapBuffers(Display *dpy, GLXDrawable draw) { if (!oglXSwapBuffers) { resolveOpenGL(); } @@ -51,10 +52,10 @@ void glXSwapBuffers(Display * dpy, GLXDrawable draw) { } memset(c, 0, sizeof(Context)); c->next = contexts; - c->dpy = dpy; + c->dpy = dpy; c->draw = draw; - c->bMesa = false; + c->bMesa = false; c->bValid = false; int major, minor; @@ -80,11 +81,11 @@ void glXSwapBuffers(Display * dpy, GLXDrawable draw) { if (c->bMesa) { GLint viewport[4]; glGetIntegerv(GL_VIEWPORT, viewport); - width = viewport[2]; + width = viewport[2]; height = viewport[3]; } else { - glXQueryDrawable(dpy, draw, GLX_WIDTH, (GLuint*)&width); - glXQueryDrawable(dpy, draw, GLX_HEIGHT, (GLuint*)&height); + glXQueryDrawable(dpy, draw, GLX_WIDTH, (GLuint *) &width); + glXQueryDrawable(dpy, draw, GLX_HEIGHT, (GLuint *) &height); } drawContext(c, width, height); @@ -92,10 +93,11 @@ void glXSwapBuffers(Display * dpy, GLXDrawable draw) { oglXSwapBuffers(dpy, draw); } -#define FGRAB(x) if (strcmp((const char *)(func), #x)==0) return (__GLXextFuncPtr)(x); +#define FGRAB(x) \ + if (strcmp((const char *) (func), #x) == 0) \ + return (__GLXextFuncPtr)(x); -__attribute__((visibility("default"))) -void (*glXGetProcAddress(const GLubyte * func))(void) { +__attribute__((visibility("default"))) void (*glXGetProcAddress(const GLubyte *func))(void) { FGRAB(glXSwapBuffers); FGRAB(glXGetProcAddressARB); FGRAB(glXGetProcAddress); @@ -108,17 +110,22 @@ void (*glXGetProcAddress(const GLubyte * func))(void) { else if (oglXGetProcAddressARB) return oglXGetProcAddressARB(func); else - return (__GLXextFuncPtr)(odlsym(RTLD_NEXT, (const char *)(func))); + return (__GLXextFuncPtr)(odlsym(RTLD_NEXT, (const char *) (func))); } -__attribute__((visibility("default"))) -__GLXextFuncPtr glXGetProcAddressARB(const GLubyte * func) { +__attribute__((visibility("default"))) __GLXextFuncPtr glXGetProcAddressARB(const GLubyte *func) { return (void (*)(void)) glXGetProcAddress(func); } -#define OGRAB(name) if (handle == RTLD_DEFAULT) handle = RTLD_NEXT; symbol = odlsym(handle, #name); if (symbol) { o##name = (__typeof__(&name)) symbol; symbol = (void *) name;} -__attribute__((visibility("default"))) -void *dlsym(void *handle, const char *name) { +#define OGRAB(name) \ + if (handle == RTLD_DEFAULT) \ + handle = RTLD_NEXT; \ + symbol = odlsym(handle, #name); \ + if (symbol) { \ + o##name = (__typeof__(&name)) symbol; \ + symbol = (void *) name; \ + } +__attribute__((visibility("default"))) void *dlsym(void *handle, const char *name) { if (!odlsym) { initializeLibrary(); } @@ -147,32 +154,32 @@ static int find_odlsym() { if (!dl) { ods("Failed to open libdl.so.2"); } else { - int i = 0; + int i = 0; struct link_map *lm = (struct link_map *) dl; - int nchains = 0; - ElfW(Sym) *symtab = NULL; - const char *strtab = NULL; -#if defined(__GLIBC__) + int nchains = 0; + ElfW(Sym) *symtab = NULL; + const char *strtab = NULL; +# if defined(__GLIBC__) const ElfW(Addr) base = 0; -#else +# else const ElfW(Addr) base = lm->l_addr; -#endif +# endif ElfW(Dyn) *dyn = lm->l_ld; while (dyn->d_tag) { switch (dyn->d_tag) { case DT_HASH: - nchains = *(int *)(base + dyn->d_un.d_ptr + 4); + nchains = *(int *) (base + dyn->d_un.d_ptr + 4); break; case DT_STRTAB: - strtab = (const char *)(base + dyn->d_un.d_ptr); + strtab = (const char *) (base + dyn->d_un.d_ptr); break; case DT_SYMTAB: - symtab = (ElfW(Sym) *)(base + dyn->d_un.d_ptr); + symtab = (ElfW(Sym) *) (base + dyn->d_un.d_ptr); break; } - dyn ++; + dyn++; } ods("Iterating dlsym table %p %p %d", symtab, strtab, nchains); for (i = 0; i < nchains; i++) { @@ -180,8 +187,8 @@ static int find_odlsym() { if (ELF32_ST_TYPE(symtab[i].st_info) != STT_FUNC) { continue; } - if (strcmp(strtab+symtab[i].st_name, "dlsym") == 0) { - odlsym = (void*)lm->l_addr + symtab[i].st_value; + if (strcmp(strtab + symtab[i].st_name, "dlsym") == 0) { + odlsym = (void *) lm->l_addr + symtab[i].st_value; } } if (odlsym == NULL) { @@ -192,11 +199,11 @@ static int find_odlsym() { return 0; #elif defined(__FreeBSD__) - int i = 0; + int i = 0; struct link_map *lm = NULL; - int nchains = 0; - Elf_Sym * symtab = NULL; - const char *strtab = NULL; + int nchains = 0; + Elf_Sym *symtab = NULL; + const char *strtab = NULL; if (dlinfo(RTLD_SELF, RTLD_DI_LINKMAP, &lm) == -1) { ods("Unable to acquire link_map: %s", dlerror()); @@ -218,13 +225,13 @@ static int find_odlsym() { while (dyn->d_tag) { switch (dyn->d_tag) { case DT_HASH: - nchains = *(int *)((uintptr_t)lm->l_addr + (uintptr_t)dyn->d_un.d_ptr + 4); + nchains = *(int *) ((uintptr_t) lm->l_addr + (uintptr_t) dyn->d_un.d_ptr + 4); break; case DT_STRTAB: - strtab = (const char *)((uintptr_t)lm->l_addr + (uintptr_t)dyn->d_un.d_ptr); + strtab = (const char *) ((uintptr_t) lm->l_addr + (uintptr_t) dyn->d_un.d_ptr); break; case DT_SYMTAB: - symtab = (Elf_Sym *)((uintptr_t)lm->l_addr + (uintptr_t)dyn->d_un.d_ptr); + symtab = (Elf_Sym *) ((uintptr_t) lm->l_addr + (uintptr_t) dyn->d_un.d_ptr); break; } dyn++; @@ -249,8 +256,7 @@ err: return -1; } -__attribute__((constructor)) -static void initializeLibrary() { +__attribute__((constructor)) static void initializeLibrary() { if (odlsym) return; diff --git a/overlay_gl/overlay.c b/overlay_gl/overlay.c index 06c5cb5cb..136abf931 100644 --- a/overlay_gl/overlay.c +++ b/overlay_gl/overlay.c @@ -7,56 +7,55 @@ #define GL_GLEXT_PROTOTYPES #define _GNU_SOURCE #include <dlfcn.h> +#include <errno.h> +#include <fcntl.h> +#include <limits.h> +#include <math.h> +#include <pwd.h> +#include <semaphore.h> +#include <stdarg.h> #include <stdio.h> -#include <unistd.h> #include <stdlib.h> #include <string.h> -#include <sys/types.h> -#include <sys/mman.h> #include <sys/ipc.h> -#include <sys/time.h> +#include <sys/mman.h> #include <sys/socket.h> -#include <sys/un.h> #include <sys/stat.h> +#include <sys/time.h> +#include <sys/types.h> +#include <sys/un.h> #include <time.h> -#include <semaphore.h> -#include <fcntl.h> -#include <stdarg.h> -#include <pwd.h> -#include <math.h> -#include <errno.h> -#include <time.h> -#include <limits.h> +#include <unistd.h> #if defined(TARGET_UNIX) -# define GLX_GLXEXT_LEGACY -# define GL_GLEXT_PROTOTYPES -# define _GNU_SOURCE -# include <GL/glx.h> -# include <GL/gl.h> -# include <GL/glext.h> +# define GLX_GLXEXT_LEGACY +# define GL_GLEXT_PROTOTYPES +# define _GNU_SOURCE +# include <GL/gl.h> +# include <GL/glext.h> +# include <GL/glx.h> -#include <link.h> +# include <link.h> typedef unsigned char bool; -# define true 1 -# define false 0 +# define true 1 +# define false 0 #elif defined(TARGET_MAC) -# include <OpenGL/OpenGL.h> -# include <Carbon/Carbon.h> -# include <Cocoa/Cocoa.h> -# include <AGL/agl.h> +# include <AGL/agl.h> +# include <Carbon/Carbon.h> +# include <Cocoa/Cocoa.h> +# include <OpenGL/OpenGL.h> # -# include <objc/objc-runtime.h> +# include <objc/objc-runtime.h> # -# include "mach_override.h" +# include "mach_override.h" # -# include "avail_mac.h" +# include "avail_mac.h" #endif #include "../overlay/overlay.h" -static bool bDebug = false; +static bool bDebug = false; static bool bCursorAvail = false; typedef struct _Context { @@ -93,42 +92,41 @@ typedef struct _Context { unsigned int frameCount; GLint maxVertexAttribs; - GLboolean* vertexAttribStates; + GLboolean *vertexAttribStates; } Context; static const char vshader[] = "" - "void main() {" - "gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;" - "gl_TexCoord[0] = gl_MultiTexCoord0;" - "}"; + "void main() {" + "gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;" + "gl_TexCoord[0] = gl_MultiTexCoord0;" + "}"; static const char fshader[] = "" - "uniform sampler2D tex;" - "void main() {" - "gl_FragColor = texture2D(tex, gl_TexCoord[0].st);" - "}"; + "uniform sampler2D tex;" + "void main() {" + "gl_FragColor = texture2D(tex, gl_TexCoord[0].st);" + "}"; -const GLfloat fBorder[] = {0.125f, 0.250f, 0.5f, 0.75f}; +const GLfloat fBorder[] = { 0.125f, 0.250f, 0.5f, 0.75f }; static Context *contexts = NULL; -#define AVAIL(name) dlsym(RTLD_DEFAULT,#name) +#define AVAIL(name) dlsym(RTLD_DEFAULT, #name) #define FDEF(name) static __typeof__(&name) o##name = NULL #if defined(TARGET_UNIX) - FDEF(dlsym); - FDEF(glXSwapBuffers); - FDEF(glXGetProcAddressARB); - FDEF(glXGetProcAddress); +FDEF(dlsym); +FDEF(glXSwapBuffers); +FDEF(glXGetProcAddressARB); +FDEF(glXGetProcAddress); #elif defined(TARGET_MAC) - FDEF(CGLFlushDrawable); - FDEF(CGDisplayHideCursor); - FDEF(CGDisplayShowCursor); +FDEF(CGLFlushDrawable); +FDEF(CGDisplayHideCursor); +FDEF(CGDisplayShowCursor); #endif -__attribute__((format(printf, 1, 2))) -static void ods(const char *format, ...) { - if (! bDebug) { +__attribute__((format(printf, 1, 2))) static void ods(const char *format, ...) { + if (!bDebug) { return; } @@ -142,16 +140,16 @@ static void ods(const char *format, ...) { fflush(stderr); } -static void newContext(Context * ctx) { - ctx->iSocket = -1; +static void newContext(Context *ctx) { + ctx->iSocket = -1; ctx->omMsg.omh.iLength = -1; - ctx->texture = ~0U; - ctx->timeT = clock(); - ctx->frameCount = 0; + ctx->texture = ~0U; + ctx->timeT = clock(); + ctx->frameCount = 0; char *home = getenv("HOME"); if (home == NULL) { - struct passwd *pwent= getpwuid(getuid()); + struct passwd *pwent = getpwuid(getuid()); if (pwent && pwent->pw_dir && pwent->pw_dir[0]) { home = pwent->pw_dir; } @@ -169,7 +167,8 @@ static void newContext(Context * ctx) { strcat(ctx->saName.sun_path, "/.MumbleOverlayPipe"); } - ods("OpenGL Version %s, Vendor %s, Renderer %s, Shader %s", glGetString(GL_VERSION), glGetString(GL_VENDOR), glGetString(GL_RENDERER), glGetString(GL_SHADING_LANGUAGE_VERSION)); + ods("OpenGL Version %s, Vendor %s, Renderer %s, Shader %s", glGetString(GL_VERSION), glGetString(GL_VENDOR), + glGetString(GL_RENDERER), glGetString(GL_SHADING_LANGUAGE_VERSION)); const char *vsource = vshader; const char *fsource = fshader; @@ -197,7 +196,7 @@ static void newContext(Context * ctx) { static void releaseMem(Context *ctx) { if (ctx->a_ucTexture) { munmap(ctx->a_ucTexture, ctx->uiMappedLength); - ctx->a_ucTexture = NULL; + ctx->a_ucTexture = NULL; ctx->uiMappedLength = 0; } if (ctx->texture != ~0U) { @@ -219,9 +218,9 @@ static void disconnect(Context *ctx) { static bool sendMessage(Context *ctx, struct OverlayMsg *om) { if (ctx->iSocket != -1) { - size_t wantsend = sizeof(struct OverlayMsgHeader) + (size_t)om->omh.iLength; - ssize_t sent = send(ctx->iSocket, om, wantsend, MSG_DONTWAIT); - if (sent != -1 && wantsend == (size_t)sent) { + size_t wantsend = sizeof(struct OverlayMsgHeader) + (size_t) om->omh.iLength; + ssize_t sent = send(ctx->iSocket, om, wantsend, MSG_DONTWAIT); + if (sent != -1 && wantsend == (size_t) sent) { return true; } ods("Short write. Disconnecting pipe."); @@ -232,7 +231,7 @@ static bool sendMessage(Context *ctx, struct OverlayMsg *om) { static void regenTexture(Context *ctx) { if (ctx->texture != ~0U) { - glDeleteTextures(1, & ctx->texture); + glDeleteTextures(1, &ctx->texture); } glGenTextures(1, &ctx->texture); @@ -243,14 +242,15 @@ static void regenTexture(Context *ctx) { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)ctx->uiWidth, (GLsizei)ctx->uiHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE, ctx->a_ucTexture); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei) ctx->uiWidth, (GLsizei) ctx->uiHeight, 0, GL_BGRA, + GL_UNSIGNED_BYTE, ctx->a_ucTexture); } static void drawOverlay(Context *ctx, unsigned int width, unsigned int height) { // if no socket is active, initialize and connect to socket if (ctx->iSocket == -1) { releaseMem(ctx); - if (! ctx->saName.sun_path[0]) + if (!ctx->saName.sun_path[0]) return; ctx->iSocket = socket(AF_UNIX, SOCK_STREAM, 0); if (ctx->iSocket == -1) { @@ -258,7 +258,7 @@ static void drawOverlay(Context *ctx, unsigned int width, unsigned int height) { return; } fcntl(ctx->iSocket, F_SETFL, O_NONBLOCK, 1); - if (connect(ctx->iSocket, (struct sockaddr *)(& ctx->saName), sizeof(ctx->saName)) != 0) { + if (connect(ctx->iSocket, (struct sockaddr *) (&ctx->saName), sizeof(ctx->saName)) != 0) { close(ctx->iSocket); ctx->iSocket = -1; ods("connect() failure %s", ctx->saName.sun_path); @@ -268,9 +268,9 @@ static void drawOverlay(Context *ctx, unsigned int width, unsigned int height) { struct OverlayMsg om; om.omh.uiMagic = OVERLAY_MAGIC_NUMBER; - om.omh.uiType = OVERLAY_MSGTYPE_PID; + om.omh.uiType = OVERLAY_MSGTYPE_PID; om.omh.iLength = sizeof(struct OverlayMsgPid); - om.omp.pid = (unsigned int)getpid(); // getpid can't fail + om.omp.pid = (unsigned int) getpid(); // getpid can't fail if (!sendMessage(ctx, &om)) return; @@ -283,17 +283,17 @@ static void drawOverlay(Context *ctx, unsigned int width, unsigned int height) { ods("Sending init overlay msg with w h %i %i", width, height); releaseMem(ctx); - ctx->uiWidth = width; + ctx->uiWidth = width; ctx->uiHeight = height; struct OverlayMsg om; - om.omh.uiMagic = OVERLAY_MAGIC_NUMBER; - om.omh.uiType = OVERLAY_MSGTYPE_INIT; - om.omh.iLength = sizeof(struct OverlayMsgInit); - om.omi.uiWidth = ctx->uiWidth; + om.omh.uiMagic = OVERLAY_MAGIC_NUMBER; + om.omh.uiType = OVERLAY_MSGTYPE_INIT; + om.omh.iLength = sizeof(struct OverlayMsgInit); + om.omi.uiWidth = ctx->uiWidth; om.omi.uiHeight = ctx->uiHeight; - if (! sendMessage(ctx, &om)) + if (!sendMessage(ctx, &om)) return; } @@ -314,7 +314,7 @@ static void drawOverlay(Context *ctx, unsigned int width, unsigned int height) { } } else { // receive the overlay message body - ssize_t length = recv(ctx->iSocket, ctx->omMsg.msgbuffer, (size_t)ctx->omMsg.omh.iLength, 0); + ssize_t length = recv(ctx->iSocket, ctx->omMsg.msgbuffer, (size_t) ctx->omMsg.omh.iLength, 0); if (length < 0) { if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) break; @@ -331,100 +331,99 @@ static void drawOverlay(Context *ctx, unsigned int width, unsigned int height) { switch (ctx->omMsg.omh.uiType) { // shared memory overlay message: case OVERLAY_MSGTYPE_SHMEM: { - struct OverlayMsgShmem *oms = (struct OverlayMsgShmem *) & ctx->omMsg.omi; - ods("SHMEM %s", oms->a_cName); - releaseMem(ctx); - int fd = shm_open(oms->a_cName, O_RDONLY, 0600); - if (fd != -1) { - struct stat buf; - - if (fstat(fd, &buf) != -1) { - unsigned int buflen = buf.st_size; - if (buflen >= ctx->uiWidth * ctx->uiHeight * 4 - && buflen < 512 * 1024 * 1024) { - ctx->uiMappedLength = buflen; - ctx->a_ucTexture = mmap(NULL, (size_t)buflen, PROT_READ, MAP_SHARED, fd, 0); - if (ctx->a_ucTexture != MAP_FAILED) { - // mmap successfull; send a new bodyless sharedmemory overlay message and regenerate the overlay texture - struct OverlayMsg om; - om.omh.uiMagic = OVERLAY_MAGIC_NUMBER; - om.omh.uiType = OVERLAY_MSGTYPE_SHMEM; - om.omh.iLength = 0; - - if (! sendMessage(ctx, &om)) - return; - - regenTexture(ctx); - continue; - } - ctx->a_ucTexture = NULL; + struct OverlayMsgShmem *oms = (struct OverlayMsgShmem *) &ctx->omMsg.omi; + ods("SHMEM %s", oms->a_cName); + releaseMem(ctx); + int fd = shm_open(oms->a_cName, O_RDONLY, 0600); + if (fd != -1) { + struct stat buf; + + if (fstat(fd, &buf) != -1) { + unsigned int buflen = buf.st_size; + if (buflen >= ctx->uiWidth * ctx->uiHeight * 4 && buflen < 512 * 1024 * 1024) { + ctx->uiMappedLength = buflen; + ctx->a_ucTexture = mmap(NULL, (size_t) buflen, PROT_READ, MAP_SHARED, fd, 0); + if (ctx->a_ucTexture != MAP_FAILED) { + // mmap successfull; send a new bodyless sharedmemory overlay message and regenerate + // the overlay texture + struct OverlayMsg om; + om.omh.uiMagic = OVERLAY_MAGIC_NUMBER; + om.omh.uiType = OVERLAY_MSGTYPE_SHMEM; + om.omh.iLength = 0; + + if (!sendMessage(ctx, &om)) + return; + + regenTexture(ctx); + continue; } - ctx->uiMappedLength = 0; - } else { - ods("Failed to fstat memory map"); + ctx->a_ucTexture = NULL; } - close(fd); + ctx->uiMappedLength = 0; + } else { + ods("Failed to fstat memory map"); } - ods("Failed to map memory"); + close(fd); } - break; + ods("Failed to map memory"); + } break; // blit overlay message: blit overlay texture from shared memory to gl-texture var case OVERLAY_MSGTYPE_BLIT: { - struct OverlayMsgBlit *omb = & ctx->omMsg.omb; - ods("BLIT %d %d %d %d", omb->x, omb->y, omb->w, omb->h); - if ((ctx->a_ucTexture != NULL) && (ctx->texture != ~0U)) { - glBindTexture(GL_TEXTURE_2D, ctx->texture); - - if ((omb->x == 0) && (omb->y == 0) && (omb->w == ctx->uiWidth) && (omb->h == ctx->uiHeight)) { - ods("Optimzied fullscreen blit"); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)ctx->uiWidth, (GLsizei)ctx->uiHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE, ctx->a_ucTexture); - } else { - // allocate temporary memory - unsigned int x = omb->x; - unsigned int y = omb->y; - unsigned int w = omb->w; - unsigned int h = omb->h; - unsigned char *ptr = (unsigned char *) malloc(w*h*4); - unsigned int row; - memset(ptr, 0, w * h * 4); - - // copy overlay texture to temporary memory to adapt to full opengl ui size (overlay at correct place) - for (row = 0; row < h; ++row) { - const unsigned char *sptr = ctx->a_ucTexture + 4 * ((y+row) * ctx->uiWidth + x); - unsigned char *dptr = ptr + 4 * w * row; - memcpy(dptr, sptr, w * 4); - } - - // copy temporary texture to opengl - glTexSubImage2D(GL_TEXTURE_2D, 0, (GLint)x, (GLint)y, (GLint)w, (GLint)h, GL_BGRA, GL_UNSIGNED_BYTE, ptr); - free(ptr); + struct OverlayMsgBlit *omb = &ctx->omMsg.omb; + ods("BLIT %d %d %d %d", omb->x, omb->y, omb->w, omb->h); + if ((ctx->a_ucTexture != NULL) && (ctx->texture != ~0U)) { + glBindTexture(GL_TEXTURE_2D, ctx->texture); + + if ((omb->x == 0) && (omb->y == 0) && (omb->w == ctx->uiWidth) && (omb->h == ctx->uiHeight)) { + ods("Optimzied fullscreen blit"); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei) ctx->uiWidth, (GLsizei) ctx->uiHeight, 0, + GL_BGRA, GL_UNSIGNED_BYTE, ctx->a_ucTexture); + } else { + // allocate temporary memory + unsigned int x = omb->x; + unsigned int y = omb->y; + unsigned int w = omb->w; + unsigned int h = omb->h; + unsigned char *ptr = (unsigned char *) malloc(w * h * 4); + unsigned int row; + memset(ptr, 0, w * h * 4); + + // copy overlay texture to temporary memory to adapt to full opengl ui size (overlay at + // correct place) + for (row = 0; row < h; ++row) { + const unsigned char *sptr = ctx->a_ucTexture + 4 * ((y + row) * ctx->uiWidth + x); + unsigned char *dptr = ptr + 4 * w * row; + memcpy(dptr, sptr, w * 4); } + + // copy temporary texture to opengl + glTexSubImage2D(GL_TEXTURE_2D, 0, (GLint) x, (GLint) y, (GLint) w, (GLint) h, GL_BGRA, + GL_UNSIGNED_BYTE, ptr); + free(ptr); } } - break; + } break; case OVERLAY_MSGTYPE_ACTIVE: { - struct OverlayMsgActive *oma = & ctx->omMsg.oma; - ods("ACTIVE %d %d %d %d", oma->x, oma->y, oma->w, oma->h); - ctx->uiLeft = oma->x; - ctx->uiTop = oma->y; - ctx->uiRight = oma->x + oma->w; - ctx->uiBottom = oma->y + oma->h; - } - break; + struct OverlayMsgActive *oma = &ctx->omMsg.oma; + ods("ACTIVE %d %d %d %d", oma->x, oma->y, oma->w, oma->h); + ctx->uiLeft = oma->x; + ctx->uiTop = oma->y; + ctx->uiRight = oma->x + oma->w; + ctx->uiBottom = oma->y + oma->h; + } break; case OVERLAY_MSGTYPE_INTERACTIVE: { #if defined(TARGET_MAC) - struct OverlayMsgInteractive *omin = & ctx->omMsg.omin; - ods("Interactive %d", omin->state); - if (bCursorAvail) { - if (omin->state) { - oCGDisplayHideCursor(kCGNullDirectDisplay); - } else { - oCGDisplayShowCursor(kCGNullDirectDisplay); - } + struct OverlayMsgInteractive *omin = &ctx->omMsg.omin; + ods("Interactive %d", omin->state); + if (bCursorAvail) { + if (omin->state) { + oCGDisplayHideCursor(kCGNullDirectDisplay); + } else { + oCGDisplayShowCursor(kCGNullDirectDisplay); } -#endif } - break; +#endif + } break; default: break; } @@ -435,7 +434,7 @@ static void drawOverlay(Context *ctx, unsigned int width, unsigned int height) { return; // texture checks, that our gltexture is still valid and sane - if (! glIsTexture(ctx->texture)) { + if (!glIsTexture(ctx->texture)) { ctx->texture = ~0U; ods("Lost texture"); regenTexture(ctx); @@ -443,10 +442,8 @@ static void drawOverlay(Context *ctx, unsigned int width, unsigned int height) { glBindTexture(GL_TEXTURE_2D, ctx->texture); GLfloat bordercolor[4]; glGetTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, bordercolor); - if (bordercolor[0] != fBorder[0] - || bordercolor[1] != fBorder[1] - || bordercolor[2] != fBorder[2] - || bordercolor[3] != fBorder[3]) { + if (bordercolor[0] != fBorder[0] || bordercolor[1] != fBorder[1] || bordercolor[2] != fBorder[2] + || bordercolor[3] != fBorder[3]) { ods("Texture was hijacked! Texture will be regenerated."); regenTexture(ctx); } @@ -455,39 +452,27 @@ static void drawOverlay(Context *ctx, unsigned int width, unsigned int height) { glBindTexture(GL_TEXTURE_2D, ctx->texture); glPushMatrix(); - float w = (float)(ctx->uiWidth); - float h = (float)(ctx->uiHeight); + float w = (float) (ctx->uiWidth); + float h = (float) (ctx->uiHeight); - float left = (float)(ctx->uiLeft); - float top = (float)(ctx->uiTop); - float right = (float)(ctx->uiRight); - float bottom = (float)(ctx->uiBottom); + float left = (float) (ctx->uiLeft); + float top = (float) (ctx->uiTop); + float right = (float) (ctx->uiRight); + float bottom = (float) (ctx->uiBottom); - float xm = left / w; - float ym = top / h; - float xmx = right / w; + float xm = left / w; + float ym = top / h; + float xmx = right / w; float ymx = bottom / h; - GLfloat vertex[] = { - left, bottom, - left, top, - right, top, + GLfloat vertex[] = { left, bottom, left, top, right, top, - left, bottom, - right, top, - right, bottom - }; + left, bottom, right, top, right, bottom }; glVertexPointer(2, GL_FLOAT, 0, vertex); - GLfloat tex[] = { - xm, ymx, - xm, ym, - xmx, ym, + GLfloat tex[] = { xm, ymx, xm, ym, xmx, ym, - xm, ymx, - xmx, ym, - xmx, ymx - }; + xm, ymx, xmx, ym, xmx, ymx }; glTexCoordPointer(2, GL_FLOAT, 0, tex); glDrawArrays(GL_TRIANGLES, 0, 6); @@ -495,22 +480,22 @@ static void drawOverlay(Context *ctx, unsigned int width, unsigned int height) { glPopMatrix(); } -static void drawContext(Context * ctx, int width, int height) { +static void drawContext(Context *ctx, int width, int height) { // calculate FPS and send it as an overlay message - clock_t t = clock(); - float elapsed = (float)(t - ctx->timeT) / CLOCKS_PER_SEC; + clock_t t = clock(); + float elapsed = (float) (t - ctx->timeT) / CLOCKS_PER_SEC; ++(ctx->frameCount); if (elapsed > OVERLAY_FPS_INTERVAL) { struct OverlayMsg om; om.omh.uiMagic = OVERLAY_MAGIC_NUMBER; - om.omh.uiType = OVERLAY_MSGTYPE_FPS; + om.omh.uiType = OVERLAY_MSGTYPE_FPS; om.omh.iLength = sizeof(struct OverlayMsgFps); - om.omf.fps = (float)ctx->frameCount / elapsed; + om.omf.fps = (float) ctx->frameCount / elapsed; sendMessage(ctx, &om); ctx->frameCount = 0; - ctx->timeT = t; + ctx->timeT = t; } GLuint program; @@ -520,7 +505,7 @@ static void drawContext(Context * ctx, int width, int height) { glPushAttrib(GL_ALL_ATTRIB_BITS); glPushClientAttrib(GL_ALL_ATTRIB_BITS); glGetIntegerv(GL_VIEWPORT, viewport); - glGetIntegerv(GL_CURRENT_PROGRAM, (GLint *)&program); + glGetIntegerv(GL_CURRENT_PROGRAM, (GLint *) &program); glViewport(0, 0, width, height); @@ -596,8 +581,8 @@ static void drawContext(Context * ctx, int width, int height) { glGetIntegerv(GL_MAX_TEXTURE_UNITS, &texunits); - for (i=texunits-1;i>=0;--i) { - glActiveTexture(GL_TEXTURE0 + (GLenum)i); + for (i = texunits - 1; i >= 0; --i) { + glActiveTexture(GL_TEXTURE0 + (GLenum) i); glDisable(GL_TEXTURE_1D); glDisable(GL_TEXTURE_2D); glDisable(GL_TEXTURE_3D); @@ -608,11 +593,11 @@ static void drawContext(Context * ctx, int width, int height) { glDisable(GL_FRAGMENT_PROGRAM_ARB); GLint enabled; - for (i=0;i<ctx->maxVertexAttribs;++i) { + for (i = 0; i < ctx->maxVertexAttribs; ++i) { enabled = GL_FALSE; - glGetVertexAttribiv((GLuint)i, GL_VERTEX_ATTRIB_ARRAY_ENABLED, &enabled); + glGetVertexAttribiv((GLuint) i, GL_VERTEX_ATTRIB_ARRAY_ENABLED, &enabled); if (enabled == GL_TRUE) { - glDisableVertexAttribArray((GLuint)i); + glDisableVertexAttribArray((GLuint) i); ctx->vertexAttribStates[i] = GL_TRUE; } } @@ -635,8 +620,8 @@ static void drawContext(Context * ctx, int width, int height) { glEnableClientState(GL_TEXTURE_COORD_ARRAY); GLuint bound = 0, vbobound = 0; - glGetIntegerv(GL_PIXEL_UNPACK_BUFFER_BINDING, (GLint *)&bound); - glGetIntegerv(GL_ARRAY_BUFFER_BINDING, (GLint *)&vbobound); + glGetIntegerv(GL_PIXEL_UNPACK_BUFFER_BINDING, (GLint *) &bound); + glGetIntegerv(GL_ARRAY_BUFFER_BINDING, (GLint *) &vbobound); if (bound != 0) { glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0); @@ -645,7 +630,7 @@ static void drawContext(Context * ctx, int width, int height) { glBindBuffer(GL_ARRAY_BUFFER, 0); } - drawOverlay(ctx, (unsigned int)width, (unsigned int)height); + drawOverlay(ctx, (unsigned int) width, (unsigned int) height); if (bound != 0) { glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, bound); @@ -654,9 +639,9 @@ static void drawContext(Context * ctx, int width, int height) { glBindBuffer(GL_ARRAY_BUFFER, vbobound); } - for (i=0;i<ctx->maxVertexAttribs;++i) { + for (i = 0; i < ctx->maxVertexAttribs; ++i) { if (ctx->vertexAttribStates[i] == GL_TRUE) { - glEnableVertexAttribArray((GLuint)i); + glEnableVertexAttribArray((GLuint) i); ctx->vertexAttribStates[i] = GL_FALSE; } } @@ -676,11 +661,12 @@ static void drawContext(Context * ctx, int width, int height) { glUseProgram(program); // drain opengl error queue - while (glGetError() != GL_NO_ERROR); + while (glGetError() != GL_NO_ERROR) + ; } #if defined(TARGET_UNIX) -# include "init_unix.c" +# include "init_unix.c" #elif defined(TARGET_MAC) -# include "init_mac.c" +# include "init_mac.c" #endif |