-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
dock_applet.in
executable file
·724 lines (550 loc) · 24.3 KB
/
dock_applet.in
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
#!/usr/bin/env python3
"""Provide an application dock applet for the MATE panel
Create a Mate panel applet and handle events generated
by it
Note: Functionality for docked apps is provided in docked_app.py
Function for the dock is provided in dock.py
"""
# Copyright (C) 1997-2003 Free Software Foundation, Inc.
#
# This program 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 2 of the
# License, or (at your option) any later version.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
# 02110-1301, USA.
#
# Author:
# Robin Thompson
# do not change the value of this variable - it will be set during build
# according to the value of the --with-gtk3 option used with .configure
build_gtk2 = False
import gi
if build_gtk2:
gi.require_version("Gtk", "2.0")
gi.require_version("Wnck", "1.0")
else:
gi.require_version("Gtk", "3.0")
gi.require_version("Wnck", "3.0")
gi.require_version("MatePanelApplet", "4.0")
import os
import sys
import threading
sys.path.insert(1, '@pythondir@')
from Xlib.display import Display
from Xlib import X, error
from gi.repository import Gtk
from gi.repository import MatePanelApplet
from gi.repository import Gdk
from gi.repository import Gio
from gi.repository import GObject
from gi.repository import GLib
from gi.repository import Wnck
import xdg.DesktopEntry as DesktopEntry
from urllib.parse import urlparse
import docked_app
import dock
from log_it import log_it as log_it
drag_dropped = False # nasty global var used to keep track of whether or not a drag-drop event has
# occurred
# define a list of keyboard shortcuts to be used to activate specific apps in the dock
# '<Super>1' to '<Super>0' will correspond to apps 1 to 10
# '<Super><Alt>1' to '<Super><Alt>9' will correspond to apps 11 to 20
keyb_shortcuts = ["<Super>1", "<Super>2", "<Super>3", "<Super>4", "<Super>5",
"<Super>6", "<Super>7", "<Super>8", "<Super>9", "<Super>0",
"<Super><Alt>1", "<Super><Alt>2", "<Super><Alt>3", "<Super><Alt>4", "<Super><Alt>5",
"<Super><Alt>6", "<Super><Alt>7", "<Super><Alt>8", "<Super><Alt>9", "<Super><Alt>0"]
def applet_button_press(widget, event, the_dock):
"""Button press event for the applet
Handle right button press events only
Find the app that was right clicked and make a record of it
Args:
widget : the widget that was clicked
event : the event args
the_dock : the Dock object
"""
# we don't get click events for the right mouse button presumably
# because the panel hijacks them in order to produce the context menu
# However, we do get button press event for the right mouse button,
# so we can do what we need to do here ....
if event.button == 3:
# right click, so save the app that was clicked because
# the_dock.app_with_mouse is going to be set to None when the
# right click menu appears and we move the mouse over the menu to
# select an option
app = the_dock.get_app_at_mouse(event.x, event.y)
the_dock.right_clicked_app = app
# because the right click menu is about to be shown, we need to hide
# the window list
the_dock.hide_win_list()
the_dock.hide_act_list()
def applet_button_release(widget, event, the_dock):
"""Button press event for the applet
Handle left button release events only
If the button is released over a non-running app, start the app
If the button is released over a running app that isn't on the
current workspace, change workspace
If the button is released over a running app:
If the app has only a single window open, activate it
If window list is showing, hide it
If the window list is not visible, show it
Args:
widget : the widget that registered the release event
event : the event args
the_dock : the Dock object
"""
if event.button == 1:
# hide popups
the_dock.hide_act_list()
app = the_dock.get_app_at_mouse(event.x, event.y)
if app is not None:
start_app = app.is_running() is False
start_app = start_app | (event.state &
Gdk.ModifierType.SHIFT_MASK) != 0
if start_app:
app.start_app()
else:
# if the app only has a single window minimize or restore it, otherwise
# allow the user to select a window from a list
if app.get_num_windows() == 1:
the_dock.minimize_or_restore_windows(app, event)
else:
the_dock.do_window_selection(app)
# See https://bugs.launchpad.net/ubuntu-mate/+bug/1554128
if event.button == 2:
app = the_dock.get_app_at_mouse(event.x, event.y)
if app is not None:
the_dock.hide_win_list()
the_dock.hide_act_list()
app.start_app()
def applet_enter_notify(widget, event, the_dock):
"""Enter notify event for the applet
Brighten the icon of the app which the mouse is currently over
If another app is currently brightened, darken it to normal
Set up the right click menu for the dock based on the app which
the mouse is currently over
Start the timer for showing app window lists
Args:
widget : the widget that registered the event i.e. the applet
event : the event args
the_dock : the Dock object
"""
# get the app underneath the mouse cursor
app = the_dock.get_app_at_mouse(event.x, event.y)
# if an app is currently highlighted, de-highlight it
if the_dock.app_with_mouse is not None:
the_dock.app_with_mouse.has_mouse = False
the_dock.app_with_mouse.queue_draw()
the_dock.app_with_mouse = None
# highlight the app under the mouse cursor
if app is not None:
app.has_mouse = True
app.queue_draw()
the_dock.app_with_mouse = app
# set up the available options for the app
the_dock.set_actions_for_app(app)
def applet_leave_notify(widget, event, the_dock):
"""Leave notify event handle for the applet
Unbright any brightened app icon
Args:
widget : the widget that registered the event i.e. the applet
event : the event args
the_dock : the Dock object
"""
if the_dock.app_with_mouse is not None:
the_dock.app_with_mouse.has_mouse = False
the_dock.app_with_mouse.queue_draw()
the_dock.app_with_mouse = None
the_dock.stop_act_list_timer()
def applet_motion_notify(widget, event, the_dock):
"""Motion notify event for the applet
If the docked app under the mouse cursor does not have its icon
brightened and another app has a brightened icon then darken the other app
# icon and reset the applet tooltip text
Then, if the docked app under the mouse cursor does not have its icon
brightened then brighten it and setup the applet right click menu
Args:
widget : the widget that registered the event i.e. the applet
event : the event args
the_dock : the Dock object
"""
app = the_dock.get_app_at_mouse(event.x, event.y)
if (the_dock.app_with_mouse is not None) and \
(the_dock.app_with_mouse != app):
the_dock.app_with_mouse.has_mouse = False
the_dock.app_with_mouse.queue_draw()
widget.queue_draw()
# because a new app is highlighted reset the window list timer and hide
# any currently open window list and action list
the_dock.hide_win_list()
the_dock.reset_act_list_timer()
the_dock.hide_act_list()
if not build_gtk2:
# set the drag and drop cursor - needs to be done here as well
# as in begin-drag to ensure that the first time we do a drag the
# app icon is set correctly
widget.drag_source_set_icon_pixbuf(the_dock.app_with_mouse.app_pb)
if app is not None:
# reset the window list timer
the_dock.reset_act_list_timer()
if app.has_mouse is False:
app.has_mouse = True
app.queue_draw()
the_dock.app_with_mouse = app
the_dock.set_actions_for_app(app)
def applet_change_orient(applet, orient, the_dock):
"""Handler for applet change orientation event
Set the dock to the new orientation and re-show the applet
Args:
applet : the widget that registered the event i.e. the applet
orient : the new orientation
the_dock : the Dock object
"""
the_dock.set_new_orientation(orient)
the_dock.applet.show_all()
the_dock.show_or_hide_app_icons()
def applet_change_size(applet, size, the_dock):
"""Handler for the applet change size event
Resize the icon and recalculate the minimize location of each app in the
dock
Args:
applet : the widget that registered the event i.e. the applet
size : the new applet size
the_dock : the Dock object
"""
for app in the_dock.app_list:
the_dock.set_app_icon(app, size)
def applet_scroll_event(applet, event, the_dock):
""" Handler for the scroll event
Call the dock's function to move forward/backward through the active app's
windows
"""
the_dock.do_window_scroll(event.direction, event.time)
def applet_drag_begin(applet, context, the_dock):
"""
Handler for the drag-begin event
Let the dock know we're dragging an icon.
Redraw the icon of the app that's being dragged so that the user has
visual feedback that the drag has started
Set the drag cursor to the app icon
Start a timer to monitor the mouse x,y and move the dragged app icon
around the dock accordingly
"""
# we can sometimes get spurious applet-leave events just before a drag
# commences. This causes app_with_mouse to be set to None. Therefore we
# may need to identify the app under the mouse ourselves...
if the_dock.app_with_mouse is None:
the_dock.app_with_mouse = the_dock.get_app_under_mouse()
if the_dock.app_with_mouse is not None:
the_dock.app_with_mouse.set_dragee(True)
the_dock.app_with_mouse.queue_draw()
applet.drag_source_set_icon_pixbuf(the_dock.app_with_mouse.app_pb)
the_dock.start_drag_motion_timer(the_dock.app_with_mouse)
# finally, hide the window list if it was being shown
the_dock.hide_win_list()
the_dock.hide_act_list()
def applet_drag_data_get(widget, drag_context, data, info, time):
"""
Handler the for drag-data-get event
Set some dummy text as data for the drag and drop
"""
data.set_text("", -1)
def applet_drag_drop(widget, context, x, y, time, the_dock):
"""
Handler for the drag-drop event
The drag drop is over so:
Call Gtk.drag-finish and indicate the drag and drop completed ok
Let the dock know that the drag and drop has finished and redraw
the dragged app's icon
Stop the timer that monitors the mouse position
"""
app = the_dock.get_dragee()
if app is not None:
the_dock.stop_drag_motion_timer()
app.set_dragee(False)
app.queue_draw()
Gtk.drag_finish(context, True, False, time)
else:
# set the drag_dropped module level var so that the drag_data_received event knows
# the dnd needs to finish
global drag_dropped
drag_dropped = True
target = widget.drag_dest_find_target(context, None)
widget.drag_get_data(context, target, time)
return True
def applet_drag_data_received(widget, drag_context, x, y, data, info, time, the_dock):
""" Called when data has been requested from an external source
during a drag drop operation
Examine the data - if it is a .desktop file and the app it relates to
is not already in the dock, add it. If the data isn't a .desktop file
and the app under the mouse cursor is running, activate it so that
the dragged data can be dropped there...
:param widget: the widget responsible for the event
:param drag_context: the dnd context
:param x: the x position of the mouse
:param y: y the y position of the mouse
:param data: the dragged data
:param info:
:param time: the time of the event
:param the_dock: the dock ....
"""
# examine the data -did we get any uris ?
uri_list = data.get_uris()
if (uri_list is not None) and (len(uri_list) > 0):
# when dragging .desktop files to the dock we only allow one to be added at
# a time. Therefore we're only interested in the first item in the list
uri = urlparse(uri_list[0])
if uri.scheme == "file":
# we're looking for a .desktop file
if (uri.path != "") and (os.path.split(uri.path)[1].endswith(".desktop")) and \
(os.path.exists(uri.path)):
# we've got a .desktop file, so if it has been dropped we may need
# to add it to the dock
global drag_dropped
if drag_dropped:
# add the .desktop file to the dock if it is not already there,,,
the_dock.add_app_to_dock(uri.path)
# cancel the dnd
Gtk.drag_finish(drag_context, True, False, time)
drag_dropped = False
return
else:
# the dnd continues ....
Gdk.drag_status(drag_context, Gdk.DragAction.COPY, time)
return
# this is not a .desktop so we need to activate the app under the mouse
tgt_app = the_dock.get_app_under_mouse()
the_dock.start_da_timer(tgt_app)
Gdk.drag_status(drag_context, Gdk.DragAction.COPY, time)
def applet_drag_end(widget, context, the_dock):
"""
Handler for the drag-end event
This will be triggered when e.g. the use drags an icon off the panel and
releases the mouse button ....
Let the dock know that the drag and drop has finished and redraw the
dragged app's icon
Stop the timer that monitors the mouse position
"""
the_dock.stop_drag_motion_timer()
app = the_dock.get_dragee()
if app is not None:
app.set_dragee(False)
app.queue_draw()
def applet_drag_motion(widget, context, x, y, time, the_dock):
""" Handler for the drag-motion event
:param widget: - the applet
:param context: - the dnd context
:param x: - x coord of the mouse
:param y: - y coord of the mouse
:param time: - the time of the event
:param the_dock - the dock
:return:
"""
# if the applet isn't dragging an app icon, we may need to examine
# the dragged data to see whay it contains....
app = the_dock.get_dragee()
if app is None:
# examine the dragged data so we can decide what to do...
tgts = context.list_targets()
for t in tgts:
if t.name() == "text/uri-list":
# if the data contains uris, we need to request the data to
# see if it contains a .desktop file
widget.drag_get_data(context, t, time)
return True
# if the dragged data is anything other than a uri, we just need to activate the app under
# the mouse...
tgt_app = the_dock.get_app_under_mouse()
the_dock.start_da_timer(tgt_app)
return True
else:
# continue the dnd...
Gdk.drag_status(context, Gdk.DragAction.COPY, time)
return True
def applet_shortcut_handler(keybinder, the_dock):
""" Handler for global keyboard shortcut presses
Start the app if it isn't already running
If it is already runnning cycle through its windows ...
:param keybinder: the keybinder object with the keystring which was pressed e.g. "<Super>4"
:param the_dock: the dock...
"""
# get the position in the dock of the app we need to activate
if keybinder.current_shortcut in keybinder.shortcuts:
app_no = keybinder.shortcuts.index(keybinder.current_shortcut)
app = the_dock.get_app_by_pos(app_no)
if app is not None:
start_app = app.is_running() is False
if start_app:
app.start_app()
else:
# if the app only has a single window minimize or restore it
# otherwise scroll through all available windows
if app.get_num_windows() == 1:
the_dock.minimize_or_restore_windows(app, None)
else:
the_dock.do_window_scroll(Gdk.ScrollDirection.DOWN, 0, app)
def applet_fill(applet):
"""
Create the applet
Register the events that we're interested in getting events for and
connect event handlers for them
Create a dock and add it V/HBox to the applet
Args:
applet : the applet
"""
os.chdir(os.path.expanduser("~"))
applet.set_events(applet.get_events() | Gdk.EventMask.BUTTON_PRESS_MASK
| Gdk.EventMask.BUTTON_RELEASE_MASK
| Gdk.EventMask.POINTER_MOTION_MASK
| Gdk.EventMask.KEY_PRESS_MASK
| Gdk.EventMask.KEY_RELEASE_MASK
| Gdk.EventMask.SCROLL_MASK
| Gdk.EventMask.STRUCTURE_MASK)
the_dock = dock.Dock(applet)
the_dock.setup_dock()
applet.add(the_dock.box)
applet.show_all()
applet.connect("enter-notify-event", applet_enter_notify, the_dock)
applet.connect("leave-notify-event", applet_leave_notify, the_dock)
applet.connect("motion-notify-event", applet_motion_notify, the_dock)
applet.connect("button-press-event", applet_button_press, the_dock)
applet.connect("button-release-event", applet_button_release, the_dock)
applet.connect("change-orient", applet_change_orient, the_dock)
applet.connect("change-size", applet_change_size, the_dock)
applet.connect("scroll-event", applet_scroll_event, the_dock)
if not build_gtk2:
# set up drag and drop - gtk3 only
# NOTE: we don't get drag-motion events when dragging app icons within the
# dock, making it difficult to tell where the mouse pointer is.....
# To get around this, dock.py now contains a timer to monitor the
# mouse x.y during these sorts of drag and drops.
# drag-motion events do fire when dropping from other apps (e.g. caja, the
# and the drag-motion event is used in these cases
# we allow .desktop files to be dropped on the applet, so....
drag_tgts = [Gtk.TargetEntry.new("text/uri-list", 0, 0)]
applet.drag_source_set(Gdk.ModifierType.BUTTON1_MASK, drag_tgts,
Gdk.DragAction.MOVE)
drag_tgts = [Gtk.TargetEntry.new("text/uri-list", 0, 0)]
applet.drag_dest_set(Gtk.DestDefaults.MOTION, None, Gdk.DragAction.COPY)
applet.drag_dest_add_image_targets()
applet.drag_dest_add_text_targets()
applet.drag_dest_add_uri_targets()
applet.connect_after("drag-begin", applet_drag_begin, the_dock)
applet.connect("drag-data-get", applet_drag_data_get)
applet.connect("drag-drop", applet_drag_drop, the_dock)
applet.connect("drag-end", applet_drag_end, the_dock)
applet.connect("drag-motion", applet_drag_motion, the_dock)
applet.connect("drag-data-received", applet_drag_data_received, the_dock)
# set up keyboard shortcuts used to activate apps in the dock
keybinder = GlobalKeyBinding()
for shortcut in keyb_shortcuts:
keybinder.grab(shortcut)
keybinder.connect("activate", applet_shortcut_handler, the_dock)
keybinder.start()
applet.set_background_widget(applet) # hack for panel transparency
def applet_factory(applet, iid, data):
"""Factory routine called when an applet needs to be created
Create a dock applet if necessary
Args:
applet : the applet
iid : the id of the applet that needs to be created
data :
Returns:
True if we created a dock applet, False otherwise
"""
if iid != "DockApplet":
return False
applet_fill(applet)
return True
class GlobalKeyBinding(GObject.GObject, threading.Thread):
__gsignals__ = {
'activate': (GObject.SignalFlags.RUN_LAST, None, ()),
}
def __init__(self):
GObject.GObject.__init__(self)
threading.Thread.__init__(self)
self.setDaemon(True)
self.display = Display()
self.screen = self.display.screen()
self.window = self.screen.root
self.keymap = Gdk.Keymap().get_default()
self.ignored_masks = self.get_mask_combinations(X.LockMask | X.Mod2Mask | X.Mod5Mask)
self.map_modifiers()
self.shortcuts = []
def get_mask_combinations(self, mask):
return [x for x in range(mask+1) if not (x & ~mask)]
def map_modifiers(self):
gdk_modifiers = (Gdk.ModifierType.CONTROL_MASK, Gdk.ModifierType.SHIFT_MASK, Gdk.ModifierType.MOD1_MASK,
Gdk.ModifierType.MOD2_MASK, Gdk.ModifierType.MOD3_MASK, Gdk.ModifierType.MOD4_MASK, Gdk.ModifierType.MOD5_MASK,
Gdk.ModifierType.SUPER_MASK, Gdk.ModifierType.HYPER_MASK)
self.known_modifiers_mask = 0
for modifier in gdk_modifiers:
if "Mod" not in Gtk.accelerator_name(0, modifier) or "Mod4" in Gtk.accelerator_name(0, modifier):
self.known_modifiers_mask |= modifier
def idle(self):
self.emit("activate")
return False
def activate(self):
GLib.idle_add(self.run)
def grab(self, shortcut):
keycode = None
accelerator = shortcut.replace("<Super>", "<Mod4>")
keyval, modifiers = Gtk.accelerator_parse(accelerator)
try:
keycode = self.keymap.get_entries_for_keyval(keyval).keys[0].keycode
except AttributeError:
# In older Gtk3 the get_entries_for_keyval() returns an unnamed tuple...
keycode = self.keymap.get_entries_for_keyval(keyval)[1][0].keycode
modifiers = int(modifiers)
self.shortcuts.append([keycode, modifiers])
# Request to receive key press/release reports from other windows that may not be using modifiers
catch = error.CatchError(error.BadWindow)
self.window.change_attributes(onerror=catch, event_mask = X.KeyPressMask)
if catch.get_error():
return False
catch = error.CatchError(error.BadAccess)
for ignored_mask in self.ignored_masks:
mod = modifiers | ignored_mask
result = self.window.grab_key(keycode, mod, True, X.GrabModeAsync, X.GrabModeAsync, onerror=catch)
self.display.flush()
if catch.get_error():
return False
return True
def run(self):
self.running = True
while self.running:
event = self.display.next_event()
modifiers = event.state & self.known_modifiers_mask
self.current_shortcut = None
if event.type == X.KeyPress and [event.detail, modifiers] in self.shortcuts:
# Track this shortcut to know which app to activate
self.current_shortcut = [event.detail, modifiers]
GLib.idle_add(self.idle)
self.display.allow_events(X.AsyncKeyboard, event.time)
else:
self.display.allow_events(X.ReplayKeyboard, event.time)
def stop(self):
self.running = False
self.ungrab()
self.display.close()
def ungrab(self):
for shortcut in self.shortcuts:
self.window.ungrab_key(shortcut[0], X.AnyModifier, self.window)
MatePanelApplet.Applet.factory_main("DockAppletFactory", True,
MatePanelApplet.Applet.__gtype__,
applet_factory, None)
def main():
"""Main function.
Debugging code can go here
"""
pass
if __name__ == "__main__":
main()