aboutsummaryrefslogtreecommitdiff
path: root/src/main.cpp
blob: ac0b7dd298810f61c247915d0465317dce297aad (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
#include "../include/gui/Button.hpp"
#include "../include/window_texture.h"
#include "../include/Process.hpp"

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

#include <X11/Xlib.h>
#include <X11/cursorfont.h>
#include <X11/extensions/shape.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/window/Window.hpp>
#include <mglpp/window/Event.hpp>
#include <mglpp/system/MemoryMappedFile.hpp>

#include <vector>

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

struct Config {
    float scale = 1.0f;
};

static Config& get_config() {
    static Config config;
    return config;
}

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

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

static bool string_to_i64(const char *str, int64_t *result) {
    errno = 0;
    char *endptr = nullptr;
    int64_t res = strtoll(str, &endptr, 0);
    if(endptr == str || errno != 0)
        return false;

    *result = res;
    return true;
}

static void window_texture_get_size_or(WindowTexture *window_texture, int *width, int *height, int fallback_width, int fallback_height) {
    Window root_window;
    int x, y;
    unsigned int w = 0, h = 0;
    unsigned int border_width, depth;
    if(!XGetGeometry(window_texture->display, window_texture->pixmap, &root_window, &x, &y, &w, &h, &border_width, &depth) || w == 0 || h == 0) {
        *width = fallback_width;
        *height = fallback_height;
    } else {
        *width = w;
        *height = h;
    }
}

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

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

    int64_t target_window;
    if(!string_to_i64(argv[1], &target_window)) {
        fprintf(stderr, "Error: invalid number '%s' was specific for window argument\n", argv[1]);
        return 1;
    }

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

    XWindowAttributes target_win_attr;
    memset(&target_win_attr, 0, sizeof(target_win_attr));
    if(!XGetWindowAttributes(display, target_window, &target_win_attr)) {
        fprintf(stderr, "Error: window argument %s is not a valid window\n", argv[1]);
        return 1;
    }

    XSelectInput(display, target_window, StructureNotifyMask | VisibilityChangeMask);

    mgl::vec2i target_window_size = { target_win_attr.width, target_win_attr.height };
    get_config().scale = std::min(1.0, (double)target_win_attr.width / 1920.0);

    mgl::Window::CreateParams window_create_params;
    window_create_params.size = target_window_size;
    window_create_params.parent_window = target_window;
    window_create_params.hidden = true;

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

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

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

    mgl::Font title_font;
    if(!title_font.load_from_file(title_font_file, 32 * get_config().scale))
        startup_error("failed to load font: fonts/Orbitron-Bold.ttf");

    mgl::Font font;
    if(!font.load_from_file(font_file, 20 * get_config().scale))
        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");

    struct MainButton {
        mgl::Text title;
        mgl::Text description;
        mgl::Sprite icon;
        gsr::Button button;
        gsr::GsrMode mode;
    };

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

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

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

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

    std::vector<MainButton> main_buttons;
    std::vector<XRectangle> shapes;

    for(int i = 0; i < 3; ++i) {
        mgl::Text title(titles[i], {0.0f, 0.0f}, title_font);
        title.set_color(mgl::Color(255, 255, 255));

        mgl::Text description(descriptions_off[i], {0.0f, 0.0f}, font);
        description.set_color(mgl::Color(255, 255, 255, 150));

        mgl::Sprite sprite(textures[i]);
        sprite.set_scale(get_config().scale);
        gsr::Button button(mgl::vec2f(425 * get_config().scale, 300 * get_config().scale).floor());

        MainButton main_button = {
            std::move(title),
            std::move(description),
            std::move(sprite),
            std::move(button),
            gsr::GsrMode::Unknown
        };

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

    // Settings button
    shapes.push_back({});

    // Replay
    main_buttons[0].button.on_click = [&]() {
        /*
        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

    // Record
    main_buttons[1].button.on_click = [&]() {
        int gpu_screen_recorder_process = -1;
        gsr::GsrMode gsr_mode = gsr::GsrMode::Unknown;
        if(gsr::is_gpu_screen_recorder_running(gpu_screen_recorder_process, gsr_mode) && gpu_screen_recorder_process > 0) {
            kill(gpu_screen_recorder_process, SIGINT);
            int status;
            if(waitpid(gpu_screen_recorder_process, &status, 0) == -1) {
                perror("waitpid failed");
                /* Ignore... */
            }
            exit(0);
        }

        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);
        exit(0);
    };
    main_buttons[1].mode = gsr::GsrMode::Record;

    main_buttons[2].mode = gsr::GsrMode::Stream;

    const int per_button_width = 425 * get_config().scale;
    const mgl::vec2i overlay_desired_size(per_button_width * (int)main_buttons.size(), 300 * get_config().scale);

    XGCValues xgcv;
    xgcv.foreground = WhitePixel(display, DefaultScreen(display));
    xgcv.line_width = 1;
    xgcv.line_style = LineSolid;

    Pixmap shape_pixmap = None;
    GC shape_gc = None;

    auto update_overlay_shape = [&]() {
        const int main_button_margin = 20 * get_config().scale;
        const mgl::vec2i main_buttons_start_pos = target_window_size/2 - overlay_desired_size/2;
        mgl::vec2i main_button_pos = main_buttons_start_pos;

        int gpu_screen_recorder_process = -1;
        gsr::GsrMode gsr_mode = gsr::GsrMode::Unknown;
        gsr::is_gpu_screen_recorder_running(gpu_screen_recorder_process, gsr_mode);

        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].description.set_string(descriptions_on[i]);
                main_buttons[i].description.set_color(mgl::Color(118, 185, 0));
                main_buttons[i].icon.set_color(mgl::Color(118, 185, 0));
            } else {
                main_buttons[i].description.set_string(descriptions_off[i]);
                main_buttons[i].description.set_color(mgl::Color(255, 255, 255));
                main_buttons[i].icon.set_color(mgl::Color(255, 255, 255));
            }

            main_buttons[i].title.set_position(
                mgl::vec2f(
                    main_button_pos.x + per_button_width * 0.5f - main_buttons[i].title.get_bounds().size.x * 0.5f,
                    main_button_pos.y + main_button_margin).floor());

            main_buttons[i].description.set_position(
                mgl::vec2f(
                    main_button_pos.x + per_button_width * 0.5f - main_buttons[i].description.get_bounds().size.x * 0.5f,
                    main_button_pos.y + overlay_desired_size.y - main_buttons[i].description.get_bounds().size.y - main_button_margin).floor());

            main_buttons[i].icon.set_position(
                mgl::vec2f(
                    main_button_pos.x + per_button_width * 0.5f - main_buttons[i].icon.get_texture()->get_size().x * main_buttons[i].icon.get_scale().x * 0.5f,
                    main_button_pos.y + overlay_desired_size.y * 0.5f - main_buttons[i].icon.get_texture()->get_size().y * main_buttons[i].icon.get_scale().y * 0.5f).floor());

            main_buttons[i].button.set_position(main_button_pos.to_vec2f());
            shapes[i] = {
                (short)main_button_pos.x, (short)main_button_pos.y, (unsigned short)per_button_width, (unsigned short)overlay_desired_size.y
            };
            main_button_pos.x += per_button_width;
        }

        const mgl::vec2i settings_button_size(128 * get_config().scale, 128 * get_config().scale);
        shapes[main_buttons.size()] = {
            (short)(main_buttons_start_pos.x + overlay_desired_size.x), (short)(main_buttons_start_pos.y - settings_button_size.y),
            (unsigned short)settings_button_size.x, (unsigned short)settings_button_size.y
        };

        if(shape_pixmap) {
            XFreePixmap(display, shape_pixmap);
            shape_pixmap = None;
        }

        if(shape_gc) {
            XFreeGC(display, shape_gc);
            shape_gc = None;
        }

        shape_pixmap = XCreatePixmap(display, window.get_system_handle(), target_window_size.x, target_window_size.y, 1);
        if(!shape_pixmap)
            fprintf(stderr, "Error: failed to create shape pixmap\n");

        shape_gc = XCreateGC(display, shape_pixmap, 0, &xgcv);

        XSetForeground(display, shape_gc, 0);
        XFillRectangle(display, shape_pixmap, shape_gc, 0, 0, target_window_size.x, target_window_size.y);

        XSetForeground(display, shape_gc, 1);
        XDrawRectangles(display, shape_pixmap, shape_gc, shapes.data(), shapes.size());
        XFillRectangles(display, shape_pixmap, shape_gc, shapes.data(), shapes.size());

        XShapeCombineMask(display, window.get_system_handle(), ShapeBounding, 0, 0, shape_pixmap, ShapeSet);
    };

    update_overlay_shape();
    window.set_visible(true);

    Cursor default_cursor = XCreateFontCursor(display, XC_arrow);

    // TODO: Retry if these fail
    XGrabPointer(display, window.get_system_handle(), True, ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, default_cursor, CurrentTime);
    XGrabKeyboard(display, window.get_system_handle(), True, GrabModeAsync, GrabModeAsync, CurrentTime);

    XEvent xev;
    mgl::Event event;

    while(window.is_open()) {
        if(XCheckTypedWindowEvent(display, target_window, DestroyNotify, &xev)) {
            window.close();
            break;
        }

        if(XCheckTypedWindowEvent(display, target_window, VisibilityNotify, &xev)) {
            if(xev.xvisibility.state) {
                
            }
        }

        if(XCheckTypedWindowEvent(display, target_window, ConfigureNotify, &xev) && (xev.xconfigure.width != target_window_size.x || xev.xconfigure.height != target_window_size.y)) {
            while(XCheckTypedWindowEvent(display, target_window, ConfigureNotify, &xev)) {}
            target_window_size.x = xev.xconfigure.width;
            target_window_size.y = xev.xconfigure.height;
            window.set_size(target_window_size);
            update_overlay_shape();
        }

        if(window.poll_event(event)) {
            for(auto &main_button : main_buttons) {
                main_button.button.on_event(event, window);
            }

            if(event.type == mgl::Event::KeyReleased) {
                if(event.key.code == mgl::Keyboard::Escape) {
                    window.close();
                    break;
                }
            }
        }

        window.clear(mgl::Color(37, 43, 47));
        for(auto &main_button : main_buttons) {
            main_button.button.draw(window);
            window.draw(main_button.icon);
            window.draw(main_button.title);
            window.draw(main_button.description);
        }
        window.display();
    }

    return 0;
}