naelstrof 10 лет назад
Родитель
Сommit
fc0d2656cd
4 измененных файлов: 117 добавлений и 23 удалений
  1. 3
    2
      README.md
  2. 100
    14
      main.cpp
  3. 14
    6
      x.cpp
  4. 0
    1
      x.hpp

+ 3
- 2
README.md Просмотреть файл

@@ -1,11 +1,12 @@
1 1
 slrn
2 2
 ====
3 3
 
4
-slrn is an application that literally just allows a user to select a region of his screen. It grabs the cursor, draws a fancy box, and prints the selection's dimensions and position to stdout.
4
+slrn (Select Region) is an application that querys for a selection from the user and prints the region to stdout. It grabs the mouse and turns it into a crosshair, lets the user click and drag to make a selection (or click on a window) while drawing a pretty box around it, then finally prints the selection's dimensions to stdout.
5 5
 
6 6
 features
7 7
 --------
8
+* Hovering over a window will cause a selection rectangle to appear over it.
8 9
 * Clicking on a window makes slrn return the dimensions of the window.
9 10
 * Clicking and dragging causes a selection rectangle to appear, renders pretty well (compared to scrot). And will return the dimensions of that rectangle in absolute screen coords.
10
-* Hovering over a window will cause a selection rectangle to appear over it.
11 11
 * On startup it turns your cursor into a crosshair, then adjusts the cursor into angles as you drag the selection rectangle.
12
+* Supports simple arguments (change selection rectangle border size, x display, padding, etc. )

+ 100
- 14
main.cpp Просмотреть файл

@@ -1,15 +1,101 @@
1 1
 #include <unistd.h>
2
+#include <cstdio>
2 3
 #include "x.hpp"
3 4
 
