~gnome15-team/gnome15/trunk

« back to all changes in this revision

Viewing changes to gnome15-plugins/src/g15daemon-server/g15daemon-server.py

  • Committer: tanktarta
  • Date: 2012-11-24 10:27:36 UTC
  • Revision ID: tanktarta-20121124102736-0drhasy3jdn862wx
0.9.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
#        | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
19
19
#        +-----------------------------------------------------------------------------+
20
20
 
21
 
import gnome15.g15locale as g15locale
22
 
_ = g15locale.get_translation("g15daemon-server", modfile = __file__).ugettext
23
 
 
24
 
import gnome15.g15screen as g15screen 
25
 
import gnome15.g15theme as g15theme 
26
 
import gnome15.g15util as g15util
27
 
import gnome15.g15driver as g15driver
28
 
import gtk
29
 
import os
30
 
import sys
31
 
import asyncore
32
 
import socket
33
21
from threading import Thread
34
 
import traceback
 
22
import Image
35
23
import ImageMath
36
 
import Image
37
24
import ImageOps
38
25
import array
39
 
import struct
 
26
import asyncore
40
27
import cairo
 
28
import gnome15.g15driver as g15driver
 
29
import gnome15.g15locale as g15locale
 
30
import gnome15.g15screen as g15screen
 
31
import gnome15.g15theme as g15theme
 
32
import gnome15.g15util as g15util
41
33
import gobject
 
34
import gtk
42
35
import logging
 
36
import os
 
37
import socket
 
38
import struct
 
39
import sys
 
40
import traceback
 
41
_ = g15locale.get_translation("g15daemon-server", modfile = __file__).ugettext
 
42
 
43
43
logger = logging.getLogger("g15daemon")
44
44
 
45
45
# Plugin details - All of these must be provided
120
120
    g15util.configure_checkbox_from_gconf(gconf_client, gconf_key + "/keep_aspect_ratio", "KeepAspectRatio", False, widget_tree, True)
121
121
    g15util.configure_checkbox_from_gconf(gconf_client, gconf_key + "/take_over_macro_keys", "TakeOverMacroKeys", True, widget_tree, True)
122
122
    
 
123
    g15util.configure_checkbox_from_gconf(gconf_client, gconf_key + "/use_custom_foreground", "UseCustomForeground", False, widget_tree)
 
124
    g15util.configure_colorchooser_from_gconf(gconf_client, gconf_key + "/custom_foreground", "CustomForeground", ( 255, 255, 255 ), widget_tree)
 
125
    
123
126
    dialog.run()
124
127
    dialog.hide()
125
128
 
131
134
        self.buffer_type = None
132
135
        self.buffer_len = 0
133
136
        self.surface = None
134
 
 
 
137
        self.last_img_buffer = None
135
138
        self.enable_keys = False
136
139
        self.plugin = plugin
137
140
        self.plugin.join(self)
138
141
        self.handshake = False
 
142
        self.keyboard_backlight_value = None
 
143
        self.backlight_value = None
139
144
                
140
145
        self.page = g15theme.G15Page("G15Daemon%d" % self.plugin.screen_index, plugin.screen, painter = self._paint, on_shown = self._on_shown, on_hidden = self._on_hidden, originating_plugin = self)
141
146
        self.page.set_title(_("G15Daemon Screen %d") % self.plugin.screen_index)
172
177
            # So instead, we just only send keyboard events if the client requests this.
173
178
            self.enable_keys = True
174
179
        elif val & CLIENT_CMD_BACKLIGHT:
175
 
            level = val - CLIENT_CMD_BACKLIGHT
176
 
            if level == 0:
177
 
                bl = 0
178
 
            elif level == 1:
179
 
                bl = self.plugin.default_lcd_brightness / 2
180
 
            elif level == 2:
181
 
                bl = self.plugin.default_lcd_brightness
182
 
            if self.backlight_acquire:                
 
180
            level = val - CLIENT_CMD_BACKLIGHT                        
 
181
            if isinstance(self.plugin.default_backlight, int):
 
182
                # Others
 
183
                bl = level
 
184
            else:       
 
185
                # G19
 
186
                if level == 0:
 
187
                    bl = 0
 
188
                elif level == 1:
 
189
                    bl = self.plugin.default_lcd_brightness / 2
 
190
                elif level == 2:
 
