summaryrefslogtreecommitdiff
path: root/engine_x11.lua
blob: 08f0d4c4d152b348f935c0c4f524b6a2d986b348 (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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
local ffi = require("ffi")

local io_write = io.write

local util = require("util")

ffi.cdef [[
typedef struct {
    unsigned long background_pixmap;
    unsigned long background_pixel;
    unsigned long border_pixmap;
    unsigned long border_pixel;
    int bit_gravity;
    int win_gravity;
    int backing_store;
    unsigned long backing_planes;
    unsigned long backing_pixel;
    int save_under;
    long event_mask;
    long do_not_propagate_mask;
    int override_redirect;
    unsigned long colormap;
    unsigned long cursor;
} XSetWindowAttributes;

typedef struct {
    char *res_name;
    char *res_class;
} XClassHint;

typedef char *XPointer;

typedef struct _XExtData {
    int number;
    struct _XExtData *next;
    int (*free_private)(struct _XExtData *extension);
    XPointer private_data;
} XExtData;

typedef struct {
    XExtData *ext_data;
    unsigned long visualid;
    int class;
    unsigned long red_mask, green_mask, blue_mask;
    int bits_per_rgb;
    int map_entries;
} Visual;

typedef struct {
    int depth;
    int nvisuals;
    Visual *visuals;
} Depth;

typedef struct _Display Display;

typedef struct {
    long flags;
    int x, y;
    int width, height;
    int min_width, min_height;
    int max_width, max_height;
    int width_inc, height_inc;
    struct { int x, y; } min_aspect, max_aspect;
    int base_width, base_height;
    int win_gravity;
} XSizeHints;

typedef struct {
    int type;
    unsigned long serial;
    int send_event;
    Display *display;
    unsigned long window;
    unsigned long root;
    unsigned long subwindow;
    unsigned long time;
    int x, y;
    int x_root, y_root;
    unsigned int state;
    unsigned int keycode;
    int same_screen;
} XKeyEvent;

typedef struct {
    int type;
    unsigned long serial;
    int send_event;
    Display *display;
    unsigned long window;
    unsigned long root;
    unsigned long subwindow;
    unsigned long time;
    int x, y;
    int x_root, y_root;
    unsigned int state;
    unsigned int button;
    int same_screen;
} XButtonEvent;

typedef struct {
    int type;
    unsigned long serial;
    int send_event;
    Display *display;
    unsigned long window;
    unsigned long root;
    unsigned long subwindow;
    unsigned long time;
    int x, y;
    int x_root, y_root;
    unsigned int state;
    char is_hint;
    int same_screen;
} XMotionEvent;

typedef struct {
    int type;
    unsigned long serial;
    int send_event;
    Display *display;
    unsigned long window;
    char key_vector[32];
} XKeymapEvent;

typedef struct {
    int type;
    unsigned long serial;
    int send_event;
    Display *display;
    unsigned long window;
    int x, y;
    int width, height;
    int count;
} XExposeEvent;

typedef struct {
    int type;
    unsigned long serial;
    int send_event;
    Display *display;
    unsigned long event;
    unsigned long window;
    int x, y;
    int width, height;
    int border_width;
    unsigned long above;
    int override_redirect;
} XConfigureEvent;

typedef union _XEvent {
    int type;
    XKeyEvent xkey;
    XButtonEvent xbutton;
    XMotionEvent xmotion;
    XExposeEvent xexpose;
    XConfigureEvent xconfigure;
    long pad[24];
} XEvent;

typedef struct {
    XExtData *ext_data;
    Display *display;
    unsigned long root;
    int width, height;
    int mwidth, mheight;
    int ndepths;
    Depth *depths;
    int root_depth;
    Visual *root_visual;
    void *default_gc;
    unsigned long cmap;
    unsigned long white_pixel;
    unsigned long black_pixel;
    int max_maps, min_maps;
    int backing_store;
    int save_unders;
    long root_input_mask;
} Screen;

typedef struct {
    XExtData *ext_data;
    int depth;
    int bits_per_pixel;
    int scanline_pad;
} ScreenFormat;

typedef struct {
    XExtData *ext_data;
    void *private1;
    int fd;
    int private2;
    int proto_major_version;
    int proto_minor_version;
    char *vendor;
    unsigned long private3;
    unsigned long private4;
    unsigned long private5;
    int private6;
    unsigned long (*resource_alloc)(Display*);
    int byte_order;
    int bitmap_unit;
    int bitmap_pad;
    int bitmap_bit_order;
    int nformats;
    ScreenFormat *pixmap_format;
    int private8;
    int release;
    void *private9, *private10;
    int qlen;
    unsigned long last_request_read;
    unsigned long request;
    XPointer private11;
    XPointer private12;
    XPointer private13;
    XPointer private14;
    unsigned max_request_size;
    void *db;
    int (*private15)(Display*);
    char *display_name;
    int default_screen;
    int nscreens;
    Screen *screens;
    unsigned long motion_buffer;
    unsigned long private16;
    int min_keycode;
    int max_keycode;
    XPointer private17;
    XPointer private18;
    int private19;
    char *xdefaults;
} *_XPrivDisplay;

extern int XCloseDisplay(Display*);
extern unsigned long XCreateWindow(Display*, unsigned long, int, int,
    unsigned int, unsigned int, unsigned int, int, unsigned int, Visual*,
    unsigned long, XSetWindowAttributes*);
extern int XDestroyWindow(Display*, unsigned long);
extern int XChangeProperty(Display*, unsigned long, unsigned long,
    unsigned long, int, int, const unsigned char*, int );
extern int XCheckMaskEvent(Display*, long, XEvent*);
extern int XInitThreads(void);
extern unsigned long XInternAtom(Display*, const char*, int);
extern unsigned long XkbKeycodeToKeysym(Display*, unsigned char, int, int);
extern char *XKeysymToString(unsigned long);
extern int XMapWindow(Display*, unsigned long);
extern Display *XOpenDisplay(const char*);
extern int XSetClassHint(Display*, unsigned long, XClassHint*);
extern void XSetWMNormalHints(Display*, unsigned long, XSizeHints*);
extern int XStoreName(Display*, unsigned long, const char*);
extern int XSync(Display*, int);

typedef struct _Evas_Engine_Info_GL_X11 Evas_Engine_Info_GL_X11;

struct _Evas_Engine_Info_GL_X11 {
    Evas_Engine_Info magic;
    struct {
        void *display;
        unsigned long drawable;
        void *visual;
        unsigned long colormap;
        int depth;
        int screen;
        int rotation;
        unsigned int destination_alpha : 1;
    } info;
    struct {
        void *(*best_visual_get) (Evas_Engine_Info_GL_X11 *einfo);
        unsigned long (*best_colormap_get) (Evas_Engine_Info_GL_X11 *einfo);
        int (*best_depth_get) (Evas_Engine_Info_GL_X11 *einfo);
    } func;
    struct {
        void (*pre_swap) (void *data, Evas *e);
        void (*post_swap) (void *data, Evas *e);
        void *data;
    } callback;
    Evas_Engine_Render_Mode render_mode;
    unsigned char vsync : 1;
    unsigned char indirect : 1;
    unsigned char swap_mode : 4;
};

typedef enum {
    EVAS_ENGINE_INFO_SOFTWARE_X11_BACKEND_XLIB,
    EVAS_ENGINE_INFO_SOFTWARE_X11_BACKEND_XCB
} Evas_Engine_Info_Software_X11_Backend;

typedef struct _Evas_Engine_Info_Software_X11 Evas_Engine_Info_Software_X11;

struct _Evas_Engine_Info_Software_X11 {
    Evas_Engine_Info magic;
    struct {
        Evas_Engine_Info_Software_X11_Backend backend;
        void *connection, *screen;
        unsigned int drawable, mask;
        void *visual;
        unsigned int colormap;
        int depth, rotation;
        Eina_Bool alloc_grayscale : 1;
        Eina_Bool debug : 1;
        Eina_Bool shape_dither : 1;
        Eina_Bool destination_alpha : 1;
        Eina_Bool track_mask_changes : 1;
        int alloc_colors_max;
    } info;
    struct {
        void *(*best_visual_get) (int backend, void *connection, int screen);
        unsigned int (*best_colormap_get) (int backend, void *connection, int screen);
        int (*best_depth_get) (int backend, void *connection, int screen);
    } func;
    unsigned char mask_changed : 1;
    Evas_Engine_Render_Mode render_mode;
};
]]

local xlib = util.load_lib("X11")
if not xlib then return { init = function() end } end
local levas = util.get_lib("evas")

local disp = nil
local win = 0
local first_expose = 0
local evas

local bor, lsh = bit.bor, bit.lshift

local gcpin

local engine_name = {
    ["gl_x11"      ] = "OpenGL X11",
    ["software_x11"] = "Software Xlib",
    ["xrender_x11" ] = "XRender X11"
}

local engine_sname = {
    ["gl_x11"      ] = "gl",
    ["software_x11"] = "xlib",
    ["xrender_x11" ] = "xr"
}

local info_struct = {
    ["gl_x11"      ] = "Evas_Engine_Info_GL_X11*",
    ["software_x11"] = "Evas_Engine_Info_Software_X11*",
    ["xrender_x11" ] = "Evas_Engine_Info_XRender_X11*"
}

local engine_x11_loop
local engine_x11_setup = function(engine, width, height, rmth)
    if not xlib then return false end
    evas = util.conf.evas
    local attr = ffi.new("XSetWindowAttributes")
    local chint = ffi.new("XClassHint")
    local szhints = ffi.new("XSizeHints")
    disp = xlib.XOpenDisplay(nil)
    if disp == nil then return false end
    levas.evas_output_method_set(evas, levas.evas_render_method_lookup(
        rmth))
    local einfo = ffi.cast(info_struct[rmth],
        levas.evas_engine_info_get(evas))
    if einfo == nil then
        io_write("Evas does not suport the " .. engine_name[rmth]
            .. " Engine\n")
        xlib.XCloseDisplay(disp)
        return false
    end
    if rmth == "gl_x11" then
        einfo.info.display = disp
        einfo.info.screen = ffi.cast("_XPrivDisplay", disp).default_screen
        einfo.info.visual = einfo.func.best_visual_get(einfo)
        einfo.info.colormap = einfo.func.best_colormap_get(einfo)
        einfo.info.depth = einfo.func.best_depth_get(einfo)
    else
        einfo.info.backend = levas.EVAS_ENGINE_INFO_SOFTWARE_X11_BACKEND_XLIB
        einfo.info.connection = disp
        einfo.info.screen = nil
        einfo.info.visual = einfo.func.best_visual_get(0, disp,
            ffi.cast("_XPrivDisplay", disp).default_screen)
        einfo.info.colormap = einfo.func.best_colormap_get(0, disp,
            ffi.cast("_XPrivDisplay", disp).default_screen)
        einfo.info.depth = einfo.func.best_depth_get(0, disp,
            ffi.cast("_XPrivDisplay", disp).default_screen)
        einfo.info.rotation = 0
        einfo.info.debug = 0
if nil then -- EVAS_FRAME_QUEUING
        local render_mode = os.getenv("EVAS_RENDER_MODE")
        if render_mode and render_mode == "non-blocking" then
            einfo.render_mode = levas.EVAS_RENDER_MODE_NONBLOCKING
        end
end
    end
    attr.backing_store = 0
    attr.colormap = einfo.info.colormap
    attr.border_pixel = 0
    attr.background_pixmap = 0
    attr.event_mask = bor(lsh(1, 15), lsh(1, 2), lsh(1, 3), lsh(1, 6),
        lsh(1, 17), lsh(1, 4), lsh(1, 5), lsh(1, 0), lsh(1, 1))
    attr.bit_gravity = 0
    local xpriv = ffi.cast("_XPrivDisplay", disp)
    win = xlib.XCreateWindow(disp, (xpriv.screens + xpriv.default_screen).root,
        0, 0, width, height, 0, einfo.info.depth, 1, einfo.info.visual,
        bor(lsh(1, 6), lsh(1, 13), lsh(1, 0), lsh(1, 3), lsh(1, 4), lsh(1, 11)),
        attr)
    if win == 0 then
        xlib.XCloseDisplay(disp)
        return false
    end
    einfo.info.drawable = win
    if levas.evas_engine_info_set(evas, ffi.cast("Evas_Engine_Info*", einfo))
    == 0 then
        io_write("Evas can not setup the informatins of the "
            .. engine_name[rmth] .. " Engine\n")
        xlib.XDestroyWindow(disp, win)
        xlib.XCloseDisplay(disp, win)
        return false
    end

    if util.conf.fullscreen then
        local prop = xlib.XInternAtom(disp, "_NET_WM_STATE", 0)
        local state = xlib.XInternAtom(disp, "_NET_WM_STATE_FULLSCREEN", 0)
        local data = ffi.new("unsigned long[1]", state)
        xlib.XChangeProperty(disp, win, prop, 4, 32, 0,
            ffi.cast("unsigned char*", data), 1)
    end
    xlib.XStoreName(disp, win, "Luaxpedite - Luaified Evas Test Suite")
    chint.res_name = ffi.cast("char*", "expedite")
    chint.res_class = ffi.cast("char*", "Expedite")
    gcpin = { "expedite", "Expedite" }
    xlib.XSetClassHint(disp, win, chint)
    szhints.flags = bor(lsh(1, 4), lsh(1, 5), lsh(1, 3), lsh(1, 1))
    szhints.min_width = width
    szhints.max_width = width
    szhints.min_height = height
    szhints.max_height = height
    xlib.XSetWMNormalHints(disp, win, szhints)
    xlib.XMapWindow(disp, win)
    xlib.XSync(disp, 0)
    while first_expose == 0 do engine_x11_loop() end
    return true
end

local modkeys = {
    ["Shift_L"  ] = true, ["Shift_R"  ] = true,
    ["Control_L"] = true, ["Control_R"] = true,
    ["Alt_L"    ] = true, ["Alt_R"    ] = true,
    ["Super_L"  ] = true, ["Super_R"  ] = true
}

local lockkeys = {
    ["Caps_Lock"] = true, ["Num_Lock"] = true, ["Scroll_Lock"] = true
}

local evtypes = {
    [4] = function(ev) -- button press
        levas.evas_event_feed_mouse_move(evas, ev.xbutton.x, ev.xbutton.y,
            0, nil)
        levas.evas_event_feed_mouse_down(evas, ev.xbutton.button,
            levas.EVAS_BUTTON_NONE, 0, nil)
    end,
    [5] = function(ev) -- button release
        levas.evas_event_feed_mouse_move(evas, ev.xbutton.x, ev.xbutton.y,
            0, nil)
        levas.evas_event_feed_mouse_up(evas, ev.xbutton.button,
            levas.EVAS_BUTTON_NONE, 0, nil)
    end,
    [6] = function(ev) -- motion notify
        levas.evas_event_feed_mouse_move(evas, ev.xmotion.x, ev.xmotion.y,
            0, nil)
    end,
    [12] = function(ev) -- expose
        first_expose = 1
        levas.evas_damage_rectangle_add(evas, ev.xexpose.x, ev.xexpose.y,
            ev.xexpose.width, ev.xexpose.height)
    end,
    [22] = function(ev) -- configure notify
        levas.evas_output_viewport_set(evas, 0, 0, ev.xconfigure.width,
            ev.xconfigure.height)
        levas.evas_output_size_set(evas, ev.xconfigure.width,
            ev.xconfigure.height)
        local profile = util.conf.profile
        profile.w, profile.h = ev.xconfigure.width, ev.xconfigure.height
    end,
    [7] = function(ev) -- enter notify
        levas.evas_event_feed_mouse_in(evas, 0, nil)
    end,
    [8] = function(ev) -- leave notify
        levas.evas_event_feed_mouse_out(evas, 0, nil)
    end,
    [2] = function(ev) -- keypress
        local ks = xlib.XkbKeycodeToKeysym(ev.xkey.display, ev.xkey.keycode,
            0, 0)
        local kstr = xlib.XKeysymToString(ks)
        if kstr ~= nil then
            kstr = ffi.string(kstr)
            if modkeys[kstr] then
                levas.evas_key_modifier_on(evas, kstr:sub(1, #kstr - 2))
            elseif lockkeys[kstr] then
                if levas.evas_key_lock_is_set(levas.evas_key_lock_get(
                evas), kstr) then
                    levas.evas_key_lock_off(evas, kstr)
                else
                    levas.evas_key_lock_on(evas, kstr)
                end
            end
            levas.evas_event_feed_key_down(evas, kstr, kstr, nil, nil,
                0, nil)
        end
    end,
    [3] = function(ev) -- keyrelease
        local ks = xlib.XkbKeycodeToKeysym(ev.xkey.display, ev.xkey.keycode,
            0, 0)
        local kstr = xlib.XKeysymToString(ks)
        if kstr ~= nil then
            kstr = ffi.string(kstr)
            if modkeys[kstr] then
                levas.evas_key_modifier_off(evas, kstr:sub(1, #kstr - 2))
            end
            levas.evas_event_feed_key_up(evas, kstr, kstr, nil, nil, 0, nil)
        end
    end
}

local ev = ffi.new("XEvent")
engine_x11_loop = function()
    if xlib.XCheckMaskEvent(disp, bor(lsh(1, 15), lsh(1, 2), lsh(1, 3),
    lsh(1, 6), lsh(1, 17), lsh(1, 4), lsh(1, 5), lsh(1, 0), lsh(1, 1)),
    ev) == 0 then return end
    local evf = evtypes[ev.type]
    if evf then evf(ev) end
    return engine_x11_loop()
end

local engine_x11_shutdown = function()
    xlib.XDestroyWindow(disp, win)
    xlib.XCloseDisplay(disp)
end

return { init = function(engines, rmth)
    engines[#engines + 1] = {
        engine_sname[rmth],
        function(e, w, h) return engine_x11_setup(e, w, h, rmth) end,
        engine_x11_loop, engine_x11_shutdown
    }
end }