]> code.delx.au - pulseaudio/blob - src/modules/module-x11-bell.c
merge 'lennart' branch back into trunk.
[pulseaudio] / src / modules / module-x11-bell.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/XKBlib.h>
34
35 #include <pulse/xmalloc.h>
36
37 #include <pulsecore/iochannel.h>
38 #include <pulsecore/sink.h>
39 #include <pulsecore/core-scache.h>
40 #include <pulsecore/modargs.h>
41 #include <pulsecore/namereg.h>
42 #include <pulsecore/log.h>
43 #include <pulsecore/x11wrap.h>
44
45 #include "module-x11-bell-symdef.h"
46
47 PA_MODULE_AUTHOR("Lennart Poettering")
48 PA_MODULE_DESCRIPTION("X11 Bell interceptor")
49 PA_MODULE_VERSION(PACKAGE_VERSION)
50 PA_MODULE_USAGE("sink=<sink to connect to> sample=<sample name> display=<X11 display>")
51
52 struct userdata {
53 pa_core *core;
54 int xkb_event_base;
55 char *sink_name;
56 char *scache_item;
57
58 pa_x11_wrapper *x11_wrapper;
59 pa_x11_client *x11_client;
60 };
61
62 static const char* const valid_modargs[] = {
63 "sink",
64 "sample",
65 "display",
66 NULL
67 };
68
69 static int x11_event_callback(pa_x11_wrapper *w, XEvent *e, void *userdata) {
70 XkbBellNotifyEvent *bne;
71 struct userdata *u = userdata;
72
73 pa_assert(w);
74 pa_assert(e);
75 pa_assert(u);
76 pa_assert(u->x11_wrapper == w);
77
78 if (((XkbEvent*) e)->any.xkb_type != XkbBellNotify)
79 return 0;
80
81 bne = (XkbBellNotifyEvent*) e;
82
83 if (pa_scache_play_item_by_name(u->core, u->scache_item, u->sink_name, (bne->percent*PA_VOLUME_NORM)/100, 1) < 0) {
84 pa_log_info("Ringing bell failed, reverting to X11 device bell.");
85 XkbForceDeviceBell(pa_x11_wrapper_get_display(w), bne->device, bne->bell_class, bne->bell_id, bne->percent);
86 }
87
88 return 1;
89 }
90
91 int pa__init(pa_module*m) {
92
93 struct userdata *u = NULL;
94 pa_modargs *ma = NULL;
95 int major, minor;
96 unsigned int auto_ctrls, auto_values;
97
98 pa_assert(m);
99
100 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
101 pa_log("Failed to parse module arguments");
102 goto fail;
103 }
104
105 m->userdata = u = pa_xnew(struct userdata, 1);
106 u->core = m->core;
107 u->scache_item = pa_xstrdup(pa_modargs_get_value(ma, "sample", "x11-bell"));
108 u->sink_name = pa_xstrdup(pa_modargs_get_value(ma, "sink", NULL));
109 u->x11_client = NULL;
110
111 if (!(u->x11_wrapper = pa_x11_wrapper_get(m->core, pa_modargs_get_value(ma, "display", NULL))))
112 goto fail;
113
114 major = XkbMajorVersion;
115 minor = XkbMinorVersion;
116
117 if (!XkbLibraryVersion(&major, &minor)) {
118 pa_log("XkbLibraryVersion() failed");
119 goto fail;
120 }
121
122 major = XkbMajorVersion;
123 minor = XkbMinorVersion;
124
125 if (!XkbQueryExtension(pa_x11_wrapper_get_display(u->x11_wrapper), NULL, &u->xkb_event_base, NULL, &major, &minor)) {
126 pa_log("XkbQueryExtension() failed");
127 goto fail;
128 }
129
130 XkbSelectEvents(pa_x11_wrapper_get_display(u->x11_wrapper), XkbUseCoreKbd, XkbBellNotifyMask, XkbBellNotifyMask);
131 auto_ctrls = auto_values = XkbAudibleBellMask;
132 XkbSetAutoResetControls(pa_x11_wrapper_get_display(u->x11_wrapper), XkbAudibleBellMask, &auto_ctrls, &auto_values);
133 XkbChangeEnabledControls(pa_x11_wrapper_get_display(u->x11_wrapper), XkbUseCoreKbd, XkbAudibleBellMask, 0);
134
135 u->x11_client = pa_x11_client_new(u->x11_wrapper, x11_event_callback, u);
136
137 pa_modargs_free(ma);
138
139 return 0;
140
141 fail:
142 if (ma)
143 pa_modargs_free(ma);
144
145 pa__done(m);
146
147 return -1;
148 }
149
150 void pa__done(pa_module*m) {
151 struct userdata *u;
152
153 pa_assert(m);
154
155 if (!m->userdata)
156 return;
157
158 u = m->userdata;
159
160 pa_xfree(u->scache_item);
161 pa_xfree(u->sink_name);
162
163 if (u->x11_client)
164 pa_x11_client_free(u->x11_client);
165
166 if (u->x11_wrapper)
167 pa_x11_wrapper_unref(u->x11_wrapper);
168
169 pa_xfree(u);
170 }