File: opacity.c

package info (click to toggle)
unagi 0.3.4-1
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, buster, jessie, jessie-kfreebsd, stretch, trixie
  • size: 1,932 kB
  • ctags: 534
  • sloc: sh: 11,119; ansic: 3,731; makefile: 74
file content (366 lines) | stat: -rw-r--r-- 10,821 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/* -*-mode:c;coding:utf-8; c-basic-offset:2;fill-column:70;c-file-style:"gnu"-*-
 *
 * Copyright (C) 2009 Arnaud "arnau" Fontaine <arnau@mini-dweeb.org>
 *
 * This  program is  free  software: you  can  redistribute it  and/or
 * modify  it under the  terms of  the GNU  General Public  License as
 * published by the Free Software  Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT  ANY  WARRANTY;  without   even  the  implied  warranty  of
 * MERCHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU
 * General Public License for more details.
 *
 * You should have  received a copy of the  GNU General Public License
 *  along      with      this      program.      If      not,      see
 *  <http://www.gnu.org/licenses/>.
 */

/** \file
 *  \brief Opacity effect plugin
 *
 *  This  plugin handles windows  opacity.  It  relies on  a structure
 *  containing,  for   each  mapped  (or   viewable)  'window_t',  its
 *  'opacity' and 'cookie', namely 'opacity_window_t'.
 *
 *  The  cookie  is the  GetProperty  request  sent  on MapNotify  and
 *  PropertyNotify events  and whose reply  is only got  when actually
 *  getting the window opacity (this  allows to take full advantage of
 *  XCB asynchronous model)
 */

#include <assert.h>
#include <stdlib.h>

#include <xcb/xcb.h>

#include "structs.h"
#include "util.h"
#include "window.h"
#include "atoms.h"
#include "display.h"

/** Opaque opacity value */
#define OPACITY_OPAQUE 0xffffffff

/** Hold global variables of this plugin */
typedef struct _opacity_window_t
{
  /** The list of windows with opacity */
  window_t *window;
  /** The GetProperty request cookie for this window */
  xcb_get_property_cookie_t cookie;
  /** Opacity value */
  uint32_t opacity;
  /** Pointer to the next window with opacity set */
  struct _opacity_window_t *next;
} opacity_window_t;

opacity_window_t *_opacity_windows = NULL;

/** Send the request to get the _NET_WM_WINDOW_OPACITY Atom of a given
 *  window     as    EWMH     specification     does    not     define
 *  _NET_WM_WINDOW_OPACITY
 *
 * \param window_id The Window XID
 * \return The GetProperty cookie associated with the request
 */
static inline xcb_get_property_cookie_t
_opacity_get_property(xcb_window_t window_id)
{
  xcb_get_property_cookie_t cookie =
    xcb_get_property_unchecked(globalconf.connection, 0, window_id,
                               _NET_WM_WINDOW_OPACITY, XCB_ATOM_CARDINAL,
                               0, 1);

  /* Flush to  make sure the request  is sent ASAP and  avoid blocking
     later on */
  xcb_flush(globalconf.connection);
  return cookie;
}

/** Get the  reply of the previously  sent request to  get the opacity
 *  property of a Window
 *
 * \param cookie The GetProperty request cookie
 * \return The opacity value as 32-bits unsigned integer
 */
static uint32_t
_opacity_get_property_reply(xcb_get_property_cookie_t cookie)
{
  xcb_get_property_reply_t *reply =
    xcb_get_property_reply(globalconf.connection, cookie, NULL);

  uint32_t opacity;

  /* If the reply is not valid  or there was an error, then the window
     is considered as opaque */
  if(!reply || reply->type != XCB_ATOM_CARDINAL || reply->format != 32 ||
     !xcb_get_property_value_length(reply))
    opacity = OPACITY_OPAQUE;
  else
    opacity = *((uint32_t *) xcb_get_property_value(reply));

  debug("window_get_opacity_property_reply: opacity: %x", opacity);

  free(reply);
  return opacity;
}

/** Create a new opacity window specific to this plugin
 *
 * \param window The window to be added
 * \return The newly allocated opacity window
 */
static opacity_window_t *
_opacity_window_new(window_t *window)
{
  opacity_window_t *new_opacity_window = calloc(1, sizeof(opacity_window_t));
  new_opacity_window->window = window;

  /* Consider the window  as opaque by default but  send a GetProperty
     request to get the actual  property value (this way get the reply
     as late as possible) */
  new_opacity_window->cookie = _opacity_get_property(window->id);
  new_opacity_window->opacity = OPACITY_OPAQUE;

  return new_opacity_window;
}

static inline void
_opacity_free_property_reply(opacity_window_t *opacity_window)
{
  if(opacity_window->cookie.sequence != 0)
    free(xcb_get_property_reply(globalconf.connection,
				opacity_window->cookie,
				NULL));  
}

static inline void
_opacity_free_window(opacity_window_t *opacity_window)
{
  _opacity_free_property_reply(opacity_window);
  free(opacity_window);
}

/** Manage existing windows
 *
 * \param nwindows The number of windows to manage
 * \param windows The windows to manage
 */
static void
opacity_window_manage_existing(const int nwindows,
			       window_t **windows)
{
  opacity_window_t *opacity_windows_tail = NULL;

  for(int nwindow = 0; nwindow < nwindows; nwindow++)
    {
      /* Only managed windows which are mapped */
      if(!windows[nwindow]->attributes ||
	 windows[nwindow]->attributes->map_state != XCB_MAP_STATE_VIEWABLE)
	continue;

      debug("Managing window %jx", (uintmax_t) windows[nwindow]->id);

      if(!_opacity_windows)
	{
	  _opacity_windows = _opacity_window_new(windows[nwindow]);
	  opacity_windows_tail = _opacity_windows;
	}
      else
	{
	  opacity_windows_tail->next = _opacity_window_new(windows[nwindow]);
	  opacity_windows_tail = opacity_windows_tail->next;
	}
    }
}

