main.cpp 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /* main.cpp
  2. *
  3. * Copyright (C) 2014: Dalton Nell, Slop Contributors (https://github.com/naelstrof/slop/graphs/contributors).
  4. *
  5. * This file is part of Slop.
  6. *
  7. * Slop is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Slop is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with Slop. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <unistd.h>
  21. #include <cstdio>
  22. #include "x.hpp"
  23. #include "rectangle.hpp"
  24. #include "cmdline.h"
  25. void printSelection( bool cancelled, int x, int y, int w, int h, int window ) {
  26. printf( "X=%i\n", x );
  27. printf( "Y=%i\n", y );
  28. printf( "W=%i\n", w );
  29. printf( "H=%i\n", h );
  30. printf( "G=%ix%i", w, h );
  31. if ( x >= 0 ) {
  32. printf( "+%i", x );
  33. } else {
  34. // Negative is already included
  35. printf( "%i", x );
  36. }
  37. if ( y >= 0 ) {
  38. printf( "+%i", y );
  39. } else {
  40. // Negative is already included
  41. printf( "%i", y );
  42. }
  43. printf( "\n" );
  44. printf( "ID=%i\n", window );
  45. if ( cancelled ) {
  46. printf( "Cancel=true\n" );
  47. } else {
  48. printf( "Cancel=false\n" );
  49. }
  50. }
  51. int parseColor( std::string arg, float* r, float* g, float* b, float* a ) {
  52. std::string copy = arg;
  53. int find = copy.find( "," );
  54. while( find != copy.npos ) {
  55. copy.at( find ) = ' ';
  56. find = copy.find( "," );
  57. }
  58. // Just in case we didn't include an alpha value
  59. *a = 1;
  60. int num = sscanf( copy.c_str(), "%f %f %f %f", r, g, b, a );
  61. if ( num != 3 && num != 4 ) {
  62. fprintf( stderr, "Error parsing color %s\n", arg.c_str() );
  63. return 1;
  64. }
  65. return 0;
  66. }
  67. void constrain( int sx, int sy, int ex, int ey, int padding, int minimumsize, int maximumsize, int* rsx, int* rsy, int* rex, int* rey ) {
  68. if ( minimumsize > maximumsize && maximumsize > 0 ) {
  69. fprintf( stderr, "Error: minimumsize is greater than maximumsize.\n" );
  70. exit( 1 );
  71. }
  72. int x = std::min( sx, ex );
  73. int y = std::min( sy, ey );
  74. // We add one to make sure we select the pixel under the mouse.
  75. int w = std::max( sx, ex ) - x + 1;
  76. int h = std::max( sy, ey ) - y + 1;
  77. // Make sure we don't turn inside out...
  78. if ( w + padding*2 >= 0 ) {
  79. x -= padding;
  80. w += padding*2;
  81. }
  82. if ( h + padding*2 >= 0 ) {
  83. y -= padding;
  84. h += padding*2;
  85. }
  86. if ( w < minimumsize ) {
  87. int diff = minimumsize - w;
  88. w = minimumsize;
  89. x -= diff/2;
  90. }
  91. if ( h < minimumsize ) {
  92. int diff = minimumsize - h;
  93. h = minimumsize;
  94. y -= diff/2;
  95. }
  96. if ( maximumsize > 0 ) {
  97. if ( w > maximumsize ) {
  98. int diff = w;
  99. w = maximumsize;
  100. x += diff/2 - maximumsize/2;
  101. }
  102. if ( h > maximumsize ) {
  103. int diff = h;
  104. h = maximumsize;
  105. y += diff/2 - maximumsize/2;
  106. }
  107. }
  108. // Center around mouse if we have a fixed size.
  109. if ( maximumsize == minimumsize && w == maximumsize && h == maximumsize ) {
  110. x = ex - maximumsize/2;
  111. y = ey - maximumsize/2;
  112. }
  113. *rsx = x;
  114. *rsy = y;
  115. *rex = x + w;
  116. *rey = y + h;
  117. }
  118. int main( int argc, char** argv ) {
  119. gengetopt_args_info options;
  120. int err = cmdline_parser( argc, argv, &options );
  121. if ( err ) {
  122. return err;
  123. }
  124. int state = 0;
  125. bool running = true;
  126. slop::Rectangle* selection = NULL;
  127. Window window = None;
  128. Window windowmemory = None;
  129. std::string xdisplay = options.xdisplay_arg;
  130. int padding = options.padding_arg;
  131. int borderSize = options.bordersize_arg;
  132. int tolerance = options.tolerance_arg;
  133. float r, g, b, a;
  134. parseColor( options.color_arg, &r, &g, &b, &a );
  135. float gracetime = atof( options.gracetime_arg );
  136. bool highlight = options.highlight_flag;
  137. bool keyboard = !options.nokeyboard_flag;
  138. bool decorations = !options.nodecorations_flag;
  139. timespec start, time;
  140. int cx = 0;
  141. int cy = 0;
  142. int xmem = 0;
  143. int ymem = 0;
  144. int wmem = 0;
  145. int hmem = 0;
  146. int minimumsize = options.min_arg;
  147. int maximumsize = options.max_arg;
  148. cmdline_parser_free( &options );
  149. // First we set up the x interface and grab the mouse,
  150. // if we fail for either we exit immediately.
  151. err = xengine->init( xdisplay.c_str() );
  152. if ( err ) {
  153. printSelection( true, 0, 0, 0, 0, None );
  154. return err;
  155. }
  156. err = xengine->grabCursor( slop::Cross );
  157. if ( err ) {
  158. printSelection( true, 0, 0, 0, 0, None );
  159. return err;
  160. }
  161. if ( keyboard ) {
  162. err = xengine->grabKeyboard();
  163. if ( err ) {
  164. fprintf( stderr, "Warning: Failed to grab the keyboard. This is non-fatal, keyboard presses might fall through to other applications.\n" );
  165. }
  166. }
  167. clock_gettime( CLOCK_REALTIME, &start );
  168. while ( running ) {
  169. clock_gettime( CLOCK_REALTIME, &time );
  170. // "ticking" the xengine makes it process all queued events.
  171. xengine->tick();
  172. // If the user presses any key on the keyboard, exit the application.
  173. // Make sure at least gracetime has passed before allowing canceling
  174. double timei = double( time.tv_sec*1000000000L + time.tv_nsec )/1000000000.f;
  175. double starti = double( start.tv_sec*1000000000L + start.tv_nsec )/1000000000.f;
  176. if ( timei - starti > gracetime ) {
  177. if ( ( xengine->anyKeyPressed() && keyboard ) || xengine->mouseDown( 3 ) ) {
  178. printSelection( true, 0, 0, 0, 0, None );
  179. fprintf( stderr, "User pressed key. Canceled selection.\n" );
  180. state = -1;
  181. running = false;
  182. }
  183. }
  184. // Our adorable little state manager will handle what state we're in.
  185. switch ( state ) {
  186. default: {
  187. break;
  188. }
  189. case 0: {
  190. // If xengine has found a window we're hovering over (or if it changed)
  191. // create a rectangle around it so the user knows he/she can click on it.
  192. // --but only if the user wants us to
  193. if ( window != xengine->m_hoverWindow && tolerance > 0 ) {
  194. slop::WindowRectangle t;
  195. t.setGeometry( xengine->m_hoverWindow, decorations );
  196. t.applyPadding( padding );
  197. t.applyMinMaxSize( minimumsize, maximumsize );
  198. // Make sure we only apply offsets to windows that we've forcibly removed decorations on.
  199. if ( !selection ) {
  200. selection = new slop::Rectangle( t.m_x,
  201. t.m_y,
  202. t.m_x + t.m_width,
  203. t.m_y + t.m_height,
  204. borderSize,
  205. highlight,
  206. r, g, b, a );
  207. } else {
  208. selection->setGeo( t.m_x, t.m_y, t.m_x + t.m_width, t.m_y + t.m_height );
  209. }
  210. window = xengine->m_hoverWindow;
  211. }
  212. // If the user clicked we move on to the next state.
  213. if ( xengine->mouseDown( 1 ) ) {
  214. state++;
  215. }
  216. break;
  217. }
  218. case 1: {
  219. // Set the mouse position of where we clicked, used so that click tolerance doesn't affect the rectangle's position.
  220. cx = xengine->m_mousex;
  221. cy = xengine->m_mousey;
  222. // Also remember where the original selection was
  223. if ( selection ) {
  224. xmem = selection->m_x;
  225. ymem = selection->m_y;
  226. wmem = selection->m_width;
  227. hmem = selection->m_height;
  228. } else {
  229. xmem = cx;
  230. ymem = cy;
  231. }
  232. state++;
  233. break;
  234. }
  235. case 2: {
  236. // It's possible that our selection doesn't exist still, lets make sure it actually gets created here.
  237. if ( !selection ) {
  238. int sx, sy, ex, ey;
  239. constrain( cx, cy, xengine->m_mousex, xengine->m_mousey, padding, minimumsize, maximumsize, &sx, &sy, &ex, &ey );
  240. selection = new slop::Rectangle( sx,
  241. sy,
  242. ex,
  243. ey,
  244. borderSize,
  245. highlight,
  246. r, g, b, a );
  247. }
  248. windowmemory = window;
  249. // If the user has let go of the mouse button, we'll just
  250. // continue to the next state.
  251. if ( !xengine->mouseDown( 1 ) ) {
  252. state++;
  253. break;
  254. }
  255. // Check to make sure the user actually wants to drag for a selection before moving things around.
  256. int w = xengine->m_mousex - cx;
  257. int h = xengine->m_mousey - cy;
  258. if ( ( std::abs( w ) < tolerance && std::abs( h ) < tolerance ) ) {
  259. // We make sure the selection rectangle stays on the window we had selected
  260. selection->setGeo( xmem, ymem, xmem + wmem, ymem + hmem );
  261. xengine->setCursor( slop::Left );
  262. // Make sure
  263. window = windowmemory;
  264. continue;
  265. }
  266. // If we're not selecting a window.
  267. windowmemory = window;
  268. window = None;
  269. // We also detect which way the user is pulling and set the mouse icon accordingly.
  270. bool x = cx > xengine->m_mousex;
  271. bool y = cy > xengine->m_mousey;
  272. if ( selection->m_width <= 1 && selection->m_height <= 1 || ( minimumsize == maximumsize && minimumsize != 0 && maximumsize != 0 ) ) {
  273. xengine->setCursor( slop::Cross );
  274. } else if ( !x && !y ) {
  275. xengine->setCursor( slop::LowerRightCorner );
  276. } else if ( x && !y ) {
  277. xengine->setCursor( slop::LowerLeftCorner );
  278. } else if ( !x && y ) {
  279. xengine->setCursor( slop::UpperRightCorner );
  280. } else if ( x && y ) {
  281. xengine->setCursor( slop::UpperLeftCorner );
  282. }
  283. // Apply padding and minimum size adjustments.
  284. int sx, sy, ex, ey;
  285. constrain( cx, cy, xengine->m_mousex, xengine->m_mousey, padding, minimumsize, maximumsize, &sx, &sy, &ex, &ey );
  286. // Set the selection rectangle's dimensions to mouse movement.
  287. selection->setGeo( sx, sy, ex, ey );
  288. break;
  289. }
  290. case 3: {
  291. int x, y, w, h;
  292. // Exit the utility after this state runs once.
  293. running = false;
  294. // We pull the dimensions and positions from the selection rectangle.
  295. // The selection rectangle automatically converts the positions and
  296. // dimensions to absolute coordinates when we set them earilier.
  297. x = selection->m_x;
  298. y = selection->m_y;
  299. w = selection->m_width;
  300. h = selection->m_height;
  301. // Delete the rectangle, which will remove it from the screen.
  302. delete selection;
  303. // Print the selection :)
  304. printSelection( false, x, y, w, h, window );
  305. break;
  306. }
  307. }
  308. // This sleep is required because drawing the rectangles is a very expensive task that acts really weird with Xorg when called as fast as possible.
  309. // 0.01 seconds
  310. usleep( 10000 );
  311. }
  312. xengine->releaseCursor();
  313. xengine->releaseKeyboard();
  314. // Try to process any last-second requests.
  315. //xengine->tick();
  316. // Clean up global classes.
  317. delete xengine;
  318. // Sleep for 0.05 seconds to ensure everything was cleaned up. (Without this, slop's window often shows up in screenshots.)
  319. usleep( 50000 );
  320. // If we canceled the selection, return error.
  321. if ( state == -1 ) {
  322. return 1;
  323. }
  324. return 0;
  325. }