1 /*
2    Copyright (C) 2002-2010 Karl J. Runge <runge@karlrunge.com>
3    All rights reserved.
4 
5 This file is part of x11vnc.
6 
7 x11vnc is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or (at
10 your option) any later version.
11 
12 x11vnc is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with x11vnc; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA
20 or see <http://www.gnu.org/licenses/>.
21 
22 In addition, as a special exception, Karl J. Runge
23 gives permission to link the code of its release of x11vnc with the
24 OpenSSL project's "OpenSSL" library (or with modified versions of it
25 that use the same license as the "OpenSSL" library), and distribute
26 the linked executables.  You must obey the GNU General Public License
27 in all respects for all of the code used other than "OpenSSL".  If you
28 modify this file, you may extend this exception to your version of the
29 file, but you are not obligated to do so.  If you do not wish to do
30 so, delete this exception statement from your version.
31 */
32 
33 /* -- xkb_bell.c -- */
34 
35 #include "x11vnc.h"
36 #include "xwrappers.h"
37 #include "connections.h"
38 
39 /*
40  * Bell event handling.  Requires XKEYBOARD extension.
41  */
42 int xkb_base_event_type = 0;
43 
44 void initialize_xkb(void);
45 void initialize_watch_bell(void);
46 void check_bell_event(void);
47 
48 
49 #if LIBVNCSERVER_HAVE_XKEYBOARD
50 
51 /*
52  * check for XKEYBOARD, set up xkb_base_event_type
53  */
initialize_xkb(void)54 void initialize_xkb(void) {
55 	int ir, reason;
56 	int op, ev, er, maj, min;
57 
58 	RAWFB_RET_VOID
59 
60 	if (xkbcompat) {
61 		xkb_present = 0;
62 	} else if (! XkbQueryExtension(dpy, &op, &ev, &er, &maj, &min)) {
63 		if (! quiet) {
64 			rfbLog("warning: XKEYBOARD extension not present.\n");
65 		}
66 		xkb_present = 0;
67 	} else {
68 		xkb_present = 1;
69 	}
70 
71 	if (! xkb_present) {
72 		return;
73 	}
74 
75 	if (! xauth_raw(1)) {
76 		return;
77 	}
78 
79 	if (! XkbOpenDisplay(DisplayString(dpy), &xkb_base_event_type, &ir,
80 	    NULL, NULL, &reason) ) {
81 		if (! quiet) {
82 			rfbLog("warning: disabling XKEYBOARD. XkbOpenDisplay"
83 			    " failed.\n");
84 		}
85 		xkb_base_event_type = 0;
86 		xkb_present = 0;
87 	}
88 	xauth_raw(0);
89 }
90 
initialize_watch_bell(void)91 void initialize_watch_bell(void) {
92 	if (! xkb_present) {
93 		if (! quiet) {
94 			rfbLog("warning: disabling bell. XKEYBOARD ext. "
95 			    "not present.\n");
96 		}
97 		watch_bell = 0;
98 		sound_bell = 0;
99 		return;
100 	}
101 
102 	RAWFB_RET_VOID
103 
104 	XkbSelectEvents(dpy, XkbUseCoreKbd, XkbBellNotifyMask, 0);
105 
106 	if (! watch_bell) {
107 		return;
108 	}
109 	if (! XkbSelectEvents(dpy, XkbUseCoreKbd, XkbBellNotifyMask,
110 	    XkbBellNotifyMask) ) {
111 		if (! quiet) {
112 			rfbLog("warning: disabling bell. XkbSelectEvents"
113 			    " failed.\n");
114 		}
115 		watch_bell = 0;
116 		sound_bell = 0;
117 	}
118 }
119 
120 /*
121  * We call this periodically to process any bell events that have
122  * taken place.
123  */
check_bell_event(void)124 void check_bell_event(void) {
125 	XEvent xev;
126 	XkbAnyEvent *xkb_ev;
127 	int got_bell = 0;
128 
129 	if (! xkb_base_event_type) {
130 		return;
131 	}
132 	RAWFB_RET_VOID
133 
134 	/* caller does X_LOCK */
135 	if (! XCheckTypedEvent(dpy, xkb_base_event_type, &xev)) {
136 		return;
137 	}
138 	if (! watch_bell) {
139 		/* we return here to avoid xkb events piling up */
140 		return;
141 	}
142 
143 	xkb_ev = (XkbAnyEvent *) &xev;
144 	if (xkb_ev->xkb_type == XkbBellNotify) {
145 		got_bell = 1;
146 	}
147 
148 	if (got_bell && sound_bell) {
149 		if (! all_clients_initialized()) {
150 			rfbLog("check_bell_event: not sending bell: "
151 			    "uninitialized clients\n");
152 		} else {
153 			if (screen && client_count) {
154 				rfbSendBell(screen);
155 			}
156 		}
157 	}
158 }
159 #else
initialize_watch_bell(void)160 void initialize_watch_bell(void) {
161 	watch_bell = 0;
162 	sound_bell = 0;
163 }
check_bell_event(void)164 void check_bell_event(void) {}
165 #endif
166 
167 
168