aboutsummaryrefslogtreecommitdiff
path: root/src/main.cpp
blob: f8a40e3107c1ebe86f7f7ac762e8badde9313cfe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
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

#include "../include/gui/StaticPage.hpp"
#include "../include/gui/ScrollablePage.hpp"
#include "../include/gui/DropdownButton.hpp"
#include "../include/gui/Button.hpp"
#include "../include/gui/Entry.hpp"
#include "../include/gui/ComboBox.hpp"
#include "../include/gui/Label.hpp"
#include "../include/gui/List.hpp"
#include "../include/Process.hpp"
#include "../include/Theme.hpp"
#include "../include/GsrInfo.hpp"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <libgen.h>
#include <signal.h>
#include <sys/wait.h>
#include <optional>
#include <signal.h>

#include <X11/Xlib.h>
#include <X11/cursorfont.h>
#include <X11/Xatom.h>
#include <X11/Xutil.h>

#include <mglpp/mglpp.hpp>
#include <mglpp/graphics/Font.hpp>
#include <mglpp/graphics/Text.hpp>
#include <mglpp/graphics/Texture.hpp>
#include <mglpp/graphics/Sprite.hpp>
#include <mglpp/graphics/Rectangle.hpp>
#include <mglpp/window/Window.hpp>
#include <mglpp/window/Event.hpp>
#include <mglpp/system/MemoryMappedFile.hpp>
#include <mglpp/system/Clock.hpp>

// TODO: If no compositor is running but a fullscreen application is running (on the focused monitor)
// then use xcomposite to get that window as a texture and use that as a background because then the background can update.
// That case can also happen when using a compositor but when the compositor gets turned off when running a fullscreen application.
// This can also happen after this overlay has started, in which case we want to update the background capture method.
// TODO: Update position when workspace changes (in dwm, sticky property handles it in real workspaces).
// TODO: Make keyboard controllable for steam deck (and other controllers).
// TODO: Keep track of gpu screen recorder run by other programs to not allow recording at the same time, or something.
// TODO: Remove gpu-screen-recorder-overlay-daemon and handle that alt+z global hotkey here instead, to show/hide the window
// without restaring the program. Or make the daemon handle gpu screen recorder program state and pass that to the overlay.
// TODO: Add systray by using org.kde.StatusNotifierWatcher/etc dbus directly.
// TODO: Dont allow replay and record/stream at the same time. If we want to allow that then do that in gpu screen recorder instead
// to make it more efficient by doing record/replay/stream with the same encoded packets.
// TODO: Make sure the overlay always stays on top. Test with starting the overlay and then opening youtube in fullscreen.

#include <vector>

extern "C" {
#include <mgl/mgl.h>
}

const mgl::Color bg_color(0, 0, 0, 160);

static void usage() {
    fprintf(stderr, "usage: window-overlay\n");
    exit(1);
}

static void startup_error(const char *msg) {
    fprintf(stderr, "Error: %s\n", msg);
    exit(1);
}

#define _NET_WM_STATE_REMOVE  0
#define _NET_WM_STATE_ADD     1
#define _NET_WM_STATE_TOGGLE  2

static Bool set_window_wm_state(Display *display, Window window, Atom atom) {
    Atom net_wm_state_atom = XInternAtom(display, "_NET_WM_STATE", False);
    if(!net_wm_state_atom) {
        fprintf(stderr, "Error: failed to find atom _NET_WM_STATE\n");
        return False;
    }

    XClientMessageEvent xclient;
    memset(&xclient, 0, sizeof(xclient));

    xclient.type = ClientMessage;
    xclient.window = window;
    xclient.message_type = net_wm_state_atom;
    xclient.format = 32;
    xclient.data.l[0] = _NET_WM_STATE_ADD;
    xclient.data.l[1] = atom;
    xclient.data.l[2] = 0;
    xclient.data.l[3] = 0;
    xclient.data.l[4] = 0;

    XSendEvent(display, DefaultRootWindow(display), False, SubstructureRedirectMask | SubstructureNotifyMask, (XEvent*)&xclient);
    XFlush(display);
    return True;
}

static Bool make_window_always_on_top(Display* display, Window window) {
    Atom net_wm_state_above_atom = XInternAtom(display, "_NET_WM_STATE_ABOVE", False);
    if(!net_wm_state_above_atom) {
        fprintf(stderr, "Error: failed to find atom _NET_WM_STATE_ABOVE\n");
        return False;
    }
    
    return set_window_wm_state(display, window, net_wm_state_above_atom);
}

