summaryrefslogtreecommitdiff
path: root/src/screen.c
blob: 5e840380ae3c53b9b82040b0ca78caf261bf2e1b (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#include "screen.h"
#undef Screen

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <pthread.h>
#include <unistd.h>
#include <signal.h>

#define XLIB_ILLEGAL_ACCESS
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>

static Display* dpy;
static int scr;
static Window root;

static int count = 0;
static size_t bit_depth = sizeof(uint32_t) * 8;
static int key_count;

static bool* key_state;
static bool* key_checked;

typedef struct {
    Window window;
    GC graphics_ctx;
    Pixmap pixel_map;
    XImage** images;
    pthread_t update_thread;
    pthread_mutex_t recreate_lock;
} WindowState;

static void init_x() {
    if ((dpy = XOpenDisplay(NULL)) == NULL) {
        printf("error: failed to open x11 display\n");
        exit(EXIT_FAILURE);
    }

    scr = DefaultScreen(dpy);
    root = RootWindow(dpy, scr);

    key_count = dpy->max_keycode - dpy->min_keycode;
    key_state = malloc(sizeof(bool) * key_count);
    key_checked = malloc(sizeof(bool) * key_count);

    memset(key_state, 0, sizeof(bool) * key_count);
    memset(key_checked, 0, sizeof(bool) * key_count);
}

static Window create_window(int x, int y, int w, int h, int d, const char* t) {
    
    XVisualInfo vis_info;
    if(!XMatchVisualInfo(dpy, scr, d, TrueColor, &vis_info)) {
        printf("error: %d depth not supported\n", d);
        exit(EXIT_FAILURE);    
    }

    Visual* visual = vis_info.visual;
    
    XSetWindowAttributes xwa;
    xwa.background_pixel = 0;
    xwa.border_pixel = 0;
    xwa.event_mask = KeyPressMask | KeyReleaseMask |  StructureNotifyMask;
    xwa.colormap = XCreateColormap(dpy, root, visual, AllocNone);

    long wm = CWBackPixel | CWColormap | CWBorderPixel | CWEventMask;

    Window window = XCreateWindow(dpy, root, x, y, w, h, 0, d, InputOutput, visual, wm, &xwa);
    XStoreName(dpy, window, t);
    XSetIconName(dpy, window, t);

    return window;
}

static XImage* create_image(int width, int height, void* data) {
    return XCreateImage(
        dpy, 
        CopyFromParent, 
        bit_depth, 
        ZPixmap, 
        0, 
        data,
        width, 
        height, 
        bit_depth, 
        0
    );
}

static Swapchain create_swapchain(int width, int height, int frames, int bit_depth, WindowState* state) {
    Swapchain swapchain;
    swapchain.width = width;
    swapchain.height = height;
    swapchain.image_count = frames;
    swapchain.image_front = 0;
    swapchain.image_recent = 0;
    swapchain.image_current = 0;
    swapchain.images = malloc(sizeof(uint32_t*) * frames);
    
    state->images = malloc(sizeof(XImage*) * frames);
    state->pixel_map = XCreatePixmap(dpy, state->window, width, height, bit_depth);
    state->graphics_ctx = XCreateGC(dpy, state->pixel_map, 0, NULL);
    
    size_t pixel_count = width * height;
    for (int i = 0; i < frames; i++) {
        uint32_t* data = malloc(pixel_count * bit_depth / 8);
        memset(data, 0, pixel_count * bit_depth / 8);

        swapchain.images[i] = data;
        state->images[i] = create_image(width, height, data);

    }

    pthread_mutex_init(&state->recreate_lock, NULL);
    pthread_mutex_init(&swapchain.lock, NULL);

    return swapchain;
}

static void free_swapchain(Swapchain* swapchain, WindowState* state) {
    pthread_mutex_lock(&state->recreate_lock);
    for (uint8_t i = 0; i < swapchain->image_count; i++) {
        XDestroyImage(state->images[i]); // also frees pixel buffer :3
    }
    
    free(swapchain->images);
    free(state->images);
    swapchain->images = NULL;
    
    XFreePixmap(dpy, state->pixel_map);
    XFreeGC(dpy, state->graphics_ctx);

    pthread_mutex_unlock(&state->recreate_lock);
}

static void recreate_swapchain(int width, int height, int bit_depth, Swapchain* swapchain, WindowState* state) {
    pthread_mutex_lock(&state->recreate_lock);

    swapchain->width = width;
    swapchain->height = height;
    
    XFreePixmap(dpy, state->pixel_map);
    XFreeGC(dpy, state->graphics_ctx);

    state->pixel_map = XCreatePixmap(dpy, state->window, width, height, bit_depth);
    state->graphics_ctx = XCreateGC(dpy, state->pixel_map, 0, NULL);
   
    size_t pixel_count = width * height;
    for (uint8_t i = 0; i < swapchain->image_count; i++) {
        XDestroyImage(state->images[i]); // also frees pixel buffer :3
        
        uint32_t* data = malloc(pixel_count * bit_depth / 8);
        memset(data, 0, pixel_count * bit_depth / 8);

        swapchain->images[i] = data;
        state->images[i] = create_image(width, height, data);
    }

    pthread_mutex_unlock(&state->recreate_lock);
}

void swapchain_next(Swapchain* swapchain) {
    pthread_mutex_lock(&swapchain->lock);
    uint8_t next = swapchain->image_current + 1;
    next %= swapchain->image_count;

    if (next == swapchain->image_front) {
        next += 1;
        next %= 3;
    }

    swapchain->image_current = next;
    pthread_mutex_unlock(&swapchain->lock);
}

void swapchain_submit(Swapchain* swapchain) {
    pthread_mutex_lock(&swapchain->lock);
    swapchain->image_recent = swapchain->image_current;
    pthread_mutex_unlock(&swapchain->lock);
}

static void* swapchain_thread(void* arg) {
    struct Screen* screen = (struct Screen*) arg;
    WindowState* state = (WindowState*) screen->internal;

    while (1) {

        if (screen->swapchain.images == NULL) {
            return NULL;
        }
        
        pthread_mutex_lock(&state->recreate_lock);

        pthread_mutex_lock(&screen->swapchain.lock);
        int index = screen->swapchain.image_recent;
        screen->swapchain.image_front = index;
        pthread_mutex_unlock(&screen->swapchain.lock);
        
        XPutImage(
            dpy,
            state->window,
            state->graphics_ctx,
            state->images[index],
            0, 0, 0, 0,
            screen->swapchain.width,
            screen->swapchain.height
        );
        XSync(dpy, 0);
        
        pthread_mutex_unlock(&state->recreate_lock);

        usleep(1000 * 1000 / 60);
    }

    return NULL;
}

#define FRAMES 3

void init_screen(struct Screen* screen, uint16_t width, uint16_t height, const char* title) {

    if (dpy == NULL) {
        init_x();
    }
    
    Window window = create_window(0, 0, width, height, bit_depth, title);
    XMapWindow(dpy, window);

    WindowState* state = malloc(sizeof(WindowState));
    state->window = window;

    screen->swapchain = create_swapchain(width, height, FRAMES, bit_depth, state);
    pthread_create(&state->update_thread, NULL, &swapchain_thread, screen);

    screen->delta = 0;
    screen->internal = state;
    count++;
}

static long get_time() {
    struct timespec spec;
    clock_gettime(CLOCK_MONOTONIC, &spec);
    return round(spec.tv_nsec / 1.0e6) + spec.tv_sec * 1000;
}

static long last = 0;

static void update_delta(struct Screen* screen) {
    long now = get_time();
    if (last == 0) {
        screen->delta = 0;
    } else {
        screen->delta = (now - last) / 1000.0;
    }
    last = now;
}

static void handle_event(struct Screen* screen) {
    XEvent event;
    XNextEvent(dpy, &event);
    switch(event.type) {
        case KeyPress: {
            XKeyEvent xkpe = event.xkey;
            uint32_t code = xkpe.keycode - dpy->min_keycode;
            key_state[code] = true;
            key_checked[code] = false;
            break;
        }

        case KeyRelease: {
            XKeyEvent xkre = event.xkey;
            uint32_t code = xkre.keycode - dpy->min_keycode;
            key_state[code] = false;
            break;
        }

        case ConfigureNotify: {
            XConfigureEvent xce = event.xconfigure;
            if (xce.width != screen->swapchain.width || xce.height != screen->swapchain.height)
                recreate_swapchain(xce.width, xce.height, bit_depth, &screen->swapchain, (WindowState*) screen->internal);
            break;
        }

        default: break;
    }
}

bool poll_screen(struct Screen* screen) {
    
    while (XPending(dpy))
        handle_event(screen);

    update_delta(screen);
    return true;
}

void free_screen(struct Screen* screen) {
    WindowState* state = (WindowState*) screen->internal;

    free_swapchain(&screen->swapchain, state);
    pthread_exit(&state->update_thread);
    pthread_mutex_destroy(&screen->swapchain.lock);
    pthread_mutex_destroy(&state->recreate_lock);

    XUnmapWindow(dpy, state->window);
    XDestroyWindow(dpy, state->window);
    free(screen->internal);

    count--;

    if(count == 0) {
        XCloseDisplay(dpy);
        free(key_state);
        free(key_checked);
    }
}

bool key_pressed(int keycode) {
    if (key_checked[keycode]) return false;
    if (!key_state[keycode]) return false;
    key_checked[keycode] = true;
    return true;
}

bool key_down(int keycode) {
    return key_state[keycode];
}