* @file gtksession.c X Windows session management API * Gaim is the legal property of its developers, whose names are too numerous * to list here. Please refer to the COPYRIGHT file distributed with this * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #include <X11/ICE/ICElib.h> #include <X11/SM/SMlib.h> static IceIOErrorHandler ice_installed_io_error_handler; static SmcConn session = NULL; static gchar *myself = NULL; static gboolean had_first_save = FALSE; static gboolean session_managed = FALSE; /* ICE belt'n'braces stuff */ struct ice_connection_info { static void ice_process_messages(gpointer data, gint fd, GaimInputCondition condition) { struct ice_connection_info *conninfo = (struct ice_connection_info*) data; IceProcessMessagesStatus status; /* please don't block... please! */ status = IceProcessMessages(conninfo->connection, NULL, NULL); if (status == IceProcessMessagesIOError) { gaim_debug(GAIM_DEBUG_INFO, "Session Management", "ICE IO error, closing connection... "); /* IO error, please disconnect */ IceSetShutdownNegotiation(conninfo->connection, False); IceCloseConnection(conninfo->connection); gaim_debug(GAIM_DEBUG_INFO, NULL, "done.\n"); gaim_input_remove(conninfo->input_id); static void ice_connection_watch(IceConn connection, IcePointer client_data, Bool opening, IcePointer *watch_data) { struct ice_connection_info *conninfo = NULL; gaim_debug(GAIM_DEBUG_INFO, "Session Management", "Handling new ICE connection... "); /* ensure ICE connection is not passed to child processes */ fcntl(IceConnectionNumber(connection), F_SETFD, FD_CLOEXEC); conninfo = g_new(struct ice_connection_info, 1); conninfo->connection = connection; /* watch the connection */ conninfo->input_id = gaim_input_add(IceConnectionNumber(connection), GAIM_INPUT_READ, ice_process_messages, conninfo); gaim_debug(GAIM_DEBUG_INFO, "Session Management", "Handling closed ICE connection... "); /* get the input ID back and stop watching it */ conninfo = (struct ice_connection_info*) *watch_data; gaim_input_remove(conninfo->input_id); gaim_debug(GAIM_DEBUG_INFO, NULL, "done.\n"); /* We call any handler installed before (or after) ice_init but * avoid calling the default libICE handler which does an exit(). * This means we do nothing by default, which is probably correct, * the connection will get closed by libICE static void ice_io_error_handler(IceConn connection) { gaim_debug(GAIM_DEBUG_INFO, "Session Management", "Handling ICE IO error... "); if (ice_installed_io_error_handler) (*ice_installed_io_error_handler)(connection); gaim_debug(GAIM_DEBUG_INFO, NULL, "done.\n"); IceIOErrorHandler default_handler; ice_installed_io_error_handler = IceSetIOErrorHandler(NULL); default_handler = IceSetIOErrorHandler(ice_io_error_handler); if (ice_installed_io_error_handler == default_handler) ice_installed_io_error_handler = NULL; IceAddConnectionWatch(ice_connection_watch, NULL); gaim_debug(GAIM_DEBUG_INFO, "Session Management", /* my magic utility function */ static gchar **session_make_command(gchar *client_id, gchar *config_dir) { if (config_dir) i += 2; /* we will specify gaim's user dir */ ret[j++] = g_strdup(myself); ret[j++] = g_strdup("--session"); ret[j++] = g_strdup(client_id); ret[j++] = g_strdup("--config"); ret[j++] = g_strdup(config_dir); /* SM callback handlers */ static void session_save_yourself(SmcConn conn, SmPointer data, int save_type, Bool shutdown, int interact_style, Bool fast) { if (had_first_save == FALSE && save_type == SmSaveLocal && interact_style == SmInteractStyleNone && !shutdown && /* this is just a dry run, spit it back */ gaim_debug(GAIM_DEBUG_INFO, "Session Management", "Received first save_yourself\n"); SmcSaveYourselfDone(conn, True); /* tum ti tum... don't add anything else here without * * reading SMlib.PS from an X.org ftp server near you */ gaim_debug(GAIM_DEBUG_INFO, "Session Management", "Received save_yourself\n"); if (save_type == SmSaveGlobal || save_type == SmSaveBoth) { /* may as well do something ... */ /* or not -- save_prefs(); */ SmcSaveYourselfDone(conn, True); static void session_die(SmcConn conn, SmPointer data) { gaim_debug(GAIM_DEBUG_INFO, "Session Management", static void session_save_complete(SmcConn conn, SmPointer data) { gaim_debug(GAIM_DEBUG_INFO, "Session Management", "Received save_complete\n"); static void session_shutdown_cancelled(SmcConn conn, SmPointer data) { gaim_debug(GAIM_DEBUG_INFO, "Session Management", "Received shutdown_cancelled\n"); /* utility functions stolen from Gnome-client */ static void session_set_value(SmcConn conn, gchar *name, char *type, int num_vals, SmPropValue *vals) { prop.num_vals = num_vals; SmcSetProperties(conn, 1, proplist); static void session_set_string(SmcConn conn, gchar *name, gchar *value) { val.length = strlen (value)+1; session_set_value(conn, name, SmARRAY8, 1, &val); static void session_set_gchar(SmcConn conn, gchar *name, gchar value) { session_set_value(conn, name, SmCARD8, 1, &val); static void session_set_array(SmcConn conn, gchar *name, gchar *array[]) { /* We count the number of elements in our array. */ for (ptr = array, argc = 0; *ptr ; ptr++, argc++) /* LOOP */; /* Now initialize the 'vals' array. */ vals = g_new (SmPropValue, argc); for (ptr = array, i = 0 ; i < argc ; ptr++, i++) { vals[i].length = strlen (*ptr); session_set_value(conn, name, SmLISTofARRAY8, argc, vals); gaim_gtk_session_init(gchar *argv0, gchar *previous_id, gchar *config_dir) gchar error[ERROR_LENGTH] = ""; /* session is already established, what the hell is going on? */ gaim_debug(GAIM_DEBUG_WARNING, "Session Management", "Duplicated call to gaim_gtk_session_init!\n"); if (g_getenv("SESSION_MANAGER") == NULL) { gaim_debug(GAIM_DEBUG_ERROR, "Session Management", "No SESSION_MANAGER found, aborting.\n"); callbacks.save_yourself.callback = session_save_yourself; callbacks.die.callback = session_die; callbacks.save_complete.callback = session_save_complete; callbacks.shutdown_cancelled.callback = session_shutdown_cancelled; callbacks.save_yourself.client_data = NULL; callbacks.die.client_data = NULL; callbacks.save_complete.client_data = NULL; callbacks.shutdown_cancelled.client_data = NULL; gaim_debug(GAIM_DEBUG_INFO, "Session Management", "Connecting with previous ID %s\n", previous_id); gaim_debug(GAIM_DEBUG_INFO, "Session Management", "Connecting with no previous ID\n"); session = SmcOpenConnection(NULL, "session", SmProtoMajor, SmProtoMinor, SmcSaveYourselfProcMask | SmcDieProcMask | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask, &callbacks, previous_id, &client_id, ERROR_LENGTH, error); gaim_debug(GAIM_DEBUG_ERROR, "Session Management", "Connection failed with error: %s\n", error); gaim_debug(GAIM_DEBUG_ERROR, "Session Management", "Connetion failed with unknown error.\n"); tmp = SmcVendor(session); gaim_debug(GAIM_DEBUG_INFO, "Session Management", "Connected to manager (%s) with client ID %s\n", gdk_set_sm_client_id(client_id); tmp = g_get_current_dir(); session_set_string(session, SmCurrentDirectory, tmp); tmp = g_strdup_printf("%d", (int) getpid()); session_set_string(session, SmProcessID, tmp); tmp = g_strdup(g_get_user_name()); session_set_string(session, SmUserID, tmp); session_set_gchar(session, SmRestartStyleHint, (gchar) SmRestartIfRunning); session_set_string(session, SmProgram, g_get_prgname()); myself = g_strdup(argv0); gaim_debug(GAIM_DEBUG_MISC, "Session Management", "Using %s as command\n", myself); cmd = session_make_command(NULL, config_dir); session_set_array(session, SmCloneCommand, cmd); /* this is currently useless, but gnome-session warns 'the following applications will not save their current status' bla bla if we don't have it and the user checks 'Save Session' cmd[0] = g_strdup("/bin/true"); session_set_array(session, SmDiscardCommand, cmd); cmd = session_make_command(client_id, config_dir); session_set_array(session, SmRestartCommand, cmd); if (session == NULL) /* no session to close */ SmcCloseConnection(session, 0, NULL); gaim_debug(GAIM_DEBUG_INFO, "Session Management",