static Bool make_window_sticky(Display* display, Window window) {
    Atom net_wm_state_sticky_atom = XInternAtom(display, "_NET_WM_STATE_STICKY", False);
    if(!net_wm_state_sticky_atom) {
        fprintf(stderr, "Error: failed to find atom _NET_WM_STATE_STICKY\n");
        return False;
    }
    
    return set_window_wm_state(display, window, net_wm_state_sticky_atom);
}

static bool is_compositor_running(Display *dpy, int screen) {
    char prop_name[20];
    snprintf(prop_name, sizeof(prop_name), "_NET_WM_CM_S%d", screen);
    Atom prop_atom = XInternAtom(dpy, prop_name, False);
    return XGetSelectionOwner(dpy, prop_atom) != None;
}

static mgl::Texture texture_from_ximage(XImage *img) {
    uint8_t *texture_data = (uint8_t*)malloc(img->width * img->height * 3);
    // TODO:

    for(int y = 0; y < img->height; ++y) {
        for(int x = 0; x < img->width; ++x) {
            unsigned long pixel = XGetPixel(img, x, y);
            unsigned char red = (pixel & img->red_mask) >> 16;
            unsigned char green = (pixel & img->green_mask) >> 8;
            unsigned char blue = pixel & img->blue_mask;

            const size_t texture_data_index = (x + y * img->width) * 3;
            texture_data[texture_data_index + 0] = red;
            texture_data[texture_data_index + 1] = green;
            texture_data[texture_data_index + 2] = blue;
        }
    }

    mgl::Texture texture;
    // TODO:
    texture.load_from_memory(texture_data, img->width, img->height, MGL_IMAGE_FORMAT_RGB);
    free(texture_data);
    return texture;
}

static sig_atomic_t running = 1;
static void sigint_handler(int dummy) {
    (void)dummy;
    running = 0;
}

static char hex_value_to_str(uint8_t v) {
    if(v <= 9)
        return '0' + v;
    else if(v >= 10 && v <= 15)
        return 'A' + (v - 10);
    else
        return '0';
}

// Excludes alpha
static std::string color_to_hex_str(mgl::Color color) {
    std::string result;
    result.resize(6);

    result[0] = hex_value_to_str((color.r & 0xF0) >> 4);
    result[1] = hex_value_to_str(color.r & 0x0F);

    result[2] = hex_value_to_str((color.g & 0xF0) >> 4);
    result[3] = hex_value_to_str(color.g & 0x0F);

    result[4] = hex_value_to_str((color.b & 0xF0) >> 4);
    result[5] = hex_value_to_str(color.b & 0x0F);

    return result;
}

/*
{
    {
        gsr::List::Orientation::VERTICAL,
        "Record area:",
        {
            {"Window, "window"},
            {"Focused window", "focused"},
        },
    },
    {
        gsr::List::Orientation::VERTICAL,
        "Video quality:",
        {
            {"Medium, "medium"},
            {"High", "high"},
            {"Very high", "very_high"},
        },
    }
}
*/

