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
);
51 PA_MODULE_LOAD_ONCE(TRUE
);
53 static int ice_in_use
= 0;
55 static const char* const valid_modargs
[] = {
59 static void die_cb(SmcConn connection
, SmPointer client_data
){
60 pa_core
*c
= PA_CORE(client_data
);
62 pa_log_debug("Got die message from XSM. Exiting...");
64 pa_core_assert_ref(c
);
65 c
->mainloop
->quit(c
->mainloop
, 0);
68 static void save_complete_cb(SmcConn connection
, SmPointer client_data
) {
71 static void shutdown_cancelled_cb(SmcConn connection
, SmPointer client_data
) {
72 SmcSaveYourselfDone(connection
, True
);
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
);
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
;
82 if (IceProcessMessages(connection
, NULL
, NULL
) == IceProcessMessagesIOError
) {
83 IceSetShutdownNegotiation(connection
, False
);
84 IceCloseConnection(connection
);
88 static void new_ice_connection(IceConn connection
, IcePointer client_data
, Bool opening
, IcePointer
*watch_data
) {
89 pa_core
*c
= client_data
;
94 *watch_data
= c
->mainloop
->io_new(c
->mainloop
, IceConnectionNumber(connection
), PA_IO_EVENT_INPUT
, ice_io_cb
, connection
);
96 c
->mainloop
->io_free(*watch_data
);
99 int pa__init(pa_module
*m
) {
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
;
112 pa_log("module-x11-xsmp may no be loaded twice.");
116 IceAddConnectionWatch(new_ice_connection
, m
->core
);
119 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
120 pa_log("Failed to parse module arguments");
124 if (!getenv("SESSION_MANAGER")) {
125 pa_log("X11 session manager not running.");
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
;
139 if (!(m
->userdata
= connection
= SmcOpenConnection(
141 SmProtoMajor
, SmProtoMinor
,
142 SmcSaveYourselfProcMask
| SmcDieProcMask
| SmcSaveCompleteProcMask
| SmcShutdownCancelledProcMask
,
143 &callbacks
, NULL
, &client_id
,
146 pa_log("Failed to open connection to session manager: %s", t
);
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
;
158 prop_user
.name
= (char*) SmUserID
;
159 prop_user
.type
= (char*) SmARRAY8
;
160 pa_get_user_name(t
, sizeof(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
;
167 SmcSetProperties(connection
, PA_ELEMENTSOF(prop_list
), prop_list
);
169 pa_log_info("Connected to session manager '%s' as '%s'.", vendor
= SmcVendor(connection
), client_id
);
186 void pa__done(pa_module
*m
) {
190 SmcCloseConnection(m
->userdata
, 0, NULL
);
193 IceRemoveConnectionWatch(new_ice_connection
, m
->core
);