-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsdl.c
853 lines (672 loc) · 22.9 KB
/
sdl.c
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
/*
* An ED's GUI module for SDL2.
*
* Sergei V. Rogachev <rogachevsergei [at] gmail [dot] com>
*
* Requires some TTF-font file in the default location stored in the
* string FONT_FILE.
*/
#include <assert.h>
#include <unistd.h>
#include <fcntl.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include "unicode.h"
#include "gui.h"
void die(char *);
#ifndef FONT_FILE
#define FONT_FILE "default.ttf"
#endif
#define FONT_SIZE 18
#define SURFACE32_LE_RMASK (0x000000FF)
#define SURFACE32_LE_GMASK (0x0000FF00)
#define SURFACE32_LE_BMASK (0x00FF0000)
#define SURFACE32_LE_AMASK (0xFF000000)
enum {
HMargin = 16,
VMargin = 2,
Border = 2,
Width = 640,
Height = 480,
};
enum ChanSide {
ChanIn = 0,
ChanOut
};
typedef enum ChanSide ChanSide;
struct GEventChan {
int desc[2];
};
typedef struct GEventChan GEventChan, *GEventChanHandle;
static int InputThreadFn(void *ptr);
static GEventChanHandle GEventChanNew(void)
{
GEventChanHandle chan = calloc(1, sizeof(GEventChan));
if (chan) {
if (0 == pipe(chan->desc)) {
int flags;
flags = fcntl(chan->desc[ChanIn], F_GETFL, 0);
fcntl(chan->desc[ChanIn], F_SETFL, flags | O_NONBLOCK);
flags = fcntl(chan->desc[ChanOut], F_GETFL, 0);
fcntl(chan->desc[ChanOut], F_SETFL, flags | O_NONBLOCK);
} else {
free(chan);
chan = NULL;
}
}
return chan;
}
static void GEventChanKill(GEventChanHandle chan)
{
assert(chan);
if (chan) {
close(chan->desc[ChanIn]);
close(chan->desc[ChanOut]);
free(chan);
}
}
typedef ssize_t (*GEventChanOpFn)(int fd, void *buf, size_t count);
static int GEventChanOp(GEventChanHandle chan,
GEvent *pEvent,
GEventChanOpFn fn,
ChanSide side)
{
int rc = -1;
assert(chan);
assert(pEvent);
assert(fn);
if (chan && pEvent && fn) {
ssize_t er = fn(chan->desc[side], (void *)pEvent, sizeof(*pEvent));
if (er == sizeof(*pEvent))
rc = 0;
}
return rc;
}
static ssize_t GEventWriteFn(int fd, void *buf, size_t size)
{
return write(fd, buf, size);
}
static ssize_t GEventReadFn(int fd, void *buf, size_t size)
{
return read(fd, buf, size);
}
static int GEventChanGet(GEventChanHandle chan, GEvent *pEvent)
{
return GEventChanOp(chan, pEvent, GEventReadFn, ChanIn);
}
static int GEventChanPut(GEventChanHandle chan, GEvent *pEvent)
{
return GEventChanOp(chan, pEvent, GEventWriteFn, ChanOut);
}
struct GSdlContext {
SDL_Window *pWindow;
SDL_Renderer *pRenderer;
SDL_Surface *pSurface;
TTF_Font *pFont;
SDL_Thread *pThread;
GEventChanHandle chan;
SDL_mutex *pMutex;
SDL_Cursor *pCursors[2];
int width;
int height;
int border;
volatile int needExit;
int ctrl;
int move;
};
typedef struct GSdlContext GSdlContext, *GSdlContextHandle;
static GSdlContextHandle globalContext = NULL;
static GSdlContextHandle GSdlContextNew(int width, int height, int border,
const char *font, int fontSize)
{
GSdlContextHandle cont = calloc(1, sizeof(*cont));
if (cont) {
if (0 != SDL_Init(SDL_INIT_EVERYTHING))
die("cannot init SDL");
if (0 != TTF_Init())
die("cannot init font renderer");
cont->width = width;
cont->height = height;
cont->border = border;
cont->pWindow = SDL_CreateWindow("ED", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
width, height, SDL_WINDOW_SHOWN);
if (!cont->pWindow)
die("cannot create window");
SDL_SetWindowResizable(cont->pWindow, SDL_TRUE);
cont->pRenderer = SDL_CreateRenderer(cont->pWindow, -1,
SDL_RENDERER_SOFTWARE);
if (!cont->pRenderer)
die("cannot create renderer");
cont->pSurface = SDL_CreateRGBSurface(0, width, height, 32,
SURFACE32_LE_RMASK,
SURFACE32_LE_GMASK,
SURFACE32_LE_BMASK,
SURFACE32_LE_AMASK);
if (!cont->pSurface)
die("cannot create offscreen surface");
cont->pFont = TTF_OpenFont(font, fontSize);
if (!cont->pFont)
die("cannot load font file");
cont->chan = GEventChanNew();
if (!cont->chan)
die("cannot create event chan");
assert(GPNormal < 2);
assert(GPResize < 2);
cont->pCursors[GPNormal] =
SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW);
cont->pCursors[GPResize] =
SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_HAND);
if (!cont->pCursors[GPNormal] ||
!cont->pCursors[GPResize])
die("cannot create cursors");
cont->pMutex = SDL_CreateMutex();
if (!cont->pMutex)
die("cannot create mutex");
SDL_StartTextInput();
cont->pThread = SDL_CreateThread(InputThreadFn,
"Input Thread", (void *)cont);
if (!cont->pThread)
die("cannot create input thread");
}
return cont;
}
static void GSdlContextKill(GSdlContextHandle cont)
{
assert(cont);
if (cont) {
int status;
cont->needExit = 1;
SDL_CompilerBarrier();
SDL_WaitThread(cont->pThread, &status);
SDL_StopTextInput();
if (cont->pMutex)
SDL_DestroyMutex(cont->pMutex);
if (cont->pCursors[GPNormal])
SDL_FreeCursor(cont->pCursors[GPNormal]);
if (cont->pCursors[GPResize])
SDL_FreeCursor(cont->pCursors[GPResize]);
if (cont->chan)
GEventChanKill(cont->chan);
if (cont->pFont)
TTF_CloseFont(cont->pFont);
if (cont->pSurface)
SDL_FreeSurface(cont->pSurface);
if (cont->pRenderer)
SDL_DestroyRenderer(cont->pRenderer);
if (cont->pWindow)
SDL_DestroyWindow(cont->pWindow);
free(cont);
}
}
static int GSdlResizeSurface(GSdlContextHandle cont)
{
assert(cont);
assert(cont->pSurface);
int rc = -1;
if (cont && cont->pSurface) {
SDL_FreeSurface(cont->pSurface);
cont->pSurface = SDL_CreateRGBSurface(0,
cont->width,
cont->height,
32,
SURFACE32_LE_RMASK,
SURFACE32_LE_GMASK,
SURFACE32_LE_BMASK,
SURFACE32_LE_AMASK);
if (cont->pSurface)
rc = 0;
}
return rc;
}
static void HandleInput(GSdlContextHandle cont)
{
SDL_Event event;
assert(cont);
if (SDL_WaitEventTimeout(&event, 100)) {
GEvent gev;
switch (event.type) {
case SDL_QUIT:
exit(0);
break;
case SDL_TEXTINPUT:
if (SDL_strlen(event.text.text)) {
/* UTF8 invariant check. */
assert(SDL_strlen(event.text.text) < 5);
gev.type = GKey;
utf8_decode_rune(&gev.key,
(unsigned char *)event.text.text,
SDL_strlen(event.text.text));
GEventChanPut(cont->chan, &gev);
}
break;
case SDL_WINDOWEVENT:
switch (event.window.event) {
SDL_Texture *pTexture;
case SDL_WINDOWEVENT_EXPOSED:
SDL_LockMutex(cont->pMutex);
/*
* TODO: Do not allocate the texture on
* every EXPOSED event. Allocate the
* texture when renderer is created,
* then reallocate it only on the window
* resize event.
*/
pTexture = SDL_CreateTextureFromSurface(
globalContext->pRenderer,
globalContext->pSurface
);
assert(pTexture);
if (pTexture) {
/*
* TODO: One more point for optimization.
* Here we copy the whole texture to the
* window's surface. Instead we can maintain
* information about damaged areas of the
* window and update only these areas.
*/
SDL_RenderCopy(globalContext->pRenderer,
pTexture, 0, 0);
SDL_DestroyTexture(pTexture);
}
SDL_RenderPresent(cont->pRenderer);
SDL_UnlockMutex(cont->pMutex);
break;
case SDL_WINDOWEVENT_RESIZED:
SDL_LockMutex(cont->pMutex);
cont->width = event.window.data1;
cont->height = event.window.data2;
SDL_SetWindowSize(cont->pWindow,
cont->width,
cont->height);
/* TODO: Check runtime errors properly. */
GSdlResizeSurface(cont);
SDL_UnlockMutex(cont->pMutex);
gev.type = GResize;
gev.resize.width = cont->width;
gev.resize.height = cont->height;
GEventChanPut(cont->chan, &gev);
break;
case SDL_WINDOWEVENT_SHOWN:
gev.type = GResize;
gev.resize.width = cont->width;
gev.resize.height = cont->height;
GEventChanPut(cont->chan, &gev);
break;
}
break;
case SDL_MOUSEBUTTONUP:
gev.type = GMouseUp;
if (SDL_BUTTON_LEFT == event.button.button)
cont->move = 0;
GEventChanPut(cont->chan, &gev);
break;
case SDL_MOUSEBUTTONDOWN:
gev.type = GMouseDown;
switch (event.button.button) {
case SDL_BUTTON_LEFT:
gev.mouse.button = GBLeft;
cont->move = 1;
break;
case SDL_BUTTON_RIGHT:
gev.mouse.button = GBRight;
break;
case SDL_BUTTON_MIDDLE:
gev.mouse.button = GBMiddle;
break;
/* TODO: handle mouse wheel properly. */
case SDL_BUTTON_X1:
gev.mouse.button = GBWheelUp;
break;
case SDL_BUTTON_X2:
gev.mouse.button = GBWheelDown;
break;
}
gev.mouse.x = event.button.x;
gev.mouse.y = event.button.y;
GEventChanPut(cont->chan, &gev);
break;
case SDL_MOUSEMOTION:
if (cont->move) {
gev.type = GMouseSelect;
gev.mouse.button = GBLeft;
gev.mouse.x = event.motion.x;
gev.mouse.y = event.motion.y;
GEventChanPut(cont->chan, &gev);
}
break;
case SDL_KEYUP:
if (event.key.keysym.sym == SDLK_LCTRL ||
event.key.keysym.sym == SDLK_RCTRL) {
cont->ctrl = 0;
}
break;
case SDL_KEYDOWN:
gev.type = GKey;
switch (event.key.keysym.sym) {
case SDLK_ESCAPE:
gev.key = GKEsc;
GEventChanPut(cont->chan, &gev);
break;
case SDLK_F1:
case SDLK_F2:
case SDLK_F3:
case SDLK_F4:
case SDLK_F5:
case SDLK_F6:
case SDLK_F7:
case SDLK_F8:
case SDLK_F9:
case SDLK_F10:
case SDLK_F11:
case SDLK_F12:
gev.key = GKF1 + (event.key.keysym.sym - SDLK_F1);
GEventChanPut(cont->chan, &gev);
break;
case SDLK_UP:
gev.key = GKUp;
GEventChanPut(cont->chan, &gev);
break;
case SDLK_DOWN:
gev.key = GKDown;
GEventChanPut(cont->chan, &gev);
break;
case SDLK_LEFT:
gev.key = GKLeft;
GEventChanPut(cont->chan, &gev);
break;
case SDLK_RIGHT:
gev.key = GKRight;
GEventChanPut(cont->chan, &gev);
break;
case SDLK_BACKSPACE:
gev.key = GKBackspace;
GEventChanPut(cont->chan, &gev);
break;
case SDLK_PAGEUP:
gev.key = GKPageUp;
GEventChanPut(cont->chan, &gev);
break;
case SDLK_PAGEDOWN:
gev.key = GKPageDown;
GEventChanPut(cont->chan, &gev);
break;
case SDLK_RETURN:
gev.key = '\n';
GEventChanPut(cont->chan, &gev);
break;
case SDLK_TAB:
gev.key = '\t';
GEventChanPut(cont->chan, &gev);
break;
case SDLK_LCTRL:
case SDLK_RCTRL:
cont->ctrl = 1;
break;
default:
/*
* Handle non-text keys. If we press some key with
* a modifier key, SDL gets two events instead of
* one text input event. So, we get LCTRL/RCTRL and
* then SDLK_<something>, where <something> is a
* letter key.
*/
if (cont->ctrl &&
event.key.keysym.sym >= SDLK_a &&
event.key.keysym.sym <= SDLK_z) {
/* Translate the key code. */
gev.key = 1 + event.key.keysym.sym - SDLK_a;
GEventChanPut(cont->chan, &gev);
}
}
break;
}
}
}
static int InputThreadFn(void *pArg)
{
GSdlContextHandle cont = (GSdlContextHandle) pArg;
assert(cont);
if (cont) {
while (1) {
HandleInput(cont);
if (cont->needExit)
break;
}
}
return 0;
}
static int GSdlInit(void)
{
globalContext = GSdlContextNew(Width, Height, Border, FONT_FILE, FONT_SIZE);
if (!globalContext)
die("cannot create global SDL context");
gui_sdl.actionr.w = HMargin - 3;
gui_sdl.actionr.h = VMargin + TTF_FontHeight(globalContext->pFont);
return globalContext->chan->desc[ChanIn];
}
static void GSdlDeinit(void)
{
assert(globalContext);
if (globalContext) {
GSdlContextKill(globalContext);
SDL_Quit();
}
}
static Uint32 getPixel32(SDL_Surface *pSurface, int x, int y)
{
Uint32 *pixels = (Uint32 *)pSurface->pixels;
return pixels[y * pSurface->w + x];
}
static void setPixel32(SDL_Surface *pSurface, int x, int y,
Uint32 color)
{
Uint32 *pixels = (Uint32 *)pSurface->pixels;
pixels[y * pSurface->w + x] = color;
}
static void GSdlInvertRect(int x, int y, int w, int h)
{
int i, j;
SDL_LockSurface(globalContext->pSurface);
for (j = y; j < y + h; ++j) {
for (i = x; i < x + w; ++i) {
Uint8 or, og, ob;
Uint32 ocolor;
Uint32 ncolor;
ocolor = getPixel32(globalContext->pSurface, i, j);
SDL_GetRGB(ocolor, globalContext->pSurface->format, &or, &og, &ob);
ncolor = SDL_MapRGB(
globalContext->pSurface->format,
255 - or, 255 - og, 255 - ob
);
setPixel32(globalContext->pSurface, i, j, ncolor);
}
}
SDL_UnlockSurface(globalContext->pSurface);
}
static void GSdlDrawRect(GRect *clip, int x, int y, int w, int h, GColor c)
{
SDL_Rect rect;
assert(globalContext);
assert(globalContext->pRenderer);
assert(globalContext->pMutex);
if (x + w > clip->w)
w = clip->w - x;
if (y + h > clip->h)
h = clip->h - y;
x += clip->x;
y += clip->y;
rect.x = x;
rect.y = y;
rect.w = w;
rect.h = h;
SDL_LockMutex(globalContext->pMutex);
assert(globalContext->pSurface);
if (c.x) {
GSdlInvertRect(x, y, w, h);
} else
SDL_FillRect(globalContext->pSurface, &rect,
SDL_MapRGB(
globalContext->pSurface->format,
c.red, c.green, c.blue
));
SDL_UnlockMutex(globalContext->pMutex);
}
static void GSdlDrawCursor(GRect *clip, int insert, int x, int y, int w)
{
assert(globalContext);
assert(globalContext->pFont);
if (insert)
GSdlDrawRect(clip, x, y, 2,
TTF_FontHeight(globalContext->pFont), GXBlack);
else
GSdlDrawRect(clip, x, y, w,
TTF_FontHeight(globalContext->pFont), GXBlack);
}
static void GSdlGetFont(GFont *ret)
{
assert(globalContext);
assert(globalContext->pFont);
ret->ascent = TTF_FontAscent(globalContext->pFont);
ret->descent = -TTF_FontDescent(globalContext->pFont) + 1;
ret->height = TTF_FontHeight(globalContext->pFont);
}
/*
* NOTE: allocates a new Unicode16 string and returns it to the
* requester.
*/
static uint16_t *RunesToUnicode16(Rune *str, int len)
{
uint16_t *text = calloc(len + 1, sizeof(*text));
assert(text);
if (text) {
int i;
for (i = 0; i < len; ++i)
text[i] = (uint16_t)str[i];
text[len] = 0;
}
return text;
}
static int Unicode16TextWidth(uint16_t *text, int len)
{
int rc, w = 0, h;
assert(globalContext);
assert(globalContext->pFont);
assert(text);
if (text && len) {
rc = TTF_SizeUNICODE(globalContext->pFont, text, &w, &h);
assert(0 == rc);
}
return w;
}
static int GSdlTextWidth(Rune *str, int len)
{
uint16_t *text = RunesToUnicode16(str, len);
int w = 0;
assert(text);
if (text) {
w = Unicode16TextWidth(text, len);
free(text);
}
return w;
}
static void GSdlDrawText(GRect *clip, Rune *str,
int len, int x, int y, GColor c)
{
SDL_Color color = { c.red, c.green, c.blue, 255 };
SDL_Surface *pSurface;
uint16_t *text = RunesToUnicode16(str, len);
assert(globalContext);
assert(globalContext->pFont);
assert(globalContext->pRenderer);
assert(globalContext->pMutex);
assert(text);
if (text) {
x += clip->x;
y += clip->y;
y -= TTF_FontAscent(globalContext->pFont);
if (0 != Unicode16TextWidth(text, len)) {
pSurface = TTF_RenderUNICODE_Blended(globalContext->pFont,
text, color);
assert(pSurface);
if (pSurface) {
SDL_LockMutex(globalContext->pMutex);
{
SDL_Rect sourceRect = {
.x = 0,
.y = 0,
.w = pSurface->w,
.h = pSurface->h,
};
SDL_Rect destRect = {
.x = x,
.y = y,
.w = pSurface->w,
.h = pSurface->h,
};
int rc = SDL_BlitSurface(pSurface,
&sourceRect,
globalContext->pSurface,
&destRect);
if (0 != rc)
die("cannot blit surface");
}
SDL_UnlockMutex(globalContext->pMutex);
SDL_FreeSurface(pSurface);
}
}
free(text);
}
}
static int GSdlNextEvent(GEvent *ev)
{
int er, rc = 0;
assert(globalContext);
assert(globalContext->chan);
er = GEventChanGet(globalContext->chan, ev);
if (0 == er)
rc = 1;
return rc;
}
static int GSdlSync(void)
{
SDL_Event event;
event.type = SDL_WINDOWEVENT;
event.window.event = SDL_WINDOWEVENT_EXPOSED;
SDL_PushEvent(&event);
return 0;
}
static void GSdlSetPointer(GPointer pt)
{
assert(globalContext);
assert(GPNormal == pt || GPResize == pt);
assert(globalContext->pCursors[pt]);
SDL_SetCursor(globalContext->pCursors[pt]);
}
static void GSdlDecorate(GRect *clip, int dirty, GColor c)
{
assert(globalContext);
assert(globalContext->pFont);
int boxh = VMargin + TTF_FontHeight(globalContext->pFont);
GSdlDrawRect(clip, HMargin-3, 0, 1, clip->h, c);
GSdlDrawRect(clip, 0, boxh, HMargin-3, 1, c);
if (dirty)
GSdlDrawRect(clip, 2, 2, HMargin-7, boxh-4, c);
}
struct gui gui_sdl = {
.init = GSdlInit,
.fini = GSdlDeinit,
.sync = GSdlSync,
.decorate = GSdlDecorate,
.drawrect = GSdlDrawRect,
.drawcursor = GSdlDrawCursor,
.drawtext = GSdlDrawText,
.getfont = GSdlGetFont,
.nextevent = GSdlNextEvent,
.setpointer = GSdlSetPointer,
.textwidth = GSdlTextWidth,
.hmargin = HMargin,
.vmargin = VMargin,
.border = Border,
.actionr = {0, 0, 0, 0},
};