winflip 0.1.0

An experiment in making a small light-weight window-setup library.
Documentation
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
/// TODO still:
/// Various glx stuff
/// Clipboard events
/// keysym_to_unicode()
/// Make init and shutdown less bonkers -- drop impls and stuff.
/// better error_handler()
/// highdpi
use std::ffi;
use std::mem;
use std::os::raw;
use std::ptr;

use crate::{Desc, EventType, GlFbConfig, Keycode, MouseButton, WFApp};

use x11_dl::{self as x11, glx, xlib};

/// These apparently aren't really in x11-dl
/// Reference: https://tronche.com/gui/x/xlib/ICC/client-to-window-manager/wm-hints.html
const WITHDRAWN_STATE: raw::c_int = 0;
const NORMAL_STATE: raw::c_int = 1;
const ICONIC_STATE: raw::c_int = 3;

/// All the static vars from the C lib
pub struct Window {
    // X11 stuff
    // _sapp_x11_* vars
    display: *mut xlib::Display,
    screen: i32,
    root: xlib::Window,
    colormap: xlib::Colormap,
    window: xlib::Window,
    _dpi: f32,
    window_state: u32,
    error_code: u8,
    xlib: xlib::Xlib,
    /// Map of held-down keys
    /// XLib manual says keycodes are in the range [8, 255] inclusive.
    /// https://tronche.com/gui/x/xlib/input/keyboard-encoding.html
    keycodes: [bool; 256],
    // Names for X11 extensions, it appears.
    x11_utf8_string: xlib::Atom,
    x11_wm_protocols: xlib::Atom,
    x11_wm_delete_window: xlib::Atom,
    x11_wm_state: xlib::Atom,
    x11_net_wm_name: xlib::Atom,
    x11_net_wm_icon_name: xlib::Atom,

    // glx stuff
    // _sap_glx_* vars
    // TODO: Figure this out
    //libgl: *const ffi::c_void,
    //gl_major: i32,
    //gl_minor: i32,
    //eventbase: i32,
    //errorbase: i32,
    glx_ctx: glx::GLXContext,
    glx_window: glx::GLXWindow,
    glx: glx::Glx,

    /// ruh-roh, x11-dl doesn't seem to include glxCreateContextAttribsARB
    /// and it's the function that lets us choose OpenGL versions
    /// so it's kinda Important to me.
    ///
    /// glutin_glx_sys appears to provide its own binding to it,
    /// but it doesn't seem to include *all* of x11-dl, so we get to choose
    /// one or the other.  Loading the function we want ourselves isn't too hard
    /// though, so we do that.
    glx_create_context_attribs_arb: Option<
        unsafe extern "C" fn(
            *mut xlib::Display,
            glx::GLXFBConfig,
            glx::GLXContext,
            bool,
            *const raw::c_int,
        ) -> glx::GLXContext,
    >,

    glx_swap_interval_ext: Option<fn(*mut xlib::Display, glx::GLXDrawable, raw::c_int)>,
    glx_swap_interval_mesa: Option<fn(raw::c_int) -> raw::c_int>,
}

/// This is the error handler set with XSetErrorHandler.
/// TODO: Try to figure out how to not make this have to mutate global state.
/// In Sokol it sets _sapp_x11_error_code appropriately, but it doesn't take
/// any userdata and so it's super stateful and not really that useful...
unsafe extern "C" fn error_handler(
    display: *mut xlib::Display,
    error: *mut xlib::XErrorEvent,
) -> i32 {
    eprintln!("Yikes: {:?}, {:?}", display, *error);
    //self.error_code = event.error_code;
    return 0;
}

fn gl_choose_fb_config(_desired: &GlFbConfig, alternatives: &[GlFbConfig]) -> Option<GlFbConfig> {
    //unimplemented!()
    //alternatives.get(0).cloned()
    alternatives.last().cloned()
}

impl Window {
    pub fn grab_error_handler(&mut self) {
        // TODO: Should this be xlib::GrabSuccess?
        // https://github.com/floooh/sokol/blob/master/sokol_app.h#L6428
        // Seems like they're both 0 but one is int and the other is unsigned char.
        self.error_code = xlib::Success;
        // TODO: Safety audit
        unsafe {
            (self.xlib.XSetErrorHandler)(Some(error_handler));
        }
    }

    pub fn release_error_handler(&mut self) {
        // TODO: Safety audit
        unsafe {
            (self.xlib.XSync)(self.display, 0);
            (self.xlib.XSetErrorHandler)(None);
        }
    }

