-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
76 lines (66 loc) · 1.73 KB
/
main.cpp
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
#include <iostream>
#include <fcntl.h>
#include "background.h"
using namespace i3_focus_last;
void handleCheckAlreadyRunning(int &argc, char *argv[])
{
DEBUG_MSG ("Starting");
bool iscmd = likely (argc > 1 && (argv[1][0] == 'f' || argv[1][0] == 'd' || argv[1][0] == 'g'));
DEBUG_MSG ("isCmd: " << iscmd);
// file exists and is being read from - write cmd
// cmd mode - file exists and not read from - remove file
// cmd mode - file not exists - ret
int fd = open (pipefname, O_WRONLY | O_NONBLOCK);
if (unlikely (fd == -1))
{
if (likely (iscmd))
{
ERROR_MSG ("Pipe open fail");
exit(EXIT_FAILURE);
}
DEBUG_MSG ("Pipe open fail");
return;
}
DEBUG_MSG ("Pipe read success");
std::string cmd;
if (likely (iscmd))
{
for (int i = 1; i < argc; ++i)
{
cmd.append(argv[i]);
}
}
else
cmd.push_back ('e');
cmd.push_back ('\n');
DEBUG_MSG ("Writing to pipe");
ssize_t written = write(fd, cmd.c_str (), cmd.length ());
close (fd);
if (!iscmd || written == -1) // && errno == EPIPE) // Pipe error
{
DEBUG_MSG ("Unlinking pipe");
unlink (pipefname);
}
DEBUG_MSG ("Written: " << written);
if (iscmd)
exit (EXIT_SUCCESS);
}
int main (int argc, char *argv[])
{
auto uid = getuid();
sprintf (pipefname, "/run/user/%d/i3-focus-last.sock", uid);
#if DEBUG
if (isatty (STDOUT_FILENO) && argc > 1 && argv[1][0] == '-' && argv[1][1] == 'd')
{
isDebug = 15u;
argc--;
argv++;
}
#endif
DEBUG_MSG("Using pipe: " << pipefname);
signal(SIGPIPE, SIG_IGN);
handleCheckAlreadyRunning (argc, argv);
signal(SIGPIPE, SIG_DFL);
DEBUG_MSG ("Starting");
(new background)->run(argc, argv);
}