static void add_widgets_to_settings_page(mgl::Font &title_font, mgl::vec2i window_size, mgl::vec2f settings_page_position, mgl::vec2f settings_page_size, gsr::Page *settings_page, gsr::Page *settings_content_page, const gsr::GsrInfo &gsr_info, const std::vector<gsr::AudioDevice> &audio_devices, std::function<void()> settings_back_button_callback) {
auto back_button = std::make_unique<gsr::Button>(&title_font, "Back", mgl::vec2f(window_size.x / 10, window_size.y / 15), gsr::get_theme().scrollable_page_bg_color);
    back_button->set_position(settings_page_position + mgl::vec2f(settings_page_size.x + window_size.x / 50, 0.0f).floor());
    back_button->on_click = settings_back_button_callback;
    settings_page->add_widget(std::move(back_button));

    auto settings_list = std::make_unique<gsr::List>(gsr::List::Orientation::VERTICAL);
    settings_list->set_position(mgl::vec2f(50.0f, 50.0f));
    {
        auto record_area_list = std::make_unique<gsr::List>(gsr::List::Orientation::VERTICAL);
        {
            record_area_list->add_widget(std::make_unique<gsr::Label>(&title_font, "Record area:", gsr::get_theme().text_color));
            auto record_area_box = std::make_unique<gsr::ComboBox>(&title_font);
            // TODO: Show options not supported but disable them
            if(gsr_info.supported_capture_options.window)
                record_area_box->add_item("Window", "window");
            if(gsr_info.supported_capture_options.focused)
                record_area_box->add_item("Focused window", "focused");
            if(gsr_info.supported_capture_options.screen)
                record_area_box->add_item("All monitors", "screen");
            for(const auto &monitor : gsr_info.supported_capture_options.monitors) {
                char name[256];
                snprintf(name, sizeof(name), "%s (%dx%d)", monitor.name.c_str(), monitor.size.x, monitor.size.y);
                record_area_box->add_item(name, monitor.name);
            }
            if(gsr_info.supported_capture_options.portal)
                record_area_box->add_item("Desktop portal", "portal");
            record_area_list->add_widget(std::move(record_area_box));
        }
        settings_list->add_widget(std::move(record_area_list));

        auto audio_devices_list = std::make_unique<gsr::List>(gsr::List::Orientation::VERTICAL);
        {
            audio_devices_list->add_widget(std::make_unique<gsr::Label>(&title_font, "Audio:", gsr::get_theme().text_color));
            auto audio_device_box = std::make_unique<gsr::ComboBox>(&title_font);
            for(const auto &audio_device : audio_devices) {
                audio_device_box->add_item(audio_device.description, audio_device.name);
            }
            audio_devices_list->add_widget(std::move(audio_device_box));
        }
        settings_list->add_widget(std::move(audio_devices_list));

        auto quality_list = std::make_unique<gsr::List>(gsr::List::Orientation::HORIZONTAL);
        {
            auto video_quality_list = std::make_unique<gsr::List>(gsr::List::Orientation::VERTICAL);
            {
                video_quality_list->add_widget(std::make_unique<gsr::Label>(&title_font, "Video quality:", gsr::get_theme().text_color));
                auto video_quality_box = std::make_unique<gsr::ComboBox>(&title_font);
                video_quality_box->add_item("Medium", "medium");
                video_quality_box->add_item("High (Recommended for live streaming)", "high");
                video_quality_box->add_item("Very high (Recommended)", "very_high");
                video_quality_box->add_item("Ultra", "ultra");
                video_quality_list->add_widget(std::move(video_quality_box));
            }
            quality_list->add_widget(std::move(video_quality_list));

            auto color_range_list = std::make_unique<gsr::List>(gsr::List::Orientation::VERTICAL);
            {
                color_range_list->add_widget(std::make_unique<gsr::Label>(&title_font, "Color range:", gsr::get_theme().text_color));
                auto color_range_box = std::make_unique<gsr::ComboBox>(&title_font);
                color_range_box->add_item("Limited", "limited");
                color_range_box->add_item("Full", "full");
                color_range_list->add_widget(std::move(color_range_box));
            }
            quality_list->add_widget(std::move(color_range_list));

            auto framerate_list = std::make_unique<gsr::List>(gsr::List::Orientation::VERTICAL);
            {
                framerate_list->add_widget(std::make_unique<gsr::Label>(&title_font, "Frame rate:", gsr::get_theme().text_color));
                //create_entry_validator_integer_in_range
                auto framerate_entry = std::make_unique<gsr::Entry>(&title_font, "60", title_font.get_character_size() * 2);
                framerate_entry->validate_handler = gsr::create_entry_validator_integer_in_range(1, 500);
                framerate_list->add_widget(std::move(framerate_entry));
            }
            quality_list->add_widget(std::move(framerate_list));
        }
        settings_list->add_widget(std::move(quality_list));

        auto codec_list = std::make_unique<gsr::List>(gsr::List::Orientation::HORIZONTAL);
        {
            auto video_codec_list = std::make_unique<gsr::List>(gsr::List::Orientation::VERTICAL);
            {
                video_codec_list->add_widget(std::make_unique<gsr::Label>(&title_font, "Video codec:", gsr::get_theme().text_color));
                auto video_codec_box = std::make_unique<gsr::ComboBox>(&title_font);
                // TODO: Show options not supported but disable them
                video_codec_box->add_item("Auto (Recommended)", "auto");
                if(gsr_info.supported_video_codecs.h264)
                    video_codec_box->add_item("H264", "h264");
                if(gsr_info.supported_video_codecs.hevc)
                    video_codec_box->add_item("HEVC", "hevc");
                if(gsr_info.supported_video_codecs.av1)
                    video_codec_box->add_item("AV1", "av1");
                if(gsr_info.supported_video_codecs.vp8)
                    video_codec_box->add_item("VP8", "vp8");
                if(gsr_info.supported_video_codecs.vp9)
                    video_codec_box->add_item("VP9", "vp9");
                // TODO: Add hdr options
                video_codec_box->add_item("H264 Software Encoder (Slow, not recommended)", "h264_software");
                video_codec_list->add_widget(std::move(video_codec_box));
            }
            codec_list->add_widget(std::move(video_codec_list));

            auto audio_codec_list = std::make_unique<gsr::List>(gsr::List::Orientation::VERTICAL);
            {
                audio_codec_list->add_widget(std::make_unique<gsr::Label>(&title_font, "Audio codec:", gsr::get_theme().text_color));
                auto audio_codec_box = std::make_unique<gsr::ComboBox>(&title_font);
                audio_codec_box->add_item("Opus (Recommended)", "opus");
                audio_codec_box->add_item("AAC", "aac");
                audio_codec_list->add_widget(std::move(audio_codec_box));
            }
            codec_list->add_widget(std::move(audio_codec_list));
        }
        settings_list->add_widget(std::move(codec_list));

        auto framerate_mode_list = std::make_unique<gsr::List>(gsr::List::Orientation::VERTICAL);
        {
            framerate_mode_list->add_widget(std::make_unique<gsr::Label>(&title_font, "Frame rate mode:", gsr::get_theme().text_color));
            auto framerate_mode_box = std::make_unique<gsr::ComboBox>(&title_font);
            framerate_mode_box->add_item("Auto (Recommended)", "auto");
            framerate_mode_box->add_item("Constant", "cfr");
            framerate_mode_box->add_item("Variable", "vfr");
            framerate_mode_list->add_widget(std::move(framerate_mode_box));
        }
        settings_list->add_widget(std::move(framerate_mode_list));

        auto file_list = std::make_unique<gsr::List>(gsr::List::Orientation::HORIZONTAL);
        {
            auto save_directory_list = std::make_unique<gsr::List>(gsr::List::Orientation::VERTICAL);
            {
                save_directory_list->add_widget(std::make_unique<gsr::Label>(&title_font, "Directory to save the video:", gsr::get_theme().text_color));
                auto directory_selection_box = std::make_unique<gsr::ComboBox>(&title_font);
                // TODO:
                directory_selection_box->add_item("/home/dec05eba/Videos", "mp4");
                save_directory_list->add_widget(std::move(directory_selection_box));
            }
            file_list->add_widget(std::move(save_directory_list));

            auto container_list = std::make_unique<gsr::List>(gsr::List::Orientation::VERTICAL);
            {
                container_list->add_widget(std::make_unique<gsr::Label>(&title_font, "Container:", gsr::get_theme().text_color));
                auto container_box = std::make_unique<gsr::ComboBox>(&title_font);
                container_box->add_item("mp4", "mp4");
                container_box->add_item("mkv", "matroska");
                container_box->add_item("flv", "flv");
                container_box->add_item("mov", "mov");
                container_box->add_item("ts", "mpegts");
                container_box->add_item("m3u8", "hls");
                container_list->add_widget(std::move(container_box));
            }
            file_list->add_widget(std::move(container_list));
        }
        settings_list->add_widget(std::move(file_list));
    }
    settings_content_page->add_widget(std::move(settings_list));
}