    pub fn init_extensions(&mut self) {
        // TODO: Safety audit
        unsafe {
            self.x11_utf8_string = (self.xlib.XInternAtom)(
                self.display,
                ffi::CString::new("UTF8_STRING").unwrap().as_ptr(),
                0,
            );
            self.x11_wm_protocols = (self.xlib.XInternAtom)(
                self.display,
                ffi::CString::new("WM_PROTOCOLS").unwrap().as_ptr(),
                0,
            );
            self.x11_wm_delete_window = (self.xlib.XInternAtom)(
                self.display,
                ffi::CString::new("WM_DELETE_WINDOW").unwrap().as_ptr(),
                0,
            );
            self.x11_wm_state = (self.xlib.XInternAtom)(
                self.display,
                ffi::CString::new("WM_STATE").unwrap().as_ptr(),
                0,
            );
            self.x11_net_wm_name = (self.xlib.XInternAtom)(
                self.display,
                ffi::CString::new("_NET_WM_NAME").unwrap().as_ptr(),
                0,
            );
            self.x11_net_wm_icon_name = (self.xlib.XInternAtom)(
                self.display,
                ffi::CString::new("_NET_WM_ICON_NAME").unwrap().as_ptr(),
                0,
            );
        }
    }

    pub fn query_system_dpi() -> f32 {
        // TODO: Care
        //unimplemented!()
        96.0
    }

    fn glx_has_ext(ext: &str, available_extensions: &str) -> bool {
        available_extensions.contains(ext)
    }

    pub fn glx_extsupported(ext: &str, available_extensions: *const raw::c_char) -> bool {
        if available_extensions.is_null() {
            return false;
        } else {
            // Big fat ASSUME that `available_extensions` is a valid C string here.
            // If it's unterminated or such this will die horribly.
            let cs = unsafe { ffi::CStr::from_ptr(available_extensions) };
            let s = cs.to_string_lossy();
            Self::glx_has_ext(ext, &s)
        }
    }

    /// We should be able to pass this to `glow` or such to let it load particular
    /// OpenGL functions.
    ///
    /// ...This is kinda bonkers in general though.  The types in x11_dl::glx don't
    /// match glutin or glow, whether or not the function exists and in which form
    /// is not well defined.  Even the experts don't understand it:
    /// https://www.khronos.org/opengl/wiki/Load_OpenGL_Functions#Linux_and_X-Windows
    ///
    /// Probably horribly unsafe but as long as you don't unload the DLL or something
    /// like that will probably always work?
    pub unsafe fn getprocaddr(glx: &glx::Glx, procname: &str) -> *const ffi::c_void {
        let procname_cstr =
            ffi::CString::new(procname).expect("Invalid C string given to getprocaddr");
        // C char types.  That is all.  Thanks, Obama.
        let procname_p = procname_cstr.as_ptr() as *const raw::c_uchar;
        let f = (glx.glXGetProcAddress)(procname_p);
        // TODO: Vet that this is at least vaguely okay.
        // Well, it WORKS, so...
        f.expect("getprocaddr: Could not find given function?") as *const ffi::c_void
    }

    pub unsafe fn glx_attrib(&mut self, fbconfig: glx::GLXFBConfig, attrib: i32) -> i32 {
        let mut res: i32 = 0;
        (self.glx.glXGetFBConfigAttrib)(self.display, fbconfig, attrib, &mut res);
        res
    }

    pub unsafe fn choose_fb_config(&mut self) -> glx::GLXFBConfig {
        let mut trust_window_bit: bool = true;

        // Per https://github.com/floooh/sokol/blob/master/sokol_app.h#L6605
        // HACK: This is a (hopefully temporary) workaround for Chromium
        // (VirtualBox GL) not setting the window bit on any GLXFBConfigs
        let vendor = (self.glx.glXGetClientString)(self.display, glx::GLX_VENDOR);
        if vendor != ptr::null() {
            let vendor_string = ffi::CStr::from_ptr(vendor).to_str().unwrap();
            trust_window_bit = vendor_string != "Chromium";
        }

        let mut native_config_count = 0;
        let native_configs =
            (self.glx.glXGetFBConfigs)(self.display, self.screen, &mut native_config_count);
        println!("GLX configs gotten?");
        if native_configs.is_null() || native_config_count == 0 {
            panic!("GLX: No GLXFBConfigs returned");
        }
        let mut usable_configs: Vec<GlFbConfig> = vec![];
        // usable_count == configs.len()
        for i in 0..(native_config_count as isize) {
            let n = native_configs.offset(i);
            let mut u = GlFbConfig::default();
            // Only consider RGBA configs
            if self.glx_attrib(*n, glx::GLX_RENDER_TYPE) & glx::GLX_RGBA_BIT == 0 {
                continue;
            }
            // Only consider window configs
            if self.glx_attrib(*n, glx::GLX_DRAWABLE_TYPE) & glx::GLX_WINDOW_BIT == 0 {
                if trust_window_bit {
                    continue;
                }
            }

            u.red_bits = self.glx_attrib(*n, glx::GLX_RED_SIZE);
            u.green_bits = self.glx_attrib(*n, glx::GLX_GREEN_SIZE);
            u.blue_bits = self.glx_attrib(*n, glx::GLX_BLUE_SIZE);
            u.alpha_bits = self.glx_attrib(*n, glx::GLX_ALPHA_SIZE);
            u.depth_bits = self.glx_attrib(*n, glx::GLX_DEPTH_SIZE);
            u.stencil_bits = self.glx_attrib(*n, glx::GLX_STENCIL_SIZE);
            u.doublebuffer = self.glx_attrib(*n, glx::GLX_DOUBLEBUFFER) == 1;
            // TODO: only do this if _sapp_glx_ARB_multisample
            u.samples = self.glx_attrib(*n, glx::GLX_SAMPLES);
            u.handle = *n as usize;
            usable_configs.push(u);
        }
        let desired = GlFbConfig {
            red_bits: 8,
            green_bits: 8,
            blue_bits: 8,
            alpha_bits: 8,
            depth_bits: 24,
            stencil_bits: 8,
            doublebuffer: true,
            // TODO: This needs to be Desc::sample_count
            samples: 1,
            ..Default::default()
        };
        let closest =
            gl_choose_fb_config(&desired, &usable_configs).expect("No usable GLXFBConfig found!");
        println!("Closest config: {:?}", closest);

        // *shreds sick air guitar riffs while casting integers to type-inferred mut pointers*
        closest.handle as *mut _
    }

