-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separated classes into header and implementation files
- Loading branch information
1 parent
aa2dcf3
commit 4e7b6c2
Showing
8 changed files
with
398 additions
and
318 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, ®istry_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, ®istry_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; | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.