]> icculus.org git repositories - manmower/openbox.git/blob - obt/instance.c
more precise includes
[manmower/openbox.git] / obt / instance.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    obt/instance.c for the Openbox window manager
4    Copyright (c) 2007        Dana Jansens
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    See the COPYING file for a copy of the GNU General Public License.
17 */
18
19 #include "obt/instance.h"
20 #include "obt/util.h"
21
22 #ifdef HAVE_STRING_H
23 #  include <string.h>
24 #endif
25 #ifdef HAVE_FCNTL_H
26 #  include <fcntl.h>
27 #endif
28 #ifdef HAVE_UNISTD_H
29 #  include <unistd.h>
30 #endif
31
32 struct _ObtInstance
33 {
34     gint ref;
35     Display *d;
36 };
37
38 ObtInstance* obt_instance_new(const char *display_name)
39 {
40     gchar *n;
41     Display *d;
42     ObtInstance *inst = NULL;
43
44     n = display_name ? g_strdup(display_name) : NULL;
45     d = XOpenDisplay(n);
46     if (d) {
47         if (fcntl(ConnectionNumber(d), F_SETFD, 1) == -1)
48             g_message("Failed to set display as close-on-exec");
49
50         inst = g_new(ObtInstance, 1);
51         inst->ref = 1;
52         inst->d = d;
53     }
54     g_free(n);
55
56     return inst;
57 }
58
59 void obt_instance_ref(ObtInstance *inst)
60 {
61     ++inst->ref;
62 }
63
64 void obt_instance_unref(ObtInstance *inst)
65 {
66     if (inst && --inst->ref == 0) {
67         XCloseDisplay(inst->d);
68         obt_free0(inst, ObtInstance, 1);
69     }
70 }
71
72 Display* obt_display(const ObtInstance *inst)
73 {
74     return inst->d;
75 }