Skip to content

Instantly share code, notes, and snippets.

@jakeogh
Created October 21, 2015 05:43
Show Gist options
  • Save jakeogh/6be38098deda85f48c72 to your computer and use it in GitHub Desktop.
Save jakeogh/6be38098deda85f48c72 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3.4
import traceback
import time
import xcffib
from xcffib.xproto import *
import xcffib.render
def find_format(screen):
for d in screen.depths:
if d.depth == depth:
for v in d.visuals:
if v.visual == visual:
return v.format
raise Exception("Failed to find an appropriate Render pictformat!")
def startup():
white = setup.roots[0].white_pixel
conn.core.CreateWindow(depth, window, root,
0, 0, 800, 600, 0,
WindowClass.InputOutput,
visual,
CW.BackPixel | CW.EventMask,
[ white, EventMask.ButtonPress | EventMask.EnterWindow | EventMask.LeaveWindow | EventMask.Exposure ])
cookie = conn.render.QueryPictFormats()
reply = cookie.reply()
format = find_format(reply.screens[0])
name = 'X Python Binding Demo'
# conn.core.ChangeProperty(PropMode.Replace, window, xcffib.XA_WM_NAME, xcffib.XA_STRING, 8, len(name), name)
conn.render.CreatePicture(pid, window, format, 0, [])
conn.core.MapWindow(window)
conn.flush()
def paint():
conn.core.ClearArea(False, window, 0, 0, 0, 0)
for x in range(0, 7):
for y in range(0, 5):
rectangle = ((x + 1) * 24 + x * 64, (y + 1) * 24 + y * 64, 64, 64)
new_rectangle = RECTANGLE.synthetic(x=int(rectangle[0]), y=int(rectangle[1]), width=int(rectangle[2]), height=int(rectangle[3]))
color = (x * 65535 / 7, y * 65535 / 5, (x * y) * 65535 / 35, 65535)
new_color = xcffib.render.COLOR.synthetic(red=int(color[0]), blue=int(color[1]), green=int(color[2]), alpha=int(color[3]))
conn.render.FillRectangles(xcffib.render.PictOp.Src, pid, new_color, [new_rectangle])
conn.flush()
def run():
startup()
print('Click in window to exit.')
while True:
try:
event = conn.wait_for_event()
print("event:", event)
except xcffib.ProtocolException as error:
print("Protocol error %s received!" % error.__class__.__name__)
traceback.print_exc()
break
except Exception as error:
print(type(error)) #<class 'xcffib.render.PictureError'>
print("Unexpected error received: %s" % error) #why is this empty?
traceback.print_exc()
break
if isinstance(event, ExposeEvent):
paint()
# time.sleep(1)
conn.disconnect()
conn = xcffib.connect()
conn.render = conn(xcffib.render.key)
setup = conn.get_setup()
root = setup.roots[0].root
depth = setup.roots[0].root_depth
visual = setup.roots[0].root_visual
window = conn.generate_id()
pid = conn.generate_id()
run()
Click in window to exit.
event: <xcffib.xproto.ExposeEvent object at 0x7f78623de2e8>
<class 'KeyError'>
Unexpected error received: 1
Traceback (most recent call last):
File "./xcffib_test_to_post.py", line 61, in run
event = conn.wait_for_event()
File "/usr/lib64/python3.4/site-packages/xcffib/__init__.py", line 555, in wrapper
return f(*args)
File "/usr/lib64/python3.4/site-packages/xcffib/__init__.py", line 589, in wait_for_event
return self.hoist_event(e)
File "/usr/lib64/python3.4/site-packages/xcffib/__init__.py", line 669, in hoist_event
return self._process_error(ffi.cast("xcb_generic_error_t *", e))
File "/usr/lib64/python3.4/site-packages/xcffib/__init__.py", line 631, in _process_error
error = self._error_offsets[c_error.error_code]
File "/usr/lib64/python3.4/site-packages/xcffib/__init__.py", line 476, in __getitem__
return things[item - offset]
KeyError: 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment