[cairo] Using Xlib to draw on root-window problem

Ryan Flannery ryan.flannery at gmail.com
Wed Dec 31 15:31:03 PST 2008


Hello cairo@,

I recently started to use cairo+pango for some simple drawing needs,
and have always found it quick-n-easy to use for most of my needs
(many thanks to the devs!).

I've had a problem recently however when trying to use the Xlib
backend to draw on the X11 root window.  I don't seem to be able to
correctly create a cairo_surface_t  and/or cairo_t object.

Attached (and below) is a small program showing the problem I'm having...
The "cairo_create" call on line 39 segfaults.

Any hints or smacks with a clue-stick would be greatly appreciated.

-Ryan

#include <stdio.h>
#include <stdlib.h>

#include <X11/Xlib.h>
#include <cairo/cairo.h>

int main (int argc, char *argv[])
{
   /* Xlib stuff */
   Display *display = NULL;
   Window   root_window;
   Visual  *visual = NULL;
   int      screen_num;
   int      screen_width;
   int      screen_height;

   /* cairo stuff */
   cairo_surface_t *cairo_surface;
   cairo_t *cairo;

   /* Get X11 display, root-window, and visual */
   if (!(display = XOpenDisplay(NULL)))
   {  fprintf(stderr, "Can't open X11 display.\n");
      exit(1);
   }

   screen_num    = DefaultScreen(display);
   screen_height = DisplayHeight(display, screen_num);
   screen_width  = DisplayWidth(display, screen_num);
   root_window   = DefaultRootWindow(display);
   visual        = DefaultVisual(display, screen_num);

   //printf("(screen %d, visual %d, %d x %d)\n",
   // screen_num, visual, screen_width, screen_height);

   /* Create cairo surface and object */
   cairo_surface = cairo_xlib_surface_create(display, root_window, visual,
      screen_width, screen_height);
   cairo = cairo_create(cairo_surface);

   cairo_select_font_face(cairo, "serif", CAIRO_FONT_SLANT_NORMAL,
      CAIRO_FONT_WEIGHT_BOLD);
   cairo_set_font_size(cairo, 32.0);
   cairo_set_source_rgb(cairo, 0.0, 0.0, 1.0);
   cairo_move_to(cairo, 10.0, 50.0);
   cairo_show_text(cairo, "Hello World");

   while (getchar() != 'x')
   {
      // just keep looping
   }

   cairo_destroy(cairo);
   cairo_surface_destroy(cairo_surface);
   XCloseDisplay(display);

   return 0;
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test.c
Type: application/octet-stream
Size: 1531 bytes
Desc: not available
Url : http://lists.cairographics.org/archives/cairo/attachments/20081231/2f4d12b4/attachment.obj 


More information about the cairo mailing list