pcremote-client-n8x0 -> client sources
[remotepc] / pcremote-client-n8x0-60 / debian / pcremote-client / usr / share / pcremote-client / edje_objects.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 # ****************************************************************************
5 # Copyright (c) 2008 INdT/Fucapi.
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU Lesser General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10
11 #  This program is distributed in the hope that it will be useful,
12 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #  GNU Lesser General Public License for more details.
15
16 #  You should have received a copy of the GNU Lesser General Public License
17 #   along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19 # ============================================================================
20 # Project Name :PC Remote
21 # Author       :André Portela
22 # Email        :andre_portela_@hotmail.com
23 # Version      :1.0
24 # Module       :main
25 # Class        :PCRemote custom Edje object with it's own call backs for the
26 #               main screen
27 # ============================================================================
28
29 import thread
30 import ecore
31 import ecore.evas
32 import evas.decorators
33 import edje
34 import edje.decorators
35 import time
36 from connection.iconnection import Iconnection
37 from kineticlist import *
38
39 class EvasCanvas(object):
40
41     def __init__(self, fullscreen, engine, size):
42         #f = ecore.evas.SoftwareX11
43         self.evas_obj = engine(w=size[0], h=size[1])
44         self.evas_obj.callback_delete_request = self.on_delete_request
45         self.evas_obj.callback_resize = self.on_resize
46
47         self.evas_obj.title = "PCRemote"
48         self.evas_obj.name_class = ('PC Remote', 'main')
49         self.evas_obj.fullscreen = fullscreen
50         self.evas_obj.size = size
51         self.evas_obj.show()
52         
53     def on_resize(self, evas_obj):
54         x, y, w, h = evas_obj.evas.viewport
55         size = (w, h)
56         for key in evas_obj.data.keys():
57             evas_obj.data[key].size = size
58
59     def on_delete_request(self, evas_obj):
60         ecore.main_loop_quit()
61         
62     def show(self):
63         self.evas_obj.show()
64
65 class EdjeObject(edje.Edje):
66
67     def __init__(self, canvas_class, file, group='main',name='edje'):
68         self.canvas_class = canvas_class
69         self.x11 = canvas_class.evas_obj
70         self.canvas = self.x11.evas
71         edje.Edje.__init__(self, self.canvas, file = file, group = group)
72         self.size = self.canvas.size
73         self.x11.data[name] = self
74
75 class MainScreen(EdjeObject):
76
77     def __init__(self, canvas, file, group, name, connection):
78         EdjeObject.__init__(self, canvas, file = file, group = group,name = name)
79
80         self.file = file
81         self.on_key_down_add(self.key_down_cb, self.x11)
82         self.sock_address = None
83         #flag that sync the discovery device's thread
84         self.flag = False
85         #flag that sync the connecting device's thread
86         self.connecting_flag = False
87         #lista de dispositivos descobertos
88         self.lista_dispositivos = []
89         #objeto que cria a conexao bluetooth
90         self.conexao = connection
91         self.kineticlist = False
92         #portela mock object
93         self.lista_teste = ['Andre Portela', 'Juliana Dias', 'Victor Hugo', 'Lucina Dias', 'Rosa Dias', 'James Italiano', 'Nona Izvi', 'Fergus Mao', 'Mauricio Brilhante', 'Edward Ordonez', 'Brankinhu', 'Banco Real', 'Banco Itaú', 'ABN-AMRO BANK']
94
95     def key_down_cb(self, bg, event, ee):
96         k = event.key
97         if k == "Escape":
98             ecore.main_loop_quit()
99         elif k == "F6":
100             ee.fullscreen = not ee.fullscreen
101         elif k == "F8":
102             print "-"
103         elif k == "F7":
104             print "+"
105       
106     def mkKineticList(self):
107         #kinetic list (the item values are tied with the part "item_background" of the "list_item" group)
108         #self.kineticlist = KineticList(self.canvas, file=self.file, item_width=407, item_height=38, father=self)
109         self.kineticlist = KineticList(self.canvas, file=self.file, item_height=57, father=self)
110         self.kineticlist.freeze()
111         #portela - test kinetic list with several devices
112         #for item in self.lista_teste:
113         #populates the list with the device's names
114         for item in self.lista_dispositivos:
115             self.kineticlist.row_add(item)
116         #reorganize and draw the list
117         self.kineticlist.thaw()
118         #embed the list in the edje object
119         self.part_swallow("list", self.kineticlist);
120
121     @edje.decorators.signal_callback("connect_to","choice")
122     def connect_to(self, emission, source):
123         self.sock_address = self.part_text_get(source)
124         #flag that sync the connecting device's thread
125         self.connecting_flag = False
126         self.signal_emit("start","device_connect")
127         thread.start_new_thread(MainScreen.threaded_connection,(self,))
128         
129     def threaded_connection(self):
130         self.conexao.create_socket('l2cap')
131         print 'connecting to: %s' % self.sock_address
132         self.conexao.set_address(self.conexao.bt_find_device_address_by_name(self.sock_address))
133         self.conexao.set_port(0x1001)
134         self.conexao.connect()
135         self.connecting_flag = True
136
137     @edje.decorators.signal_callback("connecting","device")
138     def connecting_check(self, emission, source):
139         if self.connecting_flag:
140             self.connecting_flag = False
141             self.signal_emit("stop","device_connect")
142             #we are sending a signal to main edje (there is time to animate the device locking)
143             self.signal_emit("begin","init")
144  
145     @edje.decorators.signal_callback("animation_still_loading", "loading")
146     def still_loading_cb(self, emission, source):
147         if self.flag:
148             self.flag = False
149             self.signal_emit("program,stop","loading")
150             if self.lista_dispositivos != []:
151                 self.mkKineticList()
152             else:
153                 self.no_device_found()
154
155     @edje.decorators.signal_callback("animation_sair_ended", "sair")
156     def saida(self, signal, source):
157         ecore.main_loop_quit()
158
159     @edje.decorators.signal_callback("animation_rastrear_ended", "rastrear")
160     def rastrear_key_down(self, signal, source):
161         thread.start_new_thread(MainScreen.rastrear_dispositivos,(self,None))
162
163     @edje.decorators.signal_callback("program,start", "novodevice")
164     def search_devices_again(self, signal, source):
165         self.part_unswallow(self.kineticlist)
166         del self.kineticlist
167         MainScreen.rastrear_key_down(self, None, None)
168
169     def rastrear_dispositivos(self,arg):
170         try:
171             self.lista_dispositivos = self.conexao.bt_find_devices_only_names()
172         except:
173             self.lista_dispositivos = []
174         self.flag = True
175
176     def no_device_found(self):
177         self.signal_emit("program,start","no_device")
178
179 class TabletScreen(EdjeObject):
180
181     def __init__(self, canvas, file, group,name, connection):
182         EdjeObject.__init__(self, canvas, file = file, group = group,name = name)
183         self.on_key_down_add(self.key_down_cb, self.x11)
184         #emitt events only if the mouse is inside the touch object area
185         (self.part_object_get('touch')).pointer_mode_set(evas.EVAS_OBJECT_POINTER_MODE_NOGRAB)
186         #assign the mouse_move_cb method as a mouse move callback for the touch part of the edje object
187         (self.part_object_get('touch')).on_mouse_move_add(self.mouse_move_cb)
188         #self.on_mouse_move_add(TabletScreen.mouse_move_cb)
189         self.sock = connection
190         #this flag indicates either the user are grabing something or not on the target
191         self.drag_flag = False
192         #this float indicates the wich the method left_click_down was called, and will be
193         #calculated against lcu_time in left_click_up method
194         self.lcd_time = 0.0
195         self.key_flag = False
196
197     @edje.decorators.signal_callback('mouse,down,1', 'tablet_bt-L_area')
198     def left_click_down(self, signal, source):
199         self.lcd_time = time.time()
200         #if the user are grabing something, release it
201         if self.drag_flag:
202             self.drag_flag = False
203             self.sock.send_message("Mouse:#left_click")
204
205     @edje.decorators.signal_callback('mouse,up,1', 'tablet_bt-L_area')
206     def left_click_up(self, signal, source):
207         lcu_time = time.time()
208         #threshold of 0.5 seconds to grab something on the target
209         elapsed_time = lcu_time - self.lcd_time
210         if elapsed_time < 0.5:
211             #do a simple click
212             self.sock.send_message("Mouse:#left_click")
213             self.sock.send_message("Mouse:#left_click")
214         else:
215             #do mouse grab
216             self.sock.send_message("Mouse:#left_click")
217             self.drag_flag = True
218
219     @edje.decorators.signal_callback('mouse,up,1', 'tablet_bt-R_area')
220     def rigth_click(self, signal, source):
221         self.sock.send_message("Mouse:#right_click")
222         self.sock.send_message("Mouse:#right_click")
223
224     def key_down_cb(self, bg, event, ee):
225         k = event.key
226         print k
227         if k == "Escape":
228             self.sock.send_message("Tablet:#stop")
229             self.sock.close()
230             ecore.main_loop_quit()
231         elif k == "F6":
232             ee.fullscreen = not ee.fullscreen
233         elif k == "F8":
234             self.sock.send_message("Keyboard:Up")
235         elif k == "F7":
236             self.sock.send_message("Keyboard:Down")
237         elif k == "Return":
238             self.sock.send_message("Keyboard:Alt+F1")
239         elif k == "ISO_Level3_Shift":
240             self.sock.send_message("Keyboard:Alt+F2")
241         else:
242             self.sock.send_message("Keyboard:%s" % k)
243
244     def mouse_move_cb(self, part, event):
245         x, y = event.position.output[0], event.position.output[1]
246         self.sock.send_message("Mouse:"+str(x)+","+str(y))
247
248 class SlideScreen(EdjeObject):
249
250     def __init__(self, canvas, file, group,name, connection):
251         EdjeObject.__init__(self, canvas, file = file, group = group,name = name)
252         self.on_key_down_add(self.key_down_cb, self.x11)
253         #emitt events only if the mouse is inside the touch object area
254         (self.part_object_get('touch')).pointer_mode_set(evas.EVAS_OBJECT_POINTER_MODE_NOGRAB)
255         #assign the mouse_move_cb method as a mouse move callback for the touch part of the edje object
256         (self.part_object_get('touch')).on_mouse_move_add(self.mouse_move_cb)
257         #self.on_mouse_move_add(TabletScreen.mouse_move_cb)
258         self.sock = connection
259         #this flag indicates either the user are grabing something or not on the target
260         self.drag_flag = False
261         #helps to coordenate presentation
262         self.keyboard_flag = True
263         #this float indicates the wich the method left_click_down was called, and will be
264         #calculated against lcu_time in left_click_up method
265         self.lcd_time = 0.0
266
267     @edje.decorators.signal_callback('mouse,down,1', 'slide_bt-left_area')
268     def left_click_down(self, signal, source):
269         if self.keyboard_flag:
270             self.lcd_time = time.time()
271             #if the user are grabing something, release it
272             if self.drag_flag:
273                 self.drag_flag = False
274                 self.sock.send_message("Mouse:#left_click")
275
276     @edje.decorators.signal_callback('mouse,up,1', 'slide_bt-left_area')
277     def left_click_up(self, signal, source):
278         if self.keyboard_flag:
279             lcu_time = time.time()
280             #threshold of 0.5 seconds to grab something on the target
281             elapsed_time = lcu_time - self.lcd_time
282             if elapsed_time < 0.5:
283                 #do a simple click
284                 self.sock.send_message("Mouse:#left_click")
285                 self.sock.send_message("Mouse:#left_click")
286             else:
287                 #do mouse grab
288                 self.sock.send_message("Mouse:#left_click")
289                 self.drag_flag = True
290         else:
291             self.sock.send_message("Keyboard:%s" % "Left")
292
293     @edje.decorators.signal_callback('mouse,up,1', 'slide_bt-right_area')
294     def rigth_click(self, signal, source):
295         if self.keyboard_flag:
296             self.sock.send_message("Mouse:#right_click")
297             self.sock.send_message("Mouse:#right_click")
298         else:
299             self.sock.send_message("Keyboard:%s" % "Right")
300
301     def key_down_cb(self, bg, event, ee):
302         k = event.key
303         print k
304         if k == "Escape":
305             self.sock.send_message("Slideshow:#stop")
306             self.sock.close()
307             ecore.main_loop_quit()
308         elif k == "F6":
309             self.keyboard_flag = not self.keyboard_flag
310             self.sock.send_message("Keyboard:F5")
311         elif k == "F8":
312             self.sock.send_message("Keyboard:Up")
313         elif k == "F7":
314             self.sock.send_message("Keyboard:Down")
315         else:
316             self.sock.send_message("Keyboard:%s" % k)
317
318     def mouse_move_cb(self, part, event):
319         x, y = event.position.output[0], event.position.output[1]
320         self.sock.send_message("Mouse:"+str(x)+","+str(y))