191
                    bl = self.plugin.default_lcd_brightness
 
192
            if self.backlight_acquire:      
 
193
                self.backlight_value = bl          
183
194
                self.backlight_acquire.set_value(bl)
184
195
            else:
185
196
                logger.warning("g15daemon client requested backlight be changed, but there is no backlight to change")
186
197
        elif val & CLIENT_CMD_KB_BACKLIGHT:
187
198
            level = val - CLIENT_CMD_KB_BACKLIGHT
188
 
            if level == 0:
189
 
                bl = (0, 0, 0)
190
 
            elif level == 1:
191
 
                bl = ( self.plugin.default_backlight[0] / 2, self.plugin.default_backlight[1] / 2, self.plugin.default_backlight[2] / 2 )
192
 
            elif level == 2:
193
 
                bl = self.plugin.default_backlight
 
199
            
 
200
            if isinstance(self.plugin.default_backlight, int):
 
201
                # Others
 
202
                bl = level
 
203
            else:            
 
204
                # G19
 
205
                if level == 0:
 
206
                    bl = (0, 0, 0)
 
207
                elif level == 1:
 
208
                    bl = ( self.plugin.default_backlight[0] / 2, self.plugin.default_backlight[1] / 2, self.plugin.default_backlight[2] / 2 )
 
209
                elif level == 2:
 
210
                    bl = self.plugin.default_backlight
194
211
                
195
 
            if self.keyboard_backlight_acquire:                
 
212
            if self.keyboard_backlight_acquire:    
 
213
                self.keyboard_backlight_value = bl                         
196
214
                self.keyboard_backlight_acquire.set_value(bl)
197
215
            else:
198
216
                logger.warning("g15daemon client requested keyboard backlight be changed, but there is no backlight to change")
223
241
            recv = self.recv(self.buffer_len - len(self.img_buffer))
224
242
            self.img_buffer += recv
225
243
            if len(self.img_buffer) == self.buffer_len:
226
 
                
227
244
                if self.buffer_type == "G":
228
245
                    self.img_buffer = self.convert_gbuf(self.img_buffer)
229
 
                    
230
 
                pil_img = Image.fromstring("1", (160,43), self.img_buffer)
231
 
                mask_img = pil_img.copy()                           
232
 
                mask_img = mask_img.convert("L")
233
 
                pil_img = pil_img.convert("P")
234
 
                if self.plugin.palette is not None:
235
 
                    pil_img.putpalette(self.plugin.palette)                              
236
 
                    pil_img = pil_img.convert("RGBA")                        
237
 
                    pil_img.putalpha(mask_img)                    
238
 
 
239
 
                # TODO find a quicker way of converting
240
 
                pixbuf = g15util.image_to_pixbuf(pil_img, "png")
241
 
                self.surface = g15util.pixbuf_to_surface(pixbuf)
242
 
                self.plugin.screen.redraw(self.page)
243
 
                
 
246
                self.draw_buffer(self.img_buffer)
 
247
                self.last_img_buffer = self.img_buffer
244
248
                self.img_buffer = ""
245
249
 
246
250
            elif len(self.img_buffer) > self.buffer_len:
247
251
                logger.warning('Received bad frame (%d bytes), should be %d' % ( len(self.img_buffer), self.buffer_len ) )
248
252
                
 
253
    def draw_buffer(self, img_buffer):
 
254
                
 
255
        pil_img = Image.fromstring("1", (160,43), img_buffer)
 
256
        mask_img = pil_img.copy()                           
 
257
        mask_img = mask_img.convert("L")
 
258
        pil_img = pil_img.convert("P")
 
259
        if self.plugin.palette is not None:
 
260
            pil_img.putpalette(self.plugin.palette)                              
 
261
            pil_img = pil_img.convert("RGBA")                        
 
262
            pil_img.putalpha(mask_img)                    
 
263
 
 
264
        # TODO find a quicker way of converting
 
265
        pixbuf = g15util.image_to_pixbuf(pil_img, "png")
 
266
        self.surface = g15util.pixbuf_to_surface(pixbuf)
 
267
        self.plugin.screen.redraw(self.page)
 
268
                
249
269
    def dump_buf(self, buf):
250
270
        i = 0
251
271
        for y in range(43):
319
339
    def _on_shown(self):
