~gnome15-team/gnome15/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#        +-----------------------------------------------------------------------------+
#        | GPL                                                                         |
#        +-----------------------------------------------------------------------------+
#        | Copyright (c) Brett Smith <tanktarta@blueyonder.co.uk>                      |
#        |                                                                             |
#        | This program is free software; you can redistribute it and/or               |
#        | modify it under the terms of the GNU General Public License                 |
#        | as published by the Free Software Foundation; either version 2              |
#        | of the License, or (at your option) any later version.                      |
#        |                                                                             |
#        | This program is distributed in the hope that it will be useful,             |
#        | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
#        | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
#        | GNU General Public License for more details.                                |
#        |                                                                             |
#        | You should have received a copy of the GNU General Public License           |
#        | along with this program; if not, write to the Free Software                 |
#        | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
#        +-----------------------------------------------------------------------------+

import time
from ctypes import *
from threading import Thread

libg15 = cdll.LoadLibrary("libg15.so.1")

# Default key read timeout. Too low and keys will be missed (very obvious in a VM)
KEY_READ_TIMEOUT = 100

G15_LCD = 1
G15_KEYS = 2
G15_DEVICE_IS_SHARED = 4
G15_DEVICE_5BYTE_RETURN = 8
G15_DEVICE_G13 = 16
G15_DEVICE_G510 = 32

G15_KEY_READ_LENGTH = 9
G510_STANDARD_KEYBOARD_INTERFACE = 0x0

# Error codes
G15_NO_ERROR = 0
G15_ERROR_READING_USB_DEVICE=4
G15_TRY_AGAIN = 5
G15_ERROR_NOENT = -2
G15_ERROR_NODEV = -19

# Debug levels
G15_LOG_INFO = 1
G15_LOG_WARN = 0

class KeyboardReceiveThread(Thread):
    def __init__(self, callback, key_read_timeout, on_error):
        Thread.__init__(self)
        self._run = True
        self.name = "KeyboardReceiveThread"
        self.callback = callback
        self.on_exit = None
        self.on_unplug = None
        self.key_read_timeout = key_read_timeout
        self.on_error = on_error
        
    def deactivate(self):
        if self._run:
            self._run = False
        
    def run(self):    
        try:
            pressed_keys = c_int(0)
            while self._run:  
                err = libg15.getPressedKeys(byref(pressed_keys), 10)
                code = 0
                ext_code = 0
                if err == G15_NO_ERROR:
                    if is_ext_key(pressed_keys.value):
                        ext_code = int(pressed_keys.value)
                        ext_code &= ~(1<<28)
                        err = libg15.getPressedKeys(byref(pressed_keys), 10)
                        if err == G15_NO_ERROR:
                            code = pressed_keys.value
                        elif err in [ G15_TRY_AGAIN, G15_ERROR_READING_USB_DEVICE ]:
                            pass
                        elif err == G15_ERROR_NODEV:
                            # Device unplugged
                            self._run = False
                            if self.on_unplug is not None:
                                self.on_unplug()
                        else:
                            if  self.on_error is not None:
                                self.on_error(err)
                            break
                    else:
                        code = pressed_keys.value

                    self.callback(code, ext_code)
                elif err in [ G15_TRY_AGAIN, G15_ERROR_READING_USB_DEVICE ] :
                    continue
                elif err == G15_ERROR_NODEV:
                    # Device unplugged
                    self._run = False
                    if self.on_unplug is not None:
                        self.on_unplug()
                else:
                    if  self.on_error is not None:
                        self.on_error(err)
                    break
                    
        finally:
            if self.on_exit is not None:
                self.on_exit()
            self._run = True
            
class libg15_devices_t(Structure):
    _fields_ = [ ("name", c_char_p),
                 ("vendorid", c_int),
                 ("productid", c_int),
                 ("caps", c_int) ]
    
def is_ext_key(code):
    """
    Get if the key code provide is an "Extended Key". Extended keys are used
    to cope with libg15's restriction on the number of available codes,
    which the G13 exceeds.
    
    Keyword arguments:
    code        --    code to test if extended
    """
    return code & (1<<28) != 0
    
def grab_keyboard(callback, key_read_timeout = KEY_READ_TIMEOUT, on_error = None):
    """
    Start polling for keyboard events. Device must be initialised. The thread
    returned can be stopped by calling deactivate().
    
    The callback is invoked with two arguments. The first being the bit mask
    of any pressed non-extended codes. The second is the mask of any extended
    key presses. 
    
    Keyword arguments:
    callback        -- function to call on key event
    key_read_timeout -- timeout for reading key presses. too low and keys will be missed
    """
    t = KeyboardReceiveThread(callback, key_read_timeout, on_error)
    t.start()
    return t
    
def init(init_usb = True, vendor_id = 0, product_id = 0):
    """
    This one return G15_NO_ERROR on success, something
    else otherwise (for instance G15_ERROR_OPENING_USB_DEVICE
    """
    return libg15.setupLibG15(vendor_id, product_id, 1 if init_usb else 0)

def reinit():
    """ re-initialise a previously unplugged keyboard ie ENODEV was returned at some point """
    return libg15.re_initLibG15()
    

def exit():
    return libg15.exitLibG15()

def set_debug(level):
    """
    Keyword arguments:
    level        -- level, one of G15_LOG_INFO or G15_LOG_WARN
    """
    libg15.libg15Debug(level)
    
def write_pixmap(data):
    libg15.writePixmapToLCD(data)
    
def set_contrast(level):
    return libg15.setLCDContrast(level)
    
def set_leds(leds):
    return libg15.setLEDs(leds)
    
def set_lcd_brightness(level):
    return libg15.setLCDBrightness(level)
    
def set_keyboard_brightness(level):
    return libg15.setKBBrightness(level)
    
def set_keyboard_color(color):
    val =  libg15.setG510LEDColor(color[0], color[1], color[2])
    return val

def get_joystick_position():
    return ( libg15.getJoystickX(), libg15.getJoystickY() )

def __handle_key(code):
    print "Got %d" %code