1
# -*- coding: iso-8859-1 -*-
3
# Copyright 2010 Pepijn de Vos
5
# Licensed under the Apache License, Version 2.0 (the "License");
6
# you may not use this file except in compliance with the License.
7
# You may obtain a copy of the License at
9
# http://www.apache.org/licenses/LICENSE-2.0
11
# Unless required by applicable law or agreed to in writing, software
12
# distributed under the License is distributed on an "AS IS" BASIS,
13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
# See the License for the specific language governing permissions and
15
# limitations under the License.
17
from Xlib.display import Display
19
from Xlib.ext.xtest import fake_input
20
from Xlib.ext import record
21
from Xlib.protocol import rq
23
from base import PyMouseMeta, PyMouseEventMeta
28
class PyMouse(PyMouseMeta):
30
def press(self, x, y, button = 1):
32
fake_input(display, X.ButtonPress, [None, 1, 3, 2, 4, 5][button])
35
def release(self, x, y, button = 1):
37
fake_input(display, X.ButtonRelease, [None, 1, 3, 2, 4, 5][button])
41
fake_input(display, X.MotionNotify, x=x, y=y)
45
coord = display.screen().root.query_pointer()._data
46
return coord["root_x"], coord["root_y"]
48
def screen_size(self):
49
width = display.screen().width_in_pixels
50
height = display.screen().height_in_pixels
53
class PyMouseEvent(PyMouseEventMeta):
55
PyMouseEventMeta.__init__(self)
56
self.ctx = display2.record_create_context(
60
'core_requests': (0, 0),
61
'core_replies': (0, 0),
62
'ext_requests': (0, 0, 0, 0),
63
'ext_replies': (0, 0, 0, 0),
64
'delivered_events': (0, 0),
65
'device_events': (X.ButtonPressMask, X.ButtonReleaseMask),
67
'client_started': False,
73
display2.screen().root.grab_pointer(True, X.ButtonPressMask | X.ButtonReleaseMask, X.GrabModeAsync, X.GrabModeAsync, 0, 0, X.CurrentTime)
75
display2.record_enable_context(self.ctx, self.handler)
76
display2.record_free_context(self.ctx)
79
display.record_disable_context(self.ctx)
80
display.ungrab_pointer(X.CurrentTime)
82
display2.record_disable_context(self.ctx)
83
display2.ungrab_pointer(X.CurrentTime)
86
def handler(self, reply):
89
event, data = rq.EventField(None).parse_binary_value(data, display.display, None, None)
91
if event.type == X.ButtonPress:
92
self.click(event.root_x, event.root_y, (None, 1, 3, 2, 3, 3, 3)[event.detail], True)
93
elif event.type == X.ButtonRelease:
94
self.click(event.root_x, event.root_y, (None, 1, 3, 2, 3, 3, 3)[event.detail], False)
96
self.move(event.root_x, event.root_y)