]> code.delx.au - pulseaudio/blob - src/modules/module-x11-xsmp.c
tag modules that may only be loaded once at most especially, and enforce that in...
[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 PA_MODULE_LOAD_ONCE(TRUE);
52
53 static int ice_in_use = 0;
54
55 static const char* const valid_modargs[] = {
56 NULL
57 };
58
59 static void die_cb(SmcConn connection, SmPointer client_data){
60 pa_core *c = PA_CORE(client_data);
61
62 pa_log_debug("Got die message from XSM. Exiting...");
63
64 pa_core_assert_ref(c);
65 c->mainloop->quit(c->mainloop, 0);
66 }
67
68 static void save_complete_cb(SmcConn connection, SmPointer client_data) {
69 }
70
71 static void shutdown_cancelled_cb(SmcConn connection, SmPointer client_data) {
72 SmcSaveYourselfDone(connection, True);
73 }
74
75 static void save_yourself_cb(SmcConn connection, SmPointer client_data, int save_type, Bool _shutdown, int interact_style, Bool fast) {
76 SmcSaveYourselfDone(connection, True);
77 }
78
79 static void ice_io_cb(pa_mainloop_api*a, pa_io_event *e, int fd, pa_io_event_flags_t flags, void *userdata) {
80 IceConn connection = userdata;
81
82 if (IceProcessMessages(connection, NULL, NULL) == IceProcessMessagesIOError) {
83 IceSetShutdownNegotiation(connection, False);
84 IceCloseConnection(connection);
85 }
86 }
87
88 static void new_ice_connection(IceConn connection, IcePointer client_data, Bool opening, IcePointer *watch_data) {
89 pa_core *c = client_data;
90
91 pa_assert(c);
92
93 if (opening)
94 *watch_data = c->mainloop->io_new(c->mainloop, IceConnectionNumber(connection), PA_IO_EVENT_INPUT, ice_io_cb, connection);
95 else
96 c->mainloop->io_free(*watch_data);
97 }
98
99 int pa__init(pa_module*m) {
100
101 pa_modargs *ma = NULL;
102 char t[256], *vendor, *client_id;
103 SmcCallbacks callbacks;
104 SmProp prop_program, prop_user;
105 SmProp *prop_list[2];
106 SmPropValue val_program, val_user;
107 SmcConn connection;
108
109 pa_assert(m);
110
111 if (ice_in_use) {
112 pa_log("module-x11-xsmp may no be loaded twice.");
113 return -1;
114 }
115
116 IceAddConnectionWatch(new_ice_connection, m->core);
117 ice_in_use = 1;
118
119 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
120 pa_log("Failed to parse module arguments");
121 goto fail;
122 }
123
124 if (!getenv("SESSION_MANAGER")) {
125 pa_log("X11 session manager not running.");
126 goto fail;
127 }
128
129 memset(&callbacks, 0, sizeof(callbacks));
130 callbacks.die.callback = die_cb;
131 callbacks.die.client_data = m->core;
132 callbacks.save_yourself.callback = save_yourself_cb;
133 callbacks.save_yourself.client_data = m->core;
134 callbacks.save_complete.callback = save_complete_cb;
135 callbacks.save_complete.client_data = m->core;
136 callbacks.shutdown_cancelled.callback = shutdown_cancelled_cb;
137 callbacks.shutdown_cancelled.client_data = m->core;
138
139 if (!(m->userdata = connection = SmcOpenConnection(
140 NULL, m->core,
141 SmProtoMajor, SmProtoMinor,
142 SmcSaveYourselfProcMask | SmcDieProcMask | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
143 &callbacks, NULL, &client_id,
144 sizeof(t), t))) {
145
146 pa_log("Failed to open connection to session manager: %s", t);
147 goto fail;
148 }
149
150 prop_program.name = (char*) SmProgram;
151 prop_program.type = (char*) SmARRAY8;
152 val_program.value = (char*) PACKAGE_NAME;
153 val_program.length = strlen(val_program.value);
154 prop_program.num_vals = 1;
155 prop_program.vals = &val_program;
156 prop_list[0] = &prop_program;
157
158 prop_user.name = (char*) SmUserID;
159 prop_user.type = (char*) SmARRAY8;
160 pa_get_user_name(t, sizeof(t));
161 val_user.value = t;
162 val_user.length = strlen(val_user.value);
163 prop_user.num_vals = 1;
164 prop_user.vals = &val_user;
165 prop_list[1] = &prop_user;
166
167 SmcSetProperties(connection, PA_ELEMENTSOF(prop_list), prop_list);
168
169 pa_log_info("Connected to session manager '%s' as '%s'.", vendor = SmcVendor(connection), client_id);
170 free(vendor);
171 free(client_id);
172
173 pa_modargs_free(ma);
174
175 return 0;
176
177 fail:
178 if (ma)
179 pa_modargs_free(ma);
180
181 pa__done(m);
182
183 return -1;
184 }
185
186 void pa__done(pa_module*m) {
187 pa_assert(m);
188
189 if (m->userdata)
190 SmcCloseConnection(m->userdata, 0, NULL);
191
192 if (ice_in_use) {
193 IceRemoveConnectionWatch(new_ice_connection, m->core);
194 ice_in_use = 0;
195 }
196 }