| 1 |
#include "internal.h" |
| 2 |
#include "x11.h" |
| 3 |
|
| 4 |
#include <stdlib.h> |
| 5 |
#include <cairo-xlib.h> |
| 6 |
#include <X11/extensions/Xinerama.h> |
| 7 |
#include <X11/Xutil.h> |
| 8 |
|
| 9 |
static void |
| 10 |
destroy_buffer(struct buffer *buffer) |
| 11 |
{ |
| 12 |
bm_cairo_destroy(&buffer->cairo); |
| 13 |
memset(buffer, 0, sizeof(struct buffer)); |
| 14 |
} |
| 15 |
|
| 16 |
static bool |
| 17 |
create_buffer(struct window *window, struct buffer *buffer, int32_t width, int32_t height) |
| 18 |
{ |
| 19 |
cairo_surface_t *surf; |
| 20 |
if (!(surf = cairo_xlib_surface_create(window->display, window->drawable, window->visual, width, height))) |
| 21 |
goto fail; |
| 22 |
|
| 23 |
cairo_xlib_surface_set_size(surf, width, height); |
| 24 |
|
| 25 |
const char *scale = getenv("BEMENU_SCALE"); |
| 26 |
if (scale) { |
| 27 |
buffer->cairo.scale = fmax(strtof(scale, NULL), 1.0f); |
| 28 |
} else { |
| 29 |
buffer->cairo.scale = 1; |
| 30 |
} |
| 31 |
|
| 32 |
buffer->cairo.antialiasing = true; |
| 33 |
|
| 34 |
if (!bm_cairo_create_for_surface(&buffer->cairo, surf)) { |
| 35 |
cairo_surface_destroy(surf); |
| 36 |
goto fail; |
| 37 |
} |
| 38 |
|
| 39 |
buffer->width = width; |
| 40 |
buffer->height = height; |
| 41 |
buffer->created = true; |
| 42 |
return true; |
| 43 |
|
| 44 |
fail: |
| 45 |
destroy_buffer(buffer); |
| 46 |
return false; |
| 47 |
} |
| 48 |
|
| 49 |
static struct buffer* |
| 50 |
next_buffer(struct window *window) |
| 51 |
{ |
| 52 |
assert(window); |
| 53 |
|
| 54 |
struct buffer *buffer = &window->buffer; |
| 55 |
|
| 56 |
if (!buffer) |
| 57 |
return NULL; |
| 58 |
|
| 59 |
if (window->width != buffer->width || window->height != buffer->height) |
| 60 |
destroy_buffer(buffer); |
| 61 |
|
| 62 |
if (!buffer->created && !create_buffer(window, buffer, window->width, window->height)) |
| 63 |
return NULL; |
| 64 |
|
| 65 |
return buffer; |
| 66 |
} |
| 67 |
|
| 68 |
static uint32_t |
| 69 |
get_window_width(struct window *window) |
| 70 |
{ |
| 71 |
uint32_t width = window->width * ((window->width_factor != 0) ? window->width_factor : 1); |
| 72 |
|
| 73 |
if(width > window->width - 2 * window->hmargin_size) |
| 74 |
width = window->width - 2 * window->hmargin_size; |
| 75 |
|
| 76 |
if(width < WINDOW_MIN_WIDTH || 2 * window->hmargin_size > window->width) |
| 77 |
width = WINDOW_MIN_WIDTH; |
| 78 |
|
| 79 |
return width; |
| 80 |
} |
| 81 |
|
| 82 |
void |
| 83 |
bm_x11_window_render(struct window *window, struct bm_menu *menu) |
| 84 |
{ |
| 85 |
assert(window && menu); |
| 86 |
uint32_t oldw = window->width, oldh = window->height; |
| 87 |
|
| 88 |
struct buffer *buffer; |
| 89 |
for (int32_t tries = 0; tries < 2; ++tries) { |
| 90 |
if (!(buffer = next_buffer(window))) { |
| 91 |
fprintf(stderr, "could not get next buffer"); |
| 92 |
exit(EXIT_FAILURE); |
| 93 |
} |
| 94 |
|
| 95 |
if (!window->notify.render) |
| 96 |
break; |
| 97 |
|
| 98 |
cairo_push_group(buffer->cairo.cr); |
| 99 |
struct cairo_paint_result result; |
| 100 |
window->notify.render(&buffer->cairo, buffer->width, window->max_height, menu, &result); |
| 101 |
window->displayed = result.displayed; |
| 102 |
cairo_pop_group_to_source(buffer->cairo.cr); |
| 103 |
|
| 104 |
if (window->height == result.height) |
| 105 |
break; |
| 106 |
|
| 107 |
window->height = result.height; |
| 108 |
destroy_buffer(buffer); |
| 109 |
} |
| 110 |
|
| 111 |
if (oldw != window->width || oldh != window->height) { |
| 112 |
uint32_t win_y = 0; |
| 113 |
|
| 114 |
if(window->align == BM_ALIGN_CENTER) { |
| 115 |
win_y = (window->max_height - window->height) / 2; |
| 116 |
} else if(window->align == BM_ALIGN_BOTTOM) { |
| 117 |
win_y = window->max_height - window->height; |
| 118 |
} |
| 119 |
|
| 120 |
XMoveResizeWindow(window->display, window->drawable, window->x, win_y + window->y_offset, window->width, window->height); |
| 121 |
} |
| 122 |
|
| 123 |
if (buffer->created) { |
| 124 |
cairo_save(buffer->cairo.cr); |
| 125 |
cairo_set_operator(buffer->cairo.cr, CAIRO_OPERATOR_SOURCE); |
| 126 |
cairo_paint(buffer->cairo.cr); |
| 127 |
cairo_surface_flush(buffer->cairo.surface); |
| 128 |
cairo_restore(buffer->cairo.cr); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
void |
| 133 |
bm_x11_window_key_press(struct window *window, XKeyEvent *ev) |
| 134 |
{ |
| 135 |
KeySym keysym = NoSymbol; |
| 136 |
XmbLookupString(window->xic, ev, NULL, 0, &keysym, NULL); |
| 137 |
window->mods = 0; |
| 138 |
if (ev->state & ControlMask) window->mods |= MOD_CTRL; |
| 139 |
if (ev->state & ShiftMask) window->mods |= MOD_SHIFT; |
| 140 |
if (ev->state & Mod1Mask) window->mods |= MOD_ALT; |
| 141 |
window->keysym = keysym; |
| 142 |
} |
| 143 |
|
| 144 |
void |
| 145 |
bm_x11_window_destroy(struct window *window) |
| 146 |
{ |
| 147 |
assert(window); |
| 148 |
destroy_buffer(&window->buffer); |
| 149 |
|
| 150 |
if (window->display && window->drawable) |
| 151 |
XDestroyWindow(window->display, window->drawable); |
| 152 |
} |
| 153 |
|
| 154 |
void |
| 155 |
bm_x11_window_set_monitor(struct window *window, int32_t monitor) |
| 156 |
{ |
| 157 |
if (window->monitor == monitor) |
| 158 |
return; |
| 159 |
|
| 160 |
Window root = DefaultRootWindow(window->display); |
| 161 |
|
| 162 |
{ |
| 163 |
/* xinerama logic straight from dmenu */ |
| 164 |
#define INTERSECT(x,y,w,h,r) (fmax(0, fmin((x)+(w),(r).x_org+(r).width) - fmax((x),(r).x_org)) * fmax(0, fmin((y)+(h),(r).y_org+(r).height) - fmax((y),(r).y_org))) |
| 165 |
|
| 166 |
int32_t n; |
| 167 |
XineramaScreenInfo *info; |
| 168 |
if ((info = XineramaQueryScreens(window->display, &n))) { |
| 169 |
int32_t x, y, a, j, di, i = 0, area = 0; |
| 170 |
uint32_t du; |
| 171 |
Window w, pw, dw, *dws; |
| 172 |
XWindowAttributes wa; |
| 173 |
|
| 174 |
XGetInputFocus(window->display, &w, &di); |
| 175 |
if (monitor >= 0 && monitor < n) |
| 176 |
i = monitor; |
| 177 |
else if (w != root && w != PointerRoot && w != None) { |
| 178 |
/* find top-level window containing current input focus */ |
| 179 |
do { |
| 180 |
if (XQueryTree(window->display, (pw = w), &dw, &w, &dws, &du) && dws) |
| 181 |
XFree(dws); |
| 182 |
} while(w != root && w != pw); |
| 183 |
|
| 184 |
/* find xinerama screen with which the window intersects most */ |
| 185 |
if (XGetWindowAttributes(window->display, pw, &wa)) { |
| 186 |
for (j = 0; j < n; j++) |
| 187 |
if ((a = INTERSECT(wa.x, wa.y, wa.width, wa.height, info[j])) > area) { |
| 188 |
area = a; |
| 189 |
i = j; |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
/* no focused window is on screen, so use pointer location instead */ |
| 195 |
if (monitor < 0 && !area && XQueryPointer(window->display, root, &dw, &dw, &x, &y, &di, &di, &du)) { |
| 196 |
for (i = 0; i < n; i++) { |
| 197 |
if (INTERSECT(x, y, 1, 1, info[i]) > 0) |
| 198 |
break; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
window->x = info[i].x_org; |
| 203 |
window->y = info[i].y_org; |
| 204 |
if(window->align == BM_ALIGN_CENTER) { |
| 205 |
window->y += (info[i].height - window->height) / 2; |
| 206 |
} else if(window->align == BM_ALIGN_BOTTOM) { |
| 207 |
window->y += info[i].height - window->height; |
| 208 |
} |
| 209 |
|
| 210 |
window->width = info[i].width; |
| 211 |
window->max_height = info[i].height; |
| 212 |
XFree(info); |
| 213 |
} else { |
| 214 |
window->max_height = DisplayHeight(window->display, window->screen); |
| 215 |
window->x = 0; |
| 216 |
if(window->align == BM_ALIGN_CENTER) { |
| 217 |
window->y = (window->max_height - window->height) / 2; |
| 218 |
} else if(window->align == BM_ALIGN_BOTTOM) { |
| 219 |
window->y = window->max_height - window->height; |
| 220 |
} else { |
| 221 |
window->y = 0; |
| 222 |
} |
| 223 |
window->width = DisplayWidth(window->display, window->screen); |
| 224 |
} |
| 225 |
|
| 226 |
window->orig_width = window->width; |
| 227 |
window->orig_x = window->x; |
| 228 |
window->width = get_window_width(window); |
| 229 |
window->x += (window->orig_width - window->width) / 2; |
| 230 |
|
| 231 |
#undef INTERSECT |
| 232 |
} |
| 233 |
|
| 234 |
window->monitor = monitor; |
| 235 |
XMoveResizeWindow(window->display, window->drawable, window->x, window->y + window->y_offset, window->width, window->height); |
| 236 |
XFlush(window->display); |
| 237 |
} |
| 238 |
|
| 239 |
void |
| 240 |
bm_x11_window_set_align(struct window *window, enum bm_align align) |
| 241 |
{ |
| 242 |
if(window->align == align) |
| 243 |
return; |
| 244 |
|
| 245 |
window->align = align; |
| 246 |
bm_x11_window_set_monitor(window, window->monitor); |
| 247 |
} |
| 248 |
|
| 249 |
void |
| 250 |
bm_x11_window_set_y_offset(struct window *window, int32_t y_offset) |
| 251 |
{ |
| 252 |
if(window->y_offset == y_offset) |
| 253 |
return; |
| 254 |
|
| 255 |
window->y_offset = y_offset; |
| 256 |
} |
| 257 |
|
| 258 |
void |
| 259 |
bm_x11_window_set_width(struct window *window, uint32_t margin, float factor) |
| 260 |
{ |
| 261 |
if(window->hmargin_size == margin && window->width_factor == factor) |
| 262 |
return; |
| 263 |
|
| 264 |
window->hmargin_size = margin; |
| 265 |
window->width_factor = factor; |
| 266 |
window->width = window->orig_width; |
| 267 |
window->x = window->orig_x; |
| 268 |
window->width = get_window_width(window); |
| 269 |
window->x += (window->orig_width - window->width) / 2; |
| 270 |
bm_x11_window_set_monitor(window, window->monitor); |
| 271 |
} |
| 272 |
|
| 273 |
bool |
| 274 |
bm_x11_window_create(struct window *window, Display *display) |
| 275 |
{ |
| 276 |
assert(window); |
| 277 |
|
| 278 |
window->display = display; |
| 279 |
window->screen = DefaultScreen(display); |
| 280 |
window->width = window->height = 1; |
| 281 |
window->monitor = -1; |
| 282 |
window->visual = DefaultVisual(display, window->screen); |
| 283 |
|
| 284 |
XSetWindowAttributes wa = { |
| 285 |
.override_redirect = True, |
| 286 |
.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask | ButtonPressMask |
| 287 |
}; |
| 288 |
|
| 289 |
XVisualInfo vinfo; |
| 290 |
int depth = DefaultDepth(display, window->screen); |
| 291 |
unsigned long valuemask = CWOverrideRedirect | CWEventMask | CWBackPixel; |
| 292 |
|
| 293 |
if (XMatchVisualInfo(display, DefaultScreen(display), 32, TrueColor, &vinfo)) { |
| 294 |
depth = vinfo.depth; |
| 295 |
window->visual = vinfo.visual; |
| 296 |
wa.background_pixmap = None; |
| 297 |
wa.border_pixel = 0; |
| 298 |
wa.colormap = XCreateColormap(display, DefaultRootWindow(display), window->visual, AllocNone); |
| 299 |
valuemask = CWOverrideRedirect | CWEventMask | CWBackPixmap | CWColormap | CWBorderPixel; |
| 300 |
} |
| 301 |
|
| 302 |
window->drawable = XCreateWindow(display, DefaultRootWindow(display), 0, 0, window->width, window->height, 0, depth, CopyFromParent, window->visual, valuemask, &wa); |
| 303 |
XSelectInput(display, window->drawable, ButtonPressMask | KeyPressMask); |
| 304 |
XMapRaised(display, window->drawable); |
| 305 |
window->xim = XOpenIM(display, NULL, NULL, NULL); |
| 306 |
window->xic = XCreateIC(window->xim, XNInputStyle, XIMPreeditNothing | XIMStatusNothing, XNClientWindow, window->drawable, XNFocusWindow, window->drawable, NULL); |
| 307 |
return true; |
| 308 |
} |
| 309 |
|
| 310 |
/* vim: set ts=8 sw=4 tw=0 :*/ |