aboutsummaryrefslogtreecommitdiff
path: root/src/main.cpp
blob: 4a36fe70d23705b59c1d14599ef172ef206c4666 (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
#include "../include/GsrInfo.hpp"
#include "../include/Theme.hpp"
#include "../include/window_texture.h"
#include "../include/Overlay.hpp"
#include "../include/GlobalHotkeysX11.hpp"
#include "../include/GlobalHotkeysLinux.hpp"
#include "../include/gui/Utils.hpp"

#include <unistd.h>
#include <signal.h>
#include <sys/socket.h>
#include <thread>

#include <X11/keysym.h>
#include <mglpp/mglpp.hpp>
#include <mglpp/system/Clock.hpp>

// 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: Add systray by using org.kde.StatusNotifierWatcher/etc dbus directly.
// TODO: Make sure the overlay always stays on top. Test with starting the overlay and then opening youtube in fullscreen.
//       This is done in Overlay::force_window_on_top, but it's not called right now. It cant be used because the overlay will be on top of
//       notifications.

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

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

static void disable_prime_run() {
    unsetenv("__NV_PRIME_RENDER_OFFLOAD");
    unsetenv("__NV_PRIME_RENDER_OFFLOAD_PROVIDER");
    unsetenv("__GLX_VENDOR_LIBRARY_NAME");
    unsetenv("__VK_LAYER_NV_optimus");
}

static bool is_socket_disconnected(int socket) {
    char buf = '\0';
    errno = 0;
    const ssize_t bytes_read = recv(socket, &buf, 1, MSG_PEEK | MSG_DONTWAIT);
    return bytes_read == 0 || (bytes_read == -1 && (errno == EBADF || errno == ENOTCONN));
}

int main(void) {
    setlocale(LC_ALL, "C"); // Sigh... stupid C

    if(geteuid() == 0) {
        fprintf(stderr, "Error: don't run gsr-ui as the root user\n");
        return 1;
    }

    // Cant get window texture when prime-run is used
    disable_prime_run();

    // Stop nvidia driver from buffering frames
    setenv("__GL_MaxFramesAllowed", "1", true);
    // If this is set to 1 then cuGraphicsGLRegisterImage will fail for egl context with error: invalid OpenGL or DirectX context,
    // so we overwrite it
    setenv("__GL_THREADED_OPTIMIZATIONS", "0", true);
    // Some people set this to force all applications to vsync on nvidia, but this makes eglSwapBuffers never return.
    unsetenv("__GL_SYNC_TO_VBLANK");
    // Same as above, but for amd/intel
    unsetenv("vblank_mode");

    signal(SIGINT, sigint_handler);

    if(mgl_init() != 0) {
        fprintf(stderr, "error: failed to initialize mgl. Either failed to connec to the X11 server or failed to setup opengl\n");
        exit(1);
    }

    gsr::GsrInfo gsr_info;
    // TODO: Show the error in ui
    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, error: %d\n", (int)gsr_info_exit_status);
        exit(1);
    }

   if(gsr_info.system_info.display_server == gsr::DisplayServer::WAYLAND)
       fprintf(stderr, "warning: Wayland support is experimental and requires XWayland. Things may not work as expected.\n");

    std::string resources_path;
    if(access("sibs-build", F_OK) == 0) {
        resources_path = "./";
    } else {
#ifdef GSR_UI_RESOURCES_PATH
        resources_path = GSR_UI_RESOURCES_PATH "/";
#else
        resources_path = "/usr/share/gsr-ui/";
#endif
    }

    mgl_context *context = mgl_get_context();
    const int x11_socket = XConnectionNumber((Display*)context->connection);

    egl_functions egl_funcs;
    egl_funcs.eglGetError = (decltype(egl_funcs.eglGetError))context->gl.eglGetProcAddress("eglGetError");
    egl_funcs.eglCreateImage = (decltype(egl_funcs.eglCreateImage))context->gl.eglGetProcAddress("eglCreateImage");
    egl_funcs.eglDestroyImage = (decltype(egl_funcs.eglDestroyImage))context->gl.eglGetProcAddress("eglDestroyImage");
    egl_funcs.glEGLImageTargetTexture2DOES = (decltype(egl_funcs.glEGLImageTargetTexture2DOES))context->gl.eglGetProcAddress("glEGLImageTargetTexture2DOES");

    if(!egl_funcs.eglGetError || !egl_funcs.eglCreateImage || !egl_funcs.eglDestroyImage || !egl_funcs.glEGLImageTargetTexture2DOES) {
        fprintf(stderr, "Error: required opengl functions not available on your system\n");
        exit(1);
    }

    fprintf(stderr, "info: gsr ui is now ready, waiting for inputs. Press alt+z to show/hide the overlay\n");

    auto overlay = std::make_unique<gsr::Overlay>(resources_path, gsr_info, egl_funcs);
    //overlay.show();

    // gsr::GlobalHotkeysX11 global_hotkeys;
    // const bool show_hotkey_registered = global_hotkeys.bind_key_press({ XK_z, Mod1Mask }, "show_hide", [&](const std::string &id) {
    //     fprintf(stderr, "pressed %s\n", id.c_str());
    //     overlay->toggle_show();
    // });

    // const bool record_hotkey_registered = global_hotkeys.bind_key_press({ XK_F9, Mod1Mask }, "record", [&](const std::string &id) {
    //     fprintf(stderr, "pressed %s\n", id.c_str());
    //     overlay->toggle_record();
    // });

    // const bool pause_hotkey_registered = global_hotkeys.bind_key_press({ XK_F7, Mod1Mask }, "pause", [&](const std::string &id) {
    //     fprintf(stderr, "pressed %s\n", id.c_str());
    //     overlay->toggle_pause();
    // });

    // const bool stream_hotkey_registered = global_hotkeys.bind_key_press({ XK_F8, Mod1Mask }, "stream", [&](const std::string &id) {
    //     fprintf(stderr, "pressed %s\n", id.c_str());
    //     overlay->toggle_stream();
    // });

    // const bool replay_hotkey_registered = global_hotkeys.bind_key_press({ XK_F10, ShiftMask | Mod1Mask }, "replay_start", [&](const std::string &id) {
    //     fprintf(stderr, "pressed %s\n", id.c_str());
    //     overlay->toggle_replay();
    // });

    // const bool replay_save_hotkey_registered = global_hotkeys.bind_key_press({ XK_F10, Mod1Mask }, "replay_save", [&](const std::string &id) {
    //     fprintf(stderr, "pressed %s\n", id.c_str());
    //     overlay->save_replay();
    // });

    gsr::GlobalHotkeysLinux global_hotkeys;
    if(!global_hotkeys.start())
        fprintf(stderr, "error: failed to start global hotkeys\n");

    const bool show_hotkey_registered = global_hotkeys.bind_action("show_hide", [&](const std::string &id) {
        fprintf(stderr, "pressed %s\n", id.c_str());
        overlay->toggle_show();
    });

    const bool record_hotkey_registered = global_hotkeys.bind_action("record", [&](const std::string &id) {
        fprintf(stderr, "pressed %s\n", id.c_str());
        overlay->toggle_record();
    });

    const bool pause_hotkey_registered = global_hotkeys.bind_action("pause", [&](const std::string &id) {
        fprintf(stderr, "pressed %s\n", id.c_str());
        overlay->toggle_pause();
    });

    const bool stream_hotkey_registered = global_hotkeys.bind_action("stream", [&](const std::string &id) {
        fprintf(stderr, "pressed %s\n", id.c_str());
        overlay->toggle_stream();
    });

    const bool replay_hotkey_registered = global_hotkeys.bind_action("replay_start", [&](const std::string &id) {
        fprintf(stderr, "pressed %s\n", id.c_str());
        overlay->toggle_replay();
    });

    const bool replay_save_hotkey_registered = global_hotkeys.bind_action("replay_save", [&](const std::string &id) {
        fprintf(stderr, "pressed %s\n", id.c_str());
        overlay->save_replay();
    });

    if(!show_hotkey_registered)
        fprintf(stderr, "error: failed to register hotkey alt+z for showing the overlay because the hotkey is registered by another program\n");

    if(!record_hotkey_registered)
        fprintf(stderr, "error: failed to register hotkey alt+f9 for recording because the hotkey is registered by another program\n");

    if(!pause_hotkey_registered)
        fprintf(stderr, "error: failed to register hotkey alt+f7 for pausing because the hotkey is registered by another program\n");

    if(!stream_hotkey_registered)
        fprintf(stderr, "error: failed to register hotkey alt+f8 for streaming because the hotkey is registered by another program\n");

    if(!replay_hotkey_registered)
        fprintf(stderr, "error: failed to register hotkey alt+shift+f10 for starting replay because the hotkey is registered by another program\n");

    if(!replay_save_hotkey_registered)
        fprintf(stderr, "error: failed to register hotkey alt+f10 for saving replay because the hotkey is registered by another program\n");

    mgl::Clock frame_delta_clock;
    while(running) {
        if(is_socket_disconnected(x11_socket)) {
            fprintf(stderr, "info: the X11 server has shutdown\n");
            break;
        }

        const double frame_delta_seconds = frame_delta_clock.restart();
        gsr::set_frame_delta_seconds(frame_delta_seconds);

        global_hotkeys.poll_events();
        overlay->handle_events();
        if(!overlay->draw())
            std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }

    fprintf(stderr, "info: shutting down!\n");
    overlay.reset();
    gsr::deinit_theme();
    gsr::deinit_color_theme();
    mgl_deinit();

    return 0;
}