1
|
|
|
"""Entry point and related functionality""" |
2
|
|
|
|
3
|
|
|
__author__ = "Stephan Sokolow (deitarion/SSokolow)" |
4
|
|
|
__license__ = "GNU GPL 2.0 or later" |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
import errno, logging, os, subprocess, sys |
8
|
|
|
from ConfigParser import RawConfigParser |
9
|
|
|
|
10
|
|
|
try: |
11
|
|
|
import pygtk |
12
|
|
|
pygtk.require('2.0') |
13
|
|
|
except ImportError: |
14
|
|
|
pass # Apparently Travis-CI's build environment doesn't add this |
15
|
|
|
|
16
|
|
|
import gtk, wnck |
17
|
|
|
|
18
|
|
|
from . import gtkexcepthook |
19
|
|
|
gtkexcepthook.enable() |
20
|
|
|
|
21
|
|
|
from . import commands, layout |
22
|
|
|
from .util import fmt_table, XInitError |
23
|
|
|
from .version import __version__ |
24
|
|
|
from .wm import WindowManager |
25
|
|
|
|
26
|
|
|
# Allow MyPy to work without depending on the `typing` package |
27
|
|
|
# (And silence complaints from only using the imported types in comments) |
28
|
|
|
try: |
29
|
|
|
# pylint: disable=unused-import |
30
|
|
|
from typing import Callable, Dict, Union # NOQA |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
except: # pylint: disable=bare-except |
34
|
|
|
pass |
35
|
|
|
|
36
|
|
|
#: Location for config files (determined at runtime). |
37
|
|
|
XDG_CONFIG_DIR = os.environ.get('XDG_CONFIG_HOME', |
38
|
|
|
os.path.expanduser('~/.config')) |
39
|
|
|
|
40
|
|
|
#: Default content for the config file |
41
|
|
|
DEFAULTS = { |
42
|
|
|
'general': { |
43
|
|
|
# Use Ctrl+Alt as the default base for key combinations |
44
|
|
|
'ModMask': '<Ctrl><Alt>', |
45
|
|
|
'UseWorkarea': True, |
46
|
|
|
'MovementsWrap': True, |
47
|
|
|
'ColumnCount': 3 |
48
|
|
|
}, |
49
|
|
|
'keys': { |
50
|
|
|
"KP_Enter": "monitor-switch", |
51
|
|
|
"KP_0": "maximize", |
52
|
|
|
"KP_1": "bottom-left", |
53
|
|
|
"KP_2": "bottom", |
54
|
|
|
"KP_3": "bottom-right", |
55
|
|
|
"KP_4": "left", |
56
|
|
|
"KP_5": "middle", |
57
|
|
|
"KP_6": "right", |
58
|
|
|
"KP_7": "top-left", |
59
|
|
|
"KP_8": "top", |
60
|
|
|
"KP_9": "top-right", |
61
|
|
|
"V": "vertical-maximize", |
62
|
|
|
"H": "horizontal-maximize", |
63
|
|
|
"C": "move-to-center", |
64
|
|
|
} |
65
|
|
|
} # type: Dict[str, Dict[str, Union[str, int, float, bool, None]]] |
66
|
|
|
|
67
|
|
|
KEYLOOKUP = { |
68
|
|
|
',': 'comma', |
69
|
|
|
'.': 'period', |
70
|
|
|
'+': 'plus', |
71
|
|
|
'-': 'minus', |
72
|
|
|
} #: Used for resolving certain keysyms |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
# TODO: Move this to a more appropriate place |
76
|
|
|
wnck.set_client_type(wnck.CLIENT_TYPE_PAGER) # pylint: disable=no-member |
77
|
|
|
|
78
|
|
|
# TODO: Audit all of my TODOs and API docs for accuracy and staleness. |
79
|
|
|
|
80
|
|
|
class QuickTileApp(object): |
81
|
|
|
"""The basic Glib application itself.""" |
82
|
|
|
|
83
|
|
|
keybinder = None |
84
|
|
|
dbus_name = None |
85
|
|
|
dbus_obj = None |
86
|
|
|
|
87
|
|
|
def __init__(self, winman, # type: WindowManager |
88
|
|
|
commands, # type: commands.CommandRegistry |
89
|
|
|
keys=None, # type: Dict[str, Callable] |
90
|
|
|
modmask=None # type: str |
91
|
|
|
): # type: (...) -> None |
92
|
|
|
"""Populate the instance variables. |
93
|
|
|
|
94
|
|
|
@param keys: A dict mapping X11 keysyms to L{CommandRegistry} |
95
|
|
|
command names. |
96
|
|
|
@param modmask: A modifier mask to prefix to all keybindings. |
97
|
|
|
@type winman: The L{WindowManager} instance to use. |
98
|
|
|
@type keys: C{dict} |
99
|
|
|
""" |
100
|
|
|
self.winman = winman |
101
|
|
|
self.commands = commands |
102
|
|
|
self._keys = keys or {} |
103
|
|
|
self._modmask = modmask or '' |
104
|
|
|
|
105
|
|
|
def run(self): # type: () -> bool |
106
|
|
|
"""Initialize keybinding and D-Bus if available, then call |
107
|
|
|
C{gtk.main()}. |
108
|
|
|
|
109
|
|
|
@returns: C{False} if none of the supported backends were available. |
110
|
|
|
@rtype: C{bool} |
111
|
|
|
|
112
|
|
|
@todo 1.0.0: Retire the C{doCommand} name. (API-breaking change) |
113
|
|
|
""" |
114
|
|
|
|
115
|
|
|
# Attempt to set up the global hotkey support |
116
|
|
|
try: |
117
|
|
|
from . import keybinder |
118
|
|
|
except ImportError: |
119
|
|
|
logging.error("Could not find python-xlib. Cannot bind keys.") |
120
|
|
|
else: |
121
|
|
|
self.keybinder = keybinder.init( |
122
|
|
|
self._modmask, self._keys, self.commands, self.winman) |
123
|
|
|
|
124
|
|
|
# Attempt to set up the D-Bus API |
125
|
|
|
try: |
126
|
|
|
from . import dbus_api |
127
|
|
|
except ImportError: |
128
|
|
|
logging.warn("Could not load DBus backend. " |
129
|
|
|
"Is python-dbus installed?") |
130
|
|
|
else: |
131
|
|
|
self.dbus_name, self.dbus_obj = dbus_api.init( |
132
|
|
|
self.commands, self.winman) |
133
|
|
|
|
134
|
|
|
# If either persistent backend loaded, start the GTK main loop. |
135
|
|
|
if self.keybinder or self.dbus_obj: |
136
|
|
|
try: |
137
|
|
|
gtk.main() # pylint: disable=no-member |
138
|
|
|
except KeyboardInterrupt: |
139
|
|
|
pass |
140
|
|
|
return True |
141
|
|
|
else: |
142
|
|
|
return False |
143
|
|
|
|
144
|
|
|
def show_binds(self): # type: () -> None |
145
|
|
|
"""Print a formatted readout of defined keybindings and the modifier |
146
|
|
|
mask to stdout. |
147
|
|
|
|
148
|
|
|
@todo: Look into moving this into L{KeyBinder} |
149
|
|
|
""" |
150
|
|
|
|
151
|
|
|
print "Keybindings defined for use with --daemonize:\n" |
152
|
|
|
print "Modifier: %s\n" % (self._modmask or '(none)') |
153
|
|
|
print fmt_table(self._keys, ('Key', 'Action')) |
154
|
|
|
|
155
|
|
|
def attach_glib_log_filter(): |
156
|
|
|
"""Attach a copy of grep to our stderr to filter out _OB_WM errors. |
157
|
|
|
|
158
|
|
|
Hook up grep to filter out spurious libwnck error messages that we |
159
|
|
|
can't filter properly because PyGTK doesn't expose g_log_set_handler() |
160
|
|
|
""" |
161
|
|
|
glib_log_filter = subprocess.Popen( |
162
|
|
|
['grep', '-v', 'Unhandled action type _OB_WM'], |
163
|
|
|
stdin=subprocess.PIPE) |
164
|
|
|
|
165
|
|
|
# Redirect stderr through grep |
166
|
|
|
assert glib_log_filter.stdin, "Did not receive stdin for log filter" |
167
|
|
|
os.dup2(glib_log_filter.stdin.fileno(), sys.stderr.fileno()) |
168
|
|
|
|
169
|
|
|
def load_config(path): # type: (str) -> RawConfigParser |
170
|
|
|
"""Load the config file from the given path, applying fixes as needed. |
171
|
|
|
|
172
|
|
|
@todo: Refactor all this |
173
|
|
|
""" |
174
|
|
|
first_run = not os.path.exists(path) |
175
|
|
|
|
176
|
|
|
config = RawConfigParser() |
177
|
|
|
|
178
|
|
|
# Make keys case-sensitive because keysyms must be |
179
|
|
|
config.optionxform = str # type: ignore # (Cannot assign to a method) |
180
|
|
|
|
181
|
|
|
# TODO: Maybe switch to two config files so I can have only the keys in the |
182
|
|
|
# keymap case-sensitive? |
183
|
|
|
config.read(path) |
184
|
|
|
dirty = False |
185
|
|
|
|
186
|
|
|
if not config.has_section('general'): |
187
|
|
|
config.add_section('general') |
188
|
|
|
# Change this if you make backwards-incompatible changes to the |
189
|
|
|
# section and key naming in the config file. |
190
|
|
|
config.set('general', 'cfg_schema', 1) |
191
|
|
|
dirty = True |
192
|
|
|
|
193
|
|
|
for key, val in DEFAULTS['general'].items(): |
194
|
|
|
if not config.has_option('general', key): |
195
|
|
|
config.set('general', key, str(val)) |
196
|
|
|
dirty = True |
197
|
|
|
|
198
|
|
|
mk_raw = modkeys = config.get('general', 'ModMask') |
199
|
|
|
if ' ' in modkeys.strip() and '<' not in modkeys: |
200
|
|
|
modkeys = '<%s>' % '><'.join(modkeys.strip().split()) |
201
|
|
|
logging.info("Updating modkeys format:\n %r --> %r", mk_raw, modkeys) |
202
|
|
|
config.set('general', 'ModMask', modkeys) |
203
|
|
|
dirty = True |
204
|
|
|
|
205
|
|
|
# Either load the keybindings or use and save the defaults |
206
|
|
|
if config.has_section('keys'): |
207
|
|
|
keymap = dict(config.items('keys')) |
208
|
|
|
else: |
209
|
|
|
keymap = DEFAULTS['keys'] |
210
|
|
|
config.add_section('keys') |
211
|
|
|
for row in keymap.items(): |
212
|
|
|
config.set('keys', row[0], row[1]) |
213
|
|
|
dirty = True |
214
|
|
|
|
215
|
|
|
# Migrate from the deprecated syntax for punctuation keysyms |
216
|
|
|
for key in keymap: |
217
|
|
|
# Look up unrecognized shortkeys in a hardcoded dict and |
218
|
|
|
# replace with valid names like ',' -> 'comma' |
219
|
|
|
if key in KEYLOOKUP: |
220
|
|
|
logging.warn("Updating config file from deprecated keybind syntax:" |
221
|
|
|
"\n\t%r --> %r", key, KEYLOOKUP[key]) |
222
|
|
|
config.remove_option('keys', key) |
223
|
|
|
config.set('keys', KEYLOOKUP[key], keymap[key]) |
224
|
|
|
dirty = True |
225
|
|
|
|
226
|
|
|
if dirty: |
227
|
|
|
cfg_file = open(path, 'wb') |
228
|
|
|
|
229
|
|
|
# TODO: REPORT: Argument 1 to "write" of "RawConfigParser" has |
230
|
|
|
# incompatible type BinaryIO"; expected "file" |
231
|
|
|
config.write(cfg_file) # type: ignore |
232
|
|
|
|
233
|
|
|
cfg_file.close() |
234
|
|
|
if first_run: |
235
|
|
|
logging.info("Wrote default config file to %s", path) |
236
|
|
|
|
237
|
|
|
return config |
238
|
|
|
|
239
|
|
|
def main(): # type: () -> None |
240
|
|
|
"""setuptools entry point""" |
241
|
|
|
# TODO: Switch to argparse |
242
|
|
|
from optparse import OptionParser, OptionGroup |
243
|
|
|
parser = OptionParser(usage="%prog [options] [action] ...", |
244
|
|
|
version="%%prog v%s" % __version__) |
245
|
|
|
parser.add_option('-d', '--daemonize', action="store_true", |
246
|
|
|
dest="daemonize", default=False, help="Attempt to set up global " |
247
|
|
|
"keybindings using python-xlib and a D-Bus service using dbus-python. " |
248
|
|
|
"Exit if neither succeeds") |
249
|
|
|
parser.add_option('-b', '--bindkeys', action="store_true", |
250
|
|
|
dest="daemonize", default=False, |
251
|
|
|
help="Deprecated alias for --daemonize") |
252
|
|
|
parser.add_option('--debug', action="store_true", dest="debug", |
253
|
|
|
default=False, help="Display debug messages") |
254
|
|
|
parser.add_option('--no-workarea', action="store_true", dest="no_workarea", |
255
|
|
|
default=False, help="Overlap panels but work better with " |
256
|
|
|
"non-rectangular desktops") |
257
|
|
|
|
258
|
|
|
help_group = OptionGroup(parser, "Additional Help") |
259
|
|
|
help_group.add_option('--show-bindings', action="store_true", |
260
|
|
|
dest="show_binds", default=False, help="List all configured keybinds") |
261
|
|
|
help_group.add_option('--show-actions', action="store_true", |
262
|
|
|
dest="show_args", default=False, help="List valid arguments for use " |
263
|
|
|
"without --daemonize") |
264
|
|
|
parser.add_option_group(help_group) |
265
|
|
|
|
266
|
|
|
opts, args = parser.parse_args() |
267
|
|
|
|
268
|
|
|
if not opts.debug: |
269
|
|
|
attach_glib_log_filter() |
270
|
|
|
|
271
|
|
|
# Set up the output verbosity |
272
|
|
|
logging.basicConfig(level=logging.DEBUG if opts.debug else logging.INFO, |
273
|
|
|
format='%(levelname)s: %(message)s') |
274
|
|
|
|
275
|
|
|
cfg_path = os.path.join(XDG_CONFIG_DIR, 'quicktile.cfg') |
276
|
|
|
first_run = not os.path.exists(cfg_path) |
277
|
|
|
config = load_config(cfg_path) |
278
|
|
|
|
279
|
|
|
ignore_workarea = ((not config.getboolean('general', 'UseWorkarea')) or |
280
|
|
|
opts.no_workarea) |
281
|
|
|
|
282
|
|
|
# TODO: Rearchitect so this hack isn't needed |
283
|
|
|
commands.cycle_dimensions = commands.commands.add_many( |
284
|
|
|
layout.make_winsplit_positions(config.getint('general', 'ColumnCount')) |
285
|
|
|
)(commands.cycle_dimensions) |
286
|
|
|
commands.commands.extra_state = {'config': config} |
287
|
|
|
|
288
|
|
|
try: |
289
|
|
|
winman = WindowManager(ignore_workarea=ignore_workarea) |
290
|
|
|
except XInitError as err: |
291
|
|
|
logging.critical("%s", err) |
292
|
|
|
sys.exit(1) |
293
|
|
|
app = QuickTileApp(winman, |
294
|
|
|
commands.commands, |
295
|
|
|
keys=dict(config.items('keys')), |
296
|
|
|
modmask=config.get('general', 'ModMask')) |
297
|
|
|
|
298
|
|
|
if opts.show_binds: |
299
|
|
|
app.show_binds() |
300
|
|
|
if opts.show_args: |
301
|
|
|
print commands.commands |
302
|
|
|
|
303
|
|
|
if opts.daemonize: |
304
|
|
|
if not app.run(): |
305
|
|
|
logging.critical("Neither the Xlib nor the D-Bus backends were " |
306
|
|
|
"available") |
307
|
|
|
sys.exit(errno.ENOENT) |
308
|
|
|
# FIXME: What's the proper exit code for "library not found"? |
309
|
|
|
elif not first_run: |
310
|
|
|
if args: |
311
|
|
|
winman.screen.force_update() |
312
|
|
|
|
313
|
|
|
for arg in args: |
314
|
|
|
commands.commands.call(arg, winman) |
315
|
|
|
while gtk.events_pending(): # pylint: disable=no-member |
316
|
|
|
gtk.main_iteration() # pylint: disable=no-member |
317
|
|
|
elif not opts.show_args and not opts.show_binds: |
318
|
|
|
print commands.commands |
319
|
|
|
print "\nUse --help for a list of valid options." |
320
|
|
|
sys.exit(errno.ENOENT) |
321
|
|
|
|
322
|
|
|
if __name__ == '__main__': |
323
|
|
|
main() |
324
|
|
|
|
325
|
|
|
# vim: set sw=4 sts=4 expandtab : |
326
|
|
|
|