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