Audacious $Id:Doxyfile42802007-03-2104:39:00Znenolod$

signals.c

Go to the documentation of this file.
00001 /*
00002  * Audacious
00003  * Copyright (c) 2005-2007 Yoshiki Yazawa
00004  * Copyright 2009 John Lindgren (POSIX threaded signal handling)
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; under version 3 of the License.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program.  If not, see <http://www.gnu.org/licenses>.
00017  *
00018  * The Audacious team does not consider modular code linking to
00019  * Audacious or using our public API to be a derived work.
00020  */
00021 
00022 #include <signal.h>
00023 
00024 #include <glib.h>
00025 #include <gtk/gtk.h>
00026 
00027 #include <libaudcore/eventqueue.h>
00028 
00029 #include "audconfig.h"
00030 #include "config.h"
00031 #include "main.h"
00032 #include "signals.h"
00033 
00034 #ifdef USE_EGGSM
00035 #include "eggsmclient.h"
00036 #endif
00037 
00038 static sigset_t signal_set;
00039 
00040 #ifdef USE_EGGSM
00041 static void
00042 signal_session_quit_cb(EggSMClient *client, gpointer user_data)
00043 {
00044     const gchar * argv[2];
00045 
00046     g_print("Session quit requested. Saving state and shutting down.\n");
00047 
00048     argv[0] = "audacious";
00049     argv[1] = g_strdup_printf ("--display=%s", gdk_display_get_name (gdk_display_get_default()));
00050     egg_sm_client_set_restart_command (client, 2, argv);
00051 
00052     aud_quit();
00053 }
00054 
00055 static void
00056 signal_session_save_cb(EggSMClient *client, GKeyFile *state_file, gpointer user_data)
00057 {
00058     const gchar * argv[2];
00059 
00060     g_print("Session save requested. Saving state.\n");
00061 
00062     argv[0] = "audacious";
00063     argv[1] = g_strdup_printf ("--display=%s", gdk_display_get_name (gdk_display_get_default()));
00064     egg_sm_client_set_restart_command (client, 2, argv);
00065 
00066     aud_config_save();
00067 }
00068 #endif
00069 
00070 static void * signal_thread (void * data)
00071 {
00072     gint signal;
00073 
00074     while (! sigwait (& signal_set, & signal))
00075         event_queue ("quit", 0);
00076 
00077     return NULL;
00078 }
00079 
00080 /* Must be called before any threads are created. */
00081 void signal_handlers_init (void)
00082 {
00083 #ifdef USE_EGGSM
00084     EggSMClient *client;
00085 
00086     client = egg_sm_client_get ();
00087     if (client != NULL)
00088     {
00089         egg_sm_client_set_mode (EGG_SM_CLIENT_MODE_NORMAL);
00090         g_signal_connect (client, "quit",
00091                           G_CALLBACK (signal_session_quit_cb), NULL);
00092         g_signal_connect (client, "save-state",
00093                           G_CALLBACK (signal_session_save_cb), NULL);
00094 
00095     }
00096 #endif
00097 
00098     sigemptyset (& signal_set);
00099     sigaddset (& signal_set, SIGHUP);
00100     sigaddset (& signal_set, SIGINT);
00101     sigaddset (& signal_set, SIGQUIT);
00102     sigaddset (& signal_set, SIGTERM);
00103 
00104     sigprocmask (SIG_BLOCK, & signal_set, NULL);
00105     g_thread_create (signal_thread, NULL, FALSE, NULL);
00106 }