aboutsummaryrefslogtreecommitdiffstats
path: root/xlib.c
blob: 73b8d844c1c17b5e456115595656ab82a071b83b (plain) (blame)
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
#include <cairo-xlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>

#include "runes.h"

RunesWindow *runes_window_create()
{
    RunesWindow *w;
    unsigned long white;
    XIM im;

    w = malloc(sizeof(RunesWindow));

    w->dpy = XOpenDisplay(NULL);
    white  = WhitePixel(w->dpy, DefaultScreen(w->dpy));
    w->w   = XCreateSimpleWindow(
        w->dpy, DefaultRootWindow(w->dpy),
        0, 0, 240, 80, 0, white, white
    );

    XSelectInput(w->dpy, w->w, StructureNotifyMask);
    XMapWindow(w->dpy, w->w);
    w->gc = XCreateGC(w->dpy, w->w, 0, NULL);
    XSetForeground(w->dpy, w->gc, white);

    for (;;) {
        XEvent e;

        XNextEvent(w->dpy, &e);
        if (e.type == MapNotify) {
            break;
        }
    }

    XSetLocaleModifiers("");
    im = XOpenIM(w->dpy, NULL, NULL, NULL);
    w->ic = XCreateIC(
        im,
        XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
        XNClientWindow, w->w,
        XNFocusWindow, w->w,
        NULL
    );
    if (w->ic == NULL) {
        fprintf(stderr, "failed\n");
        exit(1);
    }

    return w;
}

cairo_surface_t *runes_surface_create(RunesTerm *t)
{
    Visual *vis;
    XWindowAttributes attrs;

    XGetWindowAttributes(t->w->dpy, t->w->w, &attrs);
    vis = DefaultVisual(t->w->dpy, DefaultScreen(t->w->dpy));
    return cairo_xlib_surface_create(t->w->dpy, t->w->w, vis, attrs.width, attrs.height);
}

static void runes_get_next_event(uv_work_t *req)
{
    struct xlib_loop_data *data;

    data = (struct xlib_loop_data *)req->data;
    XNextEvent(data->data.t->w->dpy, &data->e);
}

static void runes_process_event(uv_work_t *req, int status)
{
    struct xlib_loop_data *data;
    XEvent *e;
    RunesWindow *w;

    UNUSED(status);

    data = ((struct xlib_loop_data *)req->data);
    e = &data->e;
    w = data->data.t->w;

    if (!XFilterEvent(e, None)) {
        switch (e->type) {
        case KeyPress: {
            char *buf = NULL;
            size_t len = 8;
            KeySym sym;
            Status s;
            size_t chars;

            buf = malloc(len);

            for (;;) {
                chars = Xutf8LookupString(w->ic, &e->xkey, buf, len - 1, &sym, &s);
                if (s == XBufferOverflow) {
                    len = chars + 1;
                    buf = realloc(buf, len);
                    continue;
                }
                break;
            }

            runes_handle_keyboard_event(data->data.t, buf, chars);
            free(buf);
            break;
        }
        default:
            break;
        }
    }

    uv_queue_work(req->loop, req, runes_get_next_event, runes_process_event);
}

void runes_loop_init(RunesTerm *t, uv_loop_t *loop)
{
    unsigned long mask;
    void *data;

    XGetICValues(t->w->ic, XNFilterEvents, &mask, NULL);
    XSelectInput(t->w->dpy, t->w->w, mask|KeyPressMask);
    XSetICFocus(t->w->ic);

    data = malloc(sizeof(struct xlib_loop_data));
    ((struct loop_data *)data)->req.data = data;
    ((struct loop_data *)data)->t = t;

    uv_queue_work(loop, data, runes_get_next_event, runes_process_event);
}

void runes_window_destroy(RunesWindow *w)
{
    XIM im;

    im = XIMOfIC(w->ic);
    XDestroyIC(w->ic);
    XCloseIM(im);
    XFreeGC(w->dpy, w->gc);
    XDestroyWindow(w->dpy, w->w);
    XCloseDisplay(w->dpy);

    free(w);
}