Skip to content

with --freeze capture immidately and crop later #409

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
3 changes: 1 addition & 2 deletions src/scrot.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ static Window scrotFindWindowByProperty(Display *, const Window, const Atom);
static Imlib_Image stalkImageConcat(Imlib_Image *, size_t, const enum Direction);
static int findWindowManagerFrame(Window *const, int *const);
static Imlib_Image scrotGrabWindowById(Window const window);
static void scrotGrabMousePointer(Imlib_Image, const int, const int);

/* X11 stuff */
Display *disp;
Expand Down Expand Up @@ -520,7 +519,7 @@ Window scrotGetWindow(Display *display, Window window, int x, int y)
return target;
}

static void scrotGrabMousePointer(Imlib_Image image, const int xOffset,
void scrotGrabMousePointer(Imlib_Image image, const int xOffset,
const int yOffset)
{
XFixesCursorImage *xcim = XFixesGetCursorImage(disp);
Expand Down
1 change: 1 addition & 0 deletions src/scrot.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ struct timespec clockNow(void);
struct timespec scrotSleepFor(struct timespec, int);
void scrotDoDelay(void);
Imlib_Image scrotGrabRectAndPointer(int, int, int, int, bool);
void scrotGrabMousePointer(Imlib_Image, const int, const int);
size_t scrotHaveFileExtension(const char *, char **);

#endif /* !defined(H_SCROT) */
50 changes: 33 additions & 17 deletions src/scrot_selection.c
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ static Imlib_Image loadImage(const char *const fileName, const int opacity)
Imlib_Image scrotSelectionSelectMode(void)
{
struct SelectionRect rect0, rect1;
Imlib_Image capture = NULL;

const unsigned int oldMode = opt.selection.mode;
opt.selection.mode = SELECTION_MODE_CAPTURE;
Expand All @@ -420,36 +421,51 @@ Imlib_Image scrotSelectionSelectMode(void)
if (opt.delaySelection)
scrotDoDelay();

if (opt.freeze)
if (opt.freeze) {
XGrabServer(disp);
// capture immidately to avoid the selection making a mess later
capture = imlib_create_image_from_drawable(0, 0, 0,
scr->width, scr->height, false);
if (!capture)
errx(EXIT_FAILURE, "Failed to grab image");
}

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

if (!success) {
if (opt.freeze) {
XUngrabServer(disp);
XFlush(disp);
}
return NULL;
}

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

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

if (opt.freeze) {
XUngrabServer(disp);
XFlush(disp);
// captured already, just crop it
imlib_context_set_image(capture);
if (selected) {
capture = imlib_create_cropped_image(
rect0.x, rect0.y, rect0.w, rect0.h);
} else {
capture = NULL;
}
imlib_free_image(); // frees original capture that's in context
if (capture && opt.pointer)
scrotGrabMousePointer(capture, rect0.x, rect0.y);
} else if (selected) {
capture = scrotGrabRectAndPointer(
rect0.x, rect0.y, rect0.w, rect0.h, opt.freeze);
}

if (capture == NULL) {
if (selected)
errx(EXIT_FAILURE, "Failed to grab image");
return NULL;
}

if (opt.selection.mode == SELECTION_MODE_CAPTURE)
Expand Down
Loading