|
@@ -19,35 +19,77 @@
|
19
|
19
|
*/
|
20
|
20
|
#include <unistd.h>
|
21
|
21
|
#include <cstdio>
|
|
22
|
+#include <sstream>
|
22
|
23
|
#include "x.hpp"
|
23
|
24
|
#include "rectangle.hpp"
|
24
|
25
|
#include "cmdline.h"
|
25
|
26
|
|
26
|
|
-void printSelection( bool cancelled, int x, int y, int w, int h, int window ) {
|
27
|
|
- printf( "X=%i\n", x );
|
28
|
|
- printf( "Y=%i\n", y );
|
29
|
|
- printf( "W=%i\n", w );
|
30
|
|
- printf( "H=%i\n", h );
|
31
|
|
- printf( "G=%ix%i", w, h );
|
32
|
|
- if ( x >= 0 ) {
|
33
|
|
- printf( "+%i", x );
|
34
|
|
- } else {
|
35
|
|
- // Negative is already included
|
36
|
|
- printf( "%i", x );
|
37
|
|
- }
|
38
|
|
- if ( y >= 0 ) {
|
39
|
|
- printf( "+%i", y );
|
40
|
|
- } else {
|
41
|
|
- // Negative is already included
|
42
|
|
- printf( "%i", y );
|
|
27
|
+int printSelection( std::string format, bool cancelled, int x, int y, int w, int h, int window ) {
|
|
28
|
+ size_t pos = 0;
|
|
29
|
+ while ( ( pos = format.find( "%", pos ) ) != std::string::npos ) {
|
|
30
|
+ if ( pos + 1 > format.size() ) {
|
|
31
|
+ fprintf( stderr, "Format error: %% found at the end of format string.\n", format[ pos + 1 ] );
|
|
32
|
+ return 1;
|
|
33
|
+ }
|
|
34
|
+ std::stringstream foo;
|
|
35
|
+ switch( format[ pos + 1 ] ) {
|
|
36
|
+ case '%':
|
|
37
|
+ format.replace( pos, 2, "%" );
|
|
38
|
+ pos += 1;
|
|
39
|
+ break;
|
|
40
|
+ case 'x':
|
|
41
|
+ case 'X':
|
|
42
|
+ foo << x;
|
|
43
|
+ format.replace( pos, 2, foo.str() );
|
|
44
|
+ break;
|
|
45
|
+ case 'y':
|
|
46
|
+ case 'Y':
|
|
47
|
+ foo << y;
|
|
48
|
+ format.replace( pos, 2, foo.str() );
|
|
49
|
+ break;
|
|
50
|
+ case 'w':
|
|
51
|
+ case 'W':
|
|
52
|
+ foo << w;
|
|
53
|
+ format.replace( pos, 2, foo.str() );
|
|
54
|
+ break;
|
|
55
|
+ case 'h':
|
|
56
|
+ case 'H':
|
|
57
|
+ foo << h;
|
|
58
|
+ format.replace( pos, 2, foo.str() );
|
|
59
|
+ break;
|
|
60
|
+ case 'g':
|
|
61
|
+ case 'G':
|
|
62
|
+ foo << w << 'x' << h << '+' << x << '+' << y;
|
|
63
|
+ format.replace( pos, 2, foo.str() );
|
|
64
|
+ break;
|
|
65
|
+ case 'i':
|
|
66
|
+ case 'I':
|
|
67
|
+ foo << window;
|
|
68
|
+ format.replace( pos, 2, foo.str() );
|
|
69
|
+ break;
|
|
70
|
+ case 'c':
|
|
71
|
+ case 'C':
|
|
72
|
+ format.replace( pos, 2, cancelled ? "true" : "false" );
|
|
73
|
+ break;
|
|
74
|
+ default:
|
|
75
|
+ fprintf( stderr, "Format error: %%%c is an unknown replacement identifier.\n", format[ pos + 1 ] );
|
|
76
|
+ fprintf( stderr, "Valid replacements: %%x, %%y, %%w, %%h, %%i, %%c, %%.\n" );
|
|
77
|
+ return 1;
|
|
78
|
+ break;
|
|
79
|
+ }
|
43
|
80
|
}
|
44
|
|
- printf( "\n" );
|
45
|
|
- printf( "ID=%i\n", window );
|
46
|
|
- if ( cancelled ) {
|
47
|
|
- printf( "Cancel=true\n" );
|
48
|
|
- } else {
|
49
|
|
- printf( "Cancel=false\n" );
|
|
81
|
+ pos = 0;
|
|
82
|
+ while ( ( pos = format.find( "\\", pos ) ) != std::string::npos ) {
|
|
83
|
+ if ( pos + 1 > format.size() ) {
|
|
84
|
+ break;
|
|
85
|
+ }
|
|
86
|
+ if ( format[ pos + 1 ] == 'n' ) {
|
|
87
|
+ format.replace( pos, 2, "\n" );
|
|
88
|
+ }
|
|
89
|
+ pos = pos + 1;
|
50
|
90
|
}
|
|
91
|
+ printf( "%s", format.c_str() );
|
|
92
|
+ return 0;
|
51
|
93
|
}
|
52
|
94
|
|
53
|
95
|
int parseColor( std::string arg, float* r, float* g, float* b, float* a ) {
|
|
@@ -150,18 +192,19 @@ int main( int argc, char** argv ) {
|
150
|
192
|
int hmem = 0;
|
151
|
193
|
int minimumsize = options.min_arg;
|
152
|
194
|
int maximumsize = options.max_arg;
|
|
195
|
+ std::string format = options.format_arg;
|
153
|
196
|
cmdline_parser_free( &options );
|
154
|
197
|
|
155
|
198
|
// First we set up the x interface and grab the mouse,
|
156
|
199
|
// if we fail for either we exit immediately.
|
157
|
200
|
err = xengine->init( xdisplay.c_str() );
|
158
|
201
|
if ( err ) {
|
159
|
|
- printSelection( true, 0, 0, 0, 0, None );
|
|
202
|
+ printSelection( format, true, 0, 0, 0, 0, None );
|
160
|
203
|
return err;
|
161
|
204
|
}
|
162
|
205
|
err = xengine->grabCursor( slop::Cross );
|
163
|
206
|
if ( err ) {
|
164
|
|
- printSelection( true, 0, 0, 0, 0, None );
|
|
207
|
+ printSelection( format, true, 0, 0, 0, 0, None );
|
165
|
208
|
return err;
|
166
|
209
|
}
|
167
|
210
|
if ( keyboard ) {
|
|
@@ -181,7 +224,7 @@ int main( int argc, char** argv ) {
|
181
|
224
|
double starti = double( start.tv_sec*1000000000L + start.tv_nsec )/1000000000.f;
|
182
|
225
|
if ( timei - starti > gracetime ) {
|
183
|
226
|
if ( ( xengine->anyKeyPressed() && keyboard ) || xengine->mouseDown( 3 ) ) {
|
184
|
|
- printSelection( true, 0, 0, 0, 0, None );
|
|
227
|
+ printSelection( format, true, 0, 0, 0, 0, None );
|
185
|
228
|
fprintf( stderr, "User pressed key. Canceled selection.\n" );
|
186
|
229
|
state = -1;
|
187
|
230
|
running = false;
|
|
@@ -309,7 +352,7 @@ int main( int argc, char** argv ) {
|
309
|
352
|
// Delete the rectangle, which will remove it from the screen.
|
310
|
353
|
delete selection;
|
311
|
354
|
// Print the selection :)
|
312
|
|
- printSelection( false, x, y, w, h, window );
|
|
355
|
+ printSelection( format, false, x, y, w, h, window );
|
313
|
356
|
break;
|
314
|
357
|
}
|
315
|
358
|
}
|