123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- #include <unistd.h>
- #include <cstdio>
- #include "x.hpp"
- #include "rectangle.hpp"
- #include "options.hpp"
-
- void printSelection( bool cancelled, int x, int y, int w, int h ) {
- printf( "X=%i\n", x );
- printf( "Y=%i\n", y );
- printf( "W=%i\n", w );
- printf( "H=%i\n", h );
- printf( "G=%ix%i", w, h );
- if ( x >= 0 ) {
- printf( "+%i", x );
- } else {
-
- printf( "%i", x );
- }
- if ( y >= 0 ) {
- printf( "+%i", y );
- } else {
-
- printf( "%i", y );
- }
- printf( "\n" );
- if ( cancelled ) {
- printf( "Cancel=true\n" );
- } else {
- printf( "Cancel=false\n" );
- }
- }
-
- int main( int argc, char** argv ) {
- int err = options->parseOptions( argc, argv );
- if ( err ) {
- return err;
- }
- int state = 0;
- bool running = true;
- slop::Rectangle* selection = NULL;
- Window window = None;
- std::string xdisplay = options->m_xdisplay;
- int padding = options->m_padding;
- int borderSize = options->m_borderSize;
- int tolerance = options->m_tolerance;
- float r = options->m_red;
- float g = options->m_green;
- float b = options->m_blue;
- bool keyboard = options->m_keyboard;
- bool decorations = options->m_decorations;
- timespec start, time;
- int cx = 0;
- int cy = 0;
- int xmem = 0;
- int ymem = 0;
- int wmem = 0;
- int hmem = 0;
-
-
-
- err = xengine->init( xdisplay.c_str() );
- if ( err ) {
- printSelection( true, 0, 0, 0, 0 );
- return err;
- }
- err = xengine->grabCursor( slop::Cross );
- if ( err ) {
- printSelection( true, 0, 0, 0, 0 );
- return err;
- }
- if ( keyboard ) {
- err = xengine->grabKeyboard();
- if ( err ) {
- printSelection( true, 0, 0, 0, 0 );
- return err;
- }
- }
- clock_gettime( CLOCK_REALTIME, &start );
- while ( running ) {
- clock_gettime( CLOCK_REALTIME, &time );
-
- xengine->tick();
-
-
- double timei = double( time.tv_sec*1000000000L + time.tv_nsec )/1000000000.f;
- double starti = double( start.tv_sec*1000000000L + start.tv_nsec )/1000000000.f;
- if ( timei - starti > options->m_gracetime ) {
- if ( xengine->anyKeyPressed() && keyboard || xengine->mouseDown( 3 ) ) {
- printSelection( true, 0, 0, 0, 0 );
- fprintf( stderr, "User pressed key. Canceled selection.\n" );
- state = -1;
- running = false;
- }
- }
-
- switch ( state ) {
- default: {
- break;
- }
- case 0: {
-
-
-
- if ( window != xengine->m_hoverWindow && tolerance > 0 ) {
- slop::WindowRectangle t;
- t.setGeometry( xengine->m_hoverWindow, decorations );
-
- if ( !selection ) {
- selection = new slop::Rectangle( t.m_x,
- t.m_y,
- t.m_width,
- t.m_height,
- borderSize, padding,
- r, g, b );
- } else {
- selection->setGeo( t.m_x, t.m_y, t.m_width, t.m_height );
- }
- window = xengine->m_hoverWindow;
- }
-
- if ( xengine->mouseDown( 1 ) ) {
- state++;
- }
- break;
- }
- case 1: {
-
- cx = xengine->m_mousex;
- cy = xengine->m_mousey;
-
- if ( selection ) {
- xmem = selection->m_x;
- ymem = selection->m_y;
- wmem = selection->m_width;
- hmem = selection->m_height;
- } else {
- xmem = cx;
- ymem = cy;
- }
- state++;
- break;
- }
- case 2: {
-
- if ( !selection ) {
- selection = new slop::Rectangle( cx,
- cy,
- xengine->m_mousex - cx,
- xengine->m_mousey - cy,
- borderSize, padding,
- r, g, b );
- }
-
-
- if ( !xengine->mouseDown( 1 ) ) {
- state++;
- break;
- }
-
- int w = xengine->m_mousex - cx;
- int h = xengine->m_mousey - cy;
- if ( ( std::abs( w ) < tolerance && std::abs( h ) < tolerance ) ) {
-
- selection->setGeo( xmem, ymem, wmem, hmem );
- xengine->setCursor( slop::Left );
- continue;
- }
-
- bool x = selection->m_flippedx;
- bool y = selection->m_flippedy;
- if ( selection->m_width == 0 && selection->m_height == 0 ) {
- xengine->setCursor( slop::Cross );
- } else if ( !x && !y ) {
- xengine->setCursor( slop::LowerRightCorner );
- } else if ( x && !y ) {
- xengine->setCursor( slop::LowerLeftCorner );
- } else if ( !x && y ) {
- xengine->setCursor( slop::UpperRightCorner );
- } else if ( x && y ) {
- xengine->setCursor( slop::UpperLeftCorner );
- }
-
-
-
- selection->setGeo( cx, cy, w, h );
- break;
- }
- case 3: {
- int x, y, w, h;
-
- running = false;
-
-
-
- x = selection->m_x+selection->m_xoffset;
- y = selection->m_y+selection->m_yoffset;
- w = selection->m_width;
- h = selection->m_height;
-
- delete selection;
-
- printSelection( false, x, y, w, h );
- break;
- }
- }
-
-
- usleep( 10000 );
- }
- xengine->releaseCursor();
- xengine->releaseKeyboard();
-
-
-
- delete xengine;
- delete options;
-
- if ( state == -1 ) {
- return 1;
- }
- return 0;
- }
|