Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

battery: make scroll change brightness #209

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions metadata/panel.xml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@
<default>32</default>
<min>1</min>
</option>
<option name="battery_scroll_sensitivity" type="double">
<_short>Battery Scroll Sensitivity</_short>
<default>1.8</default>
<min>0</min>
</option>
<option name="battery_icon_invert" type="bool">
<_short>Battery Icon Invert Color</_short>
<default>true</default>
Expand Down
56 changes: 56 additions & 0 deletions src/panel/widgets/battery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include <gtk-utils.hpp>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <filesystem>

#define UPOWER_NAME "org.freedesktop.UPower"
#define DISPLAY_DEVICE "/org/freedesktop/UPower/devices/DisplayDevice"
Expand All @@ -14,6 +16,13 @@
#define TIMETOEMPTY "TimeToEmpty"
#define SHOULD_DISPLAY "IsPresent"

std::string backlight = std::filesystem::directory_iterator{"/sys/class/backlight"}->path().string();

int max_brightness;
std::string brightness_file = backlight + "/brightness";
std::string max_brightness_file = backlight + "/max_brightness";
std::ifstream max_brightness_stream(max_brightness_file);

static std::string get_device_type_description(uint32_t type)
{
if (type == 2)
Expand All @@ -29,6 +38,35 @@ static std::string get_device_type_description(uint32_t type)
return "";
}

// this works as long as the owner of the wf-panel process is in the video group
// sudo usermod -a -G video USER
void change_brightness(int change) {
soreau marked this conversation as resolved.
Show resolved Hide resolved
std::ifstream brightness_stream(brightness_file);
int current_brightness;

if (brightness_stream >> current_brightness)
{
int new_brightness = current_brightness + (change*max_brightness)/100.0; // change by change%
if (new_brightness > max_brightness)
{
new_brightness = max_brightness;
}
else if (new_brightness <= 0)
{
new_brightness = 1;
}
std::cout << new_brightness << "\n";

std::ofstream brightness_output(brightness_file);
brightness_output << new_brightness;

return;
}

std::cerr << "battery.cpp: Failed to change brightness\n";
}


void WayfireBatteryInfo::on_properties_changed(
const Gio::DBus::Proxy::MapChangedProperties& properties,
const std::vector<Glib::ustring>& invalidated)
Expand Down Expand Up @@ -174,6 +212,18 @@ void WayfireBatteryInfo::update_details()
}
}

void WayfireBatteryInfo::on_battery_scroll(GdkEventScroll *event)
{
if (event->delta_y < 0)
{
change_brightness(sensitivity_opt);
}
else if (0 < event->delta_y)
{
change_brightness(-sensitivity_opt);
}
}

void WayfireBatteryInfo::update_state()
{
std::cout << "unimplemented reached, in battery.cpp: "
Expand Down Expand Up @@ -226,6 +276,8 @@ bool WayfireBatteryInfo::setup_dbus()
static const std::string default_font = "default";
void WayfireBatteryInfo::init(Gtk::HBox *container)
{
max_brightness_stream >> max_brightness;

if (!setup_dbus())
{
return;
Expand All @@ -234,6 +286,10 @@ void WayfireBatteryInfo::init(Gtk::HBox *container)
button_box.add(icon);
button.get_style_context()->add_class("flat");

button.set_events(Gdk::SCROLL_MASK | Gdk::SMOOTH_SCROLL_MASK);
button.signal_scroll_event().connect_notify(
sigc::mem_fun(this, &WayfireBatteryInfo::on_battery_scroll));

status_opt.set_callback([=] () { update_details(); });
font_opt.set_callback([=] () { update_font(); });
size_opt.set_callback([=] () { update_icon(); });
Expand Down
2 changes: 2 additions & 0 deletions src/panel/widgets/battery.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class WayfireBatteryInfo : public WayfireWidget
WfOption<std::string> status_opt{"panel/battery_status"};
WfOption<std::string> font_opt{"panel/battery_font"};
WfOption<int> size_opt{"panel/battery_icon_size"};
WfOption<double> sensitivity_opt{"panel/battery_scroll_sensitivity"};
WfOption<bool> invert_opt{"panel/battery_icon_invert"};

Gtk::Button button;
Expand All @@ -41,6 +42,7 @@ class WayfireBatteryInfo : public WayfireWidget
void update_icon();
void update_details();
void update_state();
void on_battery_scroll(GdkEventScroll *event);

void on_properties_changed(
const Gio::DBus::Proxy::MapChangedProperties& properties,
Expand Down
4 changes: 4 additions & 0 deletions src/panel/widgets/window-list/toplevel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ class WayfireToplevel::impl
{
menu.popup(event->button, event->time);
return true; // It has been handled.
} else if ((event->type == GDK_BUTTON_PRESS) && (event->button == 2)) // middle click
soreau marked this conversation as resolved.
Show resolved Hide resolved
{
zwlr_foreign_toplevel_handle_v1_close(handle);
return true;
} else
{
return false;
Expand Down