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