Преглед на файлове

Slop now uses more unix-like argument parsing. All thanks to lovely gengetopt.

Dalton Nell преди 10 години
родител
ревизия
9f7a1c0faf
променени са 7 файла, в които са добавени 1111 реда и са изтрити 49 реда
  1. 47
    23
      README.md
  2. 734
    0
      cmdline.c
  3. 214
    0
      cmdline.h
  4. 1
    1
      license.txt
  5. 38
    23
      main.cpp
  6. 2
    2
      makefile
  7. 75
    0
      options.ggo

+ 47
- 23
README.md Целия файл

@@ -51,39 +51,63 @@ While slop not only looks nicer, it's impossible for it to end up in screenshots
51 51
 help
52 52
 ----
53 53
 ```text
54
+slop v3.1.3
55
+
56
+Copyright (C) 2014 Dalton Nell, GPLv3
57
+
54 58
 Usage: slop [options]
55
-Print user selected region to stdout. Pressing keys or right-clicking cancels selection.
56 59
 
60
+slop (Select Operation) is an application that queries for a selection from the
61
+user and prints the region to stdout.
62
+
63
+  -h, --help                    Print help and exit
64
+  -V, --version                 Print version and exit
57 65
 Options
58
-    -h, --help                     Show this message.
59
-    -nkb, --nokeyboard             Disables the ability to cancel selections with the keyboard.
60
-    -b=INT, --bordersize=INT       Set selection rectangle border size.
61
-    -p=INT, --padding=INT          Set padding size for selection.
62
-    -t=INT, --tolerance=INT        How far in pixels the mouse can move after clicking and still be detected
63
-                                   as a normal click. Setting to zero will disable window selections.
64
-    -x=STRING, --xdisplay=STRING   Set x display (STRING must be hostname:number.screen_number format)
65
-    -c=COLOR, --color=COLOR        Set selection rectangle color, COLOR is in format FLOAT,FLOAT,FLOAT,FLOAT.
66
-                                   takes RGBA or RGB.
67
-    -g=FLOAT, --gracetime=FLOAT    Set the amount of time before slop will check for keyboard cancellations
68
-                                   in seconds.
69
-    -nd, --nodecorations           Attempts to remove decorations from window selections.
70
-    -min=INT, --minimumsize=INT    Sets the minimum output of width or height values, useful to avoid outputting 0
71
-                                   widths or heights.
72
-    -max=INT, --maximumsize=INT    Sets the maximum output of width or height values.
73
-    -hi, --highlight               Instead of outlining the selection, slop highlights it. Only useful when
74
-                                   used with a --color with an alpha under 1.
75
-    -v, --version                  prints version.
66
+      --xdisplay=hostname:number.screen_number
67
+                                Sets the x display.  (default=`:0')
68
+      --nokeyboard              Disables the ability to cancel selections with
69
+                                  the keyboard.  (default=off)
70
+  -b, --bordersize=INT          Set the selection rectangle's thickness. Does
71
+                                  nothing when --highlight is enabled.
72
+                                  (default=`5')
73
+  -p, --padding=INT             Set the padding size of the selection. Can be
74
+                                  negative.  (default=`0')
75
+  -t, --tolerance=INT           How far in pixels the mouse can move after
76
+                                  clicking and still be detected as a normal
77
+                                  click instead of a click and drag. Setting
78
+                                  this to 0 will disable window selections.
79
+                                  (default=`2')
80
+  -g, --gracetime=FLOAT         Set the amount of time before slop will check
81
+                                  for keyboard cancellations in seconds.
82
+                                  (default=`0.4')
83
+  -c, --color=FLOAT,FLOAT,FLOAT,FLOAT
84
+                                Set the selection rectangle's color. Supports
85
+                                  RGB or RGBA values.
86
+                                  (default=`0.5,0.5,0.5,1')
87
+  -n, --nodecorations           Attempt to select child windows in order to
88
+                                  avoid window decorations.  (default=off)
89
+      --min=INT                 Set the minimum output of width or height
90
+                                  values. This is useful to avoid outputting 0.
91
+                                  Setting min and max to the same value
92
+                                  disables drag selections.  (default=`0')
93
+      --max=INT                 Set the maximum output of width or height
94
+                                  values. Setting min and max to the same value
95
+                                  disables drag selections.  (default=`0')
96
+  -l, --highlight               Instead of outlining selections, slop
97
+                                  highlights it. This is only useful when
98
+                                  --color is set to a transparent color.
99
+                                  (default=off)
76 100
 
77 101
 Examples
78 102
     $ # Gray, thick, transparent border for maximum visiblity.
79
-    $ slop -b=20 -c=0.5,0.5,0.5,0.8
103
+    $ slop -b 20 -c 0.5,0.5,0.5,0.8
80 104
 
81 105
     $ # Remove window decorations.
82
-    $ slop -nd
106
+    $ slop --nodecorations
83 107
 
84 108
     $ # Disable window selections. Useful for selecting individual pixels.
85
-    $ slop -t=0
109
+    $ slop -t 0
86 110
 
87 111
     $ # Classic Windows XP selection.
88
-    $ slop -hi -c=0.2,0.4,0.5,0.4
112
+    $ slop -l -c 0.3,0.4,0.6,0.4
89 113
 ```

+ 734
- 0
cmdline.c Целия файл

@@ -0,0 +1,734 @@
1
+/*
2
+  File autogenerated by gengetopt version 2.22.6
3
+  generated with the following command:
4
+  gengetopt 
5
+
6
+  The developers of gengetopt consider the fixed text that goes in all
7
+  gengetopt output files to be in the public domain:
8
+  we make no copyright claims on it.
9
+*/
10
+
11
+/* If we use autoconf.  */
12
+#ifdef HAVE_CONFIG_H
13
+#include "config.h"
14
+#endif
15
+
16
+#include <stdio.h>
17
+#include <stdlib.h>
18
+#include <string.h>
19
+
20
+#ifndef FIX_UNUSED
21
+#define FIX_UNUSED(X) (void) (X) /* avoid warnings for unused params */
22
+#endif
23
+
24
+#include <getopt.h>
25
+
26
+#include "cmdline.h"
27
+
28
+const char *gengetopt_args_info_purpose = "";
29
+
30
+const char *gengetopt_args_info_usage = "Usage: slop [options]";
31
+
32
+const char *gengetopt_args_info_versiontext = "Copyright (C) 2014 Dalton Nell, GPLv3";
33
+
34
+const char *gengetopt_args_info_description = "slop (Select Operation) is an application that queries for a selection from the\nuser and prints the region to stdout.";
35
+
36
+const char *gengetopt_args_info_help[] = {
37
+  "  -h, --help                    Print help and exit",
38
+  "  -V, --version                 Print version and exit",
39
+  "Options",
40
+  "      --xdisplay=hostname:number.screen_number\n                                Sets the x display.  (default=`:0')",
41
+  "      --nokeyboard              Disables the ability to cancel selections with\n                                  the keyboard.  (default=off)",
42
+  "  -b, --bordersize=INT          Set the selection rectangle's thickness. Does\n                                  nothing when --highlight is enabled.\n                                  (default=`5')",
43
+  "  -p, --padding=INT             Set the padding size of the selection. Can be\n                                  negative.  (default=`0')",
44
+  "  -t, --tolerance=INT           How far in pixels the mouse can move after\n                                  clicking and still be detected as a normal\n                                  click instead of a click and drag. Setting\n                                  this to 0 will disable window selections.\n                                  (default=`2')",
45
+  "  -g, --gracetime=FLOAT         Set the amount of time before slop will check\n                                  for keyboard cancellations in seconds.\n                                  (default=`0.4')",
46
+  "  -c, --color=FLOAT,FLOAT,FLOAT,FLOAT\n                                Set the selection rectangle's color. Supports\n                                  RGB or RGBA values.\n                                  (default=`0.5,0.5,0.5,1')",
47
+  "  -n, --nodecorations           Attempt to select child windows in order to\n                                  avoid window decorations.  (default=off)",
48
+  "      --min=INT                 Set the minimum output of width or height\n                                  values. This is useful to avoid outputting 0.\n                                  Setting min and max to the same value\n                                  disables drag selections.  (default=`0')",
49
+  "      --max=INT                 Set the maximum output of width or height\n                                  values. Setting min and max to the same value\n                                  disables drag selections.  (default=`0')",
50
+  "  -l, --highlight               Instead of outlining selections, slop\n                                  highlights it. This is only useful when\n                                  --color is set to a transparent color.\n                                  (default=off)",
51
+  "\nExamples\n    $ # Gray, thick, transparent border for maximum visiblity.\n    $ slop -b 20 -c 0.5,0.5,0.5,0.8\n\n    $ # Remove window decorations.\n    $ slop --nodecorations\n\n    $ # Disable window selections. Useful for selecting individual pixels.\n    $ slop -t 0\n\n    $ # Classic Windows XP selection.\n    $ slop -l -c 0.3,0.4,0.6,0.4\n",
52
+    0
53
+};
54
+
55
+typedef enum {ARG_NO
56
+  , ARG_FLAG
57
+  , ARG_STRING
58
+  , ARG_INT
59
+} cmdline_parser_arg_type;
60
+
61
+static
62
+void clear_given (struct gengetopt_args_info *args_info);
63
+static
64
+void clear_args (struct gengetopt_args_info *args_info);
65
+
66
+static int
67
+cmdline_parser_internal (int argc, char **argv, struct gengetopt_args_info *args_info,
68
+                        struct cmdline_parser_params *params, const char *additional_error);
69
+
70
+
71
+static char *
72
+gengetopt_strdup (const char *s);
73
+
74
+static
75
+void clear_given (struct gengetopt_args_info *args_info)
76
+{
77
+  args_info->help_given = 0 ;
78
+  args_info->version_given = 0 ;
79
+  args_info->xdisplay_given = 0 ;
80
+  args_info->nokeyboard_given = 0 ;
81
+  args_info->bordersize_given = 0 ;
82
+  args_info->padding_given = 0 ;
83
+  args_info->tolerance_given = 0 ;
84
+  args_info->gracetime_given = 0 ;
85
+  args_info->color_given = 0 ;
86
+  args_info->nodecorations_given = 0 ;
87
+  args_info->min_given = 0 ;
88
+  args_info->max_given = 0 ;
89
+  args_info->highlight_given = 0 ;
90
+}
91
+
92
+static
93
+void clear_args (struct gengetopt_args_info *args_info)
94
+{
95
+  FIX_UNUSED (args_info);
96
+  args_info->xdisplay_arg = gengetopt_strdup (":0");
97
+  args_info->xdisplay_orig = NULL;
98
+  args_info->nokeyboard_flag = 0;
99
+  args_info->bordersize_arg = 5;
100
+  args_info->bordersize_orig = NULL;
101
+  args_info->padding_arg = 0;
102
+  args_info->padding_orig = NULL;
103
+  args_info->tolerance_arg = 2;
104
+  args_info->tolerance_orig = NULL;
105
+  args_info->gracetime_arg = gengetopt_strdup ("0.4");
106
+  args_info->gracetime_orig = NULL;
107
+  args_info->color_arg = gengetopt_strdup ("0.5,0.5,0.5,1");
108
+  args_info->color_orig = NULL;
109
+  args_info->nodecorations_flag = 0;
110
+  args_info->min_arg = 0;
111
+  args_info->min_orig = NULL;
112
+  args_info->max_arg = 0;
113
+  args_info->max_orig = NULL;
114
+  args_info->highlight_flag = 0;
115
+  
116
+}
117
+
118
+static
119
+void init_args_info(struct gengetopt_args_info *args_info)
120
+{
121
+
122
+
123
+  args_info->help_help = gengetopt_args_info_help[0] ;
124
+  args_info->version_help = gengetopt_args_info_help[1] ;
125
+  args_info->xdisplay_help = gengetopt_args_info_help[3] ;
126
+  args_info->nokeyboard_help = gengetopt_args_info_help[4] ;
127
+  args_info->bordersize_help = gengetopt_args_info_help[5] ;
128
+  args_info->padding_help = gengetopt_args_info_help[6] ;
129
+  args_info->tolerance_help = gengetopt_args_info_help[7] ;
130
+  args_info->gracetime_help = gengetopt_args_info_help[8] ;
131
+  args_info->color_help = gengetopt_args_info_help[9] ;
132
+  args_info->nodecorations_help = gengetopt_args_info_help[10] ;
133
+  args_info->min_help = gengetopt_args_info_help[11] ;
134
+  args_info->max_help = gengetopt_args_info_help[12] ;
135
+  args_info->highlight_help = gengetopt_args_info_help[13] ;
136
+  
137
+}
138
+
139
+void
140
+cmdline_parser_print_version (void)
141
+{
142
+  printf ("%s %s\n",
143
+     (strlen(CMDLINE_PARSER_PACKAGE_NAME) ? CMDLINE_PARSER_PACKAGE_NAME : CMDLINE_PARSER_PACKAGE),
144
+     CMDLINE_PARSER_VERSION);
145
+
146
+  if (strlen(gengetopt_args_info_versiontext) > 0)
147
+    printf("\n%s\n", gengetopt_args_info_versiontext);
148
+}
149
+
150
+static void print_help_common(void) {
151
+  cmdline_parser_print_version ();
152
+
153
+  if (strlen(gengetopt_args_info_purpose) > 0)
154
+    printf("\n%s\n", gengetopt_args_info_purpose);
155
+
156
+  if (strlen(gengetopt_args_info_usage) > 0)
157
+    printf("\n%s\n", gengetopt_args_info_usage);
158
+
159
+  printf("\n");
160
+
161
+  if (strlen(gengetopt_args_info_description) > 0)
162
+    printf("%s\n\n", gengetopt_args_info_description);
163
+}
164
+
165
+void
166
+cmdline_parser_print_help (void)
167
+{
168
+  int i = 0;
169
+  print_help_common();
170
+  while (gengetopt_args_info_help[i])
171
+    printf("%s\n", gengetopt_args_info_help[i++]);
172
+}
173
+
174
+void
175
+cmdline_parser_init (struct gengetopt_args_info *args_info)
176
+{
177
+  clear_given (args_info);
178
+  clear_args (args_info);
179
+  init_args_info (args_info);
180
+}
181
+
182
+void
183
+cmdline_parser_params_init(struct cmdline_parser_params *params)
184
+{
185
+  if (params)
186
+    { 
187
+      params->override = 0;
188
+      params->initialize = 1;
189
+      params->check_required = 1;
190
+      params->check_ambiguity = 0;
191
+      params->print_errors = 1;
192
+    }
193
+}
194
+
195
+struct cmdline_parser_params *
196
+cmdline_parser_params_create(void)
197
+{
198
+  struct cmdline_parser_params *params = 
199
+    (struct cmdline_parser_params *)malloc(sizeof(struct cmdline_parser_params));
200
+  cmdline_parser_params_init(params);  
201
+  return params;
202
+}
203
+
204
+static void
205
+free_string_field (char **s)
206
+{
207
+  if (*s)
208
+    {
209
+      free (*s);
210
+      *s = 0;
211
+    }
212
+}
213
+
214
+
215
+static void
216
+cmdline_parser_release (struct gengetopt_args_info *args_info)
217
+{
218
+
219
+  free_string_field (&(args_info->xdisplay_arg));
220
+  free_string_field (&(args_info->xdisplay_orig));
221
+  free_string_field (&(args_info->bordersize_orig));
222
+  free_string_field (&(args_info->padding_orig));
223
+  free_string_field (&(args_info->tolerance_orig));
224
+  free_string_field (&(args_info->gracetime_arg));
225
+  free_string_field (&(args_info->gracetime_orig));
226
+  free_string_field (&(args_info->color_arg));
227
+  free_string_field (&(args_info->color_orig));
228
+  free_string_field (&(args_info->min_orig));
229
+  free_string_field (&(args_info->max_orig));
230
+  
231
+  
232
+
233
+  clear_given (args_info);
234
+}
235
+
236
+
237
+static void
238
+write_into_file(FILE *outfile, const char *opt, const char *arg, const char *values[])
239
+{
240
+  FIX_UNUSED (values);
241
+  if (arg) {
242
+    fprintf(outfile, "%s=\"%s\"\n", opt, arg);
243
+  } else {
244
+    fprintf(outfile, "%s\n", opt);
245
+  }
246
+}
247
+
248
+
249
+int
250
+cmdline_parser_dump(FILE *outfile, struct gengetopt_args_info *args_info)
251
+{
252
+  int i = 0;
253
+
254
+  if (!outfile)
255
+    {
256
+      fprintf (stderr, "%s: cannot dump options to stream\n", CMDLINE_PARSER_PACKAGE);
257
+      return EXIT_FAILURE;
258
+    }
259
+
260
+  if (args_info->help_given)
261
+    write_into_file(outfile, "help", 0, 0 );
262
+  if (args_info->version_given)
263
+    write_into_file(outfile, "version", 0, 0 );
264
+  if (args_info->xdisplay_given)
265
+    write_into_file(outfile, "xdisplay", args_info->xdisplay_orig, 0);
266
+  if (args_info->nokeyboard_given)
267
+    write_into_file(outfile, "nokeyboard", 0, 0 );
268
+  if (args_info->bordersize_given)
269
+    write_into_file(outfile, "bordersize", args_info->bordersize_orig, 0);
270
+  if (args_info->padding_given)
271
+    write_into_file(outfile, "padding", args_info->padding_orig, 0);
272
+  if (args_info->tolerance_given)
273
+    write_into_file(outfile, "tolerance", args_info->tolerance_orig, 0);
274
+  if (args_info->gracetime_given)
275
+    write_into_file(outfile, "gracetime", args_info->gracetime_orig, 0);
276
+  if (args_info->color_given)
277
+    write_into_file(outfile, "color", args_info->color_orig, 0);
278
+  if (args_info->nodecorations_given)
279
+    write_into_file(outfile, "nodecorations", 0, 0 );
280
+  if (args_info->min_given)
281
+    write_into_file(outfile, "min", args_info->min_orig, 0);
282
+  if (args_info->max_given)
283
+    write_into_file(outfile, "max", args_info->max_orig, 0);
284
+  if (args_info->highlight_given)
285
+    write_into_file(outfile, "highlight", 0, 0 );
286
+  
287
+
288
+  i = EXIT_SUCCESS;
289
+  return i;
290
+}
291
+
292
+int
293
+cmdline_parser_file_save(const char *filename, struct gengetopt_args_info *args_info)
294
+{
295
+  FILE *outfile;
296
+  int i = 0;
297
+
298
+  outfile = fopen(filename, "w");
299
+
300
+  if (!outfile)
301
+    {
302
+      fprintf (stderr, "%s: cannot open file for writing: %s\n", CMDLINE_PARSER_PACKAGE, filename);
303
+      return EXIT_FAILURE;
304
+    }
305
+
306
+  i = cmdline_parser_dump(outfile, args_info);
307
+  fclose (outfile);
308
+
309
+  return i;
310
+}
311
+
312
+void
313
+cmdline_parser_free (struct gengetopt_args_info *args_info)
314
+{
315
+  cmdline_parser_release (args_info);
316
+}
317
+
318
+/** @brief replacement of strdup, which is not standard */
319
+char *
320
+gengetopt_strdup (const char *s)
321
+{
322
+  char *result = 0;
323
+  if (!s)
324
+    return result;
325
+
326
+  result = (char*)malloc(strlen(s) + 1);
327
+  if (result == (char*)0)
328
+    return (char*)0;
329
+  strcpy(result, s);
330
+  return result;
331
+}
332
+
333
+int
334
+cmdline_parser (int argc, char **argv, struct gengetopt_args_info *args_info)
335
+{
336
+  return cmdline_parser2 (argc, argv, args_info, 0, 1, 1);
337
+}
338
+
339
+int
340
+cmdline_parser_ext (int argc, char **argv, struct gengetopt_args_info *args_info,
341
+                   struct cmdline_parser_params *params)
342
+{
343
+  int result;
344
+  result = cmdline_parser_internal (argc, argv, args_info, params, 0);
345
+
346
+  if (result == EXIT_FAILURE)
347
+    {
348
+      cmdline_parser_free (args_info);
349
+      exit (EXIT_FAILURE);
350
+    }
351
+  
352
+  return result;
353
+}
354
+
355
+int
356
+cmdline_parser2 (int argc, char **argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required)
357
+{
358
+  int result;
359
+  struct cmdline_parser_params params;
360
+  
361
+  params.override = override;
362
+  params.initialize = initialize;
363
+  params.check_required = check_required;
364
+  params.check_ambiguity = 0;
365
+  params.print_errors = 1;
366
+
367
+  result = cmdline_parser_internal (argc, argv, args_info, &params, 0);
368
+
369
+  if (result == EXIT_FAILURE)
370
+    {
371
+      cmdline_parser_free (args_info);
372
+      exit (EXIT_FAILURE);
373
+    }
374
+  
375
+  return result;
376
+}
377
+
378
+int
379
+cmdline_parser_required (struct gengetopt_args_info *args_info, const char *prog_name)
380
+{
381
+  FIX_UNUSED (args_info);
382
+  FIX_UNUSED (prog_name);
383
+  return EXIT_SUCCESS;
384
+}
385
+
386
+
387
+static char *package_name = 0;
388
+
389
+/**
390
+ * @brief updates an option
391
+ * @param field the generic pointer to the field to update
392
+ * @param orig_field the pointer to the orig field
393
+ * @param field_given the pointer to the number of occurrence of this option
394
+ * @param prev_given the pointer to the number of occurrence already seen
395
+ * @param value the argument for this option (if null no arg was specified)
396
+ * @param possible_values the possible values for this option (if specified)
397
+ * @param default_value the default value (in case the option only accepts fixed values)
398
+ * @param arg_type the type of this option
399
+ * @param check_ambiguity @see cmdline_parser_params.check_ambiguity
400
+ * @param override @see cmdline_parser_params.override
401
+ * @param no_free whether to free a possible previous value
402
+ * @param multiple_option whether this is a multiple option
403
+ * @param long_opt the corresponding long option
404
+ * @param short_opt the corresponding short option (or '-' if none)
405
+ * @param additional_error possible further error specification
406
+ */
407
+static
408
+int update_arg(void *field, char **orig_field,
409
+               unsigned int *field_given, unsigned int *prev_given, 
410
+               char *value, const char *possible_values[],
411
+               const char *default_value,
412
+               cmdline_parser_arg_type arg_type,
413
+               int check_ambiguity, int override,
414
+               int no_free, int multiple_option,
415
+               const char *long_opt, char short_opt,
416
+               const char *additional_error)
417
+{
418
+  char *stop_char = 0;
419
+  const char *val = value;
420
+  int found;
421
+  char **string_field;
422
+  FIX_UNUSED (field);
423
+
424
+  stop_char = 0;
425
+  found = 0;
426
+
427
+  if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given)))
428
+    {
429
+      if (short_opt != '-')
430
+        fprintf (stderr, "%s: `--%s' (`-%c') option given more than once%s\n", 
431
+               package_name, long_opt, short_opt,
432
+               (additional_error ? additional_error : ""));
433
+      else
434
+        fprintf (stderr, "%s: `--%s' option given more than once%s\n", 
435
+               package_name, long_opt,
436
+               (additional_error ? additional_error : ""));
437
+      return 1; /* failure */
438
+    }
439
+
440
+  FIX_UNUSED (default_value);
441
+    
442
+  if (field_given && *field_given && ! override)
443
+    return 0;
444
+  if (prev_given)
445
+    (*prev_given)++;
446
+  if (field_given)
447
+    (*field_given)++;
448
+  if (possible_values)
449
+    val = possible_values[found];
450
+
451
+  switch(arg_type) {
452
+  case ARG_FLAG:
453
+    *((int *)field) = !*((int *)field);
454
+    break;
455
+  case ARG_INT:
456
+    if (val) *((int *)field) = strtol (val, &stop_char, 0);
457
+    break;
458
+  case ARG_STRING:
459
+    if (val) {
460
+      string_field = (char **)field;
461
+      if (!no_free && *string_field)
462
+        free (*string_field); /* free previous string */
463
+      *string_field = gengetopt_strdup (val);
464
+    }
465
+    break;
466
+  default:
467
+    break;
468
+  };
469
+
470
+  /* check numeric conversion */
471
+  switch(arg_type) {
472
+  case ARG_INT:
473
+    if (val && !(stop_char && *stop_char == '\0')) {
474
+      fprintf(stderr, "%s: invalid numeric value: %s\n", package_name, val);
475
+      return 1; /* failure */
476
+    }
477
+    break;
478
+  default:
479
+    ;
480
+  };
481
+
482
+  /* store the original value */
483
+  switch(arg_type) {
484
+  case ARG_NO:
485
+  case ARG_FLAG:
486
+    break;
487
+  default:
488
+    if (value && orig_field) {
489
+      if (no_free) {
490
+        *orig_field = value;
491
+      } else {
492
+        if (*orig_field)
493
+          free (*orig_field); /* free previous string */
494
+        *orig_field = gengetopt_strdup (value);
495
+      }
496
+    }
497
+  };
498
+
499
+  return 0; /* OK */
500
+}
501
+
502
+
503
+int
504
+cmdline_parser_internal (
505
+  int argc, char **argv, struct gengetopt_args_info *args_info,
506
+                        struct cmdline_parser_params *params, const char *additional_error)
507
+{
508
+  int c;	/* Character of the parsed option.  */
509
+
510
+  int error_occurred = 0;
511
+  struct gengetopt_args_info local_args_info;
512
+  
513
+  int override;
514
+  int initialize;
515
+  int check_required;
516
+  int check_ambiguity;
517
+  
518
+  package_name = argv[0];
519
+  
520
+  override = params->override;
521
+  initialize = params->initialize;
522
+  check_required = params->check_required;
523
+  check_ambiguity = params->check_ambiguity;
524
+
525
+  if (initialize)
526
+    cmdline_parser_init (args_info);
527
+
528
+  cmdline_parser_init (&local_args_info);
529
+
530
+  optarg = 0;
531
+  optind = 0;
532
+  opterr = params->print_errors;
533
+  optopt = '?';
534
+
535
+  while (1)
536
+    {
537
+      int option_index = 0;
538
+
539
+      static struct option long_options[] = {
540
+        { "help",	0, NULL, 'h' },
541
+        { "version",	0, NULL, 'V' },
542
+        { "xdisplay",	1, NULL, 0 },
543
+        { "nokeyboard",	0, NULL, 0 },
544
+        { "bordersize",	1, NULL, 'b' },
545
+        { "padding",	1, NULL, 'p' },
546
+        { "tolerance",	1, NULL, 't' },
547
+        { "gracetime",	1, NULL, 'g' },
548
+        { "color",	1, NULL, 'c' },
549
+        { "nodecorations",	0, NULL, 'n' },
550
+        { "min",	1, NULL, 0 },
551
+        { "max",	1, NULL, 0 },
552
+        { "highlight",	0, NULL, 'l' },
553
+        { 0,  0, 0, 0 }
554
+      };
555
+
556
+      c = getopt_long (argc, argv, "hVb:p:t:g:c:nl", long_options, &option_index);
557
+
558
+      if (c == -1) break;	/* Exit from `while (1)' loop.  */
559
+
560
+      switch (c)
561
+        {
562
+        case 'h':	/* Print help and exit.  */
563
+          cmdline_parser_print_help ();
564
+          cmdline_parser_free (&local_args_info);
565
+          exit (EXIT_SUCCESS);
566
+
567
+        case 'V':	/* Print version and exit.  */
568
+          cmdline_parser_print_version ();
569
+          cmdline_parser_free (&local_args_info);
570
+          exit (EXIT_SUCCESS);
571
+
572
+        case 'b':	/* Set the selection rectangle's thickness. Does nothing when --highlight is enabled..  */
573
+        
574
+        
575
+          if (update_arg( (void *)&(args_info->bordersize_arg), 
576
+               &(args_info->bordersize_orig), &(args_info->bordersize_given),
577
+              &(local_args_info.bordersize_given), optarg, 0, "5", ARG_INT,
578
+              check_ambiguity, override, 0, 0,
579
+              "bordersize", 'b',
580
+              additional_error))
581
+            goto failure;
582
+        
583
+          break;
584
+        case 'p':	/* Set the padding size of the selection. Can be negative..  */
585
+        
586
+        
587
+          if (update_arg( (void *)&(args_info->padding_arg), 
588
+               &(args_info->padding_orig), &(args_info->padding_given),
589
+              &(local_args_info.padding_given), optarg, 0, "0", ARG_INT,
590
+              check_ambiguity, override, 0, 0,
591
+              "padding", 'p',
592
+              additional_error))
593
+            goto failure;
594
+        
595
+          break;
596
+        case 't':	/* How far in pixels the mouse can move after clicking and still be detected as a normal click instead of a click and drag. Setting this to 0 will disable window selections..  */
597
+        
598
+        
599
+          if (update_arg( (void *)&(args_info->tolerance_arg), 
600
+               &(args_info->tolerance_orig), &(args_info->tolerance_given),
601
+              &(local_args_info.tolerance_given), optarg, 0, "2", ARG_INT,
602
+              check_ambiguity, override, 0, 0,
603
+              "tolerance", 't',
604
+              additional_error))
605
+            goto failure;
606
+        
607
+          break;
608
+        case 'g':	/* Set the amount of time before slop will check for keyboard cancellations in seconds..  */
609
+        
610
+        
611
+          if (update_arg( (void *)&(args_info->gracetime_arg), 
612
+               &(args_info->gracetime_orig), &(args_info->gracetime_given),
613
+              &(local_args_info.gracetime_given), optarg, 0, "0.4", ARG_STRING,
614
+              check_ambiguity, override, 0, 0,
615
+              "gracetime", 'g',
616
+              additional_error))
617
+            goto failure;
618
+        
619
+          break;
620
+        case 'c':	/* Set the selection rectangle's color. Supports RGB or RGBA values..  */
621
+        
622
+        
623
+          if (update_arg( (void *)&(args_info->color_arg), 
624
+               &(args_info->color_orig), &(args_info->color_given),
625
+              &(local_args_info.color_given), optarg, 0, "0.5,0.5,0.5,1", ARG_STRING,
626
+              check_ambiguity, override, 0, 0,
627
+              "color", 'c',
628
+              additional_error))
629
+            goto failure;
630
+        
631
+          break;
632
+        case 'n':	/* Attempt to select child windows in order to avoid window decorations..  */
633
+        
634
+        
635
+          if (update_arg((void *)&(args_info->nodecorations_flag), 0, &(args_info->nodecorations_given),
636
+              &(local_args_info.nodecorations_given), optarg, 0, 0, ARG_FLAG,
637
+              check_ambiguity, override, 1, 0, "nodecorations", 'n',
638
+              additional_error))
639
+            goto failure;
640
+        
641
+          break;
642
+        case 'l':	/* Instead of outlining selections, slop highlights it. This is only useful when --color is set to a transparent color..  */
643
+        
644
+        
645
+          if (update_arg((void *)&(args_info->highlight_flag), 0, &(args_info->highlight_given),
646
+              &(local_args_info.highlight_given), optarg, 0, 0, ARG_FLAG,
647
+              check_ambiguity, override, 1, 0, "highlight", 'l',
648
+              additional_error))
649
+            goto failure;
650
+        
651
+          break;
652
+
653
+        case 0:	/* Long option with no short option */
654
+          /* Sets the x display..  */
655
+          if (strcmp (long_options[option_index].name, "xdisplay") == 0)
656
+          {
657
+          
658
+          
659
+            if (update_arg( (void *)&(args_info->xdisplay_arg), 
660
+                 &(args_info->xdisplay_orig), &(args_info->xdisplay_given),
661
+                &(local_args_info.xdisplay_given), optarg, 0, ":0", ARG_STRING,
662
+                check_ambiguity, override, 0, 0,
663
+                "xdisplay", '-',
664
+                additional_error))
665
+              goto failure;
666
+          
667
+          }
668
+          /* Disables the ability to cancel selections with the keyboard..  */
669
+          else if (strcmp (long_options[option_index].name, "nokeyboard") == 0)
670
+          {
671
+          
672
+          
673
+            if (update_arg((void *)&(args_info->nokeyboard_flag), 0, &(args_info->nokeyboard_given),
674
+                &(local_args_info.nokeyboard_given), optarg, 0, 0, ARG_FLAG,
675
+                check_ambiguity, override, 1, 0, "nokeyboard", '-',
676
+                additional_error))
677
+              goto failure;
678
+          
679
+          }
680
+          /* Set the minimum output of width or height values. This is useful to avoid outputting 0. Setting min and max to the same value disables drag selections..  */
681
+          else if (strcmp (long_options[option_index].name, "min") == 0)
682
+          {
683
+          
684
+          
685
+            if (update_arg( (void *)&(args_info->min_arg), 
686
+                 &(args_info->min_orig), &(args_info->min_given),
687
+                &(local_args_info.min_given), optarg, 0, "0", ARG_INT,
688
+                check_ambiguity, override, 0, 0,
689
+                "min", '-',
690
+                additional_error))
691
+              goto failure;
692
+          
693
+          }
694
+          /* Set the maximum output of width or height values. Setting min and max to the same value disables drag selections..  */
695
+          else if (strcmp (long_options[option_index].name, "max") == 0)
696
+          {
697
+          
698
+          
699
+            if (update_arg( (void *)&(args_info->max_arg), 
700
+                 &(args_info->max_orig), &(args_info->max_given),
701
+                &(local_args_info.max_given), optarg, 0, "0", ARG_INT,
702
+                check_ambiguity, override, 0, 0,
703
+                "max", '-',
704
+                additional_error))
705
+              goto failure;
706
+          
707
+          }
708
+          
709
+          break;
710
+        case '?':	/* Invalid option.  */
711
+          /* `getopt_long' already printed an error message.  */
712
+          goto failure;
713
+
714
+        default:	/* bug: option not considered.  */
715
+          fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : ""));
716
+          abort ();
717
+        } /* switch */
718
+    } /* while */
719
+
720
+
721
+
722
+
723
+  cmdline_parser_release (&local_args_info);
724
+
725
+  if ( error_occurred )
726
+    return (EXIT_FAILURE);
727
+
728
+  return 0;
729
+
730
+failure:
731
+  
732
+  cmdline_parser_release (&local_args_info);
733
+  return (EXIT_FAILURE);
734
+}

