Tutorial program doesn't work - Xwindows

This is a discussion on Tutorial program doesn't work - Xwindows ; I got this code from a simple tutorial, but it doesn't work. It doesn't draw a line, but it displays a window. What's the problem? //gcc simplex.c -L/usr/X11R6/lib -lX11 #include #include #include int main(){ Display *dpy = XOpenDisplay(NULL); assert(dpy); int ...

+ Reply to Thread
Results 1 to 8 of 8

Thread: Tutorial program doesn't work

  1. Tutorial program doesn't work

    I got this code from a simple tutorial, but it doesn't work.
    It doesn't draw a line, but it displays a window.
    What's the problem?

    //gcc simplex.c -L/usr/X11R6/lib -lX11

    #include
    #include
    #include

    int main(){
    Display *dpy = XOpenDisplay(NULL);
    assert(dpy);

    int blackColor = BlackPixel(dpy, DefaultScreen(dpy));
    int whiteColor = WhitePixel(dpy, DefaultScreen(dpy));

    // Create the window
    Window w = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0,
    200, 100, 0, blackColor, blackColor);

    // We want to get MapNotify events
    XSelectInput(dpy, w, StructureNotifyMask);

    // "Map" the window (that is, make it appear on the screen)
    XMapWindow(dpy, w);

    // Create a "Graphics Context"
    GC gc = XCreateGC(dpy, w, 0, NULL);

    // Tell the GC we draw using the white color
    XSetForeground(dpy, gc, whiteColor);

    // Wait for the MapNotify event
    for(; {
    XEvent e;
    XNextEvent(dpy, &e);
    if (e.type == MapNotify)
    break;
    }


    // Draw the line
    XDrawLine(dpy, w, gc, 10, 60, 180, 20);

    // Send the "DrawLine" request to the server
    XFlush(dpy);

    // Wait for 10 seconds
    sleep(10);

    return 0;
    }


  2. Re: Tutorial program doesn't work

    ulvinge@gmail.com wrote:

    > I got this code from a simple tutorial, but it doesn't work.
    > It doesn't draw a line, but it displays a window.
    > What's the problem?


    The code isn't processing events after the line is drawn.


  3. Re: Tutorial program doesn't work

    In comp.windows.x, ulvinge@gmail.com

    wrote
    on 11 Aug 2006 10:22:33 -0700
    <1155316953.588887.323770@75g2000cwc.googlegroups.c om>:
    > I got this code from a simple tutorial, but it doesn't work.
    > It doesn't draw a line, but it displays a window.
    > What's the problem?
    >
    > //gcc simplex.c -L/usr/X11R6/lib -lX11
    >
    > #include
    > #include
    > #include
    >
    > int main(){
    > Display *dpy = XOpenDisplay(NULL);
    > assert(dpy);
    >
    > int blackColor = BlackPixel(dpy, DefaultScreen(dpy));
    > int whiteColor = WhitePixel(dpy, DefaultScreen(dpy));
    >
    > // Create the window
    > Window w = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0,
    > 200, 100, 0, blackColor, blackColor);
    >
    > // We want to get MapNotify events
    > XSelectInput(dpy, w, StructureNotifyMask);
    >
    > // "Map" the window (that is, make it appear on the screen)
    > XMapWindow(dpy, w);
    >
    > // Create a "Graphics Context"
    > GC gc = XCreateGC(dpy, w, 0, NULL);
    >
    > // Tell the GC we draw using the white color
    > XSetForeground(dpy, gc, whiteColor);
    >
    > // Wait for the MapNotify event
    > for(; {
    > XEvent e;
    > XNextEvent(dpy, &e);
    > if (e.type == MapNotify)
    > break;
    > }
    >
    >
    > // Draw the line
    > XDrawLine(dpy, w, gc, 10, 60, 180, 20);
    >
    > // Send the "DrawLine" request to the server
    > XFlush(dpy);
    >
    > // Wait for 10 seconds
    > sleep(10);
    >
    > return 0;
    > }
    >


    This program will not work as written, for various reasons. A
    modification to the Event processing loop, and the XSelectInput(),
    however, should fix at least part of the problem:

    XSelectInput(dpy, w, ExposureMask);

    ....

    for(;
    {
    XEvent e;
    XNextEvent(dpy, &e);

    switch(e.type)
    {
    default:
    break;
    case ExposeEvent:
    XDrawLine(dpy, w, gc, 10, 60, 160, 20);
    break;
    }
    }

    The MapNotify event might be useful but not really all that important
    for drawing a line. Note that an Expose event can also determine what
    part of the window to redraw, and that this modified program will never
    exit until the window is closed, which will cause a "broken connection"
    error.

    If you want the program to terminate after 10 seconds for some reason,
    you can either set an alarm(10) and not catch it (or catch it and exit),
    or use a more sophisticated event loop with select() and
    ConnectionNumber(dpy).

    --
    #191, ewill3@earthlink.net
    Windows Vista. Because it's time to refresh your hardware. Trust us.

  4. Re: Tutorial program doesn't work


    > This program will not work as written, for various reasons. A
    > modification to the Event processing loop, and the XSelectInput(),
    > however, should fix at least part of the problem:
    >
    > XSelectInput(dpy, w, ExposureMask);
    >
    > ...
    >
    > for(;
    > {
    > XEvent e;
    > XNextEvent(dpy, &e);
    >
    > switch(e.type)
    > {
    > default:
    > break;
    > case ExposeEvent:
    > XDrawLine(dpy, w, gc, 10, 60, 160, 20);
    > break;
    > }
    > }
    >
    > The MapNotify event might be useful but not really all that important
    > for drawing a line. Note that an Expose event can also determine what
    > part of the window to redraw, and that this modified program will never
    > exit until the window is closed, which will cause a "broken connection"
    > error.
    >
    > If you want the program to terminate after 10 seconds for some reason,
    > you can either set an alarm(10) and not catch it (or catch it and exit),
    > or use a more sophisticated event loop with select() and
    > ConnectionNumber(dpy).


    Thanks, but I get an error...

    simplex.c:40: error: `ExposeEvent' undeclared

    I'm a noob on x, and I took the code from a tutorial, lol!

    What is ExposeEvent , or is it part of some include file?


  5. Re: Tutorial program doesn't work


    "IDK" wrote in message
    news:1155585932.659983.282120@i42g2000cwa.googlegr oups.com...
    >
    >> This program will not work as written, for various reasons. A
    >> modification to the Event processing loop, and the XSelectInput(),
    >> however, should fix at least part of the problem:
    >>
    >> XSelectInput(dpy, w, ExposureMask);
    >>
    >> ...
    >>
    >> for(;
    >> {
    >> XEvent e;
    >> XNextEvent(dpy, &e);
    >>
    >> switch(e.type)
    >> {
    >> default:
    >> break;
    >> case ExposeEvent:
    >> XDrawLine(dpy, w, gc, 10, 60, 160, 20);
    >> break;
    >> }
    >> }
    >>
    >> The MapNotify event might be useful but not really all that important
    >> for drawing a line. Note that an Expose event can also determine what
    >> part of the window to redraw, and that this modified program will never
    >> exit until the window is closed, which will cause a "broken connection"
    >> error.
    >>
    >> If you want the program to terminate after 10 seconds for some reason,
    >> you can either set an alarm(10) and not catch it (or catch it and exit),
    >> or use a more sophisticated event loop with select() and
    >> ConnectionNumber(dpy).

    >
    > Thanks, but I get an error...
    >
    > simplex.c:40: error: `ExposeEvent' undeclared
    >
    > I'm a noob on x, and I took the code from a tutorial, lol!
    >
    > What is ExposeEvent , or is it part of some include file?


    There is no such thing as ExposeEvent. It should be just plain Expose
    The event type macros are in X.h (but you should include Xlib.h instead -
    it includes X.h internally)
    --
    Fred L. Kleinschmidt
    Boeing Associate Technical Fellow
    Technical Architect, Software Reuse Project




  6. Re: Tutorial program doesn't work

    In comp.windows.x, Fred Kleinschmidt

    wrote
    on Tue, 15 Aug 2006 14:25:49 GMT
    :
    >
    > "IDK" wrote in message
    > news:1155585932.659983.282120@i42g2000cwa.googlegr oups.com...
    >>
    >>> This program will not work as written, for various reasons. A
    >>> modification to the Event processing loop, and the XSelectInput(),
    >>> however, should fix at least part of the problem:
    >>>
    >>> XSelectInput(dpy, w, ExposureMask);
    >>>
    >>> ...
    >>>
    >>> for(;
    >>> {
    >>> XEvent e;
    >>> XNextEvent(dpy, &e);
    >>>
    >>> switch(e.type)
    >>> {
    >>> default:
    >>> break;
    >>> case ExposeEvent:
    >>> XDrawLine(dpy, w, gc, 10, 60, 160, 20);
    >>> break;
    >>> }
    >>> }
    >>>
    >>> The MapNotify event might be useful but not really all that important
    >>> for drawing a line. Note that an Expose event can also determine what
    >>> part of the window to redraw, and that this modified program will never
    >>> exit until the window is closed, which will cause a "broken connection"
    >>> error.
    >>>
    >>> If you want the program to terminate after 10 seconds for some reason,
    >>> you can either set an alarm(10) and not catch it (or catch it and exit),
    >>> or use a more sophisticated event loop with select() and
    >>> ConnectionNumber(dpy).

    >>
    >> Thanks, but I get an error...
    >>
    >> simplex.c:40: error: `ExposeEvent' undeclared
    >>
    >> I'm a noob on x, and I took the code from a tutorial, lol!
    >>
    >> What is ExposeEvent , or is it part of some include file?

    >
    > There is no such thing as ExposeEvent.


    Yep, typo on my part; apologies.

    As for the question: each event refers to something that the program is
    expected to do something with. An Expose is primarily concerned with
    redrawing parts of a window. MapNotify tells the app that X got around
    to making the window available (it needn't be mapped immediately, though
    it's usually not a problem). Expose and MapNotify, as well as
    ExposureMask, are all #defined in X.h.

    There is a xexpose.count field on the XEvent in an Expose, which might
    be of some use in optimizing redraws. One can either maintain a Region
    or array of Rectangles and redraw only when the array gets full or the
    count is full, or erase and redraw the whole window. For such a simple
    program as the above it's overkill for now.

    > It should be just plain Expose
    > The event type macros are in X.h (but you should include Xlib.h instead -
    > it includes X.h internally)


    I'd include both just to make sure. :-) In any event, they're
    guardbanded so it's perfectly safe.

    --
    #191, ewill3@earthlink.net
    Windows Vista. Because it's time to refresh your hardware. Trust us.

  7. Re: Tutorial program doesn't work

    Thanks all of you
    That solved everything, now I can begin programming for real

    But how could the guy who wrote the turorial be so crappy that his code
    didn't even work.
    He maybe wrote it for some older X11 release...

    The code looks very like windows programming for windows...


  8. Re: Tutorial program doesn't work

    In comp.windows.x, IDK

    wrote
    on 19 Aug 2006 02:19:35 -0700
    <1155979175.074050.213900@m79g2000cwm.googlegroups. com>:
    > Thanks all of you
    > That solved everything, now I can begin programming for real
    >
    > But how could the guy who wrote the turorial be so crappy that his code
    > didn't even work.
    > He maybe wrote it for some older X11 release...
    >
    > The code looks very like windows programming for windows...
    >


    You'd have to take it up with him. :-) In any event, there should be
    a few X tutorials out there.

    --
    #191, ewill3@earthlink.net
    Windows Vista. Because it's time to refresh your hardware. Trust us.

+ Reply to Thread