int main(int argc, char **argv) {
    if(argc != 1)
        usage();

    signal(SIGINT, sigint_handler);

    gsr::GsrInfo gsr_info;
    // TODO:
    gsr::GsrInfoExitStatus gsr_info_exit_status = gsr::get_gpu_screen_recorder_info(&gsr_info);
    if(gsr_info_exit_status != gsr::GsrInfoExitStatus::OK) {
        fprintf(stderr, "error: failed to get gpu-screen-recorder info\n");
        exit(1);
    }

    const std::vector<gsr::AudioDevice> audio_devices = gsr::get_audio_devices();

    gsr::init_theme(gsr_info);

    std::string program_root_dir = dirname(argv[0]);
    if(!program_root_dir.empty() && program_root_dir.back() != '/')
        program_root_dir += '/';
    program_root_dir += "../../../";

    mgl::Init init;
    Display *display = (Display*)mgl_get_context()->connection;

    // TODO: Put window on the focused monitor right side and update when monitor changes resolution or other modes.
    // Use monitor size instead of screen size.
    // mgl now has monitor events so this can be handled directly with mgl.

    mgl::vec2i target_monitor_size = { WidthOfScreen(DefaultScreenOfDisplay(display)), HeightOfScreen(DefaultScreenOfDisplay(display)) };

    const mgl::vec2i window_size = { target_monitor_size.x, target_monitor_size.y };
    const mgl::vec2i window_pos = { 0, 0 };

    mgl::Window::CreateParams window_create_params;
    window_create_params.size = window_size;
    window_create_params.min_size = window_size;
    window_create_params.max_size = window_size;
    window_create_params.position = window_pos;
    window_create_params.hidden = true;
    //window_create_params.override_redirect = true;
    window_create_params.background_color = bg_color;
    window_create_params.support_alpha = true;
    window_create_params.window_type = MGL_WINDOW_TYPE_DIALOG;

    mgl::Window window;
    if(!window.create("gsr overlay", window_create_params))
        startup_error("failed to create window");

    unsigned char data = 2; // Prefer being composed to allow transparency
    XChangeProperty(display, window.get_system_handle(), XInternAtom(display, "_NET_WM_BYPASS_COMPOSITOR", False), XA_CARDINAL, 32, PropModeReplace, &data, 1);

    data = 1;
    XChangeProperty(display, window.get_system_handle(), XInternAtom(display, "GAMESCOPE_EXTERNAL_OVERLAY", False), XA_CARDINAL, 32, PropModeReplace, &data, 1);

    mgl::MemoryMappedFile title_font_file;
    if(!title_font_file.load("/usr/share/fonts/noto/NotoSans-Bold.ttf", mgl::MemoryMappedFile::LoadOptions{true, false}))
        startup_error("failed to load file: fonts/Orbitron-Bold.ttf");

    mgl::MemoryMappedFile font_file;
    if(!font_file.load("/usr/share/fonts/noto/NotoSans-Regular.ttf", mgl::MemoryMappedFile::LoadOptions{true, false}))
        startup_error("failed to load file: fonts/Orbitron-Regular.ttf");

    mgl::Font top_bar_font;
    if(!top_bar_font.load_from_file(title_font_file, window_create_params.size.y * 0.03f))
        startup_error("failed to load font: fonts/Orbitron-Bold.ttf");

    mgl::Font title_font;
    if(!title_font.load_from_file(title_font_file, window_create_params.size.y * 0.019f))
        startup_error("failed to load font: fonts/Orbitron-Bold.ttf");

    mgl::Font font;
    if(!font.load_from_file(font_file, window_create_params.size.y * 0.015f))
        startup_error("failed to load font: fonts/Orbitron-Regular.ttf");

    mgl::Texture replay_button_texture;
    if(!replay_button_texture.load_from_file((program_root_dir + "images/replay.png").c_str()))
        startup_error("failed to load texture: images/replay.png");

    mgl::Texture record_button_texture;
    if(!record_button_texture.load_from_file((program_root_dir + "images/record.png").c_str()))
        startup_error("failed to load texture: images/record.png");

    mgl::Texture stream_button_texture;
    if(!stream_button_texture.load_from_file((program_root_dir + "images/stream.png").c_str()))
        startup_error("failed to load texture: images/stream.png");

    // TODO: Get size from monitor, get region specific to the monitor
    mgl::Texture screenshot_texture;
    if(!is_compositor_running(display, 0)) {
        XImage *img = XGetImage(display, DefaultRootWindow(display), window_pos.x, window_pos.y, window_size.x, window_size.y, AllPlanes, ZPixmap);
        if(!img)
            fprintf(stderr, "Error: failed to take a screenshot\n");

        if(img) {
            screenshot_texture = texture_from_ximage(img);
            XDestroyImage(img);
            img = NULL;
        }
    }

    mgl::Sprite screenshot_sprite;
    if(screenshot_texture.is_valid())
        screenshot_sprite.set_texture(&screenshot_texture);

    mgl::Rectangle bg_screenshot_overlay(window.get_size().to_vec2f());
    bg_screenshot_overlay.set_color(bg_color);

    gsr::StaticPage front_page(window_size.to_vec2f());

    const mgl::vec2f settings_page_size(window_size.x * 0.3333f, window_size.y * 0.7f);
    const mgl::vec2f settings_page_position = (window_size.to_vec2f() * 0.5f - settings_page_size * 0.5f).floor();

    auto replay_settings_content = std::make_unique<gsr::ScrollablePage>(settings_page_size);
    gsr::ScrollablePage *replay_settings_content_ptr = replay_settings_content.get();
    replay_settings_content->set_position(settings_page_position);

    auto record_settings_content = std::make_unique<gsr::ScrollablePage>(settings_page_size);
    gsr::ScrollablePage *record_settings_content_ptr = record_settings_content.get();
    record_settings_content->set_position(settings_page_position);

    auto stream_settings_content = std::make_unique<gsr::ScrollablePage>(settings_page_size);
    gsr::ScrollablePage *stream_settings_content_ptr = stream_settings_content.get();
    stream_settings_content->set_position(settings_page_position);

    gsr::StaticPage replay_settings_page(window_size.to_vec2f());
    replay_settings_page.add_widget(std::move(replay_settings_content));

    gsr::StaticPage record_settings_page(window_size.to_vec2f());
    record_settings_page.add_widget(std::move(record_settings_content));

    gsr::StaticPage stream_settings_page(window_size.to_vec2f());
    stream_settings_page.add_widget(std::move(stream_settings_content));

    gsr::Page *current_page = &front_page;

    struct MainButton {
        gsr::DropdownButton* button;
        gsr::GsrMode mode;
    };

    const int num_frontpage_buttons = 3;

    const char *titles[num_frontpage_buttons] = {
        "Instant Replay",
        "Record",
        "Livestream"
    };

    const char *descriptions_off[num_frontpage_buttons] = {
        "Off",
        "Not recording",
        "Not streaming"
    };

    const char *descriptions_on[num_frontpage_buttons] = {
        "On",
        "Recording",
        "Streaming"
    };

    mgl::Texture *textures[num_frontpage_buttons] = {
        &replay_button_texture,
        &record_button_texture,
        &stream_button_texture
    };

    const int button_height = window_create_params.size.y / 5.0f;
    const int button_width = button_height;

    std::vector<MainButton> main_buttons;

    for(int i = 0; i < num_frontpage_buttons; ++i) {
        auto button = std::make_unique<gsr::DropdownButton>(&title_font, &font, titles[i], descriptions_on[i], descriptions_off[i], textures[i], mgl::vec2f(button_width, button_height));
        button->add_item("Start", "start");
        button->add_item("Settings", "settings");
        gsr::DropdownButton *button_ptr = button.get();
        front_page.add_widget(std::move(button));

        MainButton main_button = {
            button_ptr,
            gsr::GsrMode::Unknown
        };

        main_buttons.push_back(std::move(main_button));
    }

    gsr::GsrMode gsr_mode = gsr::GsrMode::Unknown;

    auto update_overlay_shape = [&]() {
        fprintf(stderr, "update overlay shape!\n");
        const int spacing = 0;// * get_config().scale;
        const int combined_spacing = spacing * std::max(0, (int)main_buttons.size() - 1);

        const int per_button_width = main_buttons[0].button->get_size().x;// * get_config().scale;
        const mgl::vec2i overlay_desired_size(per_button_width * (int)main_buttons.size() + combined_spacing, main_buttons[0].button->get_size().y);

        const mgl::vec2i main_buttons_start_pos = mgl::vec2i(window_create_params.size.x*0.5f, window_create_params.size.y*0.25f) - overlay_desired_size/2;
        mgl::vec2i main_button_pos = main_buttons_start_pos;

        // if(!gsr_mode.has_value()) {
        //     gsr_mode = gsr::GsrMode::Unknown;
        //     pid_t gpu_screen_recorder_process = -1;
        //     gsr::is_gpu_screen_recorder_running(gpu_screen_recorder_process, gsr_mode.value());
        // }

        for(size_t i = 0; i < main_buttons.size(); ++i) {
            if(main_buttons[i].mode != gsr::GsrMode::Unknown && main_buttons[i].mode == gsr_mode) {
                main_buttons[i].button->set_activated(true);
            } else {
                main_buttons[i].button->set_activated(false);
            }

            main_buttons[i].button->set_position(main_button_pos.to_vec2f());
            main_button_pos.x += per_button_width + combined_spacing;
        }
    };

    // Replay
    main_buttons[0].button->on_click = [&](const std::string &id) {
        if(id == "settings") {
            current_page = &replay_settings_page;
            return;
        }
        /*
        char window_to_record_str[32];
        snprintf(window_to_record_str, sizeof(window_to_record_str), "%ld", target_window);

        const char *args[] = {
            "gpu-screen-recorder", "-w", window_to_record_str,
            "-c", "mp4",
            "-f", "60",
            "-o", "/home/dec05eba/Videos/gpu-screen-recorder.mp4",
            nullptr
        };
        gsr::exec_program_daemonized(args);
        */
    };
    main_buttons[0].mode = gsr::GsrMode::Replay;

    // TODO: Monitor /tmp/gpu-screen-recorder and update ui to match state

    pid_t gpu_screen_recorder_process = -1;
    // Record
    main_buttons[1].button->on_click = [&](const std::string &id) {
        if(id == "settings") {
            current_page = &record_settings_page;
            return;
        }

        if(id != "start")
            return;

        // window.close();
        // usleep(1000 * 50); // 50 milliseconds

        const std::string tint_color_as_hex = color_to_hex_str(gsr::get_theme().tint_color);

        if(gpu_screen_recorder_process != -1) {
            kill(gpu_screen_recorder_process, SIGINT);
            int status;
            if(waitpid(gpu_screen_recorder_process, &status, 0) == -1) {
                perror("waitpid failed");
                /* Ignore... */
            }
            // window.set_visible(false);
            // window.close();
            // return;
            //exit(0);
            gpu_screen_recorder_process = -1;
            gsr_mode = gsr::GsrMode::Unknown;
            update_overlay_shape();
            main_buttons[1].button->set_item_label(id, "Start");

            // TODO: Show this with a slight delay to make sure it doesn't show up in the video
            const char *notification_args[] = {
                "gpu-screen-recorder-notification", "--text", "Recording has been saved", "--timeout", "3.0",
                "--icon", "./images/record.png",
                "--icon-color", "ffffff", "--bg-color", tint_color_as_hex.c_str(),
                nullptr
            };
            gsr::exec_program_daemonized(notification_args);
            return;
        }

        const char *args[] = {
            "gpu-screen-recorder", "-w", "screen",
            "-c", "mp4",
            "-f", "60",
            "-o", "/home/dec05eba/Videos/gpu-screen-recorder.mp4",
            nullptr
        };
        gpu_screen_recorder_process = gsr::exec_program(args);
        if(gpu_screen_recorder_process == -1) {
            // TODO: Show notification failed to start
        } else {
            gsr_mode = gsr::GsrMode::Record;
            update_overlay_shape();
            main_buttons[1].button->set_item_label(id, "Stop");
        }

        // TODO: Start recording after this notification has disappeared to make sure it doesn't show up in the video.
        // Make clear to the user that the recording starts after the notification is gone.
        // Maybe have the option in notification to show timer until its getting hidden, then the notification can say:
        // Starting recording in 3...
        // 2...
        // 1...
        // TODO: Do not run this is a daemon. Instead get the pid and when launching another notification close the current notification
        // program and start another one. This can also be used to check when the notification has finished by checking with waitpid NOWAIT
        // to see when the program has exit.
        const char *notification_args[] = {
            "gpu-screen-recorder-notification", "--text", "Recording has started", "--timeout", "3.0",
            "--icon", "./images/record.png",
            "--icon-color", tint_color_as_hex.c_str(), "--bg-color", tint_color_as_hex.c_str(),
            nullptr
        };
        gsr::exec_program_daemonized(notification_args);
        //exit(0);
        // window.set_visible(false);
        // window.close();

        // TODO: Show notification with args:
        // "Recording has started" 3.0 ./images/record.png 76b900
    };
    main_buttons[1].mode = gsr::GsrMode::Record;

    // Stream
    main_buttons[2].button->on_click = [&](const std::string &id) {
        if(id == "settings") {
            current_page = &stream_settings_page;
            return;
        }
    };
    main_buttons[2].mode = gsr::GsrMode::Stream;

    update_overlay_shape();

    window.set_visible(true);
    make_window_always_on_top(display, window.get_system_handle());
    make_window_sticky(display, window.get_system_handle());

    Cursor default_cursor = XCreateFontCursor(display, XC_arrow);

    // TODO: Retry if these fail.
    // TODO: Hmm, these dont work in owlboy. Maybe owlboy uses xi2 and that breaks this (does it?).
    XGrabPointer(display, window.get_system_handle(), True,
       ButtonPressMask | ButtonReleaseMask | PointerMotionMask |
       Button1MotionMask | Button2MotionMask | Button3MotionMask | Button4MotionMask | Button5MotionMask |
       ButtonMotionMask,
       GrabModeAsync, GrabModeAsync, None, default_cursor, CurrentTime);
    XGrabKeyboard(display, window.get_system_handle(), True, GrabModeAsync, GrabModeAsync, CurrentTime);

    XSetInputFocus(display, window.get_system_handle(), RevertToParent, CurrentTime);
    XFlush(display);

    window.set_fullscreen(true);

    //XGrabServer(display);

    mgl::Rectangle top_bar_background(mgl::vec2f(window.get_size().x, window.get_size().y*0.06f).floor());
    top_bar_background.set_color(mgl::Color(0, 0, 0, 180));

    mgl::Text top_bar_text("GPU Screen Recorder", top_bar_font);
    //top_bar_text.set_color(gsr::get_theme().tint_color);
    top_bar_text.set_position((top_bar_background.get_position() + top_bar_background.get_size()*0.5f - top_bar_text.get_bounds().size*0.5f).floor());

    const int num_settings_pages = 3;

    gsr::Page *settings_pages[num_settings_pages] = {
        &replay_settings_page,
        &record_settings_page,
        &stream_settings_page
    };

    gsr::Page *settings_content_pages[num_settings_pages] = {
        replay_settings_content_ptr,
        record_settings_content_ptr,
        stream_settings_content_ptr
    };

    const auto settings_back_button_callback = [&] {
        current_page = &front_page;
    };

    for(int i = 0; i < num_settings_pages; ++i) {
        gsr::Page *settings_page = settings_pages[i];
        gsr::Page *settings_content_page = settings_content_pages[i];
        add_widgets_to_settings_page(title_font, window_size, settings_page_position, settings_page_size, settings_page, settings_content_page, gsr_info, audio_devices, settings_back_button_callback);
    }

    mgl::Texture close_texture;
    if(!close_texture.load_from_file("images/cross.png"))
        startup_error("failed to load texture: images/cross.png");

    mgl::Sprite close_sprite(&close_texture);
    close_sprite.set_height(int(top_bar_background.get_size().y * 0.3f));
    close_sprite.set_position(mgl::vec2f(window.get_size().x - close_sprite.get_size().x - 50.0f, top_bar_background.get_size().y * 0.5f - close_sprite.get_size().y * 0.5f).floor());

    mgl::Texture logo_texture;
    if(!logo_texture.load_from_file("images/gpu_screen_recorder_logo.png"))
        startup_error("failed to load texture: images/gpu_screen_recorder_logo.png");

    mgl::Sprite logo_sprite(&logo_texture);
    logo_sprite.set_height((int)(top_bar_background.get_size().y * 0.65f));

    logo_sprite.set_position(mgl::vec2f(
        (top_bar_background.get_size().y - logo_sprite.get_size().y) * 0.5f,
        top_bar_background.get_size().y * 0.5f - logo_sprite.get_size().y * 0.5f
    ).floor());

    // mgl::Clock state_update_timer;
    // const double state_update_timeout_sec = 2.0;

    mgl::Event event;

    event.type = mgl::Event::MouseMoved;
    event.mouse_move.x = window.get_mouse_position().x;
    event.mouse_move.y = window.get_mouse_position().y;
    current_page->on_event(event, window, mgl::vec2f(0.0f, 0.0f));

    const auto render = [&] {
        window.clear(bg_color);
        if(screenshot_texture.is_valid()) {
            window.draw(screenshot_sprite);
            window.draw(bg_screenshot_overlay);
        }
        window.draw(top_bar_background);
        window.draw(top_bar_text);
        window.draw(logo_sprite);
        window.draw(close_sprite);
        current_page->draw(window, mgl::vec2f(0.0f, 0.0f));
        window.display();
    };

    while(window.is_open()) {
        if(!running) {
            window.set_visible(false);
            window.close();
            break;
        }

        while(window.poll_event(event)) {
            current_page->on_event(event, window, mgl::vec2f(0.0f, 0.0f));
            if(event.type == mgl::Event::KeyPressed) {
                if(event.key.code == mgl::Keyboard::Escape) {
                    window.set_visible(false);
                    window.close();
                    break;
                }
            }
        }

        // if(state_update_timer.get_elapsed_time_seconds() >= state_update_timeout_sec) {
        //     state_update_timer.restart();
        //     update_overlay_shape();
        // }

        render();
    }

    fprintf(stderr, "shutting down!\n");

    if(gpu_screen_recorder_process != -1) {
        kill(gpu_screen_recorder_process, SIGINT);
        int status;
        if(waitpid(gpu_screen_recorder_process, &status, 0) == -1) {
            perror("waitpid failed");
            /* Ignore... */
        }
        gpu_screen_recorder_process = -1;
    }

    gsr::deinit_theme();
    return 0;
}