lt/x.c -rw-r--r-- 8.4 KiB View raw
                                                                                
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
333
334
335
336
337
338
339
340
341
342
343
344
345
#include <X11/cursorfont.h>
#include <X11/keysym.h>
#include <X11/Xft/Xft.h>
#include <X11/XKBlib.h>
#include <X11/Xatom.h>

#include <assert.h>
#include <unistd.h>
#include <stdbool.h>
#include <sys/select.h>
#include <ctype.h>
#include <time.h>

#include "x.h"
#include "utils.h"
#include "config.h"

static int x_geo_mask_to_gravity(int mask)
{
    switch (mask & (XNegative | YNegative))
    {
        case 0:
            return NorthWestGravity;
        case XNegative:
            return NorthEastGravity;
        case YNegative:
            return SouthWestGravity;
    }

    return SouthEastGravity;
}

#define XTERM_COLORS 6 * 6 * 6 + 16
static int x_window_load_color(x_window_t *x, int i, const char *name, XftColor *col)
{
    XRenderColor color = { .alpha = 0xFFFF };

    if (name == NULL)
    {
        if (BETWEEN(i, 16, 255))
        {
            if (i < XTERM_COLORS )
            {
                color.red = sixd_to_16bit(((i - 16) / 36) % 6);
                color.green = sixd_to_16bit(((i - 16) / 6) % 6);
                color.blue = sixd_to_16bit(((i - 16) / 1) % 6);
            }
            else
            {
                color.red = 0x0808 + 0x0a0a * (i - XTERM_COLORS);
                color.green = color.blue = color.red;
            }

            return XftColorAllocValue(x->dsp, x->vis, x->cmap, &color, col);
        }
        else
            name = __color_name[i];
    }

    return XftColorAllocName(x->dsp, x->vis, x->cmap, name, col);
}

static void x_window_event_init(x_window_t *x)
{
    x->attrs.background_pixel = x->dc.color[DEFAULT_BG].pixel;
    x->attrs.border_pixel = x->dc.color[DEFAULT_BG].pixel;
    x->attrs.bit_gravity = NorthWestGravity;
    x->attrs.event_mask =
        FocusChangeMask | KeyPressMask | ExposureMask | VisibilityChangeMask |
        StructureNotifyMask | ButtonMotionMask | ButtonPressMask | ButtonReleaseMask;
    x->attrs.colormap = x->cmap;
}

static void x_window_dc_init(x_window_t *x, Window *parent)
{
    XGCValues   val = { 0 };

    val.graphics_exposures = False;
    x->dc.gc = XCreateGC(x->dsp, *parent, GCGraphicsExposures, &val);
}

static bool x_window_render_init(x_window_t *x, int cols)
{
    x->buf = XCreatePixmap(x->dsp, x->win, x->term.w, x->term.h,
        DefaultDepth(x->dsp, x->screen));

    XSetForeground(x->dsp, x->dc.gc, x->dc.color[DEFAULT_BG].pixel);
    XFillRectangle(x->dsp, x->buf, x->dc.gc, 0, 0, x->term.w, x->term.h);

    x->specbuf = malloc(cols * sizeof(XftGlyphFontSpec));
    if (x->specbuf == NULL)
        return false;

    x->draw = XftDrawCreate(x->dsp, x->buf, x->vis, x->cmap);
    return true;
}

static bool x_window_input_init(x_window_t *x)
{
    static const char *locales[] =
    {
        "@im=local",
        "@im",
        "@im=none"
    };

    x->xim = XOpenIM(x->dsp, NULL, NULL, NULL);
    if (x->xim == NULL)
    {
        STATIC_ARRAY_FOREACH(const char **locale, locales)
        {
            XSetLocaleModifiers(*locale);
            x->xim = XOpenIM(x->dsp, NULL, NULL, NULL);
            if (x->xim != NULL)
                goto xim_ok;
        }

        ERROR("XOpenIM failed, could not open input device");
        return false;
    }

xim_ok:
    x->xic = XCreateIC(x->xim, XNInputStyle,
        XIMPreeditNothing | XIMStatusNothing,
        XNClientWindow, x->win, XNFocusWindow, x->win, NULL
    );

    if (x->xic != NULL)
        return true;

    ERROR("XCreateIC failed, could not obtain input method");
    return false;
}

static void x_window_cursor_init(x_window_t *x)
{
    Cursor      cursor;
    XColor      mouse_fg;
    XColor      mouse_bg;

    cursor = XCreateFontCursor(x->dsp, XC_xterm);
    XDefineCursor(x->dsp, x->win, cursor);

    if (XParseColor(x->dsp, x->cmap, __color_name[MOUSE_FG], &mouse_fg) == 0)
    {
        mouse_fg.red = 0xFFFF;
        mouse_fg.green = 0xFFFF;
        mouse_fg.blue = 0xFFFF;
    }

    if (XParseColor(x->dsp, x->cmap, __color_name[MOUSE_BG], &mouse_bg) == 0)
    {
        mouse_bg.red = 0x0000;
        mouse_bg.green = 0x0000;
        mouse_bg.blue = 0x0000;
    }

    XRecolorCursor(x->dsp, cursor, &mouse_fg, &mouse_bg);
}

