Skip to content

Commit

Permalink
Add --focused option
Browse files Browse the repository at this point in the history
Patch from Debian (04-focused.patch)
Description: src/options.c (scrot_parse_option_array): add --focused option.
             src/main.c (scrot_get_geometry, scrot_nice_clip): new functions
             src/main.c (scrot_grab_focused): new function to grab currently
Author: James Cameron <[email protected]>
Last-Update: 2009-05-19
  • Loading branch information
eribertomota committed Feb 12, 2019
1 parent f66085e commit 1e90918
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 54 deletions.
142 changes: 91 additions & 51 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/* main.c
Copyright (C) 1999,2000 Tom Gilbert.
Copyright 1999-2000 Tom Gilbert <[email protected],
[email protected],
[email protected]>
Copyright 2009 James Cameron <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
Expand Down Expand Up @@ -48,7 +51,9 @@ main(int argc,
}


if (opt.select)
if (opt.focused)
image = scrot_grab_focused();
else if (opt.select)
image = scrot_sel_and_grab_image();
else {
scrot_do_delay();
Expand Down Expand Up @@ -170,6 +175,22 @@ scrot_exec_app(Imlib_Image image, struct tm *tm,
exit(0);
}

Imlib_Image
scrot_grab_focused(void)
{
Imlib_Image im = NULL;
int rx = 0, ry = 0, rw = 0, rh = 0;
Window target = None;
int ignored;

scrot_do_delay();
XGetInputFocus(disp, &target, &ignored);
if (!scrot_get_geometry(target, &rx, &ry, &rw, &rh)) return NULL;
scrot_nice_clip(&rx, &ry, &rw, &rh);
im = gib_imlib_create_image_from_drawable(root, 0, rx, ry, rw, rh, 1);
return im;
}

Imlib_Image
scrot_sel_and_grab_image(void)
{
Expand Down Expand Up @@ -313,64 +334,83 @@ scrot_sel_and_grab_image(void)
rh = 0 - rh;
}
} else {
Window child;
XWindowAttributes attr;
int stat;

/* else it's a window click */
/* get geometry of window and use that */
/* get windowmanager frame of window */
if (target != root) {
unsigned int d, x;
int status;

status = XGetGeometry(disp, target, &root, &x, &x, &d, &d, &d, &d);
if (status != 0) {
Window rt, *children, parent;

for (;;) {
/* Find window manager frame. */
status = XQueryTree(disp, target, &rt, &parent, &children, &d);
if (status && (children != None))
XFree((char *) children);
if (!status || (parent == None) || (parent == rt))
break;
target = parent;
}
/* Get client window. */
if (!opt.border)
target = scrot_get_client_window(disp, target);
XRaiseWindow(disp, target);
}
}
stat = XGetWindowAttributes(disp, target, &attr);
if ((stat == False) || (attr.map_state != IsViewable))
return NULL;
rw = attr.width;
rh = attr.height;
XTranslateCoordinates(disp, target, root, 0, 0, &rx, &ry, &child);
}

/* clip rectangle nicely */
if (rx < 0) {
rw += rx;
rx = 0;
}
if (ry < 0) {
rh += ry;
ry = 0;
if (!scrot_get_geometry(target, &rx, &ry, &rw, &rh)) return NULL;
}
if ((rx + rw) > scr->width)
rw = scr->width - rx;
if ((ry + rh) > scr->height)
rh = scr->height - ry;
scrot_nice_clip(&rx, &ry, &rw, &rh);

XBell(disp, 0);
im = gib_imlib_create_image_from_drawable(root, 0, rx, ry, rw, rh, 1);
}
return im;
}

/* clip rectangle nicely */
void
scrot_nice_clip(int *rx,
int *ry,
int *rw,
int *rh)
{
if (*rx < 0) {
*rw += *rx;
*rx = 0;
}
if (*ry < 0) {
*rh += *ry;
*ry = 0;
}
if ((*rx + *rw) > scr->width)
*rw = scr->width - *rx;
if ((*ry + *rh) > scr->height)
*rh = scr->height - *ry;
}

/* get geometry of window and use that */
int
scrot_get_geometry(Window target,
int *rx,
int *ry,
int *rw,
int *rh)
{
Window child;
XWindowAttributes attr;
int stat;

/* get windowmanager frame of window */
if (target != root) {
unsigned int d, x;
int status;

status = XGetGeometry(disp, target, &root, &x, &x, &d, &d, &d, &d);
if (status != 0) {
Window rt, *children, parent;

for (;;) {
/* Find window manager frame. */
status = XQueryTree(disp, target, &rt, &parent, &children, &d);
if (status && (children != None))
XFree((char *) children);
if (!status || (parent == None) || (parent == rt))
break;
target = parent;
}
/* Get client window. */
if (!opt.border)
target = scrot_get_client_window(disp, target);
XRaiseWindow(disp, target);
}
}
stat = XGetWindowAttributes(disp, target, &attr);
if ((stat == False) || (attr.map_state != IsViewable))
return 0;
*rw = attr.width;
*rh = attr.height;
XTranslateCoordinates(disp, target, root, 0, 0, rx, ry, &child);
return 1;
}

Window
scrot_get_window(Display * display,
Window window,
Expand Down
9 changes: 8 additions & 1 deletion src/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Copyright 1999-2000 Tom Gilbert <[email protected],
[email protected],
[email protected]>
Copyright 2008 William Vera <[email protected]>
Copyright 2009 James Cameron <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
Expand Down Expand Up @@ -47,13 +48,15 @@ init_parse_options(int argc, char **argv)
static void
scrot_parse_option_array(int argc, char **argv)
{
static char stropts[] = "bcd:e:hmq:st:v+:";
static char stropts[] = "bcd:e:hmq:st:uv+:";
static struct option lopts[] = {
/* actions */
{"help", 0, 0, 'h'}, /* okay */
{"version", 0, 0, 'v'}, /* okay */
{"count", 0, 0, 'c'},
{"select", 0, 0, 's'},
{"focused", 0, 0, 'u'},
{"focussed", 0, 0, 'u'}, /* macquarie dictionary has both spellings */
{"border", 0, 0, 'b'},
{"multidisp", 0, 0, 'm'},
/* toggles */
Expand Down Expand Up @@ -98,6 +101,9 @@ scrot_parse_option_array(int argc, char **argv)
case 's':
opt.select = 1;
break;
case 'u':
opt.focused = 1;
break;
case '+':
opt.debug_level = atoi(optarg);
break;
Expand Down Expand Up @@ -234,6 +240,7 @@ show_usage(void)
" and join them together.\n"
" -s, --select interactively choose a window or rectangle\n"
" with the mouse\n"
" -u, --focused use the currently focused window\n"
" -t, --thumb NUM generate thumbnail too. NUM is the percentage\n"
" of the original size for the thumbnail to be,\n"
" or the geometry in percent, e.g. 50x60 or 80x20.\n"
Expand Down
6 changes: 5 additions & 1 deletion src/options.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/* options.h
Copyright (C) 1999,2000 Tom Gilbert.
Copyright 1999-2000 Tom Gilbert <[email protected],
[email protected],
[email protected]>
Copyright 2009 James Cameron <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
Expand Down Expand Up @@ -32,6 +35,7 @@ struct __scrotoptions
int delay;
int countdown;
int select;
int focused;
int quality;
int border;
int multidisp;
Expand Down
8 changes: 7 additions & 1 deletion src/scrot.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/* scrot.h
Copyright (C) 1999,2000 Tom Gilbert.
Copyright 1999-2000 Tom Gilbert <[email protected],
[email protected],
[email protected]>
Copyright 2009 James Cameron <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
Expand Down Expand Up @@ -72,7 +75,10 @@ void scrot_exec_app(Imlib_Image image, struct tm *tm,
char *filename_im, char *filename_thumb);
void scrot_do_delay(void);
Imlib_Image scrot_sel_and_grab_image(void);
Imlib_Image scrot_grab_focused(void);
void scrot_sel_area(int *x, int *y, int *w, int *h);
void scrot_nice_clip(int *rx, int *ry, int *rw, int *rh);
int scrot_get_geometry(Window target, int *rx, int *ry, int *rw, int *rh);
Window scrot_get_window(Display *display,Window window,int x,int y);
Window scrot_get_client_window(Display * display, Window target);
Window scrot_find_window_by_property(Display * display, const Window window,
Expand Down

0 comments on commit 1e90918

Please sign in to comment.