0 | | - |
| 1 | # Copyright (C) 2010, Walter Bender, Sugar Labs |
| 2 | # |
| 3 | # This program is free software; you can redistribute it and/or modify |
| 4 | # it under the terms of the GNU General Public License as published by |
| 5 | # the Free Software Foundation; either version 2 of the License, or |
| 6 | # (at your option) any later version. |
| 7 | # |
| 8 | # This program is distributed in the hope that it will be useful, |
| 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | # GNU General Public License for more details. |
| 12 | # |
| 13 | # You should have received a copy of the GNU General Public License |
| 14 | # along with this program; if not, write to the Free Software |
| 15 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 16 | |
| 17 | |
| 18 | from gettext import gettext as _ |
| 19 | import os |
| 20 | |
| 21 | import gtk |
| 22 | import gconf |
| 23 | |
| 24 | from sugar.graphics.tray import TrayIcon |
| 25 | from sugar.graphics.xocolor import XoColor |
| 26 | from sugar.graphics.palette import Palette |
| 27 | from sugar.graphics import style |
| 28 | |
| 29 | from jarabe.frame.frameinvoker import FrameWidgetInvoker |
| 30 | |
| 31 | TOUCHPAD_MODES = ['capacitive', 'resistive'] |
| 32 | STATUS_TEXT = {TOUCHPAD_MODES[0]: _('finger'), TOUCHPAD_MODES[1]: _('stylus')} |
| 33 | STATUS_ICON = {TOUCHPAD_MODES[0]: 'touchpad-' + TOUCHPAD_MODES[0], |
| 34 | TOUCHPAD_MODES[1]: 'touchpad-' + TOUCHPAD_MODES[1]} |
| 35 | # NODE_PATH is used to communicate with the touchpad device. |
| 36 | NODE_PATH = '/sys/devices/platform/i8042/serio1/ptmode' |
| 37 | |
| 38 | |
| 39 | class DeviceView(TrayIcon): |
| 40 | """ Manage the touchpad mode from the device palette on the Frame. """ |
| 41 | |
| 42 | FRAME_POSITION_RELATIVE = 500 |
| 43 | |
| 44 | def __init__(self): |
| 45 | """ Create the touchpad palette and display it on Frame. """ |
| 46 | icon_name = STATUS_ICON[read_touchpad_mode()] |
| 47 | |
| 48 | client = gconf.client_get_default() |
| 49 | color = XoColor(client.get_string('/desktop/sugar/user/color')) |
| 50 | TrayIcon.__init__(self, icon_name=icon_name, xo_color=color) |
| 51 | |
| 52 | self.set_palette_invoker(FrameWidgetInvoker(self)) |
| 53 | self.connect('button-release-event', self.__button_release_event_cb) |
| 54 | |
| 55 | def create_palette(self): |
| 56 | """ On create, set the current mode. """ |
| 57 | self.palette = ResourcePalette(_('My touchpad'), self.icon) |
| 58 | self.palette.set_group_id('frame') |
| 59 | return self.palette |
| 60 | |
| 61 | def __button_release_event_cb(self, widget, event): |
| 62 | """ On button release, switch modes. """ |
| 63 | self.palette.toggle_mode() |
| 64 | return True |
| 65 | |
| 66 | |
| 67 | class ResourcePalette(Palette): |
| 68 | """ Query the current state of the touchpad and update the display. """ |
| 69 | |
| 70 | def __init__(self, primary_text, icon): |
| 71 | """ Create the palette and initilize with current touchpad status. """ |
| 72 | Palette.__init__(self, label=primary_text) |
| 73 | |
| 74 | self._icon = icon |
| 75 | |
| 76 | vbox = gtk.VBox() |
| 77 | self.set_content(vbox) |
| 78 | |
| 79 | self._status_text = gtk.Label() |
| 80 | vbox.pack_start(self._status_text, padding=style.DEFAULT_PADDING) |
| 81 | self._status_text.show() |
| 82 | |
| 83 | vbox.show() |
| 84 | |
| 85 | self._mode = read_touchpad_mode() |
| 86 | self._update() |
| 87 | |
| 88 | def _update(self): |
| 89 | """ Update the label and icon based on the current mode. """ |
| 90 | self._status_text.set_label(STATUS_TEXT[self._mode]) |
| 91 | self._icon.props.icon_name = STATUS_ICON[self._mode] |
| 92 | |
| 93 | def toggle_mode(self): |
| 94 | """ On mouse click, toggle the mode. """ |
| 95 | self._mode = TOUCHPAD_MODES[1 - TOUCHPAD_MODES.index(self._mode)] |
| 96 | write_touchpad_mode(self._mode) |
| 97 | self._update() |
| 98 | |
| 99 | |
| 100 | def setup(tray): |
| 101 | """ Touchpad palette only appears when the device exisits. """ |
| 102 | if os.path.exists(NODE_PATH): |
| 103 | tray.add_device(DeviceView()) |
| 104 | |
| 105 | # Set the initial touchpad device value to finger |
| 106 | write_touchpad_mode('capacitive') |
| 107 | |
| 108 | |
| 109 | def read_touchpad_mode(): |
| 110 | """ Read the touchpad mode from the node path. """ |
| 111 | node_file_handle = open(NODE_PATH, 'r') |
| 112 | text = node_file_handle.read() |
| 113 | node_file_handle.close() |
| 114 | |
| 115 | return TOUCHPAD_MODES[int(text[0])] |
| 116 | |
| 117 | |
| 118 | def write_touchpad_mode(touchpad): |
| 119 | """ Write the touchpad mode to the node path. """ |
| 120 | write_to_node_file(str(TOUCHPAD_MODES.index(touchpad))) |
| 121 | |
| 122 | |
| 123 | def write_to_node_file(value): |
| 124 | """ Write to node path, catching exception is there is a problem """ |
| 125 | try: |
| 126 | node_file_handle = open(NODE_PATH, 'w') |
| 127 | except IOError, e: |
| 128 | print e |
| 129 | return |
| 130 | node_file_handle.write(value) |
| 131 | node_file_handle.close() |