awesome/lib/pwcall.cxx

61 lines
1.9 KiB
C++

#include <algorithm>
#define SOL_ALL_SAFETIES_ON 1
#include <glib.h>
#include <glib-object.h>
#include "sol.hpp"
static guint default_node_sig = 0, get_volume_sig = 0, set_volume_sig = 0;
static guint default_node(gpointer api) {
if (default_node_sig == 0)
default_node_sig = g_signal_lookup("get-default-node", G_TYPE_FROM_INSTANCE(api));
guint retval;
g_signal_emit(api, default_node_sig, 0, "Audio/Sink", &retval);
return retval;
}
static std::tuple<guint, bool> get_volume(gpointer api, guint32 id) {
if (get_volume_sig == 0)
get_volume_sig = g_signal_lookup("get-volume", G_TYPE_FROM_INSTANCE(api));
GVariant *variant = nullptr;
g_signal_emit(api, get_volume_sig, 0, id, &variant);
gdouble volumed = 0.5;
gboolean mute = false;
if (variant) {
g_variant_lookup(variant, "volume", "d", &volumed);
g_variant_lookup(variant, "mute", "b", &mute);
g_variant_unref(variant);
}
return std::tuple<guint, bool>(std::round(volumed * 100.0), mute == 1);
}
static void set_volume(gpointer api, guint32 id, int vol, bool mute) {
if (set_volume_sig == 0)
set_volume_sig = g_signal_lookup("set-volume", G_TYPE_FROM_INSTANCE(api));
vol = std::clamp(vol, 0, 150);
g_auto(GVariantBuilder) b = G_VARIANT_BUILDER_INIT(G_VARIANT_TYPE_VARDICT);
g_variant_builder_add(&b, "{sv}", "volume", g_variant_new_double(((gdouble)vol)/100));
g_variant_builder_add(&b, "{sv}", "mute", g_variant_new_boolean(mute ? TRUE : FALSE));
GVariant *variant = g_variant_builder_end(&b);
gboolean res;
g_signal_emit(api, set_volume_sig, 0, id, variant, &res);
}
extern "C" int luaopen_libpwcall(lua_State *L) {
sol::state_view lua(L);
auto tbl = lua.create_table();
tbl.set_function("default_node", default_node);
tbl.set_function("get_volume", get_volume);
tbl.set_function("set_volume", set_volume);
sol::stack::push(L, tbl);
return 1;
}