1
1
import sys , os , re , subprocess
2
- from PyQt5 .QtWidgets import QApplication , QWidget , QLabel , QVBoxLayout , QPushButton , QLineEdit , QInputDialog , QSystemTrayIcon , QMenu
2
+ from PyQt5 .QtWidgets import (
3
+ QApplication ,
4
+ QWidget ,
5
+ QLabel ,
6
+ QVBoxLayout ,
7
+ QPushButton ,
8
+ QLineEdit ,
9
+ QInputDialog ,
10
+ QSystemTrayIcon ,
11
+ QMenu ,
12
+ QMessageBox ,
13
+ )
3
14
from PyQt5 .QtGui import QIcon
4
15
from PyQt5 import QtGui
5
16
6
17
current_dir = os .path .dirname (os .path .abspath (__file__ ))
7
18
isHybrid = True
8
19
9
- class EnvyControl (QWidget ):
10
20
21
+ class EnvyControl (QWidget ):
11
22
def start_status (self ):
12
- status_output = subprocess .check_output ([' envycontrol' , '-q' ]).decode ().strip ()
23
+ status_output = subprocess .check_output ([" envycontrol" , "-q" ]).decode ().strip ()
13
24
keywords = ["hybrid" , "integrated" , "nvidia" ]
14
25
for keyword in keywords :
15
26
if re .search (keyword , status_output , re .IGNORECASE ):
16
27
return keyword
17
28
return "envycontrol -q output style was changed"
18
29
30
+ def reboot_system (self ):
31
+ message_box = QMessageBox ()
32
+ message_box .setText ("A reboot is required to switch mode, reboot ?" )
33
+ message_box .setWindowTitle ("Confirmation" )
34
+ message_box .setIcon (QMessageBox .Question )
35
+ message_box .setStandardButtons (QMessageBox .Yes | QMessageBox .No )
36
+
37
+ # Execute the message box and get the result
38
+ result = message_box .exec ()
39
+
40
+ def get_igpu_vendor ():
41
+ lspci_output = subprocess .check_output (["lspci" ]).decode ("utf-8" )
42
+ for line in lspci_output .splitlines ():
43
+ if "VGA compatible controller" in line or "Display controller" in line :
44
+ if "Intel" in line :
45
+ return "intel"
46
+ elif "ATI" in line or "AMD" in line or "AMD/ATI" in line :
47
+ return "amd"
48
+ return None
49
+
50
+ # Check the result and act accordingly
51
+ if result == QMessageBox .Yes :
52
+ print ("You clicked Yes reboot" )
53
+ subprocess .run (["sudo" , "reboot" ])
54
+ elif result == QMessageBox .No :
55
+ print ("You clicked No" )
19
56
20
57
def quit (self ):
21
58
sys .exit ()
22
59
60
+ igpu_vendor = get_igpu_vendor ()
23
61
24
62
def nvidia_tray (self ):
25
63
icon_start = self .start_status ()
26
64
global isHybrid
27
65
if icon_start == "hybrid" and isHybrid == True :
28
- password , ok = QInputDialog .getText (self , 'Password' , 'Enter your sudo password:' , QLineEdit .Password )
66
+ password , ok = QInputDialog .getText (
67
+ self , "Password" , "Enter your sudo password:" , QLineEdit .Password
68
+ )
29
69
if ok :
30
- self .run_command (' nvidia' , password )
70
+ self .run_command (" nvidia" , password )
31
71
app_path = os .path .join (current_dir , "nvidia.png" )
32
72
icon = QtGui .QIcon (app_path )
33
73
self .tray_icon .setIcon (QIcon (icon ))
@@ -37,18 +77,21 @@ def nvidia_tray(self):
37
77
isHybrid = False
38
78
39
79
def integrated_tray (self ):
40
- password , ok = QInputDialog .getText (self , 'Password' , 'Enter your sudo password:' , QLineEdit .Password )
80
+ password , ok = QInputDialog .getText (
81
+ self , "Password" , "Enter your sudo password:" , QLineEdit .Password
82
+ )
41
83
if ok :
42
- self .run_command ('integrated' , password )
43
- app_path = os .path .join (current_dir , "integrated_amd.png" )
84
+ self .run_command ("integrated" , password )
44
85
icon = QtGui .QIcon (app_path )
45
86
self .tray_icon .setIcon (QIcon (icon ))
46
87
self .update_status ()
47
88
48
89
def hybrid_tray (self ):
49
- password , ok = QInputDialog .getText (self , 'Password' , 'Enter your sudo password:' , QLineEdit .Password )
90
+ password , ok = QInputDialog .getText (
91
+ self , "Password" , "Enter your sudo password:" , QLineEdit .Password
92
+ )
50
93
if ok :
51
- self .run_command (' hybrid' , password )
94
+ self .run_command (" hybrid --rtd3" , password )
52
95
app_path = os .path .join (current_dir , "hybrid.png" )
53
96
icon = QtGui .QIcon (app_path )
54
97
self .tray_icon .setIcon (QIcon (icon ))
@@ -59,20 +102,20 @@ def __init__(self):
59
102
self .initUI ()
60
103
61
104
def initUI (self ):
62
- self .setWindowTitle (' EnvyControl Qt' )
105
+ self .setWindowTitle (" EnvyControl Qt" )
63
106
app_path = os .path .join (current_dir , "envycontrol.png" )
64
107
icon = QtGui .QIcon (app_path )
65
108
self .setWindowIcon (icon )
66
109
self .status_label = QLabel ()
67
110
self .update_status ()
68
111
69
- integrated_button = QPushButton (' Integrated' )
112
+ integrated_button = QPushButton (" Integrated" )
70
113
integrated_button .clicked .connect (self .on_integrated_clicked )
71
114
72
- hybrid_button = QPushButton (' Hybrid' )
115
+ hybrid_button = QPushButton (" Hybrid" )
73
116
hybrid_button .clicked .connect (self .on_hybrid_clicked )
74
117
75
- nvidia_button = QPushButton (' Nvidia' )
118
+ nvidia_button = QPushButton (" Nvidia" )
76
119
nvidia_button .clicked .connect (self .on_nvidia_clicked )
77
120
78
121
vbox = QVBoxLayout ()
@@ -87,9 +130,12 @@ def initUI(self):
87
130
88
131
icon_start = self .start_status ()
89
132
if icon_start == "integrated" :
90
- app_path = os .path .join (current_dir , "./integrated_amd.png" )
91
- icon = QtGui .QIcon (app_path )
92
- self .tray_icon .setIcon (QIcon (icon ))
133
+ if igpu_vendor == "intel" :
134
+ app_path = os .path .join (current_dir , "./integrated_intel.png" )
135
+ elif igpu_vendor == "amd" :
136
+ app_path = os .path .join (current_dir , "./integrated_amd.png" )
137
+ icon = QtGui .QIcon (app_path )
138
+ self .tray_icon .setIcon (QIcon (icon ))
93
139
elif icon_start == "hybrid" :
94
140
app_path = os .path .join (current_dir , "hybrid.png" )
95
141
icon = QtGui .QIcon (app_path )
@@ -104,60 +150,78 @@ def initUI(self):
104
150
print (icon_start )
105
151
106
152
self .tray_menu = QMenu ()
107
- self .tray_menu .addAction (' Integrated' , self .integrated_tray )
108
- self .tray_menu .addAction (' Hybrid' , self .hybrid_tray )
109
- self .tray_menu .addAction (' Nvidia' , self .nvidia_tray )
110
- self .tray_menu .addAction (' Show' , self .show )
111
- self .tray_menu .addAction (' Quit' , self .quit )
153
+ self .tray_menu .addAction (" Integrated" , self .integrated_tray )
154
+ self .tray_menu .addAction (" Hybrid" , self .hybrid_tray )
155
+ self .tray_menu .addAction (" Nvidia" , self .nvidia_tray )
156
+ self .tray_menu .addAction (" Show" , self .show )
157
+ self .tray_menu .addAction (" Quit" , self .quit )
112
158
self .tray_icon .setContextMenu (self .tray_menu )
113
159
114
160
def closeEvent (self , event ):
115
161
self .hide ()
116
162
event .ignore ()
117
163
118
164
def on_integrated_clicked (self ):
119
- password , ok = QInputDialog .getText (self , 'Password' , 'Enter your sudo password:' , QLineEdit .Password )
165
+ password , ok = QInputDialog .getText (
166
+ self , "Password" , "Enter your sudo password:" , QLineEdit .Password
167
+ )
120
168
if ok :
121
- self .run_command ('integrated' , password )
169
+ self .run_command ("integrated" , password )
170
+ if igpu_vendor == "intel" :
171
+ app_path = os .path .join (current_dir , "./integrated_intel.png" )
172
+ elif igpu_vendor == "amd" :
122
173
app_path = os .path .join (current_dir , "./integrated_amd.png" )
123
174
icon = QtGui .QIcon (app_path )
124
175
self .tray_icon .setIcon (QIcon (icon ))
125
176
self .update_status ()
177
+ self .reboot_system ()
126
178
127
179
def on_hybrid_clicked (self ):
128
- password , ok = QInputDialog .getText (self , 'Password' , 'Enter your sudo password:' , QLineEdit .Password )
180
+ password , ok = QInputDialog .getText (
181
+ self , "Password" , "Enter your sudo password:" , QLineEdit .Password
182
+ )
129
183
if ok :
130
- self .run_command (' hybrid' , password )
184
+ self .run_command (" hybrid" , password )
131
185
app_path = os .path .join (current_dir , "hybrid.png" )
132
186
icon = QtGui .QIcon (app_path )
133
187
self .tray_icon .setIcon (QIcon (icon ))
134
188
self .update_status ()
189
+ self .reboot_system ()
135
190
136
191
def on_nvidia_clicked (self ):
137
192
icon_start = self .start_status ()
138
193
global isHybrid
139
194
if icon_start == "hybrid" and isHybrid == True :
140
- password , ok = QInputDialog .getText (self , 'Password' , 'Enter your sudo password:' , QLineEdit .Password )
195
+ password , ok = QInputDialog .getText (
196
+ self , "Password" , "Enter your sudo password:" , QLineEdit .Password
197
+ )
141
198
if ok :
142
- self .run_command (' nvidia' , password )
199
+ self .run_command (" nvidia" , password )
143
200
app_path = os .path .join (current_dir , "nvidia.png" )
144
201
icon = QtGui .QIcon (app_path )
145
202
self .tray_icon .setIcon (QIcon (icon ))
203
+ self .reboot_system ()
146
204
else :
147
205
status = "Try switching to hybrid mode first!"
148
206
self .status_label .setText (status )
149
207
isHybrid = False
150
208
151
209
def update_status (self ):
152
- result = subprocess .check_output ([' envycontrol' , '-q' ])
210
+ result = subprocess .check_output ([" envycontrol" , "-q" ])
153
211
status = result .strip ().decode ()
154
212
self .status_label .setText (status )
155
213
156
214
def run_command (self , mode , password ):
157
- p = subprocess .Popen (['sudo' , '-S' , 'envycontrol' , '-s' , mode ], stdout = subprocess .PIPE , stdin = subprocess .PIPE , stderr = subprocess .PIPE )
215
+ p = subprocess .Popen (
216
+ ["sudo" , "-S" , "envycontrol" , "-s" , mode ],
217
+ stdout = subprocess .PIPE ,
218
+ stdin = subprocess .PIPE ,
219
+ stderr = subprocess .PIPE ,
220
+ )
158
221
stdout , stderr = p .communicate (input = password .encode ())
159
222
160
- if __name__ == '__main__' :
223
+
224
+ if __name__ == "__main__" :
161
225
app = QApplication (sys .argv )
162
226
envy = EnvyControl ()
163
227
envy .show ()
0 commit comments