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
|
#include "../include/GlobalHotkeysLinux.hpp"
#include <signal.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <limits.h>
#include <string.h>
#define PIPE_READ 0
#define PIPE_WRITE 1
namespace gsr {
static const char* grab_type_to_arg(GlobalHotkeysLinux::GrabType grab_type) {
switch(grab_type) {
case GlobalHotkeysLinux::GrabType::ALL: return "--all";
case GlobalHotkeysLinux::GrabType::VIRTUAL: return "--virtual";
}
return "--all";
}
GlobalHotkeysLinux::GlobalHotkeysLinux(GrabType grab_type) : grab_type(grab_type) {
for(int i = 0; i < 2; ++i) {
pipes[i] = -1;
}
}
GlobalHotkeysLinux::~GlobalHotkeysLinux() {
for(int i = 0; i < 2; ++i) {
if(pipes[i] > 0)
close(pipes[i]);
}
if(read_file)
fclose(read_file);
if(process_id > 0) {
kill(process_id, SIGKILL);
int status;
waitpid(process_id, &status, 0);
}
}
bool GlobalHotkeysLinux::start() {
const char *grab_type_arg = grab_type_to_arg(grab_type);
const bool inside_flatpak = getenv("FLATPAK_ID") != NULL;
const char *user_homepath = getenv("HOME");
if(!user_homepath)
user_homepath = "/tmp";
char gsr_global_hotkeys_flatpak[PATH_MAX];
snprintf(gsr_global_hotkeys_flatpak, sizeof(gsr_global_hotkeys_flatpak), "%s/.local/share/gpu-screen-recorder/gsr-global-hotkeys", user_homepath);
if(process_id > 0)
return false;
if(pipe(pipes) == -1)
return false;
const pid_t pid = vfork();
if(pid == -1) {
perror("Failed to vfork");
for(int i = 0; i < 2; ++i) {
close(pipes[i]);
pipes[i] = -1;
}
return false;
} else if(pid == 0) { /* child */
dup2(pipes[PIPE_WRITE], STDOUT_FILENO);
for(int i = 0; i < 2; ++i) {
close(pipes[i]);
}
if(inside_flatpak) {
const char *args[] = { "flatpak-spawn", "--host", "--", gsr_global_hotkeys_flatpak, grab_type_arg, nullptr };
execvp(args[0], (char* const*)args);
} else {
const char *args[] = { "gsr-global-hotkeys", grab_type_arg, nullptr };
execvp(args[0], (char* const*)args);
}
perror("gsr-global-hotkeys");
_exit(127);
} else { /* parent */
process_id = pid;
close(pipes[PIPE_WRITE]);
pipes[PIPE_WRITE] = -1;
const int fdl = fcntl(pipes[PIPE_READ], F_GETFL);
fcntl(pipes[PIPE_READ], F_SETFL, fdl | O_NONBLOCK);
read_file = fdopen(pipes[PIPE_READ], "r");
if(read_file)
pipes[PIPE_READ] = -1;
else
fprintf(stderr, "fdopen failed, error: %s\n", strerror(errno));
}
return true;
}
bool GlobalHotkeysLinux::bind_action(const std::string &id, GlobalHotkeyCallback callback) {
return bound_actions_by_id.insert(std::make_pair(id, std::move(callback))).second;
}
void GlobalHotkeysLinux::poll_events() {
if(process_id <= 0) {
fprintf(stderr, "error: GlobalHotkeysLinux::poll_events failed, process has not been started yet. Use GlobalHotkeysLinux::start to start the process first\n");
return;
}
if(!read_file) {
fprintf(stderr, "error: GlobalHotkeysLinux::poll_events failed, read file hasn't opened\n");
return;
}
std::string action;
char buffer[256];
while(true) {
char *line = fgets(buffer, sizeof(buffer), read_file);
if(!line)
break;
int line_len = strlen(line);
if(line_len == 0)
continue;
if(line[line_len - 1] == '\n') {
line[line_len - 1] = '\0';
--line_len;
}
action = line;
auto it = bound_actions_by_id.find(action);
if(it != bound_actions_by_id.end())
it->second(action);
}
}
}
|