123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- #include <unistd.h>
- #include <cstdio>
- #include "x.hpp"
- #include "options.hpp"
-
- 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;
- slop::Rectangle* windowselection = 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;
- int cx = 0;
- int cy = 0;
-
-
-
- err = xengine->init( xdisplay.c_str() );
- if ( err ) {
- return err;
- }
- err = xengine->grabCursor( slop::Cross );
- if ( err ) {
- return err;
- }
- err = xengine->grabKeyboard();
- if ( err ) {
- return err;
- }
- while ( running ) {
-
- xengine->tick();
-
- if ( xengine->m_keypressed ) {
- printf( "X=0\n" );
- printf( "Y=0\n" );
- printf( "W=0\n" );
- printf( "H=0\n" );
- fprintf( stderr, "User pressed key. Canceled selection.\n" );
- state = -1;
- running = false;
- }
- if ( xengine->mouseDown( 3 ) ) {
- printf( "X=0\n" );
- printf( "Y=0\n" );
- printf( "W=0\n" );
- printf( "H=0\n" );
- fprintf( stderr, "User right-clicked. Canceled selection.\n" );
- state = -1;
- running = false;
- }
-
- switch ( state ) {
- default: {
- break;
- }
- case 0: {
-
-
- if ( window != xengine->m_hoverXWindow ) {
-
- if ( windowselection ) {
- xengine->removeRect( windowselection );
- }
- slop::WindowRectangle t = xengine->m_hoverWindow;
- windowselection = new slop::Rectangle( t.m_x - t.m_border,
- t.m_y - t.m_border,
- t.m_width + t.m_border,
- t.m_height + t.m_border,
- borderSize, padding,
- r, g, b );
- xengine->addRect( windowselection );
- window = xengine->m_hoverXWindow;
- }
-
-
- if ( xengine->mouseDown( 1 ) ) {
- if ( windowselection ) {
- xengine->removeRect( windowselection );
- }
- state++;
- }
- break;
- }
- case 1: {
-
- cx = xengine->m_mousex;
- cy = xengine->m_mousey;
- state++;
- break;
- }
- case 2: {
-
-
- 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 ) {
- selection = new slop::Rectangle( cx, cy, 0, 0, borderSize, padding, r, g, b );
- xengine->addRect( selection );
- } else if ( std::abs( w ) <= tolerance && std::abs( h ) <= tolerance ) {
- continue;
- }
-
-
-
- selection->setDim( w, h );
-
- bool x = selection->m_flippedx;
- bool y = selection->m_flippedy;
- if ( !x && !y ) {
- xengine->setCursor( slop::LowerRightCorner );
- } else if ( x && !y ) {
- xengine->setCursor( slop::LowerLeftCorner );
- } else if ( !x && y ) {
- xengine->setCursor( slop::UpperRightCorner );
- } else {
- xengine->setCursor( slop::UpperLeftCorner );
- }
-
- break;
- }
- case 3: {
- int x, y, w, h;
-
- running = false;
- if ( selection ) {
-
-
-
- x = selection->m_x+selection->m_xoffset;
- y = selection->m_y+selection->m_yoffset;
- w = selection->m_width;
- h = selection->m_height;
-
- xengine->removeRect( selection );
-
-
-
- if ( w > tolerance || h > tolerance || xengine->m_hoverXWindow == None ) {
- printf( "X=%i\n", x );
- printf( "Y=%i\n", y );
- printf( "W=%i\n", w + 1 );
- printf( "H=%i\n", h + 1 );
- break;
- }
- }
-
- slop::WindowRectangle t = xengine->m_hoverWindow;
- x = t.m_x - padding - t.m_border;
- y = t.m_y - padding - t.m_border;
- w = t.m_width + t.m_border + padding*2;
- h = t.m_height + t.m_border + padding*2;
- printf( "X=%i\n", x );
- printf( "Y=%i\n", y );
- printf( "W=%i\n", w );
- printf( "H=%i\n", h );
- break;
- }
- }
-
-
-
-
- usleep( 1000 );
- }
- xengine->releaseCursor();
- xengine->releaseKeyboard();
-
- delete xengine;
- delete options;
- return 0;
- }
|