Ticket #2006: 0001-add-touchpad-device-to-Frame-for-switching-between-c.patch

File 0001-add-touchpad-device-to-Frame-for-switching-between-c.patch, 5.7 KB (added by walter, 14 years ago)

new version of the patch with Tomeu's suggestions incorporated

  • extensions/deviceicon/Makefile.am

    From c7c92ce8f8f7ef4a50645bd2060c43ad88b2c497 Mon Sep 17 00:00:00 2001
    From: Walter Bender <walter@sugarlabs.org>
    Date: Mon, 16 Aug 2010 11:32:17 -0400
    Subject: [PATCH] add touchpad device to Frame for switching between capacitive and resistive modes
    
    ---
     extensions/deviceicon/Makefile.am |    1 +
     extensions/deviceicon/touchpad.py |  135 +++++++++++++++++++++++++++++++++++++
     2 files changed, 136 insertions(+), 0 deletions(-)
     create mode 100644 extensions/deviceicon/touchpad.py
    
    diff --git a/extensions/deviceicon/Makefile.am b/extensions/deviceicon/Makefile.am
    index 8a2e765..d46ddde 100644
    a b sugar_PYTHON = \ 
    55        battery.py      \
    66        network.py      \
    77        speaker.py      \
     8        touchpad.py     \
    89        volume.py
  • new file extensions/deviceicon/touchpad.py

    diff --git a/extensions/deviceicon/touchpad.py b/extensions/deviceicon/touchpad.py
    new file mode 100644
    index 0000000..d78eee2
    - +  
     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
     18from gettext import gettext as _
     19import os
     20
     21import gtk
     22import gconf
     23
     24import logging
     25
     26from sugar.graphics.tray import TrayIcon
     27from sugar.graphics.xocolor import XoColor
     28from sugar.graphics.palette import Palette
     29from sugar.graphics import style
     30
     31from jarabe.frame.frameinvoker import FrameWidgetInvoker
     32
     33TOUCHPAD_MODE_CAPACITIVE = 'capacitive'
     34TOUCHPAD_MODE_RESISTIVE = 'resistive'
     35TOUCHPAD_MODES = [TOUCHPAD_MODE_CAPACITIVE, TOUCHPAD_MODE_RESISTIVE]
     36STATUS_TEXT = {TOUCHPAD_MODE_CAPACITIVE: _('finger'),
     37               TOUCHPAD_MODES[1]: _('stylus')}
     38STATUS_ICON = {TOUCHPAD_MODE_CAPACITIVE: 'touchpad-' + TOUCHPAD_MODE_CAPACITIVE,
     39               TOUCHPAD_MODE_RESISTIVE: 'touchpad-' + TOUCHPAD_MODE_RESISTIVE}
     40# NODE_PATH is used to communicate with the touchpad device.
     41NODE_PATH = '/sys/devices/platform/i8042/serio1/ptmode'
     42
     43
     44class DeviceView(TrayIcon):
     45    """ Manage the touchpad mode from the device palette on the Frame. """
     46
     47    FRAME_POSITION_RELATIVE = 500
     48
     49    def __init__(self):
     50        """ Create the icon that represents the touchpad. """
     51        icon_name = STATUS_ICON[_read_touchpad_mode()]
     52
     53        client = gconf.client_get_default()
     54        color = XoColor(client.get_string('/desktop/sugar/user/color'))
     55        TrayIcon.__init__(self, icon_name=icon_name, xo_color=color)
     56
     57        self.set_palette_invoker(FrameWidgetInvoker(self))
     58        self.connect('button-release-event', self.__button_release_event_cb)
     59
     60    def create_palette(self):
     61        """ Create a palette for this icon; called by the Sugar framework
     62        when a palette needs to be displayed. """
     63        self.palette = ResourcePalette(_('My touchpad'), self.icon)
     64        self.palette.set_group_id('frame')
     65        return self.palette
     66
     67    def __button_release_event_cb(self, widget, event):
     68        """ Callback for button release event; used to invoke touchpad-mode
     69        change. """
     70        self.palette.toggle_mode()
     71        return True
     72
     73
     74class ResourcePalette(Palette):
     75    """ Palette attached to the decive icon that represents the touchpas. """
     76
     77    def __init__(self, primary_text, icon):
     78        """ Create the palette and initilize with current touchpad status. """
     79        Palette.__init__(self, label=primary_text)
     80
     81        self._icon = icon
     82
     83        vbox = gtk.VBox()
     84        self.set_content(vbox)
     85
     86        self._status_text = gtk.Label()
     87        vbox.pack_start(self._status_text, padding=style.DEFAULT_PADDING)
     88        self._status_text.show()
     89
     90        vbox.show()
     91
     92        self._mode = _read_touchpad_mode()
     93        self._update()
     94
     95    def _update(self):
     96        """ Update the label and icon based on the current mode. """
     97        self._status_text.set_label(STATUS_TEXT[self._mode])
     98        self._icon.props.icon_name = STATUS_ICON[self._mode]
     99
     100    def toggle_mode(self):
     101        """ Toggle the touchpad mode; called by touchpad icon
     102        button-release-event handler. """
     103        self._mode = TOUCHPAD_MODES[1 - TOUCHPAD_MODES.index(self._mode)]
     104        _write_touchpad_mode(self._mode)
     105        self._update()
     106
     107
     108def setup(tray):
     109    """ Initialize the devic icon; called by the shell when initializing the
     110    Frame. """
     111    if os.path.exists(NODE_PATH):
     112        tray.add_device(DeviceView())
     113
     114        # Set the initial touchpad device value to capacitive mode (finger)
     115        _write_touchpad_mode(TOUCHPAD_MODE_CAPACITIVE)
     116
     117
     118def _read_touchpad_mode():
     119    """ Read the touchpad mode from the node path. """
     120    node_file_handle = open(NODE_PATH, 'r')
     121    text = node_file_handle.read()
     122    node_file_handle.close()
     123
     124    return TOUCHPAD_MODES[int(text[0])]
     125
     126
     127def _write_touchpad_mode(touchpad):
     128    """ Write the touchpad mode to the node path. """
     129    try:
     130        node_file_handle = open(NODE_PATH, 'w')
     131    except IOError, e:
     132        logging.error('Error opening %s for writing: %s', NODE_PATH, e)
     133        return
     134    node_file_handle.write(str(TOUCHPAD_MODES.index(touchpad)))
     135    node_file_handle.close()