newbie questions - Xwindows

This is a discussion on newbie questions - Xwindows ; Hi, I am a newbie trying to use Xlib. Specifically, I have cairo drawing into a pixmap, and I want to display this pixmap in a window (using a thread that runs "in the background"). I could make cairo draw ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: newbie questions

  1. newbie questions

    Hi,

    I am a newbie trying to use Xlib. Specifically, I have cairo drawing
    into a pixmap, and I want to display this pixmap in a window (using a
    thread that runs "in the background"). I could make cairo draw
    directly into the window, but I need the pixmap because then I would
    have to record and replay the cairo commands when refreshing the
    window.

    I want this window to

    1. redraw areas when needed,
    2. close when requested

    Here is what I have come up with (I describe my problems below):

    #include
    #include

    #define ERROR_OPENDISPLAY 1

    typedef struct {
    Display *display;
    int screen;
    Window window;
    Pixmap pixmap;
    cairo_surface_t *surface;
    } xlib_surface;

    int
    create_xlib_surface(char *display_name,
    unsigned int width,
    unsigned int height,
    xlib_surface *xlib_surf) {
    Display *display; /* display */
    Window root; /* root window */
    int screen; /* screen */
    Visual *visual; /* visual */
    int depth; /* depth */
    XSetWindowAttributes attr; /* attributes */
    Window window; /* */
    Pixmap pixmap; /* */
    GC gc; /* graphics context */
    XEvent ev; /* event */

    display = XOpenDisplay(display_name);
    if (!display)
    return ERROR_OPENDISPLAY;
    screen = DefaultScreen(display);
    root = RootWindow(display, screen);
    visual = DefaultVisual(display, screen);
    depth = DefaultDepth(display, screen);
    /* window attributes */
    attr.background_pixel = WhitePixel (display, screen);
    attr.border_pixel = 0;
    attr.event_mask = ExposureMask|KeyPressMask|KeyReleaseMask|
    StructureNotifyMask;
    /* create window and pixmap */
    window = XCreateSimpleWindow(display, root, 0, 0, width, height,
    0, 0,
    attr.background_pixel);
    pixmap = XCreatePixmap(display, window, width, height, depth);
    /* graphics context */
    gc = XCreateGC(display, pixmap, 0, 0);
    /* select events, map window */
    XSelectInput(display, window, ExposureMask);
    XMapWindow(display, window);
    /* allocate structure, create cairo surface */
    xlib_surf->surface = cairo_xlib_surface_create(display, pixmap,
    visual,
    width, height);
    /* main loop */
    for (; {
    XNextEvent(display, &ev);
    switch (ev.type) {
    case Expose:
    if (ev.xexpose.count > 0)
    break;
    XCopyArea(display, pixmap, window, gc, 0, 0, width, height, 0,
    0);
    break;
    case ClientMessage:
    return(0);
    default:
    break;
    }
    }
    }

    Questions:

    1. when I close the window, instead of returning 0, the whole
    application closes with the message X connection to :0.0 broken
    (explicit kill or server shutdown). What am I doing wrong?

    2. I want the window to be of fixed size (non-resizable), how can I do
    that?

    Thanks,

    Tamas


  2. Re: newbie questions

    On Jun 9, 11:01 am, Tamas wrote:
    > Hi,
    >
    > I am a newbie trying to use Xlib. Specifically, I have cairo drawing
    > into a pixmap, and I want to display this pixmap in a window (using a
    > thread that runs "in the background"). I could make cairo draw
    > directly into the window, but I need the pixmap because then I would
    > have to record and replay the cairo commands when refreshing the
    > window.
    >
    > I want this window to
    >
    > 1. redraw areas when needed,
    > 2. close when requested
    >
    > Here is what I have come up with (I describe my problems below):
    >
    > #include
    > #include
    >
    > #define ERROR_OPENDISPLAY 1
    >
    > typedef struct {
    > Display *display;
    > int screen;
    > Window window;
    > Pixmap pixmap;
    > cairo_surface_t *surface;
    >
    > } xlib_surface;
    >
    > int
    > create_xlib_surface(char *display_name,
    > unsigned int width,
    > unsigned int height,
    > xlib_surface *xlib_surf) {
    > Display *display; /* display */
    > Window root; /* root window */
    > int screen; /* screen */
    > Visual *visual; /* visual */
    > int depth; /* depth */
    > XSetWindowAttributes attr; /* attributes */
    > Window window; /* */
    > Pixmap pixmap; /* */
    > GC gc; /* graphics context */
    > XEvent ev; /* event */
    >
    > display = XOpenDisplay(display_name);
    > if (!display)
    > return ERROR_OPENDISPLAY;
    > screen = DefaultScreen(display);
    > root = RootWindow(display, screen);
    > visual = DefaultVisual(display, screen);
    > depth = DefaultDepth(display, screen);
    > /* window attributes */
    > attr.background_pixel = WhitePixel (display, screen);
    > attr.border_pixel = 0;
    > attr.event_mask = ExposureMask|KeyPressMask|KeyReleaseMask|
    > StructureNotifyMask;
    > /* create window and pixmap */
    > window = XCreateSimpleWindow(display, root, 0, 0, width, height,
    > 0, 0,
    > attr.background_pixel);
    > pixmap = XCreatePixmap(display, window, width, height, depth);
    > /* graphics context */
    > gc = XCreateGC(display, pixmap, 0, 0);
    > /* select events, map window */
    > XSelectInput(display, window, ExposureMask);
    > XMapWindow(display, window);
    > /* allocate structure, create cairo surface */
    > xlib_surf->surface = cairo_xlib_surface_create(display, pixmap,
    > visual,
    > width, height);
    > /* main loop */
    > for (; {
    > XNextEvent(display, &ev);
    > switch (ev.type) {
    > case Expose:
    > if (ev.xexpose.count > 0)
    > break;
    > XCopyArea(display, pixmap, window, gc, 0, 0, width, height, 0,
    > 0);
    > break;
    > case ClientMessage:
    > return(0);
    > default:
    > break;
    > }
    > }
    >
    > }
    >
    > Questions:
    >
    > 1. when I close the window, instead of returning 0, the whole
    > application closes with the message X connection to :0.0 broken
    > (explicit kill or server shutdown). What am I doing wrong?
    >
    > 2. I want the window to be of fixed size (non-resizable), how can I do
    > that?
    >
    > Thanks,
    >
    > Tamas


    1. You probably need to have your application call
    XCloseDisplay(display);

    2.
    You could try setting attr.override_redirect = True;
    OR
    can use a windows manager hint so the windows manager doesn't try to
    reparent the window with resizing..
    I do something (for now) like:

    NO_BORDER = XInternAtom(dpy, "_MOTIF_WM_HINTS", False);

    mwmhints.flags = MWM_HINTS_DECORATIONS;
    mwmhints.decorations = 0;

    XChangeProperty(dpy, window,
    NO_BORDER, NO_BORDER, 32, PropModeReplace, (unsigned char *)
    &mwmhints, 5);

    You should be able to improve this. Also this removes the decorations
    all together which you may or may not want to do. Google window
    manager hints and override_redirect if that doesn't work.


  3. Re: newbie questions

    In comp.windows.x, David L. Goldberg

    wrote
    on Sun, 10 Jun 2007 04:20:52 -0000
    <1181449252.145931.242660@q69g2000hsb.googlegroups. com>:
    > On Jun 9, 11:01 am, Tamas wrote:
    >> Hi,
    >>
    >> I am a newbie trying to use Xlib. Specifically, I have cairo drawing
    >> into a pixmap, and I want to display this pixmap in a window (using a
    >> thread that runs "in the background"). I could make cairo draw
    >> directly into the window, but I need the pixmap because then I would
    >> have to record and replay the cairo commands when refreshing the
    >> window.
    >>
    >> I want this window to
    >>
    >> 1. redraw areas when needed,
    >> 2. close when requested
    >>
    >> Here is what I have come up with (I describe my problems below):
    >>
    >> #include
    >> #include
    >>
    >> #define ERROR_OPENDISPLAY 1
    >>
    >> typedef struct {
    >> Display *display;
    >> int screen;
    >> Window window;
    >> Pixmap pixmap;
    >> cairo_surface_t *surface;
    >>
    >> } xlib_surface;
    >>
    >> int
    >> create_xlib_surface(char *display_name,
    >> unsigned int width,
    >> unsigned int height,
    >> xlib_surface *xlib_surf) {
    >> Display *display; /* display */
    >> Window root; /* root window */
    >> int screen; /* screen */
    >> Visual *visual; /* visual */
    >> int depth; /* depth */
    >> XSetWindowAttributes attr; /* attributes */
    >> Window window; /* */
    >> Pixmap pixmap; /* */
    >> GC gc; /* graphics context */
    >> XEvent ev; /* event */
    >>
    >> display = XOpenDisplay(display_name);
    >> if (!display)
    >> return ERROR_OPENDISPLAY;
    >> screen = DefaultScreen(display);
    >> root = RootWindow(display, screen);
    >> visual = DefaultVisual(display, screen);
    >> depth = DefaultDepth(display, screen);
    >> /* window attributes */
    >> attr.background_pixel = WhitePixel (display, screen);
    >> attr.border_pixel = 0;
    >> attr.event_mask = ExposureMask|KeyPressMask|KeyReleaseMask|
    >> StructureNotifyMask;
    >> /* create window and pixmap */
    >> window = XCreateSimpleWindow(display, root, 0, 0, width, height,
    >> 0, 0,
    >> attr.background_pixel);
    >> pixmap = XCreatePixmap(display, window, width, height, depth);
    >> /* graphics context */
    >> gc = XCreateGC(display, pixmap, 0, 0);
    >> /* select events, map window */
    >> XSelectInput(display, window, ExposureMask);
    >> XMapWindow(display, window);
    >> /* allocate structure, create cairo surface */
    >> xlib_surf->surface = cairo_xlib_surface_create(display, pixmap,
    >> visual,
    >> width, height);
    >> /* main loop */
    >> for (; {
    >> XNextEvent(display, &ev);
    >> switch (ev.type) {
    >> case Expose:
    >> if (ev.xexpose.count > 0)
    >> break;
    >> XCopyArea(display, pixmap, window, gc, 0, 0, width, height, 0,
    >> 0);
    >> break;
    >> case ClientMessage:
    >> return(0);
    >> default:
    >> break;
    >> }
    >> }
    >>
    >> }
    >>
    >> Questions:
    >>
    >> 1. when I close the window, instead of returning 0, the whole
    >> application closes with the message X connection to :0.0 broken
    >> (explicit kill or server shutdown). What am I doing wrong?
    >>
    >> 2. I want the window to be of fixed size (non-resizable), how can I do
    >> that?
    >>
    >> Thanks,
    >>
    >> Tamas

    >
    > 1. You probably need to have your application call
    > XCloseDisplay(display);


    There's a little more to it than that, though it depends on what
    behavior is desired.

    [a] XCloseDisplay() will of course work; usually it's in response to a
    user command (picked from a menu or a keypress or some such). If
    one wants to intercept the window close (usually the 'x' in the upper
    right, or in some cases the upper left, corner), one has to do more
    involved things by looking for client messages after calling
    XSetWMProtocols():

    XAtom prots[1];
    prots[0] = XInternAtom(dpy, "WM_DELETE_WINDOW", false);
    XSetWMProtocols(dpy, window, prots, 1);

    (or one can use XChangeProperty() on WM_PROTOCOLS directly).

    There are two other protocols that might be of interest:
    WM_SAVE_YOURSELF and WM_TAKE_FOCUS.

    [b] Closing the display closes all windows.

    >
    > 2.
    > You could try setting attr.override_redirect = True;
    > OR
    > can use a windows manager hint so the windows manager doesn't try to
    > reparent the window with resizing..
    > I do something (for now) like:
    >
    > NO_BORDER = XInternAtom(dpy, "_MOTIF_WM_HINTS", False);
    >
    > mwmhints.flags = MWM_HINTS_DECORATIONS;
    > mwmhints.decorations = 0;
    >
    > XChangeProperty(dpy, window,
    > NO_BORDER, NO_BORDER, 32, PropModeReplace, (unsigned char *)
    > &mwmhints, 5);
    >
    > You should be able to improve this. Also this removes the decorations
    > all together which you may or may not want to do. Google window
    > manager hints and override_redirect if that doesn't work.
    >


    Not sure how standard this is; I'd have to look. :-)

    --
    #191, ewill3@earthlink.net
    "Woman? What woman?"

    --
    Posted via a free Usenet account from http://www.teranews.com


  4. Re: newbie questions

    On Jun 10, 1:17 pm, The Ghost In The Machine
    wrote:
    > In comp.windows.x, David L. Goldberg
    >
    > wrote
    > on Sun, 10 Jun 2007 04:20:52 -0000
    > <1181449252.145931.242...@q69g2000hsb.googlegroups. com>:
    >
    >
    >
    > > On Jun 9, 11:01 am, Tamas wrote:
    > >> Hi,

    >
    > >> I am a newbie trying to use Xlib. Specifically, I have cairo drawing
    > >> into a pixmap, and I want to display this pixmap in a window (using a
    > >> thread that runs "in the background"). I could make cairo draw
    > >> directly into the window, but I need the pixmap because then I would
    > >> have to record and replay the cairo commands when refreshing the
    > >> window.

    >
    > >> I want this window to

    >
    > >> 1. redraw areas when needed,
    > >> 2. close when requested

    >
    > >> Here is what I have come up with (I describe my problems below):

    >
    > >> #include
    > >> #include

    >
    > >> #define ERROR_OPENDISPLAY 1

    >
    > >> typedef struct {
    > >> Display *display;
    > >> int screen;
    > >> Window window;
    > >> Pixmap pixmap;
    > >> cairo_surface_t *surface;

    >
    > >> } xlib_surface;

    >
    > >> int
    > >> create_xlib_surface(char *display_name,
    > >> unsigned int width,
    > >> unsigned int height,
    > >> xlib_surface *xlib_surf) {
    > >> Display *display; /* display */
    > >> Window root; /* root window */
    > >> int screen; /* screen */
    > >> Visual *visual; /* visual */
    > >> int depth; /* depth */
    > >> XSetWindowAttributes attr; /* attributes */
    > >> Window window; /* */
    > >> Pixmap pixmap; /* */
    > >> GC gc; /* graphics context */
    > >> XEvent ev; /* event */

    >
    > >> display = XOpenDisplay(display_name);
    > >> if (!display)
    > >> return ERROR_OPENDISPLAY;
    > >> screen = DefaultScreen(display);
    > >> root = RootWindow(display, screen);
    > >> visual = DefaultVisual(display, screen);
    > >> depth = DefaultDepth(display, screen);
    > >> /* window attributes */
    > >> attr.background_pixel = WhitePixel (display, screen);
    > >> attr.border_pixel = 0;
    > >> attr.event_mask = ExposureMask|KeyPressMask|KeyReleaseMask|
    > >> StructureNotifyMask;
    > >> /* create window and pixmap */
    > >> window = XCreateSimpleWindow(display, root, 0, 0, width, height,
    > >> 0, 0,
    > >> attr.background_pixel);
    > >> pixmap = XCreatePixmap(display, window, width, height, depth);
    > >> /* graphics context */
    > >> gc = XCreateGC(display, pixmap, 0, 0);
    > >> /* select events, map window */
    > >> XSelectInput(display, window, ExposureMask);
    > >> XMapWindow(display, window);
    > >> /* allocate structure, create cairo surface */
    > >> xlib_surf->surface = cairo_xlib_surface_create(display, pixmap,
    > >> visual,
    > >> width, height);
    > >> /* main loop */
    > >> for (; {
    > >> XNextEvent(display, &ev);
    > >> switch (ev.type) {
    > >> case Expose:
    > >> if (ev.xexpose.count > 0)
    > >> break;
    > >> XCopyArea(display, pixmap, window, gc, 0, 0, width, height, 0,
    > >> 0);
    > >> break;
    > >> case ClientMessage:
    > >> return(0);
    > >> default:
    > >> break;
    > >> }
    > >> }

    >
    > >> }

    >
    > >> Questions:

    >
    > >> 1. when I close the window, instead of returning 0, the whole
    > >> application closes with the message X connection to :0.0 broken
    > >> (explicit kill or server shutdown). What am I doing wrong?

    >
    > >> 2. I want the window to be of fixed size (non-resizable), how can I do
    > >> that?

    >
    > >> Thanks,

    >
    > >> Tamas

    >
    > > 1. You probably need to have your application call
    > > XCloseDisplay(display);

    >
    > There's a little more to it than that, though it depends on what
    > behavior is desired.
    >
    > [a] XCloseDisplay() will of course work; usually it's in response to a
    > user command (picked from a menu or a keypress or some such). If
    > one wants to intercept the window close (usually the 'x' in the upper
    > right, or in some cases the upper left, corner), one has to do more
    > involved things by looking for client messages after calling
    > XSetWMProtocols():
    >
    > XAtom prots[1];
    > prots[0] = XInternAtom(dpy, "WM_DELETE_WINDOW", false);
    > XSetWMProtocols(dpy, window, prots, 1);
    >
    > (or one can use XChangeProperty() on WM_PROTOCOLS directly).
    >
    > There are two other protocols that might be of interest:
    > WM_SAVE_YOURSELF and WM_TAKE_FOCUS.
    >
    > [b] Closing the display closes all windows.
    >
    >
    >
    >
    >
    > > 2.
    > > You could try setting attr.override_redirect = True;
    > > OR
    > > can use a windows manager hint so the windows manager doesn't try to
    > > reparent the window with resizing..
    > > I do something (for now) like:

    >
    > > NO_BORDER = XInternAtom(dpy, "_MOTIF_WM_HINTS", False);

    >
    > > mwmhints.flags = MWM_HINTS_DECORATIONS;
    > > mwmhints.decorations = 0;

    >
    > > XChangeProperty(dpy, window,
    > > NO_BORDER, NO_BORDER, 32, PropModeReplace, (unsigned char *)
    > > &mwmhints, 5);

    >
    > > You should be able to improve this. Also this removes the decorations
    > > all together which you may or may not want to do. Google window
    > > manager hints and override_redirect if that doesn't work.

    >
    > Not sure how standard this is; I'd have to look. :-)
    >
    > --
    > #191, ewi...@earthlink.net
    > "Woman? What woman?"
    >
    > --
    > Posted via a free Usenet account fromhttp://www.teranews.com


    XA_WM_TRANSIENT_FOR is another hint used for dialog boxes.

    Section 12.3.1.4.6 of the Xlib Programming Manual, Rel. 5 by Adrian
    Nye talks about reparenting.

    You can read/search it on:
    http://books.google.com


+ Reply to Thread