split the header up for obt
[dana/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/obt.h"
20
21 #ifdef HAVE_STRING_H
22 #  include <string.h>
23 #endif
24 #ifdef HAVE_FCNTL_H
25 #  include <fcntl.h>
26 #endif
27 #ifdef HAVE_UNISTD_H
28 #  include <unistd.h>
29 #endif
30
31 struct _ObtInstance
32 {
33     gint ref;
34     Display *d;
35 };
36
37 ObtInstance* obt_instance_new(const char *display_name)
38 {
39     gchar *n;
40     Display *d;
41     ObtInstance *inst = NULL;
42
43     n = display_name ? g_strdup(display_name) : NULL;
44     d = XOpenDisplay(n);
45     if (d) {
46         if (fcntl(ConnectionNumber(d), F_SETFD, 1) == -1)
47             g_message("Failed to set display as close-on-exec");
48
49         inst = g_new(ObtInstance, 1);
50         inst->ref = 1;
51         inst->d = d;
52     }
53     g_free(n);
54
55     return inst;
56 }
57
58 void obt_instance_ref(ObtInstance *inst)
59 {
60     ++inst->ref;
61 }
62
63 void obt_instance_unref(ObtInstance *inst)
64 {
65     if (inst && --inst->ref == 0) {
66         XCloseDisplay(inst->d);
67         obt_free0(inst, ObtInstance, 1);
68     }
69 }
70
71 Display* obt_display(const ObtInstance *inst)
72 {
73     return inst->d;
74 }