352d1e7bdc8c9f12b7f3cd0b7d4f45d60ba665f5
[remotepc] / pcremote-server-desktop-60 / services / .svn / text-base / ServerHandlers.py.svn-base
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_mouse(self):
152         list = self.buttons
153         print list      
154         for i in list:
155                 if i == '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                         self.buttons.remove(i)
160                         print "\nleft_button -> release."
161                 elif i == 'middle_button':
162                         self.disp.xtest_fake_input(X.ButtonRelease, 2, 5)
163                         self.buttons.remove(i)
164                         print "\nmiddle_button -> release."
165                 elif i == 'right_button':
166                         self.disp.xtest_fake_input(X.ButtonRelease, 3, 5)
167                         self.buttons.remove(i)
168                         print "\nright_button -> release."
169
170         print self.buttons
171
172 class Keyboard():
173
174     """ Keyboard
175     pass keyboard information to Xorg 
176     """
177
178     def __init__(self):
179         self.display = xlib_srv.display
180         self.screen = xlib_srv.screen
181
182     # encode key 
183     def __key_to_code(self,key):
184         new_key = getattr(XK, "XK_" + key)
185         code = self.display.keysym_to_keycode(new_key)
186         return code
187
188     # reproduce key pressed
189     def reproduce_key_press(self, key):
190         Xlib.ext.xtest.fake_input(self.display, Xlib.X.KeyPress, self.__key_to_code(key))
191         self.display.sync()
192
193     # reproduce key release
194     def reproduce_key_release(self, key):
195         Xlib.ext.xtest.fake_input(self.display, Xlib.X.KeyRelease, self.__key_to_code(key))
196         self.display.sync()
197