Debian Codegraph @

python/examples/event.py

raw
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # event event lister for libcaca
5 # Copyright (c) 2010 Alex Foulon <[email protected]>
6 #
7 # This file is a Python port of "examples/event.c"
8 # which is:
9 # Copyright (c) 2004-2010 Sam Hocevar <[email protected]>
10 # All Rights Reserverd
11 #
12 # This library is free software. It comes without any warranty, to
13 # the extent permitted by applicable law. You can redistribute it
14 # and/or modify it under the terms of the Do What the Fuck You Want
15 # to Public License, Version 2, as published by Sam Hocevar. See
16 # http://www.wtfpl.net/ for more details.
17 #
18
19 import sys
20
21 import caca
22 from caca.canvas import Canvas, CanvasError
23 from caca.display import Display, DisplayError, Event
24
25 def print_event(cv, x, y, event):
26 """ Print event name.
27
28 :cv: libcaca canvas
29 :x: X coordinate
30 :y: Y coordinate
31 :event: event to display
32 """
33
34 if event.get_type() == caca.EVENT_NONE:
35 cv.put_str(x, y, "CACA_EVENT_NONE")
36 elif event.get_type() == caca.EVENT_KEY_PRESS:
37 character = event.get_key_ch()
38 if character > 0x1f and character < 0x80:
39 cv.put_str(x, y, "CACA_EVENT_KEY_PRESS 0x%02x (%s)" % (character, chr(character)))
40 else:
41 cv.put_str(x, y, "CACA_EVENT_KEY_PRESS 0x%02x (%s)" % (character, '?'))
42 elif event.get_type() == caca.EVENT_KEY_RELEASE:
43 character = event.get_key_ch()
44 if character > 0x1f and character < 0x80:
45 cv.put_str(x, y, "CACA_EVENT_KEY_RELEASE 0x%02x (%s)" % (character, chr(character)))
46 else:
47 cv.put_str(x, y, "CACA_EVENT_KEY_RELEASE 0x%02x (%s)" % (character, '?'))
48 elif event.get_type() == caca.EVENT_MOUSE_MOTION:
49 cv.put_str(x, y, "CACA_EVENT_MOUSE_MOTION %u %u" % (event.get_mouse_x(),
50 event.get_mouse_y()))
51 elif event.get_type() == caca.EVENT_MOUSE_PRESS:
52 cv.put_str(x, y, "CACA_EVENT_MOUSE_PRESS %u" % (event.get_mouse_button()))
53 elif event.get_type() == caca.EVENT_MOUSE_RELEASE:
54 cv.put_str(x, y, "CACA_EVENT_MOUSE_RELEASE %u" % (event.get_mouse_button()))
55 elif event.get_type() == caca.EVENT_RESIZE:
56 cv.put_str(x, y, "CACA_EVENT_RESIZE %u %u" % (event.get_resize_width(),
57 event.get_resize_height()))
58 elif event.get_type() == caca.EVENT_QUIT:
59 cv.put_str(x, y, "CACA_EVENT_QUIT")
60 else:
61 cv.put_str(x, y, "CACA_EVENT_UNKNOWN")
62
63 def main():
64 """ Main function. """
65
66 events = []
67 quit = 0
68 quit_string = ["", "q", "qu", "qui", "quit"]
69
70 try:
71 cv = Canvas(80, 24)
72 dp = Display(cv)
73 except (CanvasError, DisplayError) as err:
74 sys.stderr.write("%s\n" % err)
75 sys.exit(127)
76
77 h = cv.get_height() - 1
78
79 cv.set_color_ansi(caca.COLOR_WHITE, caca.COLOR_BLUE)
80 cv.draw_line(0, 0, cv.get_width() - 1, 0, ' ')
81 cv.draw_line(0, h, cv.get_width() - 1, h, ' ')
82 cv.put_str(0, h, "Type \"quit\" to exit")
83
84 dp.refresh()
85
86 while quit < 4:
87 ev = Event()
88 if dp.get_event(caca.EVENT_ANY, ev, -1):
89 if ev.get_type() == caca.EVENT_KEY_PRESS:
90 key = ev.get_key_ch()
91 if key == ord('u') and quit == 1:
92 quit += 1
93 elif key == ord('i') and quit == 2:
94 quit += 1
95 elif key == ord('t') and quit == 3:
96 quit += 1
97 elif key == ord('q'):
98 quit = 1
99 else:
100 quit = 0
101
102 events.append(ev)
103
104 cv.set_color_ansi(caca.COLOR_LIGHTGRAY, caca.COLOR_BLACK)
105 cv.clear()
106
107 #print current event
108 cv.set_color_ansi(caca.COLOR_WHITE, caca.COLOR_BLUE)
109 cv.draw_line(0, 0, cv.get_width() - 1, 0, ' ')
110 if events:
111 print_event(cv, 0, 0, events[-1])
112
113 cv.draw_line(0, h, cv.get_width() - 1, h, ' ')
114 cv.put_str(0, h, "Type \"quit\" to exit: %s" % quit_string[quit])
115
116 #print previous events
117 cv.set_color_ansi(caca.COLOR_WHITE, caca.COLOR_BLACK)
118 counts = list(range(0, len(events)-1))
119 counts.reverse()
120 if len(events) > 1:
121 j = 0
122 for i in counts:
123 if j < h - 1 and events[i].get_type():
124 print_event(cv, 0, ((len(events) - 1) - i), events[i])
125 j += 1
126
127 dp.refresh()
128
129 if __name__ == "__main__":
130 main()
131