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