    pub fn choose_visual(&mut self) -> (*mut xlib::Visual, i32) {
        // TODO: Audit unsafe
        unsafe {
            let native = self.choose_fb_config();
            if native.is_null() {
                panic!("GLX: Failed to find suitable GLXFBConfig");
            }
            let result = (self.glx.glXGetVisualFromFBConfig)(self.display, native);
            if result.is_null() {
                panic!("GLX: Failed to retrieve Visual for GLXFBConfig");
            }
            let visual = (*result).visual;
            let depth = (*result).depth;
            (self.xlib.XFree)(result as *mut _);
            (visual, depth)
        }
    }

    pub fn glx_create_context(&mut self) {
        // TODO: Audit unsafe?
        unsafe {
            // TODO: We get the config in choose_visual() as well; redundant?
            let native = self.choose_fb_config();
            if native.is_null() {
                panic!("GLX: Failed to find suitable GLXFBConfig (2)");
            }

            self.grab_error_handler();
            let attribs: [i32; 10] = [
                glx::arb::GLX_CONTEXT_MAJOR_VERSION_ARB,
                4,
                glx::arb::GLX_CONTEXT_MINOR_VERSION_ARB,
                3,
                glx::arb::GLX_CONTEXT_PROFILE_MASK_ARB,
                glx::arb::GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
                // TODO: verify this is what we want
                glx::arb::GLX_CONTEXT_FLAGS_ARB,
                glx::arb::GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
                0,
                0,
            ];

            if let Some(f) = self.glx_create_context_attribs_arb {
                let glx_ctx = f(
                    self.display,
                    native,
                    ptr::null_mut(),
                    true,
                    attribs.as_ptr(),
                );
                if glx_ctx.is_null() {
                    panic!("GLX: Failed to create GLX context");
                }
            } else {
                panic!("GLX: glxCreateContextAttribsARB is not found, does your GPU support OpenGL >= 3.3?")
            }

            let window =
                (self.glx.glXCreateWindow)(self.display, native, self.window, ptr::null_mut());
            if window == 0 {
                panic!("GLX: Failed to create window");
            }
            self.glx_window = window;
        }
    }

    /// TODO: Make this a Drop impl.  It's unsafe 'cause it can be called on things
    /// that are already free.
    pub unsafe fn glx_destroy_context(&mut self) {
        (self.glx.glXDestroyWindow)(self.display, self.glx_window);
        (self.glx.glXDestroyContext)(self.display, self.glx_ctx);
    }

    /// Unsafe, since it does no checking that display or window or such are valid.
    unsafe fn glx_make_current(&mut self) {
        (self.glx.glXMakeCurrent)(self.display, self.window, self.glx_ctx);
    }

    /// Unsafe, since it does no checking that display or window or such are valid.
    unsafe fn glx_swap_buffers(&mut self) {
        (self.glx.glXSwapBuffers)(self.display, self.window);
    }

    unsafe fn glx_swap_interval(&mut self, interval: u32) {
        self.glx_make_current();
        if let Some(f) = self.glx_swap_interval_ext {
            f(self.display, self.glx_window, interval as i32);
        } else if let Some(f) = self.glx_swap_interval_mesa {
            f(interval as i32);
        }
        // Guess we can't set the swap interval ¯\_(ツ)_/¯
    }

    pub fn update_window_title(&mut self, title: &str) {
        let window_title: *const raw::c_char = ffi::CString::new(title).unwrap().as_ptr();
        // TODO: Audit unsafe
        unsafe {
            println!("1");
            /*
             * TODO: Fix, these property calls complain of invalid titles for some reason.
            (self.xlib.Xutf8SetWMProperties)(
                self.display,
                self.window,
                window_title,
                window_title,
                ptr::null_mut(),
                0,
                ptr::null_mut(),
                ptr::null_mut(),
                ptr::null_mut(),
            );
            println!("2");
            (self.xlib.XChangeProperty)(
                self.display,
                self.window,
                self.x11_net_wm_name,
                self.x11_utf8_string,
                8,
                xlib::PropModeReplace,
                window_title as *const _,
                title.len() as i32,
            );
            println!("3");
            (self.xlib.XChangeProperty)(
                self.display,
                self.window,
                self.x11_net_wm_icon_name,
                self.x11_utf8_string,
                8,
                xlib::PropModeReplace,
                window_title as *const _,
                title.len() as i32,
            );
            println!("4");
            */
            (self.xlib.XFlush)(self.display);
            println!("5");
        }
    }

