diff options
author | Tomeu Vizoso <tomeu@tomeuvizoso.net> | 2008-10-22 08:34:01 (GMT) |
---|---|---|
committer | Tomeu Vizoso <tomeu@tomeuvizoso.net> | 2008-10-22 08:34:01 (GMT) |
commit | 39fcc1bc37bdde6e34e5bb3e2c8b5692851d8d58 (patch) | |
tree | b54e336ea249703ae641d374ae762d4101027984 | |
parent | 17d797ea4e9b35f57b42c99eb197ad301dadbb01 (diff) |
Modularize key handlers
-rw-r--r-- | configure.ac | 1 | ||||
-rw-r--r-- | extensions/Makefile.am | 2 | ||||
-rw-r--r-- | extensions/globalkey/Makefile.am | 5 | ||||
-rw-r--r-- | extensions/globalkey/__init__.py | 0 | ||||
-rw-r--r-- | extensions/globalkey/screenshot.py | 59 | ||||
-rw-r--r-- | src/jarabe/view/keyhandler.py | 67 |
6 files changed, 93 insertions, 41 deletions
diff --git a/configure.ac b/configure.ac index 6a96900..adb99ee 100644 --- a/configure.ac +++ b/configure.ac @@ -60,6 +60,7 @@ extensions/cpsection/language/Makefile extensions/cpsection/network/Makefile extensions/cpsection/power/Makefile extensions/deviceicon/Makefile +extensions/globalkey/Makefile src/Makefile src/jarabe/Makefile src/jarabe/controlpanel/Makefile diff --git a/extensions/Makefile.am b/extensions/Makefile.am index d402374..d4ab534 100644 --- a/extensions/Makefile.am +++ b/extensions/Makefile.am @@ -1 +1 @@ -SUBDIRS = cpsection deviceicon +SUBDIRS = cpsection deviceicon globalkey diff --git a/extensions/globalkey/Makefile.am b/extensions/globalkey/Makefile.am new file mode 100644 index 0000000..84a23ad --- /dev/null +++ b/extensions/globalkey/Makefile.am @@ -0,0 +1,5 @@ +sugardir = $(pkgdatadir)/extensions/globalkey + +sugar_PYTHON = \ + __init__.py \ + screenshot.py diff --git a/extensions/globalkey/__init__.py b/extensions/globalkey/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/extensions/globalkey/__init__.py diff --git a/extensions/globalkey/screenshot.py b/extensions/globalkey/screenshot.py new file mode 100644 index 0000000..ef4f5c9 --- /dev/null +++ b/extensions/globalkey/screenshot.py @@ -0,0 +1,59 @@ +# Copyright (C) 2008 One Laptop Per Child +# +# 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 + +import os +import tempfile +import time +from gettext import gettext as _ + +import gtk +import gconf + +from sugar.datastore import datastore + +BOUND_KEYS = ['<alt>1'] + +def handle_key_press(key): + file_path = os.path.join(tempfile.gettempdir(), '%i' % time.time()) + + window = gtk.gdk.get_default_root_window() + width, height = window.get_size() + x_orig, y_orig = window.get_origin() + + screenshot = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, has_alpha=False, + bits_per_sample=8, width=width, + height=height) + screenshot.get_from_drawable(window, window.get_colormap(), x_orig, + y_orig, 0, 0, width, height) + screenshot.save(file_path, "png") + + client = gconf.client_get_default() + color = client.get_string('/desktop/sugar/user/color') + + jobject = datastore.create() + try: + jobject.metadata['title'] = _('Screenshot') + jobject.metadata['keep'] = '0' + jobject.metadata['buddies'] = '' + jobject.metadata['preview'] = '' + jobject.metadata['icon-color'] = color + jobject.metadata['mime_type'] = 'image/png' + jobject.file_path = file_path + datastore.write(jobject, transfer_ownership=True) + finally: + jobject.destroy() + del jobject + diff --git a/src/jarabe/view/keyhandler.py b/src/jarabe/view/keyhandler.py index ea6ed74..4a50e70 100644 --- a/src/jarabe/view/keyhandler.py +++ b/src/jarabe/view/keyhandler.py @@ -19,22 +19,20 @@ import signal import logging import subprocess import errno -import tempfile -import time -from gettext import gettext as _ -import gconf +import traceback +import sys import dbus import gtk from sugar._sugarext import KeyGrabber -from sugar.datastore import datastore from jarabe.model import screen from jarabe.model import sound from jarabe.model import shell from jarabe.view.tabbinghandler import TabbingHandler from jarabe.model.shell import ShellModel +from jarabe import config _BRIGHTNESS_STEP = 2 _VOLUME_STEP = sound.VOLUME_STEP @@ -55,7 +53,6 @@ _actions_table = { 'F12' : 'volume_up', '<alt>F11' : 'volume_min', '<alt>F12' : 'volume_max', - '<alt>1' : 'screenshot', '0x93' : 'frame', '0xEB' : 'rotate', '<alt>Tab' : 'next_window', @@ -95,6 +92,21 @@ class KeyHandler(object): self._tabbing_handler = TabbingHandler(self._frame, _TABBING_MODIFIER) + for f in os.listdir(os.path.join(config.ext_path, 'globalkey')): + if f.endswith('.py') and not f.startswith('__'): + module_name = f[:-3] + try: + logging.debug('Loading module %r' % module_name) + module = __import__('globalkey.' + module_name, globals(), + locals(), [module_name]) + for key in module.BOUND_KEYS: + if key in _actions_table: + raise ValueError('Key %r is already bound' % key) + _actions_table[key] = module + except Exception: + logging.error('Exception while loading extension:\n' + \ + ''.join(traceback.format_exception(*sys.exc_info()))) + self._key_grabber.grab_keys(_actions_table.keys()) def _change_volume(self, step=None, value=None): @@ -193,37 +205,6 @@ class KeyHandler(object): def handle_volume_down(self): self._change_volume(step=-_VOLUME_STEP) - def handle_screenshot(self): - file_path = os.path.join(tempfile.gettempdir(), '%i' % time.time()) - - window = gtk.gdk.get_default_root_window() - width, height = window.get_size() - x_orig, y_orig = window.get_origin() - - screenshot = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, has_alpha=False, - bits_per_sample=8, width=width, - height=height) - screenshot.get_from_drawable(window, window.get_colormap(), x_orig, - y_orig, 0, 0, width, height) - screenshot.save(file_path, "png") - - client = gconf.client_get_default() - color = client.get_string('/desktop/sugar/user/color') - - jobject = datastore.create() - try: - jobject.metadata['title'] = _('Screenshot') - jobject.metadata['keep'] = '0' - jobject.metadata['buddies'] = '' - jobject.metadata['preview'] = '' - jobject.metadata['icon-color'] = color - jobject.metadata['mime_type'] = 'image/png' - jobject.file_path = file_path - datastore.write(jobject, transfer_ownership=True) - finally: - jobject.destroy() - del jobject - def handle_frame(self): self._frame.notify_key_press() @@ -298,8 +279,13 @@ class KeyHandler(object): self._tabbing_handler.stop() return True - method = getattr(self, 'handle_' + action) - method() + if hasattr(action, 'handle_key_press'): + action.handle_key_press(key) + elif isinstance(action, basestring): + method = getattr(self, 'handle_' + action) + method() + else: + raise TypeError('Invalid action %r' % action) return True else: @@ -329,4 +315,5 @@ def setup(frame): if _instance: del _instance - _instance = KeyHandler(frame) + _instance = KeyHandler(frame) + |