summaryrefslogtreecommitdiff
path: root/src/screen.c
blob: dca9b797d05c4dc01158063512622f4538b65c46 (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
#include "screen.h"
#include <X11/X.h>
#undef Screen

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/select.h>

#define XLIB_ILLEGAL_ACCESS
#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* image;
    int width, height;
} WindowState;

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

#ifdef DEBUG
    XSynchronize(dpy, True);
#endif

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

static Window create_window(int x, int y, int w, int h, int d) {
    
    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);

    return window;
}

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


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

    if (dpy == NULL) {
        init_x();
    }

    screen->width = width;
    screen->height = height;
    screen->delta = 0;
    
    size_t pixel_count = screen->width * screen->height;

    screen->pixels = malloc(pixel_count * bit_depth / 8);
    memset(screen->pixels, 0, pixel_count * bit_depth / 8);

    Window window = create_window(0, 0, width, height, bit_depth);
    XMapWindow(dpy, window);

    Pixmap pixel_map = XCreatePixmap(dpy, window, width, height, bit_depth);

    GC gc = XCreateGC(dpy, pixel_map, 0, NULL);

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

    XWindowAttributes xwa;
    XGetWindowAttributes(dpy, window, &xwa);
    state->width = xwa.width;
    state->height = xwa.height;
    screen->internal = state;

    XImage* image = create_image(state->width, state->height);
    state->image = image;

    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 draw_screen(struct Screen* screen) {
    WindowState* state = (WindowState*) screen->internal;

    uint32_t* src = screen->pixels;
    uint32_t* dst = (uint32_t*) state->image->data;

    float x_step = screen->width / (float) state->width;
    float y_step = screen->height / (float) state->height;

    for (int x = 0; x < state->width; x++) {
        int px = (int) (x * x_step);

        for (int y = 0; y < state->height; y++) {
            int py = (int) (y * y_step);

            dst[x + y * state->width] = src[px + py * screen->width];
        }
    }

    XPutImage(
        dpy,
        state->window,
        state->graphics_ctx,
        state->image,
        0, 0, 0, 0,
        state->width,
        state->height
    );
}


static void handle_event(WindowState* state) {
    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;
            state->width = xce.width;
            state->height = xce.height;

            XDestroyImage(state->image);
            state->image = create_image(state->width, state->height);

            break;
        }

        default: break;
    }
}

bool poll_screen(struct Screen* screen) {

    draw_screen(screen);
    
    WindowState* state = (WindowState*) screen->internal;

    while (XPending(dpy))
        handle_event(state);

    update_delta(screen);
    return true;
}

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

    XUnmapWindow(dpy, state->window);
    XFreePixmap(dpy, state->pixel_map);
    XDestroyImage(state->image);
    XFreeGC(dpy, state->graphics_ctx);
    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];
}