|
@@ -1,276 +0,0 @@
|
1
|
|
-#include "options.hpp"
|
2
|
|
-
|
3
|
|
-slop::Options* options = new slop::Options();
|
4
|
|
-
|
5
|
|
-slop::Options::Options() {
|
6
|
|
- m_version = "v2.1.4";
|
7
|
|
- m_highlight = false;
|
8
|
|
- m_borderSize = 5;
|
9
|
|
- m_padding = 0;
|
10
|
|
- m_xdisplay = ":0";
|
11
|
|
- m_tolerance = 2;
|
12
|
|
- m_red = 0.5;
|
13
|
|
- m_green = 0.5;
|
14
|
|
- m_blue = 0.5;
|
15
|
|
- m_alpha = 1;
|
16
|
|
- m_gracetime = 0.4;
|
17
|
|
- m_keyboard = true;
|
18
|
|
- m_decorations = true;
|
19
|
|
- m_minimumsize = 0;
|
20
|
|
- m_maxsize = 0;
|
21
|
|
-}
|
22
|
|
-
|
23
|
|
-void slop::Options::printHelp() {
|
24
|
|
- printf( "Usage: slop [options]\n" );
|
25
|
|
- printf( "Print user selected region to stdout. Pressing keys or right-clicking cancels selection.\n" );
|
26
|
|
- printf( "\n" );
|
27
|
|
- printf( "Options\n" );
|
28
|
|
- printf( " -h, --help Show this message.\n" );
|
29
|
|
- printf( " -nkb, --nokeyboard Disables the ability to cancel selections with the keyboard.\n" );
|
30
|
|
- printf( " -b=INT, --bordersize=INT Set selection rectangle border size.\n" );
|
31
|
|
- printf( " -p=INT, --padding=INT Set padding size for selection.\n" );
|
32
|
|
- printf( " -t=INT, --tolerance=INT How far in pixels the mouse can move after clicking and still be detected\n" );
|
33
|
|
- printf( " as a normal click. Setting to zero will disable window selections.\n" );
|
34
|
|
- printf( " -x=STRING, --xdisplay=STRING Set x display (STRING must be hostname:number.screen_number format)\n" );
|
35
|
|
- printf( " -c=COLOR, --color=COLOR Set selection rectangle color, COLOR is in format FLOAT,FLOAT,FLOAT,FLOAT.\n" );
|
36
|
|
- printf( " takes RGBA or RGB.\n" );
|
37
|
|
- printf( " -g=FLOAT, --gracetime=FLOAT Set the amount of time before slop will check for keyboard cancellations\n" );
|
38
|
|
- printf( " in seconds.\n" );
|
39
|
|
- printf( " -nd, --nodecorations Attempts to remove decorations from window selections.\n" );
|
40
|
|
- printf( " -min=INT, --minimumsize=INT Sets the minimum output of width or height values, useful to avoid outputting 0\n" );
|
41
|
|
- printf( " widths or heights.\n" );
|
42
|
|
- printf( " -max=INT, --maximumsize=INT Sets the maximum output of width or height values.\n" );
|
43
|
|
- printf( " -hi, --highlight Instead of outlining the selection, slop highlights it. Only useful when\n" );
|
44
|
|
- printf( " used with a --color with an alpha under 1.\n" );
|
45
|
|
- printf( " -v, --version prints version.\n" );
|
46
|
|
- printf( "\n" );
|
47
|
|
- printf( "Examples\n" );
|
48
|
|
- printf( " $ # Gray, thick, transparent border for maximum visiblity.\n" );
|
49
|
|
- printf( " $ slop -b=20 -c=0.5,0.5,0.5,0.8\n" );
|
50
|
|
- printf( "\n" );
|
51
|
|
- printf( " $ # Remove window decorations.\n" );
|
52
|
|
- printf( " $ slop -nd\n" );
|
53
|
|
- printf( "\n" );
|
54
|
|
- printf( " $ # Disable window selections. Useful for selecting individual pixels.\n" );
|
55
|
|
- printf( " $ slop -t=0\n" );
|
56
|
|
- printf( "\n" );
|
57
|
|
- printf( " $ # Classic Windows XP selection.\n" );
|
58
|
|
- printf( " $ slop -hi -c=0.3,0.4,0.6,0.4\n" );
|
59
|
|
-}
|
60
|
|
-
|
61
|
|
-int slop::Options::parseOptions( int argc, char** argv ) {
|
62
|
|
- // Simple command parsing. Just uses sscanf to read each argument.
|
63
|
|
- // It looks complicated because you have to have spaces for delimiters for sscanf.
|
64
|
|
- for ( int i=0; i<argc; i++ ) {
|
65
|
|
- std::string arg = argv[i];
|
66
|
|
- if ( matches( arg, "-b=", "--bordersize=" ) ) {
|
67
|
|
- int err = parseInt( arg, &m_borderSize );
|
68
|
|
- if ( err ) {
|
69
|
|
- return 1;
|
70
|
|
- }
|
71
|
|
- if ( m_borderSize < 0 ) {
|
72
|
|
- m_borderSize = 0;
|
73
|
|
- }
|
74
|
|
- } else if ( matches( arg, "-min=", "--minimumsize=" ) ) {
|
75
|
|
- int err = parseInt( arg, &m_minimumsize );
|
76
|
|
- if ( err ) {
|
77
|
|
- return 1;
|
78
|
|
- }
|
79
|
|
- } else if ( matches( arg, "-max=", "--maximumsize=" ) ) {
|
80
|
|
- int err = parseInt( arg, &m_maximumsize );
|
81
|
|
- if ( err ) {
|
82
|
|
- return 1;
|
83
|
|
- }
|
84
|
|
- } else if ( matches( arg, "-p=", "--padding=" ) ) {
|
85
|
|
- int err = parseInt( arg, &m_padding );
|
86
|
|
- if ( err ) {
|
87
|
|
- return 1;
|
88
|
|
- }
|
89
|
|
- } else if ( matches( arg, "-c=", "--color=" ) ) {
|
90
|
|
- int err = parseColor( arg, &m_red, &m_green, &m_blue, &m_alpha );
|
91
|
|
- if ( err ) {
|
92
|
|
- return 1;
|
93
|
|
- }
|
94
|
|
- } else if ( matches( arg, "-t=", "--tolerance=" ) ) {
|
95
|
|
- int err = parseInt( arg, &m_tolerance );
|
96
|
|
- if ( err ) {
|
97
|
|
- return 1;
|
98
|
|
- }
|
99
|
|
- if ( m_tolerance < 0 ) {
|
100
|
|
- m_tolerance = 0;
|
101
|
|
- }
|
102
|
|
- } else if ( matches( arg, "-g=", "--gracetime=" ) ) {
|
103
|
|
- int err = parseFloat( arg, &m_gracetime );
|
104
|
|
- if ( err ) {
|
105
|
|
- return 1;
|
106
|
|
- }
|
107
|
|
- if ( m_gracetime < 0 ) {
|
108
|
|
- m_gracetime = 0;
|
109
|
|
- }
|
110
|
|
- } else if ( matches( arg, "-x=", "--xdisplay=" ) ) {
|
111
|
|
- int err = parseString( arg, &m_xdisplay );
|
112
|
|
- if ( err ) {
|
113
|
|
- return 1;
|
114
|
|
- }
|
115
|
|
- } else if ( matches( arg, "-nkb", "--nokeyboard" ) ) {
|
116
|
|
- m_keyboard = false;
|
117
|
|
- } else if ( matches( arg, "-nd", "--nodecorations" ) ) {
|
118
|
|
- m_decorations = false;
|
119
|
|
- } else if ( matches( arg, "-hi", "--highlight" ) ) {
|
120
|
|
- m_highlight = true;
|
121
|
|
- } else if ( matches( arg, "-h", "--help" ) ) {
|
122
|
|
- printHelp();
|
123
|
|
- return 2;
|
124
|
|
- } else if ( matches( arg, "-v", "--version" ) ) {
|
125
|
|
- printf( "slop %s\n", m_version.c_str() );
|
126
|
|
- return 2;
|
127
|
|
- } else {
|
128
|
|
- if ( i == 0 ) {
|
129
|
|
- continue;
|
130
|
|
- }
|
131
|
|
- fprintf( stderr, "Error: Unknown argument %s\n", argv[i] );
|
132
|
|
- fprintf( stderr, "Try -h or --help for help.\n" );
|
133
|
|
- return 1;
|
134
|
|
- }
|
135
|
|
- }
|
136
|
|
- if ( m_minimumsize > m_maximumsize && m_maximumsize > 0 ) {
|
137
|
|
- fprintf( stderr, "Error: minimumsize is greater than maximumsize.\n" );
|
138
|
|
- return 1;
|
139
|
|
- }
|
140
|
|
- return 0;
|
141
|
|
-}
|
142
|
|
-
|
143
|
|
-int slop::Options::parseInt( std::string arg, int* returnInt ) {
|
144
|
|
- std::string copy = arg;
|
145
|
|
- int find = copy.find( "=" );
|
146
|
|
- if ( find != copy.npos ) {
|
147
|
|
- copy.at( find ) = ' ';
|
148
|
|
- }
|
149
|
|
- // Just in case we error out, grab the actual argument name into x.
|
150
|
|
- char* x = new char[ arg.size() ];
|
151
|
|
- int num = sscanf( copy.c_str(), "%s %i", x, returnInt );
|
152
|
|
- if ( num != 2 ) {
|
153
|
|
- fprintf( stderr, "Error parsing command arguments near %s\n", arg.c_str() );
|
154
|
|
- fprintf( stderr, "Usage: %s=INT\n", x );
|
155
|
|
- fprintf( stderr, "Example: %s=10 or %s=-12\n", x, x );
|
156
|
|
- fprintf( stderr, "Try -h or --help for help.\n" );
|
157
|
|
- delete[] x;
|
158
|
|
- return 1;
|
159
|
|
- }
|
160
|
|
- delete[] x;
|
161
|
|
- return 0;
|
162
|
|
-}
|
163
|
|
-
|
164
|
|
-int slop::Options::parseFloat( std::string arg, float* returnFloat ) {
|
165
|
|
- std::string copy = arg;
|
166
|
|
- int find = copy.find( "=" );
|
167
|
|
- if ( find != copy.npos ) {
|
168
|
|
- copy.at( find ) = ' ';
|
169
|
|
- }
|
170
|
|
- // Just in case we error out, grab the actual argument name into x.
|
171
|
|
- char* x = new char[ arg.size() ];
|
172
|
|
- int num = sscanf( copy.c_str(), "%s %f", x, returnFloat );
|
173
|
|
- if ( num != 2 ) {
|
174
|
|
- fprintf( stderr, "Error parsing command arguments near %s\n", arg.c_str() );
|
175
|
|
- fprintf( stderr, "Usage: %s=FLOAT\n", x );
|
176
|
|
- fprintf( stderr, "Example: %s=3.14 or %s=-99\n", x, x );
|
177
|
|
- fprintf( stderr, "Try -h or --help for help.\n" );
|
178
|
|
- delete[] x;
|
179
|
|
- return 1;
|
180
|
|
- }
|
181
|
|
- delete[] x;
|
182
|
|
- return 0;
|
183
|
|
-}
|
184
|
|
-
|
185
|
|
-bool slop::Options::matches( std::string arg, std::string shorthand, std::string longhand ) {
|
186
|
|
- if ( arg.substr( 0, shorthand.size() ) == shorthand ) {
|
187
|
|
- if ( arg == shorthand || shorthand[shorthand.length()-1] == '=' ) {
|
188
|
|
- return true;
|
189
|
|
- }
|
190
|
|
- }
|
191
|
|
- if ( longhand.size() && arg.substr( 0, longhand.size() ) == longhand ) {
|
192
|
|
- if ( arg == longhand || longhand[longhand.length()-1] == '=' ) {
|
193
|
|
- return true;
|
194
|
|
- }
|
195
|
|
- }
|
196
|
|
- return false;
|
197
|
|
-}
|
198
|
|
-
|
199
|
|
-int slop::Options::parseString( std::string arg, std::string* returnString ) {
|
200
|
|
- std::string copy = arg;
|
201
|
|
- int find = copy.find( "=" );
|
202
|
|
- if ( find != copy.npos ) {
|
203
|
|
- copy.at( find ) = ' ';
|
204
|
|
- }
|
205
|
|
- // Just in case we error out, grab the actual argument name into x.
|
206
|
|
- char* x = new char[ arg.size() ];
|
207
|
|
- char* y = new char[ arg.size() ];
|
208
|
|
- int num = sscanf( copy.c_str(), "%s %s", x, y );
|
209
|
|
- if ( num != 2 ) {
|
210
|
|
- fprintf( stderr, "Error parsing command arguments near %s\n", arg.c_str() );
|
211
|
|
- fprintf( stderr, "Usage: %s=STRING\n", x );
|
212
|
|
- fprintf( stderr, "Example: %s=:0 or %s=hostname:0.1\n", x, x );
|
213
|
|
- fprintf( stderr, "Try -h or --help for help.\n" );
|
214
|
|
- delete[] x;
|
215
|
|
- delete[] y;
|
216
|
|
- return 1;
|
217
|
|
- }
|
218
|
|
- *returnString = y;
|
219
|
|
- delete[] x;
|
220
|
|
- delete[] y;
|
221
|
|
- return 0;
|
222
|
|
-}
|
223
|
|
-
|
224
|
|
-int slop::Options::parseColor( std::string arg, float* r, float* g, float* b, float* a ) {
|
225
|
|
- std::string copy = arg;
|
226
|
|
- int find = copy.find( "=" );
|
227
|
|
- while( find != copy.npos ) {
|
228
|
|
- copy.at( find ) = ' ';
|
229
|
|
- find = copy.find( "," );
|
230
|
|
- }
|
231
|
|
-
|
232
|
|
- // Just in case we error out, grab the actual argument name into x.
|
233
|
|
- char* x = new char[ arg.size() ];
|
234
|
|
- // Just in case we didn't include an alpha value
|
235
|
|
- *a = 1;
|
236
|
|
- int num = sscanf( copy.c_str(), "%s %f %f %f %f", x, r, g, b, a );
|
237
|
|
- if ( num != 4 && num != 5 ) {
|
238
|
|
- fprintf( stderr, "Error parsing command arguments near %s\n", arg.c_str() );
|
239
|
|
- fprintf( stderr, "Usage: %s=COLOR\n", x );
|
240
|
|
- fprintf( stderr, "Example: %s=0,0,0 or %s=0.7,0.2,1,0.5 or %s=1,1,1,0.8\n", x, x, x );
|
241
|
|
- fprintf( stderr, "Try -h or --help for help.\n" );
|
242
|
|
- delete[] x;
|
243
|
|
- return 1;
|
244
|
|
- }
|
245
|
|
- delete[] x;
|
246
|
|
- return 0;
|
247
|
|
-}
|
248
|
|
-
|
249
|
|
-int slop::Options::parseGeometry( std::string arg, int* x, int* y, int* w, int* h ) {
|
250
|
|
- std::string copy = arg;
|
251
|
|
- // Replace the first =, all x's and +'s with spaces.
|
252
|
|
- int find = copy.find( "=" );
|
253
|
|
- while( find != copy.npos ) {
|
254
|
|
- copy.at( find ) = ' ';
|
255
|
|
- find = copy.find( "x" );
|
256
|
|
- }
|
257
|
|
- find = copy.find( "+" );
|
258
|
|
- while( find != copy.npos ) {
|
259
|
|
- copy.at( find ) = ' ';
|
260
|
|
- find = copy.find( "+" );
|
261
|
|
- }
|
262
|
|
-
|
263
|
|
- // Just in case we error out, grab the actual argument name into x.
|
264
|
|
- char* foo = new char[ arg.size() ];
|
265
|
|
- int num = sscanf( copy.c_str(), "%s %d %d %d %d", foo, w, h, x, y );
|
266
|
|
- if ( num != 5 ) {
|
267
|
|
- fprintf( stderr, "Error parsing command arguments near %s\n", arg.c_str() );
|
268
|
|
- fprintf( stderr, "Usage: %s=GEOMETRY\n", foo );
|
269
|
|
- fprintf( stderr, "Example: %s=1920x1080+0+0 or %s=256x256+100+-200\n", foo, foo );
|
270
|
|
- fprintf( stderr, "Try -h or --help for help.\n" );
|
271
|
|
- delete[] foo;
|
272
|
|
- return 1;
|
273
|
|
- }
|
274
|
|
- delete[] foo;
|
275
|
|
- return 0;
|
276
|
|
-}
|