def create_surface(drawable, visualtype, width=None, height=None): """ create a cairo surface for *drawable*. :Parameters: `drawable` `visualtype` : ooxcb.xproto.Visualtype most likely the result of `screen.get_root_visual_type()` `width` `height` The dimensions of the surface. If one of those arguments is None, it is retrieved from the drawable's geometry. """ if (width is None or height is None): geom = drawable.get_geometry().reply() if width is None: width = geom.width if height is None: height = geom.height return cairo.cairo_xcb_surface_create(drawable.conn, drawable, visualtype, width, height)
def set_root(conn, screen_info, style, func=None): root = screen_info.root geom = root.get_geometry().reply() pixmap = xproto.Pixmap.create( conn, root, geom.width, geom.height, screen_info.root_depth ) surface = cairo.cairo_xcb_surface_create( conn, pixmap, screen_info.get_root_visual_type(), geom.width, geom.height, ) root.change_attributes(back_pixmap=pixmap) cr = cairo.cairo_create(surface) if func is None: func = yahiko_render func(cr, style, geom.width, geom.height) root.clear_area(0, 0, geom.width, geom.height) conn.flush()
visualtype = screen.get_root_visual_type() width = 640 height = 480 running = True with conn.bunch(): win = xproto.Window.create_toplevel_on_screen(conn, screen, width=width, height=height, back_pixel=screen.white_pixel, event_mask=xproto.EventMask.Exposure | xproto.EventMask.ButtonPress ) win.map() # create a surface for this window surface = cairo.cairo_xcb_surface_create(conn, win, visualtype, width, height) # and a cairo context cr = cairo.cairo_create(surface) cairo.cairo_set_operator(cr, cairo.CAIRO_OPERATOR_SOURCE) cairo.cairo_set_source_surface(cr, surface, 0, 0) cairo.cairo_set_source_rgba(cr, 255, 0, 0, 0) @win.event def on_expose(event): # paint a red rectangle when an expose event occurs cairo.cairo_set_source_rgb(cr, 255, 0, 0) cairo.cairo_rectangle(cr, 100, 100, 300, 300) cairo.cairo_fill(cr) # don't forget to flush!
import sys sys.path.append('..') import ooxcb from ooxcb.protocol import xproto from ooxcb.contrib import cairo conn = ooxcb.connect() screen = conn.setup.roots[conn.pref_screen] root = screen.root surface = cairo.cairo_xcb_surface_create(conn, root, screen.get_root_visual_type(), screen.width_in_pixels, screen.height_in_pixels) cairo.cairo_surface_write_to_png(surface, "screenshot.png") conn.disconnect()