Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for XCB surfaces via xcffib #37

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions cairocffi/__init__.py
Expand Up @@ -35,6 +35,14 @@ def dlopen(ffi, *names):


ffi = FFI()

try:
# Include XCB headers if we can.
import xcffib
ffi.include(xcffib.ffi)
except ImportError:
pass

ffi.cdef(constants._CAIRO_HEADERS)
cairo = dlopen(ffi, 'libcairo.so.2', 'libcairo.2.dylib', 'libcairo-2.dll',
'cairo', 'libcairo-2')
Expand Down Expand Up @@ -100,6 +108,10 @@ def install_as_pycairo():

from .surfaces import (Surface, ImageSurface, PDFSurface, PSSurface,
SVGSurface, RecordingSurface)
try:
from .surfaces import XCBSurface
except ImportError:
pass
from .patterns import (Pattern, SolidPattern, SurfacePattern,
Gradient, LinearGradient, RadialGradient)
from .fonts import FontFace, ToyFontFace, ScaledFont, FontOptions
Expand Down
9 changes: 7 additions & 2 deletions cairocffi/constants.py
Expand Up @@ -38,7 +38,8 @@
STATUS_DEVICE_ERROR = 35
STATUS_INVALID_MESH_CONSTRUCTION = 36
STATUS_DEVICE_FINISHED = 37
STATUS_LAST_STATUS = 38
STATUS_JBIG2_GLOBAL_MISSING = 38
STATUS_LAST_STATUS = 39

