Could somebody please help me with this program - Xwindows

This is a discussion on Could somebody please help me with this program - Xwindows ; Hi guys, I'm new at X-Windows, and faily new at C I was hoping somebody could tellme why the listed program doesn't work #include #include #include #include #include #define NIL (0); void waite(dpy) Display *dpy; { for (; { printf("Waiting ...

+ Reply to Thread
Results 1 to 6 of 6

Thread: Could somebody please help me with this program

  1. Could somebody please help me with this program

    Hi guys, I'm new at X-Windows, and faily new at C

    I was hoping somebody could tellme why the listed program doesn't
    work
    #include
    #include
    #include
    #include
    #include
    #define NIL (0);
    void waite(dpy)
    Display *dpy;
    {
    for (;{
    printf("Waiting for event\n");
    XEvent e;
    XNextEvent (dpy,&e);
    if (e.type ==MapNotify||1)
    break;
    }
    }
    XGCValues values,values2;

    int main (){

    XVisualInfo vi;
    Display *dpy=NULL;
    dpy= XOpenDisplay(NULL);
    if (dpy == NULL) {
    printf("Error opening display \n");
    exit (1);
    }

    int screen=DefaultScreen(dpy);
    XVisualInfo colorvi,greyvi;
    XVisualInfo *xvlist;
    XSetWindowAttributes xsa;
    Colormap colormap;
    ;
    greyvi.class=StaticGray; //I'm going to have two windows so I want to
    setup the visual for greyscale and 24bit color
    greyvi.depth=8; //One window will have the color input, minus the
    keyed background, the order the cooked greyscale pic
    colorvi.class=TrueColor;
    colorvi.depth=24;
    int rvi;
    int valuem;


    xvlist=XGetVisualInfo(dpy,(VisualDepthMask
    |VisualClassMask),&colorvi,&rvi);
    if (rvi>1){ //check to see if the depth and class have one or more
    visual that we can use
    printf("Found %d different visuals that I could use \n");} //Tell
    the user how many modes that are
    else
    {
    printf("Could not find a single visual to use \n");} //Well I guess
    you can guess, wait nono I know you can guess
    colormap=XCreateColormap
    (dpy,RootWindow(dpy,screen),xvlist[0].visual,AllocNone);
    xsa.colormap=colormap;
    valuem|=CWColormap;

    FILE* filehandle=openpbm("pic.pbm","r");
    magic(filehandle);
    int width=initw(filehandle);
    int height=inith(filehandle);

    long int headsize=0;
    long int filesize=rgetsize(filehandle,&headsize);
    fseek(filehandle,0,SEEK_SET);
    unsigned char *buff=malloc(filesize);
    readb(buff,filesize,filehandle);

    Window w=XCreateWindow(dpy,DefaultRootWindow(dpy),0,0,wid th,height,0,colorvi.depth,InputOutput,xvlist[0].visual,0,&xsa);
    if (w!=0){
    printf("Error creating window\n");
    }

    XSelectInput(dpy,w,ExposureMask | ButtonPressMask);
    XMapWindow(dpy,w);
    waite(dpy);
    GC gc,gc2;

    unsigned long valuemask;
    values.foreground = BlackPixel(dpy,screen);
    values.background = WhitePixel(dpy,screen);
    values2.background = BlackPixel(dpy,screen);
    values2.foreground = WhitePixel(dpy,screen);
    gc = XCreateGC(dpy,RootWindow(dpy,screen),(GCForeground |
    GCBackground),&values);
    gc2 = XCreateGC(dpy,RootWindow(dpy,screen),(GCForeground |
    GCBackground),&values2);
    XFillRectangle(dpy,w,gc,0,0,width,height);
    //XFillRectangle(dpy,w,gc2,0,0,100,100);
    XFlush(dpy);


    struct _XImage vimage;
    vimage.height = height;
    vimage.width = width;
    vimage.format = ZPixmap;
    vimage.data = buff;
    vimage.byte_order=MSBFirst;
    vimage.bitmap_unit=32;
    vimage.bitmap_bit_order=MSBFirst;
    vimage.bitmap_pad=32;
    vimage.depth=24;
    vimage.bits_per_pixel=32;
    vimage.bytes_per_line=0;
    printf("\n%d Bytes per line before xinit\n",vimage.bytes_per_line);

    XInitImage(&vimage);
    XPutImage (dpy,screen,gc2,&vi,0,0,0,0,width,height);
    XFlush(dpy);
    //waite(dpy);
    printf("Bytes %d\n",vimage.bytes_per_line);


    sleep(10);
    }

  2. Re: Could somebody please help me with this program

    On 27 Oct 2003 09:16:56 -0800, adam.dickson@sympatico.ca (Adam Dickson) wrote:

    >Hi guys, I'm new at X-Windows, and faily new at C
    >
    > I was hoping somebody could tellme why the listed program doesn't
    >work


    How do you mean "doesn't work"?

    >#include
    >#include
    >#include
    >#include
    >#include
    >#define NIL (0);
    >void waite(dpy)
    >Display *dpy;


    I see you're using a K&R C compiler instead of a more modern compiler.

    >{
    >for (;{
    > printf("Waiting for event\n");
    > XEvent e;

    Neither K&R nor C89 permit variables to be declared after the first statement of
    a block. Either move this declaration to before the printf() or enclose it (and
    the related statements in a statement block.

    > XNextEvent (dpy,&e);
    > if (e.type ==MapNotify||1)

    This probably doesn't do what you think it does.
    It compares e.type to 1 (the results of the logical or is the truth value of the
    two or'ed expressions. In this case, the result will be 1 when either MapNotify
    is non-zero _or_ 1 is non-zero. Since 1 is always non-sero, the value will
    always be true)

    > break;
    > }
    >}
    >XGCValues values,values2;
    >
    >int main (){
    >
    > XVisualInfo vi;
    > Display *dpy=NULL;
    > dpy= XOpenDisplay(NULL);
    > if (dpy == NULL) {
    > printf("Error opening display \n");
    > exit (1);
    > }
    >
    > int screen=DefaultScreen(dpy);

    Not valid with K&R C or C89.

    > XVisualInfo colorvi,greyvi;

    Not valid with K&R C or C89.

    > XVisualInfo *xvlist;

    Not valid with K&R C or C89.

    > XSetWindowAttributes xsa;

    Not valid with K&R C or C89.

    > Colormap colormap;

    Not valid with K&R C or C89.

    > ;

    Null statement. Although this doesn't harm the code, it doesn't help it either.

    > greyvi.class=StaticGray; //I'm going to have two windows so I want to
    >setup the visual for greyscale and 24bit color

    Double slash comments are not valid in K&R C

    > greyvi.depth=8; //One window will have the color input, minus the
    >keyed background, the order the cooked greyscale pic

    Double slash comments are not valid in K&R C

    > colorvi.class=TrueColor;
    > colorvi.depth=24;
    > int rvi;

    Not valid with K&R C or C89.

    > int valuem;

    Not valid with K&R C or C89.
    >
    >
    > xvlist=XGetVisualInfo(dpy,(VisualDepthMask
    >|VisualClassMask),&colorvi,&rvi);
    > if (rvi>1){ //check to see if the depth and class have one or more
    >visual that we can use

    Double slash comments are not valid in K&R C

    > printf("Found %d different visuals that I could use \n");} //Tell
    >the user how many modes that are

    Double slash comments are not valid in K&R C

    > else
    > {
    > printf("Could not find a single visual to use \n");} //Well I guess
    >you can guess, wait nono I know you can guess

    Double slash comments are not valid in K&R C

    > colormap=XCreateColormap
    >(dpy,RootWindow(dpy,screen),xvlist[0].visual,AllocNone);
    > xsa.colormap=colormap;
    > valuem|=CWColormap;
    >
    > FILE* filehandle=openpbm("pic.pbm","r");

    Not valid with K&R C or C89.

    > magic(filehandle);
    > int width=initw(filehandle);

    Not valid with K&R C or C89.

    > int height=inith(filehandle);

    Not valid with K&R C or C89.

    >
    > long int headsize=0;

    Not valid with K&R C or C89.

    > long int filesize=rgetsize(filehandle,&headsize);

    Not valid with K&R C or C89.

    > fseek(filehandle,0,SEEK_SET);
    > unsigned char *buff=malloc(filesize);

    Not valid with K&R C or C89.

    > readb(buff,filesize,filehandle);
    >
    > Window w=XCreateWindow(dpy,DefaultRootWindow(dpy),0,0,wid th,height,0,colorvi.depth,InputOutput,xvlist[0].visual,0,&xsa);
    > if (w!=0){
    > printf("Error creating window\n");
    > }
    >
    > XSelectInput(dpy,w,ExposureMask | ButtonPressMask);
    > XMapWindow(dpy,w);
    > waite(dpy);
    > GC gc,gc2;

    Not valid with K&R C or C89.

    >
    > unsigned long valuemask;

    Not valid with K&R C or C89.

    > values.foreground = BlackPixel(dpy,screen);
    > values.background = WhitePixel(dpy,screen);
    > values2.background = BlackPixel(dpy,screen);
    > values2.foreground = WhitePixel(dpy,screen);
    > gc = XCreateGC(dpy,RootWindow(dpy,screen),(GCForeground |
    >GCBackground),&values);
    > gc2 = XCreateGC(dpy,RootWindow(dpy,screen),(GCForeground |
    >GCBackground),&values2);
    > XFillRectangle(dpy,w,gc,0,0,width,height);
    > //XFillRectangle(dpy,w,gc2,0,0,100,100);

    Double slash comments are not valid in K&R C

    > XFlush(dpy);
    >
    >
    > struct _XImage vimage;

    Not valid with K&R C or C89.

    > vimage.height = height;
    > vimage.width = width;
    > vimage.format = ZPixmap;
    > vimage.data = buff;
    > vimage.byte_order=MSBFirst;
    > vimage.bitmap_unit=32;
    > vimage.bitmap_bit_order=MSBFirst;
    > vimage.bitmap_pad=32;
    > vimage.depth=24;
    > vimage.bits_per_pixel=32;
    > vimage.bytes_per_line=0;
    > printf("\n%d Bytes per line before xinit\n",vimage.bytes_per_line);
    >
    > XInitImage(&vimage);
    > XPutImage (dpy,screen,gc2,&vi,0,0,0,0,width,height);
    > XFlush(dpy);
    > //waite(dpy);

    Double slash comments are not valid in K&R C

    > printf("Bytes %d\n",vimage.bytes_per_line);
    >
    >
    > sleep(10);
    >}



    Get past your problems with the language first (decide on what version of C you
    want to use, and code to that version), then revisit your problem for logic
    errors.

    --
    Lew Pitcher
    IT Consultant, Enterprise Technology Solutions
    Toronto Dominion Bank Financial Group

    (Opinions expressed are my own, not my employers')

  3. Re: Could somebody please help me with this program

    >
    > Get past your problems with the language first (decide on what version of C you
    > want to use, and code to that version), then revisit your problem for logic
    > errors.


    Well, please excuse me. I've done much programming but I just started
    with C last week, and XWindows yesterday, everything works (including
    the event handler, it does what I want it to. Get's the Map, and then
    waits for an event when I call it without a map pending) except when I
    call XPutImage nothing appears on the window

    I'm not sure if my problem is with the colormap, gc, or my XPutImage .
    I really don't know, maybe somebody could give me a _simple_ explain
    of a file that would read a pbm and display it, or just tell me what
    I'm doing wrong (Please don't tell me that my style is wrong, if it
    complies with gcc that's all I need. My style will improve when I've
    been at it for more than a week)

  4. Re: Could somebody please help me with this program



    Adam Dickson wrote:
    >
    > Hi guys, I'm new at X-Windows, and faily new at C
    >
    > I was hoping somebody could tellme why the listed program doesn't
    > work
    > #include
    > #include
    > #include
    > #include
    > #include
    > #define NIL (0);
    > void waite(dpy)
    > Display *dpy;
    > {
    > for (;{
    > printf("Waiting for event\n");
    > XEvent e;
    > XNextEvent (dpy,&e);
    > if (e.type ==MapNotify||1)
    > break;


    Hmmm... You never do anything with the one event you will receive...

    > }
    > }
    > XGCValues values,values2;
    >
    > int main (){
    >
    > XVisualInfo vi;
    > Display *dpy=NULL;
    > dpy= XOpenDisplay(NULL);
    > if (dpy == NULL) {
    > printf("Error opening display \n");
    > exit (1);
    > }
    >
    > int screen=DefaultScreen(dpy);
    > XVisualInfo colorvi,greyvi;
    > XVisualInfo *xvlist;
    > XSetWindowAttributes xsa;
    > Colormap colormap;
    > ;
    > greyvi.class=StaticGray; //I'm going to have two windows so I want to
    > setup the visual for greyscale and 24bit color
    > greyvi.depth=8; //One window will have the color input, minus the
    > keyed background, the order the cooked greyscale pic
    > colorvi.class=TrueColor;
    > colorvi.depth=24;
    > int rvi;
    > int valuem;
    >
    > xvlist=XGetVisualInfo(dpy,(VisualDepthMask
    > |VisualClassMask),&colorvi,&rvi);
    > if (rvi>1){ //check to see if the depth and class have one or more
    > visual that we can use
    > printf("Found %d different visuals that I could use \n");} //Tell
    > the user how many modes that are
    > else
    > {
    > printf("Could not find a single visual to use \n");} //Well I guess
    > you can guess, wait nono I know you can guess
    > colormap=XCreateColormap
    > (dpy,RootWindow(dpy,screen),xvlist[0].visual,AllocNone);
    > xsa.colormap=colormap;
    > valuem|=CWColormap;
    >
    > FILE* filehandle=openpbm("pic.pbm","r");
    > magic(filehandle);
    > int width=initw(filehandle);
    > int height=inith(filehandle);
    >
    > long int headsize=0;
    > long int filesize=rgetsize(filehandle,&headsize);
    > fseek(filehandle,0,SEEK_SET);
    > unsigned char *buff=malloc(filesize);
    > readb(buff,filesize,filehandle);
    >
    > Window w=XCreateWindow(dpy,DefaultRootWindow(dpy),0,0,wid th,height,0,colorvi.depth,InputOutput,xvlist[0].visual,0,&xsa);
    > if (w!=0){
    > printf("Error creating window\n");
    > }
    >
    > XSelectInput(dpy,w,ExposureMask | ButtonPressMask);
    > XMapWindow(dpy,w);
    > waite(dpy);
    > GC gc,gc2;
    >
    > unsigned long valuemask;
    > values.foreground = BlackPixel(dpy,screen);
    > values.background = WhitePixel(dpy,screen);
    > values2.background = BlackPixel(dpy,screen);
    > values2.foreground = WhitePixel(dpy,screen);
    > gc = XCreateGC(dpy,RootWindow(dpy,screen),(GCForeground |
    > GCBackground),&values);
    > gc2 = XCreateGC(dpy,RootWindow(dpy,screen),(GCForeground |
    > GCBackground),&values2);
    > XFillRectangle(dpy,w,gc,0,0,width,height);
    > //XFillRectangle(dpy,w,gc2,0,0,100,100);
    > XFlush(dpy);
    >
    > struct _XImage vimage;
    > vimage.height = height;
    > vimage.width = width;
    > vimage.format = ZPixmap;
    > vimage.data = buff;
    > vimage.byte_order=MSBFirst;
    > vimage.bitmap_unit=32;
    > vimage.bitmap_bit_order=MSBFirst;
    > vimage.bitmap_pad=32;
    > vimage.depth=24;
    > vimage.bits_per_pixel=32;
    > vimage.bytes_per_line=0
    > printf("\n%d Bytes per line before xinit\n",vimage.bytes_per_line
    >
    > XInitImage(&vimage);
    > XPutImage (dpy,screen,gc2,&vi,0,0,0,0,width,height);


    ERROR - vi has been declared, but not initialized to anything.

    > XFlush(dpy);
    > //waite(dpy);
    > printf("Bytes %d\n",vimage.bytes_per_line);
    >
    > sleep(10);
    > }


    See the error when you call XPutImage above.

    Your approach here will not work well in a general case. I really
    suggest you use Xt, and Motif or some other toolkit, rather than just X.

    Your design should look something like this:

    int main()
    }
    /* Initialize */
    ...

    /* Create your interface */
    ...

    /* Register some event handlers or callbacks to handle events */
    ...

    /* Realize your top-level window */
    ...

    /* Enter the infinite event loop, which will invoke your registered
    * event handlers and/or callbacks when events happen.
    *
    * Using Xt, this would be (where 'context' has been declared, and
    set in the
    * set in the initialization):
    * XtAppMainLoop(context);
    */

    return 0; /* Never executed, but satisfies compiler */
    }

    /* Then write the event handlers and callbacks */


    --
    Fred L. Kleinschmidt
    Boeing Associate Technical Fellow
    Technical Architect, Common User Interface Services
    M/S 2R-94 (206)544-5225

  5. Re: Could somebody please help me with this program

    Hi Lew,

    > > XNextEvent (dpy,&e);
    > > if (e.type ==MapNotify||1)

    >
    > This probably doesn't do what you think it does. It compares e.type
    > to 1 (the results of the logical or is the truth value of the two
    > or'ed expressions. In this case, the result will be 1 when either
    > MapNotify is non-zero _or_ 1 is non-zero. Since 1 is always non-sero,
    > the value will always be true)


    Doesn't it compare e.type to MapNotify and if they're not equal take the
    result of the expression `1', i.e.

    if ((e.type == MapNotify) || 1)

    It's commonly used when debugging code, e.g. given

    if (some long expression) {

    it's changed to

    if (1 || some long expression) {

    to test the `then' part of the if-statement.

    Cheers,

    --
    Ralph Corderoy. http://inputplus.co.uk/ralph/ http://troff.org/

  6. Re: Could somebody please help me with this program

    > ERROR - vi has been declared, but not initialized to anything.
    >


    Yeah, I found that out late last night by myself, it should be
    vimage, not vi
    Thanks for the help anyway. The messed event handler does work, for
    what I want.
    Does Motif come stock with Slackware dis ??
    I thought of that, but since my program (right now) looks like it's
    only going
    to have 3 windows, two pics, and a text status display, I figure
    I'll do it with
    native calls. There will be no menubars, nothing, just three windows
    will graphics and text display on the third, it won't even receive
    most clicks or keyboard input.

+ Reply to Thread