-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.c
2900 lines (2594 loc) · 81.1 KB
/
main.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
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
yagears Yet Another Gears OpenGL / Vulkan demo
Copyright (C) 2013-2024 Nicolas Caramelli
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "config.h"
#include <math.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>
#if defined(GL_X11)
#include <GL/glx.h>
#endif
#if defined(GL_DIRECTFB)
#include <directfbgl.h>
#endif
#if defined(GL_FBDEV)
#include <dirent.h>
#include <fcntl.h>
#include <linux/input.h>
#include <sys/mman.h>
#include <GL/glfbdev.h>
#endif
#if defined(EGL_X11)
#include <X11/Xutil.h>
#endif
#if defined(EGL_DIRECTFB)
#include <directfb.h>
#endif
#if defined(EGL_FBDEV)
#include <dirent.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <linux/input.h>
#endif
#if defined(EGL_WAYLAND)
#include <sys/mman.h>
#include <wayland-egl.h>
#include <xkbcommon/xkbcommon.h>
#endif
#if defined(EGL_XCB)
#include <xcb/xcb.h>
#endif
#if defined(EGL_DRM)
#include <dirent.h>
#include <fcntl.h>
#include <gbm.h>
#include <libevdev/libevdev.h>
#include <xf86drm.h>
#include <xf86drmMode.h>
#ifdef HAVE_DRI
#include <dlfcn.h>
#include <limits.h>
struct __DRIextensionRec {
char *name;
int version;
};
struct __DRIdrawableRec;
struct __DRIimageList;
struct __DRIimageLoaderExtensionRec {
struct __DRIextensionRec base;
int (*getBuffers)(struct __DRIdrawableRec *, unsigned int, unsigned int *, void *, unsigned int, struct __DRIimageList *);
};
struct __DRIscreenRec;
struct __DRIconfigRec;
struct __DRIcoreExtensionRec {
struct __DRIextensionRec base;
struct __DRIscreenRec *(*createNewScreen)(int, int, unsigned int, struct __DRIextensionRec **, struct __DRIconfigRec ***, void *);
void (*destroyScreen)(struct __DRIscreenRec *);
struct __DRIextensionRec **(*getExtensions)(struct __DRIscreenRec *);
};
struct __DRIcontextRec;
struct __DRIbufferRec;
struct __DRIdri2ExtensionRec {
struct __DRIextensionRec base;
struct __DRIscreenRec *(*createNewScreen)(int, int, struct __DRIextensionRec **, struct __DRIconfigRec ***, void *);
struct __DRIdrawableRec *(*createNewDrawable)(struct __DRIscreenRec *, struct __DRIconfigRec *, void *);
struct __DRIcontextRec *(*createNewContext)(struct __DRIscreenRec *, struct __DRIconfigRec *, struct __DRIcontextRec *, void *);
unsigned int (*getAPIMask)(struct __DRIscreenRec *);
struct __DRIcontextRec *(*createNewContextForAPI)(struct __DRIscreenRec *, int, struct __DRIconfigRec *, struct __DRIcontextRec *, void *);
struct __DRIbufferRec *(*allocateBuffer)(struct __DRIscreenRec *, unsigned int, unsigned int, int, int);
void (*releaseBuffer)(struct __DRIscreenRec *, struct __DRIbufferRec *);
struct __DRIcontextRec *(*createContextAttribs)(struct __DRIscreenRec *, int, struct __DRIconfigRec *, struct __DRIcontextRec *, unsigned int, unsigned int *, unsigned int *, void *);
struct __DRIscreenRec *(*createNewScreen2)(int, int, struct __DRIextensionRec **, struct __DRIextensionRec **, struct __DRIconfigRec ***, void *);
};
struct __DRIimageRec;
struct __DRIimageExtensionRec {
struct __DRIextensionRec base;
struct __DRIimageRec *(*createImageFromName)(struct __DRIscreenRec *, int, int, int, int, int, void *);
struct __DRIimageRec *(*createImageFromRenderbuffer)(struct __DRIcontextRec *, int, void *);
void (*destroyImage)(struct __DRIimageRec *);
struct __DRIimageRec *(*createImage)(struct __DRIscreenRec *, int, int, int, unsigned int, void *);
int (*queryImage)(struct __DRIimageRec *, int, int *);
};
#endif
#endif
#if defined(EGL_RPI)
#include <bcm_host.h>
#include <fcntl.h>
#include <termios.h>
#endif
#if defined(EGL_X11) || defined(EGL_DIRECTFB) || defined(EGL_FBDEV) || defined(EGL_WAYLAND) || defined(EGL_XCB) || defined(EGL_DRM) || defined(EGL_RPI)
#include <EGL/egl.h>
#endif
#if defined(EGL_X11) || defined(EGL_DIRECTFB) || defined(EGL_FBDEV) || defined(EGL_WAYLAND) || defined(EGL_XCB) || defined(EGL_DRM)
#include <EGL/eglext.h>
#endif
#if defined(WAFFLE)
#include <fcntl.h>
#include <libinput.h>
#include <linux/input.h>
#include <waffle.h>
#endif
#include "gears_engine.h"
#if !defined(ENGINE_CTOR) && defined(YAGEARS_ENGINE)
#define stringify_engine_ctor(name) name##_engine_ctor()
#define engine_ctor(name) stringify_engine_ctor(name)
#define stringify_engine_ctor_proto(name) void name##_engine_ctor(void)
#define engine_ctor_proto(name) stringify_engine_ctor_proto(name)
engine_ctor_proto(YAGEARS_ENGINE);
#endif
#if defined(YAGEARS_BACKEND) && defined(YAGEARS_ENGINE)
#define XSTRINGIFY(x) #x
#define STRINGIFY(x) XSTRINGIFY(x)
#endif
/******************************************************************************/
static char *backend = NULL;
static gears_engine_t *gears_engine = NULL;
static int loop = 1, animate = 1, redisplay = 1, win_width = 0, win_height = 0, win_posx = 0, win_posy = 0;
static float fps = 0, view_tz = -40.0, view_rx = 20.0, view_ry = 30.0, model_rz = 210.0;
/******************************************************************************/
static void sighandler(int signum)
{
/* benchmark */
if (fps) {
fps /= (loop - 1);
printf("Gears demo: %.2f fps\n", fps);
}
loop = 0;
}
/******************************************************************************/
#if defined(GL_X11) || defined(EGL_X11)
static void x11_keyboard_handle_key(XEvent *event)
{
switch (XLookupKeysym(&event->xkey, 0)) {
case XK_Escape:
sighandler(SIGTERM);
return;
case XK_space:
animate = !animate;
if (animate) {
redisplay = 1;
}
else {
redisplay = 0;
}
return;
case XK_Page_Down:
view_tz -= -5.0;
break;
case XK_Page_Up:
view_tz += -5.0;
break;
case XK_Down:
view_rx -= 5.0;
break;
case XK_Up:
view_rx += 5.0;
break;
case XK_Right:
view_ry -= 5.0;
break;
case XK_Left:
view_ry += 5.0;
break;
case XK_t:
if (getenv("NO_TEXTURE")) {
unsetenv("NO_TEXTURE");
}
else {
setenv("NO_TEXTURE", "1", 1);
}
break;
default:
return;
}
if (!animate) {
redisplay = 1;
}
}
#endif
#if defined(GL_DIRECTFB) || defined(EGL_DIRECTFB)
static void dfb_keyboard_handle_key(DFBWindowEvent *event)
{
switch (event->key_symbol) {
case DIKS_ESCAPE:
sighandler(SIGTERM);
return;
case DIKS_SPACE:
animate = !animate;
if (animate) {
redisplay = 1;
}
else {
redisplay = 0;
}
return;
case DIKS_PAGE_DOWN:
view_tz -= -5.0;
break;
case DIKS_PAGE_UP:
view_tz += -5.0;
break;
case DIKS_CURSOR_DOWN:
view_rx -= 5.0;
break;
case DIKS_CURSOR_UP:
view_rx += 5.0;
break;
case DIKS_CURSOR_RIGHT:
view_ry -= 5.0;
break;
case DIKS_CURSOR_LEFT:
view_ry += 5.0;
break;
case DIKS_SMALL_T:
if (getenv("NO_TEXTURE")) {
unsetenv("NO_TEXTURE");
}
else {
setenv("NO_TEXTURE", "1", 1);
}
break;
default:
return;
}
if (!animate) {
redisplay = 1;
}
}
#endif
#if defined(GL_FBDEV) || defined(EGL_FBDEV)
struct fb_window {
int width;
int height;
int posx;
int posy;
};
static void fb_keyboard_handle_key(struct input_event *event)
{
switch (event->code) {
case KEY_ESC:
sighandler(SIGTERM);
return;
case KEY_SPACE:
animate = !animate;
if (animate) {
redisplay = 1;
}
else {
redisplay = 0;
}
return;
case KEY_PAGEDOWN:
view_tz -= -5.0;
break;
case KEY_PAGEUP:
view_tz += -5.0;
break;
case KEY_DOWN:
view_rx -= 5.0;
break;
case KEY_UP:
view_rx += 5.0;
break;
case KEY_RIGHT:
view_ry -= 5.0;
break;
case KEY_LEFT:
view_ry += 5.0;
break;
case KEY_T:
if (getenv("NO_TEXTURE")) {
unsetenv("NO_TEXTURE");
}
else {
setenv("NO_TEXTURE", "1", 1);
}
break;
default:
return;
}
if (!animate) {
redisplay = 1;
}
}
#endif
#if defined(EGL_WAYLAND)
#define wl_window wl_egl_window
struct wl_window {
long version;
int width;
int height;
int dx;
int dy;
int attached_width;
int attached_height;
void *private;
void (*resize_callback)(struct wl_window *, void *);
void (*destroy_window_callback)(void *);
struct wl_surface *surface;
};
struct wl_data {
int width;
int height;
struct wl_registry *wl_registry;
struct wl_output *wl_output;
struct wl_compositor *wl_compositor;
struct wl_shell *wl_shell;
struct wl_seat *wl_seat;
struct wl_keyboard *wl_keyboard;
struct xkb_context *xkb_context;
struct xkb_keymap *xkb_keymap;
struct xkb_state *xkb_state;
};
static void wl_output_handle_geometry(void *data, struct wl_output *output, int x, int y, int physical_width, int physical_height, int subpixel, const char *make, const char *model, int transform)
{
}
static void wl_output_handle_mode(void *data, struct wl_output *output, unsigned int flags, int width, int height, int refresh)
{
struct wl_data *wl_data = data;
wl_data->width = width;
wl_data->height = height;
}
static void wl_output_handle_done(void *data, struct wl_output *output)
{
}
static void wl_output_handle_scale(void *data, struct wl_output *output, int factor)
{
}
static struct wl_output_listener wl_output_listener = { wl_output_handle_geometry, wl_output_handle_mode, wl_output_handle_done, wl_output_handle_scale };
static void wl_keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard, unsigned int format, int fd, unsigned int size)
{
struct wl_data *wl_data = data;
char *string;
string = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
wl_data->xkb_keymap = xkb_map_new_from_string(wl_data->xkb_context, string, XKB_KEYMAP_FORMAT_TEXT_V1, XKB_MAP_COMPILE_NO_FLAGS);
munmap(string, size);
close(fd);
wl_data->xkb_state = xkb_state_new(wl_data->xkb_keymap);
}
static void wl_keyboard_handle_enter(void *data, struct wl_keyboard *keyboard, unsigned int serial, struct wl_surface *surface, struct wl_array *keys)
{
}
static void wl_keyboard_handle_leave(void *data, struct wl_keyboard *keyboard, unsigned int serial, struct wl_surface *surface)
{
}
static void wl_keyboard_handle_key(void *data, struct wl_keyboard *keyboard, unsigned int serial, unsigned int time, unsigned int key, unsigned int state)
{
struct wl_data *wl_data = data;
const xkb_keysym_t *syms;
if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
return;
xkb_key_get_syms(wl_data->xkb_state, key + 8, &syms);
switch (syms[0]) {
case XKB_KEY_Escape:
sighandler(SIGTERM);
return;
case XKB_KEY_space:
animate = !animate;
if (animate) {
redisplay = 1;
}
else {
redisplay = 0;
}
return;
case XKB_KEY_Page_Down:
view_tz -= -5.0;
break;
case XKB_KEY_Page_Up:
view_tz += -5.0;
break;
case XKB_KEY_Down:
view_rx -= 5.0;
break;
case XKB_KEY_Up:
view_rx += 5.0;
break;
case XKB_KEY_Right:
view_ry -= 5.0;
break;
case XKB_KEY_Left:
view_ry += 5.0;
break;
case XKB_KEY_t:
if (getenv("NO_TEXTURE")) {
unsetenv("NO_TEXTURE");
}
else {
setenv("NO_TEXTURE", "1", 1);
}
break;
default:
return;
}
if (!animate) {
redisplay = 1;
}
}
static void wl_keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard, unsigned int serial, unsigned int mods_depressed, unsigned int mods_latched, unsigned int mods_locked, unsigned int group)
{
}
static struct wl_keyboard_listener wl_keyboard_listener = { wl_keyboard_handle_keymap, wl_keyboard_handle_enter, wl_keyboard_handle_leave, wl_keyboard_handle_key, wl_keyboard_handle_modifiers };
static void wl_seat_handle_capabilities(void *data, struct wl_seat *seat, enum wl_seat_capability caps)
{
struct wl_data *wl_data = data;
wl_data->wl_keyboard = wl_seat_get_keyboard(wl_data->wl_seat);
wl_keyboard_add_listener(wl_data->wl_keyboard, &wl_keyboard_listener, wl_data);
}
static void wl_seat_handle_name(void *data, struct wl_seat *seat, const char *name)
{
}
static struct wl_seat_listener wl_seat_listener = { wl_seat_handle_capabilities, wl_seat_handle_name };
static void wl_registry_handle_global(void *data, struct wl_registry *registry, unsigned int name, const char *interface, unsigned int version)
{
struct wl_data *wl_data = data;
if (!strcmp(interface, "wl_output")) {
wl_data->wl_output = wl_registry_bind(registry, name, &wl_output_interface, 1);
wl_output_add_listener(wl_data->wl_output, &wl_output_listener, wl_data);
}
else if (!strcmp(interface, "wl_compositor")) {
wl_data->wl_compositor = wl_registry_bind(registry, name, &wl_compositor_interface, 1);
}
else if (!strcmp(interface, "wl_shell")) {
wl_data->wl_shell = wl_registry_bind(registry, name, &wl_shell_interface, 1);
}
else if (!strcmp(interface, "wl_seat")) {
wl_data->wl_seat = wl_registry_bind(registry, name, &wl_seat_interface, 1);
wl_seat_add_listener(wl_data->wl_seat, &wl_seat_listener, wl_data);
}
}
static void wl_registry_handle_global_remove(void *data, struct wl_registry *registry, unsigned int name)
{
}
static struct wl_registry_listener wl_registry_listener = { wl_registry_handle_global, wl_registry_handle_global_remove };
#endif
#if defined(EGL_XCB)
static void xcb_keyboard_handle_key(xcb_generic_event_t *event)
{
switch (((xcb_key_press_event_t *)event)->detail) {
case 0x09:
sighandler(SIGTERM);
return;
case 0x41:
animate = !animate;
if (animate) {
redisplay = 1;
}
else {
redisplay = 0;
}
return;
case 0x75:
view_tz -= -5.0;
break;
case 0x70:
view_tz += -5.0;
break;
case 0x74:
view_rx -= 5.0;
break;
case 0x6f:
view_rx += 5.0;
break;
case 0x72:
view_ry -= 5.0;
break;
case 0x71:
view_ry += 5.0;
break;
case 0x1c:
if (getenv("NO_TEXTURE")) {
unsetenv("NO_TEXTURE");
}
else {
setenv("NO_TEXTURE", "1", 1);
}
break;
default:
return;
}
if (!animate) {
redisplay = 1;
}
}
#endif
#if defined(EGL_DRM)
#define drm_display gbm_device
#define drm_surface gbm_surface
#define drm_bo gbm_bo
#ifdef HAVE_DRI
struct drm_display {
struct drm_display *(*dummy)(int);
int fd;
char *name;
unsigned int refcount;
struct stat stat;
void (*destroy)(struct drm_display *);
int (*is_format_supported)(struct drm_display *, unsigned int, unsigned int);
struct drm_bo *(*bo_create)(struct drm_display *, unsigned int, unsigned int, unsigned int, unsigned int);
struct drm_bo *(*bo_import)(struct drm_display *, unsigned int, void *, unsigned int);
int (*bo_write)(struct drm_bo *, void *, size_t);
#if DRI_MAJOR_VERSION > 10 || (DRI_MAJOR_VERSION == 10 && DRI_MINOR_VERSION >= 2)
int (*bo_get_fd)(struct drm_bo *);
#endif
void (*bo_destroy)(struct drm_bo *);
struct drm_surface *(*surface_create)(struct drm_display *, unsigned int, unsigned int, unsigned int, unsigned int);
struct drm_bo *(*surface_lock_front_buffer)(struct drm_surface *);
void (*surface_release_buffer)(struct drm_surface *, struct drm_bo *);
int (*surface_has_free_buffers)(struct drm_surface *);
void (*surface_destroy)(struct drm_surface *);
int type;
char *driver_name;
void *driver;
struct __DRIscreenRec *screen;
struct __DRIcoreExtensionRec *core;
struct __DRIdri2ExtensionRec *dri2;
struct __DRIimageExtensionRec *image;
#if DRI_MAJOR_VERSION > 10 || (DRI_MAJOR_VERSION == 10 && DRI_MINOR_VERSION >= 3)
struct __DRIswrastExtensionRec *swrast;
#endif
struct __DRI2flushExtensionRec *flush;
struct __DRIdri2LoaderExtensionRec *loader;
struct __DRIconfigRec **driver_configs;
#if DRI_MAJOR_VERSION > 10 || (DRI_MAJOR_VERSION == 10 && DRI_MINOR_VERSION >= 2)
struct __DRIextensionRec **extensions;
#else
struct __DRIextensionRec *extensions[5];
#endif
struct __DRIextensionRec **driver_extensions;
struct __DRIimageRec *(*lookup_image)(struct __DRIscreenRec *, void *, void *);
void *lookup_user_data;
struct __DRIbufferRec *(*get_buffers)(struct __DRIdrawableRec *, int *, int *, unsigned int *, int, int *, void *);
void (*flush_front_buffer)(struct __DRIdrawableRec *, void *);
struct __DRIbufferRec *(*get_buffers_with_format)(struct __DRIdrawableRec *, int *, int *, unsigned int *, int, int *, void *);
int (*image_get_buffers)(struct __DRIdrawableRec *, unsigned int, unsigned int *, void *, unsigned int, struct __DRIimageList *);
#if DRI_MAJOR_VERSION > 10 || (DRI_MAJOR_VERSION == 10 && DRI_MINOR_VERSION >= 3)
void (*swrast_put_image2)(struct __DRIdrawableRec *, int, int, int, int, int, int, char *, void *);
void (*swrast_get_image)(struct __DRIdrawableRec *, int, int, int, int, char *, void *);
#endif
};
struct drm_surface {
struct drm_display *display;
unsigned int width;
unsigned int height;
unsigned int format;
unsigned int flags;
void *private;
};
struct drm_bo {
struct drm_display *display;
unsigned int width;
unsigned int height;
unsigned int stride;
unsigned int format;
unsigned long long handle;
void *user_data;
void (*destroy_user_data)(struct drm_bo *, void *);
struct __DRIimageRec *image;
};
static int image_get_buffers(struct __DRIdrawableRec *drawable, unsigned int format, unsigned int *stamp, void *loaderPrivate, unsigned int buffer_mask, struct __DRIimageList *buffers)
{
struct drm_surface *surface = loaderPrivate;
struct drm_display *display = surface->display;
return display->image_get_buffers(drawable, format, stamp, surface->private, buffer_mask, buffers);
}
static struct __DRIimageLoaderExtensionRec image_loader_extension = {
.base = { "DRI_IMAGE_LOADER", 1 },
.getBuffers = image_get_buffers,
};
static struct drm_bo *drm_bo_create(struct drm_display *display, unsigned int width, unsigned int height, unsigned int format, unsigned int usage)
{
struct drm_bo *bo;
bo = calloc(1, sizeof(struct drm_bo));
bo->display = display;
bo->width = width;
bo->height = height;
bo->format = format;
bo->image = display->image->createImage(display->screen, width, height, 0x1002, 2, bo);
display->image->queryImage(bo->image, 0x2000, (int *)&bo->stride);
display->image->queryImage(bo->image, 0x2001, (int *)&bo->handle);
return bo;
}
static void drm_bo_destroy(struct drm_bo *bo)
{
bo->display->image->destroyImage(bo->image);
free(bo);
}
static void drm_destroy_user_data(struct drm_bo *bo, void *data)
{
drmModeRmFB(bo->display->fd, (uintptr_t)data);
}
#endif
static void gbm_destroy_user_data(struct gbm_bo *bo, void *data)
{
drmModeRmFB(gbm_device_get_fd(gbm_bo_get_device(bo)), (uintptr_t)data);
}
static void drm_keyboard_handle_key(struct input_event *event)
{
switch (event->code) {
case KEY_ESC:
sighandler(SIGTERM);
return;
case KEY_SPACE:
animate = !animate;
if (animate) {
redisplay = 1;
}
else {
redisplay = 0;
}
return;
case KEY_PAGEDOWN:
view_tz -= -5.0;
break;
case KEY_PAGEUP:
view_tz += -5.0;
break;
case KEY_DOWN:
view_rx -= 5.0;
break;
case KEY_UP:
view_rx += 5.0;
break;
case KEY_RIGHT:
view_ry -= 5.0;
break;
case KEY_LEFT:
view_ry += 5.0;
break;
case KEY_T:
if (getenv("NO_TEXTURE")) {
unsetenv("NO_TEXTURE");
}
else {
setenv("NO_TEXTURE", "1", 1);
}
break;
default:
return;
}
if (!animate) {
redisplay = 1;
}
}
#endif
#if defined(EGL_RPI)
typedef struct {
DISPMANX_ELEMENT_HANDLE_T element;
int width;
int height;
} DISPMANX_WINDOW_T;
static void rpi_keyboard_handle_key(char *event)
{
if (event) {
if (!strcmp(event, "\x1b")) {
sighandler(SIGTERM);
return;
}
else if (!strcmp(event, "\x20")) {
animate = !animate;
if (animate) {
redisplay = 1;
}
else {
redisplay = 0;
}
return;
}
else if (!strcmp(event, "\x1b[6~")) {
view_tz -= -5.0;
}
else if (!strcmp(event, "\x1b[5~")) {
view_tz += -5.0;
}
else if (!strcmp(event,"\x1b[B")) {
view_rx -= 5.0;
}
else if (!strcmp(event,"\x1b[A")) {
view_rx += 5.0;
}
else if (!strcmp(event,"\x1b[C")) {
view_ry -= 5.0;
}
else if (!strcmp(event,"\x1b[D")) {
view_ry += 5.0;
}
else if (!strcmp(event, "\x74")) {
if (getenv("NO_TEXTURE")) {
unsetenv("NO_TEXTURE");
}
else {
setenv("NO_TEXTURE", "1", 1);
}
}
else {
return;
}
}
if (!animate) {
redisplay = 1;
}
}
#endif
#if defined(WAFFLE)
static void waffle_keyboard_handle_key(struct libinput_event_keyboard *event)
{
switch (libinput_event_keyboard_get_key(event)) {
case KEY_ESC:
sighandler(SIGTERM);
return;
case KEY_SPACE:
animate = !animate;
if (animate) {
redisplay = 1;
}
else {
redisplay = 0;
}
return;
case KEY_PAGEDOWN:
view_tz -= -5.0;
break;
case KEY_PAGEUP:
view_tz += -5.0;
break;
case KEY_DOWN:
view_rx -= 5.0;
break;
case KEY_UP:
view_rx += 5.0;
break;
case KEY_RIGHT:
view_ry -= 5.0;
break;
case KEY_LEFT:
view_ry += 5.0;
break;
case KEY_T:
if (getenv("NO_TEXTURE")) {
unsetenv("NO_TEXTURE");
}
else {
setenv("NO_TEXTURE", "1", 1);
}
break;
default:
return;
}
if (!animate) {
redisplay = 1;
}
}
static int waffle_input_handle_open(const char *path, int flags, void *user_data)
{
return open(path, flags);
}
static void waffle_input_handle_close(int fd, void *user_data)
{
close(fd);
}
static struct libinput_interface waffle_input_interface = { waffle_input_handle_open, waffle_input_handle_close };
#endif
/******************************************************************************/
int main(int argc, char *argv[])
{
int err = 0, ret = EXIT_FAILURE;
const char *backend_arg = NULL, *engine_arg = NULL;
char backends[64], *c;
int opt, t_rate = 0, t_rot = 0, t, frames = 0;
struct timeval tv;
#if defined(GL_X11) || defined(EGL_X11)
Display *x11_dpy = NULL;
Window x11_win = 0;
int x11_event_mask = NoEventMask;
XEvent x11_event;
#endif
#if defined(GL_X11)
XVisualInfo *x11_visual = NULL;
int x11_attr[5];
GLXContext x11_ctx = NULL;
int glx_major_version = 0, glx_minor_version = 0, glx_depth_size = 0, glx_red_size = 0, glx_green_size = 0, glx_blue_size = 0, glx_alpha_size = 0;
#endif
#if defined(GL_DIRECTFB) || defined(EGL_DIRECTFB)
IDirectFB *dfb_dpy = NULL;
IDirectFBSurface *dfb_win = NULL;
IDirectFBDisplayLayer *dfb_layer = NULL;
DFBDisplayLayerConfig dfb_layer_config;
DFBSurfaceCapabilities dfb_attr = DSCAPS_NONE;
DFBWindowDescription dfb_desc;
IDirectFBWindow *dfb_window = NULL;
IDirectFBEventBuffer *dfb_event_buffer = NULL;
DFBWindowEventType dfb_event_mask = DWET_ALL;
DFBWindowEvent dfb_event;
#endif
#if defined(GL_DIRECTFB)
IDirectFBGL *dfb_ctx = NULL;
DFBGLAttributes directfbgl;
#endif
#if defined(GL_FBDEV) || defined(EGL_FBDEV)
int fb_dpy = -1;
struct fb_window *fb_win = NULL;
struct fb_fix_screeninfo fb_finfo;
struct fb_var_screeninfo fb_vinfo;
int fb_keyboard = -1;
DIR *fb_input_dir = NULL;
struct dirent *fb_input_dev = NULL;
unsigned char fb_key_bits[(KEY_CNT - 1) / 8 + 1];
struct input_event fb_event;
#endif
#if defined(GL_FBDEV)
void *fb_addr = NULL;
GLFBDevVisualPtr fb_visual = NULL;
GLFBDevBufferPtr fb_buffer = NULL;
int fb_attr[4];
GLFBDevContextPtr fb_ctx = NULL;
int glfbdev_depth_size = 0;
#endif
#if defined(EGL_WAYLAND)
struct wl_display *wl_dpy = NULL;
struct wl_window *wl_win = NULL;
struct wl_data wl_data;
struct wl_surface *wl_surface = NULL;
struct wl_shell_surface *wl_shell_surface = NULL;
#endif
#if defined(EGL_XCB)
xcb_connection_t *xcb_dpy = NULL;
xcb_window_t xcb_win = -1;
xcb_void_cookie_t xcb_cookie;
uint32_t xcb_value_list[2];
xcb_event_mask_t xcb_event_mask = XCB_EVENT_MASK_NO_EVENT;
xcb_generic_event_t *xcb_event = NULL;
#endif
#if defined(EGL_DRM)
struct drm_display *drm_dpy = NULL;
struct drm_surface *drm_win = NULL;
int drm_fd = -1;
#ifdef HAVE_DRI
char drm_driver_path[PATH_MAX];
struct __DRIcoreExtensionRec **drm_driver_extensions = NULL;
struct __DRIextensionRec *drm_extensions[] = { &image_loader_extension.base, NULL };
#endif
drmModeResPtr drm_resources = NULL;
drmModeConnectorPtr drm_connector = NULL;
drmModeEncoderPtr drm_encoder = NULL;
drmModeCrtcPtr drm_crtc = NULL;
struct gbm_bo *drm_bo = NULL;
uint32_t drm_fb_id = 0;
drmEventContext drm_context = { DRM_EVENT_CONTEXT_VERSION, NULL, NULL };
int drm_keyboard = -1;
DIR *drm_input_dir = NULL;
struct dirent *drm_input_dev = NULL;
struct libevdev *drm_evdev = NULL;
struct input_event drm_event;
#endif
#if defined(EGL_RPI)
DISPMANX_DISPLAY_HANDLE_T rpi_dpy = DISPMANX_NO_HANDLE;
DISPMANX_WINDOW_T *rpi_win = NULL;
DISPMANX_MODEINFO_T rpi_info;
DISPMANX_UPDATE_HANDLE_T rpi_update = DISPMANX_NO_HANDLE;
DISPMANX_ELEMENT_HANDLE_T rpi_element = DISPMANX_NO_HANDLE;
VC_RECT_T rpi_dst_rect;
VC_RECT_T rpi_src_rect;
struct termios *rpi_termios = NULL, rpi_termios_new;
int rpi_fdflags = -1;
char rpi_event[5];
#endif
#if defined(EGL_X11) || defined(EGL_DIRECTFB) || defined(EGL_FBDEV) || defined(EGL_WAYLAND) || defined(EGL_XCB) || defined(EGL_DRM)
#ifdef EGL_EXT_platform_base
const char *egl_extension_name = NULL, *egl_extensions = NULL;
PFNEGLGETPLATFORMDISPLAYEXTPROC eglGetPlatformDisplayEXT = NULL;
PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC eglCreatePlatformWindowSurfaceEXT = NULL;
#endif
#endif
#if defined(EGL_X11) || defined(EGL_DIRECTFB) || defined(EGL_FBDEV) || defined(EGL_WAYLAND) || defined(EGL_XCB) || defined(EGL_DRM) || defined(EGL_RPI)
EGLDisplay egl_dpy = NULL;
EGLSurface egl_win = NULL;
EGLint egl_config_attr[16];
EGLint egl_configs_count = 0;
EGLConfig *egl_configs = NULL, egl_config = NULL;
EGLint egl_ctx_attr[3];
EGLContext egl_ctx = NULL;