libs: backends: X11 backend code refactoring.
[gfxprim.git] / libs / backends / GP_X11_Conn.h
blobf8bf3bf936b7188b6baf82e70eb7a6da5f7635c9
1 /*****************************************************************************
2 * This file is part of gfxprim library. *
3 * *
4 * Gfxprim is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU Lesser General Public *
6 * License as published by the Free Software Foundation; either *
7 * version 2.1 of the License, or (at your option) any later version. *
8 * *
9 * Gfxprim is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12 * Lesser General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with gfxprim; if not, write to the Free Software *
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301 USA *
18 * *
19 * Copyright (C) 2009-2013 Cyril Hrubis <metan@ucw.cz> *
20 * *
21 *****************************************************************************/
24 * X11 connection, singleton.
26 struct x11_conn {
27 Display *dpy;
29 /* window reference counter */
30 unsigned int ref_cnt;
33 static struct x11_conn x11_conn = {
34 .dpy = NULL,
35 .ref_cnt = 0,
38 static unsigned int x11_open(const char *display)
40 if (x11_conn.ref_cnt != 0)
41 return ++x11_conn.ref_cnt;
43 GP_DEBUG(1, "Opening X11 display '%s'", display);
45 if (!XInitThreads()) {
46 GP_DEBUG(2, "XInitThreads failed");
47 return 0;
50 x11_conn.dpy = XOpenDisplay(display);
52 if (x11_conn.dpy == NULL) {
53 GP_WARN("Failed to initialize X11 display");
54 return 0;
57 /* Initialized key translation table */
58 GP_InputDriverX11Init(x11_conn.dpy);
60 return ++x11_conn.ref_cnt;
63 static void x11_close(void)
65 /* Ignore close requests if connection is closed */
66 if (x11_conn.ref_cnt == 0)
67 return;
69 if (--x11_conn.ref_cnt != 0)
70 return;
72 GP_DEBUG(1, "Closing X11 display");
74 XLockDisplay(x11_conn.dpy);
76 /* I wonder if this is right sequence... */
77 //XUnlockDisplay(x11_conn.dpy);
78 XCloseDisplay(x11_conn.dpy);