summaryrefslogtreecommitdiff
path: root/engine_sw_sdl.lua
blob: 2bc2862171ec44964c488425b82e84907bcbd922 (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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
local ffi = require("ffi")
local ffi_cast = ffi.cast
local ffi_str = ffi.string
local bor = bit.bor

local util = require("util")

local write_out = util.write_out

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

ffi.cdef [[
typedef struct SDL_Surface {
    uint32_t flags;
    void *format;
    int w, h;
    int pitch;
    void *pixels;
} SDL_Surface;

typedef enum {
    SDLK_UNKNOWN = 0,
    SDLK_FIRST = 0,
    SDLK_BACKSPACE = 8,
    SDLK_TAB = 9,
    SDLK_CLEAR = 12,
    SDLK_RETURN = 13,
    SDLK_PAUSE = 19,
    SDLK_ESCAPE = 27,
    SDLK_SPACE = 32,
    SDLK_EXCLAIM = 33,
    SDLK_QUOTEDBL = 34,
    SDLK_HASH = 35,
    SDLK_DOLLAR = 36,
    SDLK_AMPERSAND = 38,
    SDLK_QUOTE = 39,
    SDLK_LEFTPAREN = 40,
    SDLK_RIGHTPAREN = 41,
    SDLK_ASTERISK = 42,
    SDLK_PLUS = 43,
    SDLK_COMMA = 44,
    SDLK_MINUS = 45,
    SDLK_PERIOD = 46,
    SDLK_SLASH = 47,
    SDLK_0 = 48,
    SDLK_1 = 49,
    SDLK_2 = 50,
    SDLK_3 = 51,
    SDLK_4 = 52,
    SDLK_5 = 53,
    SDLK_6 = 54,
    SDLK_7 = 55,
    SDLK_8 = 56,
    SDLK_9 = 57,
    SDLK_COLON = 58,
    SDLK_SEMICOLON = 59,
    SDLK_LESS = 60,
    SDLK_EQUALS = 61,
    SDLK_GREATER = 62,
    SDLK_QUESTION = 63,
    SDLK_AT = 64,
    SDLK_LEFTBRACKET = 91,
    SDLK_BACKSLASH = 92,
    SDLK_RIGHTBRACKET = 93,
    SDLK_CARET = 94,
    SDLK_UNDERSCORE = 95,
    SDLK_BACKQUOTE = 96,
    SDLK_a = 97,
    SDLK_b = 98,
    SDLK_c = 99,
    SDLK_d = 100,
    SDLK_e = 101,
    SDLK_f = 102,
    SDLK_g = 103,
    SDLK_h = 104,
    SDLK_i = 105,
    SDLK_j = 106,
    SDLK_k = 107,
    SDLK_l = 108,
    SDLK_m = 109,
    SDLK_n = 110,
    SDLK_o = 111,
    SDLK_p = 112,
    SDLK_q = 113,
    SDLK_r = 114,
    SDLK_s = 115,
    SDLK_t = 116,
    SDLK_u = 117,
    SDLK_v = 118,
    SDLK_w = 119,
    SDLK_x = 120,
    SDLK_y = 121,
    SDLK_z = 122,
    SDLK_DELETE = 127,
    SDLK_WORLD_0 = 160,
    SDLK_WORLD_1 = 161,
    SDLK_WORLD_2 = 162,
    SDLK_WORLD_3 = 163,
    SDLK_WORLD_4 = 164,
    SDLK_WORLD_5 = 165,
    SDLK_WORLD_6 = 166,
    SDLK_WORLD_7 = 167,
    SDLK_WORLD_8 = 168,
    SDLK_WORLD_9 = 169,
    SDLK_WORLD_10 = 170,
    SDLK_WORLD_11 = 171,
    SDLK_WORLD_12 = 172,
    SDLK_WORLD_13 = 173,
    SDLK_WORLD_14 = 174,
    SDLK_WORLD_15 = 175,
    SDLK_WORLD_16 = 176,
    SDLK_WORLD_17 = 177,
    SDLK_WORLD_18 = 178,
    SDLK_WORLD_19 = 179,
    SDLK_WORLD_20 = 180,
    SDLK_WORLD_21 = 181,
    SDLK_WORLD_22 = 182,
    SDLK_WORLD_23 = 183,
    SDLK_WORLD_24 = 184,
    SDLK_WORLD_25 = 185,
    SDLK_WORLD_26 = 186,
    SDLK_WORLD_27 = 187,
    SDLK_WORLD_28 = 188,
    SDLK_WORLD_29 = 189,
    SDLK_WORLD_30 = 190,
    SDLK_WORLD_31 = 191,
    SDLK_WORLD_32 = 192,
    SDLK_WORLD_33 = 193,
    SDLK_WORLD_34 = 194,
    SDLK_WORLD_35 = 195,
    SDLK_WORLD_36 = 196,
    SDLK_WORLD_37 = 197,
    SDLK_WORLD_38 = 198,
    SDLK_WORLD_39 = 199,
    SDLK_WORLD_40 = 200,
    SDLK_WORLD_41 = 201,
    SDLK_WORLD_42 = 202,
    SDLK_WORLD_43 = 203,
    SDLK_WORLD_44 = 204,
    SDLK_WORLD_45 = 205,
    SDLK_WORLD_46 = 206,
    SDLK_WORLD_47 = 207,
    SDLK_WORLD_48 = 208,
    SDLK_WORLD_49 = 209,
    SDLK_WORLD_50 = 210,
    SDLK_WORLD_51 = 211,
    SDLK_WORLD_52 = 212,
    SDLK_WORLD_53 = 213,
    SDLK_WORLD_54 = 214,
    SDLK_WORLD_55 = 215,
    SDLK_WORLD_56 = 216,
    SDLK_WORLD_57 = 217,
    SDLK_WORLD_58 = 218,
    SDLK_WORLD_59 = 219,
    SDLK_WORLD_60 = 220,
    SDLK_WORLD_61 = 221,
    SDLK_WORLD_62 = 222,
    SDLK_WORLD_63 = 223,
    SDLK_WORLD_64 = 224,
    SDLK_WORLD_65 = 225,
    SDLK_WORLD_66 = 226,
    SDLK_WORLD_67 = 227,
    SDLK_WORLD_68 = 228,
    SDLK_WORLD_69 = 229,
    SDLK_WORLD_70 = 230,
    SDLK_WORLD_71 = 231,
    SDLK_WORLD_72 = 232,
    SDLK_WORLD_73 = 233,
    SDLK_WORLD_74 = 234,
    SDLK_WORLD_75 = 235,
    SDLK_WORLD_76 = 236,
    SDLK_WORLD_77 = 237,
    SDLK_WORLD_78 = 238,
    SDLK_WORLD_79 = 239,
    SDLK_WORLD_80 = 240,
    SDLK_WORLD_81 = 241,
    SDLK_WORLD_82 = 242,
    SDLK_WORLD_83 = 243,
    SDLK_WORLD_84 = 244,
    SDLK_WORLD_85 = 245,
    SDLK_WORLD_86 = 246,
    SDLK_WORLD_87 = 247,
    SDLK_WORLD_88 = 248,
    SDLK_WORLD_89 = 249,
    SDLK_WORLD_90 = 250,
    SDLK_WORLD_91 = 251,
    SDLK_WORLD_92 = 252,
    SDLK_WORLD_93 = 253,
    SDLK_WORLD_94 = 254,
    SDLK_WORLD_95 = 255,
    SDLK_KP0 = 256,
    SDLK_KP1 = 257,
    SDLK_KP2 = 258,
    SDLK_KP3 = 259,
    SDLK_KP4 = 260,
    SDLK_KP5 = 261,
    SDLK_KP6 = 262,
    SDLK_KP7 = 263,
    SDLK_KP8 = 264,
    SDLK_KP9 = 265,
    SDLK_KP_PERIOD = 266,
    SDLK_KP_DIVIDE = 267,
    SDLK_KP_MULTIPLY = 268,
    SDLK_KP_MINUS = 269,
    SDLK_KP_PLUS = 270,
    SDLK_KP_ENTER = 271,
    SDLK_KP_EQUALS = 272,
    SDLK_UP = 273,
    SDLK_DOWN = 274,
    SDLK_RIGHT = 275,
    SDLK_LEFT = 276,
    SDLK_INSERT = 277,
    SDLK_HOME = 278,
    SDLK_END = 279,
    SDLK_PAGEUP = 280,
    SDLK_PAGEDOWN = 281,
    SDLK_F1 = 282,
    SDLK_F2 = 283,
    SDLK_F3 = 284,
    SDLK_F4 = 285,
    SDLK_F5 = 286,
    SDLK_F6 = 287,
    SDLK_F7 = 288,
    SDLK_F8 = 289,
    SDLK_F9 = 290,
    SDLK_F10 = 291,
    SDLK_F11 = 292,
    SDLK_F12 = 293,
    SDLK_F13 = 294,
    SDLK_F14 = 295,
    SDLK_F15 = 296,
    SDLK_NUMLOCK = 300,
    SDLK_CAPSLOCK = 301,
    SDLK_SCROLLOCK = 302,
    SDLK_RSHIFT = 303,
    SDLK_LSHIFT = 304,
    SDLK_RCTRL = 305,
    SDLK_LCTRL = 306,
    SDLK_RALT = 307,
    SDLK_LALT = 308,
    SDLK_RMETA = 309,
    SDLK_LMETA = 310,
    SDLK_LSUPER = 311,
    SDLK_RSUPER = 312,
    SDLK_MODE = 313,
    SDLK_COMPOSE = 314,
    SDLK_HELP = 315,
    SDLK_PRINT = 316,
    SDLK_SYSREQ = 317,
    SDLK_BREAK = 318,
    SDLK_MENU = 319,
    SDLK_POWER = 320,
    SDLK_EURO = 321,
    SDLK_UNDO = 322,
    SDLK_LAST
} SDLKey;

typedef enum {
    KMOD_NONE = 0x0000,
    KMOD_LSHIFT = 0x0001,
    KMOD_RSHIFT = 0x0002,
    KMOD_LCTRL = 0x0040,
    KMOD_RCTRL = 0x0080,
    KMOD_LALT = 0x0100,
    KMOD_RALT = 0x0200,
    KMOD_LMETA = 0x0400,
    KMOD_RMETA = 0x0800,
    KMOD_NUM = 0x1000,
    KMOD_CAPS = 0x2000,
    KMOD_MODE = 0x4000,
    KMOD_RESERVED = 0x8000
} SDLMod;

typedef enum {
    SDL_NOEVENT = 0,
    SDL_ACTIVEEVENT,
    SDL_KEYDOWN,
    SDL_KEYUP,
    SDL_MOUSEMOTION,
    SDL_MOUSEBUTTONDOWN,
    SDL_MOUSEBUTTONUP,
    SDL_JOYAXISMOTION,
    SDL_JOYBALLMOTION,
    SDL_JOYHATMOTION,
    SDL_JOYBUTTONDOWN,
    SDL_JOYBUTTONUP,
    SDL_QUIT,
    SDL_SYSWMEVENT,
    SDL_EVENT_RESERVEDA,
    SDL_EVENT_RESERVEDB,
    SDL_VIDEORESIZE,
    SDL_VIDEOEXPOSE,
    SDL_EVENT_RESERVED2,
    SDL_EVENT_RESERVED3,
    SDL_EVENT_RESERVED4,
    SDL_EVENT_RESERVED5,
    SDL_EVENT_RESERVED6,
    SDL_EVENT_RESERVED7,
    SDL_USEREVENT = 24,
    SDL_NUMEVENTS = 32
} SDL_EventType;

typedef struct SDL_keysym {
    uint8_t scancode;
    SDLKey sym;
    SDLMod mod;
    uint16_t unicode;
} SDL_keysym;

typedef struct SDL_KeyboardEvent {
    uint8_t type;
    uint8_t which;
    uint8_t state;
    SDL_keysym keysym;
} SDL_KeyboardEvent;

typedef struct SDL_MouseMotionEvent {
    uint8_t type;
    uint8_t which;
    uint8_t state;
    uint16_t x, y;
    int16_t xrel;
    int16_t yrel;
} SDL_MouseMotionEvent;

typedef struct SDL_MouseButtonEvent {
    uint8_t type;
    uint8_t which;
    uint8_t button;
    uint8_t state;
    uint16_t x, y;
} SDL_MouseButtonEvent;

typedef struct SDL_ResizeEvent {
    uint8_t type;
    int w;
    int h;
} SDL_ResizeEvent;

typedef union {
    SDL_KeyboardEvent key;
    SDL_MouseMotionEvent motion;
    SDL_MouseButtonEvent button;
    SDL_ResizeEvent resize;
    uint8_t type;
} SDL_Event;

int SDL_Flip(SDL_Surface *screen);
int SDL_Init(uint32_t flags);
const char *SDL_GetError(void);
void SDL_Quit(void);
SDL_Surface *SDL_SetVideoMode(int width, int height, int bpp, uint32_t flags);
int SDL_SetAlpha(SDL_Surface *surface, uint32_t flag, uint8_t alpha);
int SDL_FillRect(SDL_Surface *dst, void *dstrect, uint32_t color);
int SDL_PollEvent(SDL_Event *event);
void SDL_WM_SetCaption(const char *title, const char *icon);

typedef struct _Evas_Engine_Info_Buffer Evas_Engine_Info_Buffer;

struct _Evas_Engine_Info_Buffer {
   Evas_Engine_Info magic;
   struct {
      int   depth_type;

      void *dest_buffer;
      int   dest_buffer_row_bytes;

      char  use_color_key : 1;
      int   alpha_threshold;
      int   color_key_r;
      int   color_key_g;
      int   color_key_b;
      struct {
         void * (*new_update_region) (int x, int y, int w, int h, int *row_bytes);
         void   (*free_update_region) (int x, int y, int w, int h, void *data);
         void * (*switch_buffer) (void *data, void *dest_buffer);
      } func;

      void *switch_data;
   } info;

   Evas_Engine_Render_Mode render_mode;
};
]]

local evas
local flip_cb
local win_w, win_h

local engine_sdl_setup = function(engine, width, height)
    if not lsdl then return false end
    win_w, win_h = width, height
    evas = util.conf.evas
    local ok = (engine == "sdl")
    if not ok then return false end
    levas.evas_output_method_set(evas, levas.evas_render_method_lookup(
        "buffer"))
    local einfo = ffi.cast("Evas_Engine_Info_Buffer*",
        levas.evas_engine_info_get(evas))
    if not einfo then return false end
    -- video, noparachute
    if lsdl.SDL_Init(bor(0x00100000, 0x00000020)) < 0 then
        write_out("SDL_Init failed with " .. ffi_str(lsdl.SDL_GetError()))
        lsdl.SDL_Quit()
        return false
    end
    einfo.info.depth_type = 4 -- rgb32
    einfo.info.switch_data = lsdl.SDL_SetVideoMode(width, height, 32,
        bor(0x00000001, 0x40000000))
    if einfo.info.switch_data == nil then
        write_out("SDL_SetVideoMode failed!\n")
        lsdl.SDL_Quit()
        return false
    end
    lsdl.SDL_WM_SetCaption("Luaxpedite - Luaified Evas Test Suite", nil)
    lsdl.SDL_SetAlpha(einfo.info.switch_data, 0x00010000, 0) -- SDL_SRCALPHA
    lsdl.SDL_FillRect(einfo.info.switch_data, nil, 0)
    einfo.info.dest_buffer = ffi_cast("SDL_Surface*",
        einfo.info.switch_data).pixels
    einfo.info.dest_buffer_row_bytes = width * ffi.sizeof("int")
    einfo.info.use_color_key = 0
    einfo.info.alpha_threshold = 0
    einfo.info.func.new_update_region = nil
    einfo.info.func.free_update_region = nil
    einfo.info.func.switch_buffer = function(data, dest)
        lsdl.SDL_Flip(data)
        return ffi_cast("SDL_Surface*", data).pixels
    end
    flip_cb = einfo.info.func.switch_buffer
    if levas.evas_engine_info_set(evas, ffi.cast("Evas_Engine_Info*", einfo))
    == 0 then
        write_out("evas_engine_info_set() for engine 'sdl' with "
            .. "'buffer' backend failed.\n")
        lsdl.SDL_Quit()
        return false
    end
    return true
end

local modkeys = {
    [lsdl.SDLK_LSHIFT] = "Shift", [lsdl.SDLK_RSHIFT] = "Shift",
    [lsdl.SDLK_LCTRL] = "Control", [lsdl.SDLK_RCTRL] = "Control",
    [lsdl.SDLK_MENU] = "Alt",
    [lsdl.SDLK_LALT] = "Alt", [lsdl.SDLK_RALT] = "Alt",
    [lsdl.SDLK_LSUPER] = "Super", [lsdl.SDLK_RSUPER] = "Super",
}

local lockkeys = {
    [lsdl.SDLK_CAPSLOCK] = "Caps_Lock", [lsdl.SDLK_NUMLOCK] = "Num_Lock",
    [lsdl.SDLK_SCROLLOCK] = "Scroll_Lock"
}

local feedkeys = {
    [lsdl.SDLK_ESCAPE] = "Escape",
    [lsdl.SDLK_KP_ENTER] = "Return", [lsdl.SDLK_RETURN] = "Return",
    [lsdl.SDLK_LEFT] = "Left", [lsdl.SDLK_RIGHT] = "Right"
}

local evtypes = {
    [lsdl.SDL_MOUSEMOTION] = function(ev)
        levas.evas_event_feed_mouse_move(evas, ev.motion.x, ev.motion.y,
            0, nil)
    end,
    [lsdl.SDL_MOUSEBUTTONDOWN] = function(ev)
        levas.evas_event_feed_mouse_move(evas, ev.button.x, ev.button.y,
            0, nil)
        levas.evas_event_feed_mouse_down(evas, ev.button.button,
            levas.EVAS_BUTTON_NONE, 0, nil)
    end,
    [lsdl.SDL_MOUSEBUTTONUP] = function(ev)
        levas.evas_event_feed_mouse_move(evas, ev.button.x, ev.button.y,
            0, nil)
        levas.evas_event_feed_mouse_up(evas, ev.button.button,
            levas.EVAS_BUTTON_NONE, 0, nil)
    end,
    [lsdl.SDL_VIDEORESIZE] = function(ev)
        -- we're not resizable, don't bother
    end,
    [lsdl.SDL_VIDEOEXPOSE] = function(ev)
        levas.evas_damage_rectangle_add(evas, 0, 0, win_w, win_h)
    end,
    [lsdl.SDL_KEYDOWN] = function(ev)
        local sym = tonumber(ev.key.keysym.sym)
        local mod = modkeys[sym]
        if mod then
            levas.evas_key_modifier_on(evas, mod)
            return
        end
        local lock = lockkeys[sym]
        if lock then
            if levas.evas_key_lock_is_set(levas.evas_key_lock_get(evas),
            lock) ~= 0 then
                levas.evas_key_lock_off(evas, lock)
            else
                levas.evas_key_lock_on(evas, lock)
            end
            return
        end
        local feed = feedkeys[sym]
        if feed then
            levas.evas_event_feed_key_down(evas, feed, feed, nil, nil,
                0, nil)
            return
        end
        if sym == lsdl.SDLK_q then
            if levas.evas_key_lock_is_set(levas.evas_key_lock_get(evas),
            "Caps_Lock") ~= 0 then
                levas.evas_event_feed_key_down(evas, "Q", "Q", nil, nil,
                    0, nil)
            else
                levas.evas_event_feed_key_down(evas, "q", "q", nil, nil,
                    0, nil)
            end
        end
    end,
    [lsdl.SDL_KEYUP] = function(ev)
        local sym = tonumber(ev.key.keysym.sym)
        local mod = modkeys[sym]
        if mod then
            levas.evas_key_modifier_off(evas, mod)
            return
        end
        local feed = feedkeys[sym]
        if feed then
            levas.evas_event_feed_key_up(evas, feed, feed, nil, nil, 0, nil)
            return
        end
        if sym == lsdl.SDLK_q then
            if levas.evas_key_lock_is_set(levas.evas_key_lock_get(evas),
            "Caps_Lock") ~= 0 then
                levas.evas_event_feed_key_up(evas, "Q", "Q", nil, nil,
                    0, nil)
            else
                levas.evas_event_feed_key_up(evas, "q", "q", nil, nil,
                    0, nil)
            end
        end
    end,
    [lsdl.SDL_QUIT] = function(ev)
        levas.evas_event_feed_key_down(evas, "Escape", "Escape", nil, nil,
            0, nil)
    end,
    [lsdl.SDL_SYSWMEVENT] = function(ev) end,
    [lsdl.SDL_USEREVENT] = function(ev) end,
    [lsdl.SDL_ACTIVEEVENT] = function(ev) end,
    [lsdl.SDL_JOYAXISMOTION] = function(ev) end,
    [lsdl.SDL_JOYHATMOTION] = function(ev) end,
    [lsdl.SDL_JOYBUTTONDOWN] = function(ev) end,
    [lsdl.SDL_JOYBUTTONUP] = function(ev) end
}

local ev = ffi.new("SDL_Event")
local engine_sdl_loop; engine_sdl_loop = function()
    if lsdl.SDL_PollEvent(ev) == 0 then return end
    local f = evtypes[ev.type]
    if f then f(ev) end
    return engine_sdl_loop()
end

local engine_sdl_shutdown = function()
    lsdl.SDL_Quit()
end

return { init = function(engines)
    engines[#engines + 1] = {
        "sdl", engine_sdl_setup, engine_sdl_loop, engine_sdl_shutdown
    }
end }