04-focused.patch 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. Description: src/options.c (scrot_parse_option_array): add --focused option.
  2. src/main.c (scrot_get_geometry, scrot_nice_clip): new functions
  3. src/main.c (scrot_grab_focused): new function to grab currently
  4. Author: James Cameron <quozl@us.netrek.org>
  5. Last-Update: 2009-05-19
  6. Index: scrot-0.8/src/options.h
  7. ===================================================================
  8. --- scrot-0.8.orig/src/options.h
  9. +++ scrot-0.8/src/options.h
  10. @@ -32,6 +32,7 @@ struct __scrotoptions
  11. int delay;
  12. int countdown;
  13. int select;
  14. + int focused;
  15. int quality;
  16. int border;
  17. int multidisp;
  18. Index: scrot-0.8/src/options.c
  19. ===================================================================
  20. --- scrot-0.8.orig/src/options.c
  21. +++ scrot-0.8/src/options.c
  22. @@ -44,13 +44,15 @@ init_parse_options(int argc, char **argv
  23. static void
  24. scrot_parse_option_array(int argc, char **argv)
  25. {
  26. - static char stropts[] = "bcd:e:hmq:st:v+:";
  27. + static char stropts[] = "bcd:e:hmq:st:uv+:";
  28. static struct option lopts[] = {
  29. /* actions */
  30. {"help", 0, 0, 'h'}, /* okay */
  31. {"version", 0, 0, 'v'}, /* okay */
  32. {"count", 0, 0, 'c'},
  33. {"select", 0, 0, 's'},
  34. + {"focused", 0, 0, 'u'},
  35. + {"focussed", 0, 0, 'u'}, /* macquarie dictionary has both spellings */
  36. {"border", 0, 0, 'b'},
  37. {"multidisp", 0, 0, 'm'},
  38. /* toggles */
  39. @@ -95,6 +97,9 @@ scrot_parse_option_array(int argc, char
  40. case 's':
  41. opt.select = 1;
  42. break;
  43. + case 'u':
  44. + opt.focused = 1;
  45. + break;
  46. case '+':
  47. opt.debug_level = atoi(optarg);
  48. break;
  49. @@ -231,6 +236,7 @@ show_usage(void)
  50. " and join them together.\n"
  51. " -s, --select interactively choose a window or rectangle\n"
  52. " with the mouse\n"
  53. + " -u, --focused use the currently focused window\n"
  54. " -t, --thumb NUM generate thumbnail too. NUM is the percentage\n"
  55. " of the original size for the thumbnail to be,\n"
  56. " or the geometry in percent, e.g. 50x60 or 80x20.\n"
  57. Index: scrot-0.8/src/scrot.h
  58. ===================================================================
  59. --- scrot-0.8.orig/src/scrot.h
  60. +++ scrot-0.8/src/scrot.h
  61. @@ -72,7 +72,10 @@ void scrot_exec_app(Imlib_Image image, s
  62. char *filename_im, char *filename_thumb);
  63. void scrot_do_delay(void);
  64. Imlib_Image scrot_sel_and_grab_image(void);
  65. +Imlib_Image scrot_grab_focused(void);
  66. void scrot_sel_area(int *x, int *y, int *w, int *h);
  67. +void scrot_nice_clip(int *rx, int *ry, int *rw, int *rh);
  68. +int scrot_get_geometry(Window target, int *rx, int *ry, int *rw, int *rh);
  69. Window scrot_get_window(Display *display,Window window,int x,int y);
  70. Window scrot_get_client_window(Display * display, Window target);
  71. Window scrot_find_window_by_property(Display * display, const Window window,
  72. Index: scrot-0.8/src/main.c
  73. ===================================================================
  74. --- scrot-0.8.orig/src/main.c
  75. +++ scrot-0.8/src/main.c
  76. @@ -48,7 +48,9 @@ main(int argc,
  77. }
  78. - if (opt.select)
  79. + if (opt.focused)
  80. + image = scrot_grab_focused();
  81. + else if (opt.select)
  82. image = scrot_sel_and_grab_image();
  83. else {
  84. scrot_do_delay();
  85. @@ -171,6 +173,22 @@ scrot_exec_app(Imlib_Image image, struct
  86. }
  87. Imlib_Image
  88. +scrot_grab_focused(void)
  89. +{
  90. + Imlib_Image im = NULL;
  91. + int rx = 0, ry = 0, rw = 0, rh = 0;
  92. + Window target = None;
  93. + int ignored;
  94. +
  95. + scrot_do_delay();
  96. + XGetInputFocus(disp, &target, &ignored);
  97. + if (!scrot_get_geometry(target, &rx, &ry, &rw, &rh)) return NULL;
  98. + scrot_nice_clip(&rx, &ry, &rw, &rh);
  99. + im = gib_imlib_create_image_from_drawable(root, 0, rx, ry, rw, rh, 1);
  100. + return im;
  101. +}
  102. +
  103. +Imlib_Image
  104. scrot_sel_and_grab_image(void)
  105. {
  106. Imlib_Image im = NULL;
  107. @@ -313,57 +331,10 @@ scrot_sel_and_grab_image(void)
  108. rh = 0 - rh;
  109. }
  110. } else {
  111. - Window child;
  112. - XWindowAttributes attr;
  113. - int stat;
  114. -
  115. /* else it's a window click */
  116. - /* get geometry of window and use that */
  117. - /* get windowmanager frame of window */
  118. - if (target != root) {
  119. - unsigned int d, x;
  120. - int status;
  121. -
  122. - status = XGetGeometry(disp, target, &root, &x, &x, &d, &d, &d, &d);
  123. - if (status != 0) {
  124. - Window rt, *children, parent;
  125. -
  126. - for (;;) {
  127. - /* Find window manager frame. */
  128. - status = XQueryTree(disp, target, &rt, &parent, &children, &d);
  129. - if (status && (children != None))
  130. - XFree((char *) children);
  131. - if (!status || (parent == None) || (parent == rt))
  132. - break;
  133. - target = parent;
  134. - }
  135. - /* Get client window. */
  136. - if (!opt.border)
  137. - target = scrot_get_client_window(disp, target);
  138. - XRaiseWindow(disp, target);
  139. - }
  140. - }
  141. - stat = XGetWindowAttributes(disp, target, &attr);
  142. - if ((stat == False) || (attr.map_state != IsViewable))
  143. - return NULL;
  144. - rw = attr.width;
  145. - rh = attr.height;
  146. - XTranslateCoordinates(disp, target, root, 0, 0, &rx, &ry, &child);
  147. - }
  148. -
  149. - /* clip rectangle nicely */
  150. - if (rx < 0) {
  151. - rw += rx;
  152. - rx = 0;
  153. + if (!scrot_get_geometry(target, &rx, &ry, &rw, &rh)) return NULL;
  154. }
  155. - if (ry < 0) {
  156. - rh += ry;
  157. - ry = 0;
  158. - }
  159. - if ((rx + rw) > scr->width)
  160. - rw = scr->width - rx;
  161. - if ((ry + rh) > scr->height)
  162. - rh = scr->height - ry;
  163. + scrot_nice_clip(&rx, &ry, &rw, &rh);
  164. XBell(disp, 0);
  165. im = gib_imlib_create_image_from_drawable(root, 0, rx, ry, rw, rh, 1);
  166. @@ -371,6 +342,72 @@ scrot_sel_and_grab_image(void)
  167. return im;
  168. }
  169. +/* clip rectangle nicely */
  170. +void
  171. +scrot_nice_clip(int *rx,
  172. + int *ry,
  173. + int *rw,
  174. + int *rh)
  175. +{
  176. + if (*rx < 0) {
  177. + *rw += *rx;
  178. + *rx = 0;
  179. + }
  180. + if (*ry < 0) {
  181. + *rh += *ry;
  182. + *ry = 0;
  183. + }
  184. + if ((*rx + *rw) > scr->width)
  185. + *rw = scr->width - *rx;
  186. + if ((*ry + *rh) > scr->height)
  187. + *rh = scr->height - *ry;
  188. +}
  189. +
  190. +/* get geometry of window and use that */
  191. +int
  192. +scrot_get_geometry(Window target,
  193. + int *rx,
  194. + int *ry,
  195. + int *rw,
  196. + int *rh)
  197. +{
  198. + Window child;
  199. + XWindowAttributes attr;
  200. + int stat;
  201. +
  202. + /* get windowmanager frame of window */
  203. + if (target != root) {
  204. + unsigned int d, x;
  205. + int status;
  206. +
  207. + status = XGetGeometry(disp, target, &root, &x, &x, &d, &d, &d, &d);
  208. + if (status != 0) {
  209. + Window rt, *children, parent;
  210. +
  211. + for (;;) {
  212. + /* Find window manager frame. */
  213. + status = XQueryTree(disp, target, &rt, &parent, &children, &d);
  214. + if (status && (children != None))
  215. + XFree((char *) children);
  216. + if (!status || (parent == None) || (parent == rt))
  217. + break;
  218. + target = parent;
  219. + }
  220. + /* Get client window. */
  221. + if (!opt.border)
  222. + target = scrot_get_client_window(disp, target);
  223. + XRaiseWindow(disp, target);
  224. + }
  225. + }
  226. + stat = XGetWindowAttributes(disp, target, &attr);
  227. + if ((stat == False) || (attr.map_state != IsViewable))
  228. + return 0;
  229. + *rw = attr.width;
  230. + *rh = attr.height;
  231. + XTranslateCoordinates(disp, target, root, 0, 0, rx, ry, &child);
  232. + return 1;
  233. +}
  234. +
  235. Window
  236. scrot_get_window(Display * display,
  237. Window window,