From dd935f7031c719c8be00f376faea67e3678fe1f7 Mon Sep 17 00:00:00 2001 From: Jarcode Date: Sun, 7 Jan 2018 19:43:00 -0800 Subject: [PATCH] Fixed shm resource issue --- Makefile | 2 +- render.c | 6 +++--- xwin.c | 18 +++++++++++++----- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index 35907dc..e843f77 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ obj = $(src:.c=.o) # Build type parameter ifeq ($(BUILD),debug) - CFLAGS_BUILD = -ggdb + CFLAGS_BUILD = -ggdb -Wall GLAD_GEN = c-debug else CFLAGS_BUILD = -O2 -march=native diff --git a/render.c b/render.c index 3b19cf7..94dc3d3 100644 --- a/render.c +++ b/render.c @@ -986,7 +986,7 @@ struct renderer* rd_new(const char** paths, const char* entry, const char* force dir = opendir(shaders); while ((d = readdir(dir)) != NULL) { if (d->d_type == DT_REG || d->d_type == DT_UNKNOWN) { - snprintf(buf, sizeof(buf), "%d." SHADER_EXT_FRAG, idx); + snprintf(buf, sizeof(buf), "%d." SHADER_EXT_FRAG, (int) idx); if (!strcmp(buf, d->d_name)) { printf("found GLSL stage: '%s'\n", d->d_name); ++count; @@ -1007,7 +1007,7 @@ struct renderer* rd_new(const char** paths, const char* entry, const char* force dir = opendir(shaders); while ((d = readdir(dir)) != NULL) { if (d->d_type == DT_REG || d->d_type == DT_UNKNOWN) { - snprintf(buf, sizeof(buf), "%d." SHADER_EXT_FRAG, idx); + snprintf(buf, sizeof(buf), "%d." SHADER_EXT_FRAG, (int) idx); if (!strcmp(buf, d->d_name)) { printf("compiling: '%s'\n", d->d_name); @@ -1218,7 +1218,7 @@ void rd_update(struct renderer* r, float* lb, float* rb, size_t bsz, bool modifi " fragment = texture(tex, vec2(gl_FragCoord.x / screen.x, " "\n" " (screen.y - gl_FragCoord.y) / screen.y));" "\n" " fragment.a = 1.0F;" "\n" - "}"; "\n"; + "}" "\n"; if (!setup) { bg_prog = shaderlink(shaderload(NULL, GL_VERTEX_SHADER, VERTEX_SHADER_SRC, NULL, NULL, 330, true), diff --git a/xwin.c b/xwin.c index a839c2f..6ed1d9e 100644 --- a/xwin.c +++ b/xwin.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -34,7 +35,7 @@ bool xwin_should_render(void) { unsigned long nitems, bytes_after; unsigned char* data; - int handler(Display* d, XErrorEvent* e) {} + int handler(Display* d, XErrorEvent* e) { return 0; } XSetErrorHandler(handler); /* dummy error handler */ @@ -130,7 +131,10 @@ unsigned int xwin_copyglbg(struct renderer* rd, unsigned int tex) { XVisualInfo* info = XGetVisualInfo(d, VisualIDMask, &match, &nret); XImage* image = XShmCreateImage(d, visual, info->depth, ZPixmap, NULL, &shminfo, (unsigned int) w, (unsigned int) h); - shminfo.shmid = shmget(IPC_PRIVATE, image->bytes_per_line * image->height, IPC_CREAT | 0777); + if ((shminfo.shmid = shmget(IPC_PRIVATE, image->bytes_per_line * image->height, IPC_CREAT | 0777)) == -1) { + fprintf(stderr, "shmget() failed: %s\n", strerror(errno)); + exit(EXIT_FAILURE); + } shminfo.shmaddr = image->data = shmat(shminfo.shmid, 0, 0); shminfo.readOnly = false; XShmAttach(d, &shminfo); @@ -200,18 +204,22 @@ unsigned int xwin_copyglbg(struct renderer* rd, unsigned int tex) { } else { /* Use image data directly. The alpha value is garbage/unassigned data, but we need to read it because X11 keeps pixel data aligned */ - buf = image->data; + buf = (uint8_t*) image->data; /* Data could be 2, 4, or 8 byte aligned, the RGBA format and type (depth) already ensures reads will be properly aligned across scanlines */ glPixelStorei(GL_UNPACK_ALIGNMENT, 1); GLenum format = image->bitmap_bit_order == LSBFirst ? (!aligned ? GL_BGRA : GL_BGR) : (!aligned ? GL_RGBA : GL_RGB); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, format, GL_UNSIGNED_BYTE, buf); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, format, type, buf); glPixelStorei(GL_UNPACK_ALIGNMENT, 4); /* restore default */ } - XFree(image); + XShmDetach(d, &shminfo); + shmdt(shminfo.shmaddr); + shmctl(shminfo.shmid, IPC_RMID, NULL); + + XDestroyImage(image); XCloseDisplay(d); return texture;