-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgpdscreen-indicator.py
executable file
·216 lines (180 loc) · 6.81 KB
/
gpdscreen-indicator.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
#
# __author__ = "Simone Roberto Nunzi aka Stockmind"
# __copyright__ = "Copyright 2017"
# __credits__ = ["Timur Rubeko for initial daemon setup"]
# __license__ = "GPL"
# __version__ = "3.0"
# Icon used: https://www.flaticon.com/free-icon/screen-rotation-button_61030
# Icon author link: https://www.flaticon.com/authors/google
import os
import time
import signal
import subprocess
import ConfigParser
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('AppIndicator3', '0.1')
from gi.repository import Gtk as gtk
from gi.repository import AppIndicator3 as appindicator
from os.path import expanduser
import os.path
import errno
import sys
home = expanduser("~")
APPINDICATOR_ID = 'GPDScreenManager'
maincolor = "white"
menucolor = "white"
configfile = "{}/.config/gpdscreen-indicator/config".format(home)
Config = ConfigParser.ConfigParser()
# Taken from https://stackoverflow.com/a/600612/119527
def mkdir_p(path):
try:
os.makedirs(path)
except OSError as exc: # Python >2.5
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else: raise
def safe_open(path,option):
''' Open "path", creating any parent directories as needed.
'''
mkdir_p(os.path.dirname(path))
return open(path, option)
def getconfig(section):
dict1 = {}
options = Config.options(section)
for option in options:
try:
dict1[option] = Config.get(section, option)
if dict1[option] == -1:
DebugPrint("skip: %s" % option)
except:
print("exception on %s!" % option)
dict1[option] = None
return dict1
def setconfig(section,key,value):
try:
Config.add_section(section)
except:
pass
Config.set(section,key,value)
with safe_open(configfile,'w+') as file:
Config.write(file)
def main():
# Read config
try:
Config.read(configfile)
maincolor = getconfig("icons")['maincolor'] # white - black
menucolor = getconfig("icons")['menucolor'] # white - black
except:
setconfig("icons","maincolor","white")
setconfig("icons","menucolor","white")
maincolor = "white"
menucolor = "white"
# Init indicator
indicator = appindicator.Indicator.new(APPINDICATOR_ID, get_resource_path('/usr/local/share/gpdscreen-indicator/icons/screen-rotation-button-{}.svg'.format(maincolor)), appindicator.IndicatorCategory.SYSTEM_SERVICES)
indicator.set_status(appindicator.IndicatorStatus.ACTIVE)
menu = build_menu()
indicator.set_menu(menu)
gtk.main()
def build_menu():
menu = gtk.Menu()
# Rotate landscape
item_landscape = gtk.ImageMenuItem('Rotate landscape')
item_icon = gtk.Image()
item_icon.set_from_file(get_resource_path('/usr/local/share/gpdscreen-indicator/icons/tablet-with-blank-screen-{}.svg'.format(menucolor)))
item_landscape.set_image(item_icon)
item_landscape.set_always_show_image(True)
item_landscape.connect('activate', landscape)
menu.append(item_landscape)
# Rotate portrait
item_portrait = gtk.ImageMenuItem('Rotate portrait')
item_icon = gtk.Image()
item_icon.set_from_file(get_resource_path('/usr/local/share/gpdscreen-indicator/icons/cell-phone-with-blank-screen-{}.svg'.format(menucolor)))
item_portrait.set_image(item_icon)
item_portrait.set_always_show_image(True)
item_portrait.connect('activate', portrait)
menu.append(item_portrait)
# Rotate inverted portrait
item_portrait = gtk.ImageMenuItem('Rotate inverse portrait')
item_icon = gtk.Image()
item_icon.set_from_file(get_resource_path('/usr/local/share/gpdscreen-indicator/icons/cell-phone-with-blank-screen-{}.svg'.format(menucolor)))
item_portrait.set_image(item_icon)
item_portrait.set_always_show_image(True)
item_portrait.connect('activate', invertedportrait)
menu.append(item_portrait)
# Restore display size
#item_displaysize = gtk.MenuItem('Restore display size')
#item_displaysize.connect('activate', displaysize)
#menu.append(item_displaysize)
# Restore display size
item_resettouch = gtk.ImageMenuItem('Reset touchscreen')
item_icon = gtk.Image()
item_icon.set_from_file(get_resource_path('/usr/local/share/gpdscreen-indicator/icons/synchronization-arrows-{}.svg'.format(menucolor)))
item_resettouch.set_image(item_icon)
item_resettouch.set_always_show_image(True)
item_resettouch.connect('activate', resettouch)
menu.append(item_resettouch)
# Normal DPI
item_normaldpi = gtk.ImageMenuItem('Normal DPI')
item_normaldpi.connect('activate', normaldpi)
menu.append(item_normaldpi)
# High DPI
item_highdpi = gtk.ImageMenuItem('High DPI')
item_highdpi.connect('activate', highdpi)
menu.append(item_highdpi)
# Preferences menu
preferencesmenu = gtk.Menu()
item_preferences = gtk.ImageMenuItem('Preferences')
item_preferences.set_submenu(preferencesmenu)
# Theme White
item_preferences_themewhite = gtk.ImageMenuItem('White Theme')
item_preferences_themewhite.connect('activate', themewhite)
preferencesmenu.append(item_preferences_themewhite)
# Theme Black
item_preferences_themeblack = gtk.ImageMenuItem('Black Theme')
item_preferences_themeblack.connect('activate', themeblack)
preferencesmenu.append(item_preferences_themeblack)
menu.append(item_preferences)
# Quit
item_quit = gtk.ImageMenuItem('Quit')
item_quit.connect('activate', quit)
menu.append(item_quit)
menu.show_all()
return menu
def quit(*source):
gtk.main_quit()
def landscape(*source):
subprocess.call(["gpdscreen", "landscape"])
def portrait(*source):
subprocess.call(["gpdscreen", "portrait"])
def invertedportrait(*source):
subprocess.call(["gpdscreen", "invertedportrait"])
def displaysize(*source):
subprocess.call(["gpdscreen", "displaysize"])
def resettouch(*source):
subprocess.Popen(["pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY gpdscreen touchreset"], shell=True)
def highdpi(*source):
subprocess.call(["gpdscreen", "highdpi"])
def normaldpi(*source):
subprocess.call(["gpdscreen", "normaldpi"])
def themewhite(*source):
setconfig("icons","maincolor","white")
setconfig("icons","menucolor","white")
os.execv(__file__, sys.argv)
gtk.main_quit()
def themeblack(*source):
setconfig("icons","maincolor","black")
setconfig("icons","menucolor","black")
os.execv(__file__, sys.argv)
gtk.main_quit()
def get_resource_path(rel_path):
#dir_of_py_file = os.path.dirname(__file__)
#rel_path_to_resource = os.path.join(dir_of_py_file, rel_path)
#abs_path_to_resource = os.path.abspath(rel_path_to_resource)
#return abs_path_to_resource
return rel_path
if __name__ == "__main__":
signal.signal(signal.SIGINT, signal.SIG_DFL)
main()