static void x_window_property_init(x_window_t *x, pid_t *pid)
{
    x->xembed = XInternAtom(x->dsp, "_XEMBED", False);
    x->wm_delete_win = XInternAtom(x->dsp, "WM_DELETE_WINDOW", False);
    x->net_wm_name = XInternAtom(x->dsp, "_NET_WM_NAME", False);

    XSetWMProtocols(x->dsp, x->win, &x->wm_delete_win, 1);

    x->net_wm_pid = XInternAtom(x->dsp, "_NET_WM_PID", False);
    XChangeProperty(x->dsp, x->win, x->net_wm_pid, XA_CARDINAL, 32,
        PropModeReplace, (u_char *)pid, 1);
}

static bool x_window_color_init(x_window_t *x)
{
    static bool         loaded = false;

    x->dc.col_len = MAX(COUNT_OF(__color_name), 256);
    x->dc.color = malloc(x->dc.col_len * sizeof(XftColor));

    if (x->dc.color == NULL)
    {
        ERROR("Could not allocate memory for color array");
        return false;
    }

    if (loaded)
    {
        for (XftColor *cp = x->dc.color; cp < &(x->dc.color[x->dc.col_len]); cp++)
            XftColorFree(x->dsp, x->vis, x->cmap, cp);
    }

    for (size_t i = 0; i < x->dc.col_len; i++)
    {
        if (!x_window_load_color(x, i, NULL, &x->dc.color[i]))
        {
            ERROR("Could not allocate color %s (%zu)", __color_name[i], i);
            return false;
        }
    }
    loaded = true;

    return true;
}

bool x_window_init(x_window_t *x, int cols, int rows)
{
    pid_t       pid = getpid();
    Window      parent;

    x->dsp = XOpenDisplay(NULL);
    if (x->dsp == NULL)
    {
        ERROR("Can't open display");
        return false;
    }

    if (!FcInit())
    {
        ERROR("Could not init fontconfig");
        return false;
    }

    x->screen = XDefaultScreen(x->dsp);
    x->vis = XDefaultVisual(x->dsp, x->screen);
    x->fd = ConnectionNumber(x->dsp);

    if (!fonts_load(x, "Inconsolata", 12))
        goto error;

    x->cmap = XDefaultColormap(x->dsp, x->screen);

    if (!x_window_color_init(x))
        goto error;

    x->term.w = 2 * BORDER_PX + cols * x->term.cw;
    x->term.h = 2 * BORDER_PX + rows * x->term.ch;

    if (x->gm & XNegative)
        x->l = DisplayWidth(x->dsp, x->screen) - x->term.w - 2;
    if (x->gm & YNegative)
        x->t = DisplayHeight(x->dsp, x->screen) - x->term.h - 2;

    x_window_event_init(x);

    parent = XRootWindow(x->dsp, x->screen);

    /* Create actual window */
    x->win = XCreateWindow(
        x->dsp,
        parent,
        x->l,
        x->t,
        x->term.w,
        x->term.h,
        0,
        XDefaultDepth(x->dsp, x->screen),
        InputOutput,
        x->vis,
        CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask | CWColormap,
        &x->attrs
    );

    x_window_dc_init(x, &parent);

    if (!x_window_render_init(x, cols))
        goto error;

    if (!x_window_input_init(x))
        goto error;

    x_window_cursor_init(x);
    x_window_property_init(x, &pid);

    x->term.mode = MODE_NUMLOCK;
    XMapWindow(x->dsp, x->win);
    x_window_hints(x);

    XSync(x->dsp, False);
    return true;

error:
    x_window_dtr(x);
    return false;
}

void x_window_dtr(x_window_t *x)
{
    for (XftColor *cp = x->dc.color; cp < &(x->dc.color[x->dc.col_len]); cp++)
        XftColorFree(x->dsp, x->vis, x->cmap, cp);

    free(x->dc.color);
    XFree(x->draw);
    XFree(x->dc.gc);
    free(x->specbuf);

    fonts_unload(x);

    XDestroyWindow(x->dsp, x->win);
    XCloseDisplay(x->dsp);
}

void x_window_hints(x_window_t *x)
{
    XSizeHints  *size = NULL;
    XClassHint  class = {
        TERM_NAME,
        TERM_NAME
    };
    XWMHints    wm = {
        .flags = InputHint,
        .input = 1
    };

    size = XAllocSizeHints();
    /* PANIC! here */
    assert(size != NULL);

    size->flags = PSize | PResizeInc | PBaseSize;
    size->height = x->term.h;
    size->width = x->term.w;
    size->height_inc = x->term.ch;
    size->width_inc = x->term.cw;
    size->base_height = 2 * BORDER_PX;
    size->base_width = 2 * BORDER_PX;

    if (x->is_fixed)
    {
        size->flags |= PMaxSize | PMinSize;
        size->min_width = size->max_width = x->term.w;
        size->min_height = size->max_height = x->term.h;
    }

    if (x->gm & (XValue | YValue))
    {
        size->flags |= USPosition | PWinGravity;
        size->x = x->l;
        size->y = x->t;
        size->win_gravity = x_geo_mask_to_gravity(x->gm);
    }

    XSetWMProperties(x->dsp, x->win, NULL, NULL, NULL, 0, size, &wm, &class);
    XFree(size);
}