Skip to content

Commit

Permalink
feature: keyboard backlight button and cli
Browse files Browse the repository at this point in the history
Signed-off-by: ldelossa <[email protected]>
  • Loading branch information
ldelossa committed Jun 14, 2024
1 parent 7b0b5ea commit 59d21ad
Show file tree
Hide file tree
Showing 14 changed files with 788 additions and 104 deletions.
15 changes: 13 additions & 2 deletions data/org.ldelossa.way-shell.gschema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<!--system related settings-->
<schema path="/org/ldelossa/way-shell/system/" id="org.ldelossa.way-shell.system">
<key name="suspend-method" type="s">
<default>'suspend'</default>
<default>"suspend"</default>
<summary>The suspend method used when suspend is invoked from Quick Settings</summary>
<description>
The method used to suspend the system. Options are:
Expand All @@ -19,7 +19,7 @@
</description>
</key>
<key name="backlight-directory" type="s">
<default>'intel_backlight'</default>
<default>"intel_backlight"</default>
<summary>The relative directory of your backlight device file</summary>
<description>
The directory located under /sys/class/backlight/ which contains
Expand All @@ -28,6 +28,17 @@
on your machine.
</description>
</key>
<key name="keyboard-backlight-directory" type="s">
<default>""</default>
<summary>The relative directory of your backlight device file</summary>
<description>
The directory located typically under /sys/class/leds/ which contains
the keyboard backlight device files such as "brightness" and "max_brightness"
There is no default since this tends to be specific to the manufacturer.
For instance Lenovo Thinkpads usually have they keyboard backlight directory
idenified as '/sys/class/leds/tpacpi::kbd_backlight'
</description>
</key>
<key name="idle-inhibitor" type="b">
<default>false</default>
<summary>Enable light theme</summary>
Expand Down
10 changes: 10 additions & 0 deletions src/panel/quick_settings/quick_settings_grid/quick_settings_grid.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

#include <adwaita.h>

