-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleft-title.cpp
78 lines (77 loc) · 1.86 KB
/
left-title.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
77
78
#!/usr/bin/env cpprunner
#include <iostream>
#include <fcntl.h>
#include <sys/wait.h>
#include <unistd.h>
using namespace std;
void runCommand(const char **argv, char *buffer, int bsize);
const char nl = '\n';
int main(int argc, char *argv[]) {
char out[20];
const char *bspc[] = {"bspc", "query", "-M", "--names", "-m", "focused" , NULL};
int xtpfd[2]; //0 - read
int pret = pipe(xtpfd);
int xtpid = fork();
char monitorStart = argv[2][0];
if(xtpid == -1) {
perror("fork before xtitle");
return 2;
}
else if(xtpid == 0) {
//child
close(xtpfd[0]); //no reading
dup2(xtpfd[1], STDOUT_FILENO); //write to pipe
execl("/home/navn/aur/bspwm-tools/bin/xtitle","xtitle","-s",NULL);
perror("exec xtitle");
return 3;
}
else {
//parent
char buffer[200];
buffer[199] = 0;
close(xtpfd[1]);//no writing
while(int r = read(xtpfd[0],buffer,199)) {
buffer[r] = 0;
runCommand(bspc, out, 20);
if(out[0] == monitorStart) //assume DP-1
/* if(out[0] == 'D') //assume DP-1 */
cout << buffer<<flush;
/* printf("%s\n",buffer); */
}
}
}
void runCommand(const char **argv, char *buffer, int bsize) {
int pfd[2];
if(buffer != nullptr) {
if(pipe(pfd) == -1) {
cout<<"pipe failed.\n";
return;
}
}
int pid = fork();
if(pid == -1) {
cout<<"fork failed.";
}
else if(pid == 0) {
if(buffer != nullptr) {
close(pfd[0]); //child won't read
dup2(pfd[1],STDOUT_FILENO); //write to pipe instead of stdout
dup2(pfd[1],STDERR_FILENO); //write to pipe instead of stderr
close(pfd[1]); //stdout and err is set
}
execvp(argv[0], const_cast<char **>(argv));
/* execvp(argv[0], const_cast<char **>(argv)); */
cout<<argv;
perror("execvp failed");
}
else {
//parent
close(pfd[1]);//no write needed
if(buffer != nullptr) {
int r = read(pfd[0],buffer,bsize-1);
buffer[r] = 0;
}
close(pfd[0]);//read done
waitpid(pid,0,0);
}
}