Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix racy --freeze behavior #382

Merged
merged 1 commit into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/scrot.c
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,9 @@ size_t scrotHaveFileExtension(const char *filename, char **ext)
return strlen(*ext);
}

Imlib_Image scrotGrabRectAndPointer(int x, int y, int w, int h)
Imlib_Image scrotGrabRectAndPointer(int x, int y, int w, int h, bool isGrabbed)
{
Imlib_Image im = imlib_create_image_from_drawable(0, x, y, w, h, 1);
Imlib_Image im = imlib_create_image_from_drawable(0, x, y, w, h, !isGrabbed);
if (!im)
errx(EXIT_FAILURE, "failed to grab image");
if (opt.pointer)
Expand All @@ -315,7 +315,7 @@ static Imlib_Image scrotGrabWindowById(Window const window)
if (!scrotGetGeometry(window, &rx, &ry, &rw, &rh))
return NULL;
scrotNiceClip(&rx, &ry, &rw, &rh);
im = scrotGrabRectAndPointer(rx, ry, rw, rh);
im = scrotGrabRectAndPointer(rx, ry, rw, rh, false);
clientWindow = window;
return im;
}
Expand All @@ -333,7 +333,7 @@ static Imlib_Image scrotGrabAutoselect(void)
int rx = opt.autoselectX, ry = opt.autoselectY, rw = opt.autoselectW,
rh = opt.autoselectH;
scrotNiceClip(&rx, &ry, &rw, &rh);
return scrotGrabRectAndPointer(rx, ry, rw, rh);
return scrotGrabRectAndPointer(rx, ry, rw, rh, false);
}

void scrotDoDelay(void)
Expand Down Expand Up @@ -625,7 +625,7 @@ static Imlib_Image scrotGrabShot(void)
{
if (!opt.silent)
XBell(disp, 0);
return scrotGrabRectAndPointer(0, 0, scr->width, scr->height);
return scrotGrabRectAndPointer(0, 0, scr->width, scr->height, false);
}

static void scrotExecApp(Imlib_Image image, struct tm *tm, char *filenameIM,
Expand Down Expand Up @@ -984,7 +984,7 @@ static Imlib_Image scrotGrabShotMonitor(void)
XFree(screens);

scrotNiceClip(&x, &y, &w, &h);
return scrotGrabRectAndPointer(x, y, w, h);
return scrotGrabRectAndPointer(x, y, w, h, false);
}

static Imlib_Image stalkImageConcat(
Expand Down
2 changes: 1 addition & 1 deletion src/scrot.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void scrotNiceClip(int *, int *, int *, int *);
struct timespec clockNow(void);
struct timespec scrotSleepFor(struct timespec, int);
void scrotDoDelay(void);
Imlib_Image scrotGrabRectAndPointer(int x, int y, int w, int h);
Imlib_Image scrotGrabRectAndPointer(int, int, int, int, bool);
size_t scrotHaveFileExtension(const char *, char **);

#endif /* !defined(H_SCROT) */
27 changes: 19 additions & 8 deletions src/scrot_selection.c
Original file line number Diff line number Diff line change
Expand Up @@ -413,22 +413,33 @@ Imlib_Image scrotSelectionSelectMode(void)
if (opt.delaySelection)
scrotDoDelay();

if (!scrotSelectionGetUserSel(&rect0))
return NULL;

opt.selection.mode = oldMode;
if (opt.freeze)
XGrabServer(disp);

bool success = scrotSelectionGetUserSel(&rect0);
if (success) {
opt.selection.mode = oldMode;
if (opt.selection.mode & SELECTION_MODE_NOT_CAPTURE)
success = scrotSelectionGetUserSel(&rect1);
}

if (opt.selection.mode & SELECTION_MODE_NOT_CAPTURE)
if (!scrotSelectionGetUserSel(&rect1))
return NULL;
if (!success) {
if (opt.freeze)
XUngrabServer(disp);
return NULL;
}

// this doesn't seem to make much sense if `--freeze` is enabled...
if (!opt.delaySelection) {
opt.delayStart = clockNow();
scrotDoDelay();
}

Imlib_Image capture = scrotGrabRectAndPointer(
rect0.x, rect0.y, rect0.w, rect0.h);
rect0.x, rect0.y, rect0.w, rect0.h, opt.freeze);

if (opt.freeze)
XUngrabServer(disp);

if (opt.selection.mode == SELECTION_MODE_CAPTURE)
return capture;
Expand Down
6 changes: 0 additions & 6 deletions src/selection_classic.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,6 @@ void selectionClassicCreate(void)

XSetLineAttributes(disp, pc->gc, opt.lineWidth, opt.lineStyle, CapRound,
JoinRound);

if (opt.freeze)
XGrabServer(disp);
}

void selectionClassicDraw(void)
Expand All @@ -100,9 +97,6 @@ void selectionClassicDestroy(void)
const struct Selection *const sel = &selection;
const struct SelectionClassic *pc = &sel->classic;

if (opt.freeze)
XUngrabServer(disp);

if (pc->gc)
XFreeGC(disp, pc->gc);

Expand Down
Loading