3
# examples/record_demo.py -- demonstrate record extension
5
# Copyright (C) 2006 Alex Badea <vamposdecampos@gmail.com>
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.
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.
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
21
# Simple demo for the RECORD extension
22
# Not very much unlike the xmacrorec2 program in the xmacro package.
27
# Change path so we find Xlib
28
sys.path.insert(1, os.path.join(sys.path[0], '..'))
30
from Xlib import X, XK, display
31
from Xlib.ext import record
32
from Xlib.protocol import rq
34
local_dpy = display.Display()
35
record_dpy = display.Display()
37
def lookup_keysym(keysym):
39
if name[:3] == "XK_" and getattr(XK, name) == keysym:
41
return "[%d]" % keysym
43
def record_callback(reply):
44
if reply.category != record.FromServer:
46
if reply.client_swapped:
47
print "* received swapped protocol data, cowardly ignored"
49
if not len(reply.data) or ord(reply.data[0]) < 2:
55
event, data = rq.EventField(None).parse_binary_value(data, record_dpy.display, None, None)
57
if event.type in [X.KeyPress, X.KeyRelease]:
58
pr = event.type == X.KeyPress and "Press" or "Release"
60
keysym = local_dpy.keycode_to_keysym(event.detail, 0)
62
print "KeyCode%s" % pr, event.detail
64
print "KeyStr%s" % pr, lookup_keysym(keysym)
66
if event.type == X.KeyPress and keysym == XK.XK_Escape:
67
local_dpy.record_disable_context(ctx)
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
78
# Check if the extension is present
79
if not record_dpy.has_extension("RECORD"):
80
print "RECORD extension not found"
82
r = record_dpy.record_get_version(0, 0)
83
print "RECORD extension version %d.%d" % (r.major_version, r.minor_version)
85
# Create a recording context; we only want key and mouse events
86
ctx = record_dpy.record_create_context(
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),
97
'client_started': False,
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)
105
# Finally free the context
106
record_dpy.record_free_context(ctx)