Author: dmeyer
Date: Fri Apr  7 17:40:20 2006
New Revision: 1384

Added:
   trunk/base/src/input/stdin.py
Removed:
   trunk/base/src/utils.py
Modified:
   trunk/base/src/ipc.py
   trunk/base/src/notifier/__init__.py

Log:
move stdin signal code (getch) from util to input

Added: trunk/base/src/input/stdin.py
==============================================================================
--- (empty file)
+++ trunk/base/src/input/stdin.py       Fri Apr  7 17:40:20 2006
@@ -0,0 +1,106 @@
+import sys
+import tty
+import termios
+import os
+import atexit
+
+import kaa
+import kaa.notifier
+
+_tc_orig_settings = None
+_getch_enabled = False
+
+_keycode_names = {
+    "\x1b\x5b\x41": "up",
+    "\x1b\x5b\x42": "down",
+    "\x1b\x5b\x43": "right",
+    "\x1b\x5b\x44": "left",
+
+    "\x1b\x4f\x50": "F1",
+    "\x1b\x4f\x51": "F2",
+    "\x1b\x4f\x52": "F3",
+    "\x1b\x4f\x53": "F4",
+    "\x1b\x5b\x31\x35\x7e": "F5",
+    "\x1b\x5b\x31\x37\x7e": "F6",
+    "\x1b\x5b\x31\x38\x7e": "F7",
+    "\x1b\x5b\x31\x39\x7e": "F8",
+    "\x1b\x5b\x32\x30\x7e": "F9",
+    "\x1b\x5b\x32\x31\x7e": "F10",
+    "\x1b\x5b\x32\x33\x7e": "F11",
+    "\x1b\x5b\x32\x34\x7e": "F12",
+
+    "\x1b\x5b\x32\x7e": "ins",
+    "\x1b\x5b\x33\x7e": "del",
+    "\x1b\x4f\x46": "end",
+    "\x1b\x4f\x48": "home",
+    "\x1b\x1b": "esc",
+    "\x0a": "enter",
+    "\x20": "space",
+    "\x7f": "backspace"
+}
+    
+def getch():
+    global _getch_enabled
+
+    if not _getch_enabled:
+        getch_enable()
+        _getch_enabled = True
+
+    buf = sys.stdin.read(1)
+    while buf in ("\x1b", "\x1b\x4f", "\x1b\x5b") or \
+          buf[:3] in ("\x1b\x5b\x31", "\x1b\x5b\x32", "\x1b\x5b\x33"):
+        buf += sys.stdin.read(1)
+        if buf[-1] == "\x7e":
+            break
+
+    #print "KEYCODE:"
+    #for c in buf:
+    #    print "  " +  hex(ord(c)) 
+    code = buf
+    #buf = ""
+    if code in _keycode_names:
+        return _keycode_names[code]
+    elif len(code) == 1:
+        return code
+    else:
+        return "??"
+
+
+def getch_enable():
+    global _tc_orig_settings
+    _tc_orig_settings = termios.tcgetattr(sys.stdin.fileno())
+    tc = termios.tcgetattr(sys.stdin.fileno())
+    tc[3] = tc[3] & ~(termios.ICANON | termios.ECHO)
+    tc[6][termios.VMIN] = 1
+    tc[6][termios.VTIME] = 0
+    termios.tcsetattr(sys.stdin.fileno(), termios.TCSANOW, tc)
+    atexit.register(getch_disable)
+    
+
+def getch_disable():
+    global _tc_orig_settings
+    if _tc_orig_settings != None:
+        termios.tcsetattr(sys.stdin.fileno(), termios.TCSANOW, 
_tc_orig_settings)
+    os.system("stty echo")
+
+
+def _handle_stdin_keypress(fd):
+    ch = getch()
+    kaa.signals["stdin_key_press_event"].emit(ch)
+    return True
+
+
+_dispatcher = kaa.notifier.SocketDispatcher(_handle_stdin_keypress)
+
+def _keypress_signal_changed(signal, flag):
+    if flag == kaa.notifier.Signal.SIGNAL_CONNECTED and signal.count() == 1:
+        getch_enable()
+        _dispatcher.register(sys.stdin)
+    elif flag == kaa.notifier.Signal.SIGNAL_DISCONNECTED and signal.count() == 
0:
+        getch_disable()
+        _dispatcher.unregister()
+
+
+# init
+_signal = kaa.notifier.Signal(changed_cb = _keypress_signal_changed)
+kaa.signals["stdin_key_press_event"] = _signal

Modified: trunk/base/src/ipc.py
==============================================================================
--- trunk/base/src/ipc.py       (original)
+++ trunk/base/src/ipc.py       Fri Apr  7 17:40:20 2006
@@ -686,6 +686,7 @@
         if packet_type[:3] == "REQ" and timeout > 0:
             t0 = time.time()
             while self.socket and self._wait_queue[seq][1] == False and 
time.time() - t0 < timeout:
+                print 'step'
                 kaa.notifier.step()
         else:
             self.handle_write()

Modified: trunk/base/src/notifier/__init__.py
==============================================================================
--- trunk/base/src/notifier/__init__.py (original)
+++ trunk/base/src/notifier/__init__.py Fri Apr  7 17:40:20 2006
@@ -58,12 +58,6 @@
 # Set if currently in shutdown() (to prevent reentrancy)
 shutting_down = False
 
-def _handle_stdin_keypress(fd):
-    ch = utils.getch()
-    signals["stdin_key_press_event"].emit(ch)
-    return True
-
-
 def _idle_signal_changed(signal, flag):
     if flag == Signal.SIGNAL_CONNECTED and signal.count() == 1:
         notifier.dispatcher_add(signal.emit)
@@ -71,20 +65,9 @@
         notifier.dispatcher_remove(signal.emit)
 
 
-def _keypress_signal_changed(signal, flag):
-    if flag == Signal.SIGNAL_CONNECTED and signal.count() == 1:
-        utils.getch_enable()
-        notifier.socket_add(sys.stdin, _handle_stdin_keypress)
-    elif flag == Signal.SIGNAL_DISCONNECTED and signal.count() == 0:
-        utils.getch_disable()
-        notifier.socket_remove(sys.stdin)
-
-
 signals = {
     "shutdown": Signal(),
     "idle": Signal(changed_cb = _idle_signal_changed),
-    # Temporary until I find a better place.
-    "stdin_key_press_event": Signal(changed_cb = _keypress_signal_changed),
 }
 
 


-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
Freevo-cvslog mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freevo-cvslog

Reply via email to