]> code.delx.au - pulseaudio/blob - src/modules/x11/module-x11-bell.c
modules: Fix resource leak in oss
[pulseaudio] / src / modules / x11 / module-x11-bell.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2.1 of the License,
9 or (at your option) any later version.
10
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <stdlib.h>
28
29 #include <X11/Xlib.h>
30 #include <X11/XKBlib.h>
31
32 #include <pulse/xmalloc.h>
33
34 #include <pulsecore/core-scache.h>
35 #include <pulsecore/modargs.h>
36 #include <pulsecore/log.h>
37 #include <pulsecore/x11wrap.h>
38
39 #include "module-x11-bell-symdef.h"
40
41 PA_MODULE_AUTHOR("Lennart Poettering");
42 PA_MODULE_DESCRIPTION("X11 bell interceptor");
43 PA_MODULE_VERSION(PACKAGE_VERSION);
44 PA_MODULE_LOAD_ONCE(false);
45 PA_MODULE_USAGE("sink=<sink to connect to> sample=<sample name> display=<X11 display>");
46
47 static const char* const valid_modargs[] = {
48 "sink",
49 "sample",
50 "display",
51 NULL
52 };
53
54 struct userdata {
55 pa_core *core;
56 pa_module *module;
57
58 int xkb_event_base;
59
60 char *sink_name;
61 char *scache_item;
62
63 pa_x11_wrapper *x11_wrapper;
64 pa_x11_client *x11_client;
65 };
66
67 static int x11_event_cb(pa_x11_wrapper *w, XEvent *e, void *userdata) {
68 XkbBellNotifyEvent *bne;
69 struct userdata *u = userdata;
70
71 pa_assert(w);
72 pa_assert(e);
73 pa_assert(u);
74 pa_assert(u->x11_wrapper == w);
75
76 if (((XkbEvent*) e)->any.xkb_type != XkbBellNotify)
77 return 0;
78
79 bne = (XkbBellNotifyEvent*) e;
80
81 if (pa_scache_play_item_by_name(u->core, u->scache_item, u->sink_name, ((pa_volume_t) bne->percent*PA_VOLUME_NORM)/100U, NULL, NULL) < 0) {
82 pa_log_info("Ringing bell failed, reverting to X11 device bell.");
83 XkbForceDeviceBell(pa_x11_wrapper_get_display(w), bne->device, bne->bell_class, bne->bell_id, bne->percent);
84 }
85
86 return 1;
87 }
88
89 static void x11_kill_cb(pa_x11_wrapper *w, void *userdata) {
90 struct userdata *u = userdata;
91
92 pa_assert(w);
93 pa_assert(u);
94 pa_assert(u->x11_wrapper == w);
95
96 if (u->x11_client)
97 pa_x11_client_free(u->x11_client);
98
99 if (u->x11_wrapper)
100 pa_x11_wrapper_unref(u->x11_wrapper);
101
102 u->x11_client = NULL;
103 u->x11_wrapper = NULL;
104
105 pa_module_unload_request(u->module, true);
106 }
107
108 int pa__init(pa_module*m) {
109
110 struct userdata *u = NULL;
111 pa_modargs *ma = NULL;
112 int major, minor;
113 unsigned int auto_ctrls, auto_values;
114
115 pa_assert(m);
116
117 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
118 pa_log("Failed to parse module arguments");
119 goto fail;
120 }
121
122 m->userdata = u = pa_xnew(struct userdata, 1);
123 u->core = m->core;
124 u->module = m;
125 u->scache_item = pa_xstrdup(pa_modargs_get_value(ma, "sample", "bell-window-system"));
126 u->sink_name = pa_xstrdup(pa_modargs_get_value(ma, "sink", NULL));
127 u->x11_client = NULL;
128
129 if (!(u->x11_wrapper = pa_x11_wrapper_get(m->core, pa_modargs_get_value(ma, "display", NULL))))
130 goto fail;
131
132 major = XkbMajorVersion;
133 minor = XkbMinorVersion;
134
135 if (!XkbLibraryVersion(&major, &minor)) {
136 pa_log("XkbLibraryVersion() failed");
137 goto fail;
138 }
139
140 major = XkbMajorVersion;
141 minor = XkbMinorVersion;
142
143 if (!XkbQueryExtension(pa_x11_wrapper_get_display(u->x11_wrapper), NULL, &u->xkb_event_base, NULL, &major, &minor)) {
144 pa_log("XkbQueryExtension() failed");
145 goto fail;
146 }
147
148 XkbSelectEvents(pa_x11_wrapper_get_display(u->x11_wrapper), XkbUseCoreKbd, XkbBellNotifyMask, XkbBellNotifyMask);
149 auto_ctrls = auto_values = XkbAudibleBellMask;
150 XkbSetAutoResetControls(pa_x11_wrapper_get_display(u->x11_wrapper), XkbAudibleBellMask, &auto_ctrls, &auto_values);
151 XkbChangeEnabledControls(pa_x11_wrapper_get_display(u->x11_wrapper), XkbUseCoreKbd, XkbAudibleBellMask, 0);
152
153 u->x11_client = pa_x11_client_new(u->x11_wrapper, x11_event_cb, x11_kill_cb, u);
154
155 pa_modargs_free(ma);
156
157 return 0;
158
159 fail:
160 if (ma)
161 pa_modargs_free(ma);
162
163 pa__done(m);
164
165 return -1;
166 }
167
168 void pa__done(pa_module*m) {
169 struct userdata *u;
170
171 pa_assert(m);
172
173 if (!(u = m->userdata))
174 return;
175
176 pa_xfree(u->scache_item);
177 pa_xfree(u->sink_name);
178
179 if (u->x11_client)
180 pa_x11_client_free(u->x11_client);
181
182 if (u->x11_wrapper)
183 pa_x11_wrapper_unref(u->x11_wrapper);
184
185 pa_xfree(u);
186 }