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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
/* GATE PROJECT LICENSE:
+----------------------------------------------------------------------------+
| Copyright(c) 2018-2025, Stefan Meislinger                                  |
| All rights reserved.                                                       |
|                                                                            |
| Redistribution and use in source and binary forms, with or without         |
| modification, are permitted provided that the following conditions are met:|
|                                                                            |
| 1. Redistributions of source code must retain the above copyright notice,  |
|    this list of conditions and the following disclaimer.                   |
| 2. Redistributions in binary form must reproduce the above copyright       |
|    notice, this list of conditions and the following disclaimer in the     |
|    documentation and/or other materials provided with the distribution.    |
|                                                                            |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE  |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE    |
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR        |
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF       |
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS   |
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN    |
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)    |
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF     |
| THE POSSIBILITY OF SUCH DAMAGE.                                            |
+----------------------------------------------------------------------------+
*/

#include "gate/graphics/framebuffers.h"
#include "gate/atomics.h"
#include "gate/results.h"
#include "gate/times.h"

#if defined(GATE_SYS_WINSTORE)
#	define GATE_GRAPHICS_FRAMEBUFFERS_DIRECTX
#elif defined(GATE_SYS_WIN16)
#	define GATE_GRAPHICS_FRAMEBUFFERS_WIN16
#elif defined(GATE_SYS_WIN)
#	define GATE_GRAPHICS_FRAMEBUFFERS_WIN32
#elif defined(GATE_SYS_ANDROID) 
#	define GATE_GRAPHICS_FRAMEBUFFERS_GL
#elif defined(GATE_SYS_OPENBSD) || defined(GATE_SYS_BEOS)
#	define GATE_GRAPHICS_FRAMEBUFFERS_NOIMPL
#elif defined(GATE_SYS_DARWIN)
#	define GATE_GRAPHICS_FRAMEBUFFERS_NOIMPL
#elif defined(GATE_SYS_POSIX)
#	if defined(GATE_GRAPHICS_WAYLAND)
#		define GATE_GRAPHICS_FRAMEBUFFERS_WAYLAND 1
#	else
#		if defined(HAVE_X11_HEADERS)
#			define GATE_GRAPHICS_FRAMEBUFFERS_XLIB 1
#		else
#			define GATE_GRAPHICS_FRAMEBUFFERS_NOIMPL
#		endif
#	endif
#elif defined(GATE_SYS_EFI)
#	define GATE_GRAPHICS_FRAMEBUFFERS_UGA
#elif defined(GATE_SYS_DOS)
#	define GATE_GRAPHICS_FRAMEBUFFERS_INT10
#elif defined(GATE_SYS_WASM)
#	define GATE_GRAPHICS_FRAMEBUFFERS_WASM
#else
#	define GATE_GRAPHICS_FRAMEBUFFERS_NOIMPL
#endif



/**************************************************
 * generic rasterimage framebuffer implementation *
 **************************************************/

typedef struct rasterfb_impl
{
    GATE_INTERFACE_VTBL(gate_framebuffer) const* vtbl;

    gate_atomic_int_t	ref_counter;

    gate_rasterimage_t	image_header;

    gate_uint8_t		image_data[8];
} rasterfb_t;


static char const* rasterfb_get_interface_name(void* obj)
{
    GATE_UNUSED_ARG(obj);
    return GATE_INTERFACE_NAME_FRAMEBUFFER;
}
static void rasterfb_release(void* obj)
{
    rasterfb_t* const impl = (rasterfb_t*)obj;
    if (0 == gate_atomic_int_dec(&impl->ref_counter))
    {
        gate_mem_clear(impl, sizeof(rasterfb_t));
        gate_mem_dealloc(impl);
    }
}
static int rasterfb_retain(void* obj)
{
    rasterfb_t* const impl = (rasterfb_t*)obj;
    return gate_atomic_int_inc(&impl->ref_counter);
}

static gate_result_t rasterfb_get_info(void* obj, gate_framebuffer_info_t* ptr_info)
{
    rasterfb_t* const impl = (rasterfb_t*)obj;
    gate_result_t ret = GATE_RESULT_INVALIDARG;
    if (impl && ptr_info)
    {
        ptr_info->width = gate_rasterimage_width(&impl->image_header);
        ptr_info->height = gate_rasterimage_height(&impl->image_header);
        ptr_info->bits_per_pixel = 32;
        ptr_info->format_id = 0;
        ret = GATE_RESULT_OK;
    }
    return ret;
}
static gate_result_t rasterfb_get_property(void* obj, gate_string_t const* name, gate_property_t* prop)
{
    GATE_UNUSED_ARG(obj);
    GATE_UNUSED_ARG(name);
    GATE_UNUSED_ARG(prop);
    return GATE_RESULT_NOTSUPPORTED;
}
static gate_result_t rasterfb_set_property(void* obj, gate_string_t const* name, gate_property_t const* prop)
{
    GATE_UNUSED_ARG(obj);
    GATE_UNUSED_ARG(name);
    GATE_UNUSED_ARG(prop);
    return GATE_RESULT_NOTSUPPORTED;
}

static gate_result_t rasterfb_update(void* obj)
{
    /* rasterimage framebuffer is always up-to-date */
    GATE_UNUSED_ARG(obj);
    return GATE_RESULT_OK;
}

