tools: pass a context around as userdata

We need the options during open_restricted(), so instead of the caller just
passing in a custom userdata, let them wrap it into a tools_context.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
This commit is contained in:
Peter Hutterer 2015-06-24 15:07:53 +10:00
parent 41ad79f8d8
commit b7c414558d
5 changed files with 38 additions and 26 deletions

View file

@ -42,7 +42,7 @@
uint32_t start_time;
static const uint32_t screen_width = 100;
static const uint32_t screen_height = 100;
struct tools_options options;
struct tools_context context;
static unsigned int stop = 0;
static void
@ -298,7 +298,7 @@ handle_and_print_events(struct libinput *li)
case LIBINPUT_EVENT_DEVICE_REMOVED:
print_device_notify(ev);
tools_device_apply_config(libinput_event_get_device(ev),
&options);
&context.options);
break;
case LIBINPUT_EVENT_KEYBOARD_KEY:
print_key_event(ev);
@ -380,12 +380,12 @@ main(int argc, char **argv)
struct libinput *li;
struct timespec tp;
tools_init_options(&options);
tools_init_context(&context);
if (tools_parse_args(argc, argv, &options))
if (tools_parse_args(argc, argv, &context))
return 1;
li = tools_open_backend(&options, NULL);
li = tools_open_backend(&context);
if (!li)
return 1;

View file

@ -44,7 +44,7 @@
#define clip(val_, min_, max_) min((max_), max((min_), (val_)))
struct tools_options options;
struct tools_context context;
struct touch {
int active;
@ -264,6 +264,7 @@ change_ptraccel(struct window *w, double amount)
static void
handle_event_device_notify(struct libinput_event *ev)
{
struct tools_context *context;
struct libinput_device *dev = libinput_event_get_device(ev);
struct libinput *li;
struct window *w;
@ -280,11 +281,12 @@ handle_event_device_notify(struct libinput_event *ev)
libinput_device_get_name(dev),
type);
tools_device_apply_config(libinput_event_get_device(ev),
&options);
li = libinput_event_get_context(ev);
w = libinput_get_user_data(li);
context = libinput_get_user_data(li);
w = context->user_data;
tools_device_apply_config(libinput_event_get_device(ev),
&context->options);
if (libinput_event_get_type(ev) == LIBINPUT_EVENT_DEVICE_ADDED) {
for (i = 0; i < ARRAY_LENGTH(w->devices); i++) {
@ -430,7 +432,8 @@ static gboolean
handle_event_libinput(GIOChannel *source, GIOCondition condition, gpointer data)
{
struct libinput *li = data;
struct window *w = libinput_get_user_data(li);
struct tools_context *context = libinput_get_user_data(li);
struct window *w = context->user_data;
struct libinput_event *ev;
libinput_dispatch(li);
@ -498,16 +501,17 @@ main(int argc, char *argv[])
gtk_init(&argc, &argv);
tools_init_options(&options);
tools_init_context(&context);
if (tools_parse_args(argc, argv, &options) != 0)
if (tools_parse_args(argc, argv, &context) != 0)
return 1;
udev = udev_new();
if (!udev)
error("Failed to initialize udev\n");
li = tools_open_backend(&options, &w);
context.user_data = &w;
li = tools_open_backend(&context);
if (!li)
return 1;

View file

@ -268,7 +268,7 @@ int
main(int argc, char **argv)
{
struct libinput *li;
struct tools_options options;
struct tools_context context;
struct libinput_event *ev;
if (argc > 1) {
@ -284,9 +284,9 @@ main(int argc, char **argv)
}
}
tools_init_options(&options);
tools_init_context(&context);
li = tools_open_backend(&options, NULL);
li = tools_open_backend(&context);
if (!li)
return 1;

View file

@ -101,8 +101,12 @@ tools_usage()
}
void
tools_init_options(struct tools_options *options)
tools_init_context(struct tools_context *context)
{
struct tools_options *options = &context->options;
context->user_data = NULL;
memset(options, 0, sizeof(*options));
options->tapping = -1;
options->drag_lock = -1;
@ -367,15 +371,15 @@ static const struct libinput_interface interface = {
};
struct libinput *
tools_open_backend(struct tools_options *options,
void *userdata)
tools_open_backend(struct tools_context *context)
{
struct libinput *li = NULL;
struct tools_options *options = &context->options;
if (options->backend == BACKEND_UDEV) {
li = open_udev(&interface, userdata, options->seat, options->verbose);
li = open_udev(&interface, context, options->seat, options->verbose);
} else if (options->backend == BACKEND_DEVICE) {
li = open_device(&interface, userdata, options->device, options->verbose);
li = open_device(&interface, context, options->device, options->verbose);
} else
abort();

View file

@ -48,10 +48,14 @@ struct tools_options {
double speed;
};
void tools_init_options(struct tools_options *options);
int tools_parse_args(int argc, char **argv, struct tools_options *options);
struct libinput* tools_open_backend(struct tools_options *options,
void *userdata);
struct tools_context {
struct tools_options options;
void *user_data;
};
void tools_init_context(struct tools_context *context);
int tools_parse_args(int argc, char **argv, struct tools_context *context);
struct libinput* tools_open_backend(struct tools_context *context);
void tools_device_apply_config(struct libinput_device *device,
struct tools_options *options);
void tools_usage();