瀏覽代碼

added some features

naelstrof 11 年之前
父節點
當前提交
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
 slrn
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
 features
6
 features
7
 --------
7
 --------
8
+* Hovering over a window will cause a selection rectangle to appear over it.
8
 * Clicking on a window makes slrn return the dimensions of the window.
9
 * Clicking on a window makes slrn return the dimensions of the window.
9
 * 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
 * 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
 * On startup it turns your cursor into a crosshair, then adjusts the cursor into angles as you drag the selection rectangle.
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
 #include <unistd.h>
1
 #include <unistd.h>
2
+#include <cstdio>
2
 #include "x.hpp"
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
 int main( int argc, char** argv ) {
80
 int main( int argc, char** argv ) {
81
+    int err = parseOptions( argc, argv );
82
+    if ( err ) {
83
+        return err;
84
+    }
5
     int state = 0;
85
     int state = 0;
6
     bool running = true;
86
     bool running = true;
7
     is::Rectangle* selection;
87
     is::Rectangle* selection;
8
     is::Rectangle* windowselection = NULL;
88
     is::Rectangle* windowselection = NULL;
9
     Window window = None;
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
     while ( running ) {
99
     while ( running ) {
14
         xengine->tick();
100
         xengine->tick();
15
         if ( xengine->mouseDown( 3 ) ) {
101
         if ( xengine->mouseDown( 3 ) ) {
31
                         xengine->removeRect( windowselection );
117
                         xengine->removeRect( windowselection );
32
                     }
118
                     }
33
                     is::WindowRectangle t = xengine->m_hoverWindow;
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
                     xengine->addRect( windowselection );
125
                     xengine->addRect( windowselection );
40
                     window = xengine->m_hoverXWindow;
126
                     window = xengine->m_hoverXWindow;
41
                 }
127
                 }
48
                 break;
134
                 break;
49
             }
135
             }
50
             case 1: {
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
                 xengine->addRect( selection );
139
                 xengine->addRect( selection );
54
                 state++;
140
                 state++;
55
                 break;
141
                 break;
59
                     state++;
145
                     state++;
60
                     break;
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
                 // x and y offsets can indicate if the selection is inside-out, which lets us know which kind of cursor we need.
149
                 // x and y offsets can indicate if the selection is inside-out, which lets us know which kind of cursor we need.
64
                 int x = selection->m_xoffset;
150
                 int x = selection->m_xoffset;
65
                 int y = selection->m_yoffset;
151
                 int y = selection->m_yoffset;
90
                     break;
176
                     break;
91
                 }
177
                 }
92
                 is::WindowRectangle t = xengine->m_hoverWindow;
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
                 printf( "X: %i\n", x );
183
                 printf( "X: %i\n", x );
98
                 printf( "Y: %i\n", y );
184
                 printf( "Y: %i\n", y );
99
                 printf( "W: %i\n", w );
185
                 printf( "W: %i\n", w );

+ 14
- 6
x.cpp 查看文件

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

+ 0
- 1
x.hpp 查看文件

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