static gate_result_t rasterfb_get_pixel(void* obj, gate_uint32_t x, gate_uint32_t y, gate_color_t* ptr_col)
{
    rasterfb_t* const impl = (rasterfb_t*)obj;
    return gate_rasterimage_get_pixel(&impl->image_header, x, y, ptr_col);
}
static gate_result_t rasterfb_set_pixel(void* obj, gate_uint32_t x, gate_uint32_t y, gate_color_t col)
{
    rasterfb_t* const impl = (rasterfb_t*)obj;
    return gate_rasterimage_set_pixel(&impl->image_header, x, y, &col);
}

static gate_result_t rasterfb_get_image(void* obj, gate_uint32_t src_x, gate_uint32_t src_y, gate_uint32_t width, gate_uint32_t height, gate_rasterimage_t* ptr_image)
{
    rasterfb_t* const impl = (rasterfb_t*)obj;
    gate_result_t ret = GATE_RESULT_FAILED;
    gate_uint32_t fb_width = gate_rasterimage_width(&impl->image_header);
    gate_uint32_t fb_height = gate_rasterimage_height(&impl->image_header);

    if ((src_x == 0) && (src_y == 0) && (((width == 0) && (height == 0)) || ((width == fb_width) && (height == fb_height))))
    {
        if (gate_rasterimage_copy(ptr_image, &impl->image_header))
        {
            ret = GATE_RESULT_OK;
        }
    }
    else
    {
        do
        {
            if (!gate_rasterimage_create(ptr_image, GATE_IMAGE_PIXELFORMAT_BGR32, width, height, NULL))
            {
                ret = GATE_RESULT_OUTOFMEMORY;
                break;
            }
            ret = gate_rasterimage_paste_image(ptr_image, &impl->image_header, 0, 0, src_x, src_y, width, height, false);
            if (GATE_FAILED(ret))
            {
                gate_rasterimage_release(ptr_image);
            }
        } while (0);
    }
    return ret;
}

static gate_result_t rasterfb_set_image(void* obj, gate_rasterimage_t const* ptr_image, gate_uint32_t dest_x, gate_uint32_t dest_y)
{
    rasterfb_t* const impl = (rasterfb_t*)obj;
    gate_result_t ret = GATE_RESULT_FAILED;
    gate_uint32_t width = gate_rasterimage_width(ptr_image);
    gate_uint32_t height = gate_rasterimage_height(ptr_image);

    ret = gate_rasterimage_paste_image(&impl->image_header, ptr_image, dest_x, dest_y, 0, 0, width, height, false);
    return ret;
}

static gate_result_t rasterfb_await_event(void* obj, gate_uint32_t timeout_ms, gate_framebuffer_event_t* ptr_received_evt)
{
    GATE_UNUSED_ARG(obj);
    GATE_UNUSED_ARG(timeout_ms);
    GATE_UNUSED_ARG(ptr_received_evt);
    return GATE_RESULT_NOTSUPPORTED;
}

static GATE_INTERFACE_VTBL(gate_framebuffer) gate_rasterfb_vtbl;
static void gate_init_rasterfb_vtbl()
{
    if (!gate_rasterfb_vtbl.get_interface_name)
    {
        GATE_INTERFACE_VTBL(gate_framebuffer) const local_vtbl =
        {
            &rasterfb_get_interface_name,
            &rasterfb_release,
            &rasterfb_retain,

            &rasterfb_get_info,
            &rasterfb_get_property,
            &rasterfb_set_property,

            &rasterfb_update,

            &rasterfb_get_pixel,
            &rasterfb_set_pixel,

            &rasterfb_get_image,
            &rasterfb_set_image,

            &rasterfb_await_event
        };

        gate_rasterfb_vtbl = local_vtbl;
    }
}


gate_result_t gate_framebuffer_create_image(gate_uint32_t width, gate_uint32_t height, gate_framebuffer_t** ptr_framebuffer)
{
    rasterfb_t* impl = NULL;
    gate_result_t ret = GATE_RESULT_FAILED;
    gate_size_t datasize;

    do
    {
        datasize = sizeof(gate_color_t) * width * height;
        impl = (rasterfb_t*)gate_mem_alloc(sizeof(rasterfb_t) + datasize);
        if (!impl)
        {
            ret = GATE_RESULT_OUTOFMEMORY;
            break;
        }
        gate_init_rasterfb_vtbl();
        impl->vtbl = &gate_rasterfb_vtbl;
        gate_atomic_int_init(&impl->ref_counter, 1);

        impl->image_header.width = width;
        impl->image_header.height = height;
        impl->image_header.line_padding = 0;
        impl->image_header.bits_per_pixel = sizeof(gate_color_t) * 8;
        impl->image_header.data = &impl->image_data[0];
        impl->image_header.databuffer = NULL;

        gate_mem_clear(&impl->image_data[0], datasize);

        if (ptr_framebuffer)
        {
            *ptr_framebuffer = (gate_framebuffer_t*)impl;
            impl = NULL;
        }
        ret = GATE_RESULT_OK;
    } while (0);

    if (impl)
    {
        gate_mem_dealloc(impl);
    }
    return ret;
}



#if defined(GATE_GRAPHICS_FRAMEBUFFERS_DIRECTX)
#include "gate/graphics/platform/framebuffers_impl_directx.h"
#endif /* GATE_GRAPHICS_FRAMEBUFFERS_DIRECTX */


#if defined(GATE_GRAPHICS_FRAMEBUFFERS_WIN16)
#include "gate/graphics/platform/framebuffers_impl_win16.h"
#endif /* GATE_GRAPHICS_FRAMEBUFFERS_WIN16 */


#if defined(GATE_GRAPHICS_FRAMEBUFFERS_WIN32)
#include "gate/graphics/platform/framebuffers_impl_win32.h"
#endif /* GATE_GRAPHICS_FRAMEBUFFERS_WIN32 */


#if defined(GATE_GRAPHICS_FRAMEBUFFERS_WAYLAND)

#include "gate/graphics/platform/framebuffers_impl_wayland.h"
#include "gate/debugging.h"

#include "gate/graphics/framebuffers.h"
#include "gate/inputs.h"

#include <wayland-client.h>
#include <wayland-client-core.h>
#include <xdg-shell-client-protocol.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <sys/mman.h>
#include <xkbcommon/xkbcommon.h>

typedef struct waylandfb_impl
{
    GATE_INTERFACE_VTBL(gate_framebuffer) const* vtbl;

    gate_atomic_int_t	ref_counter;

    unsigned int		width;
    unsigned int		height;

    struct wl_display* display;
    struct wl_registry* registry;
    struct wl_compositor* compositor;
    struct wl_shm* shm;
    struct xdg_wm_base* wm_base;

    struct wl_surface* surface;
    struct xdg_surface* xdgsurface;
    struct xdg_toplevel* toplevel;

    struct wl_seat* seat;
    struct wl_keyboard* keyboard;
    struct xkb_context* kb_context;
    struct xkb_keymap* kb_keymap;
    struct xkb_state* kb_state;
    struct wl_pointer* pointer;

    gate_rasterimage_t		current_frame;

    gate_arraylist_t		events;

} waylandfb_t;

static void waylandfb_push_event(waylandfb_t* impl, gate_framebuffer_event_t const* evt)
{
    GATE_DEBUG_TRACE("Push event");
    gate_arraylist_add(impl->events, evt);
}

static gate_framebuffer_event_t const* waylandfb_get_event(waylandfb_t* impl)
{
    if (gate_arraylist_length(impl->events) == 0)
    {
        return NULL;
    }
    return (gate_framebuffer_event_t const*)gate_arraylist_get(impl->events, 0);
}

static void waylandfb_pop_event(waylandfb_t* impl)
{
    GATE_DEBUG_TRACE("Pop event");
    gate_arraylist_remove(impl->events, 0, 1);
}



/********************
 *  WM_BASE events  *
 ********************/

static void xdg_wm_base_ping(void* data, struct xdg_wm_base* xdg_wm_base, uint32_t serial)
{
    waylandfb_t* const impl = (waylandfb_t*)data;<--- Variable 'impl' is assigned a value that is never used.
    xdg_wm_base_pong(xdg_wm_base, serial);
}

static const struct xdg_wm_base_listener xdg_wm_base_listener =
{
    &xdg_wm_base_ping
};



/*********************
 *  KEYBOARD events  *
 *********************/

static void waylandfb_kb_map(void* data, struct wl_keyboard* kb, uint32_t frmt, int32_t fd, uint32_t sz)
{
    waylandfb_t* const impl = (waylandfb_t*)data;
    struct xkb_keymap* keymap = NULL;
    struct xkb_state* keystate = NULL;
    char* ptr_shared_map = MAP_FAILED;
    do
    {
        if (frmt == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1)
        {
            if (NULL == impl->kb_context)
            {
                impl->kb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
                if (NULL == impl->kb_context)
                {
                    /* error */
                    break;
                }
            }

            ptr_shared_map = mmap(NULL, sz, PROT_READ, MAP_PRIVATE, fd, 0);
            if (ptr_shared_map == MAP_FAILED)
            {
                /* error */
                break;
            }

            keymap = xkb_keymap_new_from_string(impl->kb_context, ptr_shared_map, XKB_KEYMAP_FORMAT_TEXT_V1, XKB_KEYMAP_COMPILE_NO_FLAGS);
            keystate = xkb_state_new(keymap);

            if (keymap && keystate)
            {
                /* attach new objects to framebuffer object */
                xkb_keymap_unref(impl->kb_keymap);
                impl->kb_keymap = keymap;
                keymap = NULL;

                xkb_state_unref(impl->kb_state);
                impl->kb_state = keystate;
                keystate = NULL;
            }
        }

    } while (0);

    /* cleanup */
    if (ptr_shared_map != MAP_FAILED)
    {
        munmap(ptr_shared_map, sz);
        close(fd);
    }
    xkb_keymap_unref(keymap);
    xkb_state_unref(keystate);
}

static void waylandfb_kb_enter(void* data, struct wl_keyboard* kb, uint32_t ser, struct wl_surface* srfc, struct wl_array* keys)
{
    /* surface has received keyboard focus */
    waylandfb_t* const impl = (waylandfb_t*)data;
    uint32_t* key;
    wl_array_for_each(key, keys)
    {
        xkb_keysym_t sym = xkb_state_key_get_one_sym(impl->kb_state, *key + 8);
        /* TODO: handle already pressed keys */
    }
}

static void waylandfb_kb_leave(void* data, struct wl_keyboard* kb, uint32_t ser, struct wl_surface* srfc)
{
    /* surface has lost keyboard focus */
    waylandfb_t* const impl = (waylandfb_t*)data;<--- Variable 'impl' is assigned a value that is never used.
}

static void waylandfb_kb_key(void* data, struct wl_keyboard* kb, uint32_t ser, uint32_t t, uint32_t key, uint32_t stat)
{
    /* receive keyboard key */
    waylandfb_t* const impl = (waylandfb_t*)data;

    uint32_t const keycode = key + 8;
    xkb_keysym_t const sym = xkb_state_key_get_one_sym(impl->kb_state, keycode);
    gate_bool_t const pressed = (stat == WL_KEYBOARD_KEY_STATE_PRESSED);
    uint32_t const chr32 = xkb_state_key_get_utf32(impl->kb_state, keycode);
    gate_framebuffer_event_t evt;

    evt.event_type = pressed ? GATE_FRAMEBUFFER_EVENT_KEY_DOWN : GATE_FRAMEBUFFER_EVENT_KEY_UP;
    evt.event_data.key.native_code = key;
    evt.event_data.key.key_states = 0; /* TODO */
    evt.event_data.key.key_code = chr32;
    evt.event_data.key.char_code = chr32;
    waylandfb_push_event(impl, &evt);
}

static void waylandfb_kb_mod(void* data, struct wl_keyboard* kb, uint32_t ser, uint32_t depressed, uint32_t latched, uint32_t locked, uint32_t group)
{
    waylandfb_t* const impl = (waylandfb_t*)data;
    if (impl && impl->kb_state)
    {
        xkb_state_update_mask(impl->kb_state, depressed, latched, locked, 0, 0, group);
    }
}

static void waylandfb_kb_rep(void* data, struct wl_keyboard* kb, int32_t rate, int32_t del)
{
    waylandfb_t* const impl = (waylandfb_t*)data;<--- Variable 'impl' is assigned a value that is never used.
}


static struct wl_keyboard_listener kb_listener =
{
    &waylandfb_kb_map,
    &waylandfb_kb_enter,
    &waylandfb_kb_leave,
    &waylandfb_kb_key,
    &waylandfb_kb_mod,
    &waylandfb_kb_rep
};



/***************************
 *  MOUSE / POINER events  *
 ***************************/

static void pointer_enter(void* data, struct wl_pointer* wl_pointer, uint32_t serial, struct wl_surface* surface, wl_fixed_t surface_x, wl_fixed_t surface_y)
{
    waylandfb_t* const impl = (waylandfb_t*)data;<--- Variable 'impl' is assigned a value that is never used.
}
static void pointer_leave(void* data, struct wl_pointer* wl_pointer, uint32_t serial, struct wl_surface* surface)
{
    waylandfb_t* const impl = (waylandfb_t*)data;<--- Variable 'impl' is assigned a value that is never used.
}
static void pointer_motion(void* data, struct wl_pointer* wl_pointer, uint32_t time, wl_fixed_t surface_x, wl_fixed_t surface_y)
{
    waylandfb_t* const impl = (waylandfb_t*)data;<--- Variable 'impl' is assigned a value that is never used.
}
static void pointer_button(void* data, struct wl_pointer* wl_pointer, uint32_t serial, uint32_t time, uint32_t button, uint32_t state)
{
    waylandfb_t* const impl = (waylandfb_t*)data;<--- Variable 'impl' is assigned a value that is never used.
}
static void pointer_axis(void* data, struct wl_pointer* wl_pointer, uint32_t time, uint32_t axis, wl_fixed_t value)
{
    waylandfb_t* const impl = (waylandfb_t*)data;<--- Variable 'impl' is assigned a value that is never used.
}
static void pointer_frame(void* data, struct wl_pointer* wl_pointer)
{
    waylandfb_t* const impl = (waylandfb_t*)data;<--- Variable 'impl' is assigned a value that is never used.
}
static void pointer_axis_source(void* data, struct wl_pointer* wl_pointer, uint32_t axis_source)
{
    waylandfb_t* const impl = (waylandfb_t*)data;<--- Variable 'impl' is assigned a value that is never used.
}
static void pointer_axis_stop(void* data, struct wl_pointer* wl_pointer, uint32_t time, uint32_t axis)
{
    waylandfb_t* const impl = (waylandfb_t*)data;<--- Variable 'impl' is assigned a value that is never used.
}
static void pointer_axis_discrete(void* data, struct wl_pointer* wl_pointer, uint32_t axis, int32_t discrete)
{
    waylandfb_t* const impl = (waylandfb_t*)data;<--- Variable 'impl' is assigned a value that is never used.
}
static void pointer_axis_value120(void* data, struct wl_pointer* wl_pointer, uint32_t axis, int32_t value120)
{
    waylandfb_t* const impl = (waylandfb_t*)data;<--- Variable 'impl' is assigned a value that is never used.
}


static struct wl_pointer_listener pointer_listener =
{
    &pointer_enter,
    &pointer_leave,
    &pointer_motion,
    &pointer_button,
    &pointer_axis,
    &pointer_frame,
    &pointer_axis_source,
    &pointer_axis_stop,
    &pointer_axis_discrete,
    &pointer_axis_value120
};



/*****************
 *  SEAT events  *
 *****************/

static void seat_cap(void* data, struct wl_seat* seat, uint32_t cap)
{
    waylandfb_t* const impl = (waylandfb_t*)data;

    if ((cap & WL_SEAT_CAPABILITY_KEYBOARD) && (impl->keyboard == NULL))
    {
        impl->keyboard = wl_seat_get_keyboard(seat);
        wl_keyboard_add_listener(impl->keyboard, &kb_listener, impl);
    }
    else if ((cap & WL_SEAT_CAPABILITY_POINTER) && (impl->pointer == NULL))
    {
        impl->pointer = wl_seat_get_pointer(seat);
        wl_pointer_add_listener(impl->pointer, &pointer_listener, impl);
    }
}

