Last active
February 4, 2016 07:14
-
-
Save jakeogh/fd9a325f3306a6551147 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3.3 | |
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.OverrideRedirect| 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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment