[cairo] begginer needs help, using cairo to make a background program working for X

Uli Schlachter psychon at znc.in
Sun May 19 01:37:25 PDT 2013


On 19.05.2013 05:27, First Last wrote:
> Hi, I try to make a program who renders a background when I launch X, for now I'm able to render a rectangle filled with a color, but I didn't find how to put a picture in this rectangle, I add my code :
> 
> #include<cairo.h>
> #include<cairo-xlib.h>
> #include<X11/Xlib.h>
> #include<stdio.h>
> #include<stdlib.h>
> #include<string.h>
> 
> #define DPYW DisplayWidth(dpy,scr)
> #define DPYH DisplayHeight(dpy,scr)
> 
> 
> int main(int argc, char *argv[]){
>         Display *dpy;
>         int scr;
>         Window rootwin;
>         Window wbg;
>         XEvent e;
>         cairo_surface_t *bg;
> 
> 
>         scr=DefaultScreen(dpy);

dpy is an uninitialized variable at this point. So clearly, you never ran this
code since it will always crash.

>         rootwin=RootWindow(dpy, scr);
>         wbg=XCreateSimpleWindow(dpy,rootwin,0,0,
>                                 DPYW,DPYH,0,
>                                 BlackPixel(dpy, scr),
>                                 BlackPixel(dpy, scr) );
> 
> 
>         XStoreName(dpy, wbg, "background");
>         XSelectInput(dpy, wbg, ExposureMask);
>         XMapWindow(dpy, wbg);
> 
> //      bg=cairo_xlib_surface_create(dpy, wbg, DefaultVisual(dpy, 0), DPYW, DPYH);
>         bg=cairo_image_surface_create_from_png("/usr/share/backgrounds/wallpaper.png");

Uhm, why just either? You need a surface that contains your image and another
one that you want to draw too.

>         while(1) {
>                 XNextEvent(dpy, &e);
>                 if(e.type==Expose && e.xexpose.count<1) {
>                 cairo_t *c;
> 
>                 c=cairo_create(bg);
>         //      cairo_rectangle(c,0,0,DPYW,DPYH);
>         //      cairo_set_source_rgb(c,0.5,0,0);
>         //      cairo_fill(c);
>         //      cairo_show_page(c);
>         //      cairo_destroy(c);
> 
>                 cairo_set_source_surface (c, bg, 100, 100);

This draws bg to itself. That is not really something that has well-defined
semantics.

>                 cairo_paint(c);
>         //      cairo_show_page(c);
>         //      cairo_surface_destroy(bg);
>                 cairo_destroy(c);
>                 }
>         }
> 
>         cairo_surface_destroy(bg);
>         XCloseDisplay(dpy);
> 
>         return 0;
> }
>
> I don't understand what it's missing, or what I'm doing wrong.

Attached is a working version of this code.

Uli
-- 
Q: Because it reverses the logical flow of conversation.
A: Why is putting a reply at the top of the message frowned upon?
-------------- next part --------------
A non-text attachment was scrubbed...
Name: t.c
Type: text/x-csrc
Size: 1719 bytes
Desc: not available
URL: <http://lists.cairographics.org/archives/cairo/attachments/20130519/12b72c22/attachment.c>


More information about the cairo mailing list