+ 214
- 0
cmdline.h Целия файл

@@ -0,0 +1,214 @@
1
+/** @file cmdline.h
2
+ *  @brief The header file for the command line option parser
3
+ *  generated by GNU Gengetopt version 2.22.6
4
+ *  http://www.gnu.org/software/gengetopt.
5
+ *  DO NOT modify this file, since it can be overwritten
6
+ *  @author GNU Gengetopt by Lorenzo Bettini */
7
+
8
+#ifndef CMDLINE_H
9
+#define CMDLINE_H
10
+
11
+/* If we use autoconf.  */
12
+#ifdef HAVE_CONFIG_H
13
+#include "config.h"
14
+#endif
15
+
16
+#include <stdio.h> /* for FILE */
17
+
18
+#ifdef __cplusplus
19
+extern "C" {
20
+#endif /* __cplusplus */
21
+
22
+#ifndef CMDLINE_PARSER_PACKAGE
23
+/** @brief the program name (used for printing errors) */
24
+#define CMDLINE_PARSER_PACKAGE "slop"
25
+#endif
26
+
27
+#ifndef CMDLINE_PARSER_PACKAGE_NAME
28
+/** @brief the complete program name (used for help and version) */
29
+#define CMDLINE_PARSER_PACKAGE_NAME "slop"
30
+#endif
31
+
32
+#ifndef CMDLINE_PARSER_VERSION
33
+/** @brief the program version */
34
+#define CMDLINE_PARSER_VERSION "v3.1.3"
35
+#endif
36
+
37
+/** @brief Where the command line options are stored */
38
+struct gengetopt_args_info
39
+{
40
+  const char *help_help; /**< @brief Print help and exit help description.  */
41
+  const char *version_help; /**< @brief Print version and exit help description.  */
42
+  char * xdisplay_arg;	/**< @brief Sets the x display. (default=':0').  */
43
+  char * xdisplay_orig;	/**< @brief Sets the x display. original value given at command line.  */
44
+  const char *xdisplay_help; /**< @brief Sets the x display. help description.  */
45
+  int nokeyboard_flag;	/**< @brief Disables the ability to cancel selections with the keyboard. (default=off).  */
46
+  const char *nokeyboard_help; /**< @brief Disables the ability to cancel selections with the keyboard. help description.  */
47
+  int bordersize_arg;	/**< @brief Set the selection rectangle's thickness. Does nothing when --highlight is enabled. (default='5').  */
48
+  char * bordersize_orig;	/**< @brief Set the selection rectangle's thickness. Does nothing when --highlight is enabled. original value given at command line.  */
49
+  const char *bordersize_help; /**< @brief Set the selection rectangle's thickness. Does nothing when --highlight is enabled. help description.  */
50
+  int padding_arg;	/**< @brief Set the padding size of the selection. Can be negative. (default='0').  */
51
+  char * padding_orig;	/**< @brief Set the padding size of the selection. Can be negative. original value given at command line.  */
52
+  const char *padding_help; /**< @brief Set the padding size of the selection. Can be negative. help description.  */
53
+  int tolerance_arg;	/**< @brief How far in pixels the mouse can move after clicking and still be detected as a normal click instead of a click and drag. Setting this to 0 will disable window selections. (default='2').  */
54
+  char * tolerance_orig;	/**< @brief How far in pixels the mouse can move after clicking and still be detected as a normal click instead of a click and drag. Setting this to 0 will disable window selections. original value given at command line.  */
55
+  const char *tolerance_help; /**< @brief How far in pixels the mouse can move after clicking and still be detected as a normal click instead of a click and drag. Setting this to 0 will disable window selections. help description.  */
56
+  char * gracetime_arg;	/**< @brief Set the amount of time before slop will check for keyboard cancellations in seconds. (default='0.4').  */
57
+  char * gracetime_orig;	/**< @brief Set the amount of time before slop will check for keyboard cancellations in seconds. original value given at command line.  */
58
+  const char *gracetime_help; /**< @brief Set the amount of time before slop will check for keyboard cancellations in seconds. help description.  */
59
+  char * color_arg;	/**< @brief Set the selection rectangle's color. Supports RGB or RGBA values. (default='0.5,0.5,0.5,1').  */
60
+  char * color_orig;	/**< @brief Set the selection rectangle's color. Supports RGB or RGBA values. original value given at command line.  */
61
+  const char *color_help; /**< @brief Set the selection rectangle's color. Supports RGB or RGBA values. help description.  */
62
+  int nodecorations_flag;	/**< @brief Attempt to select child windows in order to avoid window decorations. (default=off).  */
63
+  const char *nodecorations_help; /**< @brief Attempt to select child windows in order to avoid window decorations. help description.  */
64
+  int min_arg;	/**< @brief Set the minimum output of width or height values. This is useful to avoid outputting 0. Setting min and max to the same value disables drag selections. (default='0').  */
65
+  char * min_orig;	/**< @brief Set the minimum output of width or height values. This is useful to avoid outputting 0. Setting min and max to the same value disables drag selections. original value given at command line.  */
66
+  const char *min_help; /**< @brief Set the minimum output of width or height values. This is useful to avoid outputting 0. Setting min and max to the same value disables drag selections. help description.  */
67
+  int max_arg;	/**< @brief Set the maximum output of width or height values. Setting min and max to the same value disables drag selections. (default='0').  */
68
+  char * max_orig;	/**< @brief Set the maximum output of width or height values. Setting min and max to the same value disables drag selections. original value given at command line.  */
69
+  const char *max_help; /**< @brief Set the maximum output of width or height values. Setting min and max to the same value disables drag selections. help description.  */
70
+  int highlight_flag;	/**< @brief Instead of outlining selections, slop highlights it. This is only useful when --color is set to a transparent color. (default=off).  */
71
+  const char *highlight_help; /**< @brief Instead of outlining selections, slop highlights it. This is only useful when --color is set to a transparent color. help description.  */
72
+  
73
+  unsigned int help_given ;	/**< @brief Whether help was given.  */
74
+  unsigned int version_given ;	/**< @brief Whether version was given.  */
75
+  unsigned int xdisplay_given ;	/**< @brief Whether xdisplay was given.  */
76
+  unsigned int nokeyboard_given ;	/**< @brief Whether nokeyboard was given.  */
77
+  unsigned int bordersize_given ;	/**< @brief Whether bordersize was given.  */
78
+  unsigned int padding_given ;	/**< @brief Whether padding was given.  */
79
+  unsigned int tolerance_given ;	/**< @brief Whether tolerance was given.  */
80
+  unsigned int gracetime_given ;	/**< @brief Whether gracetime was given.  */
81
+  unsigned int color_given ;	/**< @brief Whether color was given.  */
82
+  unsigned int nodecorations_given ;	/**< @brief Whether nodecorations was given.  */
83
+  unsigned int min_given ;	/**< @brief Whether min was given.  */
84
+  unsigned int max_given ;	/**< @brief Whether max was given.  */
85
+  unsigned int highlight_given ;	/**< @brief Whether highlight was given.  */
86
+
87
+} ;
88
+
89
+/** @brief The additional parameters to pass to parser functions */
90
+struct cmdline_parser_params
91
+{
92
+  int override; /**< @brief whether to override possibly already present options (default 0) */
93
+  int initialize; /**< @brief whether to initialize the option structure gengetopt_args_info (default 1) */
94
+  int check_required; /**< @brief whether to check that all required options were provided (default 1) */
95
+  int check_ambiguity; /**< @brief whether to check for options already specified in the option structure gengetopt_args_info (default 0) */
96
+  int print_errors; /**< @brief whether getopt_long should print an error message for a bad option (default 1) */
97
+} ;
98
+
99
+/** @brief the purpose string of the program */
100
+extern const char *gengetopt_args_info_purpose;
101
+/** @brief the usage string of the program */
102
+extern const char *gengetopt_args_info_usage;
103
+/** @brief the description string of the program */
104
+extern const char *gengetopt_args_info_description;
105
+/** @brief all the lines making the help output */
106
+extern const char *gengetopt_args_info_help[];
107
+
108
+/**
109
+ * The command line parser
110
+ * @param argc the number of command line options
111
+ * @param argv the command line options
112
+ * @param args_info the structure where option information will be stored
113
+ * @return 0 if everything went fine, NON 0 if an error took place
114
+ */
115
+int cmdline_parser (int argc, char **argv,
116
+  struct gengetopt_args_info *args_info);
117
+
118
+/**
119
+ * The command line parser (version with additional parameters - deprecated)
120
+ * @param argc the number of command line options
121
+ * @param argv the command line options
122
+ * @param args_info the structure where option information will be stored
123
+ * @param override whether to override possibly already present options
124
+ * @param initialize whether to initialize the option structure my_args_info
125
+ * @param check_required whether to check that all required options were provided
126
+ * @return 0 if everything went fine, NON 0 if an error took place
127
+ * @deprecated use cmdline_parser_ext() instead
128
+ */
129
+int cmdline_parser2 (int argc, char **argv,
130
+  struct gengetopt_args_info *args_info,
131
+  int override, int initialize, int check_required);
132
+
133
+/**
134
+ * The command line parser (version with additional parameters)
135
+ * @param argc the number of command line options
136
+ * @param argv the command line options
137
+ * @param args_info the structure where option information will be stored
138
+ * @param params additional parameters for the parser
139
+ * @return 0 if everything went fine, NON 0 if an error took place
140
+ */
141
+int cmdline_parser_ext (int argc, char **argv,
142
+  struct gengetopt_args_info *args_info,
143
+  struct cmdline_parser_params *params);
144
+
145
+/**
146
+ * Save the contents of the option struct into an already open FILE stream.
147
+ * @param outfile the stream where to dump options
148
+ * @param args_info the option struct to dump
149
+ * @return 0 if everything went fine, NON 0 if an error took place
150
+ */
151
+int cmdline_parser_dump(FILE *outfile,
152
+  struct gengetopt_args_info *args_info);
153
+
154
+/**
155
+ * Save the contents of the option struct into a (text) file.
156
+ * This file can be read by the config file parser (if generated by gengetopt)
157
+ * @param filename the file where to save
158
+ * @param args_info the option struct to save
159
+ * @return 0 if everything went fine, NON 0 if an error took place
160
+ */
161
+int cmdline_parser_file_save(const char *filename,
162
+  struct gengetopt_args_info *args_info);
163
+
164
+/**
165
+ * Print the help
166
+ */
167
+void cmdline_parser_print_help(void);
168
+/**
169
+ * Print the version
170
+ */
171
+void cmdline_parser_print_version(void);
172
+
173
+/**
174
+ * Initializes all the fields a cmdline_parser_params structure 
175
+ * to their default values
176
+ * @param params the structure to initialize
177
+ */
178
+void cmdline_parser_params_init(struct cmdline_parser_params *params);
179
+
180
+/**
181
+ * Allocates dynamically a cmdline_parser_params structure and initializes
182
+ * all its fields to their default values
183
+ * @return the created and initialized cmdline_parser_params structure
184
+ */
185
+struct cmdline_parser_params *cmdline_parser_params_create(void);
186
+
187
+/**
188
+ * Initializes the passed gengetopt_args_info structure's fields
189
+ * (also set default values for options that have a default)
190
+ * @param args_info the structure to initialize
191
+ */
192
+void cmdline_parser_init (struct gengetopt_args_info *args_info);
193
+/**
194
+ * Deallocates the string fields of the gengetopt_args_info structure
195
+ * (but does not deallocate the structure itself)
196
+ * @param args_info the structure to deallocate
197
+ */
198
+void cmdline_parser_free (struct gengetopt_args_info *args_info);
199
+
200
+/**
201
+ * Checks that all the required options were specified
202
+ * @param args_info the structure to check
203
+ * @param prog_name the name of the program that will be used to print
204
+ *   possible errors
205
+ * @return
206
+ */
207
+int cmdline_parser_required (struct gengetopt_args_info *args_info,
208
+  const char *prog_name);
209
+
210
+
211
+#ifdef __cplusplus
212
+}
213
+#endif /* __cplusplus */
214
+#endif /* CMDLINE_H */