#include "../../../services/brightness_service/brightness_service.h"
#include "../../../services/logind_service/logind_service.h"
#include "../../../services/network_manager_service.h"
#include "../../../services/power_profiles_service/power_profiles_service.h"
#include "./quick_settings_grid_airplane_mode_button.h"
#include "./quick_settings_grid_night_light/quick_settings_grid_night_light.h"
#include "./quick_settings_grid_power_profiles/quick_settings_grid_power_profiles.h"
#include "./quick_settings_grid_wifi/quick_settings_grid_wifi.h"
#include "./quick_settings_keyboard_brightness/quick_settings_grid_keyboard_brightness.h"
#include "nm-dbus-interface.h"
#include "quick_settings_grid_button.h"
#include "quick_settings_grid_cluster.h"
Expand Down Expand Up @@ -277,6 +279,14 @@ static void quick_settings_grid_init_layout(QuickSettingsGrid *self) {
quick_settings_grid_add_button(
self, (QuickSettingsGridButton *)airplane_mode_button);

BrightnessService *bs = brightness_service_get_global();
if (bs && brightness_service_has_keyboard_brightness(bs)) {
QuickSettingsGridKeyboardBrightnessButton *keyboard_brightness_button =
quick_settings_grid_keyboard_brightness_button_init();
quick_settings_grid_add_button(
self, (QuickSettingsGridButton *)keyboard_brightness_button);
}

// add theme button
QuickSettingsGridOneThemeButton *theme_button =
quick_settings_grid_theme_button_init();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "./quick_settings_grid_power_profiles/quick_settings_grid_power_profiles.h"
#include "./quick_settings_grid_theme.h"
#include "./quick_settings_grid_wifi/quick_settings_grid_wifi.h"
#include "./quick_settings_keyboard_brightness/quick_settings_grid_keyboard_brightness.h"
#include "gtk/gtkrevealer.h"
#include "quick_settings_grid_ethernet.h"

Expand Down Expand Up @@ -206,5 +207,9 @@ void quick_settings_grid_button_free(QuickSettingsGridButton *self) {
quick_settings_grid_airplane_mode_button_free(
(QuickSettingsGridAirplaneModeButton *)self);
break;
case QUICK_SETTINGS_BUTTON_KEYBOARD_BRIGHTNESS:
quick_settings_grid_keyboard_brightness_button_free(
(QuickSettingsGridKeyboardBrightnessButton *)self);
break;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ enum QuickSettingsButtonType {
QUICK_SETTINGS_BUTTON_THEME,
QUICK_SETTINGS_BUTTON_NIGHT_LIGHT,
QUICK_SETTINGS_BUTTON_AIRPLANE_MODE,
QUICK_SETTINGS_BUTTON_KEYBOARD_BRIGHTNESS,
};

typedef struct _QuickSettingsGridCluster QuickSettingsGridCluster;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#include "./quick_settings_grid_keyboard_brightness.h"

#include <adwaita.h>

#include "../../../../services/brightness_service/brightness_service.h"
#include "glib-object.h"

void quick_settings_grid_keyboard_brightness_button_layout(
QuickSettingsGridKeyboardBrightnessButton *self) {
// create associated menu
self->menu =
g_object_new(QUICK_SETTINGS_GRID_KEYBOARD_BRIGHTNESS_MENU_TYPE, NULL);

quick_settings_grid_button_init(
&self->button, QUICK_SETTINGS_BUTTON_KEYBOARD_BRIGHTNESS, "Keyboard",
NULL, "keyboard-brightness-symbolic",
quick_settings_grid_keyboard_brightness_button_get_menu_widget(self),
quick_settings_grid_keyboard_brightness_menu_on_reveal);
}

static void on_toggle_button_clicked(
GtkButton *button, QuickSettingsGridKeyboardBrightnessButton *self) {
g_debug(
"quick_settings_grid_keyboard_brightness.c: "
"on_toggle_button_clicked(): "
"toggling keyboard backlight");

BrightnessService *bs = brightness_service_get_global();
guint keyboard_brightness = brightness_service_get_keyboard(bs);

if (keyboard_brightness > 0)
brightness_service_set_keyboard(bs, 0);
else
brightness_service_set_keyboard(bs, 1);
}

static void on_keyboard_brightness_changed(
BrightnessService *bs, guint keyboard_brightness,
QuickSettingsGridKeyboardBrightnessButton *self) {
g_debug(
"quick_settings_grid_keyboard_brightness.c: "
"on_keyboard_brightness_changed(): "
"keyboard_brightness = %d",
keyboard_brightness);

if (keyboard_brightness > 0)
quick_settings_grid_button_set_toggled(&self->button, TRUE);
else
quick_settings_grid_button_set_toggled(&self->button, FALSE);
}

QuickSettingsGridKeyboardBrightnessButton *
quick_settings_grid_keyboard_brightness_button_init() {
QuickSettingsGridKeyboardBrightnessButton *self =
g_malloc0(sizeof(QuickSettingsGridKeyboardBrightnessButton));

quick_settings_grid_keyboard_brightness_button_layout(self);

BrightnessService *bs = brightness_service_get_global();

guint keyboard_brightness = brightness_service_get_keyboard(bs);
on_keyboard_brightness_changed(bs, keyboard_brightness, self);

g_signal_connect(self->button.toggle, "clicked",
G_CALLBACK(on_toggle_button_clicked), self);
g_signal_connect(bs, "keyboard-brightness-changed",
G_CALLBACK(on_keyboard_brightness_changed), self);

return self;
}

GtkWidget *quick_settings_grid_keyboard_brightness_button_get_menu_widget(
QuickSettingsGridKeyboardBrightnessButton *self) {
return GTK_WIDGET(
quick_settings_grid_keyboard_brightness_menu_get_widget(self->menu));
}

void quick_settings_grid_keyboard_brightness_button_free(
QuickSettingsGridKeyboardBrightnessButton *self) {
g_debug(
"quick_settings_grid_keyboard_brightness.c:qs_grid_keyboard_brightness_"
"button_free() "
"called");

// kill signals
BrightnessService *bs = brightness_service_get_global();
g_signal_handlers_disconnect_by_func(bs, on_keyboard_brightness_changed,
self);

// free ourselves
g_free(self);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include <NetworkManager.h>
#include <adwaita.h>

#include "../quick_settings_grid_button.h"
#include "quick_settings_grid_keyboard_brightness_menu.h"

#define QUICK_SETTINGS_KEYBOARD_BRIGHTNESS_TYPE "keyboard-brightness"

typedef struct _QuickSesstingsGridKeyboardBrightnessButton {
// embedd this as first argument so we can cast to it.
QuickSettingsGridButton button;
QuickSettingsGridKeyboardBrightnessMenu *menu;
gboolean enabled;

} QuickSettingsGridKeyboardBrightnessButton;

QuickSettingsGridKeyboardBrightnessButton *
quick_settings_grid_keyboard_brightness_button_init();

void quick_settings_grid_keyboard_brightness_button_free(
QuickSettingsGridKeyboardBrightnessButton *self);

GtkWidget *quick_settings_grid_keyboard_brightness_button_get_menu_widget(
QuickSettingsGridKeyboardBrightnessButton *self);

void quick_settings_grid_keyboard_brightness_menu_on_reveal(
QuickSettingsGridButton *button, gboolean is_revealed);
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
#include "quick_settings_grid_keyboard_brightness_menu.h"

#include <adwaita.h>

#include "../../../../services/brightness_service/brightness_service.h"
#include "../../quick_settings.h"
#include "../../quick_settings_menu_widget.h"
#include "gtk/gtk.h"
#include "quick_settings_grid_keyboard_brightness.h"

enum signals { signals_n };

typedef struct _QuickSettingsGridKeyboardBrightnessMenu {
GObject parent_instance;
QuickSettingsMenuWidget menu;
GtkScale *brightness_slider;
} QuickSettingsGridKeyboardBrightnessMenu;
G_DEFINE_TYPE(QuickSettingsGridKeyboardBrightnessMenu,
quick_settings_grid_keyboard_brightness_menu, G_TYPE_OBJECT);

// stub out dispose, finalize, init and class init functions for GObject
static void quick_settings_grid_keyboard_brightness_menu_dispose(
GObject *object) {
QuickSettingsGridKeyboardBrightnessMenu *self =
QUICK_SETTINGS_GRID_KEYBOARD_BRIGHTNESS_MENU(object);
}

static void quick_settings_grid_keyboard_brightness_menu_finalize(
GObject *object) {}

static void quick_settings_grid_keyboard_brightness_menu_class_init(
QuickSettingsGridKeyboardBrightnessMenuClass *klass) {
GObjectClass *object_class = G_OBJECT_CLASS(klass);
object_class->dispose =
quick_settings_grid_keyboard_brightness_menu_dispose;
object_class->finalize =
quick_settings_grid_keyboard_brightness_menu_finalize;
}

static void on_value_changed(GtkRange *range,
QuickSettingsGridKeyboardBrightnessMenu *self);

static void on_keyboard_brightness_changed(
BrightnessService *bs, guint32 brightness,
QuickSettingsGridKeyboardBrightnessMenu *self);

static void block_brightness_service_signals(
QuickSettingsGridKeyboardBrightnessMenu *self, gboolean block) {
BrightnessService *bs = brightness_service_get_global();
if (block) {
g_signal_handlers_block_by_func(bs, on_keyboard_brightness_changed,
self);
} else {
g_signal_handlers_unblock_by_func(bs, on_keyboard_brightness_changed,
self);
}
}

static void block_brightness_scale_signals(
QuickSettingsGridKeyboardBrightnessMenu *self, gboolean block) {
if (block) {
g_signal_handlers_block_by_func(self->brightness_slider,
on_value_changed, self);
} else {
g_signal_handlers_unblock_by_func(self->brightness_slider,
on_value_changed, self);
}
}

static void on_keyboard_brightness_changed(
BrightnessService *bs, guint32 brightness,
QuickSettingsGridKeyboardBrightnessMenu *self) {
g_debug(
"quick_settings_grid_keyboard_brightness_menu.c:on_keyboard_brightness_"
"changed() "
"called: %d",
brightness);

// we are setting out scale, so ignore next scale event
block_brightness_scale_signals(self, true);

gtk_range_set_value(GTK_RANGE(self->brightness_slider), brightness);

block_brightness_scale_signals(self, false);
}

static void on_value_changed(GtkRange *range,
QuickSettingsGridKeyboardBrightnessMenu *self) {
g_debug(
"quick_settings_grid_keyboard_brightness_menu.c:on_value_changed() "
"called: %f",
gtk_range_get_value(range));

// we are writing to brightness API so ignore next brightness service
// event

block_brightness_service_signals(self, true);

BrightnessService *bs = brightness_service_get_global();
brightness_service_set_keyboard(bs, gtk_range_get_value(range));

block_brightness_service_signals(self, false);
}

static void quick_settings_grid_keyboard_brightness_menu_init_layout(
QuickSettingsGridKeyboardBrightnessMenu *self) {
quick_settings_menu_widget_init(&self->menu, false);
gtk_image_set_from_icon_name(self->menu.icon,
"keyboard-brightness-symbolic");
gtk_label_set_text(self->menu.title, "Keyboard Backlight");

BrightnessService *bs = brightness_service_get_global();
guint32 keyboard_max = brightness_service_get_keyboard_max(bs);
guint32 keyboard_cur = brightness_service_get_keyboard(bs);

self->brightness_slider = GTK_SCALE(gtk_scale_new_with_range(
GTK_ORIENTATION_HORIZONTAL, 0, keyboard_max, 1.0));
gtk_range_set_round_digits(GTK_RANGE(self->brightness_slider), 0);

for (guint32 i = 0; i <= keyboard_max; i++) {
gtk_scale_add_mark(self->brightness_slider, i, GTK_POS_BOTTOM, NULL);
}

gtk_range_set_value(GTK_RANGE(self->brightness_slider), keyboard_cur);

// connect signal
g_signal_connect(self->brightness_slider, "value-changed",
G_CALLBACK(on_value_changed), self);

g_signal_connect(bs, "keyboard_brightness_changed",
G_CALLBACK(on_keyboard_brightness_changed), self);

gtk_box_append(self->menu.options, GTK_WIDGET(self->brightness_slider));
}

static void quick_settings_grid_keyboard_brightness_menu_init(
QuickSettingsGridKeyboardBrightnessMenu *self) {
quick_settings_grid_keyboard_brightness_menu_init_layout(self);
}

void quick_settings_grid_keyboard_brightness_menu_on_reveal(
QuickSettingsGridButton *button_, gboolean is_revealed) {
QuickSettingsGridKeyboardBrightnessButton *button =
(QuickSettingsGridKeyboardBrightnessButton *)button_;
QuickSettingsGridKeyboardBrightnessMenu *self = button->menu;
g_debug(
"quick_settings_grid_keyboard_brightness_menu.c:on_reveal() called");
}

GtkWidget *quick_settings_grid_keyboard_brightness_menu_get_widget(
QuickSettingsGridKeyboardBrightnessMenu *self) {
return GTK_WIDGET(self->menu.container);
}

GtkScale *quick_settings_grid_keyboard_brightness_menu_get_temp_scale(
QuickSettingsGridKeyboardBrightnessMenu *grid) {
return grid->brightness_slider;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include <NetworkManager.h>
#include <adwaita.h>

G_BEGIN_DECLS

struct _QuickSettingsGridKeyboardBrightnessMenu;
#define QUICK_SETTINGS_GRID_KEYBOARD_BRIGHTNESS_MENU_TYPE \
quick_settings_grid_keyboard_brightness_menu_get_type()
G_DECLARE_FINAL_TYPE(QuickSettingsGridKeyboardBrightnessMenu,
quick_settings_grid_keyboard_brightness_menu, QUICK_SETTINGS,
GRID_KEYBOARD_BRIGHTNESS_MENU, GObject);

G_END_DECLS

GtkScale *quick_settings_grid_keyboard_brightness_menu_get_temp_scale(
QuickSettingsGridKeyboardBrightnessMenu *grid);

GtkWidget *quick_settings_grid_keyboard_brightness_menu_get_widget(
QuickSettingsGridKeyboardBrightnessMenu *grid);
Loading

0 comments on commit 59d21ad

Please sign in to comment.