UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
x11_common.c
Go to the documentation of this file.
1
5/*
6 * Copyright (c) 2011-2018 CESNET, z. s. p. o.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, is permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * 3. Neither the name of CESNET nor the names of its contributors may be
21 * used to endorse or promote products derived from this software without
22 * specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
26 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
27 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
28 * EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
35 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
47#ifdef HAVE_CONFIG_H
48#include "config.h"
49#include "config_unix.h"
50#include "config_win32.h"
51#endif
52
53#include "x11_common.h"
54
55#include <pthread.h>
56#include <X11/Xlib.h>
57#include <X11/Xutil.h>
58
59#include "debug.h"
61
62#define resource_symbol "X11-state"
63
64struct x11_state {
65 Display *display;
66 pthread_mutex_t lock;
67 int display_opened_here; /* indicates wheather we opened the display
68 in such a case, we count references and if 0,
69 we close it */
72};
73
74struct x11_state *get_state(void);
75
77 struct x11_state *state;
78 rm_lock();
79 state = (struct x11_state *) rm_get_shm(resource_symbol, sizeof(struct x11_state));
80 if(!state->initialized) {
81 state->display = NULL;
82 pthread_mutex_init(&state->lock, NULL);
83 state->display_opened_here = TRUE;
84 state->ref_num = 0;
85 state->initialized = true;
86 }
87 rm_unlock();
88
89 return state;
90}
91
92void x11_set_display(void *disp)
93{
94 struct x11_state *s = get_state();
95 Display *d = disp;
96 if (d == NULL)
97 return;
98 pthread_mutex_lock(&s->lock);
99 if(s->display != NULL) {
100 fprintf(stderr, __FILE__ ": Fatal error: Display already set.\n");
101 abort();
102 }
103 s->display = d;
105 pthread_mutex_unlock(&s->lock);
106}
107
109{
110 struct x11_state *s = get_state();
111 if(!s->display) {
112 s->display = XOpenDisplay(0);
114 }
115
116 if ( !s->display )
117 {
118 fprintf(stderr, "Failed to open X display\n" );
119 return NULL;
120 }
121
122 s->ref_num++;
123 return s->display;
124}
125
126void * x11_get_display(void)
127{
128 struct x11_state *s = get_state();
129 return s->display;
130}
131
133 struct x11_state *s = get_state();
134 s->ref_num--;
135
136 if(s->ref_num < 0) {
137 fprintf(stderr, __FILE__ ": WARNING: Unpaired glx_free call.");
138 }
139
140 if(s->display_opened_here && s->ref_num == 0) {
141 fprintf(stderr, "Display closed (last client disconnected)\n");
142 XCloseDisplay( s->display );
143 s->display = NULL;
144 }
145}
146
147void x11_lock(void)
148{
149 struct x11_state *s = get_state();
150 pthread_mutex_lock(&s->lock);
151}
152
153void x11_unlock(void)
154{
155 struct x11_state *s = get_state();
156 pthread_mutex_unlock(&s->lock);
157}
158
159/*
160 * Copyright (C) 2017 Alberts Muktupāvels
161 * Code to disable window decorations taken from:
162 * https://gist.github.com/muktupavels/d03bb14ea6042b779df89b4c87df975d
163 */
164typedef struct
165{
166 unsigned long flags;
167 unsigned long functions;
168 unsigned long decorations;
170 unsigned long status;
172
173static MotifWmHints * get_motif_wm_hints (Display *display, Window window)
174{
175 Atom property;
176 int result;
177 Atom actual_type;
178 int actual_format;
179 unsigned long nitems;
180 unsigned long bytes_after;
181 unsigned char *data;
182
183 property = XInternAtom (display, "_MOTIF_WM_HINTS", False);
184 result = XGetWindowProperty (display, window, property,
185 0, LONG_MAX, False, AnyPropertyType,
186 &actual_type, &actual_format,
187 &nitems, &bytes_after, &data);
188
189 if (result == Success && data != NULL)
190 {
191 size_t data_size;
192 size_t max_size;
193 MotifWmHints *hints;
194
195 data_size = nitems * sizeof (long);
196 max_size = sizeof (*hints);
197
198 hints = calloc (1, max_size);
199
200 memcpy (hints, data, data_size > max_size ? max_size : data_size);
201 XFree (data);
202
203 return hints;
204 }
205
206 return NULL;
207}
208
209void x11_unset_window_decorations (Display *display, Window window)
210{
211 MotifWmHints *hints;
212 Atom property;
213 int nelements;
214
215 hints = get_motif_wm_hints (display, window);
216 if (hints == NULL)
217 {
218 hints = calloc (1, sizeof (*hints));
219 hints->decorations = (1L << 0);
220 }
221
222 hints->flags |= (1L << 1);
223 hints->decorations = 0;
224
225 property = XInternAtom (display, "_MOTIF_WM_HINTS", False);
226 nelements = sizeof (*hints) / sizeof (long);
227
228 XChangeProperty (display, window, property, property, 32, PropModeReplace,
229 (unsigned char *) hints, nelements);
230
231 free (hints);
232}
233
void ** state
int property
#define TRUE
#define FALSE
void rm_lock()
void * rm_get_shm(const char *name, int size)
void rm_unlock()
unsigned long flags
Definition x11_common.c:166
unsigned long status
Definition x11_common.c:170
unsigned long functions
Definition x11_common.c:167
unsigned long decorations
Definition x11_common.c:168
This struct represents initialized video display state.
Display * display
Definition x11_common.c:65
int display_opened_here
Definition x11_common.c:67
int ref_num
Definition x11_common.c:70
pthread_mutex_t lock
Definition x11_common.c:66
bool initialized
Definition x11_common.c:71
void * x11_acquire_display(void)
Definition x11_common.c:108
static MotifWmHints * get_motif_wm_hints(Display *display, Window window)
Definition x11_common.c:173
void x11_lock(void)
Definition x11_common.c:147
void x11_unlock(void)
Definition x11_common.c:153
void x11_set_display(void *disp)
Definition x11_common.c:92
void * x11_get_display(void)
Definition x11_common.c:126
void x11_unset_window_decorations(Display *display, Window window)
Definition x11_common.c:209
struct x11_state * get_state(void)
Definition x11_common.c:76
#define resource_symbol
Definition x11_common.c:62
void x11_release_display()
Definition x11_common.c:132