From 9b66b2ee444124df22543ccc38cd7b3b6302f994 Mon Sep 17 00:00:00 2001 From: streetturtle Date: Sat, 16 Jan 2021 14:50:54 -0500 Subject: [brightness] rework brightness widget also remove brightnessarc widget, as now it's possible to create arcchart widget in brightness widget --- brightness-widget/README.md | 57 ++++++--------- brightness-widget/br-wid-1.png | Bin 1355 -> 4912 bytes brightness-widget/brightness.lua | 147 +++++++++++++++++++++++++++---------- brightness-widget/brightness.svg | 153 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 284 insertions(+), 73 deletions(-) create mode 100644 brightness-widget/brightness.svg (limited to 'brightness-widget') diff --git a/brightness-widget/README.md b/brightness-widget/README.md index 02aa2d3..5428029 100644 --- a/brightness-widget/README.md +++ b/brightness-widget/README.md @@ -1,6 +1,6 @@ # Brightness widget -This widget represents current brightness level: ![Brightness widget](./br-wid-1.png) +This widget represents current brightness level, depending on config parameters could be an arcchart or icon with text: ![Brightness widget](./br-wid-1.png) ## Customization @@ -8,50 +8,39 @@ It is possible to customize widget by providing a table with all or some of the | Name | Default | Description | |---|---|---| -| `get_brightness_cmd` | `light -G` | Get current screen brightness | -| `inc_brightness_cmd` | `light -A 5` | Increase brightness | -| `dec_brightness_cmd` | `light -U 5`| Decrease brightness | +| `type`| `arc` | The widget type. Could be `arc` or `icon_and_text` | +| `program` | `light` | The program used to control the brightness, either 'light' or 'xbacklight'. | +| `step` | 5 | Step | | `path_to_icon` | `/usr/share/icons/Arc/status/symbolic/display-brightness-symbolic.svg` | Path to the icon | | `font` | `Play 9` | Font | -| `timeout` | 1 | How often in seconds the widget refreshes | - -### Example: - -```lua -brightness_widget({ - get_brightness_cmd = 'xbacklight -get', - inc_brightness_cmd = 'xbacklight -inc 5', - dec_brightness_cmd = 'xbacklight -dec 5' -}) -``` +| `timeout` | 1 | How often in seconds the widget refreshes. Check the note below | +_Note:_ If brightness is controlled only by the widget (either by a mouse, or by a shortcut, then the `timeout` could be quite big, as there is no reason to synchronize the brightness level). ## Installation -First you need to get the current brightness level. There are two options: +To choose the right `program` argument, first you need to check which of them works better for you. - - using `xbacklight` command (depending on your video card (I guess) it may or may not work) + - using `xbacklight`: - To check if it works install xbackligth and check if it works: + Install (on Ubuntu it's available in the apt repository) it and check if it works by running: ```bash - sudo apt-get install xbacklight xbacklight -get ``` - If there is no output it means that it doesn't work, but there is a second option: + If there is no output it means that it doesn't work, you can either try to fix it, or try to use `light`. - using `light` command - Install it from this git repo: [github.com/haikarainen/light](https://github.com/haikarainen/light) and check if it works but running + Install (on Ubuntu it's available in the apt repository) from the repo: [github.com/haikarainen/light](https://github.com/haikarainen/light) and check if it works by running ```bash - git clone https://github.com/haikarainen/light.git && \ - cd ./light && \ - sudo make && sudo make install \ light -G 49.18 + light -A 5 ``` + If you're on Ubuntu/debian and if the brightness level doesn't change, try to do this: https://github.com/haikarainen/light/issues/113#issuecomment-632638436. Then clone this repo under **~/.config/awesome/**: @@ -65,7 +54,7 @@ Require widget at the beginning of **rc.lua**: local brightness_widget = require("awesome-wm-widgets.brightness-widget.brightness") ``` -Add widget to the tasklist: +Add the widget to the tasklist: ```lua s.mytasklist, -- Middle widget @@ -75,13 +64,13 @@ s.mytasklist, -- Middle widget -- default brightness_widget(), -- or customized - brightness_widget({ - get_brightness_cmd = 'xbacklight -get', - inc_brightness_cmd = 'xbacklight -inc 5', - dec_brightness_cmd = 'xbacklight -dec 5' - }) + brightness_widget{ + type = 'icon_and_text', + program = 'xbacklight', + step = 2, + } } - ... + ... ``` ## Controls @@ -89,7 +78,7 @@ s.mytasklist, -- Middle widget In order to change brightness by shortcuts you can add them to the `globalkeys` table in the **rc.lua**: ```lua -awful.key({ modkey }, ";", function () awful.spawn("light -A 5") end, {description = "increase brightness", group = "custom"}), -awful.key({ modkey, "Shift"}, ";", function () awful.spawn("light -U 5") end, {description = "decrease brightness", group = "custom"}), +awful.key({ modkey }, ";", function () brightness_widget:inc() end, {description = "increase brightness", group = "custom"}), +awful.key({ modkey, "Shift"}, ";", function () brightness_widget:dec() end, {description = "decrease brightness", group = "custom"}), ``` -On laptop you can use `XF86MonBrightnessUp` and `XF86MonBrightnessDown` keys. +On a laptop you can use `XF86MonBrightnessUp` and `XF86MonBrightnessDown` keys. diff --git a/brightness-widget/br-wid-1.png b/brightness-widget/br-wid-1.png index f9200eb..b00b0e6 100644 Binary files a/brightness-widget/br-wid-1.png and b/brightness-widget/br-wid-1.png differ diff --git a/brightness-widget/brightness.lua b/brightness-widget/brightness.lua index 4686041..d5a79a9 100644 --- a/brightness-widget/brightness.lua +++ b/brightness-widget/brightness.lua @@ -5,66 +5,135 @@ -- https://github.com/streetturtle/awesome-wm-widgets/tree/master/brightness-widget -- @author Pavel Makhov --- @copyright 2017-2019 Pavel Makhov +-- @copyright 2021 Pavel Makhov ------------------------------------------------- +local awful = require("awful") local wibox = require("wibox") local watch = require("awful.widget.watch") local spawn = require("awful.spawn") +local naughty = require("naughty") -local PATH_TO_ICON = "/usr/share/icons/Arc/status/symbolic/display-brightness-symbolic.svg" -local GET_BRIGHTNESS_CMD = "light -G" -- "xbacklight -get" -local INC_BRIGHTNESS_CMD = "light -A 5" -- "xbacklight -inc 5" -local DEC_BRIGHTNESS_CMD = "light -U 5" -- "xbacklight -dec 5" +local ICON_DIR = os.getenv("HOME") .. '/.config/awesome/awesome-wm-widgets/brightness-widget/' +local get_brightness_cmd +local inc_brightness_cmd +local dec_brightness_cmd local brightness_widget = {} +local function show_warning(message) + naughty.notify{ + preset = naughty.config.presets.critical, + title = 'Brightness Widget', + text = message} +end + local function worker(user_args) local args = user_args or {} - local get_brightness_cmd = args.get_brightness_cmd or GET_BRIGHTNESS_CMD - local inc_brightness_cmd = args.inc_brightness_cmd or INC_BRIGHTNESS_CMD - local dec_brightness_cmd = args.dec_brightness_cmd or DEC_BRIGHTNESS_CMD - local path_to_icon = args.path_to_icon or PATH_TO_ICON + local type = args.type or 'arc' -- arc or icon_and_text + local path_to_icon = args.path_to_icon or ICON_DIR .. 'brightness.svg' local font = args.font or 'Play 9' - local timeout = args.timeout or 1 - - local brightness_text = wibox.widget.textbox() - brightness_text:set_font(font) - - local brightness_icon = wibox.widget { - { - image = path_to_icon, - resize = false, - widget = wibox.widget.imagebox, - }, - top = 3, - widget = wibox.container.margin - } - - brightness_widget = wibox.widget { - brightness_icon, - brightness_text, - layout = wibox.layout.fixed.horizontal, - } + local timeout = args.timeout or 100 + + local program = args.program or 'light' + local step = args.step or 5 + if program == 'light' then + get_brightness_cmd = 'sh -c "light -G"' + inc_brightness_cmd = 'sh -c "light -A ' .. step .. '"' + dec_brightness_cmd = 'sh -c "light -U ' .. step .. '"' + elseif program == 'xbacklight' then + get_brightness_cmd = 'xbacklight -get' + inc_brightness_cmd = 'xbacklight -inc ' .. step + dec_brightness_cmd = 'xbacklight -dec ' .. step + else + show_warning(program .. " command is not supported by the widget") + return + end + + if type == 'icon_and_text' then + brightness_widget.widget = wibox.widget { + { + { + image = path_to_icon, + resize = false, + widget = wibox.widget.imagebox, + }, + valigh = 'center', + layout = wibox.container.place + }, + { + id = 'txt', + font = font, + widget = wibox.widget.textbox + }, + spacing = 4, + layout = wibox.layout.fixed.horizontal, + set_value = function(self, level) + self:get_children_by_id('txt')[1]:set_text(level .. '%') + end + } + elseif type == 'arc' then + brightness_widget.widget = wibox.widget { + { + { + image = path_to_icon, + resize = true, + widget = wibox.widget.imagebox, + }, + valigh = 'center', + layout = wibox.container.place + }, + max_value = 100, + thickness = 2, + start_angle = 4.71238898, -- 2pi*3/4 + forced_height = 18, + forced_width = 18, + bg = bg_color, + paddings = 2, + colors = {color}, + widget = wibox.container.arcchart, + set_value = function(self, level) + self:set_value(level) + end + } + else + show_warning(type .. " type is not supported by the widget") + return + + end local update_widget = function(widget, stdout, _, _, _) local brightness_level = tonumber(string.format("%.0f", stdout)) - widget:set_text(" " .. brightness_level .. "%") + widget:set_value(brightness_level) + end + + function brightness_widget:inc() + spawn.easy_async(inc_brightness_cmd, function() + spawn.easy_async(get_brightness_cmd, function(out) + update_widget(brightness_widget.widget, out) + end) + end) + end + function brightness_widget:dec() + spawn.easy_async(dec_brightness_cmd, function() + spawn.easy_async(get_brightness_cmd, function(out) + update_widget(brightness_widget.widget, out) + end) + end) end - brightness_widget:connect_signal("button::press", function(_, _, _, button) - if (button == 4) then - spawn(inc_brightness_cmd, false) - elseif (button == 5) then - spawn(dec_brightness_cmd, false) - end - end) + brightness_widget.widget:buttons( + awful.util.table.join( + awful.button({}, 4, function() brightness_widget:inc() end), + awful.button({}, 5, function() brightness_widget:dec() end) + ) + ) - watch(get_brightness_cmd, timeout, update_widget, brightness_text) + watch(get_brightness_cmd, timeout, update_widget, brightness_widget.widget) - return brightness_widget + return brightness_widget.widget end return setmetatable(brightness_widget, { __call = function(_, ...) diff --git a/brightness-widget/brightness.svg b/brightness-widget/brightness.svg new file mode 100644 index 0000000..d334372 --- /dev/null +++ b/brightness-widget/brightness.svg @@ -0,0 +1,153 @@ + + + + + + + + + + + + + + + + + image/svg+xml + + + + + + + + + + + + + + + + + -- cgit v1.2.3