Re: begginer needs help, using cairo to make a background program working for X

Uli Schlachter <[email protected]>
Newsgroups gmane.comp.lib.cairo
Message-ID <[email protected]>
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?

-- 
cairo mailing list
[email protected]
http://lists.cairographics.org/mailman/listinfo/cairo
t.c (text/x-csrc, 1.7 KB)
#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, *bg2;

	dpy = XOpenDisplay(NULL);

        scr=DefaultScreen(dpy);
        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);
        bg2=cairo_image_surface_create_from_png("/usr/share/backgrounds/wallpaper.png");

        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, bg2, 100, 100);
                cairo_paint(c);
        //      cairo_show_page(c);
        //      cairo_surface_destroy(bg);
		cairo_surface_flush(cairo_get_target(c));
                cairo_destroy(c);
		XFlush(dpy);
                }
        }

        cairo_surface_destroy(bg);
        XCloseDisplay(dpy);

        return 0;
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.