# -*- coding: utf-8 -*- # **************************************************************************** # Copyright (c) 2008 INdT/Fucapi. # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with this program. If not, see . # # ============================================================================ # Project Name : PC Remote # Author : Nilson Silva # Email : fergus.mao@gmail.com # Reviewer : Jônatas Isvi # Email : jonatas.nona@gmail.com # Version : 1.0 # Package : service # Description : Singleton, Mouse and Keyboard # ============================================================================ import Xlib from Xlib import display, X, XK class Singleton_Xlib(): """ Singleton defines a singleton. """ def __init__(self): self.display = display.Display() self.screen = self.display.screen() xlib_srv = Singleton_Xlib() class Mouse(object): """ Mouse pass mouse information to Xorg """ #Initialize the class def __init__(self): self.disp = xlib_srv.display self.screen = xlib_srv.screen self.fator = 10 self.lbutton = False self.mbutton = False self.rbutton = False self.buttons = [] #Set the mouse pointer position def position(self,x=None,y=None): if (x == None): x = self.fator if (y == None): y = self.fator #Get the current mouse pointer position current_x = self.screen.root.query_pointer()._data["root_x"] current_y = self.screen.root.query_pointer()._data["root_y"] def absolute(ax = None, ay = None): if (ax == None): ax = x if (ay == None): ay = y self.screen.root.warp_pointer(ax, ay) self.disp.sync() def relative(): rX = current_x + x rY = current_y + y absolute(rX,rY) relative() #Returns the current X position def get_x(self): return self.screen.root.query_pointer()._data["root_x"] #Returns the current Y position def get_y(self): return self.screen.root.query_pointer()._data["root_y"] #Defines the factor(px) of the mouse pointer move def set_fator(self,fator): self.fator = fator #Returns the factor def get_fator(self): return self.fator #Mouse Left Click def left_click(self, fg_lbutton = None): if (fg_lbutton != None): self.lbutton = not fg_lbutton if not self.lbutton: self.disp.xtest_fake_input(X.ButtonPress, 1, 0) self.buttons.append('left_button') self.lbutton = True else: self.disp.xtest_fake_input(X.ButtonRelease, 1, 5) self.buttons.remove('left_button') self.lbutton = False self.disp.flush() #Mouse Middle Click def middle_click(self): if not self.mbutton: self.disp.xtest_fake_input(X.ButtonPress, 2, 0) self.buttons.append('middle_button') self.mbutton = True else: self.disp.xtest_fake_input(X.ButtonRelease, 2, 5) self.buttons.remove('middle_button') self.mbutton = False self.disp.flush() #Mouse Right Click def right_click(self, fg_rbutton = None): if (fg_rbutton != None): self.rbutton = not fg_rbutton if not self.rbutton: self.disp.xtest_fake_input(X.ButtonPress, 3, 0) self.buttons.append('right_button') self.rbutton = True else: self.disp.xtest_fake_input(X.ButtonRelease, 3, 5) self.buttons.remove('right_button') self.rbutton = False self.disp.flush() def clean_up_mouse(self): list = self.buttons print list for i in list: if i == 'left_button': self.disp.xtest_fake_input(X.ButtonRelease, 1, 5) self.disp.xtest_fake_input(X.ButtonPress, 3, 5) self.disp.xtest_fake_input(X.ButtonRelease, 3, 5) self.buttons.remove(i) print "\nleft_button -> release." elif i == 'middle_button': self.disp.xtest_fake_input(X.ButtonRelease, 2, 5) self.buttons.remove(i) print "\nmiddle_button -> release." elif i == 'right_button': self.disp.xtest_fake_input(X.ButtonRelease, 3, 5) self.buttons.remove(i) print "\nright_button -> release." print self.buttons class Keyboard(): """ Keyboard pass keyboard information to Xorg """ def __init__(self): self.display = xlib_srv.display self.screen = xlib_srv.screen # encode key def __key_to_code(self,key): new_key = getattr(XK, "XK_" + key) code = self.display.keysym_to_keycode(new_key) return code # reproduce key pressed def reproduce_key_press(self, key): Xlib.ext.xtest.fake_input(self.display, Xlib.X.KeyPress, self.__key_to_code(key)) self.display.sync() # reproduce key release def reproduce_key_release(self, key): Xlib.ext.xtest.fake_input(self.display, Xlib.X.KeyRelease, self.__key_to_code(key)) self.display.sync()