]> code.delx.au - pulseaudio/blob - src/modules/module-x11-xsmp.c
merge 'lennart' branch back into trunk.
[pulseaudio] / src / modules / module-x11-xsmp.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
12
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include <X11/Xlib.h>
33 #include <X11/SM/SMlib.h>
34
35 #include <pulse/xmalloc.h>
36 #include <pulse/util.h>
37
38 #include <pulsecore/iochannel.h>
39 #include <pulsecore/sink.h>
40 #include <pulsecore/core-scache.h>
41 #include <pulsecore/modargs.h>
42 #include <pulsecore/namereg.h>
43 #include <pulsecore/log.h>
44 #include <pulsecore/core-util.h>
45
46 #include "module-x11-xsmp-symdef.h"
47
48 PA_MODULE_AUTHOR("Lennart Poettering")
49 PA_MODULE_DESCRIPTION("X11 session management")
50 PA_MODULE_VERSION(PACKAGE_VERSION)
51
52 static int ice_in_use = 0;
53
54 static const char* const valid_modargs[] = {
55 NULL
56 };
57
58 static void die_cb(SmcConn connection, SmPointer client_data){
59 pa_core *c = PA_CORE(client_data);
60
61 pa_log_debug("Got die message from XSM. Exiting...");
62
63 pa_core_assert_ref(c);
64 c->mainloop->quit(c->mainloop, 0);
65 }
66
67 static void save_complete_cb(SmcConn connection, SmPointer client_data) {
68 }
69
70 static void shutdown_cancelled_cb(SmcConn connection, SmPointer client_data) {
71 SmcSaveYourselfDone(connection, True);
72 }
73
74 static void save_yourself_cb(SmcConn connection, SmPointer client_data, int save_type, Bool _shutdown, int interact_style, Bool fast) {
75 SmcSaveYourselfDone(connection, True);
76 }
77
78 static void ice_io_cb(pa_mainloop_api*a, pa_io_event *e, int fd, pa_io_event_flags_t flags, void *userdata) {
79 IceConn connection = userdata;
80
81 if (IceProcessMessages(connection, NULL, NULL) == IceProcessMessagesIOError) {
82 IceSetShutdownNegotiation(connection, False);
83 IceCloseConnection(connection);
84 }
85 }
86
87 static void new_ice_connection(IceConn connection, IcePointer client_data, Bool opening, IcePointer *watch_data) {
88 pa_core *c = client_data;
89
90 pa_assert(c);
91
92 if (opening)
93 *watch_data = c->mainloop->io_new(c->mainloop, IceConnectionNumber(connection), PA_IO_EVENT_INPUT, ice_io_cb, connection);
94 else
95 c->mainloop->io_free(*watch_data);
96 }
97
98 int pa__init(pa_module*m) {
99
100 pa_modargs *ma = NULL;
101 char t[256], *vendor, *client_id;
102 SmcCallbacks callbacks;
103 SmProp prop_program, prop_user;
104 SmProp *prop_list[2];
105 SmPropValue val_program, val_user;
106 SmcConn connection;
107
108 pa_assert(m);
109
110 if (ice_in_use) {
111 pa_log("module-x11-xsmp may no be loaded twice.");
112 return -1;
113 }
114
115 IceAddConnectionWatch(new_ice_connection, m->core);
116 ice_in_use = 1;
117
118 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
119 pa_log("Failed to parse module arguments");
120 goto fail;
121 }
122
123 if (!getenv("SESSION_MANAGER")) {
124 pa_log("X11 session manager not running.");
125 goto fail;
126 }
127
128 memset(&callbacks, 0, sizeof(callbacks));
129 callbacks.die.callback = die_cb;
130 callbacks.die.client_data = m->core;
131 callbacks.save_yourself.callback = save_yourself_cb;
132 callbacks.save_yourself.client_data = m->core;
133 callbacks.save_complete.callback = save_complete_cb;
134 callbacks.save_complete.client_data = m->core;
135 callbacks.shutdown_cancelled.callback = shutdown_cancelled_cb;
136 callbacks.shutdown_cancelled.client_data = m->core;
137
138 if (!(m->userdata = connection = SmcOpenConnection(
139 NULL, m->core,
140 SmProtoMajor, SmProtoMinor,
141 SmcSaveYourselfProcMask | SmcDieProcMask | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
142 &callbacks, NULL, &client_id,
143 sizeof(t), t))) {
144
145 pa_log("Failed to open connection to session manager: %s", t);
146 goto fail;
147 }
148
149 prop_program.name = (char*) SmProgram;
150 prop_program.type = (char*) SmARRAY8;
151 val_program.value = (char*) PACKAGE_NAME;
152 val_program.length = strlen(val_program.value);
153 prop_program.num_vals = 1;
154 prop_program.vals = &val_program;
155 prop_list[0] = &prop_program;
156
157 prop_user.name = (char*) SmUserID;
158 prop_user.type = (char*) SmARRAY8;
159 pa_get_user_name(t, sizeof(t));
160 val_user.value = t;
161 val_user.length = strlen(val_user.value);
162 prop_user.num_vals = 1;
163 prop_user.vals = &val_user;
164 prop_list[1] = &prop_user;
165
166 SmcSetProperties(connection, PA_ELEMENTSOF(prop_list), prop_list);
167
168 pa_log_info("Connected to session manager '%s' as '%s'.", vendor = SmcVendor(connection), client_id);
169 free(vendor);
170 free(client_id);
171
172 pa_modargs_free(ma);
173
174 return 0;
175
176 fail:
177 if (ma)
178 pa_modargs_free(ma);
179
180 pa__done(m);
181
182 return -1;
183 }
184
185 void pa__done(pa_module*m) {
186 pa_assert(m);
187
188 if (m->userdata)
189 SmcCloseConnection(m->userdata, 0, NULL);
190
191 if (ice_in_use) {
192 IceRemoveConnectionWatch(new_ice_connection, m->core);
193 ice_in_use = 0;
194 }
195 }