    /// Returns window width/height and framebuffer width/height.
    pub fn query_window_size(&mut self) -> (u32, u32, u32, u32) {
        // TODO: Audit unsafe.
        unsafe {
            let mut attribs: xlib::XWindowAttributes = mem::zeroed();
            // TODO: Check error code?
            (self.xlib.XGetWindowAttributes)(self.display, self.window, &mut attribs);
            let width = attribs.width as u32;
            let height = attribs.height as u32;
            (width, height, width, height)
        }
    }

    pub fn create_window(&mut self, app: &WFApp, visual: &mut xlib::Visual, depth: i32) {
        // TODO: Audit unsafe.
        unsafe {
            self.colormap =
                (self.xlib.XCreateColormap)(self.display, self.root, visual, xlib::AllocNone);
            let mut wa: xlib::XSetWindowAttributes = mem::zeroed();
            let wamask = xlib::CWBorderPixel | xlib::CWColormap | xlib::CWEventMask;
            wa.colormap = self.colormap;
            wa.border_pixel = 0;
            wa.event_mask = xlib::StructureNotifyMask
                | xlib::KeyPressMask
                | xlib::KeyReleaseMask
                | xlib::PointerMotionMask
                | xlib::ButtonPressMask
                | xlib::ButtonReleaseMask
                | xlib::ExposureMask
                | xlib::FocusChangeMask
                | xlib::VisibilityChangeMask
                | xlib::EnterWindowMask
                | xlib::LeaveWindowMask
                | xlib::PropertyChangeMask;
            self.grab_error_handler();
            self.window = (self.xlib.XCreateWindow)(
                self.display,
                self.root,
                0,
                0,
                app.window_width,
                app.window_height,
                0,     // Border width
                depth, // Color depth,
                xlib::InputOutput as u32,
                visual,
                wamask,
                &mut wa,
            );
            self.release_error_handler();
            if self.window == 0 {
                panic!("X11: Failed to create window");
            }
            let protocols = &mut [self.x11_wm_delete_window];
            // TODO: Check return?
            (self.xlib.XSetWMProtocols)(self.display, self.window, protocols.as_mut_ptr(), 1);

            {
                let hints = (self.xlib.XAllocSizeHints)();
                (*hints).flags |= xlib::PWinGravity;
                (*hints).win_gravity = xlib::StaticGravity;
                (self.xlib.XSetWMNormalHints)(self.display, self.window, hints);
                (self.xlib.XFree)(hints as *mut _);
            }
            self.update_window_title("TODO: Placeholder title");
            // TODO
            let _ = self.query_window_size();
        }
    }

    /// TODO: Clean this up and make it a Drop impl
    pub fn destroy_window(&mut self) {
        unsafe {
            if self.window != 0 {
                (self.xlib.XUnmapWindow)(self.display, self.window);
                (self.xlib.XDestroyWindow)(self.display, self.window);
                self.window = 0;
            }
            if self.colormap != 0 {
                (self.xlib.XFreeColormap)(self.display, self.colormap);
                self.colormap = 0;
            }
            (self.xlib.XFlush)(self.display);
        }
    }

    pub fn window_visible(&mut self) -> bool {
        unsafe {
            let mut wa: xlib::XWindowAttributes = mem::zeroed();
            (self.xlib.XGetWindowAttributes)(self.display, self.window, &mut wa);
            wa.map_state == xlib::IsViewable
        }
    }

    pub fn show_window(&mut self) {
        if !self.window_visible() {
            unsafe {
                (self.xlib.XMapWindow)(self.display, self.window);
                (self.xlib.XRaiseWindow)(self.display, self.window);
                (self.xlib.XFlush)(self.display);
            }
        }
    }

    pub fn hide_window(&mut self) {
        unsafe {
            (self.xlib.XUnmapWindow)(self.display, self.window);
            (self.xlib.XFlush)(self.display);
        }
    }

    pub unsafe fn get_window_property(
        &mut self,
        property: xlib::Atom,
        typ: xlib::Atom,
        value: *mut *mut raw::c_char,
    ) -> u64 {
        let actual_type: &mut xlib::Atom = &mut 0;
        let actual_format: &mut i32 = &mut 0;
        let item_count: &mut u64 = &mut 0;
        let bytes_after: &mut u64 = &mut 0;
        (self.xlib.XGetWindowProperty)(
            self.display,
            self.window,
            property,
            0,
            std::u64::MAX as i64,
            0,
            typ,
            actual_type,
            actual_format,
            item_count,
            bytes_after,
            value as _,
        );
        *item_count
    }

