diff options
author | Heghedus Razvan <heghedus.razvan@gmail.com> | 2018-01-07 21:42:33 +0200 |
---|---|---|
committer | Heghedus Razvan <heghedus.razvan@gmail.com> | 2018-01-13 17:32:15 +0200 |
commit | 9e85283f063d32c5e35b2091c4e67fc5cea0b630 (patch) | |
tree | d5cc4d2757383a68a6c6d5994c8b5b089864880b | |
parent | 440cf7112df12bd84dc15c07d65dab1ae26630ba (diff) |
Add example for idle protocol
Signed-off-by: Heghedus Razvan <heghedus.razvan@gmail.com>
-rw-r--r-- | examples/idle.c | 189 | ||||
-rw-r--r-- | examples/meson.build | 9 |
2 files changed, 198 insertions, 0 deletions
diff --git a/examples/idle.c b/examples/idle.c new file mode 100644 index 00000000..e5e01f63 --- /dev/null +++ b/examples/idle.c @@ -0,0 +1,189 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <getopt.h> +#include <pthread.h> +#include <wayland-client.h> +#include <wayland-client-protocol.h> +#include <idle-client-protocol.h> + +static struct org_kde_kwin_idle *idle_manager = NULL; +static struct wl_seat *seat = NULL; +static uint32_t timeout = 0, simulate_activity_timeout = 0, close_timeout = 0; +static int run = 1; + +struct thread_args { + struct wl_display *display; + struct org_kde_kwin_idle_timeout *timer; +}; + +static void handle_global(void *data, struct wl_registry *registry, + uint32_t name, const char *interface, uint32_t version) { + fprintf(stdout, "interfaces found: %s\n", interface); + if (strcmp(interface, "org_kde_kwin_idle") == 0) { + idle_manager = wl_registry_bind(registry, name, &org_kde_kwin_idle_interface, 1); + } + else if (strcmp(interface, "wl_seat") == 0) { + seat = wl_registry_bind(registry, name, &wl_seat_interface, 1); + } +} + +static void handle_global_remove(void *data, struct wl_registry *registry, uint32_t name) { + //TODO +} + +static const struct wl_registry_listener registry_listener = { + .global = handle_global, + .global_remove = handle_global_remove, +}; + +static void handle_idle(void* data, struct org_kde_kwin_idle_timeout *timer) { + fprintf(stdout, "idle state\n"); +} + +static void handle_resume(void* data, struct org_kde_kwin_idle_timeout *timer) { + fprintf(stdout, "active state\n"); +} + +static const struct org_kde_kwin_idle_timeout_listener idle_timer_listener = { + .idle = handle_idle, + .resumed = handle_resume, +}; + +int parse_args(int argc, char *argv[]) { + int c; + while ((c = getopt(argc, argv, "c:hs:t:")) != -1) { + switch(c) + { + case 'c': + close_timeout = strtoul(optarg, NULL, 10); + break; + case 's': + simulate_activity_timeout = strtoul(optarg, NULL, 10); + break; + case 't': + timeout = strtoul(optarg, NULL, 10); + break; + case 'h': + case '?': + printf("Usage: %s [OPTIONS]\n", argv[0]); + printf(" -t seconds\t\t\tidle timeout in seconds\n"); + printf(" -s seconds optional\t\tsimulate user activity after x seconds\n"); + printf(" -c seconds optional\t\tclose program after x seconds\n"); + printf(" -h\t\t\t\tthis help menu\n"); + return 1; + default: + return 1; + } + } + return 0; +} + +void *simulate_activity(void *data) { + sleep(simulate_activity_timeout); + fprintf(stdout, "simulate user activity\n"); + struct thread_args *arg = data; + org_kde_kwin_idle_timeout_simulate_user_activity(arg->timer); + wl_display_roundtrip(arg->display); + return NULL; +} + +void *close_program(void *data) { + sleep(close_timeout); + struct thread_args *arg = data; + org_kde_kwin_idle_timeout_release(arg->timer); + wl_display_roundtrip(arg->display); + fprintf(stdout, "close program\n"); + run = 0; + return NULL; +} + +void *main_loop(void *data) { + struct wl_display *display = data; + while (wl_display_dispatch(display) != -1) { + ; + } + return NULL; +} + +int main(int argc, char *argv[]) { + + if (parse_args(argc, argv) != 0) { + return -1; + } + if (timeout == 0) { + printf("idle timeout 0 is invalid\n"); + return -1; + } + + struct wl_display *display = wl_display_connect(NULL); + if (display == NULL) { + fprintf(stderr, "failed to create display\n"); + return -1; + } + + struct wl_registry *registry = wl_display_get_registry(display); + wl_registry_add_listener(registry, ®istry_listener, NULL); + wl_display_dispatch(display); + wl_display_roundtrip(display); + + if (idle_manager == NULL) { + fprintf(stderr, "display doesn't support idle protocol\n"); + return -1; + } + if (seat== NULL) { + fprintf(stderr, "seat error\n"); + return -1; + } + struct org_kde_kwin_idle_timeout *timer = + org_kde_kwin_idle_get_idle_timeout(idle_manager, seat, timeout * 1000); + + if (timer == NULL) { + fprintf(stderr, "Could not create idle_timeout\n"); + return -1; + } + + pthread_t t1, t2, t3; + struct thread_args arg = { + .timer = timer, + .display = display, + }; + + if (simulate_activity_timeout != 0 && simulate_activity_timeout < close_timeout) { + if (pthread_create(&t1, NULL, &simulate_activity, (void *)&arg) != 0) { + return -1; + } + } + if (close_timeout != 0) { + if (pthread_create(&t2, NULL, &close_program, (void *)&arg) != 0) { + if (simulate_activity_timeout != 0) { + pthread_cancel(t1); + } + return -1; + } + } + + org_kde_kwin_idle_timeout_add_listener(timer, &idle_timer_listener, timer); + fprintf(stdout, "waiting\n"); + + if (pthread_create(&t3, NULL, &main_loop, (void *)display) != 0) { + if (simulate_activity_timeout != 0) { + pthread_cancel(t1); + } + if (close_timeout != 0 ) { + pthread_cancel(t2); + } + } + + if (simulate_activity_timeout != 0) { + pthread_join(t1, NULL); + } + if (close_timeout != 0) { + pthread_join(t2, NULL); + } + pthread_cancel(t3); + + wl_display_disconnect(display); + return 0; +} diff --git a/examples/meson.build b/examples/meson.build index af0f5a18..aa9eac47 100644 --- a/examples/meson.build +++ b/examples/meson.build @@ -5,6 +5,8 @@ lib_shared = static_library( include_directories: include_directories('support') ) +threads = dependency('threads') + executable('simple', 'simple.c', dependencies: wlroots, link_with: lib_shared) executable('pointer', 'pointer.c', dependencies: wlroots, link_with: lib_shared) executable('touch', 'touch.c', dependencies: wlroots, link_with: lib_shared) @@ -32,3 +34,10 @@ executable( dependencies: [wayland_client, wlr_protos, wlroots], link_with: lib_shared, ) + +executable( + 'idle', + 'idle.c', + dependencies: [wayland_client, wlr_protos, wlroots, threads], + link_with: lib_shared, +) |