Find the answer to your Linux question:
Results 1 to 2 of 2
Hello, I tried to draw two lines with xlib.h in C. If I start this compiled program it show me in most cases a window with two lines as it ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jul 2010
    Posts
    1

    xlib C program doesn´t work every time


    Hello,
    I tried to draw two lines with xlib.h in C.
    If I start this compiled program it show me in most cases a window with two lines as it should be.
    But sometimes the two lines are not drawn.If I insert XFlush() before the second XSync() it worked better but not every time.Why?How can I solve the problem?How works the X-Server buffer in detail?
    Code:
    #include <X11/Xlib.h>
     #include <stdio.h>
     #include <unistd.h>
     
     int main(void) {
       Display *d;
       Window w;
       int s;
     
                            /* open connection with the server */
       d = XOpenDisplay(NULL);
       if (d == NULL) {
         fprintf(stderr, "Cannot open display\n");
         return 1;
       }
     
       s = DefaultScreen(d);
     
                            /* create window */
       w = XCreateSimpleWindow(d, RootWindow(d, s), 10, 10, 200, 200, 1,
                               BlackPixel(d, s), WhitePixel(d, s));
     
                          
                            /* map (show) the window */
       XMapWindow(d, w);
     
       GC gc=DefaultGC(d, s);
       XSync(d, False); //the mistake seems to depend on this function
                            
      
       XDrawLine(d, w, gc,100, 0, 100, 200);
       XDrawLine(d, w, gc, 0, 100, 200, 100);
       
       XSync(d, False); //
    
       sleep(1);
     
                            /* close connection to server */
       XCloseDisplay(d);
     
       return 0;
     }
    Thanks for all answers

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,525
    Look at the default graphics context you are using which will specify the line width to use. It may be that it is sometimes drawing it where it cannot be displayed by your hardware. You might want to set up a custom context to use and try a wider line width for your XDrawLine() calls. Adding the XFlush() shouldn't be needed with XSync(), but I'm not 100% sure about that.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •