Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!ucsd!ucbvax!bloom-beacon!dont-send-mail-to-path-lines From: mouse@larry.mcrcim.mcgill.EDU Newsgroups: comp.windows.x Subject: Re: Help! What is the magic of the sleep() statement? Message-ID: <9101100804.AA00842@Larry.McRCIM.McGill.EDU> Date: 10 Jan 91 08:04:39 GMT Sender: daemon@athena.mit.edu (Mr Background) Organization: The Internet Lines: 77 >> The following few statements attempt to create a window and draw a >> line with some strings. I found that the sleep(1) statement is very >> crucial for its success. Its presence can make the program work as >> I expect but its absence make the program map the windows only. No >> drawing at all, if I run this program from another machine. >> Occasionally I can get it running in the same workstation where I >> started X. I haven't seen the original posting, but I would guess this is the common problem about not waiting for the Expose event. I will append the response to question 74 from the FAQ posting, which addresses this. > Try something like this: > all your init stuff. > XMapRaised > XSelectInput (dpy, win, ExposureMask); Whoops. The XSelectInput should be before the XMapRaised. I would recommend that you set the event mask when you create the window. der Mouse old: mcgill-vision!mouse new: mouse@larry.mcrcim.mcgill.edu ---------------------------------------------------------------------- Subject: 74) Why doesn't anything appear when I run this simple program? > ... > the_window = XCreateSimpleWindow(the_display, > root_window,size_hints.x,size_hints.y, > size_hints.width,size_hints.height,BORDER_WIDTH, > BlackPixel(the_display,the_screen), > WhitePixel(the_display,the_screen)); > ... > XSelectInput(the_display,the_window,ExposureMask|ButtonPressMask| > ButtonReleaseMask); > XMapWindow(the_display,the_window); > ... > XDrawLine(the_display,the_window,the_GC,5,5,100,100); > ... You are right to map the window before drawing into it. However, the window is not ready to be drawn into until it actually appears on the screen -- until your application receives an Expose event. Drawing done before that will generally not appear. You'll see code like this in many programs; this code would appear after window was created and mapped: while (!done) { XNextEvent(the_display,&the_event); switch (the_event.type) { case Expose: /* On expose events, redraw */ XDrawLine(the_display,the_window,the_GC,5,5,100,100); break; ... } } Note that there is a second problem: some X servers don't set up the default graphics context to have reasonable foreground/background colors, and your program should not assume that the server does, so this program could previously include this code to prevent the case of having the foreground and background colors the same: ... the_GC_values.foreground=BlackPixel(the_display,the_screen); /* e.g. */ the_GC_values.background=WhitePixel(the_display,the_screen); /* e.g. */ the_GC = XCreateGC(the_display,the_window, GCForeground|GCBackground,&the_GC_values); ... Note: the code uses BlackPixel and WhitePixel to avoid assuming that 1 is black and 0 is white or vice-versa. The relationship between pixels 0 and 1 and the colors black and white is implementation-dependent. They may be reversed, or they may not even correspond to black and white at all. ----------------------------------------------------------------------