~ubuntu-branches/ubuntu/natty/mago/natty

« back to all changes in this revision

Viewing changes to mago/mouse/unix.py

  • Committer: Bazaar Package Importer
  • Author(s): Michael Vogt
  • Date: 2011-02-08 13:32:13 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20110208133213-m1og7ey0m990chg6
Tags: 0.3+bzr20-0ubuntu1
* debian/rules:
  - updated to debhelper 7
  - use dh_python2 instead of python-central
* debian/pycompat:
  - removed, no longer needed
* debian/control:
  - dropped cdbs and python-central dependencies
* bzr snapshot of the current trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: iso-8859-1 -*-
 
2
 
 
3
#   Copyright 2010 Pepijn de Vos
 
4
#
 
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
 
8
#
 
9
#       http://www.apache.org/licenses/LICENSE-2.0
 
10
#
 
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.
 
16
 
 
17
from Xlib.display import Display
 
18
from Xlib import X
 
19
from Xlib.ext.xtest import fake_input
 
20
from Xlib.ext import record
 
21
from Xlib.protocol import rq
 
22
 
 
23
from base import PyMouseMeta, PyMouseEventMeta
 
24
 
 
25
display = Display()
 
26
display2 = Display()
 
27
 
 
28
class PyMouse(PyMouseMeta):
 
29
 
 
30
    def press(self, x, y, button = 1):
 
31
        self.move(x, y)
 
32
        fake_input(display, X.ButtonPress, [None, 1, 3, 2, 4, 5][button])
 
33
        display.sync()
 
34
 
 
35
    def release(self, x, y, button = 1):
 
36
        self.move(x, y)
 
37
        fake_input(display, X.ButtonRelease, [None, 1, 3, 2, 4, 5][button])
 
38
        display.sync()
 
39
 
 
40
    def move(self, x, y):
 
41
        fake_input(display, X.MotionNotify, x=x, y=y)
 
42
        display.sync()
 
43
 
 
44
    def position(self):
 
45
        coord = display.screen().root.query_pointer()._data
 
46
        return coord["root_x"], coord["root_y"]
 
47
 
 
48
    def screen_size(self):
 
49
        width = display.screen().width_in_pixels
 
50
        height = display.screen().height_in_pixels
 
51
        return width, height
 
52
 
 
53
class PyMouseEvent(PyMouseEventMeta):
 
54
    def __init__(self):
 
55
        PyMouseEventMeta.__init__(self)
 
56
        self.ctx = display2.record_create_context(
 
57
            0,
 
58
            [record.AllClients],
 
59
            [{
 
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),
 
66
                    'errors': (0, 0),
 
67
                    'client_started': False,
 
68
                    'client_died': False,
 
69
            }])
 
70
 
 
71
    def run(self):
 
72
        if self.capture:
 
73
            display2.screen().root.grab_pointer(True, X.ButtonPressMask | X.ButtonReleaseMask, X.GrabModeAsync, X.GrabModeAsync, 0, 0, X.CurrentTime)
 
74
 
 
75
        display2.record_enable_context(self.ctx, self.handler)
 
76
        display2.record_free_context(self.ctx)
 
77
    
 
78
    def stop(self):
 
79
        display.record_disable_context(self.ctx)
 
80
        display.ungrab_pointer(X.CurrentTime)
 
81
        display.flush()
 
82
        display2.record_disable_context(self.ctx)
 
83
        display2.ungrab_pointer(X.CurrentTime)
 
84
        display2.flush()
 
85
 
 
86
    def handler(self, reply):
 
87
        data = reply.data
 
88
        while len(data):
 
89
            event, data = rq.EventField(None).parse_binary_value(data, display.display, None, None)
 
90
            
 
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)
 
95
            else:
 
96
                self.move(event.root_x, event.root_y)
 
97
            
 
98