aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/idle.c191
-rw-r--r--examples/meson.build9
-rw-r--r--examples/multi-pointer.c1
-rw-r--r--examples/output-layout.c1
-rw-r--r--examples/pointer.c1
-rw-r--r--examples/rotation.c1
-rw-r--r--examples/screenshot.c2
-rw-r--r--examples/simple.c1
-rw-r--r--examples/support/shared.c2
-rw-r--r--examples/tablet.c1
-rw-r--r--examples/touch.c1
11 files changed, 210 insertions, 1 deletions
diff --git a/examples/idle.c b/examples/idle.c
new file mode 100644
index 00000000..57c366d1
--- /dev/null
+++ b/examples/idle.c
@@ -0,0 +1,191 @@
+#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>
+#include <wlr/util/log.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[]) {
+ wlr_log_init(L_DEBUG, NULL);
+
+ 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, &registry_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,
+)
diff --git a/examples/multi-pointer.c b/examples/multi-pointer.c
index 3f8b2415..62aa1bac 100644
--- a/examples/multi-pointer.c
+++ b/examples/multi-pointer.c
@@ -176,6 +176,7 @@ static void handle_input_remove(struct compositor_state *state,
}
int main(int argc, char *argv[]) {
+ wlr_log_init(L_DEBUG, NULL);
struct sample_state state = {
.default_color = { 0.25f, 0.25f, 0.25f, 1 },
.clear_color = { 0.25f, 0.25f, 0.25f, 1 },
diff --git a/examples/output-layout.c b/examples/output-layout.c
index d9325838..35ed22a8 100644
--- a/examples/output-layout.c
+++ b/examples/output-layout.c
@@ -181,6 +181,7 @@ static void handle_keyboard_key(struct keyboard_state *kbstate,
}
int main(int argc, char *argv[]) {
+ wlr_log_init(L_DEBUG, NULL);
struct sample_state state = {0};
state.x_vel = 500;
diff --git a/examples/pointer.c b/examples/pointer.c
index 280372e0..9894e311 100644
--- a/examples/pointer.c
+++ b/examples/pointer.c
@@ -257,6 +257,7 @@ static void handle_tablet_tool_axis(struct wl_listener *listener, void *data) {
}
int main(int argc, char *argv[]) {
+ wlr_log_init(L_DEBUG, NULL);
struct sample_state state = {
.default_color = { 0.25f, 0.25f, 0.25f, 1 },
.clear_color = { 0.25f, 0.25f, 0.25f, 1 },
diff --git a/examples/rotation.c b/examples/rotation.c
index f9307ed3..1d974025 100644
--- a/examples/rotation.c
+++ b/examples/rotation.c
@@ -123,6 +123,7 @@ static void handle_keyboard_key(struct keyboard_state *kbstate,
}
int main(int argc, char *argv[]) {
+ wlr_log_init(L_DEBUG, NULL);
struct sample_state state = {0};
state.config = parse_args(argc, argv);
diff --git a/examples/screenshot.c b/examples/screenshot.c
index 77652805..441d8684 100644
--- a/examples/screenshot.c
+++ b/examples/screenshot.c
@@ -36,6 +36,7 @@
#include <sys/param.h>
#include <screenshooter-client-protocol.h>
#include "util/os-compatibility.h"
+#include <wlr/util/log.h>
static struct wl_shm *shm = NULL;
static struct orbital_screenshooter *screenshooter = NULL;
@@ -242,6 +243,7 @@ static int set_buffer_size(int *width, int *height) {
}
int main(int argc, char *argv[]) {
+ wlr_log_init(L_DEBUG, NULL);
struct wl_display * display = wl_display_connect(NULL);
if (display == NULL) {
fprintf(stderr, "failed to create display: %m\n");
diff --git a/examples/simple.c b/examples/simple.c
index ad1b0792..95c056c8 100644
--- a/examples/simple.c
+++ b/examples/simple.c
@@ -44,6 +44,7 @@ void handle_output_frame(struct output_state *output, struct timespec *ts) {
}
int main() {
+ wlr_log_init(L_DEBUG, NULL);
struct sample_state state = {
.color = { 1.0, 0.0, 0.0 },
.dec = 0,
diff --git a/examples/support/shared.c b/examples/support/shared.c
index f76e4389..6cfaa6aa 100644
--- a/examples/support/shared.c
+++ b/examples/support/shared.c
@@ -425,7 +425,7 @@ static void output_add_notify(struct wl_listener *listener, void *data) {
ostate->frame.notify = output_frame_notify;
ostate->resolution.notify = output_resolution_notify;
wl_signal_add(&output->events.frame, &ostate->frame);
- wl_signal_add(&output->events.resolution, &ostate->resolution);
+ wl_signal_add(&output->events.mode, &ostate->resolution);
wl_list_insert(&state->outputs, &ostate->link);
if (state->output_add_cb) {
state->output_add_cb(ostate);
diff --git a/examples/tablet.c b/examples/tablet.c
index 4eaa204a..f3cd5339 100644
--- a/examples/tablet.c
+++ b/examples/tablet.c
@@ -139,6 +139,7 @@ static void handle_pad_button(struct tablet_pad_state *pstate,
}
int main(int argc, char *argv[]) {
+ wlr_log_init(L_DEBUG, NULL);
struct sample_state state = {
.tool_color = { 1, 1, 1, 1 },
.pad_color = { 0.75, 0.75, 0.75, 1.0 }
diff --git a/examples/touch.c b/examples/touch.c
index f18b5d26..4c1f97b6 100644
--- a/examples/touch.c
+++ b/examples/touch.c
@@ -94,6 +94,7 @@ static void handle_touch_motion(struct touch_state *tstate, int32_t touch_id,
}
int main(int argc, char *argv[]) {
+ wlr_log_init(L_DEBUG, NULL);
struct sample_state state;
wl_list_init(&state.touch_points);