]> code.delx.au - pulseaudio/blob - polyp/module-x11-bell.c
add modinfo support
[pulseaudio] / polyp / 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 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 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 "module.h"
35 #include "sink.h"
36 #include "scache.h"
37 #include "modargs.h"
38 #include "xmalloc.h"
39 #include "namereg.h"
40 #include "log.h"
41
42 PA_MODULE_AUTHOR("Lennart Poettering")
43 PA_MODULE_DESCRIPTION("X11 Bell interceptor")
44 PA_MODULE_VERSION(PACKAGE_VERSION)
45 PA_MODULE_USAGE("sink=<sink to connect to> sample=<sample name> display=<X11 display>")
46
47 struct x11_source {
48 struct pa_io_event *io_event;
49 struct x11_source *next;
50 };
51
52 struct userdata {
53 struct pa_core *core;
54 Display *display;
55 struct x11_source *x11_sources;
56 int xkb_event_base;
57
58 char *sink_name;
59 char *scache_item;
60 };
61
62 static const char* const valid_modargs[] = {
63 "sink",
64 "sample",
65 "display",
66 NULL
67 };
68
69 static int ring_bell(struct userdata *u, int percent) {
70 struct pa_sink *s;
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\n");
75 return -1;
76 }
77
78 pa_scache_play_item(u->core, u->scache_item, s, percent*2);
79 return 0;
80 }
81
82 static void io_callback(struct pa_mainloop_api*a, struct pa_io_event *e, int fd, enum pa_io_event_flags f, void *userdata) {
83 struct userdata *u = userdata;
84 assert(u);
85
86 while (XPending(u->display)) {
87 XEvent e;
88 XkbBellNotifyEvent *bne;
89 XNextEvent(u->display, &e);
90
91 if (((XkbEvent*) &e)->any.xkb_type != XkbBellNotify)
92 continue;
93
94 bne = ((XkbBellNotifyEvent*) &e);
95
96 if (ring_bell(u, bne->percent) < 0) {
97 pa_log(__FILE__": Ringing bell failed, reverting to X11 device bell.\n");
98 XkbForceDeviceBell(u->display, bne->device, bne->bell_class, bne->bell_id, bne->percent);
99 }
100 }
101 }
102
103 static void new_io_source(struct userdata *u, int fd) {
104 struct x11_source *s;
105
106 s = pa_xmalloc(sizeof(struct x11_source));
107 s->io_event = u->core->mainloop->io_new(u->core->mainloop, fd, PA_IO_EVENT_INPUT, io_callback, u);
108 assert(s->io_event);
109 s->next = u->x11_sources;
110 u->x11_sources = s;
111 }
112
113 int pa__init(struct pa_core *c, struct pa_module*m) {
114 struct userdata *u = NULL;
115 struct pa_modargs *ma = NULL;
116 int major, minor;
117 unsigned int auto_ctrls, auto_values;
118 assert(c && m);
119
120 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
121 pa_log(__FILE__": failed to parse module arguments\n");
122 goto fail;
123 }
124
125 m->userdata = u = pa_xmalloc(sizeof(struct userdata));
126 u->core = c;
127 u->display = NULL;
128 u->x11_sources = NULL;
129 u->scache_item = pa_xstrdup(pa_modargs_get_value(ma, "sample", "x11-bell"));
130 u->sink_name = pa_xstrdup(pa_modargs_get_value(ma, "sink", NULL));
131
132 if (!(u->display = XOpenDisplay(pa_modargs_get_value(ma, "display", NULL)))) {
133 pa_log(__FILE__": XOpenDisplay() failed\n");
134 goto fail;
135 }
136
137 new_io_source(u, ConnectionNumber(u->display));
138
139 major = XkbMajorVersion;
140 minor = XkbMinorVersion;
141
142 if (!XkbLibraryVersion(&major, &minor)) {
143 pa_log(__FILE__": XkbLibraryVersion() failed\n");
144 goto fail;
145 }
146
147 major = XkbMajorVersion;
148 minor = XkbMinorVersion;
149
150 if (!XkbQueryExtension(u->display, NULL, &u->xkb_event_base, NULL, &major, &minor)) {
151 pa_log(__FILE__": XkbQueryExtension() failed\n");
152 goto fail;
153 }
154
155 XkbSelectEvents(u->display, XkbUseCoreKbd, XkbBellNotifyMask, XkbBellNotifyMask);
156 auto_ctrls = auto_values = XkbAudibleBellMask;
157 XkbSetAutoResetControls(u->display, XkbAudibleBellMask, &auto_ctrls, &auto_values);
158 XkbChangeEnabledControls(u->display, XkbUseCoreKbd, XkbAudibleBellMask, 0);
159
160 pa_modargs_free(ma);
161
162 return 0;
163
164 fail:
165 if (ma)
166 pa_modargs_free(ma);
167 if (m->userdata)
168 pa__done(c, m);
169 return -1;
170 }
171
172 void pa__done(struct pa_core *c, struct pa_module*m) {
173 struct userdata *u = m->userdata;
174 assert(c && m && u);
175
176 while (u->x11_sources) {
177 struct x11_source *s = u->x11_sources;
178 u->x11_sources = u->x11_sources->next;
179 c->mainloop->io_free(s->io_event);
180 pa_xfree(s);
181 }
182
183 pa_xfree(u->scache_item);
184 pa_xfree(u->sink_name);
185
186 if (u->display)
187 XCloseDisplay(u->display);
188 pa_xfree(u);
189 }