-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathbumblebee-indicator
executable file
·195 lines (160 loc) · 7.69 KB
/
bumblebee-indicator
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
### BEGIN LICENSE
#
# ----------------------------------------------------------------------------
# "THE BEER-WARE LICENSE" (Revision 42):
# <[email protected]> wrote this file. As long as you retain this notice you
# can do whatever you want with this stuff. If we meet some day, and you think
# this stuff is worth it, you can buy me a beer in return Davy Renaud (glyptostroboides)
# ----------------------------------------------------------------------------
#
# This file is part of bumblebee-ui.
#
# bumblebee-ui is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# bumblebee-ui is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with bumblebee-ui. If not, see <http://www.gnu.org/licenses/>.
#
### END LICENSE
#UI MODULE
import pygtk
pygtk.require('2.0')
import gtk
import gobject
import appindicator
import pynotify
#SYSTEM MODULE
import os
import subprocess
###ORIGINAL CLASS
import app.Config
from app.AppSettings import Applications_settings, IconSet
from app.DesktopFile import DesktopFile, DesktopFileSet
class BumblebeeIndicator():
#TODO There must be a better way to get the icon than the URI
#FIXME The notification must be replaced when still visible
def notify_state(self, title, msg, icon_name):
self.notification= pynotify.Notification(title, msg, IconSet().get_uri(icon_name,48))
self.notification.set_urgency(pynotify.URGENCY_CRITICAL)
#self.notification.set_timeout(pynotify.EXPIRES_NEVER) ###### NICE TO SHOW A MESSAGE SIMPLY
#FIXME The notification is to slow and this doesn't work
#self.notification.set_timeout(1000)
self.notification.show()
###INITIALIZATION OF INDICATOR AND MENU
def __init__(self):
#TODO Choose the best category for the indicator : appindicator.CATEGORY_APPLICATION_STATUS or appindicator.CATEGORY_SYSTEM_SERVICES
self.ind = appindicator.Indicator ("bumblebee-indicator", "bumblebee-indicator", appindicator.CATEGORY_HARDWARE)
self.ind.set_status (appindicator.STATUS_ACTIVE)
self.ind.set_icon_theme_path(app.Config.icon_file_directory)
#TODO The icons style and accesibility must be enhanced : see icons/test directory
self.ind.set_icon(app.Config.icon_file_directory + "bumblebee-indicator.svg",'Bumblebee is unactive')
self.ind.set_attention_icon (app.Config.icon_file_directory + "bumblebee-indicator-active.svg",'Bumblebee is active')
self.card_state=False
self.build_menu()
self.menu.show()
self.ind.set_menu(self.menu)
def quit(self, widget, data=None):
gtk.main_quit()
def build_menu(self):
self.menu = gtk.Menu()
self.switch = gtk.CheckMenuItem()
self.initial_state_checker()
self.switch.set_sensitive(False)
self.menu.append(self.switch)
self.build_menu_separator()
self.prefered_app_submenu = gtk.MenuItem("Preferred Apps")
self.update_menu()
self.prefered_app_submenu.connect('activate', self.update_menu)
self.menu.append(self.prefered_app_submenu)
item2 = gtk.MenuItem("Configure Apps")
item2.connect("activate", self.app_configure)
self.menu.append(item2)
#TODO An UI to configure Bumblebee would be nice
#item3 = gtk.MenuItem("Configure Bumblebee")
#item3.show()
#self.menu.append(item3)
self.build_menu_separator()
quit = gtk.ImageMenuItem(gtk.STOCK_QUIT)
quit.connect("activate", self.quit)
self.menu.append(quit)
self.menu.show_all()
def build_menu_separator(self):
separator = gtk.SeparatorMenuItem()
separator.show()
self.menu.append(separator)
#FUNCTION TO GET THE MENU FROM THE CONFIG FILE
def update_menu(self, widget=None):
self.pref_menu=gtk.Menu()
subitem = gtk.MenuItem("Glxgears")
subitem.connect("activate", self.call_app, ['optirun', 'glxgears'])
subitem.show()
self.pref_menu.append(subitem)
file_set.get_configured_from_cfg()
for file_name in file_set.configured_set:
[Name, File_name, Categorie, Icon_name] = DesktopFile(file_name).get_app_info()
subitem = gtk.MenuItem(label=Name)
#TODO : Discuss about style : is an icon needed for this simple shortcut
#subitem = gtk.ImageMenuItem()
#subitem.set_image(gtk.image_new_from_pixbuf(IconSet().get_pixbuf(Icon_name,icon_size=gtk.ICON_SIZE_MENU)))
#subitem.set_label(Name)
subitem.connect("activate", self.call_app, DesktopFile(file_name).get_exec_list())
subitem.show()
self.pref_menu.append(subitem)
self.pref_menu.show()
self.prefered_app_submenu.set_submenu(self.pref_menu)
def initial_state_checker(self):
if self.attention_state_condition(): self.set_attention_state(notify=False)
else : self.set_active_state(notify=False)
def state_checker(self):
if self.attention_state_condition():
if self.card_state == False : self.set_attention_state()
elif self.card_state == True: self.set_active_state()
return True
def attention_state_condition(self):
stdout_handle = os.popen("cat /etc/default/bumblebee |grep VGL_DISPLAY |cut -f2 -d:", "r")
vgl_display = stdout_handle.read().rstrip("\n")
vgl_display = "/tmp/.X%s-lock" % vgl_display
if os.path.exists(vgl_display): return True
else: return False
def set_attention_state(self, notify=True):
self.ind.set_status(appindicator.STATUS_ATTENTION)
self.card_state = True
if notify == True: self.notify_state("Discrete card : ON", "Discrete graphic card is powered on", "bumblebee-indicator-active")
self.switch.set_label("Discrete card : ON")
self.switch.set_active(True)
def set_active_state(self, notify=True):
self.ind.set_status(appindicator.STATUS_ACTIVE)
self.card_state = False
if notify == True: self.notify_state("Discrete card : OFF", "Discrete graphic card is powered off", "bumblebee-indicator")
self.switch.set_label("Discrete card : OFF")
self.switch.set_active(False)
def app_configure(self,widget):
Applications_settings(file_set=file_set)
def call_app(self, widget, app_exec):
#FIXME There is a problem when closing the launched app and when the indicator has been closed: the indicator is still running : What a daemon!!
subprocess.Popen(app_exec,shell=False)
def about_box(self, widget, data=None):
dialog = gtk.MessageDialog(None, type=gtk.MESSAGE_INFO,buttons=gtk.BUTTONS_NONE,message_format="Preferences/About")
dialog.format_secondary_text("This is Optimus by mrs_sheep. \n It is based on bumblebee by MrMEEE. \n The configuration can (by now) only be set thorugh the config file!")
dialog.add_button(gtk.STOCK_CLOSE, gtk.RESPONSE_CLOSE)
dialog.run()
dialog.destroy()
def main(self):
self.state_checker()
#FIXME It would be nice to avoid this loop : Maybe by using a signal emitted by the system
#gtk.timeout_add(1000,self.state_checker)
gobject.timeout_add_seconds(2, self.state_checker)
gtk.main()
if __name__ == "__main__":
file_set=DesktopFileSet()
indicator = BumblebeeIndicator()
indicator.main()