]>
code.delx.au - pulseaudio/blob - polyp/module-x11-bell.c
7 #include <X11/XKBlib.h>
17 struct pa_io_event
*io_event
;
18 struct x11_source
*next
;
24 struct x11_source
*x11_sources
;
31 static const char* const valid_modargs
[] = {
38 static int ring_bell(struct userdata
*u
, int percent
) {
42 if (!(s
= pa_namereg_get(u
->core
, u
->sink_name
, PA_NAMEREG_SINK
, 1))) {
43 fprintf(stderr
, __FILE__
": Invalid sink\n");
47 if (pa_scache_play_item(u
->core
, u
->scache_item
, s
, percent
*2) < 0) {
48 fprintf(stderr
, __FILE__
": Failed to play sample\n");
55 static void io_callback(struct pa_mainloop_api
*a
, struct pa_io_event
*e
, int fd
, enum pa_io_event_flags f
, void *userdata
) {
56 struct userdata
*u
= userdata
;
59 while (XPending(u
->display
)) {
61 XkbBellNotifyEvent
*bne
;
62 XNextEvent(u
->display
, &e
);
64 if (((XkbEvent
*) &e
)->any
.xkb_type
!= XkbBellNotify
)
67 bne
= ((XkbBellNotifyEvent
*) &e
);
69 if (ring_bell(u
, bne
->percent
) < 0) {
70 fprintf(stderr
, __FILE__
": Ringing bell failed, reverting to X11 device bell.\n");
71 XkbForceDeviceBell(u
->display
, bne
->device
, bne
->bell_class
, bne
->bell_id
, bne
->percent
);
76 static void new_io_source(struct userdata
*u
, int fd
) {
79 s
= pa_xmalloc(sizeof(struct x11_source
));
80 s
->io_event
= u
->core
->mainloop
->io_new(u
->core
->mainloop
, fd
, PA_IO_EVENT_INPUT
, io_callback
, u
);
82 s
->next
= u
->x11_sources
;
86 int pa_module_init(struct pa_core
*c
, struct pa_module
*m
) {
87 struct userdata
*u
= NULL
;
88 struct pa_modargs
*ma
= NULL
;
90 unsigned int auto_ctrls
, auto_values
;
93 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
94 fprintf(stderr
, __FILE__
": failed to parse module arguments\n");
98 m
->userdata
= u
= pa_xmalloc(sizeof(struct userdata
));
101 u
->x11_sources
= NULL
;
102 u
->scache_item
= pa_xstrdup(pa_modargs_get_value(ma
, "sample", "x11-bell"));
103 u
->sink_name
= pa_xstrdup(pa_modargs_get_value(ma
, "sink", NULL
));
105 if (!(u
->display
= XOpenDisplay(pa_modargs_get_value(ma
, "display", NULL
)))) {
106 fprintf(stderr
, __FILE__
": XOpenDisplay() failed\n");
110 new_io_source(u
, ConnectionNumber(u
->display
));
112 major
= XkbMajorVersion
;
113 minor
= XkbMinorVersion
;
115 if (!XkbLibraryVersion(&major
, &minor
)) {
116 fprintf(stderr
, __FILE__
": XkbLibraryVersion() failed\n");
120 major
= XkbMajorVersion
;
121 minor
= XkbMinorVersion
;
123 if (!XkbQueryExtension(u
->display
, NULL
, &u
->xkb_event_base
, NULL
, &major
, &minor
)) {
124 fprintf(stderr
, __FILE__
": XkbQueryExtension() failed\n");
128 XkbSelectEvents(u
->display
, XkbUseCoreKbd
, XkbBellNotifyMask
, XkbBellNotifyMask
);
129 auto_ctrls
= auto_values
= XkbAudibleBellMask
;
130 XkbSetAutoResetControls(u
->display
, XkbAudibleBellMask
, &auto_ctrls
, &auto_values
);
131 XkbChangeEnabledControls(u
->display
, XkbUseCoreKbd
, XkbAudibleBellMask
, 0);
141 pa_module_done(c
, m
);
145 void pa_module_done(struct pa_core
*c
, struct pa_module
*m
) {
146 struct userdata
*u
= m
->userdata
;
149 while (u
->x11_sources
) {
150 struct x11_source
*s
= u
->x11_sources
;
151 u
->x11_sources
= u
->x11_sources
->next
;
152 c
->mainloop
->io_free(s
->io_event
);
156 pa_xfree(u
->scache_item
);
159 XCloseDisplay(u
->display
);