1 /*
2 * Copyright © 2013 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 */
24
25 #include <X11/Xlib.h>
26 #include <cairo.h>
27 #include <cairo-xlib.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <stdbool.h>
31 #include <string.h>
32 #include <unistd.h>
33
34 #include "../overlay.h"
35 #include "position.h"
36
37 struct x11_window {
38 struct overlay base;
39 cairo_surface_t *front;
40 Display *dpy;
41 Window win;
42 int width, height;
43 int visible;
44 };
45
to_x11_window(struct overlay * o)46 static inline struct x11_window *to_x11_window(struct overlay *o)
47 {
48 return (struct x11_window *)o;
49 }
50
noop(Display * dpy,XErrorEvent * event)51 static int noop(Display *dpy, XErrorEvent *event)
52 {
53 return 0;
54 }
55
x11_window_show(struct overlay * overlay)56 static void x11_window_show(struct overlay *overlay)
57 {
58 struct x11_window *priv = to_x11_window(overlay);
59 cairo_t *cr;
60
61 cr = cairo_create(priv->front);
62 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
63 cairo_set_source_surface(cr, priv->base.surface, 0, 0);
64 cairo_paint(cr);
65 cairo_destroy(cr);
66
67 cairo_surface_flush(priv->front);
68
69 if (!priv->visible) {
70 XMapWindow(priv->dpy, priv->win);
71 priv->visible = true;
72 }
73
74 XFlush(priv->dpy);
75 }
76
x11_window_hide(struct overlay * overlay)77 static void x11_window_hide(struct overlay *overlay)
78 {
79 struct x11_window *priv = to_x11_window(overlay);
80 if (priv->visible) {
81 XUnmapWindow(priv->dpy, priv->win);
82 XFlush(priv->dpy);
83 priv->visible = false;
84 }
85 }
86
x11_window_destroy(void * data)87 static void x11_window_destroy(void *data)
88 {
89 struct x11_window *priv = data;
90 cairo_surface_destroy(priv->front);
91 XDestroyWindow(priv->dpy, priv->win);
92 XCloseDisplay(priv->dpy);
93 free(priv);
94 }
95
prefer_image(struct config * config)96 static int prefer_image(struct config *config)
97 {
98 const char *v = config_get_value(config, "x11", "prefer-image");
99
100 if (v == NULL)
101 return 0;
102 if (*v == '\0')
103 return 1;
104
105 return atoi(v);
106 }
107
108 cairo_surface_t *
x11_window_create(struct config * config,int * width,int * height)109 x11_window_create(struct config *config, int *width, int *height)
110 {
111 Display *dpy;
112 Window win;
113 int screen;
114 cairo_surface_t *surface;
115 XSetWindowAttributes attr;
116 struct x11_window *priv;
117 int x, y, w, h;
118
119 dpy = XOpenDisplay(NULL);
120 if (dpy == NULL)
121 return NULL;
122
123 screen = DefaultScreen(dpy);
124
125 XSetErrorHandler(noop);
126
127 x11_position(dpy, *width, *height, config, &x, &y, &w, &h);
128
129 attr.override_redirect = True;
130 win = XCreateWindow(dpy, DefaultRootWindow(dpy),
131 x, y, w, h, 0,
132 DefaultDepth(dpy, screen),
133 InputOutput,
134 DefaultVisual(dpy, screen),
135 CWOverrideRedirect, &attr);
136
137 surface = cairo_xlib_surface_create(dpy, win, DefaultVisual (dpy, screen), w, h);
138 if (cairo_surface_status(surface))
139 goto err_win;
140
141 priv = malloc(sizeof(*priv));
142 if (priv == NULL)
143 goto err_surface;
144
145 if (prefer_image(config))
146 priv->base.surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, w, h);
147 else
148 priv->base.surface = cairo_surface_create_similar(surface, CAIRO_CONTENT_COLOR, w, h);
149 if (cairo_surface_status(priv->base.surface))
150 goto err_priv;
151
152 priv->base.show = x11_window_show;
153 priv->base.hide = x11_window_hide;
154
155 priv->dpy = dpy;
156 priv->win = win;
157 priv->front = surface;
158 priv->visible = false;
159
160 priv->width = w;
161 priv->height = h;
162
163 cairo_surface_set_user_data(priv->base.surface, &overlay_key, priv, x11_window_destroy);
164
165 *width = w;
166 *height = h;
167 return priv->base.surface;
168
169 err_priv:
170 free(priv);
171 err_surface:
172 cairo_surface_destroy(surface);
173 err_win:
174 XDestroyWindow(dpy, win);
175 XCloseDisplay(dpy);
176 return NULL;
177 }
178