~bennabiy/+junk/python-xlib

« back to all changes in this revision

Viewing changes to .pc/python3.patch/examples/record_demo.py

  • Committer: Package Import Robot
  • Author(s): Andrew Shadura, Ramkumar Ramachandra, Andrew Shadura
  • Date: 2015-08-13 08:14:19 UTC
  • mfrom: (6.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20150813081419-hdefinnghp2iydkx
Tags: 0.14+20091101-3
[ Ramkumar Ramachandra ]
* Remove useless debugging output (Closes: #565996)

[ Andrew Shadura ]
* Switch to 3.0 (quilt) format.
* Rename patches.
* Use debhelper 9 in its short form.
* Use pybuild.
* Bump Standards-Version.
* Don't build or install PostScript documentation and info files.
* Use system-provided texi2html instead of a shipped version
  (Closes: #795057).
* Update debian/copyright (Closes: #795057).
* Don't install Makefile or texi2html with the documentation.
* Set executable bit for examples.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
#
 
3
# examples/record_demo.py -- demonstrate record extension
 
4
#
 
5
#    Copyright (C) 2006 Alex Badea <vamposdecampos@gmail.com>
 
6
#
 
7
#    This program is free software; you can redistribute it and/or modify
 
8
#    it under the terms of the GNU General Public License as published by
 
9
#    the Free Software Foundation; either version 2 of the License, or
 
10
#    (at your option) any later version.
 
11
#
 
12
#    This program is distributed in the hope that it will be useful,
 
13
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
#    GNU General Public License for more details.
 
16
#
 
17
#    You should have received a copy of the GNU General Public License
 
18
#    along with this program; if not, write to the Free Software
 
19
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
20
 
 
21
# Simple demo for the RECORD extension
 
22
# Not very much unlike the xmacrorec2 program in the xmacro package.
 
23
 
 
24
import sys
 
25
import os
 
26
 
 
27
# Change path so we find Xlib
 
28
sys.path.insert(1, os.path.join(sys.path[0], '..'))
 
29
 
 
30
from Xlib import X, XK, display
 
31
from Xlib.ext import record
 
32
from Xlib.protocol import rq
 
33
 
 
34
local_dpy = display.Display()
 
35
record_dpy = display.Display()
 
36
 
 
37
def lookup_keysym(keysym):
 
38
    for name in dir(XK):
 
39
        if name[:3] == "XK_" and getattr(XK, name) == keysym:
 
40
            return name[3:]
 
41
    return "[%d]" % keysym
 
42
 
 
43
def record_callback(reply):
 
44
    if reply.category != record.FromServer:
 
45
        return
 
46
    if reply.client_swapped:
 
47
        print "* received swapped protocol data, cowardly ignored"
 
48
        return
 
49
    if not len(reply.data) or ord(reply.data[0]) < 2:
 
50
        # not an event
 
51
        return
 
52
 
 
53
    data = reply.data
 
54
    while len(data):
 
55
        event, data = rq.EventField(None).parse_binary_value(data, record_dpy.display, None, None)
 
56
 
 
57
        if event.type in [X.KeyPress, X.KeyRelease]:
 
58
            pr = event.type == X.KeyPress and "Press" or "Release"
 
59
 
 
60
            keysym = local_dpy.keycode_to_keysym(event.detail, 0)
 
61
            if not keysym:
 
62
                print "KeyCode%s" % pr, event.detail
 
63
            else:
 
64
                print "KeyStr%s" % pr, lookup_keysym(keysym)
 
65
 
 
66
            if event.type == X.KeyPress and keysym == XK.XK_Escape:
 
67
                local_dpy.record_disable_context(ctx)
 
68
                local_dpy.flush()
 
69
                return
 
70
        elif event.type == X.ButtonPress:
 
71
            print "ButtonPress", event.detail
 
72
        elif event.type == X.ButtonRelease:
 
73
            print "ButtonRelease", event.detail
 
74
        elif event.type == X.MotionNotify:
 
75
            print "MotionNotify", event.root_x, event.root_y
 
76
 
 
77
 
 
78
# Check if the extension is present
 
79
if not record_dpy.has_extension("RECORD"):
 
80
    print "RECORD extension not found"
 
81
    sys.exit(1)
 
82
r = record_dpy.record_get_version(0, 0)
 
83
print "RECORD extension version %d.%d" % (r.major_version, r.minor_version)
 
84
 
 
85
# Create a recording context; we only want key and mouse events
 
86
ctx = record_dpy.record_create_context(
 
87
        0,
 
88
        [record.AllClients],
 
89
        [{
 
90
                'core_requests': (0, 0),
 
91
                'core_replies': (0, 0),
 
92
                'ext_requests': (0, 0, 0, 0),
 
93
                'ext_replies': (0, 0, 0, 0),
 
94
                'delivered_events': (0, 0),
 
95
                'device_events': (X.KeyPress, X.MotionNotify),
 
96
                'errors': (0, 0),
 
97
                'client_started': False,
 
98
                'client_died': False,
 
99
        }])
 
100
 
 
101
# Enable the context; this only returns after a call to record_disable_context,
 
102
# while calling the callback function in the meantime
 
103
record_dpy.record_enable_context(ctx, record_callback)
 
104
 
 
105
# Finally free the context
 
106
record_dpy.record_free_context(ctx)