LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 08-08-2007, 10:56 AM   #1
apetrescu
LQ Newbie
 
Registered: Aug 2007
Location: Waterloo Ontario (Canada)
Distribution: Kubuntu 7.04
Posts: 8

Rep: Reputation: 0
Catching X client messages


Hello, everyone. I'm trying to get my feet wet with some Linux/OSS programming by fixing some bugs in packages I use all the time. At the moment I have this X application (raw X, doesn't use GTK or Qt or anything like that) that spawns a subprocess when it starts. However, if you close the X application with anything other than the program's "Quit" button, like the X "Close" button in the window manager, the subprocess is not stopped and continues running ad infinitum, taking up a lot of resources in the process. I essentially want to catch the event of the user clicking the "X" button, so I can close it properly.

At first I naively thought that X would simply send a SIGTERM signal when the user closed the window. So I registered a SIGTERM handler in main():
Code:
1839     // Register SIGTERM callback
1840     struct sigaction sa;
1841     sa.sa_handler = handler;
1842     sa.sa_flags = 0;
1843     sigaction(SIGKILL, &sa, NULL);
1844     puts("Registered sigkill handler");
where "handler" is simply:
Code:
1865 void handler(int status) {
1866         puts("In the handler function");
1867         exit(EXIT_SUCCESS);
1868 }
If I use "kill -TERM", then the handler works, and I see the "In the handler function" output. But it doesn't get called at all when closing the X window, so I figured some other type of mechanism was being used. So I looked up the comp.windows.x FAQ and found something that looked promising:
Quote:
Originally Posted by comp.windows.x FAQ Question 188
188) How do I catch the "close window" event to avoid "fatal IO error"?

Several windows managers offer a function such as f.kill or f.delete
which sends a message to the application that it should delete its window;
this is usually interpreted as a shutdown message.
The application needs to catch the WM_DELETE_WINDOW client message.
There is a good example in the xcalc sources in X11R5.
Motif-based applications should in addition set the resource
XmNdeleteResponse on the top-level shell to XmDO_NOTHING, whether they are
using the Motif window manager or not.
If the application doesn't handle this message the window manager may
wind up calling XKillClient, which disconnects the client from the display and
typically gives an Xlib error along the lines of "fatal IO error 32 (Broken
pipe)".
However, I'm not sure what "catch the WM_DELETE_WINDOW client message" entails, and Google'ing hasn't helped me. I checked the source of xcalc as they said, but I found nothing helpful there. The only reference to WM_DELETE_WINDOW was here:
Code:
146     dpy = XtDisplay(toplevel);
147     wm_delete_window = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
148     (void) XSetWMProtocols(dpy, XtWindow(toplevel), &wm_delete_window, 1);
But I don't really understand what that does.


I'm hoping one of the gurus here can help me out I have little experience with X and C, so please use simple language . Thanks in advance!
 
Old 08-09-2007, 01:09 AM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
Practically every X application uses an event loop. You know about event loops, ja?

If you're using plain old X, then you're not interested in WM_DELETE_WINDOW. You'll want to catch the ButtonPress event.

If it's not clear how to do that, do this at the command line:

Code:
man XNextEvent
as the first step of a long but interesting road.

If that isn't enough google for this:

Code:
Xlib tutorial
Stay away from Motif until you're sure that's what you want.

Hope this helps.

Last edited by wjevans_7d1@yahoo.co; 08-09-2007 at 01:11 AM.
 
  


Reply



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
How to change mutt client default format for sent messages? Blyiss Linux - Software 0 02-22-2007 11:54 AM
Shutdown messages on a diskless client Goober07 Linux - Newbie 1 05-10-2005 06:23 AM
Catching a Hacker... Shr00mBoXx Linux - Security 14 06-30-2004 09:59 PM
Catching first letter. TheRealDeal Linux - General 1 03-28-2004 08:50 PM
Relay messages from remote client to anywhere with postfix mogumbo Linux - Networking 3 12-04-2003 07:06 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:54 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration