Browse code
feat: python xcb example
Ed Langley authored on 29/10/2020 23:41:55
Showing 1 changed files
Showing 1 changed files
1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,118 @@ |
1 |
+# Copyright (c) 2011 Edward Langley |
|
2 |
+# All rights reserved. |
|
3 |
+# |
|
4 |
+# Redistribution and use in source and binary forms, with or without |
|
5 |
+# modification, are permitted provided that the following conditions |
|
6 |
+# are met: |
|
7 |
+# |
|
8 |
+# Redistributions of source code must retain the above copyright notice, |
|
9 |
+# this list of conditions and the following disclaimer. |
|
10 |
+# |
|
11 |
+# Redistributions in binary form must reproduce the above copyright |
|
12 |
+# notice, this list of conditions and the following disclaimer in the |
|
13 |
+# documentation and/or other materials provided with the distribution. |
|
14 |
+# |
|
15 |
+# Neither the name of the project's author nor the names of its |
|
16 |
+# contributors may be used to endorse or promote products derived from |
|
17 |
+# this software without specific prior written permission. |
|
18 |
+# |
|
19 |
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
20 |
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
21 |
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
|
22 |
+# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
23 |
+# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
24 |
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
|
25 |
+# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
26 |
+# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
|
27 |
+# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
28 |
+# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
29 |
+# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
30 |
+ |
|
31 |
+import xcb |
|
32 |
+from xcb.xproto import * |
|
33 |
+import xcb.render |
|
34 |
+ |
|
35 |
+def find_format(screen): |
|
36 |
+ for d in screen.depths: |
|
37 |
+ if d.depth == depth: |
|
38 |
+ for v in d.visuals: |
|
39 |
+ if v.visual == visual: |
|
40 |
+ return v.format |
|
41 |
+ |
|
42 |
+ raise Exception("Failed to find an appropriate Render pictformat!") |
|
43 |
+ |
|
44 |
+ |
|
45 |
+def startup(): |
|
46 |
+ white = setup.roots[0].white_pixel |
|
47 |
+ |
|
48 |
+ conn.core.CreateWindow(depth, window, root, |
|
49 |
+ 0, 0, 640, 480, 0, |
|
50 |
+ WindowClass.InputOutput, |
|
51 |
+ visual, |
|
52 |
+ CW.BackPixel | CW.EventMask, |
|
53 |
+ [ white, EventMask.ButtonPress | EventMask.EnterWindow | EventMask.LeaveWindow | EventMask.Exposure ]) |
|
54 |
+ |
|
55 |
+ cookie = conn.render.QueryPictFormats() |
|
56 |
+ reply = cookie.reply() |
|
57 |
+ format = find_format(reply.screens[0]) |
|
58 |
+ |
|
59 |
+ name = 'X Python Binding Demo' |
|
60 |
+ conn.core.ChangeProperty(PropMode.Replace, window, xcb.XA_WM_NAME, xcb.XA_STRING, 8, len(name), name) |
|
61 |
+ conn.render.CreatePicture(pid, window, format, 0, []) |
|
62 |
+ conn.core.MapWindow(window) |
|
63 |
+ conn.flush() |
|
64 |
+ |
|
65 |
+ |
|
66 |
+def paint(): |
|
67 |
+ conn.core.ClearArea(False, window, 0, 0, 0, 0) |
|
68 |
+ |
|
69 |
+ for x in xrange(0, 7): |
|
70 |
+ for y in xrange(0, 5): |
|
71 |
+ rectangle = ((x + 1) * 24 + x * 64, (y + 1) * 24 + y * 64, 64, 64) |
|
72 |
+ color = (x * 65535 / 7, y * 65535 / 5, (x * y) * 65535 / 35, 65535) |
|
73 |
+ conn.render.FillRectangles(xcb.render.PictOp.Src, pid, color, 1, rectangle) |
|
74 |
+ |
|
75 |
+ conn.flush() |
|
76 |
+ |
|
77 |
+ |
|
78 |
+def run(): |
|
79 |
+ startup() |
|
80 |
+ print 'Click in window to exit.' |
|
81 |
+ |
|
82 |
+ while True: |
|
83 |
+ try: |
|
84 |
+ event = conn.wait_for_event() |
|
85 |
+ except xcb.ProtocolException, error: |
|
86 |
+ print "Protocol error %s received!" % error.__class__.__name__ |
|
87 |
+ break |
|
88 |
+ except: |
|
89 |
+ print "Unexpected error received: %s" % error.message |
|
90 |
+ break |
|
91 |
+ |
|
92 |
+ if isinstance(event, ExposeEvent): |
|
93 |
+ paint() |
|
94 |
+ elif isinstance(event, EnterNotifyEvent): |
|
95 |
+ print 'Enter (%d, %d)' % (event.event_x, event.event_y) |
|
96 |
+ elif isinstance(event, LeaveNotifyEvent): |
|
97 |
+ print 'Leave (%d, %d)' % (event.event_x, event.event_y) |
|
98 |
+ elif isinstance(event, ButtonPressEvent): |
|
99 |
+ print 'Button %d down' % event.detail |
|
100 |
+ break |
|
101 |
+ |
|
102 |
+ conn.disconnect() |
|
103 |
+ |
|
104 |
+ |
|
105 |
+ |
|
106 |
+conn = xcb.connect(display=':0.0') |
|
107 |
+conn.render = conn(xcb.render.key) |
|
108 |
+ |
|
109 |
+setup = conn.get_setup() |
|
110 |
+root = setup.roots[0].root |
|
111 |
+depth = setup.roots[0].root_depth |
|
112 |
+visual = setup.roots[0].root_visual |
|
113 |
+ |
|
114 |
+window = conn.generate_id() |
|
115 |
+pid = conn.generate_id() |
|
116 |
+ |
|
117 |
+run() |
|
118 |
+ |