#include "../../include/mgl/window/window.h" #include "../../include/mgl/window/event.h" #include "../../include/mgl/mgl.h" #include #include #include /* TODO: check for glx swap control extension string (GLX_EXT_swap_control, etc) */ static void set_vertical_sync_enabled(Window window, int enabled) { int result = 0; mgl_context *context = mgl_get_context(); if(context->gl.glXSwapIntervalEXT) { context->gl.glXSwapIntervalEXT(context->connection, window, enabled ? 1 : 0); } else if(context->gl.glXSwapIntervalMESA) { result = context->gl.glXSwapIntervalMESA(enabled ? 1 : 0); } else if(context->gl.glXSwapIntervalSGI) { result = context->gl.glXSwapIntervalSGI(enabled ? 1 : 0); } else { static int warned = 0; if (!warned) { warned = 1; fprintf(stderr, "Warning: setting vertical sync not supported\n"); } } if(result != 0) fprintf(stderr, "Warning: setting vertical sync failed\n"); } static void mgl_window_on_resize(mgl_window *self, int width, int height) { self->size.x = width; self->size.y = height; mgl_view view; view.position = (mgl_vec2i){ 0, 0 }; view.size = self->size; mgl_window_set_view(self, &view); } int mgl_window_create(mgl_window *self, const char *title, int width, int height) { return mgl_window_create_with_params(self, title, width, height, 0); } static int mgl_window_init(mgl_window *self, const char *title, int width, int height, mgl_window_handle parent_window, Window existing_window) { self->window = 0; mgl_context *context = mgl_get_context(); if(parent_window == 0) parent_window = DefaultRootWindow(context->connection); self->glx_context = context->gl.glXCreateContext(context->connection, context->visual_info, NULL, 1); if(!self->glx_context) { fprintf(stderr, "glXCreateContext failed\n"); return -1; } Colormap color_map = XCreateColormap(context->connection, DefaultRootWindow(context->connection), ((XVisualInfo*)context->visual_info)->visual, AllocNone); if(!color_map) { fprintf(stderr, "XCreateColormap failed\n"); return -1; } XSetWindowAttributes window_attr; window_attr.colormap = color_map; window_attr.event_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | Button1MotionMask | Button2MotionMask | Button3MotionMask | Button4MotionMask | Button5MotionMask | ButtonMotionMask | StructureNotifyMask; window_attr.bit_gravity = NorthWestGravity; if(existing_window) { if(!XChangeWindowAttributes(context->connection, existing_window, CWColormap | CWEventMask | CWBitGravity, &window_attr)) { fprintf(stderr, "XChangeWindowAttributes failed\n"); XFreeColormap(context->connection, color_map); mgl_window_deinit(self); return -1; } XFreeColormap(context->connection, color_map); self->window = existing_window; } else { self->window = XCreateWindow(context->connection, parent_window, 0, 0, width, height, 0, ((XVisualInfo*)context->visual_info)->depth, InputOutput, ((XVisualInfo*)context->visual_info)->visual, CWColormap | CWEventMask | CWBitGravity, &window_attr); XFreeColormap(context->connection, color_map); if(!self->window) { fprintf(stderr, "XCreateWindow failed\n"); mgl_window_deinit(self); return -1; } XStoreName(context->connection, self->window, title); XMapWindow(context->connection, self->window); } XFlush(context->connection); /* TODO: Check for failure? */ context->gl.glXMakeCurrent(context->connection, self->window, self->glx_context); set_vertical_sync_enabled(self->window, 1); context->gl.glEnable(GL_TEXTURE_2D); context->gl.glEnable(GL_BLEND); context->gl.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); context->gl.glEnableClientState(GL_VERTEX_ARRAY); context->gl.glEnableClientState(GL_TEXTURE_COORD_ARRAY); context->gl.glEnableClientState(GL_COLOR_ARRAY); XWindowAttributes gwa; gwa.width = 0; gwa.height = 0; XGetWindowAttributes(context->connection, self->window, &gwa); Window dummy_w; int dummy_i; unsigned int dummy_u; XQueryPointer(context->connection, self->window, &dummy_w, &dummy_w, &dummy_i, &dummy_i, &self->cursor_position.x, &self->cursor_position.y, &dummy_u); mgl_window_on_resize(self, gwa.width, gwa.height); return 0; } int mgl_window_create_with_params(mgl_window *self, const char *title, int width, int height, mgl_window_handle parent_window) { return mgl_window_init(self, title, width, height, parent_window, None); } int mgl_window_init_from_existing_window(mgl_window *self, mgl_window_handle existing_window) { return mgl_window_init(self, "", 0, 0, 0, existing_window); } void mgl_window_deinit(mgl_window *self) { mgl_context *context = mgl_get_context(); if(self->glx_context) { context->gl.glXDestroyContext(context->connection, self->glx_context); self->glx_context = NULL; } if(self->window) { XDestroyWindow(context->connection, self->window); self->window = 0; } } static mgl_key x11_keysym_to_mgl_key(KeySym key_sym) { if(key_sym >= XK_A && key_sym <= XK_Z) return MGL_KEY_A + (key_sym - XK_A); /* TODO: Check if this ever happens */ if(key_sym >= XK_a && key_sym <= XK_z) return MGL_KEY_A + (key_sym - XK_a); if(key_sym >= XK_0 && key_sym <= XK_9) return MGL_KEY_NUM0 + (key_sym - XK_0); if(key_sym >= XK_KP_0 && key_sym <= XK_KP_9) return MGL_KEY_NUMPAD0 + (key_sym - XK_KP_0); /* TODO: Fill in the rest */ switch(key_sym) { case XK_space: return MGL_KEY_SPACE; case XK_BackSpace: return MGL_KEY_BACKSPACE; case XK_Tab: return MGL_KEY_TAB; case XK_Return: return MGL_KEY_ENTER; case XK_Escape: return MGL_KEY_ESCAPE; case XK_Delete: return MGL_KEY_DELETE; case XK_Home: return MGL_KEY_HOME; case XK_Left: return MGL_KEY_LEFT; case XK_Up: return MGL_KEY_UP; case XK_Right: return MGL_KEY_RIGHT; case XK_Down: return MGL_KEY_DOWN; case XK_Page_Up: return MGL_KEY_PAGEUP; case XK_Page_Down: return MGL_KEY_PAGEDOWN; case XK_End: return MGL_KEY_END; case XK_F1: return MGL_KEY_F1; case XK_F2: return MGL_KEY_F2; case XK_F3: return MGL_KEY_F3; case XK_F4: return MGL_KEY_F4; case XK_F5: return MGL_KEY_F5; case XK_F6: return MGL_KEY_F6; case XK_F7: return MGL_KEY_F7; case XK_F8: return MGL_KEY_F8; case XK_F9: return MGL_KEY_F9; case XK_F10: return MGL_KEY_F10; case XK_F11: return MGL_KEY_F11; case XK_F12: return MGL_KEY_F12; case XK_F13: return MGL_KEY_F13; case XK_F14: return MGL_KEY_F14; case XK_F15: return MGL_KEY_F15; } return MGL_KEY_UNKNOWN; } static mgl_mouse_button x11_button_to_mgl_button(unsigned int button) { switch(button) { case 1: return MGL_BUTTON_LEFT; case 2: return MGL_BUTTON_MIDDLE; case 3: return MGL_BUTTON_RIGHT; case 8: return MGL_BUTTON_XBUTTON1; case 9: return MGL_BUTTON_XBUTTON2; } return MGL_BUTTON_UNKNOWN; } /* Returns true if processed */ static bool mgl_window_on_receive_event(mgl_window *self, XEvent *xev, mgl_event *event, mgl_context *context) { /* TODO: Handle wm_delete_window event */ switch(xev->type) { case KeyPress: { /* TODO: Fill with correct data */ event->type = MGL_EVENT_KEY_PRESSED; event->key.code = x11_keysym_to_mgl_key(XKeycodeToKeysym(context->connection, xev->xkey.keycode, 0)); event->key.alt = ((xev->xkey.state & Mod1Mask) != 0); event->key.control = ((xev->xkey.state & ControlMask) != 0); event->key.shift = ((xev->xkey.state & ShiftMask) != 0); event->key.system = ((xev->xkey.state & Mod5Mask) != 0); /* TODO: Fix, doesn't work */ return true; } case KeyRelease: { /* TODO: Fill with correct data */ event->type = MGL_EVENT_KEY_RELEASED; event->key.code = x11_keysym_to_mgl_key(XKeycodeToKeysym(context->connection, xev->xkey.keycode, 0)); event->key.alt = ((xev->xkey.state & Mod1Mask) != 0); event->key.control = ((xev->xkey.state & ControlMask) != 0); event->key.shift = ((xev->xkey.state & ShiftMask) != 0); event->key.system = ((xev->xkey.state & Mod5Mask) != 0); /* TODO: Fix, doesn't work */ return true; } case ButtonPress: { event->type = MGL_EVENT_MOUSE_BUTTON_PRESSED; event->mouse_button.button = x11_button_to_mgl_button(xev->xbutton.button); event->mouse_button.x = xev->xbutton.x; event->mouse_button.y = xev->xbutton.y; return true; } case ButtonRelease: { event->type = MGL_EVENT_MOUSE_BUTTON_RELEASED; event->mouse_button.button = x11_button_to_mgl_button(xev->xbutton.button); event->mouse_button.x = xev->xbutton.x; event->mouse_button.y = xev->xbutton.y; return true; } case ConfigureNotify: { while(XCheckTypedWindowEvent(mgl_get_context()->connection, self->window, ConfigureNotify, xev)) {} if(xev->xconfigure.width != self->size.x || xev->xconfigure.height != self->size.x) { mgl_window_on_resize(self, xev->xconfigure.width, xev->xconfigure.height); event->type = MGL_EVENT_RESIZED; event->size.width = self->size.x; event->size.height = self->size.y; return true; } return false; } case MotionNotify: { while(XCheckTypedWindowEvent(mgl_get_context()->connection, self->window, MotionNotify, xev)) {} self->cursor_position.x = xev->xmotion.x; self->cursor_position.y = xev->xmotion.y; event->type = MGL_EVENT_MOUSE_MOVED; event->mouse_move.x = self->cursor_position.x; event->mouse_move.y = self->cursor_position.y; return true; } } /*fprintf(stderr, "unhandled event type: %d\n", xev->type);*/ event->type = MGL_EVENT_UNKNOWN; return false; } void mgl_window_clear(mgl_window *self, mgl_color color) { mgl_context *context = mgl_get_context(); context->gl.glClear(GL_COLOR_BUFFER_BIT); context->gl.glClearColor((float)color.r / 255.0f, (float)color.g / 255.0f, (float)color.b / 255.0f, (float)color.a / 255.0f); } bool mgl_window_poll_event(mgl_window *self, mgl_event *event) { mgl_context *context = mgl_get_context(); Display *display = context->connection; if(XPending(display)) { XEvent xev; /* TODO: Move to window struct */ XNextEvent(display, &xev); mgl_window_on_receive_event(self, &xev, event, context); return true; } else { return false; } } void mgl_window_display(mgl_window *self) { mgl_context *context = mgl_get_context(); context->gl.glXSwapBuffers(context->connection, self->window); } void mgl_window_set_view(mgl_window *self, mgl_view *new_view) { mgl_context *context = mgl_get_context(); self->view = *new_view; context->gl.glViewport(new_view->position.x, self->size.y - new_view->size.y - new_view->position.y, new_view->size.x, new_view->size.y); context->gl.glMatrixMode(GL_PROJECTION); context->gl.glLoadIdentity(); context->gl.glOrtho(0.0, new_view->size.x, new_view->size.y, 0.0, 0.0, 1.0); } void mgl_window_get_view(mgl_window *self, mgl_view *view) { *view = self->view; }