static void seat_name(void* data, struct wl_seat* seat, const char* name)
{
}

static struct wl_seat_listener seat_listener =
{
    &seat_cap,
    &seat_name
};


/*********************
 *  REGISTRY events  *
 *********************/

static void waylandfb_registry_global(void* data, struct wl_registry* wl_registry, uint32_t name, const char* interface, uint32_t version)
{
    waylandfb_t* const impl = (waylandfb_t*)data;

    if (gate_str_comp(interface, wl_shm_interface.name) == 0)
    {
        impl->shm = wl_registry_bind(wl_registry, name, &wl_shm_interface, 1);
    }
    else if (gate_str_comp(interface, wl_compositor_interface.name) == 0)
    {
        impl->compositor = wl_registry_bind(wl_registry, name, &wl_compositor_interface, 4);
    }
    else if (gate_str_comp(interface, xdg_wm_base_interface.name) == 0)
    {
        impl->wm_base = wl_registry_bind(wl_registry, name, &xdg_wm_base_interface, 1);
        xdg_wm_base_add_listener(impl->wm_base, &xdg_wm_base_listener, impl);
    }
    else if (gate_str_comp(interface, wl_seat_interface.name) == 0)
    {
        impl->seat = wl_registry_bind(wl_registry, name, &wl_seat_interface, 1);
        wl_seat_add_listener(impl->seat, &seat_listener, impl);
    }
}

static void waylandfb_registry_global_remove(void* data, struct wl_registry* wl_registry, uint32_t name)
{
    waylandfb_t* const impl = (waylandfb_t*)data;<--- Variable 'impl' is assigned a value that is never used.
}

static struct wl_registry_listener const waylandfb_registry_listener =
{
    &waylandfb_registry_global,
    &waylandfb_registry_global_remove
};

static int open_gate_wl_shm(gate_size_t length)
{
    const gate_uint32_t my_pid = (gate_uint32_t)getpid();
    static gate_uint32_t rnd_counter = 0;
    char buffer[255];
    gate_strbuilder_t path;
    int fd = -1;
    unsigned retries = 1000;
    int result;

    do
    {
        gate_strbuilder_create_static(&path, buffer, sizeof(buffer), 0);
        gate_strbuilder_append_cstr(&path, "/wl_shm-gate-fb-");
        gate_strbuilder_append_uint32(&path, my_pid);
        gate_strbuilder_append_cstr(&path, "-");
        gate_strbuilder_append_uint32(&path, rnd_counter);

        fd = shm_open(buffer, O_RDWR | O_CREAT | O_EXCL, 0600);
        if (fd >= 0)
        {
            shm_unlink(buffer);
            break;
        }
        ++rnd_counter;
        --retries;
    } while ((retries > 0) && (errno == EEXIST));

    if (fd < 0)
    {
        return fd;
    }

    do
    {
        result = ftruncate(fd, length);
    } while ((result < 0) && (errno == EINTR));

    if (result < 0)
    {
        close(fd);
        return -1;
    }
    return fd;
}

static void waylandfb_buffer_release(void* data, struct wl_buffer* wl_buffer)
{
    wl_buffer_destroy(wl_buffer);
}

static const struct wl_buffer_listener waylandfb_buffer_listener =
{
    .release = &waylandfb_buffer_release,
};

static struct wl_buffer* draw_frame(waylandfb_t* impl)
{
    struct wl_buffer* ret = NULL;
    int fd = -1;
    gate_size_t stride;
    gate_size_t length;
    gate_uint32_t* ptr_pixels = NULL;
    struct wl_shm_pool* pool = NULL;
    unsigned y;

    do
    {
        stride = (gate_size_t)impl->width * 4;
        length = stride * (gate_size_t)impl->height;
        fd = open_gate_wl_shm(length);
        if (fd < 0)
        {
            break;
        }

        ptr_pixels = (gate_uint32_t*)mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
        if (ptr_pixels == MAP_FAILED)
        {
            break;
        }

        pool = wl_shm_create_pool(impl->shm, fd, length);
        if (pool == NULL)
        {
            break;
        }

        ret = wl_shm_pool_create_buffer(pool, 0, impl->width, impl->height, stride, WL_SHM_FORMAT_XRGB8888);

        if (ret != NULL)
        {
            /* attach cleanup-callback */
            wl_buffer_add_listener(ret, &waylandfb_buffer_listener, NULL);
        }

        if (!gate_rasterimage_is_empty(&impl->current_frame))
        {
            for (y = 0; y != impl->height; ++y)
            {
                void* ptr_src = gate_rasterimage_get_line_ptr(&impl->current_frame, y);
                gate_uint32_t* ptr_dst = &ptr_pixels[y * impl->width];
                gate_mem_copy(ptr_dst, ptr_src, stride);
            }
        }
    } while (0);

    if (pool != NULL)
    {
        wl_shm_pool_destroy(pool);
    }

    if (ptr_pixels != MAP_FAILED)
    {
        munmap(ptr_pixels, length);
    }

    if (fd >= 0)
    {
        close(fd);
    }

    return ret;
}