5
+int borderSize = 10;
6
+int padding = 0;
7
+std::string xserver = ":0";
8
+
9
+void printHelp() {
10
+    printf( "Usage: slrn [options]\n" );
11
+    printf( "Print user selected region to stdout.\n" );
12
+    printf( "\n" );
13
+    printf( "options\n" );
14
+    printf( "    -h, --help                     show this message.\n" );
15
+    printf( "    -b=INT, --bordersize=INT       set selection rectangle border size.\n" );
16
+    printf( "    -p=INT, --padding=INT          set padding size for selection.\n" );
17
+    printf( "    -x=STRING, --xdisplay=STRING   set x display (STRING must be hostname:number.screen_number format)\n" );
18
+    printf( "examples\n" );
19
+    printf( "    slrn -b=10 -x=:0 -p=-30\n" );
20
+}
21
+
22
+int parseOptions( int argc, char** argv ) {
23
+    for ( int i=0; i<argc; i++ ) {
24
+        std::string arg = argv[i];
25
+        if ( arg.substr( 0, 3 ) == "-b=" || arg.substr( 0, 13 ) == "--bordersize=" ) {
26
+            int find = arg.find( "=" );
27
+            if ( find != arg.npos ) {
28
+                arg.at( find ) = ' ';
29
+            }
30
+            int num = sscanf( arg.c_str(), "%*s %i", &borderSize );
31
+            if ( num != 1 ) {
32
+                printf( "Error parsing command arguments near %s\n", argv[i] );
33
+                printf( "Usage: -b=INT or --bordersize=INT\n" );
34
+                printf( "Example: -b=10 or --bordersize=12\n" );
35
+                return 1;
36
+            }
37
+        } else if ( arg.substr( 0, 3 ) == "-p=" || arg.substr( 0, 10 ) == "--padding=" ) {
38
+            int find = arg.find( "=" );
39
+            if ( find != arg.npos ) {
40
+                arg.at( find ) = ' ';
41
+            }
42
+            int num = sscanf( arg.c_str(), "%*s %i", &padding );
43
+            if ( num != 1 ) {
44
+                printf( "Error parsing command arguments near %s\n", argv[i] );
45
+                printf( "Usage: -p=INT or --padding=INT\n" );
46
+                printf( "Example: -p=0 or --padding=-12\n" );
47
+                return 1;
48
+            }
49
+        } else if ( arg.substr( 0, 3 ) == "-x=" || arg.substr( 0, 11 ) == "--xdisplay=" ) {
50
+            int find = arg.find( "=" );
51
+            if ( find != arg.npos ) {
52
+                arg.at( find ) = ' ';
53
+            }
54
+            char* x = new char[ arg.size() ];
55
+            int num = sscanf( arg.c_str(), "%*s %s", x );
56
+            if ( num != 1 ) {
57
+                printf( "Error parsing command arguments near %s\n", argv[i] );
58
+                printf( "Usage: -x=STRING or --xserver=STRING.\n" );
59
+                printf( "Example: -x=:0 or --xserver=winston:1.3\n" );
60
+                delete[] x;
61
+                return 1;
62
+            }
63
+            xserver = x;
64
+            delete[] x;
65
+        } else if ( arg == "-h" || arg == "--help" ) {
66
+            printHelp();
67
+            return 2;
68
+        } else {
69
+            if ( i == 0 ) {
70
+                continue;
71
+            }
72
+            printf( "Error: Unknown argument %s\n", argv[i] );
73
+            printf( "Try -h or --help for help.\n" );
74
+            return 1;
75
+        }
76
+    }
77
+    return 0;
78
+}
79
+
4 80
 int main( int argc, char** argv ) {
81
+    int err = parseOptions( argc, argv );
82
+    if ( err ) {
83
+        return err;
84
+    }
5 85
     int state = 0;
6 86
     bool running = true;
7 87
     is::Rectangle* selection;
8 88
     is::Rectangle* windowselection = NULL;
9 89
     Window window = None;
10 90
 
11
-    xengine->init( ":0" );
12
-    xengine->grabCursor( is::Cross );
91
+    err = xengine->init( xserver.c_str() );
92
+    if ( err ) {
93
+        return err;
94
+    }
95
+    err = xengine->grabCursor( is::Cross );
96
+    if ( err ) {
97
+        return err;
98
+    }
13 99
     while ( running ) {
14 100
         xengine->tick();
15 101
         if ( xengine->mouseDown( 3 ) ) {
@@ -31,11 +117,11 @@ int main( int argc, char** argv ) {
31 117
                         xengine->removeRect( windowselection );
32 118
                     }
33 119
                     is::WindowRectangle t = xengine->m_hoverWindow;
34
-                    windowselection = new is::Rectangle( t.m_x,
35
-                                                         t.m_y,
36
-                                                         t.m_width,
37
-                                                         t.m_height,
38
-                                                         10, 0 );
120
+                    windowselection = new is::Rectangle( t.m_x - padding - t.m_border,
121
+                                                         t.m_y - padding - t.m_border,
122
+                                                         t.m_width + padding*2 + t.m_border,
123
+                                                         t.m_height + padding*2 + t.m_border,
124
+                                                         borderSize, 0 );
39 125
                     xengine->addRect( windowselection );
40 126
                     window = xengine->m_hoverXWindow;
41 127
                 }
@@ -48,8 +134,8 @@ int main( int argc, char** argv ) {
48 134
                 break;
49 135
             }
50 136
             case 1: {
51
-                selection = new is::Rectangle( xengine->m_mousex, xengine->m_mousey, 0, 0, 10, 0 );
52
-                selection->setPos( xengine->m_mousex, xengine->m_mousey );
137
+                selection = new is::Rectangle( xengine->m_mousex - padding, xengine->m_mousey - padding, padding, padding, borderSize, 0 );
138
+                selection->setPos( xengine->m_mousex - padding, xengine->m_mousey - padding );
53 139
                 xengine->addRect( selection );
54 140
                 state++;
55 141
                 break;
@@ -59,7 +145,7 @@ int main( int argc, char** argv ) {
59 145
                     state++;
60 146
                     break;
61 147
                 }
62
-                selection->setDim( xengine->m_mousex - selection->m_x, xengine->m_mousey - selection->m_y );
148
+                selection->setDim( xengine->m_mousex - selection->m_x + padding, xengine->m_mousey - selection->m_y + padding );
63 149
                 // x and y offsets can indicate if the selection is inside-out, which lets us know which kind of cursor we need.
64 150
                 int x = selection->m_xoffset;
65 151
                 int y = selection->m_yoffset;
@@ -90,10 +176,10 @@ int main( int argc, char** argv ) {
90 176
                     break;
91 177
                 }
92 178
                 is::WindowRectangle t = xengine->m_hoverWindow;
93
-                x = t.m_x;
94
-                y = t.m_y;
95
-                w = t.m_width + t.m_border;
96
-                h = t.m_height + t.m_border;
179
+                x = t.m_x - padding - t.m_border;
180
+                y = t.m_y - padding - t.m_border;
181
+                w = t.m_width + t.m_border + padding*2;
182
+                h = t.m_height + t.m_border + padding*2;
97 183
                 printf( "X: %i\n", x );
98 184
                 printf( "Y: %i\n", y );
99 185
                 printf( "W: %i\n", w );

+ 14
- 6
x.cpp Просмотреть файл

@@ -174,6 +174,9 @@ void is::XEngine::setCursor( is::CursorType type ) {
174 174
 
175 175
 is::Rectangle::~Rectangle() {
176 176
     //XFreeGC( xengine->m_display, m_gc );
177
+    if ( m_window == None ) {
178
+        return;
179
+    }
177 180
     XUnmapWindow( xengine->m_display, m_window );
178 181
     XDestroyWindow( xengine->m_display, m_window );
179 182
 }
@@ -187,6 +190,11 @@ is::Rectangle::Rectangle( int x, int y, int width, int height, int border, int p
187 190
     m_height = height;
188 191
     m_border = border;
189 192
     m_padding = padding;
193
+    m_window = None;
194
+
195
+    if ( m_border == 0 ) {
196
+        return;
197
+    }
190 198
 
191 199
     if ( m_width < 0 ) {
192 200
         m_xoffset += m_width;
@@ -219,8 +227,6 @@ is::Rectangle::Rectangle( int x, int y, int width, int height, int border, int p
219 227
 
220 228
     XShapeCombineRectangles( xengine->m_display, m_window, ShapeBounding, 0, 0, &rect, 1, ShapeSubtract, 0);
221 229
     XMapWindow( xengine->m_display, m_window );
222
-
223
-    draw();
224 230
 }
225 231
 
226 232
 void is::Rectangle::setPos( int x, int y ) {
@@ -229,6 +235,9 @@ void is::Rectangle::setPos( int x, int y ) {
229 235
     }
230 236
     m_x = x;
231 237
     m_y = y;
238
+    if ( m_border == 0 ) {
239
+        return;
240
+    }
232 241
     XMoveWindow( xengine->m_display, m_window, m_x-m_border+m_xoffset, m_y-m_border+m_yoffset );
233 242
 }
234 243
 
@@ -249,6 +258,9 @@ void is::Rectangle::setDim( int w, int h ) {
249 258
         m_yoffset += h;
250 259
         m_height = -h;
251 260
     }
261
+    if ( m_border == 0 ) {
262
+        return;
263
+    }
252 264
     XResizeWindow( xengine->m_display, m_window, m_width+m_border*2, m_height+m_border*2 );
253 265
     XMoveWindow( xengine->m_display, m_window, m_x-m_border+m_xoffset, m_y-m_border+m_yoffset );
254 266
     // Now punch another hole in it.
@@ -262,10 +274,6 @@ void is::Rectangle::setDim( int w, int h ) {
262 274
     rect.width = m_width;
263 275
     rect.height = m_height;
264 276
     XShapeCombineRectangles( xengine->m_display, m_window, ShapeBounding, 0, 0, &rect, 1, ShapeSubtract, 0);
265
-    draw();
266
-}
267
-
268
-void is::Rectangle::draw() {
269 277
 }
270 278
 
271 279
 void is::XEngine::updateHoverWindow() {

+ 0
- 1
x.hpp Просмотреть файл

@@ -40,7 +40,6 @@ public:
40 40
             ~Rectangle();
41 41
     void    setPos( int x, int y );
42 42
     void    setDim( int w, int h );
43
-    void    draw();
44 43
     Window  m_window;
45 44
     int     m_x;
46 45
     int     m_y;