Skip to content

Commit

Permalink
Separated classes into header and implementation files
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikReider committed Sep 5, 2021
1 parent aa2dcf3 commit 4e7b6c2
Show file tree
Hide file tree
Showing 8 changed files with 398 additions and 318 deletions.
52 changes: 52 additions & 0 deletions include/data.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#pragma once

#include <pulse/context.h>
#include <pulse/thread-mainloop.h>

#include <iostream>

#include "idle.hpp"

using namespace std;

enum SubscriptionType {
SUBSCRIPTION_TYPE_IDLE,
SUBSCRIPTION_TYPE_DRY_BOTH,
SUBSCRIPTION_TYPE_DRY_SINK,
SUBSCRIPTION_TYPE_DRY_SOURCE,
};
enum EventType {
EVENT_TYPE_IDLE,
EVENT_TYPE_DRY_BOTH,
EVENT_TYPE_DRY_SINK,
EVENT_TYPE_DRY_SOURCE,
EVENT_TYPE_NONE,
};

struct Data {
pa_threaded_mainloop *mainloop;
pa_mainloop_api *mainloop_api;
pa_context *context;

EventType eventCalled = EVENT_TYPE_NONE;
bool activeSource = false;
bool activeSink = false;

SubscriptionType subscriptionType;
pa_subscription_mask_t pa_subscriptionType;

Idle *idle = NULL;

bool failed = false;

Data(pa_threaded_mainloop *mainloop, pa_mainloop_api *mainloop_api,
SubscriptionType subscriptionType,
pa_subscription_mask_t pa_subscriptionType, EventType eventType);

void quit(int returnValue = 0);

void handleAction();

private:
void print(bool isRunning);
};
23 changes: 23 additions & 0 deletions include/idle.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#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);

static void global_remove(void *, struct wl_registry *, uint32_t);

public:
Idle();

void update(bool isRunning);
};
48 changes: 48 additions & 0 deletions include/pulse.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#pragma once

#include <pulse/context.h>
#include <pulse/def.h>
#include <pulse/mainloop-api.h>
#include <pulse/pulseaudio.h>
#include <pulse/thread-mainloop.h>
#include <string.h>

#include <cstdlib>
#include <iostream>

#include "data.hpp"
#include "idle.hpp"

class Pulse {
public:
int init(SubscriptionType subscriptionType,
pa_subscription_mask_t pa_subscriptionType, EventType eventType);

private:
static void sink_input_info_callback(pa_context *,
const pa_sink_input_info *i, int,
void *userdata);

static void source_output_info_callback(pa_context *,
const pa_source_output_info *i, int,
void *userdata);

void getRunning(EventType eventType, Data *data, pa_context *context);

static void subscribe_callback(pa_context *,
pa_subscription_event_type_t type, uint32_t,
void *userdata);

static void context_state_callback(pa_context *c, void *userdata);

static pa_context *getContext(pa_threaded_mainloop *mainloop,
pa_mainloop_api *mainloop_api, void *userdata);

void connect(pa_threaded_mainloop *mainloop, pa_mainloop_api *mainloop_api,
SubscriptionType subscriptionType,
pa_subscription_mask_t pa_subscriptionType, EventType eventType);

pa_threaded_mainloop *getMainLoop();

pa_mainloop_api *getMainLoopApi(pa_threaded_mainloop *mainloop);
};
4 changes: 4 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,13 @@ executable(
'sway-audio-idle-inhibit',
[
'./src/main.cpp',
'./src/data.cpp',
'./src/pulse.cpp',
'./src/idle.cpp',
],
include_directories: [
include_directories('include')
],
dependencies: [
dependency('libpulse', version: '>= 15.0'),
wayland_protos,
Expand Down
40 changes: 40 additions & 0 deletions src/data.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "data.hpp"

Data::Data(pa_threaded_mainloop *mainloop, pa_mainloop_api *mainloop_api,
SubscriptionType subscriptionType,
pa_subscription_mask_t pa_subscriptionType, EventType eventType) {
this->mainloop = mainloop;
this->mainloop_api = mainloop_api;
this->subscriptionType = subscriptionType;
this->pa_subscriptionType = pa_subscriptionType;
this->eventCalled = eventType;

if (subscriptionType == SUBSCRIPTION_TYPE_IDLE) idle = new Idle();
}

void Data::quit(int returnValue) {
mainloop_api->quit(mainloop_api, returnValue);
pa_threaded_mainloop_stop(mainloop);
pa_threaded_mainloop_free(mainloop);
}

void Data::handleAction() {
switch (subscriptionType) {
case SUBSCRIPTION_TYPE_IDLE:
idle->update(activeSink || activeSource);
break;
case SUBSCRIPTION_TYPE_DRY_BOTH:
this->print(activeSink || activeSource);
break;
case SUBSCRIPTION_TYPE_DRY_SINK:
this->print(activeSink);
break;
case SUBSCRIPTION_TYPE_DRY_SOURCE:
this->print(activeSource);
break;
}
}

void Data::print(bool isRunning) {
cout << (isRunning ? "RUNNING" : "NOT RUNNING") << endl;
}
118 changes: 53 additions & 65 deletions src/idle.cpp
Original file line number Diff line number Diff line change
@@ -1,81 +1,69 @@
#include "idle.hpp"

#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);
}
void Idle::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);
}
void Idle::global_remove(void *, struct wl_registry *, uint32_t) {}

const struct wl_registry_listener registry_listener = {
.global = global_add,
.global_remove = global_remove,
};
Idle::Idle() {
display = wl_display_connect(NULL);
if (display == NULL) {
fprintf(stderr, "failed to connect to wl_display\n");
exit(1);
}

struct wl_registry *registry = wl_display_get_registry(display);
wl_registry_add_listener(registry, &registry_listener, this);
wl_display_roundtrip(display);
const struct wl_registry_listener registry_listener = {
.global = global_add,
.global_remove = global_remove,
};

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);
}
struct wl_registry *registry = wl_display_get_registry(display);
wl_registry_add_listener(registry, &registry_listener, this);
wl_display_roundtrip(display);

surface = wl_compositor_create_surface(compositor);
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;
void Idle::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;
}
};
}
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <cstring>

#include "./pulse.cpp"
#include "pulse.hpp"

void showHelp(char **argv) {
cout << "Usage:" << endl;
Expand Down
Loading

0 comments on commit 4e7b6c2

Please sign in to comment.