CONTENT_COLOR = 0x1000
CONTENT_ALPHA = 0x2000
Expand Down Expand Up @@ -230,7 +231,7 @@
{
int unused;
} cairo_user_data_key_t;
typedef enum _cairo_status {CAIRO_STATUS_SUCCESS = 0, CAIRO_STATUS_NO_MEMORY, CAIRO_STATUS_INVALID_RESTORE, CAIRO_STATUS_INVALID_POP_GROUP, CAIRO_STATUS_NO_CURRENT_POINT, CAIRO_STATUS_INVALID_MATRIX, CAIRO_STATUS_INVALID_STATUS, CAIRO_STATUS_NULL_POINTER, CAIRO_STATUS_INVALID_STRING, CAIRO_STATUS_INVALID_PATH_DATA, CAIRO_STATUS_READ_ERROR, CAIRO_STATUS_WRITE_ERROR, CAIRO_STATUS_SURFACE_FINISHED, CAIRO_STATUS_SURFACE_TYPE_MISMATCH, CAIRO_STATUS_PATTERN_TYPE_MISMATCH, CAIRO_STATUS_INVALID_CONTENT, CAIRO_STATUS_INVALID_FORMAT, CAIRO_STATUS_INVALID_VISUAL, CAIRO_STATUS_FILE_NOT_FOUND, CAIRO_STATUS_INVALID_DASH, CAIRO_STATUS_INVALID_DSC_COMMENT, CAIRO_STATUS_INVALID_INDEX, CAIRO_STATUS_CLIP_NOT_REPRESENTABLE, CAIRO_STATUS_TEMP_FILE_ERROR, CAIRO_STATUS_INVALID_STRIDE, CAIRO_STATUS_FONT_TYPE_MISMATCH, CAIRO_STATUS_USER_FONT_IMMUTABLE, CAIRO_STATUS_USER_FONT_ERROR, CAIRO_STATUS_NEGATIVE_COUNT, CAIRO_STATUS_INVALID_CLUSTERS, CAIRO_STATUS_INVALID_SLANT, CAIRO_STATUS_INVALID_WEIGHT, CAIRO_STATUS_INVALID_SIZE, CAIRO_STATUS_USER_FONT_NOT_IMPLEMENTED, CAIRO_STATUS_DEVICE_TYPE_MISMATCH, CAIRO_STATUS_DEVICE_ERROR, CAIRO_STATUS_INVALID_MESH_CONSTRUCTION, CAIRO_STATUS_DEVICE_FINISHED, CAIRO_STATUS_LAST_STATUS} cairo_status_t;
typedef enum _cairo_status {CAIRO_STATUS_SUCCESS = 0, CAIRO_STATUS_NO_MEMORY, CAIRO_STATUS_INVALID_RESTORE, CAIRO_STATUS_INVALID_POP_GROUP, CAIRO_STATUS_NO_CURRENT_POINT, CAIRO_STATUS_INVALID_MATRIX, CAIRO_STATUS_INVALID_STATUS, CAIRO_STATUS_NULL_POINTER, CAIRO_STATUS_INVALID_STRING, CAIRO_STATUS_INVALID_PATH_DATA, CAIRO_STATUS_READ_ERROR, CAIRO_STATUS_WRITE_ERROR, CAIRO_STATUS_SURFACE_FINISHED, CAIRO_STATUS_SURFACE_TYPE_MISMATCH, CAIRO_STATUS_PATTERN_TYPE_MISMATCH, CAIRO_STATUS_INVALID_CONTENT, CAIRO_STATUS_INVALID_FORMAT, CAIRO_STATUS_INVALID_VISUAL, CAIRO_STATUS_FILE_NOT_FOUND, CAIRO_STATUS_INVALID_DASH, CAIRO_STATUS_INVALID_DSC_COMMENT, CAIRO_STATUS_INVALID_INDEX, CAIRO_STATUS_CLIP_NOT_REPRESENTABLE, CAIRO_STATUS_TEMP_FILE_ERROR, CAIRO_STATUS_INVALID_STRIDE, CAIRO_STATUS_FONT_TYPE_MISMATCH, CAIRO_STATUS_USER_FONT_IMMUTABLE, CAIRO_STATUS_USER_FONT_ERROR, CAIRO_STATUS_NEGATIVE_COUNT, CAIRO_STATUS_INVALID_CLUSTERS, CAIRO_STATUS_INVALID_SLANT, CAIRO_STATUS_INVALID_WEIGHT, CAIRO_STATUS_INVALID_SIZE, CAIRO_STATUS_USER_FONT_NOT_IMPLEMENTED, CAIRO_STATUS_DEVICE_TYPE_MISMATCH, CAIRO_STATUS_DEVICE_ERROR, CAIRO_STATUS_INVALID_MESH_CONSTRUCTION, CAIRO_STATUS_DEVICE_FINISHED, CAIRO_STATUS_JBIG2_GLOBAL_MISSING, CAIRO_STATUS_LAST_STATUS} cairo_status_t;
typedef enum _cairo_content {CAIRO_CONTENT_COLOR = 0x1000, CAIRO_CONTENT_ALPHA = 0x2000, CAIRO_CONTENT_COLOR_ALPHA = 0x3000} cairo_content_t;
typedef enum _cairo_format {CAIRO_FORMAT_INVALID = -1, CAIRO_FORMAT_ARGB32 = 0, CAIRO_FORMAT_RGB24 = 1, CAIRO_FORMAT_A8 = 2, CAIRO_FORMAT_A1 = 3, CAIRO_FORMAT_RGB16_565 = 4, CAIRO_FORMAT_RGB30 = 5} cairo_format_t;
typedef cairo_status_t (*cairo_write_func_t)(void *closure, const unsigned char *data, unsigned int length);
Expand Down Expand Up @@ -544,6 +545,8 @@
void cairo_surface_flush(cairo_surface_t *surface);
void cairo_surface_mark_dirty(cairo_surface_t *surface);
void cairo_surface_mark_dirty_rectangle(cairo_surface_t *surface, int x, int y, int width, int height);
void cairo_surface_set_device_scale(cairo_surface_t *surface, double x_scale, double y_scale);
void cairo_surface_get_device_scale(cairo_surface_t *surface, double *x_scale, double *y_scale);
void cairo_surface_set_device_offset(cairo_surface_t *surface, double x_offset, double y_offset);
void cairo_surface_get_device_offset(cairo_surface_t *surface, double *x_offset, double *y_offset);
void cairo_surface_set_fallback_resolution(cairo_surface_t *surface, double x_pixels_per_inch, double y_pixels_per_inch);
Expand Down Expand Up @@ -685,4 +688,6 @@
void cairo_svg_surface_restrict_to_version(cairo_surface_t *surface, cairo_svg_version_t version);
void cairo_svg_get_versions(const cairo_svg_version_t **versions, int *num_versions);
const char *cairo_svg_version_to_string(cairo_svg_version_t version);
cairo_surface_t *cairo_xcb_surface_create(xcb_connection_t *connection, xcb_drawable_t drawable, xcb_visualtype_t *visual, int width, int height);
void cairo_xcb_surface_set_size(cairo_surface_t *surface, int width, int height);
"""
59 changes: 57 additions & 2 deletions cairocffi/mkconstants.py
Expand Up @@ -2,6 +2,8 @@
import re
import pycparser.c_generator

import textwrap


def parse_constant(node):
if isinstance(node, pycparser.c_ast.Constant):
Expand All @@ -27,6 +29,16 @@ def visit_Enum(self, node):
print('')


xcb_typedefs = textwrap.dedent("""
typedef void xcb_connection_t;
typedef void xcb_drawable_t;
typedef void xcb_visualtype_t;
typedef void xcb_screen_t;
typedef void xcb_render_pictforminfo_t;
typedef void xcb_pixmap_t;
""")


def generate(include_dir):
# Remove comments, preprocessor instructions and macros.
source = re.sub(
Expand All @@ -36,12 +48,55 @@ def generate(include_dir):
r'|^\s*#.*?[^\\]\n',
'',
''.join(open('%s/cairo%s.h' % (include_dir, suffix), 'r').read()
for suffix in ['', '-pdf', '-ps', '-svg']),
for suffix in ['', '-pdf', '-ps', '-svg', '-xcb']),
flags=re.DOTALL | re.MULTILINE)
print('# Generated by mkconstants.py\n')
ast = pycparser.CParser().parse(source)
ast = pycparser.CParser().parse(xcb_typedefs + source)
Visitor().visit(ast)
source = pycparser.c_generator.CGenerator().visit(ast)

try:
# Since we're going to ffi.include() the real definitions for xcb_*_t
# via xcffib, there's no need to include the headers here. However, the
# C parser fails to parse things it doesn't know about, so we typedef
# them all to void and strip them all out at the end. They won't ever
# actually be compiled because of a similar try/except in __init__ that
# doesn't add this header if it can't find xcffib to ffi.include()
import xcffib

# for now we're only implementing compat with py2cairo, which only
# uses cairo_xcb_surface_create and cairo_xcb_surface_set_size. The
# trailing ( is a cheap way of getting exactly these functions to
# match.
xcb_functions = [
'cairo_xcb_surface_create(', 'cairo_xcb_surface_set_size(']

filtered = []
for line in source.split('\n'):
ok = False

if 'xcb' in line:
if any([function in line for function in xcb_functions]):
ok = True
else:
ok = True

if line in xcb_typedefs.split('\n'):
ok = False

if ok:
filtered.append(line)

source = '\n'.join(filtered)

except ImportError:
# filter all XCB related stuff out since we're not going to be defining XCB
no_xcb = []
for line in source.split('\n'):
if 'xcb' not in line:
no_xcb.append(line)
source = '\n'.join(no_xcb)

print('_CAIRO_HEADERS = r"""\n%s\n"""' % source.strip())


Expand Down
33 changes: 33 additions & 0 deletions cairocffi/surfaces.py
Expand Up @@ -1280,3 +1280,36 @@ def ink_extents(self):
constants.SURFACE_TYPE_SVG: SVGSurface,
constants.SURFACE_TYPE_RECORDING: RecordingSurface,
}


try:
import xcffib

class XCBSurface(Surface):
"""The XCB surface is used to render cairo graphics to X Window System
windows and pixmaps using the XCB library.

Note that the XCB surface automatically takes advantage of the X render
extension if it is available.

:param conn: The `xcffib.Connection` object
:param drawable: An XID corresponding to an XCB drawable
:param visual: An xcffib.xproto.VISUALTYPE object.
:param width: width (integer)
:param height: height (integer)
"""
def __init__(self, conn, drawable, visual, width, height):
c_visual = xcffib.visualtype_to_c_struct(visual)

p = cairo.cairo_xcb_surface_create(
conn._conn, drawable, c_visual, width, height)
Surface.__init__(self, p)

def set_size(self, width, height):
cairo.cairo_xcb_surface_set_size(self._pointer, width, height)
self._check_status()

SURFACE_TYPE_TO_CLASS[constants.SURFACE_TYPE_XCB] = XCBSurface

except ImportError:
pass