    pub fn get_window_state(&mut self) -> u32 {
        // TODO: Audit unsafe
        unsafe {
            // oh xlib, you so undocumented
            let mut result: u32 = WITHDRAWN_STATE as u32;
            #[derive(Debug)]
            #[repr(C)]
            struct State {
                state: x11::xmd::CARD32,
                icon: xlib::Window,
            };
            let state: *mut State = ptr::null_mut();
            let r = self.get_window_property(
                self.x11_wm_state,
                self.x11_wm_state,
                &mut (state as *mut _),
            );
            if r >= 2 && !state.is_null() {
                result = (*state).state;
            }
            if !state.is_null() {
                (self.xlib.XFree)(state as *mut _);
            }
            result
        }
    }

    pub fn x11_mod(mods: u32) -> u32 {
        let mut res = 0;
        if (mods & xlib::ShiftMask) != 0 {
            res |= crate::MOD_SHIFT;
        }
        if (mods & xlib::ControlMask) != 0 {
            res |= crate::MOD_CTRL;
        }
        if (mods & xlib::Mod1Mask) != 0 {
            res |= crate::MOD_ALT;
        }
        if (mods & xlib::Mod4Mask) != 0 {
            res |= crate::MOD_SUPER;
        }
        res
    }

    pub fn translate_button(event: *const xlib::XEvent) -> MouseButton {
        // TODO: XEvent is a union, so this is horribly unsafe.
        unsafe {
            match (*event).button.button {
                xlib::Button1 => MouseButton::Left,
                xlib::Button2 => MouseButton::Middle,
                xlib::Button3 => MouseButton::Right,
                _ => MouseButton::Invalid,
            }
        }
    }

    /// TODO: Why does this live here?
    pub fn mouse_event(app: &mut WFApp, typ: EventType, btn: MouseButton, mods: u32) {
        let mut e = app.new_event(typ);
        e.mouse_button = btn;
        e.modifiers = mods;
        e.mouse_x = app.mouse_x;
        e.mouse_y = app.mouse_y;
        // TODO: Unused return value here
        app.call_event(&e);
    }

    pub fn scroll_event(app: &mut WFApp, x: f32, y: f32, mods: u32) {
        let mut e = app.new_event(EventType::MouseScroll);
        e.scroll_x = x;
        e.scroll_y = y;
        e.modifiers = mods;
        // TODO: Unused return value here
        app.call_event(&e);
    }

    pub fn key_event(app: &mut WFApp, typ: EventType, key: Keycode, repeat: bool, mods: u32) {
        let mut e = app.new_event(typ);
        e.keycode = key;
        e.key_repeat = repeat;
        e.modifiers = mods;
        // TODO: Unused return value here
        app.call_event(&e);
        // check if a CLIPBOARD_PASTED event must be sent too
        // TODO: We don't handle clipboard yet since it looks like sokol_app.h
        // does some Squirrelly things with it in terms of capturing keys, not sure
        // what to do here.
    }

    pub fn char_event(app: &mut WFApp, chr: char, repeat: bool, mods: u32) {
        let mut e = app.new_event(EventType::Char);
        e.char_code = chr;
        e.key_repeat = repeat;
        e.modifiers = mods;
        // TODO: Unused return value here
        app.call_event(&e);
    }

    pub fn translate_key(&mut self, scancode: u8) -> Keycode {
        // TODO: Audit unsafety
        unsafe {
            let dummy: &mut i32 = &mut 0;
            let keysyms = (self.xlib.XGetKeyboardMapping)(self.display, scancode, 1, dummy);
            assert_ne!(keysyms, ptr::null_mut());
            let keysym = *keysyms as u32;
            (self.xlib.XFree)(keysyms as *mut _);
            match keysym {
                x11::keysym::XK_Escape =>          Keycode::Escape,
                x11::keysym::XK_Tab =>             Keycode::Tab,
                x11::keysym::XK_Shift_L =>         Keycode::LeftShift,
                x11::keysym::XK_Shift_R =>         Keycode::RightShift,
                x11::keysym::XK_Control_L =>       Keycode::LeftControl,
                x11::keysym::XK_Control_R =>       Keycode::RightControl,
                x11::keysym::XK_Meta_L |
                x11::keysym::XK_Alt_L =>           Keycode::LeftAlt,
                x11::keysym::XK_Mode_switch |     /* Mapped to alt_r on many keyboards */
                x11::keysym::XK_ISO_Level3_Shift |  /* AltGr on at least some machines */
                x11::keysym::XK_Meta_R|
                x11::keysym::XK_Alt_R =>           Keycode::RightAlt,
                x11::keysym::XK_Super_L =>         Keycode::LeftSuper,
                x11::keysym::XK_Super_R =>         Keycode::RightSuper,
                x11::keysym::XK_Menu =>            Keycode::Menu,
                x11::keysym::XK_Num_Lock =>        Keycode::NumLock,
                x11::keysym::XK_Caps_Lock =>       Keycode::CapsLock,
                x11::keysym::XK_Print =>           Keycode::PrintScreen,
                x11::keysym::XK_Scroll_Lock =>     Keycode::ScrollLock,
                x11::keysym::XK_Pause =>           Keycode::Pause,
                x11::keysym::XK_Delete =>          Keycode::Delete,
                x11::keysym::XK_BackSpace =>       Keycode::Backspace,
                x11::keysym::XK_Return =>          Keycode::Enter,
                x11::keysym::XK_Home =>            Keycode::Home,
                x11::keysym::XK_End =>             Keycode::End,
                x11::keysym::XK_Page_Up =>         Keycode::PageUp,
                x11::keysym::XK_Page_Down =>       Keycode::PageDown,
                x11::keysym::XK_Insert =>          Keycode::Insert,
                x11::keysym::XK_Left =>            Keycode::Left,
                x11::keysym::XK_Right =>           Keycode::Right,
                x11::keysym::XK_Down =>            Keycode::Down,
                x11::keysym::XK_Up =>              Keycode::Up,
                x11::keysym::XK_F1 =>              Keycode::F1,
                x11::keysym::XK_F2 =>              Keycode::F2,
                x11::keysym::XK_F3 =>              Keycode::F3,
                x11::keysym::XK_F4 =>              Keycode::F4,
                x11::keysym::XK_F5 =>              Keycode::F5,
                x11::keysym::XK_F6 =>              Keycode::F6,
                x11::keysym::XK_F7 =>              Keycode::F7,
                x11::keysym::XK_F8 =>              Keycode::F8,
                x11::keysym::XK_F9 =>              Keycode::F9,
                x11::keysym::XK_F10 =>             Keycode::F10,
                x11::keysym::XK_F11 =>             Keycode::F11,
                x11::keysym::XK_F12 =>             Keycode::F12,
                x11::keysym::XK_F13 =>             Keycode::F13,
                x11::keysym::XK_F14 =>             Keycode::F14,
                x11::keysym::XK_F15 =>             Keycode::F15,
                x11::keysym::XK_F16 =>             Keycode::F16,
                x11::keysym::XK_F17 =>             Keycode::F17,
                x11::keysym::XK_F18 =>             Keycode::F18,
                x11::keysym::XK_F19 =>             Keycode::F19,
                x11::keysym::XK_F20 =>             Keycode::F20,
                x11::keysym::XK_F21 =>             Keycode::F21,
                x11::keysym::XK_F22 =>             Keycode::F22,
                x11::keysym::XK_F23 =>             Keycode::F23,
                x11::keysym::XK_F24 =>             Keycode::F24,
                x11::keysym::XK_F25 =>             Keycode::F25,

                x11::keysym::XK_KP_Divide =>       Keycode::KpDivide,
                x11::keysym::XK_KP_Multiply =>     Keycode::KpMultiply,
                x11::keysym::XK_KP_Subtract =>     Keycode::KpSubtract,
                x11::keysym::XK_KP_Add =>          Keycode::KpAdd,

                x11::keysym::XK_KP_Insert =>       Keycode::Kp0,
                x11::keysym::XK_KP_End =>          Keycode::Kp1,
                x11::keysym::XK_KP_Down =>         Keycode::Kp2,
                x11::keysym::XK_KP_Page_Down =>    Keycode::Kp3,
                x11::keysym::XK_KP_Left =>         Keycode::Kp4,
                x11::keysym::XK_KP_Begin =>        Keycode::Kp5,
                x11::keysym::XK_KP_Right =>        Keycode::Kp6,
                x11::keysym::XK_KP_Home =>         Keycode::Kp7,
                x11::keysym::XK_KP_Up =>           Keycode::Kp8,
                x11::keysym::XK_KP_Page_Up =>      Keycode::Kp9,
                x11::keysym::XK_KP_Delete =>       Keycode::KpDecimal,
                x11::keysym::XK_KP_Equal =>        Keycode::KpEqual,
                x11::keysym::XK_KP_Enter =>        Keycode::KpEnter,

                x11::keysym::XK_a =>               Keycode::A,
                x11::keysym::XK_b =>               Keycode::B,
                x11::keysym::XK_c =>               Keycode::C,
                x11::keysym::XK_d =>               Keycode::D,
                x11::keysym::XK_e =>               Keycode::E,
                x11::keysym::XK_f =>               Keycode::F,
                x11::keysym::XK_g =>               Keycode::G,
                x11::keysym::XK_h =>               Keycode::H,
                x11::keysym::XK_i =>               Keycode::I,
                x11::keysym::XK_j =>               Keycode::J,
                x11::keysym::XK_k =>               Keycode::K,
                x11::keysym::XK_l =>               Keycode::L,
                x11::keysym::XK_m =>               Keycode::M,
                x11::keysym::XK_n =>               Keycode::N,
                x11::keysym::XK_o =>               Keycode::O,
                x11::keysym::XK_p =>               Keycode::P,
                x11::keysym::XK_q =>               Keycode::Q,
                x11::keysym::XK_r =>               Keycode::R,
                x11::keysym::XK_s =>               Keycode::S,
                x11::keysym::XK_t =>               Keycode::T,
                x11::keysym::XK_u =>               Keycode::U,
                x11::keysym::XK_v =>               Keycode::V,
                x11::keysym::XK_w =>               Keycode::W,
                x11::keysym::XK_x =>               Keycode::X,
                x11::keysym::XK_y =>               Keycode::Y,
                x11::keysym::XK_z =>               Keycode::Z,
                x11::keysym::XK_1 =>               Keycode::Num1,
                x11::keysym::XK_2 =>               Keycode::Num2,
                x11::keysym::XK_3 =>               Keycode::Num3,
                x11::keysym::XK_4 =>               Keycode::Num4,
                x11::keysym::XK_5 =>               Keycode::Num5,
                x11::keysym::XK_6 =>               Keycode::Num6,
                x11::keysym::XK_7 =>               Keycode::Num7,
                x11::keysym::XK_8 =>               Keycode::Num8,
                x11::keysym::XK_9 =>               Keycode::Num9,
                x11::keysym::XK_0 =>               Keycode::Num0,
                x11::keysym::XK_space =>           Keycode::Space,
                x11::keysym::XK_minus =>           Keycode::Minus,
                x11::keysym::XK_equal =>           Keycode::Equal,
                x11::keysym::XK_bracketleft =>     Keycode::LeftBracket,
                x11::keysym::XK_bracketright =>    Keycode::RightBracket,
                x11::keysym::XK_backslash =>       Keycode::Backslash,
                x11::keysym::XK_semicolon =>       Keycode::Semicolon,
                x11::keysym::XK_apostrophe =>      Keycode::Apostrophe,
                x11::keysym::XK_grave =>           Keycode::GraveAccent,
                x11::keysym::XK_comma =>           Keycode::Comma,
                x11::keysym::XK_period =>          Keycode::Period,
                x11::keysym::XK_slash =>           Keycode::Slash,
                x11::keysym::XK_less =>            Keycode::World1,
                _ => Keycode::Invalid,
            }
        }
    }

    pub fn keysym_to_unicode(_keysym: xlib::KeySym) -> Option<char> {
        // TODO
        Some('☹')
    }

    pub fn process_event(&mut self, app: &mut WFApp, event: *mut xlib::XEvent) {
        // TODO: XEvent is a union so this is all massively unsafe
        // if event.type is wrong...
        unsafe {
            match (*event).type_ {
                xlib::KeyPress => {
                    let keycode = (*event).key.keycode as u8;
                    let key = self.translate_key(keycode);
                    let repeat = self.keycodes[keycode as usize];
                    self.keycodes[keycode as usize] = true;
                    let mods = Self::x11_mod((*event).key.state);
                    if key != Keycode::Invalid {
                        Self::key_event(app, EventType::KeyDown, key, repeat, mods);
                    }
                    let keysym: &mut xlib::KeySym = &mut 0;
                    (self.xlib.XLookupString)(
                        &mut (*event).key,
                        ptr::null_mut(),
                        0,
                        keysym,
                        ptr::null_mut(),
                    );
                    if let Some(chr) = Self::keysym_to_unicode(*keysym) {
                        Self::char_event(app, chr, repeat, mods);
                    }
                }
                xlib::KeyRelease => {
                    let keycode = (*event).key.keycode as u8;
                    let key = self.translate_key(keycode);
                    self.keycodes[keycode as usize] = false;
                    let mods = Self::x11_mod((*event).key.state);
                    if key != Keycode::Invalid {
                        Self::key_event(app, EventType::KeyUp, key, false, mods);
                    }
                }
                xlib::ButtonPress => {
                    let btn = Self::translate_button(event);
                    let mods = Self::x11_mod((*event).button.state);
                    if btn != MouseButton::Invalid {
                        Self::mouse_event(app, EventType::MouseDown, btn, mods);
                    } else {
                        // Might be a scroll event
                        match (*event).button.button {
                            4 => Self::scroll_event(app, 0.0, 1.0, mods),
                            5 => Self::scroll_event(app, 0.0, -1.0, mods),
                            6 => Self::scroll_event(app, 1.0, 0.0, mods),
                            7 => Self::scroll_event(app, -1.0, 0.0, mods),
                            _ => (),
                        }
                    }
                }
                xlib::ButtonRelease => {
                    let btn = Self::translate_button(event);
                    let mods = Self::x11_mod((*event).button.state);
                    if btn != MouseButton::Invalid {
                        Self::mouse_event(app, EventType::MouseUp, btn, mods);
                    }
                }
                xlib::EnterNotify => {
                    let mods = Self::x11_mod((*event).crossing.state);
                    Self::mouse_event(app, EventType::MouseEnter, MouseButton::Invalid, mods);
                }
                xlib::LeaveNotify => {
                    let mods = Self::x11_mod((*event).crossing.state);
                    Self::mouse_event(app, EventType::MouseLeave, MouseButton::Invalid, mods);
                }
                xlib::MotionNotify => {
                    app.mouse_x = (*event).motion.x as f32;
                    app.mouse_y = (*event).motion.y as f32;
                    let mods = Self::x11_mod((*event).motion.state);
                    Self::mouse_event(app, EventType::MouseMove, MouseButton::Invalid, mods);
                }
                xlib::ConfigureNotify => {
                    let width = (*event).configure.width as u32;
                    let height = (*event).configure.height as u32;
                    if width != app.window_width || height != app.window_height {
                        app.window_width = width;
                        app.window_height = height;
                        app.framebuffer_width = width;
                        app.framebuffer_height = height;
                        let event = app.new_event(EventType::Resized);
                        app.call_event(&event);
                    }
                }
                xlib::PropertyNotify => {
                    if (*event).property.state == xlib::PropertyNewValue
                        && (*event).property.atom == self.x11_wm_state
                    {
                        let state = self.get_window_state();
                        if state != self.window_state {
                            self.window_state = state;
                            if state == ICONIC_STATE as u32 {
                                let event = app.new_event(EventType::Iconified);
                                app.call_event(&event);
                            } else if state == NORMAL_STATE as u32 {
                                let event = app.new_event(EventType::Restored);
                                app.call_event(&event);
                            }
                        }
                    }
                }
                xlib::ClientMessage => {
                    if (*event).client_message.message_type == self.x11_wm_protocols {
                        let protocol = (*event).client_message.data.as_longs()[0] as u64;
                        if protocol == self.x11_wm_delete_window {
                            app.quit_requested = true;
                        }
                    }
                }
                xlib::DestroyNotify => {
                    // Nothing?
                }
                _ => (),
            }
        }
    }
}

pub fn run(desc: &Desc) {
    let mut app = WFApp::new(desc);
    let xlib = xlib::Xlib::open().unwrap();
    let glx = glx::Glx::open().unwrap();
    // TODO: Audit unsafe
    unsafe {
        (xlib.XInitThreads)();
        (xlib.XrmInitialize)();
        let display = (xlib.XOpenDisplay)(ptr::null());
        if display.is_null() {
            panic!("XOpenDisplay() failed!");
        }
        let screen = (xlib.XDefaultScreen)(display);
        let root = (xlib.XDefaultRootWindow)(display);
        (xlib.XkbSetDetectableAutoRepeat)(display, 1, ptr::null_mut());
        let dpi = Window::query_system_dpi();
        //let dpi_scale = dpi / 96.0;

        // Load some OpenGL extensions we need, in a kinda ghetto way.
        // Aieee, transmute!  Apparently that's the only way to get a C void* to
        // turn to a Rust fn() -- or an Option<fn()> works too.
        let glx_create_context_attribs_arb =
            mem::transmute(Window::getprocaddr(&glx, "glXCreateContextAttribsARB"));
        let glx_swap_interval_ext = mem::transmute(Window::getprocaddr(&glx, "glXSwapIntervalEXT"));
        let glx_swap_interval_mesa =
            mem::transmute(Window::getprocaddr(&glx, "glXSwapIntervalMESA"));

        let mut win = Window {
            display,
            screen,
            root,
            colormap: 0,
            window: 0,
            _dpi: dpi,
            window_state: 0,
            error_code: 0,
            xlib,
            keycodes: [false; 256],
            x11_utf8_string: 0,
            x11_wm_protocols: 0,
            x11_wm_delete_window: 0,
            x11_wm_state: 0,
            x11_net_wm_name: 0,
            x11_net_wm_icon_name: 0,

            //libgl: ptr::null(),
            //gl_major: 0,
            //gl_minor: 0,
            //eventbase: 0,
            //errorbase: 0,
            glx_ctx: ptr::null_mut(),
            glx_window: 0,
            glx,
            glx_create_context_attribs_arb,
            glx_swap_interval_ext,
            glx_swap_interval_mesa,
        };

        // Most of the window stuff is basically uninitalized, because
        // this is copied from C code, so here we initialize a pile of stuff
        // in it.
        win.init_extensions();
        println!("Extensions init'd");
        // win.glx_init();
        let (visual, depth) = win.choose_visual();
        win.create_window(&app, &mut *visual, depth);
        println!("Window created");
        win.glx_create_context();
        win.show_window();
        win.glx_swap_interval(app.swap_interval);
        (win.xlib.XFlush)(win.display);

        // Main event loop
        while !app.quit_ordered {
            win.glx_make_current();
            let count = (win.xlib.XPending)(win.display);
            for _ in 0..count {
                let event: &mut xlib::XEvent = &mut mem::zeroed();
                (win.xlib.XNextEvent)(win.display, event);
                win.process_event(&mut app, event);
            }
            app.frame();
            win.glx_swap_buffers();
            (win.xlib.XFlush)(win.display);
            // Handle quit requested, either from window or application
            if app.quit_requested && !app.quit_ordered {
                // Give user code a chance to intervene
                let event = app.new_event(EventType::QuitRequested);
                app.call_event(&event);
                // If we still want to quit, quit the app.
                if app.quit_requested {
                    app.quit_ordered = true;
                }
            }
        }
        // We might want this to still exist as a chance to destroy stuff
        // before we nuke all our window context...
        // app.call_cleanup()
        win.glx_destroy_context();
        win.destroy_window();
        (win.xlib.XCloseDisplay)(win.display);
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn xlib_open() {
        use x11_dl::xlib;
        let xl = xlib::Xlib::open().unwrap();
    }

    #[test]
    fn glx_open() {
        use x11_dl::glx;
        let glx = glx::Glx::open().unwrap();
    }
}