1 # -*- coding: utf-8 -*-
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.
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.
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/>.
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
26 # Description : Singleton, Mouse and Keyboard
27 # ============================================================================
30 from Xlib import display, X, XK
32 class Singleton_Xlib():
38 self.display = display.Display()
39 self.screen = self.display.screen()
41 xlib_srv = Singleton_Xlib()
46 pass mouse information to Xorg
51 self.disp = xlib_srv.display
52 self.screen = xlib_srv.screen
59 #Set the mouse pointer position
60 def position(self,x=None,y=None):
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"]
72 def absolute(ax = None, ay = None):
78 self.screen.root.warp_pointer(ax, ay)
88 #Returns the current X position
90 return self.screen.root.query_pointer()._data["root_x"]
92 #Returns the current Y position
94 return self.screen.root.query_pointer()._data["root_y"]
96 #Defines the factor(px) of the mouse pointer move
97 def set_fator(self,fator):
105 def left_click(self, fg_lbutton = None):
107 if (fg_lbutton != None):
108 self.lbutton = not fg_lbutton
111 self.disp.xtest_fake_input(X.ButtonPress, 1, 0)
112 self.buttons.append('left_button')
115 self.disp.xtest_fake_input(X.ButtonRelease, 1, 5)
116 self.buttons.remove('left_button')
122 def middle_click(self):
124 self.disp.xtest_fake_input(X.ButtonPress, 2, 0)
125 self.buttons.append('middle_button')
128 self.disp.xtest_fake_input(X.ButtonRelease, 2, 5)
129 self.buttons.remove('middle_button')
135 def right_click(self, fg_rbutton = None):
137 if (fg_rbutton != None):
138 self.rbutton = not fg_rbutton
141 self.disp.xtest_fake_input(X.ButtonPress, 3, 0)
142 self.buttons.append('right_button')
145 self.disp.xtest_fake_input(X.ButtonRelease, 3, 5)
146 self.buttons.remove('right_button')
151 def clean_up_mouse(self):
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."
175 pass keyboard information to Xorg
179 self.display = xlib_srv.display
180 self.screen = xlib_srv.screen
183 def __key_to_code(self,key):
184 new_key = getattr(XK, "XK_" + key)
185 code = self.display.keysym_to_keycode(new_key)
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))
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))