|
| 1 | +/* |
| 2 | +Copyright 2020-2021 Daniel T. Borelli <danieltborelli@gmail.com> |
| 3 | +Copyright 2021-2023 Guilherme Janczak <guilherme.janczak@yandex.com> |
| 4 | +Copyright 2021 Peter Wu <peterwu@hotmail.com> |
| 5 | +Copyright 2023-2026 NRK <nrk@disroot.org> |
| 6 | +
|
| 7 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +of this software and associated documentation files (the "Software"), to |
| 9 | +deal in the Software without restriction, including without limitation the |
| 10 | +rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 11 | +sell copies of the Software, and to permit persons to whom the Software is |
| 12 | +furnished to do so, subject to the following conditions: |
| 13 | +
|
| 14 | +The above copyright notice and this permission notice shall be included in |
| 15 | +all copies of the Software and its documentation and acknowledgment shall be |
| 16 | +given in the documentation and software packages that this Software was |
| 17 | +used. |
| 18 | +
|
| 19 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 22 | +THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 23 | +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 24 | +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 25 | +
|
| 26 | +*/ |
| 27 | + |
| 28 | +/* |
| 29 | + This file is part of the scrot project. |
| 30 | + Part of the code comes from the scrot.c file and maintains its authorship. |
| 31 | +*/ |
| 32 | + |
| 33 | +#include <errno.h> |
| 34 | +#include <stdlib.h> |
| 35 | +#include <time.h> |
| 36 | + |
| 37 | +#include <X11/Xatom.h> |
| 38 | +#include <X11/Xlib.h> |
| 39 | +#include <X11/Xutil.h> |
| 40 | +#include <X11/extensions/shape.h> |
| 41 | + |
| 42 | +#include "options.h" |
| 43 | +#include "scrot.h" |
| 44 | +#include "scrot_selection.h" |
| 45 | +#include "selection_wind.h" |
| 46 | +#include "util.h" |
| 47 | + |
| 48 | +void selectionWindCreate(void) |
| 49 | +{ |
| 50 | + struct SelectionWind *const pw = &selection.wind; |
| 51 | + |
| 52 | + XColor color = scrotSelectionGetLineColor(); |
| 53 | + |
| 54 | + XSetWindowAttributes attr; |
| 55 | + attr.background_pixel = color.pixel; |
| 56 | + attr.override_redirect = True; |
| 57 | + Atom winType = XInternAtom(disp, "_NET_WM_WINDOW_TYPE", False); |
| 58 | + Atom winDock = XInternAtom(disp, "_NET_WM_WINDOW_TYPE_DOCK", False); |
| 59 | + Atom winOpacity = XInternAtom(disp, "_NET_WM_WINDOW_OPACITY", False); |
| 60 | + |
| 61 | + for (size_t i = 0; i < ARRAY_COUNT(pw->windows); ++i) { |
| 62 | + pw->windows[i] = XCreateWindow(disp, root, |
| 63 | + 0, 0, WidthOfScreen(scr), HeightOfScreen(scr), 0, |
| 64 | + CopyFromParent, InputOutput, CopyFromParent, |
| 65 | + CWOverrideRedirect | CWBackPixel, &attr); |
| 66 | + |
| 67 | + XChangeProperty(disp, pw->windows[i], winType, XA_ATOM, 32, |
| 68 | + PropModeReplace, (unsigned char *)&winDock, 1L); |
| 69 | + |
| 70 | + // NOTE: opacity has not been tested... |
| 71 | + unsigned long opacity = opt.lineOpacity * (0xFFFFFFFFu / 255); |
| 72 | + XChangeProperty(disp, pw->windows[i], winOpacity, XA_CARDINAL, 32, |
| 73 | + PropModeReplace, (unsigned char *)&opacity, 1L); |
| 74 | + |
| 75 | + XClassHint hint = { .res_name = "scrot", .res_class = "scrot" }; |
| 76 | + XSetClassHint(disp, pw->windows[i], &hint); |
| 77 | + } |
| 78 | + pw->isMapped = false; |
| 79 | + |
| 80 | +} |
| 81 | + |
| 82 | +void selectionWindDraw(void) |
| 83 | +{ |
| 84 | + struct Selection *const sel = &selection; |
| 85 | + struct SelectionWind *const pw = &sel->wind; |
| 86 | + |
| 87 | + XRectangle rects[4] = { |
| 88 | + { sel->rect.x, sel->rect.y, opt.lineWidth, sel->rect.h }, // left |
| 89 | + { sel->rect.x, sel->rect.y, sel->rect.w, opt.lineWidth }, // top |
| 90 | + // right |
| 91 | + { sel->rect.x + sel->rect.w, sel->rect.y, opt.lineWidth, sel->rect.h }, |
| 92 | + // bottom |
| 93 | + { sel->rect.x, sel->rect.y + sel->rect.h, sel->rect.w + opt.lineWidth, |
| 94 | + opt.lineWidth } |
| 95 | + }; |
| 96 | + |
| 97 | + if (sel->rect.w == 0 || sel->rect.h == 0) |
| 98 | + return; |
| 99 | + |
| 100 | + for (size_t i = 0; i < ARRAY_COUNT(pw->windows); ++i) { |
| 101 | + XRectangle *rp = rects + i; |
| 102 | + XMoveResizeWindow(disp, pw->windows[i], rp->x, rp->y, rp->width, rp->height); |
| 103 | + XMapWindow(disp, pw->windows[i]); |
| 104 | + } |
| 105 | + pw->isMapped = true; |
| 106 | +} |
| 107 | + |
| 108 | +void selectionWindMotionDraw(int x0, int y0, int x1, int y1) |
| 109 | +{ |
| 110 | + struct Selection *const sel = &selection; |
| 111 | + |
| 112 | + selectionCalculateRect(x0, y0, x1, y1); |
| 113 | + |
| 114 | + sel->rect.x -= opt.lineWidth; |
| 115 | + sel->rect.y -= opt.lineWidth; |
| 116 | + sel->rect.w += opt.lineWidth; |
| 117 | + sel->rect.h += opt.lineWidth; |
| 118 | + |
| 119 | + selectionWindDraw(); |
| 120 | +} |
| 121 | + |
| 122 | +void selectionWindDestroy(void) |
| 123 | +{ |
| 124 | + struct SelectionWind *const pw = &selection.wind; |
| 125 | + |
| 126 | + for (size_t i = 0; i < ARRAY_COUNT(pw->windows); ++i) { |
| 127 | + if (pw->windows[i] == None) |
| 128 | + continue; |
| 129 | + XSelectInput(disp, pw->windows[i], StructureNotifyMask); |
| 130 | + XDestroyWindow(disp, pw->windows[i]); |
| 131 | + bool isUnmapped = !pw->isMapped, isDestroyed = false; |
| 132 | + for (XEvent ev; !(isUnmapped && isDestroyed);) { |
| 133 | + XNextEvent(disp, &ev); |
| 134 | + if (ev.type == DestroyNotify && ev.xdestroywindow.window == pw->windows[i]) |
| 135 | + isDestroyed = true; |
| 136 | + if (ev.type == UnmapNotify && ev.xunmap.window == pw->windows[i]) |
| 137 | + isUnmapped = true; |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments