-
Notifications
You must be signed in to change notification settings - Fork 0
/
updateTermInfo.cpp
193 lines (167 loc) · 4.84 KB
/
updateTermInfo.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
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
#include <iostream>
#include <fstream>
#include <sstream>
#include <unistd.h>
#include <climits>
using namespace std;
string getCmdOut(string cmd)
{
string data;
FILE* stream;
const int max_buffer = 256;
char buffer[max_buffer];
cmd.append(" 2>&1");
stream=popen(cmd.c_str(), "r");
if(stream)
{
while (!feof(stream))
if (fgets(buffer, max_buffer, stream) != NULL) data.append(buffer);
pclose(stream);
}
return data;
}
int main()
{
const unsigned short int maxTerms=UINT8_MAX;
const unsigned short int maxMonitors=UINT8_MAX;
int term[maxTerms],termX[maxTerms],termY[maxTerms];
int monitorXOff[maxMonitors],monitorYOff[maxMonitors],monitorXRes[maxMonitors],monitorYRes[maxMonitors];
string line,tty;
string user=getlogin();
string configpath="/home/"+user+"/.config/WallFade/";
string termColor[maxTerms];
string termID[maxTerms];
ifstream readstream;
//while(true)
while(getCmdOut("ps -aux | grep WallFade | grep -v grep")!="")
{
//Reset terminal arrays
for(int i=0; i<maxTerms; i++)
{
term[i]=0;
termID[i]="";
termX[i]=0;
termY[i]=0;
}
cout<<"Getting terminal positions"<<endl;
readstream.open(configpath+"TermInfo");
for(int i=0; !readstream.eof()&&readstream; i++)
{
getline(readstream,line);
std::istringstream st (line);
st >> term[i];
getline(readstream,line);
std::istringstream sid (line);
sid >> termID[i];
getline(readstream,line);
std::istringstream sx (line);
sx >> termX[i];
getline(readstream,line);
std::istringstream sy (line);
sy >> termY[i];
if(term[i]!=0)
{
cout<<"\t"+to_string(term[i])+", "+termID[i]+", "+to_string(termX[i])+", "+to_string(termY[i])<<endl;
}
else
{
break;
}
}
readstream.close();
cout<<endl;
cout<<"Updating term info"<<endl;
system(("rm -f "+configpath+"TermInfo").c_str());
system(("ls /dev/pts | grep -E '[1-9]{1,9}' > "+configpath+"tty").c_str());
for(int i=0; term[i]!=0; i++)
{
readstream.open(configpath+"tty");
while(readstream)
{
//Check if terminal still exists, remove duplicates, and write back to TermInfo
getline(readstream,line);
std::istringstream stty (line);
stty >> tty;
if(stoi(tty)==term[i])
{
cout<<"Terminal "+to_string(term[i])+" located ("+termID[i]+"), checking for duplicates"<<endl;
//Check for duplicate entries and remove if they exist
for(int k=i+1; term[k]!=0; k++)
{
if(term[k]==term[i])
{
cout<<"\tRemoving duplicate entry (tty "+to_string(term[k])+", "+termID[k]+")"<<endl;
for(int j=i; term[j]!=0; j++)
{
cout<<"\tSetting term["+to_string(j)+"] to "+to_string(term[j+1])<<endl;
if(term[j+1]==0)
{
term[j]=0;
termID[j]="";
termX[j]=0;
termY[j]=0;
break;
}
else
{
term[j]=term[j+1];
termID[j]=termID[j+1];
termX[j]=termX[j+1];
termY[j]=termY[j+1];
}
}
}
}
system(("echo "+to_string(term[i])+" >> "+configpath+"TermInfo").c_str());
system(("echo "+termID[i]+" >> "+configpath+"TermInfo").c_str());
std::istringstream sx (getCmdOut(("xwininfo -id "+termID[i]+" | grep -E 'Absolute upper-left X: [0-9]{0,6}' | grep -Eo '[0-9]{0,6}'").c_str()));
sx >> termX[i];
system(("echo "+to_string(termX[i])+" >> "+configpath+"TermInfo").c_str());
std::istringstream sy (getCmdOut(("xwininfo -id "+termID[i]+" | grep -E 'Absolute upper-left Y: [0-9]{0,6}' | grep -Eo '[0-9]{0,6}'").c_str()));
sx >> termY[i];
system(("echo "+to_string(termY[i])+" >> "+configpath+"TermInfo").c_str());
cout<<"\tRe-cached term "<<term[i]<<" ("<<termID[i]<<") at: "<<termX[i]<<", "<<termY[i]<<endl;
readstream.close();
break;
}
//If terminal doesn't exist, delete it and sort
if(readstream.eof())
{
cout<<"Terminal "+to_string(term[i])+" NOT located, deleting"<<endl;
for(int j=i; term[j]!=0; j++)
{
cout<<"\tSetting term["+to_string(j)+"] to "+to_string(term[j+1])<<endl;
if(term[j+1]==0)
{
term[j]=0;
termID[j]="";
termX[j]=0;
termY[j]=0;
break;
}
else
{
term[j]=term[j+1];
termID[j]=termID[j+1];
termX[j]=termX[j+1];
termY[j]=termY[j+1];
}
}
i--;
cout<<endl;
cout<<"TERM ORDER"<<endl;
for(int x=0; term[x]!=0; x++)
{
cout<<to_string(term[x])+" ";
}
cout<<endl;
readstream.close();
break;
}
}
}
cout<<endl;
sleep(1);
}
return 0;
}