selection_edge.c (scrot-1.6.tar.bz2) | : | selection_edge.c (scrot-1.7.tar.bz2) | ||
---|---|---|---|---|
/* scrot_selection_edge.c | /* scrot_selection_edge.c | |||
Copyright 2020-2021 Daniel T. Borelli <daltomi@disroot.org> | Copyright 2020-2021 Daniel T. Borelli <danieltborelli@gmail.com> | |||
Copyright 2021 Peter Wu <peterwu@hotmail.com> | ||||
Permission is hereby granted, free of charge, to any person obtaining a copy | Permission is hereby granted, free of charge, to any person obtaining a copy | |||
of this software and associated documentation files (the "Software"), to | of this software and associated documentation files (the "Software"), to | |||
deal in the Software without restriction, including without limitation the | deal in the Software without restriction, including without limitation the | |||
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | |||
sell copies of the Software, and to permit persons to whom the Software is | sell copies of the Software, and to permit persons to whom the Software is | |||
furnished to do so, subject to the following conditions: | furnished to do so, subject to the following conditions: | |||
The above copyright notice and this permission notice shall be included in | The above copyright notice and this permission notice shall be included in | |||
all copies of the Software and its documentation and acknowledgment shall be | all copies of the Software and its documentation and acknowledgment shall be | |||
skipping to change at line 32 | skipping to change at line 33 | |||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |||
*/ | */ | |||
/* | /* | |||
This file is part of the scrot project. | This file is part of the scrot project. | |||
Part of the code comes from the main.c file and maintains its authorship. | Part of the code comes from the main.c file and maintains its authorship. | |||
*/ | */ | |||
#include "scrot.h" | #include "scrot.h" | |||
extern void selection_calculate_rect(int x0, int y0, int x1, int y1); | extern void selectionCalculateRect(int, int, int, int); | |||
extern struct selection_t** selection_get(void); | extern struct Selection** selectionGet(void); | |||
struct selection_edge_t { | struct SelectionEdge { | |||
Window wndDraw; | Window wndDraw; | |||
XClassHint* classHint; | XClassHint* classHint; | |||
}; | }; | |||
static Bool xevent_unmap(Display *dpy, XEvent *ev, XPointer arg) | static Bool xeventUnmap(Display* dpy, XEvent* ev, XPointer arg) | |||
{ | { | |||
(void) dpy; //unused | (void)dpy; // unused | |||
Window *win = (Window*)arg; | Window* win = (Window*)arg; | |||
return (ev->xunmap.window == *win); | return (ev->xunmap.window == *win); | |||
} | } | |||
static void wait_unmap_window_notify(void) | static void waitUnmapWindowNotify(void) | |||
{ | { | |||
struct selection_t const *const sel = *selection_get(); | struct Selection const* const sel = *selectionGet(); | |||
struct selection_edge_t const *const pe = sel->edge; | struct SelectionEdge const* const pe = sel->edge; | |||
XEvent ev; | XEvent ev; | |||
XSelectInput(disp, pe->wndDraw, StructureNotifyMask); | XSelectInput(disp, pe->wndDraw, StructureNotifyMask); | |||
XUnmapWindow(disp, pe->wndDraw); | XUnmapWindow(disp, pe->wndDraw); | |||
for(short i = 0; i < 30; ++i) { | struct timespec delay = {0, 80000000L}; // 80ms | |||
if (XCheckIfEvent(disp, &ev, &xevent_unmap, (XPointer)&(pe->wndDraw)) == | ||||
True) { | for (short i = 0; i < 30; ++i) { | |||
if (XCheckIfEvent(disp, &ev, &xeventUnmap, (XPointer) & (pe->wndDraw))) | ||||
break; | break; | |||
} | nanosleep(&delay, NULL); | |||
usleep(8000); | ||||
} | } | |||
} | } | |||
void selection_edge_create(void) | void selectionEdgeCreate(void) | |||
{ | { | |||
struct selection_t *const sel = *selection_get(); | struct Selection* const sel = *selectionGet(); | |||
sel->edge = calloc(1, sizeof(struct selection_edge_t)); | ||||
struct selection_edge_t *const pe = sel->edge; | sel->edge = calloc(1, sizeof(*sel->edge)); | |||
// It is ok that in the "classic" mode it is to NULL, but it is not "edge" m | struct SelectionEdge* const pe = sel->edge; | |||
ode | ||||
if (opt.line_color == NULL) { | ||||
opt.line_color = "gray"; | ||||
} | ||||
XColor color; | XColor color; | |||
scrotSelectionGetLineColor(&color); | ||||
if (0 == XAllocNamedColor(disp, XDefaultColormap(disp, DefaultScreen(disp)), | ||||
opt.line_color, &color, &(XColor){})) | ||||
{ | ||||
fprintf(stderr, "Error allocate color:%s\n", strerror(BadColor)); | ||||
scrot_selection_destroy(); | ||||
exit(EXIT_FAILURE); | ||||
} | ||||
XSetWindowAttributes attr; | XSetWindowAttributes attr; | |||
attr.background_pixel = color.pixel; | attr.background_pixel = color.pixel; | |||
attr.override_redirect = True; | attr.override_redirect = True; | |||
pe->classHint = XAllocClassHint(); | pe->classHint = XAllocClassHint(); | |||
pe->classHint->res_name = "scrot"; | pe->classHint->res_name = "scrot"; | |||
pe->classHint->res_class = "scrot"; | pe->classHint->res_class = "scrot"; | |||
pe->wndDraw = XCreateWindow(disp, root, 0, 0, WidthOfScreen(scr), HeightOfSc reen(scr), 0, | pe->wndDraw = XCreateWindow(disp, root, 0, 0, WidthOfScreen(scr), HeightOfSc reen(scr), 0, | |||
CopyFromParent, InputOutput, CopyFromParent, CWOverrideRedirect | CWBa | CopyFromParent, InputOutput, CopyFromParent, CWOverrideRedirect | CWBack | |||
ckPixel, &attr); | Pixel, &attr); | |||
int const lineOpacity = optionsParseRequireRange(opt.lineOpacity, | ||||
SELECTION_EDGE_OPACITY_MIN, SELECTION_OPACITY_MAX); | ||||
unsigned long opacity = opt.line_opacity * ((unsigned)-1 / 100); | unsigned long opacity = lineOpacity * ((unsigned)-1 / 100); | |||
XChangeProperty(disp, pe->wndDraw, XInternAtom(disp, "_NET_WM_WINDOW_OPACITY ", False), | XChangeProperty(disp, pe->wndDraw, XInternAtom(disp, "_NET_WM_WINDOW_OPACITY ", False), | |||
XA_CARDINAL, 32, PropModeReplace, | XA_CARDINAL, 32, PropModeReplace, | |||
(unsigned char*)&opacity, 1L); | (unsigned char*) &opacity, 1L); | |||
XChangeProperty(disp, pe->wndDraw, XInternAtom(disp, "_NET_WM_WINDOW_TYPE", False), | XChangeProperty(disp, pe->wndDraw, XInternAtom(disp, "_NET_WM_WINDOW_TYPE", False), | |||
XA_ATOM, 32, PropModeReplace, | XA_ATOM, 32, PropModeReplace, | |||
(unsigned char*)&(Atom){XInternAtom(disp, "_NET_WM_WINDOW_TYPE | (unsigned char*) &(Atom) { XInternAtom(disp, "_NET_WM_WINDOW_TYPE_DOCK", | |||
_DOCK", False)}, | False) }, | |||
1L); | 1L); | |||
XSetClassHint(disp, pe->wndDraw, pe->classHint); | XSetClassHint(disp, pe->wndDraw, pe->classHint); | |||
} | } | |||
void selection_edge_destroy(void) | void selectionEdgeDestroy(void) | |||
{ | { | |||
struct selection_t const *const sel = *selection_get(); | struct Selection const* const sel = *selectionGet(); | |||
struct selection_edge_t *pe = sel->edge; | struct SelectionEdge* pe = sel->edge; | |||
if (pe->wndDraw != 0) { | ||||
waitUnmapWindowNotify(); | ||||
XFree(pe->classHint); | ||||
XDestroyWindow(disp, pe->wndDraw); | ||||
} | ||||
wait_unmap_window_notify(); | ||||
XFree(pe->classHint); | ||||
XDestroyWindow(disp, pe->wndDraw); | ||||
free(pe); | free(pe); | |||
} | } | |||
void selection_edge_draw(void) | void selectionEdgeDraw(void) | |||
{ | { | |||
struct selection_t const *const sel = *selection_get(); | struct Selection const* const sel = *selectionGet(); | |||
struct selection_edge_t const *const pe = sel->edge; | struct SelectionEdge const* const pe = sel->edge; | |||
XRectangle rects[4] = { | XRectangle rects[4] = { | |||
{sel->rect.x, sel->rect.y, opt.line_width, sel->rect.h}, | { sel->rect.x, sel->rect.y, opt.lineWidth, sel->rect.h }, // left | |||
//left | { sel->rect.x, sel->rect.y, sel->rect.w, opt.lineWidth }, // top | |||
{sel->rect.x, sel->rect.y, sel->rect.w, opt.line_width}, | { sel->rect.x + sel->rect.w, sel->rect.y, opt.lineWidth, sel->rect.h }, | |||
//top | // right | |||
{sel->rect.x + sel->rect.w, sel->rect.y, opt.line_width, sel->rect.h | { sel->rect.x, sel->rect.y + sel->rect.h, sel->rect.w + opt.lineWidth, o | |||
}, //right | pt.lineWidth } // bottom | |||
{sel->rect.x, sel->rect.y + sel->rect.h, sel->rect.w + opt.line_widt | ||||
h, opt.line_width} //bottom | ||||
}; | }; | |||
XShapeCombineRectangles(disp, pe->wndDraw, ShapeBounding, 0, 0, rects, 4, Sh apeSet, 0); | XShapeCombineRectangles(disp, pe->wndDraw, ShapeBounding, 0, 0, rects, 4, Sh apeSet, 0); | |||
XMapWindow(disp, pe->wndDraw); | XMapWindow(disp, pe->wndDraw); | |||
} | } | |||
void selection_edge_motion_draw(int x0, int y0, int x1, int y1) | void selectionEdgeMotionDraw(int x0, int y0, int x1, int y1) | |||
{ | { | |||
struct selection_t *const sel = *selection_get(); | struct Selection* const sel = *selectionGet(); | |||
selection_calculate_rect(x0, y0, x1, y1); | selectionCalculateRect(x0, y0, x1, y1); | |||
sel->rect.x -= opt.line_width; | sel->rect.x -= opt.lineWidth; | |||
sel->rect.y -= opt.line_width; | sel->rect.y -= opt.lineWidth; | |||
sel->rect.w += opt.line_width; | sel->rect.w += opt.lineWidth; | |||
sel->rect.h += opt.line_width; | sel->rect.h += opt.lineWidth; | |||
selection_edge_draw(); | selectionEdgeDraw(); | |||
} | } | |||
End of changes. 30 change blocks. | ||||
68 lines changed or deleted | 60 lines changed or added |