]> code.delx.au - pulseaudio/blob - src/modules/module-x11-bell.c
Reorganised the source tree. We now have src/ with a couple of subdirs:
[pulseaudio] / src / modules / module-x11-bell.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio 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 of the License,
9 or (at your option) any later version.
10
11 polypaudio 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 polypaudio; 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 <assert.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include <X11/Xlib.h>
32 #include <X11/XKBlib.h>
33
34 #include <polypcore/iochannel.h>
35 #include <polypcore/sink.h>
36 #include <polypcore/scache.h>
37 #include <polypcore/modargs.h>
38 #include <polypcore/xmalloc.h>
39 #include <polypcore/namereg.h>
40 #include <polypcore/log.h>
41 #include <polypcore/x11wrap.h>
42
43 #include "module-x11-bell-symdef.h"
44
45 PA_MODULE_AUTHOR("Lennart Poettering")
46 PA_MODULE_DESCRIPTION("X11 Bell interceptor")
47 PA_MODULE_VERSION(PACKAGE_VERSION)
48 PA_MODULE_USAGE("sink=<sink to connect to> sample=<sample name> display=<X11 display>")
49
50 struct userdata {
51 pa_core *core;
52 int xkb_event_base;
53 char *sink_name;
54 char *scache_item;
55 Display *display;
56
57 pa_x11_wrapper *x11_wrapper;
58 pa_x11_client *x11_client;
59 };
60
61 static const char* const valid_modargs[] = {
62 "sink",
63 "sample",
64 "display",
65 NULL
66 };
67
68 static int ring_bell(struct userdata *u, int percent) {
69 pa_sink *s;
70 pa_cvolume cv;
71 assert(u);
72
73 if (!(s = pa_namereg_get(u->core, u->sink_name, PA_NAMEREG_SINK, 1))) {
74 pa_log(__FILE__": Invalid sink: %s\n", u->sink_name);
75 return -1;
76 }
77
78 pa_scache_play_item(u->core, u->scache_item, s, pa_cvolume_set(&cv, PA_CHANNELS_MAX, percent*PA_VOLUME_NORM/100));
79 return 0;
80 }
81
82 static int x11_event_callback(pa_x11_wrapper *w, XEvent *e, void *userdata) {
83 XkbBellNotifyEvent *bne;
84 struct userdata *u = userdata;
85 assert(w && e && u && u->x11_wrapper == w);
86
87 if (((XkbEvent*) e)->any.xkb_type != XkbBellNotify)
88 return 0;
89
90 bne = (XkbBellNotifyEvent*) e;
91
92 if (ring_bell(u, bne->percent) < 0) {
93 pa_log_info(__FILE__": Ringing bell failed, reverting to X11 device bell.\n");
94 XkbForceDeviceBell(pa_x11_wrapper_get_display(w), bne->device, bne->bell_class, bne->bell_id, bne->percent);
95 }
96
97 return 1;
98 }
99
100 int pa__init(pa_core *c, pa_module*m) {
101 struct userdata *u = NULL;
102 pa_modargs *ma = NULL;
103 int major, minor;
104 unsigned int auto_ctrls, auto_values;
105 assert(c && m);
106
107 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
108 pa_log(__FILE__": failed to parse module arguments\n");
109 goto fail;
110 }
111
112 m->userdata = u = pa_xmalloc(sizeof(struct userdata));
113 u->core = c;
114 u->scache_item = pa_xstrdup(pa_modargs_get_value(ma, "sample", "x11-bell"));
115 u->sink_name = pa_xstrdup(pa_modargs_get_value(ma, "sink", NULL));
116 u->x11_client = NULL;
117
118 if (!(u->x11_wrapper = pa_x11_wrapper_get(c, pa_modargs_get_value(ma, "display", NULL))))
119 goto fail;
120
121 u->display = pa_x11_wrapper_get_display(u->x11_wrapper);
122
123 major = XkbMajorVersion;
124 minor = XkbMinorVersion;
125
126 if (!XkbLibraryVersion(&major, &minor)) {
127 pa_log(__FILE__": XkbLibraryVersion() failed\n");
128 goto fail;
129 }
130
131 major = XkbMajorVersion;
132 minor = XkbMinorVersion;
133
134
135 if (!XkbQueryExtension(u->display, NULL, &u->xkb_event_base, NULL, &major, &minor)) {
136 pa_log(__FILE__": XkbQueryExtension() failed\n");
137 goto fail;
138 }
139
140 XkbSelectEvents(u->display, XkbUseCoreKbd, XkbBellNotifyMask, XkbBellNotifyMask);
141 auto_ctrls = auto_values = XkbAudibleBellMask;
142 XkbSetAutoResetControls(u->display, XkbAudibleBellMask, &auto_ctrls, &auto_values);
143 XkbChangeEnabledControls(u->display, XkbUseCoreKbd, XkbAudibleBellMask, 0);
144
145 u->x11_client = pa_x11_client_new(u->x11_wrapper, x11_event_callback, u);
146
147 pa_modargs_free(ma);
148
149 return 0;
150
151 fail:
152 if (ma)
153 pa_modargs_free(ma);
154 if (m->userdata)
155 pa__done(c, m);
156 return -1;
157 }
158
159 void pa__done(pa_core *c, pa_module*m) {
160 struct userdata *u = m->userdata;
161 assert(c && m && u);
162
163 pa_xfree(u->scache_item);
164 pa_xfree(u->sink_name);
165
166 if (u->x11_client)
167 pa_x11_client_free(u->x11_client);
168
169 if (u->x11_wrapper)
170 pa_x11_wrapper_unref(u->x11_wrapper);
171
172 pa_xfree(u);
173 }