forked from MrMEEE/bumblebee-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbumblebee-indicator
executable file
·273 lines (212 loc) · 10.7 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#!/usr/bin/python
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
### BEGIN LICENSE
#
# 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
from abc import ABCMeta, abstractmethod
# UI MODULE
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import GLib
from gi.repository import Gtk
from gi.repository import GObject
from gi.repository import Notify
from gi.repository import Gio
import xdg.Menu
# SYSTEM MODULE
import os
import subprocess
import sys
# ORIGINAL CLASS
import app.DesktopFile
import app.Config
from app.AppSettings import IconSet
import fcntl
# I18N MODULE
import gettext
gettext.install('bumblebee-ui', os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), 'i18n')))
class AbstractBumblebeeIndicator():
__metaclass__ = ABCMeta
def __init__(self):
self.lock_file = "/tmp/.X%s-lock" % app.Config.vgl_display
self.lock_monitor = Gio.File.new_for_path(self.lock_file).monitor_file(Gio.FileMonitorFlags.NONE, None)
self.lock_monitor.connect("changed", self.status_changed)
self.poll_status()
self.build_menu()
def build_menu_separator(self, menu):
separator = Gtk.SeparatorMenuItem()
#separator.show()
menu.append(separator)
def build_menu(self):
self.menu = Gtk.Menu()
self.switch = Gtk.CheckMenuItem()
self.switch.set_sensitive(False)
self.menu.append(self.switch)
self.build_menu_separator(self.menu)
self.prefered_app_submenu = Gtk.MenuItem(_(u"Preferred Apps"))
self.update_menu()
self.prefered_app_submenu.connect('activate', self.update_menu)
self.menu.append(self.prefered_app_submenu)
if app.Config.applications_setting_path :
item = Gtk.MenuItem(_(u"Configure Apps"))
item.connect("activate", self.app_configure)
self.menu.append(item)
self.build_menu_separator(self.menu)
#TODO An UI to configure Bumblebee would be nice
quit = Gtk.ImageMenuItem.new_from_stock(Gtk.STOCK_QUIT, None)
quit.connect("activate", self.quit)
self.menu.append(quit)
self.set_status(status=self.card_status, notify=False)
self.menu.show_all()
# FUNCTIONS TO BUILD THE "PREFERRED APP" MENU FROM THE LOCAL DESKTOP FILES
def update_menu(self, widget=None):
pref_menu=Gtk.Menu()
for item_info in app.Config.default_preferred_apps:
self.add_submenu_item( pref_menu, item_info)
if app.Config.applications_setting_path:
self.build_menu_separator( pref_menu )
self.get_configured_entries( pref_menu , xdg.Menu.parse(app.Config.menu_file_path))
pref_menu.show_all()
self.prefered_app_submenu.set_submenu( pref_menu )
def add_submenu_item(self, submenu, item_info):
[Name, Exec] = item_info
subitem = Gtk.MenuItem(label=Name)
subitem.connect("activate", self.call_app, ['optirun','-f'] + Exec)
subitem.show()
submenu.append( subitem )
# FUNCTIONS TO BUILD THE "PREFERRED APP" MENU FROM THE MENU FILE
def get_configured_entries(self, submenu, menu):
for entry in menu.getEntries():
if isinstance(entry, xdg.Menu.Menu):
self.get_configured_entries(submenu, entry)
elif isinstance(entry, xdg.Menu.MenuEntry):
desktopEntry=app.DesktopFile.GetDesktop(entry)
if desktopEntry.isConfigured():
self.add_submenu_item(submenu, desktopEntry.getConfiguredEntry())
# FUNCTION TO LAUNCH THE APPS WITHIN THE INDICATOR
def call_app(self, widget, app_exec):
print app_exec
print "Command launched through the bumblebee-ui : " + ' '.join(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)
# FUNCTIONS TO CHECK FOR THE STATUS OF THE INDICATOR
@abstractmethod
def set_indicator_status(self, status): pass
def poll_status(self):
if os.path.exists(self.lock_file): self.card_status = True
else: self.card_status = False
def status_changed(self, monitor, a_file, other_file, event_type):
if (event_type == Gio.FileMonitorEvent.CREATED):
self.set_status(True)
elif (event_type == Gio.FileMonitorEvent.DELETED):
self.set_status(False)
def set_status(self, status, notify=True):
# status is True if bumblebee is active
# status is False if bumblebee is unactive
self.card_status = status
label = app.Config.active_label if status else app.Config.unactive_label
comment = app.Config.active_comment if status else app.Config.unactive_comment
#FIXME: use Config paramenter for icon name
icon = 'bumblebee-indicator-active' if status else 'bumblebee-indicator'
self.set_indicator_status(status)
if notify == True: self.notify_status(label, comment, icon)
self.switch.set_label(label)
self.switch.set_active(status)
#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_status(self, title, msg, icon_name):
Notify.init('Bumblebee Indicator')
self.notification = Notify.Notification.new(summary=title, body=msg, icon=IconSet().get_uri(icon_name,48))
#self.notification.set_urgency(Notify.Urgency.NORMAL)
self.notification.set_category('device')
self.notification.set_hint('transient', GLib.Variant.new_boolean(True))
self.notification.show()
# FUNCTION TO DEFINE THE APPLICATIONS SETTING LINK IN THE INDICATOR
def app_configure(self,widget):
self.call_app(widget, [ app.Config.applications_setting_path ])
#Applications_settings()
def quit(self, widget, data=None):
Gtk.main_quit()
# MAIN LOOP LAUNCHING A STATUS CHECK EVERY TWO SECONDS
#FIXME This check every two second could be avoid if bumblebee send this signal through dbus with this shell commands,
#The indicator will then know in real time the status of bumblebee and later of the card with an other signal : bumblebee : the code is already there to receive this signal : think to add this two lines at the good place in the ubuntu devel release
#dbus-send --session --dest=org.bumblebee.indicator /org/bumblebee/indicator org.bumblebee.indicator.BumblebeeStarted
#dbus-send --session --dest=org.bumblebee.indicator /org/bumblebee/indicator org.bumblebee.indicator.BumblebeeStopped
def main(self):
Gtk.main()
class BumblebeeStatusIcon(AbstractBumblebeeIndicator):
# INITIALIZATION OF INDICATOR AND MENU
def __init__(self):
#TODO The icons style and accessibility must be enhanced : see icons/test directory
self.statusIcon = Gtk.StatusIcon.new_from_file(filename=app.Config.icon_file_directory + "bumblebee-indicator.svg")
self.statusIcon.set_tooltip_text(_(u"Bumblebee is unactive"))
self.statusIcon.connect("popup-menu", self.right_click_event)
super(BumblebeeStatusIcon, self).__init__()
self.statusIcon.position_menu(self.menu, self.statusIcon)
def right_click_event(self, icon, button, time):
self.menu.show_all()
def pos(menu, icon):
return (Gtk.StatusIcon.position_menu(menu, icon))
self.menu.popup(None, None, pos, self.statusIcon, button, time)
def set_indicator_status(self, status):
icon_filename = (app.Config.active_icon if status else app.Config.unactive_icon)
self.statusIcon.set_from_file(app.Config.icon_file_directory + icon_filename)
self.statusIcon.set_tooltip_text(_(u"Bumblebee is active") if status else _(u"Bumblebee is unactive"))
class BumblebeeAppIndicator(AbstractBumblebeeIndicator):
# INITIALIZATION OF INDICATOR AND MENU
def __init__(self):
self.ind = AppIndicator3.Indicator ("bumblebee-indicator", "bumblebee-indicator", AppIndicator3.IndicatorCategory.HARDWARE)
self.ind.set_status (AppIndicator3.IndicatorStatus.ACTIVE)
self.ind.set_icon_theme_path(app.Config.icon_file_directory)
#TODO The icons style and accessibility must be enhanced : see icons/test directory
self.ind.set_icon(app.Config.icon_file_directory + app.Config.unactive_icon,_(u"Bumblebee is unactive"))
self.ind.set_attention_icon (app.Config.icon_file_directory + app.Config.active_icon,_(u"Bumblebee is active"))
super(BumblebeeAppIndicator, self).__init__()
self.ind.set_menu(self.menu)
# FUNCTIONS TO SET THE STATUS OF THE INDICATOR AND LAUNCH NOTIFICATION
def set_attention_status(self, notify=True):
self.set_status(True, AppIndicator3.IndicatorStatus.ATTENTION,
app.Config.active_label,
app.Config.active_comment,
"bumblebee-indicator-active", notify)
def set_active_status(self, notify=True):
self.set_status(False, AppIndicator3.IndicatorStatus.ACTIVE,
app.Config.unactive_label,
app.Config.unactive_comment,
"bumblebee-indicator", notify)
def set_status(self, status, indicator, label, comment, icon, notify):
self.ind.set_status(indicator)
self.card_status = status
if notify == True: self.notify_status(label, comment, icon)
self.switch.set_label(label)
self.switch.set_active(status)
if __name__ == "__main__":
pid_file = 'bumblebee-indicator.pid'
fp = open(pid_file, 'w')
try:
fcntl.lockf(fp, fcntl.LOCK_EX | fcntl.LOCK_NB)
try:
from gi.repository import AppIndicator3
indicator = BumblebeeAppIndicator()
except ImportError:
# AppIndicator failed to import, fallback to Gtk.StatusIcon.
indicator = BumblebeeStatusIcon()
indicator.main()
except IOError:
print "Another instance of bumblebee-indicator is running : Quit"
quit()