#include <gtk/gtk.h>
#include "common.h"
+#include "options.h"
#include "clock.h"
#include "hvclock.h"
#include "dockapp.h"
/* seconds hand:
* operates identically to the minute hand
*/
- if (hvclock_infos.show_seconds) {
+ if (opts.show_seconds) {
angle = M_PI / 30 * seconds;
draw_clock_hand_common(clock, cr, angle,
HAND_SECONDS_LENGTH1_RATIO,
event->area.width, event->area.height);
#elif defined (HAVE_GTK3)
gdk_cairo_get_clip_rectangle(cr, &rect);
- if (hvclock_infos.debug)
+ if (opts.debug)
{
printf("Redrawing (%d,%d+%d+%d)\n", rect.x, rect.y,
rect.width, rect.height);
{
(void) event; /* Unused parameter. */
- if (hvclock_infos.debug)
+ if (opts.debug)
printf("%s: button released\n", PACKAGE_NAME);
/* single-click --> changing viewing mode */
if (hvclock_mode == CLOCK_MODE) {
hvclock_mode = CALENDAR_MODE;
- if (hvclock_infos.debug)
+ if (opts.debug)
printf("%s: switching to calendar mode\n", PACKAGE_NAME);
} else {
hvclock_mode = CLOCK_MODE;
- if (hvclock_infos.debug)
+ if (opts.debug)
printf("%s: switching to clock mode\n", PACKAGE_NAME);
}
/* If the seconds have changed, force update only if displaying of
* the seconds hand is requested. */
- if (hvclock_infos.show_seconds && (now->tm_sec != previous.tm_sec)) {
+ if (opts.show_seconds && (now->tm_sec != previous.tm_sec)) {
update = true;
}
* This file is released under the GPLv2
*/
+/* For proper scope */
+#define OPTIONS_M 1
+
#if HAVE_CONFIG_H
# include "config.h"
#endif
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
+#include <getopt.h>
#if STDC_HEADERS
# include <string.h>
#include "hvclock.h"
#include "options.h"
-/*******************************************************************************
- * Display the help message and exit
- ******************************************************************************/
-static void DisplayUsage(void)
-{
- printf("Usage: %s [OPTIONS]...\n", PACKAGE_NAME);
- printf("Dockable analog clock and calendar.\n\n");
- printf
- (" -d display debugging messages.\n");
- printf(" -h display this help and exit\n");
- printf(" -s display seconds hand\n");
- printf(" -version display version information");
- printf(" and exit\n");
- printf("\n");
-}
+#define PACKAGE_DESCRIPTION "Dockable analog clock and calendar"
-/*******************************************************************************
- * Display version information and exit
- ******************************************************************************/
-static void DisplayVersion(void)
+static const char *opt_string = "vh?ds";
+
+static const struct option long_opts[] = {
+ {"help", no_argument, NULL, 'h'},
+ {"version", no_argument, NULL, 'v'},
+ {"debug", no_argument, NULL, 'd'},
+ {"seconds", no_argument, NULL, 's'},
+ {NULL, no_argument, NULL, 0}
+};
+
+/*
+ * Display the help message
+ */
+static void
+display_help(void)
{
- printf("\n");
- printf(" %s, version %s\n", PACKAGE_NAME, PACKAGE_VERSION);
- printf(" Written by Hugo Villeneuve\n\n");
+ printf("%s\n\n", PACKAGE_NAME " - " PACKAGE_DESCRIPTION);
+ printf("Usage: %s [OPTION]...\n", PACKAGE_NAME);
+ printf(" -h, --help Display this help and exit\n");
+ printf(" -v, --version Display version information and exit\n");
+ printf(" -d, --debug Display debugging messages\n");
+ printf(" -s, --seconds Display seconds hand\n");
+ printf("\n");
}
-static void InvalidOption(char *message, /*@null@ */ char *string)
+/*
+ * Display version information and exit
+ */
+static void
+display_version(void)
{
- if (string == NULL)
- fprintf(stderr, "%s: %s\n", PACKAGE_NAME, message);
- else
- fprintf(stderr, "%s: %s %s\n", PACKAGE_NAME, message, string);
-
- fprintf(stderr, "Try `%s -h' for more information.\n", PACKAGE_NAME);
-
- exit(EXIT_FAILURE);
+ printf(PACKAGE_STRING);
+ printf("\n");
}
/*******************************************************************************
* Initializes the different options passed as arguments on the command line.
******************************************************************************/
-void ParseCommandLineOptions(int argc, char *argv[])
+void
+parse_command_line_options(int argc, char *argv[])
{
- int i;
- char *token;
+ int opt;
+ int long_index = 0;
+
+ /*
+ * Setting default values.
+ *
+ * You don't ordinarily need to copy an option string, since it is a
+ * pointer into the original argv array, not into a static area that might
+ * be overwritten.
+ */
+ opts.debug = false;
+ opts.show_seconds = false;
+
+ /* Parse our arguments. */
+ opt = getopt_long(argc, argv, opt_string, long_opts, &long_index);
- /* Default values. */
- hvclock_infos.debug = false;
- hvclock_infos.show_seconds = false;
+ while (opt != -1)
+ {
+ switch (opt)
+ {
+ case 'd':
+ opts.debug = true;
+ break;
+ case 's':
+ opts.show_seconds = true;
+ break;
+ case 'h':
+ case '?':
+ display_help();
+ exit(EXIT_SUCCESS);
+ break;
+ case 'v':
+ display_version();
+ exit(EXIT_SUCCESS);
+ break;
+ default:
+ /* You won't actually get here. */
+ break;
+ }
- for (i = 1; i < argc; i++) {
- token = argv[i];
- switch (token[0]) {
- case '-':
- /* Processing options names */
- switch (token[1]) {
- case 'd':
- hvclock_infos.debug = true;
- break;
- case 'h':
- DisplayUsage();
- exit(EXIT_SUCCESS);
- case 's':
- hvclock_infos.show_seconds = true;
- break;
- case 'v':
- if (STREQ("version", &token[1])) {
- DisplayVersion();
- exit(EXIT_SUCCESS);
- }
- InvalidOption("invalid option", token);
- break;
- default:
- InvalidOption("invalid option", token);
- break;
- } /* end switch( token[1] ) */
- break;
- default:
- /* Processing options arguments */
- InvalidOption("invalid option", token);
- break;
- } /* end switch( token[0] ) */
- } /* end for */
+ opt = getopt_long(argc, argv, opt_string, long_opts, &long_index);
+ }
}