]>
code.delx.au - pulseaudio/blob - polyp/module-x11-bell.c
4 This file is part of polypaudio.
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.
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.
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
32 #include <X11/XKBlib.h>
42 struct pa_io_event
*io_event
;
43 struct x11_source
*next
;
49 struct x11_source
*x11_sources
;
56 static const char* const valid_modargs
[] = {
63 static int ring_bell(struct userdata
*u
, int percent
) {
67 if (!(s
= pa_namereg_get(u
->core
, u
->sink_name
, PA_NAMEREG_SINK
, 1))) {
68 fprintf(stderr
, __FILE__
": Invalid sink\n");
72 pa_scache_play_item(u
->core
, u
->scache_item
, s
, percent
*2);
76 static void io_callback(struct pa_mainloop_api
*a
, struct pa_io_event
*e
, int fd
, enum pa_io_event_flags f
, void *userdata
) {
77 struct userdata
*u
= userdata
;
80 while (XPending(u
->display
)) {
82 XkbBellNotifyEvent
*bne
;
83 XNextEvent(u
->display
, &e
);
85 if (((XkbEvent
*) &e
)->any
.xkb_type
!= XkbBellNotify
)
88 bne
= ((XkbBellNotifyEvent
*) &e
);
90 if (ring_bell(u
, bne
->percent
) < 0) {
91 fprintf(stderr
, __FILE__
": Ringing bell failed, reverting to X11 device bell.\n");
92 XkbForceDeviceBell(u
->display
, bne
->device
, bne
->bell_class
, bne
->bell_id
, bne
->percent
);
97 static void new_io_source(struct userdata
*u
, int fd
) {
100 s
= pa_xmalloc(sizeof(struct x11_source
));
101 s
->io_event
= u
->core
->mainloop
->io_new(u
->core
->mainloop
, fd
, PA_IO_EVENT_INPUT
, io_callback
, u
);
103 s
->next
= u
->x11_sources
;
107 int pa_module_init(struct pa_core
*c
, struct pa_module
*m
) {
108 struct userdata
*u
= NULL
;
109 struct pa_modargs
*ma
= NULL
;
111 unsigned int auto_ctrls
, auto_values
;
114 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
115 fprintf(stderr
, __FILE__
": failed to parse module arguments\n");
119 m
->userdata
= u
= pa_xmalloc(sizeof(struct userdata
));
122 u
->x11_sources
= NULL
;
123 u
->scache_item
= pa_xstrdup(pa_modargs_get_value(ma
, "sample", "x11-bell"));
124 u
->sink_name
= pa_xstrdup(pa_modargs_get_value(ma
, "sink", NULL
));
126 if (!(u
->display
= XOpenDisplay(pa_modargs_get_value(ma
, "display", NULL
)))) {
127 fprintf(stderr
, __FILE__
": XOpenDisplay() failed\n");
131 new_io_source(u
, ConnectionNumber(u
->display
));
133 major
= XkbMajorVersion
;
134 minor
= XkbMinorVersion
;
136 if (!XkbLibraryVersion(&major
, &minor
)) {
137 fprintf(stderr
, __FILE__
": XkbLibraryVersion() failed\n");
141 major
= XkbMajorVersion
;
142 minor
= XkbMinorVersion
;
144 if (!XkbQueryExtension(u
->display
, NULL
, &u
->xkb_event_base
, NULL
, &major
, &minor
)) {
145 fprintf(stderr
, __FILE__
": XkbQueryExtension() failed\n");
149 XkbSelectEvents(u
->display
, XkbUseCoreKbd
, XkbBellNotifyMask
, XkbBellNotifyMask
);
150 auto_ctrls
= auto_values
= XkbAudibleBellMask
;
151 XkbSetAutoResetControls(u
->display
, XkbAudibleBellMask
, &auto_ctrls
, &auto_values
);
152 XkbChangeEnabledControls(u
->display
, XkbUseCoreKbd
, XkbAudibleBellMask
, 0);
162 pa_module_done(c
, m
);
166 void pa_module_done(struct pa_core
*c
, struct pa_module
*m
) {
167 struct userdata
*u
= m
->userdata
;
170 while (u
->x11_sources
) {
171 struct x11_source
*s
= u
->x11_sources
;
172 u
->x11_sources
= u
->x11_sources
->next
;
173 c
->mainloop
->io_free(s
->io_event
);
177 pa_xfree(u
->scache_item
);
178 pa_xfree(u
->sink_name
);
181 XCloseDisplay(u
->display
);