static void paint_frame_on_surface(waylandfb_t* impl)
{
    struct wl_buffer* buffer = draw_frame(impl);
    wl_surface_attach(impl->surface, buffer, 0, 0);
    wl_surface_commit(impl->surface);
}

static void xdg_surface_configure(void* data, struct xdg_surface* xdg_surface, uint32_t serial)
{
    waylandfb_t* const impl = (waylandfb_t*)data;
    xdg_surface_ack_configure(xdg_surface, serial);

    paint_frame_on_surface(impl);
}

static const struct xdg_surface_listener xdg_surface_listener =
{
    &xdg_surface_configure,
};



static int waylandfb_displatch_events(waylandfb_t* impl, gate_uint32_t timeout_ms)
{
    int events_dispatched;

    events_dispatched = wl_display_roundtrip(impl->display);
    /* events_dispatched = wl_display_dispatch_pending(impl->display); */

    if ((timeout_ms > 0) && (events_dispatched >= 0))
    {
#if 0
        int awaited_events;
        struct timespec timeout;
        timeout.tv_sec = timeout_ms / 1000;
        timeout.tv_nsec = (timeout_ms % 1000) * 1000000;
        awaited_events = wl_display_dispatch_timeout(impl->display, &timeout);
        if (awaited_events >= 0)
        {
            events_dispatched += awaited_events;
        }
#endif
    }
    return events_dispatched;
}

static gate_result_t waylandfb_init(waylandfb_t* impl)
{
    gate_result_t ret = GATE_RESULT_FAILED;
    do
    {
        impl->display = wl_display_connect(NULL);
        if (impl->display == NULL)
        {
            GATE_DEBUG_TRACE("Failed to connect to wayland display");
            ret = GATE_RESULT_OUTOFRESOURCES;
            break;
        }

        impl->registry = wl_display_get_registry(impl->display);
        if (impl->registry == NULL)
        {
            GATE_DEBUG_TRACE("Failed to get wayland registry");
            ret = GATE_RESULT_OUTOFRESOURCES;
            break;
        }

        wl_registry_add_listener(impl->registry, &waylandfb_registry_listener, impl);
        wl_display_roundtrip(impl->display);

        if (impl->compositor == NULL)
        {
            GATE_DEBUG_TRACE("Missing wayland compositor");
            ret = GATE_RESULT_OUTOFRESOURCES;
            break;
        }

        impl->surface = wl_compositor_create_surface(impl->compositor);
        if (NULL == impl->surface)
        {
            GATE_DEBUG_TRACE("Failed to create wayland surface");
            ret = GATE_RESULT_OUTOFRESOURCES;
            break;
        }

        if (impl->shm == NULL)
        {
            GATE_DEBUG_TRACE("Missing wayland SHM instance");
            ret = GATE_RESULT_OUTOFRESOURCES;
            break;
        }

        if (impl->wm_base == NULL)
        {
            GATE_DEBUG_TRACE("Missing wayland wm_base instance");
            ret = GATE_RESULT_OUTOFRESOURCES;
            break;
        }

        impl->xdgsurface = xdg_wm_base_get_xdg_surface(impl->wm_base, impl->surface);
        if (impl->xdgsurface == NULL)
        {
            GATE_DEBUG_TRACE("Missing wayland XDG surface");
            ret = GATE_RESULT_OUTOFRESOURCES;
            break;
        }

        xdg_surface_add_listener(impl->xdgsurface, &xdg_surface_listener, impl);

        impl->toplevel = xdg_surface_get_toplevel(impl->xdgsurface);
        if (impl->toplevel == NULL)
        {
            GATE_DEBUG_TRACE("Missing wayland toplevel object");
            ret = GATE_RESULT_OUTOFRESOURCES;
            break;
        }

        xdg_toplevel_set_title(impl->toplevel, "GATE Framebuffer Window");

        wl_surface_commit(impl->surface);

        wl_display_roundtrip(impl->display);

        ret = GATE_RESULT_OK;
    } while (0);

    return ret;
}

static void waylandfb_release_resources(waylandfb_t* impl)
{
    if (impl->pointer)
    {
        wl_pointer_destroy(impl->pointer);
        impl->pointer = NULL;
    }
    if (impl->keyboard)
    {
        wl_keyboard_destroy(impl->keyboard);
        impl->keyboard = NULL;
    }
    if (impl->seat)
    {
        wl_seat_destroy(impl->seat);
        impl->seat = NULL;
    }
    if (impl->toplevel)
    {
        xdg_toplevel_destroy(impl->toplevel);
        impl->toplevel = NULL;
    }

    if (impl->xdgsurface)
    {
        xdg_surface_destroy(impl->xdgsurface);
        impl->xdgsurface = NULL;
    }

    if (impl->surface)
    {
        wl_surface_destroy(impl->surface);
        impl->surface = NULL;
    }

    if (impl->wm_base)
    {
        xdg_wm_base_destroy(impl->wm_base);
        impl->wm_base = NULL;
    }

    if (impl->shm)
    {
        wl_shm_destroy(impl->shm);
        impl->shm = NULL;
    }
    if (impl->compositor)
    {
        wl_compositor_destroy(impl->compositor);
        impl->compositor = NULL;
    }
    if (impl->registry)
    {
        wl_registry_destroy(impl->registry);
        impl->registry = NULL;
    }
    if (impl->display)
    {
        wl_display_disconnect(impl->display);
        impl->display = NULL;
    }
}


/****************************************
 *  GATE wayland framebuffer interface  *
 ****************************************/

