4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
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.
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.
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
33 #include <X11/SM/SMlib.h>
35 #include <pulse/xmalloc.h>
36 #include <pulse/util.h>
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>
46 #include "module-x11-xsmp-symdef.h"
48 PA_MODULE_AUTHOR("Lennart Poettering")
49 PA_MODULE_DESCRIPTION("X11 session management")
50 PA_MODULE_VERSION(PACKAGE_VERSION
)
52 static int ice_in_use
= 0;
54 static const char* const valid_modargs
[] = {
58 static void die_cb(SmcConn connection
, SmPointer client_data
){
59 pa_core
*c
= PA_CORE(client_data
);
61 pa_log_debug("Got die message from XSM. Exiting...");
63 pa_core_assert_ref(c
);
64 c
->mainloop
->quit(c
->mainloop
, 0);
67 static void save_complete_cb(SmcConn connection
, SmPointer client_data
) {
70 static void shutdown_cancelled_cb(SmcConn connection
, SmPointer client_data
) {
71 SmcSaveYourselfDone(connection
, True
);
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
);
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
;
81 if (IceProcessMessages(connection
, NULL
, NULL
) == IceProcessMessagesIOError
) {
82 IceSetShutdownNegotiation(connection
, False
);
83 IceCloseConnection(connection
);
87 static void new_ice_connection(IceConn connection
, IcePointer client_data
, Bool opening
, IcePointer
*watch_data
) {
88 pa_core
*c
= client_data
;
93 *watch_data
= c
->mainloop
->io_new(c
->mainloop
, IceConnectionNumber(connection
), PA_IO_EVENT_INPUT
, ice_io_cb
, connection
);
95 c
->mainloop
->io_free(*watch_data
);
98 int pa__init(pa_module
*m
) {
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
;
111 pa_log("module-x11-xsmp may no be loaded twice.");
115 IceAddConnectionWatch(new_ice_connection
, m
->core
);
118 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
119 pa_log("Failed to parse module arguments");
123 if (!getenv("SESSION_MANAGER")) {
124 pa_log("X11 session manager not running.");
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
;
138 if (!(m
->userdata
= connection
= SmcOpenConnection(
140 SmProtoMajor
, SmProtoMinor
,
141 SmcSaveYourselfProcMask
| SmcDieProcMask
| SmcSaveCompleteProcMask
| SmcShutdownCancelledProcMask
,
142 &callbacks
, NULL
, &client_id
,
145 pa_log("Failed to open connection to session manager: %s", t
);
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
;
157 prop_user
.name
= (char*) SmUserID
;
158 prop_user
.type
= (char*) SmARRAY8
;
159 pa_get_user_name(t
, sizeof(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
;
166 SmcSetProperties(connection
, PA_ELEMENTSOF(prop_list
), prop_list
);
168 pa_log_info("Connected to session manager '%s' as '%s'.", vendor
= SmcVendor(connection
), client_id
);
185 void pa__done(pa_module
*m
) {
189 SmcCloseConnection(m
->userdata
, 0, NULL
);
192 IceRemoveConnectionWatch(new_ice_connection
, m
->core
);