LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-14-2010, 03:25 PM   #1
10110111
Member
 
Registered: Jun 2008
Location: St.-Petersburg, Russia
Distribution: (B)LFS, Ubuntu, SliTaz
Posts: 402

Rep: Reputation: 51
How to make X program not abort when its window is closed/server killed?


I try to run a simple Xlib program:
Code:
 /*
   Simple Xlib application drawing a box in a window.
   gcc input.c -o output -lX11
 */

 #include <X11/Xlib.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>

 int main(void) {
   Display *d;
   Window w;
   XEvent e;
   char *msg = "Hello, World!";
   int s;

                        /* open connection with the server */
   d = XOpenDisplay(NULL);
   if (d == NULL) {
     fprintf(stderr, "Cannot open display\n");
     exit(1);
   }

   s = DefaultScreen(d);

                        /* create window */
   w = XCreateSimpleWindow(d, RootWindow(d, s), 10, 10, 200, 200, 1,
                           BlackPixel(d, s), WhitePixel(d, s));

                        /* select kind of events we are interested in */
   XSelectInput(d, w, ExposureMask | KeyPressMask);

                        /* map (show) the window */
   XMapWindow(d, w);

                        /* event loop */
   while (1) {
     XNextEvent(d, &e);
                        /* draw or redraw the window */
     if (e.type == Expose) {
       XFillRectangle(d, w, DefaultGC(d, s), 20, 20, 10, 10);
       XDrawString(d, w, DefaultGC(d, s), 50, 50, msg, strlen(msg));
     }
                        /* exit on key press */
     if (e.type == KeyPress)
       break;
   }

                        /* close connection to server */
   XCloseDisplay(d);

   return 0;
 }
When i kill X server or close program's window, it aborts with error:
Quote:
XIO: fatal IO error 11 (Resource temporarily unavailable) on X server ":0.0"
after 9 requests (9 known processed) with 0 events remaining.
I found that XNextEvent is the reason of abort.
How can i make it try to reconnect to new server or do anything else instead of just aborting?
 
Old 01-15-2010, 10:30 AM   #2
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

This thread might be useful to you:

http://www.linuxquestions.org/questi...IOErrorHandler

Search for "XSetIOErrorHandler" or "WM_DELETE_WINDOW".

'Hope that helps .. PSM
 
Old 01-15-2010, 02:41 PM   #3
10110111
Member
 
Registered: Jun 2008
Location: St.-Petersburg, Russia
Distribution: (B)LFS, Ubuntu, SliTaz
Posts: 402

Original Poster
Rep: Reputation: 51
Quote:
Xlib calls the program's supplied error handler if any sort of system call error occurs (for example, the connection to the server was lost). This is assumed to be a fatal condition, and the called routine should not return. If the I/O error handler does return, the client process exits.
So, how should the program continue execution if it doesn't assume i/o error as fatal? goto some label in main()? But then there's a memory leak at least in stack... is there a better way?
 
Old 01-17-2010, 01:22 AM   #4
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

If you want to mess around with Xlib, you get to manage the low level details yourself.

If you wanted to try a higher-level framework (which, of course, I strongly encourage), then the framework will probably let you handle things like "window manager close" events more easily. You might want to investigate either/both GTK+ and/or Qt.

IMHO .. PSM
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Does anyone know how to reopen a window closed due to a bug? InfinitePalindrome Linux - Newbie 14 06-13-2008 03:57 PM
my splash window does not get closed duvvuripavan Linux - Server 1 11-27-2007 12:50 PM
firefox does process is not killed when closed dh2k Suse/Novell 2 01-13-2007 06:39 AM
help I've killed my window manager! quinnk Linux - Software 2 06-01-2004 05:38 PM
I just killed my x window session! Thaidog Linux - Newbie 5 02-22-2004 06:59 AM


All times are GMT -5. The time now is 05:55 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration