Factor out global keys handling from the shell

This commit is contained in:
Marco Pesenti Gritti 2007-01-18 15:04:56 +01:00
parent 704ad1c886
commit 1143623d35
3 changed files with 160 additions and 140 deletions

View File

@ -9,6 +9,7 @@ sugar_PYTHON = \
BuddyMenu.py \
clipboardicon.py \
clipboardmenu.py \
keyhandler.py \
OverlayWindow.py \
Shell.py \
stylesheet.py

View File

@ -16,10 +16,8 @@
import logging
import gtk
import gobject
import wnck
import dbus
import view.stylesheet
from sugar.graphics import style
@ -27,13 +25,10 @@ from view.home.HomeWindow import HomeWindow
from sugar.presence import PresenceService
from view.ActivityHost import ActivityHost
from sugar.activity import ActivityFactory
from sugar.activity import Activity
from view.frame.Frame import Frame
from model.ShellModel import ShellModel
from hardwaremanager import HardwareManager
from _sugar import KeyGrabber
from view.keyhandler import KeyHandler
from view.hardwaremanager import HardwareManager
from _sugar import AudioManager
from sugar import env
import sugar
class Shell(gobject.GObject):
@ -51,17 +46,12 @@ class Shell(gobject.GObject):
self._hw_manager = HardwareManager()
self._audio_manager = AudioManager()
self._key_grabber = KeyGrabber()
self._key_grabber.connect('key-pressed',
self._key_pressed_cb)
self._key_grabber.connect('key-released',
self._key_released_cb)
self._grab_keys()
self._home_window = HomeWindow(self)
self._home_window.show()
self.set_zoom_level(sugar.ZOOM_HOME)
self._key_handler = KeyHandler(self)
home_model = self._model.get_home()
home_model.connect('activity-added', self._activity_added_cb)
home_model.connect('activity-removed', self._activity_removed_cb)
@ -75,132 +65,6 @@ class Shell(gobject.GObject):
#self.start_activity('org.laptop.JournalActivity')
def _handle_camera_key(self):
if self._current_host:
if self._current_host.execute('camera', []):
return
self.start_activity('org.laptop.CameraActivity')
def _grab_keys(self):
self._key_grabber.grab('F1')
self._key_grabber.grab('F2')
self._key_grabber.grab('F3')
self._key_grabber.grab('F4')
self._key_grabber.grab('F5')
self._key_grabber.grab('F6')
self._key_grabber.grab('F7')
self._key_grabber.grab('F8')
self._key_grabber.grab('F9')
self._key_grabber.grab('F10')
self._key_grabber.grab('F11')
self._key_grabber.grab('F12')
self._key_grabber.grab('<alt>F5')
self._key_grabber.grab('<alt>F8')
self._key_grabber.grab('<alt>equal')
self._key_grabber.grab('<alt>0')
self._key_grabber.grab('0xDC') # Camera key
self._key_grabber.grab('0xE0') # Overlay key
self._key_grabber.grab('0x93') # Frame key
self._key_grabber.grab('0x7C') # Power key
self._key_grabber.grab('0xEB') # Rotate key
self._key_grabber.grab('0xEC') # Keyboard brightness
self._key_grabber.grab('<alt>Tab')
# For non-OLPC machines
self._key_grabber.grab('<alt>f')
self._key_grabber.grab('<alt>o')
self._key_grabber.grab('<alt>r')
self._key_grabber.grab('<alt><shift>s')
def _key_pressed_cb(self, grabber, key):
if key == 'F1':
self.set_zoom_level(sugar.ZOOM_MESH)
elif key == 'F2':
self.set_zoom_level(sugar.ZOOM_FRIENDS)
elif key == 'F3':
self.set_zoom_level(sugar.ZOOM_HOME)
elif key == 'F4':
self.set_zoom_level(sugar.ZOOM_ACTIVITY)
elif key == 'F5':
self._hw_manager.set_display_brightness(0)
elif key == 'F6':
self._hw_manager.set_display_brightness(5)
elif key == 'F7':
self._hw_manager.set_display_brightness(9)
elif key == 'F8':
self._hw_manager.set_display_brightness(15)
elif key == 'F9':
self._audio_manager.set_volume(0)
elif key == 'F10':
self._audio_manager.set_volume(40)
elif key == 'F11':
self._audio_manager.set_volume(75)
elif key == 'F12':
self._audio_manager.set_volume(100)
elif key == '<alt>F5':
self._hw_manager.set_display_mode(HardwareManager.COLOR_MODE)
elif key == '<alt>F8':
self._hw_manager.set_display_mode(HardwareManager.B_AND_W_MODE)
elif key == '<alt>equal' or key == '<alt>0':
gobject.idle_add(self._toggle_console_visibility_cb)
elif key == '<alt>f':
self._frame.notify_key_press()
elif key == '<alt>o':
self.toggle_chat_visibility()
elif key == '0xDC': # Camera key
self._handle_camera_key()
elif key == '0xE0': # Overlay key
self.toggle_chat_visibility()
elif key == '0x93': # Frame key
self._frame.notify_key_press()
elif key == '0x7C' or key == '<alt><shift>s': # Power key
self._shutdown()
elif key == '0xEB' or key == '<alt>r': # Rotate key
self._rotate_screen()
elif key == '0xEC': # Keyboard brightness
self._hw_manager.toggle_keyboard_brightness()
elif key == '<alt>Tab':
self.set_zoom_level(sugar.ZOOM_HOME)
box = self._home_window.get_home_box()
box.grab_and_rotate()
def _toggle_console_visibility_cb(self):
bus = dbus.SessionBus()
proxy = bus.get_object('org.laptop.sugar.Console',
'/org/laptop/sugar/Console')
console = dbus.Interface(proxy, 'org.laptop.sugar.Console')
console.toggle_visibility()
def _rotate_screen(self):
states = [ 'normal', 'left', 'inverted', 'right']
self._screen_rotation += 1
if self._screen_rotation == len(states):
self._screen_rotation = 0
gobject.spawn_async(['xrandr', '-o', states[self._screen_rotation]],
flags=gobject.SPAWN_SEARCH_PATH)
def _shutdown(self):
self._model.props.state = ShellModel.STATE_SHUTDOWN
if not env.is_emulator():
self._shutdown_system()
def _shutdown_system(self):
bus = dbus.SystemBus()
proxy = bus.get_object('org.freedesktop.Hal',
'/org/freedesktop/Hal/devices/computer')
mgr = dbus.Interface(proxy, 'org.freedesktop.Hal.Device.SystemPowerManagement')
mgr.Shutdown()
def _key_released_cb(self, grabber, key):
if key == '<shft><alt>F9':
self._frame.notify_key_release()
elif key == '0x93':
self._frame.notify_key_release()
def _activity_added_cb(self, home_model, home_activity):
activity_host = ActivityHost(home_activity)
self._hosts[activity_host.get_xid()] = activity_host
@ -227,9 +91,18 @@ class Shell(gobject.GObject):
if self._current_host:
self._current_host.set_active(True)
def get_hardware_manager(self):
return self._hw_manager
def get_audio_manager(self):
return self._audio_manager
def get_model(self):
return self._model
def get_frame(self):
return self._frame
def _join_success_cb(self, handler, activity, activity_ps, activity_id, activity_type):
logging.debug("Joining activity %s (%s)" % (activity_id, activity_type))
activity.join(activity_ps.object_path())

