pcremote-client-n8x0 -> client sources
[remotepc] / pcremote-server-desktop-60 / services / ObjectServers.py
1 # -*- coding: utf-8 -*-
2
3 # ****************************************************************************
4 # Copyright (c) 2008 INdT/Fucapi.
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Lesser General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #
18 # ============================================================================
19 # Project Name : PC Remote
20 # Author       : Nilson Silva, Jonatas Isvi, Andre Portela
21 # Email        : fergus.mao@gmail.com, jonatas.nona@gmail.com,
22 #                andre_portela_@hotmail.com
23 # Reviewer     : Jônatas Isvi
24 # Email        : 
25 # Version      : 1.0
26 # Package      : services
27 # Description  : Mouse Server, Keyboard Server
28 # ============================================================================
29
30 import time
31 from utils.labels import *
32 from ServerHandlers import *
33 from players import amarok
34
35 class Mouse_Server:
36
37     """ Mouse Server
38     Defines all mouse behaviors.
39     Clicks and coordinates.
40     """
41
42     #Initialize the class
43     def __init__(self, service):
44         self._service_name = service
45         self.mouse  = Mouse()
46         self.labels = Labels()
47         self.timer  = time
48         self.timerclick = 0
49         
50         self.fg_dbclick = False
51         self.fg_move = True
52         self.x = 0      
53         self.y = 0
54
55     #Executes the action requested by the Service Manager
56     def execute(self, command):
57
58         self.mouse_counter_lclick()
59
60         if (command == self.labels.CLICK):
61             self.mouse_click()
62         elif (command == self.labels.DOUBLE_CLICK):
63             self.mouse_press_dbclick()
64         elif (command == self.labels.TRIPLE_CLICK):
65             self.mouse_release_dbclick()
66         elif (command == self.labels.LEFT_CLICK):
67             self.mouse_lclick()
68         elif (command == self.labels.MIDDLE_CLICK):
69             self.mouse_mclick()
70         elif (command == self.labels.RIGHT_CLICK):
71             self.mouse_rclick()
72         elif (command[0] == "#"):
73             self.mouse_fator(command)
74         else:
75             self.mouse_move(command)
76
77     #Gets the time the mouse pointer is pressed. If It is greater than (or equal to) 2s, The Mouse Left Click is activated.
78     def mouse_counter_lclick(self):
79                   
80         if ((not self.fg_move) and ((int(self.timer.time()) - self.timerclick) >= 2)):
81             self.mouse.right_click(True)
82             self.mouse.right_click(False)
83             self.timerclick = 0
84             self.fg_move = True
85
86     #Mouse Pointer - Single Click
87     def mouse_click(self):
88         self.timerclick = int(self.timer.time())
89         self.fg_move = False
90
91     #Mouse Pointer - Double Click
92     def mouse_press_dbclick(self):
93         self.mouse.left_click(True)
94         self.fg_dbclick = True
95
96     #Mouse Pointer - Released after a Double Click
97     def mouse_release_dbclick(self):
98         if self.fg_dbclick:
99             self.mouse.left_click(False)
100             self.fg_dbclick = False
101
102     #Mouse Left Click
103     def mouse_lclick(self):
104         self.mouse.left_click()
105
106     #Mouse Middle Click
107     def mouse_mclick(self):
108         self.mouse.middle_click()
109
110     #Mouse Right Click
111     def mouse_rclick(self):
112         self.mouse.right_click()
113
114     #Sets the factor of the Mouse Pointer Move
115     def mouse_fator(self, command):
116         num = ""
117         for i in range(1, len(command)):
118             num = num + command(i)
119
120         self.mouse.set_fator(int(num))
121
122     #Moves the Mouse Pointer
123     def mouse_move(self, command):
124         coord = command.split(",")
125
126         i = int(coord[0]) - self.x
127         if ((abs(i) == 1) or (abs(i) >= 20)):
128             i = 0
129
130         j = int(coord[1]) - self.y
131         if ((abs(j) == 1) or (abs(j) >= 20)):
132             j = 0
133
134         if not ((i == 0) and (j == 0)):            
135             if ((i >= 4) or (j >= 4)):
136                 self.fg_move = True
137             if self._service_name == "Tablet":
138                 self.mouse.position(i, j)
139             else:
140                 self.mouse.position(-j, i)
141
142         self.x = int(coord[0])
143         self.y = int(coord[1])
144
145     def clean_up(self):
146         self.mouse.clean_up()
147
148 class KeyBoard_Server:
149
150     """ Keyboard Server
151     Defines all keyboard behaviors.
152     Map keys and events.
153     """    
154
155     def __init__(self, service):
156         self.keyboard = Keyboard()
157         self.shift_flag = False
158         self.control_flag = False
159         self.alt_flag = False
160         self._service_name = service
161
162     # execute key command
163     def execute(self, command):
164         
165         print "\n", command
166
167         if(command == 'F8'):
168             self.keyboard.reproduce_key_press('Control_L')
169             self.keyboard.reproduce_key_press('z')
170             self.keyboard.reproduce_key_release('z')
171             self.keyboard.reproduce_key_release('Control_L')
172         elif(self._service_name == 'Slideshow' and command == 'F6'):
173             self.keyboard.reproduce_key_press('F5')
174             self.keyboard.reproduce_key_release('F5')
175         elif(command == 'Control_R'):
176             self.control_flag = True
177             self.keyboard.reproduce_key_press('Control_R')
178             #self.keys.append(command)
179         elif(command == 'Shift_L'):
180             self.shift_flag = True
181             self.keyboard.reproduce_key_press('Shift_L')
182             #self.keys.append(command)
183         elif(command == 'Alt_L'):
184             self.alt_flag = True
185             self.keyboard.reproduce_key_press('Alt_L')
186         elif(command == 'F7'):
187             self.keyboard.reproduce_key_press('Control_L')
188             self.keyboard.reproduce_key_press('y')
189             self.keyboard.reproduce_key_release('y')
190             self.keyboard.reproduce_key_release('Control_L')
191         elif(command == 'Alt+F1'):
192             self.keyboard.reproduce_key_press('Alt_L')
193             self.keyboard.reproduce_key_press('F1')
194             self.keyboard.reproduce_key_release('F1')
195             self.keyboard.reproduce_key_release('Alt_L')
196         elif(command == 'Alt+F2'):
197             self.keyboard.reproduce_key_press('Alt_L')
198             self.keyboard.reproduce_key_press('F2')
199             self.keyboard.reproduce_key_release('F2')
200             self.keyboard.reproduce_key_release('Alt_L')
201         elif(command == 'Alt+F4'):
202             self.keyboard.reproduce_key_press('Alt_L')
203             self.keyboard.reproduce_key_press('F4')
204             self.keyboard.reproduce_key_release('F4')
205             self.keyboard.reproduce_key_release('Alt_L')
206         elif(command == 'Alt+F9'):
207             self.keyboard.reproduce_key_press('Alt_L')
208             self.keyboard.reproduce_key_press('F9')
209             self.keyboard.reproduce_key_release('F9')
210             self.keyboard.reproduce_key_release('Alt_L')
211         elif(command == 'Alt+F0'):
212             self.keyboard.reproduce_key_press('Alt_L')
213             self.keyboard.reproduce_key_press('F10')
214             self.keyboard.reproduce_key_release('F10')
215             self.keyboard.reproduce_key_release('Alt_L')
216         elif(command == 'Alt+Space'):
217             self.keyboard.reproduce_key_press('Alt_L')
218             self.keyboard.reproduce_key_press('space')
219             self.keyboard.reproduce_key_release('space')
220             self.keyboard.reproduce_key_release('Alt_L')
221         elif(command == 'Tab'):
222             self.keyboard.reproduce_key_press('Tab')
223             self.keyboard.reproduce_key_release('Tab')
224         else:
225             self.keyboard.reproduce_key_press(command)
226             self.keyboard.reproduce_key_release(command)
227                         
228             if self.shift_flag:
229                 self.keyboard.reproduce_key_release('Shift_L')
230                 #self.keys.remove('Shift_L')
231                 self.shift_flag = False
232             elif self.control_flag:
233                 self.keyboard.reproduce_key_release('Control_R')
234                 #self.keys.remove('Control_R')
235                 self.control_flag = False
236             elif self.alt_flag:
237                 self.keyboard.reproduce_key_release('Alt_L')
238                 self.alt_flag = False
239         
240     # clean all keys pressed                    
241     def clean_up(self):
242         self.keyboard.clean_up()
243
244 class Player_Server():
245
246     def __init__(self):
247         if not amarok.isRunning():
248             self.player = amarok.AmarokPlayer()
249         else:
250             amarok.start()
251             self.player = amarok.AmarokPlayer()
252         self.labels = Labels()
253
254     def execute(self, command):
255         if len(command) > 2:
256             if command[1] == self.labels.PLAY:
257                 if command[2] and command[3]:
258                     self.player.play(track=int(command[2]), rmd=bool(command[3]))      
259                 elif command[2]:
260                     arg = int(command[2])
261                     if isinstance(arg, int):
262                         self.player.play(track=arg)
263                     else:
264                         arg = bool(command[2])
265                         self.player.play(rmd=arg)
266                 else:
267                     pass
268         else:
269             if command[1] == self.labels.STOP:
270                 self.player.stop()
271             elif command[1] == self.labels.PLAY:
272                 self.player.play()
273             elif command[1] == self.labels.PAUSE:
274                 self.player.pause()
275             elif command[1] == self.labels.NEXT:
276                 self.player.next()
277             elif command[1] == self.labels.PREVIOUS:
278                 self.player.prev()
279             elif command[1] == self.labels.VOL_UP:
280                 self.player.volume_up()
281             elif command[1] == self.labels.VOL_DOWN:
282                 self.player.volume_down()
283             elif command[1] == self.labels.SEEK:
284                 self.player.seek(int(command[2]))
285             elif command[1] == self.labels.DOWNLOAD:
286                 path = self.player.file_properties()
287                 addr = command[2]
288                 amarok.send_file(addr, path)
289                 pass
290             elif command[1] == self.labels.LOAD_PLAYLIST:
291                 # falta o metodo de trasnferencia
292                 pass
293             else:
294                 pass