+ 1
- 1
license.txt Целия файл

@@ -1,4 +1,4 @@
1
-Copyright (C) 2013 Dalton Nell
1
+Copyright (C) 2014 Dalton Nell
2 2
 
3 3
 This program is free software: you can redistribute it and/or modify
4 4
 it under the terms of the GNU General Public License as published by

+ 38
- 23
main.cpp Целия файл

@@ -2,7 +2,7 @@
2 2
 #include <cstdio>
3 3
 #include "x.hpp"
4 4
 #include "rectangle.hpp"
5
-#include "options.hpp"
5
+#include "cmdline.h"
6 6
 
7 7
 void printSelection( bool cancelled, int x, int y, int w, int h, int window ) {
8 8
     printf( "X=%i\n", x );
@@ -31,6 +31,24 @@ void printSelection( bool cancelled, int x, int y, int w, int h, int window ) {
31 31
     }
32 32
 }
33 33
 
34
+int parseColor( std::string arg, float* r, float* g, float* b, float* a ) {
35
+    std::string copy = arg;
36
+    int find = copy.find( "," );
37
+    while( find != copy.npos ) {
38
+        copy.at( find ) = ' ';
39
+        find = copy.find( "," );
40
+    }
41
+
42
+    // Just in case we didn't include an alpha value
43
+    *a = 1;
44
+    int num = sscanf( copy.c_str(), "%f %f %f %f", r, g, b, a );
45
+    if ( num != 3 && num != 4 ) {
46
+        fprintf( stderr, "Error parsing color %s\n", arg.c_str() );
47
+        return 1;
48
+    }
49
+    return 0;
50
+}
51
+
34 52
 void constrain( int sx, int sy, int ex, int ey, int padding, int minimumsize, int maximumsize, int* rsx, int* rsy, int* rex, int* rey ) {
35 53
     if ( minimumsize > maximumsize && maximumsize > 0 ) {
36 54
         fprintf( stderr, "Error: minimumsize is greater than maximumsize.\n" );
@@ -84,7 +102,8 @@ void constrain( int sx, int sy, int ex, int ey, int padding, int minimumsize, in
84 102
 }
85 103
 
86 104
 int main( int argc, char** argv ) {
87
-    int err = options->parseOptions( argc, argv );
105
+    gengetopt_args_info options;
106
+    int err = cmdline_parser( argc, argv, &options );
88 107
     if ( err ) {
89 108
         return err;
90 109
     }
@@ -93,17 +112,16 @@ int main( int argc, char** argv ) {
93 112
     slop::Rectangle* selection = NULL;
94 113
     Window window = None;
95 114
     Window windowmemory = None;
96
-    std::string xdisplay = options->m_xdisplay;
97
-    int padding = options->m_padding;
98
-    int borderSize = options->m_borderSize;
99
-    int tolerance = options->m_tolerance;
100
-    float r = options->m_red;
101
-    float g = options->m_green;
102
-    float b = options->m_blue;
103
-    float a = options->m_alpha;
104
-    bool highlight = options->m_highlight;
105
-    bool keyboard = options->m_keyboard;
106
-    bool decorations = options->m_decorations;
115
+    std::string xdisplay = options.xdisplay_arg;
116
+    int padding = options.padding_arg;
117
+    int borderSize = options.bordersize_arg;
118
+    int tolerance = options.tolerance_arg;
119
+    float r, g, b, a;
120
+    parseColor( options.color_arg, &r, &g, &b, &a );
121
+    float gracetime = atof( options.gracetime_arg );
122
+    bool highlight = options.highlight_flag;
123
+    bool keyboard = !options.nokeyboard_flag;
124
+    bool decorations = !options.nodecorations_flag;
107 125
     timespec start, time;
108 126
     int cx = 0;
109 127
     int cy = 0;
@@ -111,8 +129,8 @@ int main( int argc, char** argv ) {
111 129
     int ymem = 0;
112 130
     int wmem = 0;
113 131
     int hmem = 0;
114
-    int minimumsize = options->m_minimumsize;
115
-    int maximumsize = options->m_maximumsize;
132
+    int minimumsize = options.min_arg;
133
+    int maximumsize = options.max_arg;
116 134
 
117 135
     // First we set up the x interface and grab the mouse,
118 136
     // if we fail for either we exit immediately.
@@ -128,11 +146,9 @@ int main( int argc, char** argv ) {
128 146
     }
129 147
     if ( keyboard ) {
130 148
         err = xengine->grabKeyboard();
131
-        // We shouldn't error out from failing to grab the keyboard.
132
-        //if ( err ) {
133
-            //printSelection( true, 0, 0, 0, 0 );
134
-            //return err;
135
-        //}
149
+        if ( err ) {
150
+            fprintf( stderr, "Warning: Failed to grab the keyboard. This is non-fatal, keyboard presses might fall through to other applications.\n" );
151
+        }
136 152
     }
137 153
     clock_gettime( CLOCK_REALTIME, &start );
138 154
     while ( running ) {
@@ -140,10 +156,10 @@ int main( int argc, char** argv ) {
140 156
         // "ticking" the xengine makes it process all queued events.
141 157
         xengine->tick();
142 158
         // If the user presses any key on the keyboard, exit the application.
143
-        // Make sure at least options->m_gracetime has passed before allowing canceling
159
+        // Make sure at least gracetime has passed before allowing canceling
144 160
         double timei = double( time.tv_sec*1000000000L + time.tv_nsec )/1000000000.f;
145 161
         double starti = double( start.tv_sec*1000000000L + start.tv_nsec )/1000000000.f;
146
-        if ( timei - starti > options->m_gracetime ) {
162
+        if ( timei - starti > gracetime ) {
147 163
             if ( ( xengine->anyKeyPressed() && keyboard ) || xengine->mouseDown( 3 ) ) {
148 164
                 printSelection( true, 0, 0, 0, 0, None );
149 165
                 fprintf( stderr, "User pressed key. Canceled selection.\n" );
@@ -285,7 +301,6 @@ int main( int argc, char** argv ) {
285 301
     //xengine->tick();
286 302
     // Clean up global classes.
287 303
     delete xengine;
288
-    delete options;
289 304
     // Sleep for 0.05 seconds to ensure everything was cleaned up. (Without this, slop's window often shows up in screenshots.)
290 305
     usleep( 50000 );
291 306
     // If we canceled the selection, return error.

+ 2
- 2
makefile Целия файл

@@ -1,8 +1,8 @@
1 1
 CXX ?= g++
2 2
 CXXFLAGS ?= -O2 -g
3 3
 LDFLAGS += -lX11 -lXext -lrt
4
-SOURCES = main.cpp x.cpp options.cpp rectangle.cpp
5
-OBJECTS = $(SOURCES:.cpp=.o)
4
+SOURCES = main.cpp x.cpp cmdline.c rectangle.cpp
5
+OBJECTS = main.o x.o cmdline.o rectangle.o
6 6
 EXECUTABLE = slop
7 7
 BINDIR = "/usr/bin"
8 8
 

+ 75
- 0
options.ggo Целия файл

@@ -0,0 +1,75 @@
1
+package "slop"
2
+version "v3.1.3"
3
+usage "slop [options]"
4
+description "slop (Select Operation) is an application that queries for a selection from the user and prints the region to stdout."
5
+versiontext "Copyright (C) 2014 Dalton Nell, GPLv3"
6
+
7
+text "Options"
8
+
9
+option "xdisplay" - "Sets the x display."
10
+    string
11
+    typestr="hostname:number.screen_number"
12
+    default=":0"
13
+    optional
14
+
15
+option "nokeyboard" - "Disables the ability to cancel selections with the keyboard."
16
+    flag
17
+    off
18
+
19
+option "bordersize" b "Set the selection rectangle's thickness. Does nothing when --highlight is enabled."
20
+    int
21
+    default="5"
22
+    optional
23
+
24
+option "padding" p "Set the padding size of the selection. Can be negative."
25
+    int
26
+    default="0"
27
+    optional
28
+
29
+option "tolerance" t "How far in pixels the mouse can move after clicking and still be detected as a normal click instead of a click and drag. Setting this to 0 will disable window selections."
30
+    int
31
+    default="2"
32
+    optional
33
+
34
+option "gracetime" g "Set the amount of time before slop will check for keyboard cancellations in seconds."
35
+    string
36
+    typestr="FLOAT"
37
+    default="0.4"
38
+    optional
39
+
40
+option "color" c "Set the selection rectangle's color. Supports RGB or RGBA values."
41
+    string
42
+    typestr="FLOAT,FLOAT,FLOAT,FLOAT"
43
+    default="0.5,0.5,0.5,1"
44
+    optional
45
+
46
+option "nodecorations" n "Attempt to select child windows in order to avoid window decorations."
47
+    flag
48
+    off
49
+
50
+option "min" - "Set the minimum output of width or height values. This is useful to avoid outputting 0. Setting min and max to the same value disables drag selections."
51
+    int
52
+    default="0"
53
+    optional
54
+
55
+option "max" - "Set the maximum output of width or height values. Setting min and max to the same value disables drag selections."
56
+    int
57
+    default="0"
58
+    optional
59
+
60
+option "highlight" l "Instead of outlining selections, slop highlights it. This is only useful when --color is set to a transparent color."
61
+    flag
62
+    off
63
+
64
+text "\nExamples\n"
65
+text "    $ # Gray, thick, transparent border for maximum visiblity.\n"
66
+text "    $ slop -b 20 -c 0.5,0.5,0.5,0.8\n"
67
+text "\n"
68
+text "    $ # Remove window decorations.\n"
69
+text "    $ slop --nodecorations\n"
70
+text "\n"
71
+text "    $ # Disable window selections. Useful for selecting individual pixels.\n"
72
+text "    $ slop -t 0\n"
73
+text "\n"
74
+text "    $ # Classic Windows XP selection.\n"
75
+text "    $ slop -l -c 0.3,0.4,0.6,0.4\n"