File: aosd-internal.c

package info (click to toggle)
libaosd 0.2.7-1.1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 512 kB
  • sloc: sh: 2,859; ansic: 1,731; makefile: 82
file content (219 lines) | stat: -rw-r--r-- 5,761 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
/* aosd -- OSD with transparency, cairo, and pango.
 *
 * Copyright (C) 2006 Evan Martin <martine@danga.com>
 *
 * With further development by Giacomo Lozito <james@develia.org>
 * - added real transparency with X Composite Extension
 * - added mouse event handling on OSD window
 * - added/changed some other stuff
 */

#include "config.h"

#include <stdio.h>
#include <stdlib.h>

#include <X11/Xlib.h>
#include <X11/Xatom.h>

#ifdef HAVE_XCOMPOSITE
#include <X11/extensions/Xrender.h>
#include <X11/extensions/Xcomposite.h>
#endif

#include "aosd-internal.h"

void
make_window(Aosd* aosd)
{
  Display* dsp = aosd->display;
  Window root_win = aosd->root_win;

  if (aosd->win != None)
  {
    if (aosd->background.set)
    {
      XFreePixmap(dsp, aosd->background.pixmap);
      aosd->background.set = False;
    }

    if (aosd->colormap != None)
    {
      XFreeColormap(dsp, aosd->colormap);
      aosd->colormap = None;
    }

    XDestroyWindow(dsp, aosd->win);
    aosd->win = None;
  }

  if (root_win == None)
    return;

  XSetWindowAttributes att;

  att.backing_store = WhenMapped;
  att.background_pixel = 0x0;
  att.border_pixel = 0;
  att.background_pixmap = None;
  att.colormap = None;
  att.save_under = True;
  att.event_mask = ExposureMask | StructureNotifyMask | ButtonPressMask;
  att.override_redirect = True;

  if (aosd->mode == TRANSPARENCY_COMPOSITE)
  {
#ifdef HAVE_XCOMPOSITE
    Visual* visual = NULL;
    if (composite_check_ext_and_mgr(dsp, aosd->screen_num) &&
        (visual = composite_find_argb_visual(dsp, aosd->screen_num)) != NULL)
    {
      aosd->visual = visual;
      aosd->colormap = att.colormap =
        XCreateColormap(dsp, root_win, visual, AllocNone);
      aosd->win = XCreateWindow(dsp, root_win,
          -1, -1, 1, 1, 0, 32, InputOutput, visual,
          CWBackingStore | CWBackPixel | CWBackPixmap | CWBorderPixel |
          CWColormap | CWEventMask | CWSaveUnder | CWOverrideRedirect,
          &att);
    }
    else
#endif
    {
      aosd->mode = TRANSPARENCY_FAKE;
      make_window(aosd);
      return;
    }
  }
  else
  {
    aosd->win = XCreateWindow(dsp, root_win,
        -1, -1, 1, 1, 0, CopyFromParent, InputOutput, CopyFromParent,
        CWBackingStore | CWBackPixel | CWBackPixmap | CWBorderPixel |
        CWEventMask | CWSaveUnder | CWOverrideRedirect, &att);
  }

  set_window_properties(dsp, aosd->win);
  if (aosd->width && aosd->height)
    aosd_set_geometry(aosd, aosd->x, aosd->y, aosd->width, aosd->height);
  if (aosd->shown)
    aosd_show(aosd);
}

Pixmap
take_snapshot(Aosd* aosd)
{
  Display* dsp = aosd->display;
  Window root_win = aosd->root_win, win = aosd->win;
  int width = aosd->width, height = aosd->height;
  int scr = aosd->screen_num;
  Pixmap pixmap;
  GC gc;

  /* create a pixmap to hold the screenshot */
  pixmap = XCreatePixmap(dsp, win, width, height, DefaultDepth(dsp, scr));

  /* then copy the screen into the pixmap */
  gc = XCreateGC(dsp, pixmap, 0, NULL);
  XSetSubwindowMode(dsp, gc, IncludeInferiors);
  XCopyArea(dsp, root_win, pixmap, gc, aosd->x, aosd->y, width, height, 0, 0);
  XSetSubwindowMode(dsp, gc, ClipByChildren);
  XFreeGC(dsp, gc);

  return pixmap;
}

void
set_window_properties(Display* dsp, Window win)
{
  /* we're almost a _NET_WM_WINDOW_TYPE_SPLASH, but we don't want
   * to be centered on the screen.  instead, manually request the
   * behavior we want. */

  /* turn off window decorations.
   * we could pull this in from a motif header, but it's easier to
   * use this snippet i found on a mailing list. */
  Atom mwm_hints = XInternAtom(dsp, "_MOTIF_WM_HINTS", False);
  struct
  {
    long flags, functions, decorations, input_mode;
  }
  mwm_hints_setting = { (1L<<1), 0L, 0L, 0L };

  XChangeProperty(dsp, win, mwm_hints, mwm_hints, 32,
      PropModeReplace, (unsigned char *)&mwm_hints_setting, 4);

  Atom win_type = XInternAtom(dsp, "_NET_WM_WINDOW_TYPE", False);
  Atom win_type_notif[] = {
    XInternAtom(dsp, "_NET_WM_WINDOW_TYPE_NOTIFICATION", False),
  };
  XChangeProperty(dsp, win, win_type, XA_ATOM, 32,
      PropModeReplace, (unsigned char *)&win_type_notif, 1);

  /* always on top, not in taskbar or pager. */
  Atom win_state = XInternAtom(dsp, "_NET_WM_STATE", False);
  Atom win_state_setting[] =
  {
    XInternAtom(dsp, "_NET_WM_STATE_ABOVE", False),
    XInternAtom(dsp, "_NET_WM_STATE_STICKY", False),
    XInternAtom(dsp, "_NET_WM_STATE_SKIP_TASKBAR", False),
    XInternAtom(dsp, "_NET_WM_STATE_SKIP_PAGER", False)
  };
  XChangeProperty(dsp, win, win_state, XA_ATOM, 32,
      PropModeReplace, (unsigned char*)&win_state_setting, 3);
}

#ifdef HAVE_XCOMPOSITE
Bool
composite_check_ext_and_mgr(Display* dsp, int scr)
{
  int event_base, error_base;
  Atom comp_manager_atom;
  char comp_manager_hint[32];
  
  if (!XCompositeQueryExtension(dsp, &event_base, &error_base))
    return False;

  snprintf(comp_manager_hint, 32, "_NET_WM_CM_S%d", scr);
  comp_manager_atom = XInternAtom(dsp, comp_manager_hint, False);

  return (XGetSelectionOwner(dsp, comp_manager_atom) != None);
}

Visual*
composite_find_argb_visual(Display* dsp, int scr)
{
  XVisualInfo* xvi;
  XVisualInfo	template;
  int nvi, i;
  XRenderPictFormat* format;
  Visual* visual = NULL;

  template.screen = scr;
  template.depth = 32;
  template.class = TrueColor;

  xvi = XGetVisualInfo(dsp,
      VisualScreenMask | VisualDepthMask | VisualClassMask,
      &template, &nvi);

  if (xvi == NULL)
    return NULL;

  for (i = 0; i < nvi; i++)
  {
    format = XRenderFindVisualFormat(dsp, xvi[i].visual);
    if (format->type == PictTypeDirect &&
        format->direct.alphaMask)
    {
      visual = xvi[i].visual;
      break;
    }
  }

  XFree (xvi); 
  return visual;
}
#endif

/* vim: set ts=2 sw=2 et : */