/** Get the window opacity
 *
 * \param window The window object to get opacity from
 * \return The window opacity as a 16-bits digit (ARGB)
 */
static uint16_t
opacity_get_window_opacity(const window_t *window)
{
  opacity_window_t *opacity_window;
  for(opacity_window = _opacity_windows;
      opacity_window && opacity_window->window != window;
      opacity_window = opacity_window->next)
    ;

  /* Can't find this window, maybe  because it comes from a plugin, so
     consider it as opaque */
  if(!opacity_window)
    return UINT16_MAX;

  /* Request the reply for the GetProperty request previously sent */
  if(opacity_window->cookie.sequence != 0)
    {
      opacity_window->opacity = _opacity_get_property_reply(opacity_window->cookie);
      opacity_window->cookie.sequence = 0;
    }

  return (uint16_t) (((double) opacity_window->opacity / OPACITY_OPAQUE) * 0xffff);
}

/** Handler for  MapNotify event. Get the opacity  property because we
 *  don't receive PropertyNotify events while the window is not mapped
 *
 * \param event The MapNotify event
 * \param window The window object
 */
static void
opacity_event_handle_map_notify(xcb_map_notify_event_t *event,
				window_t *window)
{
  debug("MapNotify: event=%jx, window=%jx",
	(uintmax_t) event->event, (uintmax_t) event->window);

  /* There may be no windows mapped yet */
  if(!_opacity_windows)
    _opacity_windows = _opacity_window_new(window);
  else
    {
      for(opacity_window_t *old__opacity_windows_tail = _opacity_windows;;
	  old__opacity_windows_tail = old__opacity_windows_tail->next)
	if(!old__opacity_windows_tail->next)
	  {
	    old__opacity_windows_tail->next = _opacity_window_new(window);
	    break;
	  }
    }

  window_register_notify(window);
}

/** Handler  for PropertyNotify  event which  only send  a GetProperty
 *  request on the given window
 *
 * \param event The PropertyNotify event
 * \param window The window object
 */
static void
opacity_event_handle_property_notify(xcb_property_notify_event_t *event,
				     window_t *window)
{
  /* Update the opacity atom if any */
  if(event->atom != _NET_WM_WINDOW_OPACITY)
    return;

  debug("PropertyNotify: window=%jx, atom=%ju",
	(uintmax_t) event->window, (uintmax_t) event->atom);

  /* Get the corresponding opacity window */
  opacity_window_t *opacity_window;
  for(opacity_window = _opacity_windows;
      opacity_window && opacity_window->window != window;
      opacity_window = opacity_window->next)
    ;

  /* A PropertyNotify may be  received before the MapNotify, therefore
     the  window  may  not  be  in '_opacity_windows'  yet.  This  bug
     happened  on  Awesome   restart  which  sends  UnmapWindow,  then
     ChangeProperty and finally a MapWindow request (Bug #13) */
  if(opacity_window == NULL)
    return;

  /* Send  a  GetProperty  request  if  the property  value  has  been
     updated, but free existing one if any */
  _opacity_free_property_reply(opacity_window);

  switch(event->state)
    {
    case XCB_PROPERTY_NEW_VALUE:
      opacity_window->cookie = _opacity_get_property(window->id);
      break;

    case XCB_PROPERTY_DELETE:
      opacity_window->opacity = OPACITY_OPAQUE;
      break;
    }
      
  /* Force redraw of the window as the opacity has changed */
  display_add_damaged_region(&window->region, false);
}

/** Handle  for  UnmapNotify,  only  responsible to  free  the  memory
 *  allocated on MapNotify because  opacity is only relevant to mapped
 *  windows and moreover PropertyNotify  events are not sent while the
 *  window is unmapped
 *
 * \param event The UnmapNotify event
 * \param window The window object
 */
static void
opacity_event_handle_unmap_notify(xcb_unmap_notify_event_t *event __attribute__((unused)),
				  window_t *window)
{
  if(!_opacity_windows)
    return;

  opacity_window_t *old_opacity_window = NULL;

  if(_opacity_windows->window == window)
    {
      old_opacity_window = _opacity_windows;
      _opacity_windows = _opacity_windows->next;
    }
  else
    {
      opacity_window_t *opacity_window;
      for(opacity_window = _opacity_windows;
	  opacity_window->next && opacity_window->next->window != window;
	  opacity_window = opacity_window->next)
	;

      if(!opacity_window->next)
	return;

      old_opacity_window = opacity_window->next;
      opacity_window->next = old_opacity_window->next;
    }

  _opacity_free_window(old_opacity_window);
}

/** Called on dlclose() and free the memory allocated by this plugin */
static void __attribute__((destructor))
opacity_destructor(void)
{
  opacity_window_t *opacity_window = _opacity_windows;
  opacity_window_t *opacity_window_next;

  while(opacity_window != NULL)
    {
      opacity_window_next = opacity_window->next;
      _opacity_free_window(opacity_window);
      opacity_window = opacity_window_next;
    }
}

/** Structure holding all the functions addresses */
plugin_vtable_t plugin_vtable = {
  .name = "opacity",
  .events = {
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    opacity_event_handle_map_notify,
    NULL,
    opacity_event_handle_unmap_notify,
    opacity_event_handle_property_notify
  },
  .check_requirements = NULL,
  .window_manage_existing = opacity_window_manage_existing,
  .window_get_opacity = opacity_get_window_opacity,
  .render_windows = NULL
};