146
shell/view/keyhandler.py Normal file
View File

@ -0,0 +1,146 @@
import dbus
import gobject
from sugar import env
from view.hardwaremanager import HardwareManager
from model.ShellModel import ShellModel
from _sugar import KeyGrabber
import sugar
_actions_table = {
'F1' : 'zoom_mesh',
'F2' : 'zoom_friends',
'F3' : 'zoom_home',
'F4' : 'zoom_activity',
'F5' : 'brightness_1',
'F6' : 'brightness_2',
'F7' : 'brightness_3',
'F8' : 'brightness_4',
'F9' : 'volume_1',
'F10' : 'volume_2',
'F11' : 'volume_3',
'F12' : 'volume_4',
'<alt>F5' : 'color_mode',
'<alt>F8' : 'b_and_w_mode',
'<alt>equal' : 'console',
'<alt>0' : 'console',
'<alt>f' : 'frame',
'0x93' : 'frame',
'<alt>o' : 'overlay',
'0xE0' : 'overlay',
'0xDC' : 'camera',
'0x7C' : 'shutdown',
'<alt><shift>s' : 'shutdown',
'0xEB' : 'rotate',
'<alt>r' : 'rotate',
'0xEC' : 'keyboard_brightness',
'<alt>Tab' : 'home'
}
class KeyHandler(object):
def __init__(self, shell):
self._shell = shell
self._hw_manager = shell.get_hardware_manager()
self._audio_manager = shell.get_audio_manager()
self._key_grabber = KeyGrabber()
self._key_grabber.connect('key-pressed',
self._key_pressed_cb)
self._key_grabber.connect('key-released',
self._key_released_cb)
for key in _actions_table.keys():
self._key_grabber.grab(key)
def handle_zoom_mesh(self):
self._shell.set_zoom_level(sugar.ZOOM_MESH)
def handle_zoom_friends(self):
self._shell.set_zoom_level(sugar.ZOOM_FRIENDS)
def handle_zoom_home(self):
self._shell.set_zoom_level(sugar.ZOOM_HOME)
def handle_zoom_activity(self):
self._shell.set_zoom_level(sugar.ZOOM_ACTIVITY)
def handle_brightness_1(self):
self._hw_manager.set_display_brightness(0)
def handle_brightness_2(self):
self._hw_manager.set_display_brightness(5)
def handle_brightness_3(self):
self._hw_manager.set_display_brightness(9)
def handle_brightness_4(self):
self._hw_manager.set_display_brightness(15)
def handle_volume_1(self):
self._audio_manager.set_volume(0)
def handle_volume_2(self):
self._audio_manager.set_volume(40)
def handle_volume_3(self):
self._audio_manager.set_volume(75)
def handle_volume_4(self):
self._audio_manager.set_volume(100)
def handle_color_mode(self):
self._hw_manager.set_display_mode(HardwareManager.COLOR_MODE)
def handle_b_and_w_mode(self):
self._hw_manager.set_display_mode(HardwareManager.B_AND_W_MODE)
def handle_console(self):
gobject.idle_add(self._toggle_console_visibility_cb)
def handle_frame(self):
self._shell.get_frame().notify_key_press()
def handle_overlay(self):
self._shell.toggle_chat_visibility()
def handle_camera(self):
current_activity = self._shell.get_current_activity()
if current_activity:
if current_activity.execute('camera', []):
return
self._shell.start_activity('org.laptop.CameraActivity')
def handle_shutdown(self):
model = self._shell.get_model()
model.props.state = ShellModel.STATE_SHUTDOWN
if env.is_emulator():
return
bus = dbus.SystemBus()
proxy = bus.get_object('org.freedesktop.Hal',
'/org/freedesktop/Hal/devices/computer')
mgr = dbus.Interface(proxy, 'org.freedesktop.Hal.Device.SystemPowerManagement')
mgr.Shutdown()
def handle_keyboard_brightness(self):
self._hw_manager.toggle_keyboard_brightness()
def _key_pressed_cb(self, grabber, key):
action = _actions_table[key]
method = getattr(self, 'handle_' + action)
method()
def _key_released_cb(self, grabber, key):
if key == '<shft><alt>F9':
self._frame.notify_key_release()
elif key == '0x93':
self._frame.notify_key_release()
def _toggle_console_visibility_cb(self):
bus = dbus.SessionBus()
proxy = bus.get_object('org.laptop.sugar.Console',
'/org/laptop/sugar/Console')
console = dbus.Interface(proxy, 'org.laptop.sugar.Console')
console.toggle_visibility()