aboutsummaryrefslogtreecommitdiff
path: root/platform/x11/x11.c
blob: c117c47a4a9fb7cbab46ef7e2cfa9ab7a6ac59ec (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
// WARNING: CURRENTLY UNMAINTAINED IN FAVOR OF THE GL PLATFORM
// 
// This platform is very restrictive and has issues with shearing. It is more
// pure than OpenGL, but it has some undesired artifacts such as shearing.

#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include <string.h>

#include <X11/Xlib.h>
#include <X11/Xutil.h>

#include "common.h"
#include "teensy.h"
#include "platform.h"

#define SEC2NANO ((uint64_t)1000000000)

struct x11_platform {
    Display *dis;
    Window root;
    Window win;
    GC gc;
    int scr;

    Atom wm_delete_window;

    bool should_close;
    uint64_t initial_time;
};

struct x11_platform p;

static
int err_handler(Display *dis, XErrorEvent *ev)
{
    char msg[1024];
    XGetErrorText(dis, ev->error_code, msg, 1024);
    ty_log_fatal(
        TY_PLATFORM_ERR,
        "X Error (0x%x): %s",
        ev->error_code,
        msg
    );
    return 0;
}

void ty_platform_init(struct ty_ctx *ctx)
{
    XSetErrorHandler(err_handler);

    struct timespec ts;
    clock_gettime(CLOCK_REALTIME, &ts);
    p.initial_time = (uint64_t)ts.tv_sec * SEC2NANO + (uint64_t)ts.tv_nsec;

    p.dis = XOpenDisplay(0);
    p.root = XDefaultRootWindow(p.dis);
    p.scr = DefaultScreen(p.dis);

    XSetWindowAttributes attributes = {};
    attributes.background_pixel = 0x00000000;
    attributes.event_mask =
        StructureNotifyMask |
        KeyPressMask |
        KeyReleaseMask;


    p.win = XCreateWindow(
        p.dis,
        p.root, 
        0, 0,
        ctx->creation_hints.win_width, ctx->creation_hints.win_height,
        0,
        DefaultDepth(p.dis, p.scr),
        CopyFromParent,
        DefaultVisual(p.dis, p.scr),
        CWBackPixel | CWEventMask,
        &attributes
    );

    p.gc = XCreateGC(p.dis, p.win, 0, NULL);

    XMapWindow(p.dis, p.win);

    p.wm_delete_window = XInternAtom(
        p.dis,
        "WM_DELETE_WINDOW",
        false
    );

    p.should_close = false;
}

void ty_platform_deinit(void)
{
    XUnmapWindow(p.dis, p.win);
    XFreeGC(p.dis, p.gc);
    XDestroyWindow(p.dis, p.win);
    XCloseDisplay(p.dis);
}

static
void display_image(struct ty_image img)
{
    size_t size = img.width * img.height;
    // Don't use ty_alloc cause it will be owned and freed by Xlib
    uint32_t *xdata = malloc(sizeof(uint32_t) * size);
    for (int i = 0; i < size; i++) {
        uint8_t r = img.data[i].r;
        uint8_t g = img.data[i].g;
        uint8_t b = img.data[i].b;
        xdata[i] = ((uint32_t)r << 16) | ((uint32_t)g << 8) | (uint32_t)b;
    }

    XImage *image = XCreateImage(
        p.dis,
        DefaultVisual(p.dis, p.scr),
        DefaultDepth(p.dis, p.scr),
        ZPixmap,
        0,
        (char*)xdata,
        img.width,
        img.height,
        32,
        0
    );

    XPutImage(
        p.dis,
        p.win,
        p.gc,
        image,
        0, 0, 0, 0,
        img.width,
        img.height
    );

    XFlush(p.dis);

    XDestroyImage(image);
}

static
void handle_keypress(XEvent ev)
{
    XKeyPressedEvent *kp_ev = (XKeyPressedEvent*)&ev;
    if (kp_ev->keycode == XKeysymToKeycode(p.dis, XK_Escape)) {
        p.should_close = true;
    }
}

static
void handle_client_msg(XEvent ev)
{
    if ((Atom)ev.xclient.data.l[0] == p.wm_delete_window)
        p.should_close = true;
}

void ty_platform_frame(struct ty_image img)
{
    XPending(p.dis);

    while (QLength(p.dis)) {
        XEvent ev;
        XNextEvent(p.dis, &ev);

        switch (ev.type) {
        case KeyPress:
        case KeyRelease:    handle_keypress(ev); break;
        case ClientMessage: handle_client_msg(ev); break;
        }
    }

    display_image(img);
}

bool ty_platform_os_wants_quit(void)
{
    return p.should_close;
}

// Thanks GLFW, ily
// https://github.com/glfw/glfw/blob/master/src/posix_time.c#L52
double ty_platform_get_time(void)
{
    struct timespec ts;
    clock_gettime(CLOCK_REALTIME, &ts);
    uint64_t timensec = (uint64_t)ts.tv_sec * SEC2NANO + (uint64_t)ts.tv_nsec;
    return (double)(timensec - p.initial_time) / SEC2NANO;
}