Add an option parser

On one hand, getopt (in particular the -o suboption syntax) sucks on the
server side, and on the client side we would like to avoid the glib
dependency.  We can roll out own option parser and solve both problems
and save a few lines of code total.
This commit is contained in:
Kristian Høgsberg
2012-03-11 21:05:57 -04:00
parent 22ba60e514
commit bcacef19b0
26 changed files with 226 additions and 240 deletions
+3 -2
View File
@@ -9,8 +9,9 @@ AM_CPPFLAGS = \
weston_LDFLAGS = -export-dynamic
weston_CFLAGS = $(GCC_CFLAGS)
weston_LDADD = \
$(COMPOSITOR_LIBS) $(DLOPEN_LIBS) $(XSERVER_LAUNCHER_LIBS) -lm
weston_LDADD = \
$(COMPOSITOR_LIBS) $(DLOPEN_LIBS) $(XSERVER_LAUNCHER_LIBS) -lm \
../shared/libconfig-parser.la
weston_SOURCES = \
compositor.c \
+9 -24
View File
@@ -1639,34 +1639,19 @@ drm_compositor_create(struct wl_display *display,
return &ec->base;
}
struct weston_compositor *
backend_init(struct wl_display *display, char *options);
WL_EXPORT struct weston_compositor *
backend_init(struct wl_display *display, char *options)
backend_init(struct wl_display *display, int argc, char *argv[])
{
int connector = 0, i;
const char *seat;
char *p, *value;
int tty = 0;
int connector = 0, tty = 0;
const char *seat = default_seat;
static char * const tokens[] = { "connector", "seat", "tty", NULL };
const struct weston_option drm_options[] = {
{ WESTON_OPTION_INTEGER, "connector", 0, &connector },
{ WESTON_OPTION_STRING, "seat", 0, &seat },
{ WESTON_OPTION_INTEGER, "tty", 0, &tty },
};
p = options;
seat = default_seat;
while (i = getsubopt(&p, tokens, &value), i != -1) {
switch (i) {
case 0:
connector = strtol(value, NULL, 0);
break;
case 1:
seat = value;
break;
case 2:
tty = strtol(value, NULL, 0);
break;
}
}
parse_options(drm_options, ARRAY_LENGTH(drm_options), argc, argv);
return drm_compositor_create(display, connector, seat, tty);
}
+9 -24
View File
@@ -663,34 +663,19 @@ wfd_compositor_create(struct wl_display *display,
return &ec->base;
}
struct weston_compositor *
backend_init(struct wl_display *display, char *options);
WL_EXPORT struct weston_compositor *
backend_init(struct wl_display *display, char *options)
backend_init(struct wl_display *display, int argc, char *argv[])
{
int connector = 0, i;
int connector = 0, tty = 0;
const char *seat;
char *p, *value;
int tty = 0;
static char * const tokens[] = { "connector", "seat", "tty", NULL };
p = options;
seat = default_seat;
while (i = getsubopt(&p, tokens, &value), i != -1) {
switch (i) {
case 0:
connector = strtol(value, NULL, 0);
break;
case 1:
seat = value;
break;
case 2:
tty = strtol(value, NULL, 0);
break;
}
}
const struct weston_option wfd_options[] = {
{ WESTON_OPTION_INTEGER, "connector", 0, &connector },
{ WESTON_OPTION_STRING, "seat", 0, &seat },
{ WESTON_OPTION_INTEGER, "tty", 0, &tty },
};
parse_options(&wfd_options, ARRAY_LENGTH(wfd_options), argc, argv);
return wfd_compositor_create(display, connector, seat, tty);
}
+11 -22
View File
@@ -721,31 +721,20 @@ wayland_compositor_create(struct wl_display *display,
return &c->base;
}
struct weston_compositor *
backend_init(struct wl_display *display, char *options);
WL_EXPORT struct weston_compositor *
backend_init(struct wl_display *display, char *options)
backend_init(struct wl_display *display, int argc, char *argv[])
{
int width = 1024, height = 640, i;
char *p, *value, *display_name = NULL;
int width = 1024, height = 640;
char *display_name = NULL;
static char * const tokens[] = { "width", "height", "display", NULL };
p = options;
while (i = getsubopt(&p, tokens, &value), i != -1) {
switch (i) {
case 0:
width = strtol(value, NULL, 0);
break;
case 1:
height = strtol(value, NULL, 0);
break;
case 2:
display_name = strdup(value);
break;
}
}
const struct weston_option wayland_options[] = {
{ WESTON_OPTION_INTEGER, "width", 0, &width },
{ WESTON_OPTION_INTEGER, "height", 0, &height },
{ WESTON_OPTION_STRING, "display", 0, &display_name },
};
parse_options(wayland_options,
ARRAY_LENGTH(wayland_options), argc, argv);
return wayland_compositor_create(display, width, height, display_name);
}
+9 -26
View File
@@ -817,36 +817,19 @@ x11_compositor_create(struct wl_display *display,
return &c->base;
}
struct weston_compositor *
backend_init(struct wl_display *display, char *options);
WL_EXPORT struct weston_compositor *
backend_init(struct wl_display *display, char *options)
backend_init(struct wl_display *display, int argc, char *argv[])
{
int width = 1024, height = 640, fullscreen = 0, count = 1, i;
char *p, *value;
int width = 1024, height = 640, fullscreen = 0, count = 1;
static char * const tokens[] = {
"width", "height", "fullscreen", "output-count", NULL
const struct weston_option x11_options[] = {
{ WESTON_OPTION_INTEGER, "width", 0, &width },
{ WESTON_OPTION_INTEGER, "height", 0, &height },
{ WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fullscreen },
{ WESTON_OPTION_INTEGER, "output-count", 0, &count },
};
p = options;
while (i = getsubopt(&p, tokens, &value), i != -1) {
switch (i) {
case 0:
width = strtol(value, NULL, 0);
break;
case 1:
height = strtol(value, NULL, 0);
break;
case 2:
fullscreen = 1;
break;
case 3:
count = strtol(value, NULL, 0);
break;
}
}
parse_options(x11_options, ARRAY_LENGTH(x11_options), argc, argv);
return x11_compositor_create(display,
width, height, count, fullscreen);
+24 -51
View File
@@ -42,7 +42,6 @@
#include <math.h>
#include <linux/input.h>
#include <dlfcn.h>
#include <getopt.h>
#include <signal.h>
#include <setjmp.h>
#include <execinfo.h>
@@ -50,8 +49,6 @@
#include <wayland-server.h>
#include "compositor.h"
static const char *option_socket_name = NULL;
static struct wl_list child_process_list;
static jmp_buf segv_jmp_buf;
@@ -2460,57 +2457,28 @@ int main(int argc, char *argv[])
struct wl_event_source *signals[4];
struct wl_event_loop *loop;
struct sigaction segv_action;
int o, xserver = 0;
void *shell_module, *backend_module;
int (*shell_init)(struct weston_compositor *ec);
struct weston_compositor
*(*backend_init)(struct wl_display *display, char *options);
char *backend = NULL;
char *backend_options = "";
char *shell = NULL;
char *p;
int option_idle_time = 300;
*(*backend_init)(struct wl_display *display,
int argc, char *argv[]);
int i;
char *backend = NULL;
char *shell = NULL;
int32_t idle_time = 300;
int32_t xserver;
char *socket_name = NULL;
static const char opts[] = "B:b:o:S:i:s:x";
static const struct option longopts[ ] = {
{ "backend", 1, NULL, 'B' },
{ "backend-options", 1, NULL, 'o' },
{ "socket", 1, NULL, 'S' },
{ "idle-time", 1, NULL, 'i' },
{ "shell", 1, NULL, 's' },
{ "xserver", 0, NULL, 'x' },
{ NULL, }
const struct weston_option core_options[] = {
{ WESTON_OPTION_STRING, "backend", 'B', &backend },
{ WESTON_OPTION_STRING, "socket", 'S', &socket_name },
{ WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
{ WESTON_OPTION_STRING, "shell", 's', &shell },
{ WESTON_OPTION_BOOLEAN, "xserver", 0, &xserver },
};
while (o = getopt_long(argc, argv, opts, longopts, &o), o > 0) {
switch (o) {
case 'B':
backend = optarg;
break;
case 'o':
backend_options = optarg;
break;
case 'S':
option_socket_name = optarg;
break;
case 'i':
option_idle_time = strtol(optarg, &p, 0);
if (*p != '\0') {
fprintf(stderr,
"invalid idle time option: %s\n",
optarg);
exit(EXIT_FAILURE);
}
break;
case 's':
shell = optarg;
break;
case 'x':
xserver = 1;
break;
}
}
argc = parse_options(core_options,
ARRAY_LENGTH(core_options), argc, argv);
display = wl_display_create();
@@ -2553,14 +2521,19 @@ int main(int argc, char *argv[])
if (!shell_init)
exit(EXIT_FAILURE);
ec = backend_init(display, backend_options);
ec = backend_init(display, argc, argv);
if (ec == NULL) {
fprintf(stderr, "failed to create compositor\n");
exit(EXIT_FAILURE);
}
ec->option_idle_time = option_idle_time;
ec->idle_time = option_idle_time;
for (i = 1; argv[i]; i++)
fprintf(stderr, "unhandled option: %s\n", argv[i]);
if (argv[1])
exit(EXIT_FAILURE);
ec->option_idle_time = idle_time;
ec->idle_time = idle_time;
#ifdef BUILD_XSERVER_LAUNCHER
if (xserver)
@@ -2570,7 +2543,7 @@ int main(int argc, char *argv[])
if (shell_init(ec) < 0)
exit(EXIT_FAILURE);
if (wl_display_add_socket(display, option_socket_name)) {
if (wl_display_add_socket(display, socket_name)) {
fprintf(stderr, "failed to add socket: %m\n");
exit(EXIT_FAILURE);
}
+4
View File
@@ -34,6 +34,7 @@
#include <EGL/eglext.h>
#include "matrix.h"
#include "../shared/config-parser.h"
struct weston_transform {
struct weston_matrix matrix;
@@ -552,4 +553,7 @@ weston_surface_set_color(struct weston_surface *surface,
void
weston_surface_destroy(struct weston_surface *surface);
struct weston_compositor *
backend_init(struct wl_display *display, int argc, char *argv[]);
#endif