From 1bf36a3f2118d7b355316bc4b7b5148d2bc4a41e Mon Sep 17 00:00:00 2001
From: Walter Bender <walter@sugarlabs.org>
Date: Fri, 30 Jul 2010 15:49:11 -0400
Subject: [PATCH 1/2] no hardwired paths

---
 extensions/deviceicon/touchpad.py |   29 ++++++++++++++++++++++-------
 1 files changed, 22 insertions(+), 7 deletions(-)

diff --git a/extensions/deviceicon/touchpad.py b/extensions/deviceicon/touchpad.py
index 7f3ecb5..897961f 100644
--- a/extensions/deviceicon/touchpad.py
+++ b/extensions/deviceicon/touchpad.py
@@ -32,8 +32,8 @@ TOUCHPAD_MODES = ['capacitive', 'resistive']
 STATUS_TEXT = {TOUCHPAD_MODES[0]: _('finger'), TOUCHPAD_MODES[1]: _('stylus')}
 STATUS_ICON = {TOUCHPAD_MODES[0]: 'touchpad-' + TOUCHPAD_MODES[0],
                TOUCHPAD_MODES[1]: 'touchpad-' + TOUCHPAD_MODES[1]}
-# FLAG_PATH is used to preserve status between boots.
-FLAG_PATH = '/home/olpc/.olpc-pentablet-mode'
+# FLAG_FILE is used to preserve status between boots.
+FLAG_FILE = '.olpc-pentablet-mode'
 # NODE_PATH is used to communicate with the touchpad device.
 NODE_PATH = '/sys/devices/platform/i8042/serio1/ptmode'
 
@@ -103,6 +103,11 @@ def setup(tray):
     if os.path.exists(NODE_PATH):
         tray.add_device(DeviceView())
 
+        # if _flag_path exists, set the initial device value to stylus
+        _flag_path = os.join(os.environ['HOME'], FLAG_FILE)
+        if os.path.exists(_flag_path):
+            write_to_node_file(str(TOUCHPAD_MODES.index('stylus')))
+
 
 def read_touchpad_mode():
     """ Read the touchpad mode from the node path. """
@@ -117,13 +122,23 @@ def write_touchpad_mode(touchpad):
     """ Write the touchpad mode to the node path and set/unset the flag. """
     touchpad_mode_index = TOUCHPAD_MODES.index(touchpad)
 
-    node_file_handle = open(NODE_PATH, 'w')
-    node_file_handle.write(str(touchpad_mode_index))
-    node_file_handle.close()
+    write_to_node_file(str(touchpad_mode_index))
 
+    _flag_path = os.join(os.environ['HOME'], FLAG_FILE)
     if touchpad_mode_index == 0:
-        if os.path.exists(FLAG_PATH):
+        if os.path.exists(_flag_path):
             os.remove(FLAG_PATH)
     else:
-        flag_file_handle = open(FLAG_PATH, 'w')
+        flag_file_handle = open(_flag_path, 'w')
         flag_file_handle.close()
+
+
+def write_to_node_file(value):
+    """ Write to node path, catching exception is there is a problem """
+    try:
+        node_file_handle = open(NODE_PATH, 'w')
+    except IOError, e:
+        print e
+        return
+    node_file_handle.write(value)
+    node_file_handle.close()
-- 
1.7.0.4

