blob: 55585cfbef43a151b4cb4e2c9f1d876f8520e4a1 [file] [log] [blame]
San Mehata430b2b2014-09-23 08:30:51 -07001/*
2 Copyright (C) 2002-2010 Karl J. Runge <runge@karlrunge.com>
3 All rights reserved.
4
5This file is part of x11vnc.
6
7x11vnc is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or (at
10your option) any later version.
11
12x11vnc is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with x11vnc; if not, write to the Free Software
19Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA
20or see <http://www.gnu.org/licenses/>.
21
22In addition, as a special exception, Karl J. Runge
23gives permission to link the code of its release of x11vnc with the
24OpenSSL project's "OpenSSL" library (or with modified versions of it
25that use the same license as the "OpenSSL" library), and distribute
26the linked executables. You must obey the GNU General Public License
27in all respects for all of the code used other than "OpenSSL". If you
28modify this file, you may extend this exception to your version of the
29file, but you are not obligated to do so. If you do not wish to do
30so, delete this exception statement from your version.
31*/
32
33#ifndef _X11VNC_UTIL_H
34#define _X11VNC_UTIL_H
35
36/* -- util.h -- */
37
38extern int nfix(int i, int n);
39extern int nmin(int n, int m);
40extern int nmax(int n, int m);
41extern int nabs(int n);
42extern double dabs(double x);
43extern void lowercase(char *str);
44extern void uppercase(char *str);
45extern char *lblanks(char *str);
46extern void strzero(char *str);
47extern int scan_hexdec(char *str, unsigned long *num);
48extern int parse_geom(char *str, int *wp, int *hp, int *xp, int *yp, int W, int H);
49extern void set_env(char *name, char *value);
50extern char *bitprint(unsigned int st, int nbits);
51extern char *get_user_name(void);
52extern char *get_home_dir(void);
53extern char *get_shell(void);
54extern char *this_host(void);
55
56extern int match_str_list(char *str, char **list);
57extern char **create_str_list(char *cslist);
58
59extern double dtime(double *);
60extern double dtime0(double *);
61extern double dnow(void);
62extern double dnowx(void);
63extern double rnow(void);
64extern double rfac(void);
65
66extern int rfbPE(long usec);
67extern void rfbCFD(long usec);
68extern double rect_overlap(int x1, int y1, int x2, int y2, int X1, int Y1,
69 int X2, int Y2);
70extern 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... */
81extern 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
103extern MUTEX(x11Mutex);
104extern MUTEX(scrollMutex);
105MUTEX(clientMutex);
106MUTEX(inputMutex);
107MUTEX(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
116extern 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 */