-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutoChSX.py
174 lines (147 loc) · 4.58 KB
/
AutoChSX.py
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
import os
from subprocess import DEVNULL, check_output
import subprocess
import time
# Replace my home path with your path with Vim:
# :%s/\/home\/ari/<your_path>/g
# Hit enter
#
# Quick explanation:
# Replace all instances of a by b in Vim is as follows: :%s/a/b/g
# But since "/" is part of the command it must be "escaped" (or made "invisible"), by writing "\" right before it, thus we write "\/home\/ari"
# IMPORTANT
# the calls to "setsx2m", "setnm", and "setsxm" are calls to bash scripts that all display a different indicator on my window manager statusbar
# It looks something like this:
# xsetroot -name "SXXXX 00000 00000 00000 00000 00000"
# (Which in font 5 looks exactly how I want it to)
##### CONFIG SWAMPING FUNCTIONS #####
def runsxhkd():
os.system("runsxhkd")
fl = ''
imw_count = 3
def GetSX():
global fl
with open(r'/home/ari/.config/sxhkd/sxhkdrc') as f:
fl = f.readline()
# print(fl)
global imw_count
if fl == '# SX_2\n':
# print("2")
os.system('setsx2m')
imw_count = 2
if fl == '# SX_0\n':
imw_count = 0
# print("0")
os.system('setnm')
if fl == '# SX_1\n':
imw_count = 1
os.system('setsxm')
return(imw_count)
def SetStatus(n):
if n == 0:
fout = open("/home/ari/.sx/status.txt", "w", encoding="utf-8")
fout.write("sx0")
fout.close()
if n == 1:
fout = open("/home/ari/.sx/status.txt", "w", encoding="utf-8")
fout.write("sx1")
fout.close()
if n == 2:
fout = open("/home/ari/.sx/status.txt", "w", encoding="utf-8")
fout.write("sx2")
fout.close()
os.system("ksx")
runsxhkd()
GetSX()
if imw_count == 0:
SetStatus(0)
if imw_count == 1:
SetStatus(1)
if imw_count == 2:
SetStatus(2)
# print("first imw", imw_count)
def ToSX1():
global imw_count
if imw_count == 0:
os.system('ksx')
os.rename(r'/home/ari/.config/sxhkd/sxhkdrc',r'/home/ari/.config/sxhkd/sx_0')
os.rename(r'/home/ari/.config/sxhkd/sx_1',r'/home/ari/.config/sxhkd/sxhkdrc')
os.system('runsxhkd')
SetStatus(1)
os.system('setsxm')
imw_count = 1
# print("success tosx1")
return(imw_count)
def ToSX0():
#KITTY
global imw_count
#global imw_count
# print(imw_count)
# print("ok")
if imw_count == 1:
os.system('ksx')
os.rename(r'/home/ari/.config/sxhkd/sxhkdrc',r'/home/ari/.config/sxhkd/sx_1')
os.rename(r'/home/ari/.config/sxhkd/sx_0',r'/home/ari/.config/sxhkd/sxhkdrc')
os.system('runsxhkd')
SetStatus(0)
os.system('setnm')
imw_count = 0
# print(imw_count)
return(imw_count)
##### AUTO SWITCHING RELATED FUNCTION #####
def GetWin():
output = check_output(['getactivewin'], stderr=DEVNULL, stdin=DEVNULL).decode("utf-8").rstrip('\n')
return(output)
# Retrieve currently active sxhkd configuration
def GetSXConfig():
with open(r'/home/ari/.sx/status.txt', 'r') as fr:
fl = fr.readline()
return(fl)
# Retrieve "pause" status
def GetPause():
with open(r'/home/ari/.sx/pause.txt', 'r') as fr:
pause = fr.readline()
pause = pause.rstrip('\n')
return(pause)
# For testing with https://github.com/shiro/map2
def Hypr():
os.system('hyprlock')
# For testing with https://github.com/shiro/map2
def Map2():
output = subprocess.Popen(['map2', '-d', '/home/ari/.map2/devices.list', '/home/ari/.map2/map.m2'])
return(output)
def UndoPause():
os.system("python3 /home/ari/Scripts/sx_undopause.py")
try:
os.system("rm /home/ari/.toggle/toggle_pause")
except Exception:
pass
UndoPause()
# For testing with https://github.com/shiro/map2
map2 = 'map2 -d /home/ari/.map2/devices.list /home/ari/.map2/map.m2'
# Apps that require sx0 configuration
sx0 = ["kitty", "mpv", "zathura"]
## Script will change to sx1 configuration when all other windows are focused
##### MAIN LOOP #####
while True:
time.sleep(0.1)
win = GetWin()
status = GetSXConfig()
pause = GetPause()
if win in sx0 and status != "sx0":
# Triggering config change to terminal hotkeys
# print("status: ", status)
ToSX0()
time.sleep(0.15)
continue
if pause == "no" and win not in sx0 and status == "sx0":
# Triggering config change to browsing hotkeys
ToSX1()
time.sleep(0.15)
continue
if pause == "yes" and win not in sx0:
ToSX0()
time.sleep(0.15)
continue
else:
continue