static char const* waylandfb_get_interface_name(void* obj)
{
    return GATE_INTERFACE_NAME_FRAMEBUFFER;
}

static void waylandfb_release(void* obj)
{
    waylandfb_t* const impl = (waylandfb_t*)obj;
    if (0 == gate_atomic_int_dec(&impl->ref_counter))
    {
        waylandfb_release_resources(impl);
        gate_mem_dealloc(impl);
    }
}
static int waylandfb_retain(void* obj)
{
    waylandfb_t* const impl = (waylandfb_t*)obj;
    return (int)gate_atomic_int_inc(&impl->ref_counter);
}

static gate_result_t waylandfb_get_info(void* obj, gate_framebuffer_info_t* ptr_info)
{
    waylandfb_t* const impl = (waylandfb_t*)obj;

    ptr_info->format_id = GATE_IMAGE_PIXELFORMAT_RGBA;
    ptr_info->bits_per_pixel = 32;
    ptr_info->width = impl->width;
    ptr_info->height = impl->height;

    return GATE_RESULT_OK;
}
static gate_result_t waylandfb_get_property(void* obj, gate_string_t const* name, gate_property_t* prop)
{
    waylandfb_t* const impl = (waylandfb_t*)obj;<--- Variable 'impl' is assigned a value that is never used.
    return GATE_RESULT_NOTIMPLEMENTED;
}
static gate_result_t waylandfb_set_property(void* obj, gate_string_t const* name, gate_property_t const* prop)
{
    waylandfb_t* const impl = (waylandfb_t*)obj;<--- Variable 'impl' is assigned a value that is never used.
    return GATE_RESULT_NOTIMPLEMENTED;
}

static gate_result_t waylandfb_update(void* obj)
{
    waylandfb_t* const impl = (waylandfb_t*)obj;

    paint_frame_on_surface(impl);
    wl_surface_damage(impl->surface, 0, 0, impl->width, impl->height);
    return GATE_RESULT_OK;
}

static gate_result_t waylandfb_get_pixel(void* obj, gate_uint32_t x, gate_uint32_t y, gate_color_t* ptr_col)
{
    waylandfb_t* const impl = (waylandfb_t*)obj;<--- Variable 'impl' is assigned a value that is never used.
    return GATE_RESULT_NOTIMPLEMENTED;
}
static gate_result_t waylandfb_set_pixel(void* obj, gate_uint32_t x, gate_uint32_t y, gate_color_t col)
{
    waylandfb_t* const impl = (waylandfb_t*)obj;<--- Variable 'impl' is assigned a value that is never used.
    return GATE_RESULT_NOTIMPLEMENTED;
}

static gate_result_t waylandfb_get_image(void* obj, gate_uint32_t src_x, gate_uint32_t src_y, gate_uint32_t width, gate_uint32_t height, gate_rasterimage_t* ptr_image)
{
    waylandfb_t* const impl = (waylandfb_t*)obj;<--- Variable 'impl' is assigned a value that is never used.
    return GATE_RESULT_NOTIMPLEMENTED;
}
static gate_result_t waylandfb_set_image(void* obj, gate_rasterimage_t const* ptr_image, gate_uint32_t dest_x, gate_uint32_t dest_y)
{
    waylandfb_t* const impl = (waylandfb_t*)obj;
    if (gate_rasterimage_is_empty(&impl->current_frame))
    {
        if (NULL == gate_rasterimage_create(&impl->current_frame, GATE_IMAGE_PIXELFORMAT_BGR32, impl->width, impl->height, NULL))
        {
            return GATE_RESULT_OUTOFMEMORY;
        }
    }
    return gate_rasterimage_paste_image(&impl->current_frame, ptr_image, dest_x, dest_y,
        0, 0, gate_rasterimage_width(ptr_image), gate_rasterimage_height(ptr_image), false);
}

static gate_result_t waylandfb_await_event(void* obj, gate_uint32_t timeout_ms, gate_framebuffer_event_t* ptr_received_evt)
{
    waylandfb_t* const impl = (waylandfb_t*)obj;

    int events_dispatched = waylandfb_displatch_events(impl, timeout_ms);
    gate_framebuffer_event_t const* ptr_evt = waylandfb_get_event(impl);

    if (ptr_evt != NULL)
    {
        gate_mem_copy(ptr_received_evt, ptr_evt, sizeof(gate_framebuffer_event_t));
        waylandfb_pop_event(impl);
        return GATE_RESULT_OK;
    }

    if (events_dispatched == 0)
    {
        return GATE_RESULT_TIMEOUT;
    }
    else
    {
        gate_mem_clear(ptr_received_evt, sizeof(gate_framebuffer_event_t));
        ptr_received_evt->event_type = GATE_FRAMEBUFFER_EVENT_IDLE;
        return GATE_RESULT_OK;
    }

}

static GATE_INTERFACE_VTBL(gate_framebuffer)* gate_init_waylandfb_vtbl()
{
    static GATE_INTERFACE_VTBL(gate_framebuffer) gate_waylandfb_vtbl;
    if (!gate_waylandfb_vtbl.get_interface_name)
    {
        GATE_INTERFACE_VTBL(gate_framebuffer) const local_vtbl =
        {
            &waylandfb_get_interface_name,
            &waylandfb_release,
            &waylandfb_retain,

            &waylandfb_get_info,
            &waylandfb_get_property,
            &waylandfb_set_property,

            &waylandfb_update,

            &waylandfb_get_pixel,
            &waylandfb_set_pixel,

            &waylandfb_get_image,
            &waylandfb_set_image,

            &waylandfb_await_event
        };
        gate_waylandfb_vtbl = local_vtbl;
    }
    return &gate_waylandfb_vtbl;
}

