4b160120a0bc3752ee80bc28d7952eb826fd3689
[remotepc] / pcremote-server-desktop / debian / pcremote-server / usr / share / pcremote-server / services / ServerHandlers.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
21 # Email        : fergus.mao@gmail.com
22 # Reviewer     : Jônatas Isvi
23 # Email        : jonatas.nona@gmail.com
24 # Version      : 1.0
25 # Package      : service
26 # Description  : Singleton, Mouse and Keyboard
27 # ============================================================================
28
29 import Xlib
30 from Xlib import display, X, XK
31
32 class Singleton_Xlib():
33
34     """ Singleton
35     defines a singleton. 
36     """
37     def __init__(self):
38         self.display = display.Display()
39         self.screen = self.display.screen()
40
41 xlib_srv = Singleton_Xlib()
42
43 class Mouse(object):
44
45     """ Mouse
46     pass mouse information to Xorg
47     """
48
49     #Initialize the class
50     def __init__(self):
51         self.disp = xlib_srv.display
52         self.screen = xlib_srv.screen
53         self.fator = 10
54         self.lbutton = False
55         self.mbutton = False
56         self.rbutton = False
57         self.buttons = []
58
59     #Set the mouse pointer position
60     def position(self,x=None,y=None):            
61         
62         if (x == None):
63             x = self.fator
64             
65         if (y == None):
66             y = self.fator
67
68         #Get the current mouse pointer position
69         current_x = self.screen.root.query_pointer()._data["root_x"]
70         current_y = self.screen.root.query_pointer()._data["root_y"]
71         
72         def absolute(ax = None, ay = None):
73             if (ax == None):
74                 ax = x
75             if (ay == None):
76                 ay = y
77
78             self.screen.root.warp_pointer(ax, ay)
79             self.disp.sync()
80
81         def relative():
82             rX = current_x + x
83             rY = current_y + y
84             absolute(rX,rY)
85
86         relative()
87
88     #Returns the current X position
89     def get_x(self):
90         return self.screen.root.query_pointer()._data["root_x"]
91
92     #Returns the current Y position
93     def get_y(self):
94         return self.screen.root.query_pointer()._data["root_y"]
95
96     #Defines the factor(px) of the mouse pointer move
97     def set_fator(self,fator):
98         self.fator = fator
99
100     #Returns the factor
101     def get_fator(self):
102         return self.fator
103         
104     #Mouse Left Click 
105     def left_click(self, fg_lbutton = None):
106
107         if (fg_lbutton != None):
108             self.lbutton = not fg_lbutton
109
110         if not self.lbutton:
111             self.disp.xtest_fake_input(X.ButtonPress, 1, 0)
112             self.buttons.append('left_button')
113             self.lbutton = True
114         else:
115             self.disp.xtest_fake_input(X.ButtonRelease, 1, 5)
116             self.buttons.remove('left_button')
117             self.lbutton = False
118
119         self.disp.flush()
120         
121     #Mouse Middle Click 
122     def middle_click(self):
123         if not self.mbutton:    
124             self.disp.xtest_fake_input(X.ButtonPress, 2, 0)
125             self.buttons.append('middle_button')
126             self.mbutton = True
127         else:
128             self.disp.xtest_fake_input(X.ButtonRelease, 2, 5)
129             self.buttons.remove('middle_button')
130             self.mbutton = False
131
132         self.disp.flush()
133
134     #Mouse Right Click 
135     def right_click(self, fg_rbutton = None):
136
137         if (fg_rbutton != None):
138             self.rbutton = not fg_rbutton
139
140         if not self.rbutton:
141             self.disp.xtest_fake_input(X.ButtonPress, 3, 0)
142             self.buttons.append('right_button')
143             self.rbutton = True
144         else:
145             self.disp.xtest_fake_input(X.ButtonRelease, 3, 5)
146             self.buttons.remove('right_button')
147             self.rbutton = False
148
149         self.disp.flush()
150
151     def clean_up(self):
152         if self.buttons:
153             while self.buttons:
154                button = self.buttons.pop()
155                if button == 'left_button':
156                    self.disp.xtest_fake_input(X.ButtonRelease, 1, 5)
157                    self.disp.xtest_fake_input(X.ButtonPress, 3, 5)
158                    self.disp.xtest_fake_input(X.ButtonRelease, 3, 5)
159                elif button == 'middle_button':
160                    self.disp.xtest_fake_input(X.ButtonRelease, 2, 5)
161                elif button == 'right_button':
162                    self.disp.xtest_fake_input(X.ButtonRelease, 3, 5)
163
164         print self.buttons
165
166 class Keyboard():
167
168     """ Keyboard
169     pass keyboard information to Xorg 
170     """
171
172     def __init__(self):
173         self.display = xlib_srv.display
174         self.screen = xlib_srv.screen
175         self.keys = []
176
177     # encode key 
178     def __key_to_code(self,key):
179         new_key = getattr(XK, "XK_" + key)
180         code = self.display.keysym_to_keycode(new_key)
181         return code
182
183     # reproduce key pressed
184     def reproduce_key_press(self, key):
185         Xlib.ext.xtest.fake_input(self.display, Xlib.X.KeyPress, self.__key_to_code(key))
186         self.display.sync()
187         self.keys.append(key)
188
189     # reproduce key release
190     def reproduce_key_release(self, key):
191         Xlib.ext.xtest.fake_input(self.display, Xlib.X.KeyRelease, self.__key_to_code(key))
192         self.display.sync()
193         self.keys.remove(key)
194
195     # clean all pressed keys
196     def clean_up(self):
197         if self.keys:
198             while self.keys:
199                 key = self.keys.pop()
200                 self.reproduce_key_release(key)
201