-
Notifications
You must be signed in to change notification settings - Fork 1
/
adesk-menu
executable file
·424 lines (352 loc) · 13.2 KB
/
adesk-menu
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#!/usr/bin/python2
# -*- coding: utf-8 -*-
MENU_WIDTH = 280
MENU_HEIGHT = 180
import os
import sys
import gtk
import signal
cat_name = {'accessories':('Utility','Accessories'),
'development':('Development'),
'engineering':(),
'games':('Game'),
'graphics':('Graphics'),
'internet':('Internet', 'Network'),
'multimedia':('AudioVideo', 'Audio', 'Video', 'Multimedia'),
'office':('Office'),
'other':('Other'),
'science':(),
'system':('System', 'Administration'),
'settings':('Settings', 'X-XFCE'),
'utilities':()
}
def exec_cmd(cmd=None):
if cmd and not cmd=='':
os.system('%s &' % cmd)
# Find the Name / Command / Icon from .desktop file
def info_desktop(file):
cmd, icon, name, category = None, None, None, None
terminal = False
try:
cfile = open(file,"r")
for line in cfile:
if 'NoDisplay=true' in line or 'OnlyShowIn=KDE' in line:
icon, category = None, None
break
elif 'Terminal=true' in line:
terminal = True
elif '=' in line:
words = line.split('=')
if words[0] == 'Name':
name = words[1].replace('\n','')
# print 'Name =', name
elif words[0] == 'Icon':
icon = words[1].replace('\n','')
# print 'Icon =', icon
elif words[0] == 'Exec':
cmd = words[1].replace('\n','')
cmd = cmd.split('%')[0]
# print 'Exec :', cmd
elif words[0] == 'Categories':
tab = words[1].replace('\n','')
tab = tab.split(';')
#~ category = '***************************'
#~ if not 'X-XFCE' in tab:
for cat in tab:
found = False
for c_name in cat_name:
if cat in cat_name[c_name]:
category = c_name
found = True
break
if found:
break
# if command is 'console only', launch it with terminal ..
if terminal:
cmd = "x-terminal-emulator -e %s" % cmd
cfile.close()
except:
#~ print("# Error : parsing %s" % file)
pass
return (cmd, icon, name, category)
# Find the best icon with highest resolution for the launcher
def find_icon(icon):
foundiconfile=None
if icon == '':
return foundiconfile
if icon[0] == '/':
return icon
iconbase=('','Madbox','gnome','hicolor','locolor')
iconpath='/usr/share/icons'
sizelist =('', 'scalable', '256x256', '128x128', '64x64', '48x48', '32x32', '24x24')
categorylist=('actions', 'apps',"devices", 'categories','filesystems', 'places', 'status', '')
extensionlist = ('png', 'svg', 'xpm')
iconimagelist=[]
for extension in extensionlist:
if (icon.find('.'+extension) != -1):
icon = icon.replace('.'+extension,'')
for size in sizelist:
for extension in extensionlist:
for category in categorylist:
for iconbasecat in iconbase:
iconfile = iconpath+"/"+iconbasecat+'/'+size+'/'+category+'/'+icon+'.'+extension
iconimagelist.append(iconfile)
for extension in extensionlist:
iconfile = '/usr/share/pixmaps/'+icon+'.'+extension
iconimagelist.append(iconfile)
for extension in extensionlist:
iconfile = '/usr/share/app-install/icons/'+icon+'.'+extension
iconimagelist.append(iconfile)
# Seek if the files in pre-generated list exists.. first match is the best
# return it
for iconimage in iconimagelist:
if os.path.exists(iconimage):
return iconimage
return foundiconfile
def parse_desktop_dir(check_config=True):
config = {}
cfg_file = home = os.getenv('HOME') + '/.config/adesk-menu/config'
if os.access(cfg_file, os.F_OK|os.R_OK) and check_config:
print('found user config ..')
f = open(cfg_file,'r')
for line in f:
if line == '\n' or line.startswith('#'):
continue
elif line.startswith('@'):
cat = line[1:]
cat = cat.strip('\n')
cat = cat.strip(' ')
config[cat] = []
cat_index = cat
else:
line = line.strip('\n')
line = line.strip(' ')
config[cat_index].append(line.split('##'))
f.close()
else:
print('scan /home/.config/adesk-menu/applications and parse .desktop file ..')
#APP_PATH = '/usr/share/applications/'
APP_PATH = os.getenv('HOME') + '/.config/adesk-menu/applications/'
listdir = os.listdir(APP_PATH)
for i in listdir:
if '.desktop' in i:
#~ print i
(cmd, icon, name, category) = info_desktop(APP_PATH + i)
#~ print cmd, icon, name, category
if category:
if not config.has_key(category):
#~ print "@ add :", category
config[category] = []
if icon:
icon_path = find_icon(icon)
config[category].append((cmd, icon_path, name))
else:
pass
#~ print "File =", i
#~ print '=> NO ICON ...'
else:
pass
#~ print "File =", i
#~ print '=> NO CATEGORY ...'
#~ print config
return config
def write_config():
home = os.environ['HOME']
cfg_file = "%s/.config/adesk-menu/config" % home
if not os.path.exists("%s/.config/adesk-menu" % home):
os.makedirs("%s/.config/adesk-menu" % home)
f = open(cfg_file,'w')
m = parse_desktop_dir(False)
m_tmp = []
for cat in m:
m_tmp.append(cat)
m_tmp.sort()
for cat in m_tmp:
f.write('@%s\n' % cat)
for item in m[cat]:
(cmd, icon, text) = item
f.write('%s##%s##%s\n' % item)
f.close()
print "menu saved to %s" % cfg_file
class Menu():
def __init__(self):
self.hide_me = False
self.focus_check = False
self.mode = None
self.create_window()
# Menu
self.popupMenu = gtk.Menu()
menuPopup = gtk.ImageMenuItem (gtk.STOCK_QUIT)
menuPopup.connect("activate", self.doquit)
self.popupMenu.add(menuPopup)
self.popupMenu.show_all()
def doquit(self, widget=None, data=None):
gtk.main_quit()
def run(self):
gtk.main()
def create_window(self):
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL) #gtk.WINDOW_POPUP) ## gtk.WINDOW_TOPLEVEL)
self.window.connect("destroy", self.doquit)
#~ self.window.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_UTILITY)
self.window.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_NORMAL)
self.window.add_events(gtk.gdk.FOCUS_CHANGE_MASK)
self.window.connect("focus-out-event", self.lost_focus)
self.window.connect("key-press-event", self.onkeypress)
self.window.stick()
self.window.set_decorated(False)
self.window.set_keep_below(False)
self.window.set_keep_above(True)
self.window.set_resizable(True)
self.window.set_border_width(2)
self.window.set_app_paintable(True)
self.window.set_skip_taskbar_hint(True)
self.frame = gtk.Frame()
self.frame.show()
self.frame.set_size_request(MENU_WIDTH,MENU_HEIGHT)
self.nbook = gtk.Notebook()
self.nbook.show()
self.nbook.set_tab_pos(gtk.POS_LEFT)
self.nbook.set_border_width(2)
self.frame.add(self.nbook)
self.window.add(self.frame)
m = parse_desktop_dir()
m_tmp = []
for cat in m:
m_tmp.append(cat)
m_tmp.sort()
self.cat_but = []
self.cur_pos = 0
self.cat_visible = True
for cat in m_tmp:
bBox = gtk.VBox()
bBox.show()
bBox.set_spacing(4)
bBox.set_border_width(2)
scrolled = gtk.ScrolledWindow()
scrolled.show()
scrolled.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
scrolled.add_with_viewport(bBox)
label = gtk.Label(cat[0].upper() + cat[1:])
label.show()
label.set_alignment(0, 1)
self.nbook.append_page(scrolled, label)
for item in m[cat]:
(cmd, icon, text) = item
widget = gtk.Button() # item.label)
widget.set_relief(gtk.RELIEF_NONE)
widget.set_border_width(0)
widget.set_focus_on_click(False)
#~ widget.set_property('can-focus', False)
image = gtk.Image()
image.show()
image.set_size_request(24,24)
label = gtk.Label(text)
label.show()
label.set_alignment(0.2, 1)
align = gtk.Alignment(1, 0, 0.5, 0.5)
align.show()
align.add(label)
box = gtk.HBox(False, 4)
box.show()
box.pack_start(image, False, False)
box.pack_start(align, False, False)
widget.add(box)
try:
pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(icon, 24, 24)
except:
pixbuf = gtk.gdk.pixbuf_new_from_file_at_size('default.png', 24, 24)
image.set_from_pixbuf(pixbuf)
image.show()
widget.connect("button-release-event", self.ExecuteAction, cmd)
bBox.pack_start(widget,False,False)
widget.show()
def ExecuteAction(self, widget, event, cmd):
self.focus_check = False
exec_cmd(cmd)
self.doquit()
def toggle_hide(self, widget=None, event=None):
if self.hide_me:
self.hide_me = False
self.show_menu(self.mode)
self.window.show()
self.focus_check = True
else:
self.hide_me = True
self.focus_check = False
self.window.hide()
def show_menu(self, mode=None):
screen_width, screen_height = gtk.gdk.screen_width(), gtk.gdk.screen_height()
rootwin = self.window.get_screen().get_root_window()
w_width , w_height = self.window.get_size()
# Set x always to left
x = 0
# Set y value to be on top of tint2
y = screen_height - w_height - 30 # 30 it's the tint2 panel height
#to move the menu adjust the x= and y=
#this should not be commented out
self.window.move(x, y)
self.window.grab_focus()
def lost_focus(self, widget, event):
if self.focus_check:
self.doquit()
def onkeypress(self, widget, event):
if event.keyval == gtk.keysyms.Escape:
self.doquit()
def status_icon_popup(self, widget, button, active_time):
self.popupMenu.popup(None, None, None, 1, 0)
def callback_signal(self):
#~ print "callback_signal .."
self.mode = "mouse"
self.toggle_hide()
self.mode = None
##--
global g_menu
def signal_handler(signum, frame):
""" grab signal to quit """
# kill -10 <adesk-menu pid>
global g_menu
g_menu.doquit()
return
def save_pid():
home = os.environ['HOME']
pid_file = "%s/.config/adesk-menu/pid" % home
pid = os.getpid()
print "PID = %s" % pid
if not os.path.exists("%s/.config/adesk-menu" % home):
os.makedirs("%s/.config/adesk-menu" % home)
f = open(pid_file,'w')
f.write('%s' % pid)
f.close()
print "PID saved to %s" % pid_file
if __name__ == "__main__":
if len(sys.argv) == 2:
if sys.argv[1] == '-s':
write_config()
elif sys.argv[1] == '-t':
cmd = "kill -10 `cat ~/.config/adesk-menu/pid`"
os.system(cmd)
else:
print "Unknow option .."
print "Usage : adesk-menu [-s|-t]"
print " -s : save menu list ( ~/.config/adesk-menu/config )"
print " -t : show menu ( cursor position )"
else:
if os.system("kill -INT `cat ~/.config/adesk-menu/pid`"):
global g_menu
signal.signal(signal.SIGINT, signal_handler)
save_pid()
## need to change directory
SRC_PATH = os.path.dirname(os.path.realpath( __file__ ))
os.chdir(SRC_PATH)
g_menu = Menu()
# change directory to Home
os.chdir(os.getenv('HOME'))
g_menu.hide_me = False
g_menu.show_menu(g_menu.mode)
g_menu.window.show()
g_menu.focus_check = True
# Set focus to current window
g_menu.window.present()
g_menu.run()
else:
sys.exit()