320
340
        self.backlight_control = self.plugin.screen.driver.get_control_for_hint(g15driver.HINT_DIMMABLE)
321
341
        if self.backlight_control:
322
 
            self.backlight_acquire = self.plugin.screen.driver.acquire_control(self.backlight_control, val = self.backlight_control.value)
 
342
            if self.backlight_value is None:
 
343
                 self.backlight_value = self.backlight_control.value
 
344
            self.backlight_acquire = self.plugin.screen.driver.acquire_control(self.backlight_control, val = self.backlight_value)
323
345
        self.keyboard_backlight_control = self.plugin.screen.driver.get_control_for_hint(g15driver.HINT_SHADEABLE)
324
346
        if self.keyboard_backlight_control:
325
 
            self.keyboard_backlight_acquire = self.plugin.screen.driver.acquire_control(self.keyboard_backlight_control, val = self.backlight_control.value)
 
347
            if self.keyboard_backlight_value is None:
 
348
                 self.keyboard_backlight_value = self.keyboard_backlight_control.value 
 
349
            self.keyboard_backlight_acquire = self.plugin.screen.driver.acquire_control(self.keyboard_backlight_control, val = self.keyboard_backlight_value)
326
350
        
327
351
    def _paint(self, canvas):
328
352
        if self.surface != None:
380
404
        pass
381
405
    
382
406
    def control_updated(self, control):
383
 
        self.load_configuration()
384
 
        self.screen.redraw()
 
407
        if control.id == "foreground":
 
408
            self.load_configuration()
 
409
            self.screen.redraw()
385
410
        
386
411
    def _get_port(self):
387
412
        port_entry = self.gconf_client.get(self.gconf_key + "/port")
399
424
            self.daemon = G15Daemon(port, self)
400
425
            self.async = G15Async()
401
426
            self.async.start()
 
427
        else:
 
428
            for c in self.clients:
 
429
                if c.last_img_buffer is not None:
 
430
                    c.draw_buffer(c.last_img_buffer)
402
431
            
403
432
    def _stop_all_clients(self):
404
433
        for c in self.clients:
405
434
            c.handle_close()
406
435
        
407
436
    def load_configuration(self):
408
 
        self.take_over_macro_keys = g15util.get_bool_or_default(self.gconf_client, "%s/take_over_macro_keys" % self.gconf_key, True) 
409
 
        foreground_control = self.screen.driver.get_control("foreground")
410
 
        if foreground_control is None:
411
 
            self.palette = None
412
 
        else:        
 
437
        self.take_over_macro_keys = g15util.get_bool_or_default(self.gconf_client, "%s/take_over_macro_keys" % self.gconf_key, True)
 
438
        
 
439
        if g15util.get_bool_or_default(self.gconf_client, "%s/use_custom_foreground" % self.gconf_key, False):
 
440
            col = g15util.get_rgb_or_default(self.gconf_client, "%s/custom_foreground" % self.gconf_key, (255,255,255))
413
441
            self.palette = [0 for n in range(768)]
414
 
            self.palette[765] = foreground_control.value[0]
415
 
            self.palette[766] = foreground_control.value[1]
416
 
            self.palette[767] = foreground_control.value[2]
 
442
            self.palette[765] = col[0]
 
443
            self.palette[766] = col[1]
 
444
            self.palette[767] = col[2]
 
445
        else: 
 
446
            foreground_control = self.screen.driver.get_control("foreground")
 
447
            if foreground_control is None:
 
448
                self.palette = None
 
449
            else:        
 
450
                self.palette = [0 for n in range(768)]
 
451
                self.palette[765] = foreground_control.value[0]
 
452
                self.palette[766] = foreground_control.value[1]
 
453
                self.palette[767] = foreground_control.value[2]
417
454
        
418
455
        backlight_control = self.screen.driver.get_control_for_hint(g15driver.HINT_DIMMABLE)
419
456
        self.default_backlight = backlight_control.value if backlight_control is not None else None 
420
457
        
421
 
        lcd_brightness_control = self.screen.driver.get_control_for_hint(g15driver.HINT_DIMMABLE)
 
458
        lcd_brightness_control = self.screen.driver.get_control_for_hint(g15driver.HINT_SHADEABLE)
422
459
        self.default_lcd_brightness = lcd_brightness_control.value if lcd_brightness_control is not None else None
423
460
    
424
461
    def leave(self, client):