Skip to content

Commit

Permalink
Added wlroots idle inhibit
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikReider committed Aug 29, 2021
1 parent 3ff4c42 commit 45fb9f8
Show file tree
Hide file tree
Showing 3 changed files with 224 additions and 72 deletions.
46 changes: 41 additions & 5 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,49 @@ project(
default_options: ['warning_level=3'],
)

deps = [
dependency('libpulse', version: '>= 15.0'),
]

wayland_protos = dependency('wayland-protocols')
wayland_client = dependency('wayland-client', version: '>=1.14.91')

wl_protocol_dir = wayland_protos.get_pkgconfig_variable('pkgdatadir')

wayland_scanner = find_program('wayland-scanner')
wayland_scanner_code = generator(
wayland_scanner,
output: '@BASENAME@-protocol.c',
arguments: ['private-code', '@INPUT@', '@OUTPUT@'],
)
wayland_scanner_client = generator(
wayland_scanner,
output: '@BASENAME@-client-protocol.h',
arguments: ['client-header', '@INPUT@', '@OUTPUT@'],
)

xml = join_paths([wl_protocol_dir, 'unstable/idle-inhibit/idle-inhibit-unstable-v1.xml'])
client_protos_src = wayland_scanner_code.process(xml)
client_protos_headers = wayland_scanner_client.process(xml)

lib_client_protos = static_library(
'client_protos',
[ client_protos_src, client_protos_headers ],
)

client_protos = declare_dependency(
link_with: lib_client_protos,
sources: client_protos_headers,
)

executable(
'audioListener',
'./src/main.cpp',
dependencies: deps,
[
'./src/main.cpp',
'./src/idle.cpp',
],
dependencies: [
dependency('libpulse', version: '>= 15.0'),
wayland_protos,
wayland_client,
client_protos,
],
install: true,
)
81 changes: 81 additions & 0 deletions src/idle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include <string.h>
#include <wayland-client-protocol.h>

#include <iostream>

#include "idle-inhibit-unstable-v1-client-protocol.h"

using namespace std;

class Idle {
struct wl_compositor *compositor = NULL;
struct zwp_idle_inhibit_manager_v1 *wl_idle_inhibit_manager = NULL;
struct wl_surface *surface = NULL;
struct wl_display *display = NULL;
struct zwp_idle_inhibitor_v1 *idle = NULL;

static void global_add(void *data, struct wl_registry *registry,
uint32_t name, const char *interface, uint32_t) {
Idle *idle = (Idle *)data;
if (strcmp(interface, wl_compositor_interface.name) == 0) {
idle->compositor = (wl_compositor *)wl_registry_bind(
registry, name, &wl_compositor_interface, 1);
} else if (strcmp(interface, zwp_idle_inhibit_manager_v1_interface.name) ==
0) {
idle->wl_idle_inhibit_manager =
(zwp_idle_inhibit_manager_v1 *)wl_registry_bind(
registry, name, &zwp_idle_inhibit_manager_v1_interface, 1);
}
}

static void global_remove(void *, struct wl_registry *, uint32_t) {
// Do nothing
}

public:
Idle() {
display = wl_display_connect(NULL);
if (display == NULL) {
fprintf(stderr, "failed to connect to wl_display\n");
exit(1);
}

const struct wl_registry_listener registry_listener = {
.global = global_add,
.global_remove = global_remove,
};

struct wl_registry *registry = wl_display_get_registry(display);
wl_registry_add_listener(registry, &registry_listener, this);
wl_display_roundtrip(display);

if (wl_idle_inhibit_manager == NULL) {
fprintf(stderr, "wl_idle_inhibit_manager is NULL\n");
exit(1);
}
if (compositor == NULL) {
fprintf(stderr, "compositor is NULL\n");
exit(1);
}

surface = wl_compositor_create_surface(compositor);
}

void update(bool isRunning) {
if (isRunning) {
if (idle == NULL) {
idle = zwp_idle_inhibit_manager_v1_create_inhibitor(
wl_idle_inhibit_manager, surface);
wl_display_roundtrip(display);
cout << "IDLE INHIBITED" << endl;
}
} else {
if (idle != NULL) {
zwp_idle_inhibitor_v1_destroy(idle);
idle = NULL;
wl_display_roundtrip(display);
cout << "NOT IDLE INHIBITED" << endl;
}
}
}
};
Loading

0 comments on commit 45fb9f8

Please sign in to comment.