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 #ifndef _X11VNC_UTIL_H
34 #define _X11VNC_UTIL_H
35 
36 /* -- util.h -- */
37 
38 extern int nfix(int i, int n);
39 extern int nmin(int n, int m);
40 extern int nmax(int n, int m);
41 extern int nabs(int n);
42 extern double dabs(double x);
43 extern void lowercase(char *str);
44 extern void uppercase(char *str);
45 extern char *lblanks(char *str);
46 extern void strzero(char *str);
47 extern int scan_hexdec(char *str, unsigned long *num);
48 extern int parse_geom(char *str, int *wp, int *hp, int *xp, int *yp, int W, int H);
49 extern void set_env(char *name, char *value);
50 extern char *bitprint(unsigned int st, int nbits);
51 extern char *get_user_name(void);
52 extern char *get_home_dir(void);
53 extern char *get_shell(void);
54 extern char *this_host(void);
55 
56 extern int match_str_list(char *str, char **list);
57 extern char **create_str_list(char *cslist);
58 
59 extern double dtime(double *);
60 extern double dtime0(double *);
61 extern double dnow(void);
62 extern double dnowx(void);
63 extern double rnow(void);
64 extern double rfac(void);
65 
66 extern int rfbPE(long usec);
67 extern void rfbCFD(long usec);
68 extern double rect_overlap(int x1, int y1, int x2, int y2, int X1, int Y1,
69     int X2, int Y2);
70 extern char *choose_title(char *display);
71 
72 
73 #define NONUL(x) ((x) ? (x) : "")
74 
75 /*
76 	Put this in usleep2() for debug printout.
77 	fprintf(stderr, "_mysleep: %08d %10.6f %s:%d\n", (x), dnow() - x11vnc_start, __FILE__, __LINE__); \
78  */
79 
80 /* XXX usleep(3) is not thread safe on some older systems... */
81 extern struct timeval _mysleep;
82 #define usleep2(x) \
83 	_mysleep.tv_sec  = (x) / 1000000; \
84 	_mysleep.tv_usec = (x) % 1000000; \
85 	select(0, NULL, NULL, NULL, &_mysleep);
86 #if !defined(X11VNC_USLEEP)
87 #undef usleep
88 #define usleep usleep2
89 #endif
90 
91 /*
92  * following is based on IsModifierKey in Xutil.h
93 */
94 #define ismodkey(keysym) \
95   ((((KeySym)(keysym) >= XK_Shift_L) && ((KeySym)(keysym) <= XK_Hyper_R) && \
96   ((KeySym)(keysym) != XK_Caps_Lock) && ((KeySym)(keysym) != XK_Shift_Lock)))
97 
98 /*
99  * When threaded we have to mutex our X11 calls to avoid XIO crashes
100  * due to callbacks.
101  */
102 #ifdef LIBVNCSERVER_HAVE_LIBPTHREAD
103 extern MUTEX(x11Mutex);
104 extern MUTEX(scrollMutex);
105 MUTEX(clientMutex);
106 MUTEX(inputMutex);
107 MUTEX(pointerMutex);
108 #endif
109 
110 #define X_INIT INIT_MUTEX(x11Mutex)
111 
112 #if 1
113 #define X_LOCK       LOCK(x11Mutex)
114 #define X_UNLOCK   UNLOCK(x11Mutex)
115 #else
116 extern int hxl;
117 #define X_LOCK       fprintf(stderr, "*** X_LOCK**  %d%s  %s:%d\n", \
118 	hxl, hxl  ? "  BAD-PRE-LOCK":"",   __FILE__, __LINE__);   LOCK(x11Mutex); hxl = 1;
119 #define X_UNLOCK     fprintf(stderr, "    x_unlock  %d%s  %s:%d\n", \
120 	hxl, !hxl ? "  BAD-PRE-UNLOCK":"", __FILE__, __LINE__); UNLOCK(x11Mutex); hxl = 0;
121 #endif
122 
123 #define SCR_LOCK   if (use_threads) {LOCK(scrollMutex);}
124 #define SCR_UNLOCK if (use_threads) {UNLOCK(scrollMutex);}
125 #define SCR_INIT INIT_MUTEX(scrollMutex)
126 
127 #define CLIENT_LOCK   if (use_threads) {LOCK(clientMutex);}
128 #define CLIENT_UNLOCK if (use_threads) {UNLOCK(clientMutex);}
129 #define CLIENT_INIT INIT_MUTEX(clientMutex)
130 
131 #if 1
132 #define INPUT_LOCK   if (use_threads) {LOCK(inputMutex);}
133 #define INPUT_UNLOCK if (use_threads) {UNLOCK(inputMutex);}
134 #else
135 #define INPUT_LOCK
136 #define INPUT_UNLOCK
137 #endif
138 #define INPUT_INIT INIT_MUTEX(inputMutex)
139 
140 #define POINTER_LOCK   if (use_threads) {LOCK(pointerMutex);}
141 #define POINTER_UNLOCK if (use_threads) {UNLOCK(pointerMutex);}
142 #define POINTER_INIT INIT_MUTEX(pointerMutex)
143 
144 /*
145  * The sendMutex member was added to libvncserver 0.9.8
146  * rfb/rfb.h sets LIBVNCSERVER_SEND_MUTEX if present.
147  */
148 #if LIBVNCSERVER_HAVE_LIBPTHREAD && defined(LIBVNCSERVER_SEND_MUTEX)
149 #define SEND_LOCK(cl)   if (use_threads)   LOCK((cl)->sendMutex);
150 #define SEND_UNLOCK(cl) if (use_threads) UNLOCK((cl)->sendMutex);
151 #else
152 #define SEND_LOCK(cl)
153 #define SEND_UNLOCK(cl)
154 #endif
155 
156 #endif /* _X11VNC_UTIL_H */
157