aboutsummaryrefslogtreecommitdiff
path: root/src/window/window.c
blob: 2e9a1baa7c8e4f53614da3e6751d2af2d2830ce9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "../../include/mgl/window/window.h"
#include "../../include/mgl/mgl.h"
#include <X11/Xutil.h>
#include <errno.h>
#include <stdio.h>

/* 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) {
    mgl_context *context = mgl_get_context();
    self->size.x = width;
    self->size.y = height;
    context->gl.glViewport(0, 0, self->size.x, self->size.y);
    context->gl.glMatrixMode(GL_PROJECTION);
    context->gl.glLoadIdentity();
    context->gl.glOrtho(0.0, width, height, 0.0, -1.0, 1.0);
}

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);

    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);
            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: Switch current when rendering to another window, and set current to NULL when destroying the currently selected context */
    context->gl.glXMakeCurrent(context->connection, self->window, context->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_COLOR_ARRAY);
    context->gl.glEnableClientState(GL_TEXTURE_COORD_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->window) {
        XDestroyWindow(context->connection, self->window);
        self->window = 0;
    }
}

static void mgl_window_on_receive_event(mgl_window *self, XEvent *xev) {
    switch(xev->type) {
        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);
                /*fprintf(stderr, "resize!\n");*/
            }
            break;
        }
        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;
            break;
        }
    }
}

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) {
    /* TODO: Use |event| */

    Display *display = mgl_get_context()->connection;
    if(XPending(display)) {
        XEvent xev; /* TODO: Move to window struct */
        XNextEvent(display, &xev);
        mgl_window_on_receive_event(self, &xev);
        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);
}