gate_size_t gate_framebuffer_enum_displays(gate_framebuffer_info_t* ptr_infos, gate_size_t infos_capacity)
{
    gate_size_t ret = infos_capacity;

    if (ret > 2)
    {
        ret = 2;
    }

    if (ret >= 1)
    {
        ptr_infos[0].width = 640;
        ptr_infos[0].height = 480;
        ptr_infos[0].bits_per_pixel = 32;
        ptr_infos[0].format_id = 0;
    }

    if (ret >= 2)
    {
        ptr_infos[1].width = 800;
        ptr_infos[1].height = 600;
        ptr_infos[1].bits_per_pixel = 32;
        ptr_infos[1].format_id = 0;
    }

    return ret;
}

gate_result_t gate_framebuffer_find_display(gate_uint32_t required_width, gate_uint32_t required_height, gate_framebuffer_info_t* ptr_found_display_info)
{
    /* TODO */
    ptr_found_display_info->bits_per_pixel = 32;
    ptr_found_display_info->format_id = 0;
    ptr_found_display_info->width = required_width;
    ptr_found_display_info->height = required_height;
    return GATE_RESULT_OK;
}

gate_result_t gate_framebuffer_open_display(gate_framebuffer_info_t const* ptr_infos, gate_framebuffer_t** ptr_framebuffer)
{
    gate_result_t ret = GATE_RESULT_NOTSUPPORTED;
    waylandfb_t* impl = NULL;

    do
    {
        impl = (waylandfb_t*)gate_mem_alloc(sizeof(waylandfb_t));
        if (impl == NULL)
        {
            ret = GATE_RESULT_OUTOFMEMORY;
            break;
        }

        gate_mem_clear(impl, sizeof(waylandfb_t));
        impl->vtbl = gate_init_waylandfb_vtbl();
        gate_atomic_int_init(&impl->ref_counter, 1);

        impl->width = ptr_infos->width;
        impl->height = ptr_infos->height;

        impl->events = gate_arraylist_create(sizeof(gate_framebuffer_event_t), NULL, 0, NULL, NULL);
        if (impl->events == NULL)
        {
            ret = GATE_RESULT_OUTOFMEMORY;
            break;
        }

        ret = waylandfb_init(impl);
        GATE_BREAK_IF_FAILED(ret);

        if (ptr_framebuffer)
        {
            *ptr_framebuffer = (gate_framebuffer_t*)impl;
            impl = NULL;
        }
        ret = GATE_RESULT_OK;

    } while (0);

    if (impl)
    {
        waylandfb_release_resources(impl);
        gate_mem_dealloc(impl);
    }

    return ret;
}

#endif /* GATE_GRAPHICS_FRAMEBUFFERS_WAYLAND */

#if defined(GATE_GRAPHICS_FRAMEBUFFERS_XLIB)
#include "gate/graphics/platform/framebuffers_impl_xlib.h"
#endif /* GATE_GRAPHICS_FRAMEBUFFERS_XLIB */


#if defined(GATE_GRAPHICS_FRAMEBUFFERS_GL)
#include "gate/graphics/platform/framebuffers_impl_gl.h"
#endif /* GATE_GRAPHICS_FRAMEBUFFERS_GL */


#if defined(GATE_GRAPHICS_FRAMEBUFFERS_UGA)
#include "gate/graphics/platform/framebuffers_impl_efigop.h"
#endif  /* GATE_GRAPHICS_FRAMEBUFFERS_UGA */


#if defined(GATE_GRAPHICS_FRAMEBUFFERS_INT10)
#include "gate/graphics/platform/framebuffers_impl_int10.h"
#endif /* GATE_GRAPHICS_FRAMEBUFFERS_INT10 */


#if defined(GATE_GRAPHICS_FRAMEBUFFERS_WASM)
#include "gate/graphics/platform/framebuffers_impl_wasm.h"
#endif /* GATE_GRAPHICS_FRAMEBUFFERS_WASM */

#if defined(GATE_GRAPHICS_FRAMEBUFFERS_NOIMPL)

gate_size_t gate_framebuffer_enum_displays(gate_framebuffer_info_t* ptr_infos, gate_size_t infos_capacity)
{
    GATE_UNUSED_ARG(ptr_infos);
    GATE_UNUSED_ARG(infos_capacity);
    return 0;
}

gate_result_t gate_framebuffer_find_display(gate_uint32_t required_width, gate_uint32_t required_height, gate_framebuffer_info_t* ptr_found_display_info)
{
    GATE_UNUSED_ARG(required_width);
    GATE_UNUSED_ARG(required_height);
    GATE_UNUSED_ARG(ptr_found_display_info);
    return GATE_RESULT_NOMATCH;
}

gate_result_t gate_framebuffer_open_display(gate_framebuffer_info_t const* ptr_infos, gate_framebuffer_t** ptr_framebuffer)
{
    GATE_UNUSED_ARG(ptr_infos);
    GATE_UNUSED_ARG(ptr_framebuffer);
    return GATE_RESULT_NOMATCH;
}


#endif  /* GATE_GRAPHICS_FRAMEBUFFERS_NOIMPL */