Re: Looking for xlib programing help.... - Xwindows

This is a discussion on Re: Looking for xlib programing help.... - Xwindows ; Thanks. This is the information I was looking for. I didn't know where to post the question before. I will post a code example shortly. "John Tsiombikas" wrote in message news:slrnfpdcil.7g2.nuclear@nuclear.dnsalias.com.. . > On 2008-01-23, Dwayne Dilbeck wrote: >> >> ...

+ Reply to Thread
Results 1 to 6 of 6

Thread: Re: Looking for xlib programing help....

  1. Re: Looking for xlib programing help....

    Thanks. This is the information I was looking for. I didn't know where to
    post the question before. I will post a code example shortly.
    "John Tsiombikas" wrote in message
    news:slrnfpdcil.7g2.nuclear@nuclear.dnsalias.com.. .
    > On 2008-01-23, Dwayne Dilbeck wrote:
    >>
    >> I have an application that was written in "C" and uses xlib. The
    >> application works as expected on Sparc Solaris 7,8,9 and 10.
    >>
    >> The application now needs to be ported to red hat 4.0. The obvious
    >> changes
    >> to library linking have been made . The original application compiles
    >> with
    >> 0 Errors and 0 Warnings. But the application does not work. It launches
    >> and
    >> hangs in the event loop.
    >>
    >> When using a debuger the hang occurs with XNextEvent(...). XNextEvent
    >> will
    >> wait if there is nothing in the queue. I rewrote the event algorythm to
    >> use
    >> XCheckWindowEvent(...) The resulted in something wierd the code worked
    >> as
    >> expected as long as there were not any events. If it recieved an event
    >> it
    >> was looking for the program hangs inside of XCheckWindowEvent.
    >>

    >
    > XNextEvent is supposed to block until there is at least one X event. I
    > really doubt that it works otherwise on Solaris If you want to poll
    > events, you may use the XPending() function in a loop, so that you call
    > XNextEvent only if there are pending events, thus XNextEvent wouldn't
    > block.
    >
    > I don't think I can help any more without any more information on the
    > expected behaviour, how it's supposed to work, and what it's actually
    > doing. Of course a *minimal* code sample that duplicates your problem
    > would be helpful as well.
    >
    > P.S. Such questions are mostly topical on comp.windows.x. I did set
    > followup-to accordingly.
    >
    > --
    > John Tsiombikas (Nuclear / Mindlapse)
    > http://nuclear.sdf-eu.org/




  2. Re: Looking for xlib programing help....

    I hate it when this happens, I spend days racking my brains as to why
    something isn't working. I even had co-workers examining the code. (Granted
    they don't know anything about Xlib. ) Then when I go to get outside help,
    the stupid code starts working.

    Well here is the code anyways and I welcome comments:

    This code creates a rectange window, with a black outline and a white
    center. When compiled with the socket code, it waits for a X,Y,Color
    pixel to be sent. When recieved it displays the pixel to the screen. At
    any time "q" in the small window will exit.

    This code originally worked on Sparc Solaris 7,8,9,10. But would only
    display a Black rectangle and not respond to "q" on Red Hat 9. Now it seems
    to work on Red Hat 9. I will have to wait until tomorrow to verify full
    functionality.


    /*
    * OLEDsim program:
    *
    */


    /* Define my includes */
    #include
    #include
    #include
    #include
    #include
    #include


    /* Set up global variables, I am lazy and don't want to pass them properly
    */
    Display *mydisplay;
    Window mywindow;
    GC mygc,myyellow;

    XEvent myevent;
    KeySym mykey;
    int myscreen;
    unsigned long myforeground, mybackground;
    int SCALE=1;


    /* Hard coded pixel values array, this will need to be changed later */
    int points[128][64];

    /* Define a buffer to hold characters */
    char text[10]="";


    /* Declare my socket_server procedure */
    /* code for the socket transactions not included here */
    extern void* socket_server(void* inputs);



    /* a function to draw individual pixels, a pixel location(x,y)
    * along with it's color value are passed via socket communication
    * this function will draw an individual pixel at a time
    */

    int draw_point(int x, int y, unsigned long color) {
    /* decalre loop variables */
    int count;
    int count2;

    /* make use of the global variables to set the incoming color for drawing
    */
    XSetForeground(mydisplay,myyellow,color);
    for(count =0;count for(count2=0;count2 XDrawPoint(myevent.xexpose.display,myevent.xexpose .window,
    myyellow,(x*SCALE+count)+20*SCALE,(y*SCALE+count2) +20*SCALE);
    XFlush(myevent.xexpose.display);
    return 0;
    }


    /* The Xwindow event loop, taken from an online "Hello World" example
    * The loop has been pulled from the main function to work in parallel
    * to the socket server
    */
    void* eventloop(void* inputs)
    {
    int done = 0;
    int x,y,i;

    #ifdef DEBUG_TXT
    fprintf(stdout,"Starting event queuee\n");
    #endif

    while (done == 0) {
    #ifdef WORKAROUND_ATTEMPT
    if ( XCheckWindowEvent(mydisplay,mywindow,
    ButtonPressMask|KeyPressMask|ExposureMask,
    &myevent))
    {
    #else
    XNextEvent(mydisplay,&myevent);
    #endif

    switch(myevent.type) {
    case Expose: /* Repaint window on expose */
    if (myevent.xexpose.count==0)
    XFillRectangle(myevent.xexpose.display,myevent.xex pose.window,
    mygc,20*SCALE,20*SCALE, myhint.width-40*SCALE,
    myhint.height -40*SCALE);
    for (x=0;x<128;x++)
    for(y=0;y<64;y++)
    draw_point(x, y, points[x][y]);
    break;


    case KeyPress: /* Process key press - quit on q: */
    i = XLookupString((XKeyEvent *)&myevent,text,10,&mykey,0);
    if (i==1 && text[0]=='q') done = 1;
    break;
    }
    //}

    }
    fprintf(stdout,"Exiting event Queue");
    XFreeGC(mydisplay,mygc);
    XDestroyWindow(mydisplay,mywindow);
    XCloseDisplay(mydisplay);
    pthread_exit(NULL);
    }

    int main(
    int argc, char **argv)
    {

    int x,y=0;


    //int done;
    char *hello = "Hello World";
    int thr_id;
    void* thr_retval;
    void* thr_retval2;
    pthread_t p_thread[2];

    if(argc > 2)
    if(strcmp(argv[1],"-scale")==0)
    SCALE = atoi(argv[2]);
    /* initialize video array */
    for (x=0;x<128; x++)
    for(y=0;y<64;y++)
    points[x][y]= 0x0001FFFF;

    mydisplay = XOpenDisplay("");
    myscreen = DefaultScreen(mydisplay);
    myforeground = WhitePixel(mydisplay,myscreen);
    mybackground = BlackPixel(mydisplay,myscreen);

    /* Suggest where to position the window: */
    myhint.x = 200;
    myhint.y = 200;
    myhint.width = 168*SCALE;
    myhint.height = 104*SCALE;
    myhint.flags = PPosition | PSize;

    /* Create a window - not displayed yet however: */
    mywindow = XCreateSimpleWindow(mydisplay,DefaultRootWindow(my display),
    myhint.x,myhint.y,myhint.width,
    myhint.height,5,myforeground,
    mybackground);

    XSetStandardProperties(mydisplay,mywindow,hello,he llo,None,argv,argc,&myhint
    );

    /* Create a Graphics Context (GC) for the window: */
    mygc = XCreateGC(mydisplay,mywindow,0,0);
    myyellow= XCreateGC(mydisplay,mywindow,0,0);
    XSetBackground(mydisplay,mygc,mybackground);
    XSetBackground(mydisplay,myyellow,myforeground);
    /* 0x0001F800 -> bright red
    0x0000F000 -> red
    0x00000F00 -> green
    0x00000800 -> blackish
    0x000007E0 -> bright green 6 Bits Green
    0x000003E0 -> green
    0x000000F0 -> blue
    0x00000020 -> blackish
    0x0000001f -> bright blue
    0x00000010 -> light blue
    0x0000000F -> blue
    0x00000002 -> blackish 5 bits BLUE

    */
    XSetForeground(mydisplay,myyellow,0x0001F800);
    XSetForeground(mydisplay,mygc,myforeground);


    /* Select input devices to listen to: */
    XSelectInput(mydisplay,mywindow,ButtonPressMask|Ke yPressMask|ExposureMask);


    /************************************************** ****************/
    /* Actually display the window: */
    /************************************************** ****************/
    XMapRaised(mydisplay,mywindow);


    /* Main Event Loop: This is the core of any X program: */
    fprintf(stdout,"Starting thread\n");

    thr_id= pthread_create(&p_thread[0], NULL, socket_server, thr_retval);
    thr_id= pthread_create(&p_thread[1], NULL, eventloop, thr_retval2);

    #ifdef DEBUG_txt
    fprintf(stdout,"%d <- new thread ID\n",0);
    #endif

    pthread_join(p_thread[0],NULL);
    pthread_join(p_thread[1],NULL);
    fprintf(stdout,"Exiting program\n");


    return(0);
    }



    "Dwayne Dilbeck" wrote in message
    news:13pdefch3tdj36e@corp.supernews.com...
    > Thanks. This is the information I was looking for. I didn't know where
    > to
    > post the question before. I will post a code example shortly.
    > "John Tsiombikas" wrote in message
    > news:slrnfpdcil.7g2.nuclear@nuclear.dnsalias.com.. .
    >> On 2008-01-23, Dwayne Dilbeck wrote:
    >>>
    >>> I have an application that was written in "C" and uses xlib. The
    >>> application works as expected on Sparc Solaris 7,8,9 and 10.
    >>>
    >>> The application now needs to be ported to red hat 4.0. The obvious
    >>> changes
    >>> to library linking have been made . The original application compiles
    >>> with
    >>> 0 Errors and 0 Warnings. But the application does not work. It launches
    >>> and
    >>> hangs in the event loop.
    >>>
    >>> When using a debuger the hang occurs with XNextEvent(...). XNextEvent
    >>> will
    >>> wait if there is nothing in the queue. I rewrote the event algorythm to
    >>> use
    >>> XCheckWindowEvent(...) The resulted in something wierd the code worked
    >>> as
    >>> expected as long as there were not any events. If it recieved an event
    >>> it
    >>> was looking for the program hangs inside of XCheckWindowEvent.
    >>>

    >>
    >> XNextEvent is supposed to block until there is at least one X event. I
    >> really doubt that it works otherwise on Solaris If you want to poll
    >> events, you may use the XPending() function in a loop, so that you call
    >> XNextEvent only if there are pending events, thus XNextEvent wouldn't
    >> block.
    >>
    >> I don't think I can help any more without any more information on the
    >> expected behaviour, how it's supposed to work, and what it's actually
    >> doing. Of course a *minimal* code sample that duplicates your problem
    >> would be helpful as well.
    >>
    >> P.S. Such questions are mostly topical on comp.windows.x. I did set
    >> followup-to accordingly.
    >>
    >> --
    >> John Tsiombikas (Nuclear / Mindlapse)
    >> http://nuclear.sdf-eu.org/

    >
    >





  3. Re: Looking for xlib programing help....

    Dwayne Dilbeck wrote:
    > I hate it when this happens, I spend days racking my brains as to why
    > something isn't working. I even had co-workers examining the code. (Granted
    > they don't know anything about Xlib. ) Then when I go to get outside help,
    > the stupid code starts working.
    >
    > Well here is the code anyways and I welcome comments:
    >
    > This code creates a rectange window, with a black outline and a white
    > center. When compiled with the socket code, it waits for a X,Y,Color
    > pixel to be sent. When recieved it displays the pixel to the screen. At
    > any time "q" in the small window will exit.
    >
    > This code originally worked on Sparc Solaris 7,8,9,10. But would only
    > display a Black rectangle and not respond to "q" on Red Hat 9. Now it seems
    > to work on Red Hat 9. I will have to wait until tomorrow to verify full
    > functionality.
    >
    >
    > /*
    > * OLEDsim program:
    > *
    > */
    >
    >
    > /* Define my includes */
    > #include
    > #include
    > #include
    > #include
    > #include
    > #include
    >
    >
    > /* Set up global variables, I am lazy and don't want to pass them properly
    > */
    > Display *mydisplay;
    > Window mywindow;
    > GC mygc,myyellow;
    >
    > XEvent myevent;
    > KeySym mykey;
    > int myscreen;
    > unsigned long myforeground, mybackground;
    > int SCALE=1;
    >
    >
    > /* Hard coded pixel values array, this will need to be changed later */
    > int points[128][64];
    >
    > /* Define a buffer to hold characters */
    > char text[10]="";
    >
    >
    > /* Declare my socket_server procedure */
    > /* code for the socket transactions not included here */
    > extern void* socket_server(void* inputs);
    >
    >
    >
    > /* a function to draw individual pixels, a pixel location(x,y)
    > * along with it's color value are passed via socket communication
    > * this function will draw an individual pixel at a time
    > */
    >
    > int draw_point(int x, int y, unsigned long color) {
    > /* decalre loop variables */
    > int count;
    > int count2;
    >
    > /* make use of the global variables to set the incoming color for drawing
    > */
    > XSetForeground(mydisplay,myyellow,color);
    > for(count =0;count > for(count2=0;count2 > XDrawPoint(myevent.xexpose.display,myevent.xexpose .window,
    > myyellow,(x*SCALE+count)+20*SCALE,(y*SCALE+count2) +20*SCALE);
    > XFlush(myevent.xexpose.display);
    > return 0;
    > }
    >
    >
    > /* The Xwindow event loop, taken from an online "Hello World" example
    > * The loop has been pulled from the main function to work in parallel
    > * to the socket server
    > */
    > void* eventloop(void* inputs)
    > {
    > int done = 0;
    > int x,y,i;
    >
    > #ifdef DEBUG_TXT
    > fprintf(stdout,"Starting event queuee\n");
    > #endif
    >
    > while (done == 0) {
    > #ifdef WORKAROUND_ATTEMPT
    > if ( XCheckWindowEvent(mydisplay,mywindow,
    > ButtonPressMask|KeyPressMask|ExposureMask,
    > &myevent))
    > {
    > #else
    > XNextEvent(mydisplay,&myevent);
    > #endif
    >
    > switch(myevent.type) {
    > case Expose: /* Repaint window on expose */
    > if (myevent.xexpose.count==0)
    > XFillRectangle(myevent.xexpose.display,myevent.xex pose.window,
    > mygc,20*SCALE,20*SCALE, myhint.width-40*SCALE,
    > myhint.height -40*SCALE);
    > for (x=0;x<128;x++)
    > for(y=0;y<64;y++)
    > draw_point(x, y, points[x][y]);
    > break;
    >
    >
    > case KeyPress: /* Process key press - quit on q: */
    > i = XLookupString((XKeyEvent *)&myevent,text,10,&mykey,0);
    > if (i==1 && text[0]=='q') done = 1;
    > break;
    > }
    > //}
    >
    > }
    > fprintf(stdout,"Exiting event Queue");
    > XFreeGC(mydisplay,mygc);
    > XDestroyWindow(mydisplay,mywindow);
    > XCloseDisplay(mydisplay);
    > pthread_exit(NULL);
    > }
    >
    > int main(
    > int argc, char **argv)
    > {
    >
    > int x,y=0;
    >
    >
    > //int done;
    > char *hello = "Hello World";
    > int thr_id;
    > void* thr_retval;
    > void* thr_retval2;
    > pthread_t p_thread[2];
    >
    > if(argc > 2)
    > if(strcmp(argv[1],"-scale")==0)
    > SCALE = atoi(argv[2]);
    > /* initialize video array */
    > for (x=0;x<128; x++)
    > for(y=0;y<64;y++)
    > points[x][y]= 0x0001FFFF;
    >
    > mydisplay = XOpenDisplay("");
    > myscreen = DefaultScreen(mydisplay);
    > myforeground = WhitePixel(mydisplay,myscreen);
    > mybackground = BlackPixel(mydisplay,myscreen);
    >
    > /* Suggest where to position the window: */
    > myhint.x = 200;
    > myhint.y = 200;
    > myhint.width = 168*SCALE;
    > myhint.height = 104*SCALE;
    > myhint.flags = PPosition | PSize;
    >
    > /* Create a window - not displayed yet however: */
    > mywindow = XCreateSimpleWindow(mydisplay,DefaultRootWindow(my display),
    > myhint.x,myhint.y,myhint.width,
    > myhint.height,5,myforeground,
    > mybackground);
    >
    > XSetStandardProperties(mydisplay,mywindow,hello,he llo,None,argv,argc,&myhint
    > );
    >
    > /* Create a Graphics Context (GC) for the window: */
    > mygc = XCreateGC(mydisplay,mywindow,0,0);
    > myyellow= XCreateGC(mydisplay,mywindow,0,0);
    > XSetBackground(mydisplay,mygc,mybackground);
    > XSetBackground(mydisplay,myyellow,myforeground);
    > /* 0x0001F800 -> bright red
    > 0x0000F000 -> red
    > 0x00000F00 -> green
    > 0x00000800 -> blackish
    > 0x000007E0 -> bright green 6 Bits Green
    > 0x000003E0 -> green
    > 0x000000F0 -> blue
    > 0x00000020 -> blackish
    > 0x0000001f -> bright blue
    > 0x00000010 -> light blue
    > 0x0000000F -> blue
    > 0x00000002 -> blackish 5 bits BLUE
    >
    > */
    > XSetForeground(mydisplay,myyellow,0x0001F800);
    > XSetForeground(mydisplay,mygc,myforeground);
    >
    >
    > /* Select input devices to listen to: */
    > XSelectInput(mydisplay,mywindow,ButtonPressMask|Ke yPressMask|ExposureMask);
    >
    >
    > /************************************************** ****************/
    > /* Actually display the window: */
    > /************************************************** ****************/
    > XMapRaised(mydisplay,mywindow);
    >
    >
    > /* Main Event Loop: This is the core of any X program: */
    > fprintf(stdout,"Starting thread\n");
    >
    > thr_id= pthread_create(&p_thread[0], NULL, socket_server, thr_retval);
    > thr_id= pthread_create(&p_thread[1], NULL, eventloop, thr_retval2);
    >
    > #ifdef DEBUG_txt
    > fprintf(stdout,"%d <- new thread ID\n",0);
    > #endif
    >
    > pthread_join(p_thread[0],NULL);
    > pthread_join(p_thread[1],NULL);
    > fprintf(stdout,"Exiting program\n");
    >
    >
    > return(0);
    > }



    From the look of things you're using threads with Xlib without doing
    XInitThreads(). You may find that works better. Internally XNextEvent
    and other event queue X functions may call select() or poll() so if your
    socket_server() path does that too, you may experience weird bugs.


    George

  4. Re: Looking for xlib programing help....

    Interesting the book I have doesn't even list XInitThreads(). I will look
    it up on google.

    I have been getting my Xlib information from "XLib Programming Manual for
    version 11(R4/R5)" by Adrian Nye - O'Reilly & Associates, Inc. I don't
    even remember when I purchased this book. I believe I purchased it about 8
    years ago. So that tells you how often I actually mess around with xlib.


    "George Peter Staplin" wrote in message
    news:fn8u93$m1j$1@news.xmission.com...
    >
    >
    > From the look of things you're using threads with Xlib without doing
    > XInitThreads(). You may find that works better. Internally XNextEvent
    > and other event queue X functions may call select() or poll() so if your
    > socket_server() path does that too, you may experience weird bugs.
    >
    >
    > George




  5. Re: Looking for xlib programing help....

    Thanks for the info on XInitThreads. That fixed a mysterious error I was
    recieving.
    Perhaps it is time to get a newer book.

    Dwayne

    "Dwayne Dilbeck" wrote in message
    news:13pi00omhdub102@corp.supernews.com...
    > Interesting the book I have doesn't even list XInitThreads(). I will
    > look it up on google.
    >
    > I have been getting my Xlib information from "XLib Programming Manual for
    > version 11(R4/R5)" by Adrian Nye - O'Reilly & Associates, Inc. I don't
    > even remember when I purchased this book. I believe I purchased it about
    > 8 years ago. So that tells you how often I actually mess around with
    > xlib.
    >
    >
    > "George Peter Staplin" wrote in message
    > news:fn8u93$m1j$1@news.xmission.com...
    >>
    >>
    >> From the look of things you're using threads with Xlib without doing
    >> XInitThreads(). You may find that works better. Internally XNextEvent
    >> and other event queue X functions may call select() or poll() so if your
    >> socket_server() path does that too, you may experience weird bugs.
    >>
    >>
    >> George

    >
    >




  6. Re: Looking for xlib programing help....

    Dwayne Dilbeck wrote:
    > Interesting the book I have doesn't even list XInitThreads(). I will look
    > it up on google.
    >
    > I have been getting my Xlib information from "XLib Programming Manual for
    > version 11(R4/R5)" by Adrian Nye - O'Reilly & Associates, Inc. I don't
    > even remember when I purchased this book. I believe I purchased it about 8
    > years ago. So that tells you how often I actually mess around with xlib.


    I'm not sure that a unix/POSIX thread API existed at the time, or people
    even programmed with pthreads much when Adrian Nye's book was originally
    written.

    Nye's book is good though. I still use it as a reference. Xlib
    unfortunately though isn't that popular to program with. The biggest
    demand these days for Xlib is in combination with things like Gtk+/Gdk,
    and similar toolkits. There is also ongoing work as I understand it by
    some X.org programmers to bring a newer Xlib-work-alike interface to
    X11.


    > "George Peter Staplin" wrote in message
    > news:fn8u93$m1j$1@news.xmission.com...
    >>
    >>
    >> From the look of things you're using threads with Xlib without doing
    >> XInitThreads(). You may find that works better. Internally XNextEvent
    >> and other event queue X functions may call select() or poll() so if your
    >> socket_server() path does that too, you may experience weird bugs.
    >>
    >>
    >> George

    >
    >


+ Reply to Thread