| 1 | # Copyright (c) 2008-2009, samurai-x.org |
|---|
| 2 | # All rights reserved. |
|---|
| 3 | # |
|---|
| 4 | # Redistribution and use in source and binary forms, with or without |
|---|
| 5 | # modification, are permitted provided that the following conditions are met: |
|---|
| 6 | # * Redistributions of source code must retain the above copyright |
|---|
| 7 | # notice, this list of conditions and the following disclaimer. |
|---|
| 8 | # * Redistributions in binary form must reproduce the above copyright |
|---|
| 9 | # notice, this list of conditions and the following disclaimer in the |
|---|
| 10 | # documentation and/or other materials provided with the distribution. |
|---|
| 11 | # * Neither the name of the samurai-x.org nor the |
|---|
| 12 | # names of its contributors may be used to endorse or promote products |
|---|
| 13 | # derived from this software without specific prior written permission. |
|---|
| 14 | # |
|---|
| 15 | # THIS SOFTWARE IS PROVIDED BY SAMURAI-X.ORG ``AS IS'' AND ANY |
|---|
| 16 | # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|---|
| 17 | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|---|
| 18 | # DISCLAIMED. IN NO EVENT SHALL SAMURAI-X.ORG BE LIABLE FOR ANY |
|---|
| 19 | # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
|---|
| 20 | # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|---|
| 21 | # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
|---|
| 22 | # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|---|
| 23 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|---|
| 24 | # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 25 | |
|---|
| 26 | """ |
|---|
| 27 | sx-bookmarks is a simple plugin for bookmarking windows. You can |
|---|
| 28 | assign ("set") a unique "bookmark key" to the currently focused |
|---|
| 29 | window to easily jump back to this window at any time (i.e. |
|---|
| 30 | "activate the bookmark"). You can assign multiple bookmarks to one window. |
|---|
| 31 | |
|---|
| 32 | Example: You focus a window, press ``Meta+b e`` ... and at any time |
|---|
| 33 | you want to, you can press ``Meta+g e`` to jump to the window's desktop |
|---|
| 34 | and focus it. |
|---|
| 35 | |
|---|
| 36 | sx-bookmarks doesn't have dependencies and isn't configurable. |
|---|
| 37 | |
|---|
| 38 | Actions |
|---|
| 39 | ------- |
|---|
| 40 | |
|---|
| 41 | .. function:: bookmarks.set(name) |
|---|
| 42 | :module: |
|---|
| 43 | |
|---|
| 44 | Associate the bookmark *name* to the currently focused window. |
|---|
| 45 | |
|---|
| 46 | .. function:: bookmarks.set_key() |
|---|
| 47 | :module: |
|---|
| 48 | |
|---|
| 49 | Grab the keyboard, wait for the user pressing an arbitrary key |
|---|
| 50 | and assign that key to the currently focused window. |
|---|
| 51 | |
|---|
| 52 | .. function:: bookmarks.activate(name) |
|---|
| 53 | :module: |
|---|
| 54 | |
|---|
| 55 | Activate the bookmark *name*. If needed, jump to another desktop. |
|---|
| 56 | If there is no such bookmark, print a warning to the log. |
|---|
| 57 | |
|---|
| 58 | .. function:: bookmarks.activate_key() |
|---|
| 59 | :module: |
|---|
| 60 | |
|---|
| 61 | Grab the keyboard, wait for the user pressing an arbitrary key |
|---|
| 62 | and pass that key to :func:`bookmarks.activate` as bookmark name. |
|---|
| 63 | |
|---|
| 64 | Example |
|---|
| 65 | ------- |
|---|
| 66 | |
|---|
| 67 | A typical configuration for sx-bookmarks would look like that:: |
|---|
| 68 | |
|---|
| 69 | 'bind.keys': { |
|---|
| 70 | 'meta+b': 'bookmarks.set_key', |
|---|
| 71 | 'meta+g': 'bookmarks.activate_key' |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | """ |
|---|
| 75 | |
|---|
| 76 | import logging |
|---|
| 77 | log = logging.getLogger(__name__) |
|---|
| 78 | |
|---|
| 79 | from weakref import WeakValueDictionary |
|---|
| 80 | |
|---|
| 81 | from samuraix.plugin import Plugin |
|---|
| 82 | from samuraix.util import wait_for_key |
|---|
| 83 | |
|---|
| 84 | from ooxcb import keysymdef |
|---|
| 85 | |
|---|
| 86 | class SXBookmarks(Plugin): |
|---|
| 87 | key = 'bookmarks' |
|---|
| 88 | |
|---|
| 89 | def __init__(self, app): |
|---|
| 90 | self.app = app |
|---|
| 91 | app.plugins['actions'].register('bookmarks.set', self.action_set) |
|---|
| 92 | app.plugins['actions'].register('bookmarks.set_key', self.action_set_key) |
|---|
| 93 | app.plugins['actions'].register('bookmarks.activate', self.action_activate) |
|---|
| 94 | app.plugins['actions'].register('bookmarks.activate_key', self.action_activate_key) |
|---|
| 95 | app.push_handlers(self) |
|---|
| 96 | |
|---|
| 97 | def on_new_screen(self, screen): |
|---|
| 98 | self.attach_data_to( |
|---|
| 99 | screen, |
|---|
| 100 | WeakValueDictionary()) |
|---|
| 101 | |
|---|
| 102 | def action_set(self, info): |
|---|
| 103 | screen = info['screen'] |
|---|
| 104 | name = info['name'] |
|---|
| 105 | if screen.focused_client is None: |
|---|
| 106 | log.warning('No focused client, no bookmark!') |
|---|
| 107 | else: |
|---|
| 108 | log.debug('Setting bookmark "%s" to %s' % (name, screen.focused_client)) |
|---|
| 109 | self.get_data(screen)[name] = screen.focused_client |
|---|
| 110 | |
|---|
| 111 | def action_activate(self, info): |
|---|
| 112 | screen = info['screen'] |
|---|
| 113 | name = info['name'] |
|---|
| 114 | client = self.get_data(screen).get(name, None) |
|---|
| 115 | if client is None: |
|---|
| 116 | log.warning('There is no bookmark named "%s"' % name) |
|---|
| 117 | else: |
|---|
| 118 | log.debug('Activating bookmark "%s"' % name) |
|---|
| 119 | screen.focus(client) |
|---|
| 120 | |
|---|
| 121 | def action_set_key(self, info): |
|---|
| 122 | screen = info['screen'] |
|---|
| 123 | def callback(keycode): |
|---|
| 124 | keysym = screen.conn.keysyms.get_keysym(keycode, 0) |
|---|
| 125 | if not keysym: |
|---|
| 126 | log.warning('Unknown keysym: %s' % keysym) |
|---|
| 127 | return |
|---|
| 128 | name = keysymdef.names[keysym] |
|---|
| 129 | info['name'] = name |
|---|
| 130 | self.action_set(info) |
|---|
| 131 | |
|---|
| 132 | wait_for_key(screen, callback) |
|---|
| 133 | |
|---|
| 134 | def action_activate_key(self, info): |
|---|
| 135 | screen = info['screen'] |
|---|
| 136 | def callback(keycode): |
|---|
| 137 | keysym = screen.conn.keysyms.get_keysym(keycode, 0) |
|---|
| 138 | if not keysym: |
|---|
| 139 | log.warning('Unknown keysym: %s' % keysym) |
|---|
| 140 | return |
|---|
| 141 | name = keysymdef.names[keysym] |
|---|
| 142 | info['name'] = name |
|---|
| 143 | self.action_activate(info) |
|---|
| 144 | |
|---|
| 145 | wait_for_key(screen, callback) |
|---|