From 17fe2b579f096ab4b3daacaca2db2d5443fd5e92 Mon Sep 17 00:00:00 2001 From: Josh Rahm Date: Tue, 2 Nov 2021 00:41:08 -0600 Subject: Big powerpill added to my XMonad and XMobar. --- install.sh | 2 + package.yaml | 1 + src/Internal/LayoutDraw.hs | 19 ++-- src/Main.hs | 38 +++++-- startup | 10 +- xmobar/extras/battery/battery.c | 226 ++++++++++++++++++++++++++++++++++++++++ xmobarrc | 43 ++++++-- 7 files changed, 308 insertions(+), 31 deletions(-) create mode 100644 xmobar/extras/battery/battery.c diff --git a/install.sh b/install.sh index dd8c299..a965ad8 100755 --- a/install.sh +++ b/install.sh @@ -5,6 +5,8 @@ cd "$real_dir" mkdir -p "$HOME/.xmonad" +cc -o ~/.xmonad/xmobar-battery xmobar/extras/battery/battery.c -lm + ln -sfv "$real_dir/build-script.sh" "$HOME/.xmonad/build" ln -sfv "$real_dir/compton.conf" "$HOME/.config/compton.conf" ln -sfv "$real_dir/startup" "$HOME/.xmonad/startup" diff --git a/package.yaml b/package.yaml index 686aaa7..e6e3648 100644 --- a/package.yaml +++ b/package.yaml @@ -19,3 +19,4 @@ dependencies: - cryptohash - listsafe - X11 + - split diff --git a/src/Internal/LayoutDraw.hs b/src/Internal/LayoutDraw.hs index 7dc2087..4bb069a 100644 --- a/src/Internal/LayoutDraw.hs +++ b/src/Internal/LayoutDraw.hs @@ -54,13 +54,13 @@ drawPng l = do -- (0.9, 0.9, 1.0), -- (0.8, 0.8, 1.0), -- (0.7, 0.7, 1.0), - (0.6, 0.6, 0.8), - (0.5, 0.5, 0.8), - (0.4, 0.4, 0.8), - (0.3, 0.3, 0.8), - (0.2, 0.2, 0.8), - (0.1, 0.1, 0.8), - (0.0, 0.0, 0.8) + (0.8, 0.6, 0.6), + (0.8, 0.5, 0.5), + (0.8, 0.4, 0.4), + (0.8, 0.3, 0.3), + (0.8, 0.2, 0.2), + (0.8, 0.1, 0.1), + (0.8, 0.0, 0.0) ] exists <- liftIO $ doesFileExist filepathXpm @@ -71,7 +71,7 @@ drawPng l = do setLineCap LineCapButt setLineJoin LineJoinMiter - forM_ (reverse $ zip (map (second padR) rects) colors) $ + forM_ (reverse $ zip (map (second extraPad) rects) colors) $ \((wind, Rectangle x y w h), (r, g, b)) -> do setSourceRGBA r g b 1 @@ -93,7 +93,8 @@ drawPng l = do return filepathXpm where - padR = id + extraPad (Rectangle x y w h) = + Rectangle (x + 100) (y + 100) (w - 100) (h - 100) -- padR (Rectangle x y w h) = -- Rectangle x y (max 1 $ w - 120) (max 1 $ h - 120) diff --git a/src/Main.hs b/src/Main.hs index 25c930e..7e1cc68 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -10,6 +10,7 @@ import Internal.Layout import XMonad.Hooks.ManageHelpers import XMonad.Layout.IndependentScreens import Text.Printf +import Data.List.Split import XMonad.Hooks.EwmhDesktops import Internal.Keys @@ -29,7 +30,8 @@ main = do , borderWidth = 2 , keys = \config -> mempty , focusedBorderColor = "#ff6c00" - , normalBorderColor = "#ffd9bf" + -- , normalBorderColor = "#ffd9bf" + , normalBorderColor = "#000000" , layoutHook = myLayout , startupHook = do ewmhDesktopsStartup @@ -55,15 +57,17 @@ main = do statusBar "xmobar" xmobarPP { - ppCurrent = xmobarColor "#ffffff" "red" . printf "%s" - , ppVisible = xmobarColor "#8888ff" "" . printf "%s" - , ppHidden = xmobarColor "#888888" "" . printf "%s" - , ppWsSep = " · " + ppCurrent = xmobarColor "#ff8888" "red" . printf "%s" + , ppVisible = xmobarColor "#8888ff" "" . printf "%s" + , ppHidden = xmobarColor "#888888" "" . printf "%s" + , ppWsSep = " " , ppTitle = - xmobarColor "#8888ff" "" . printf "%s" . - (printf "%s" :: String -> String) + xmobarColor "#808080" "" . + printf "%s" . + parseOut . + trunc 50 - , ppSep = xmobarColor "#404040" "" " ──── " + , ppSep = xmobarColor "#404040" "" " │ " , ppLayout = const "" , ppExtras = [showLayout] , ppOrder = \ss -> @@ -72,3 +76,21 @@ main = do } toggleStructsKey config + + where + parseOut :: String -> String + parseOut str = + let colors = ["#ff878f", "#e187ff", "#8f87ff", "#87fff7", "#8bff87", "#ffe987", "#ff8787"] + components = zip (cycle colors) (splitOnAll [" - ", " · ", " "] str) + in concatMap (\(color, str) -> + printf "%s " color str) components + + trunc amt str = + if length str > amt - 4 + then take (amt - 4) str ++ " ..." + else str + + splitOnAll arr str = splitOnAll' arr [str] + splitOnAll' [] str = str + splitOnAll' (a:as) str = splitOnAll' as (concatMap (splitOn a) str) + diff --git a/startup b/startup index bb43beb..dc302b7 100755 --- a/startup +++ b/startup @@ -11,14 +11,18 @@ hostname_rahm1() { xinput set-prop "TPPS/2 Elan TrackPoint" "Coordinate Transformation Matrix" 3 0 0 0 3 0 0 0 1 xinput set-prop "SYNA8004:00 06CB:CD8B Touchpad" "Coordinate Transformation Matrix" 3 0 0 0 3 0 0 0 1 - feh --bg-scale "/home/rahm/.xmonad/assets/Death-Valley-desert-USA_3840x2160.jpg" + if [ -z "$(ps aux | grep compton | grep -v grep)" ] ; then + __GL_SYNC_TO_VBLANK=1 nohup compton --backend=glx &>/dev/null & + fi + + feh --bg-scale "$HOME/wp.jpg" } hostname_photon() { # Startup commands specific to my desktop. - if [ ! -z "$(ps aux | grep compton | grep -v grep)" ] ; then - nohup compton --backend xrender & > /dev/null + if [ -z "$(ps aux | grep compton | grep -v grep)" ] ; then + __GL_SYNC_TO_VBLANK=1 nohup compton --backend=glx &>/dev/null & fi xrandr --output DVI-I-1 --right-of DP-5 --mode 2560x1440 feh --bg-scale "/home/rahm/.xmonad/wallpaper.jpg" diff --git a/xmobar/extras/battery/battery.c b/xmobar/extras/battery/battery.c new file mode 100644 index 0000000..8e5e58e --- /dev/null +++ b/xmobar/extras/battery/battery.c @@ -0,0 +1,226 @@ +#include +#include +#include +#include +#include +#include +#include + +#define N_ICONS 5 + +char* icons[] = { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", +}; + +typedef long long llong_t; + +llong_t get_number(const char* file) +{ + int fd; + char buf[128]; + fd = open(file, O_RDONLY); + + if (fd < 0) { + return -1; + } + + size_t count = read(fd, buf, sizeof(buf)); + close(fd); + + if (count < 0 || count > sizeof(buf) - 1) { + return -1; + } + + buf[count] = 0; + return atoll(buf); +} + +float absf(float f) +{ + return f < 0 ? -f : f; +} + +uint32_t hsv_to_rgb(int h, int s, int v){ + if(h > 360 || h < 0 || s > 100 || s < 0 || v > 100 || v < 0){ + return -1; + } + + float sf = s / 100.0; + float vf = v / 100.0; + float hf = (float) h; + + float c = sf * vf; + float x = c * (1 - absf(fmod(hf / 60.0, 2) - 1)); + float m = vf - c; + float r,g,b; + + if(h >= 0 && h < 60){ + r = c; + g = x; + b = 0; + } + else if(h >= 60 && h < 120){ + r = x; + g = c; + b = 0; + } + else if(h >= 120 && h < 180){ + r = 0; + g = c; + b = x; + } + else if(h >= 180 && h < 240){ + r = 0; + g = x; + b = c; + } + else if(h >= 240 && h < 300){ + r = x; + g = 0; + b = c; + } + else{ + r = c; + g = 0; + b = x; + } + + int ri = (int) ((r + m) * 255); + int gi = (int) ((g + m) * 255); + int bi = (int) ((b + m) * 255); + + return (ri << 16) | (gi << 8) | bi; +} + +llong_t get_capacity() +{ + return get_number("/sys/class/power_supply/BAT0/capacity"); +} + +llong_t get_energy_full() +{ + return get_number("/sys/class/power_supply/BAT0/energy_full"); +} + +llong_t get_energy_now() +{ + return get_number("/sys/class/power_supply/BAT0/energy_now"); +} + +llong_t get_ac_online() +{ + return get_number("/sys/class/power_supply/AC/online"); +} + +llong_t get_power() +{ + return get_number("/sys/class/power_supply/BAT0/power_now"); +} + +int get_status(char* buf, size_t size) +{ + int fd; + fd = open("/sys/class/power_supply/BAT0/status", O_RDONLY); + + if (fd < 0) { + return 1; + } + + size_t count = read(fd, buf, size); + close(fd); + + if (count < 0 || count > size - 1) { + return 1; + } + + buf[count] = 0; + --count; + while (count > 0 && buf[count] == '\n') { + buf[count --] = 0; + } + + return 0; +} + +uint32_t percentage_to_color(int percentage) +{ + int h = 135 * percentage / 100; + int s = 81; + int v = 76; + + return hsv_to_rgb(h, s, v); +} + +void get_time_left(char* buf, size_t sz, llong_t energy, llong_t power) +{ + llong_t minutes_left = energy * 60 / power; + + llong_t hours = minutes_left / 60; + llong_t minutes = minutes_left % 60; + + snprintf(buf, sz - 1, "%2lluh%2llum", hours, minutes); + buf[sz - 1] = 0; +} + +int main(int argc, char** argv) +{ + char* icon; + char timeleft[128]; + + llong_t capacity; + llong_t energy_now; + llong_t ac_online; + llong_t power; + + if ((energy_now = get_energy_now()) < 0) { + fprintf(stderr, "Unable to get current energy."); + goto fail; + } + + if ((capacity = get_capacity()) < 0) { + fprintf(stderr, "Unable to get capacity."); + goto fail; + } + + if ((ac_online = get_ac_online()) < 0) { + fprintf(stderr, "Unable to get status."); + goto fail; + } + + if ((power = get_power()) < 0) { + fprintf(stderr, "Unable toget power."); + goto fail; + } + + ac_online = !! ac_online; + + int percentage = (int) capacity; + if (percentage >= 100) { + icon = icons[10 + ac_online]; + } else { + int quintile = percentage / 20; + icon = icons[quintile + (5 * ac_online) ]; + } + + get_time_left(timeleft, sizeof(timeleft), energy_now, power); + + double dpower = power / 1000000.0; + uint32_t color = percentage_to_color(percentage); + printf("%s %d%% %2.1fW %s", color, icon, percentage, dpower, timeleft); + return 0; + +fail: + printf(""); + return 0; +} diff --git a/xmobarrc b/xmobarrc index 9c0b7f8..2893a17 100644 --- a/xmobarrc +++ b/xmobarrc @@ -1,10 +1,13 @@ Config - { font = "xft:Ubuntu Mono:size=20" - , additionalFonts = ["xft:Lato:style=bold"] -- default: [] - , borderColor = "#ffd9bf" - , border = BottomB - , borderWidth = 1 - , bgColor = "black" + { font = "xft:Monofur Nerd Font:size=15" + , additionalFonts = [ + "xft:Monofur bold Nerd Font:style=bold:size=15", + "xft:Monofur Nerd Font:size=12", + "xft:Monofur Nerd Font:size=12" ] + , borderColor = "black" + , border = FullB + , borderWidth = 2 + , bgColor = "#17171b" , fgColor = "white" , alpha = 250 -- default: 255 , position = TopSize L 100 50 @@ -19,7 +22,7 @@ Config , overrideRedirect = False -- default: True , sepChar = "%" , alignSep = "}{" - , template = " %StdinReader% }{ ──── %battery% ──── %date% " + , template = " %date% │ %StdinReader% }%time%{ %cpu% │ %KBDU% %uname% │ %bat% " , commands = [ Run Battery [ "--template" , "" , "--Low" , "10" -- units: % @@ -30,12 +33,30 @@ Config , "--" -- battery specific options -- discharging status - , "-o" , "% - " + , "-o" , " (%, )" -- AC "on" status - , "-O" , "Charging" + , "-O" , " (%)" -- charged status - , "-i" , "Charged" + , "-i" , "" ] 50, - Run StdinReader + Run StdinReader, + Run Date "%H:%M:%S" "time" 10, + Run Date "%m/%d" "date" 10, + Run Cpu ["-t", "%", "-L","3","-H","50","--normal","green","--high","red"] 10, + Run WeatherX "KBDU" + [ ("clear", "") + , ("sunny", "") + , ("mostly clear", "") + , ("mostly sunny", "") + , ("partly sunny", "") + , ("fair", "🌑") + , ("cloudy","摒") + , ("overcast","") + , ("partly cloudy", "杖") + , ("mostly cloudy", "") + , ("considerable cloudiness", "ﭽ")] + ["--template", " °F"] 360000, + Run Com "uname" ["-r"] "uname" 0, + Run Com ".xmonad/xmobar-battery" [] "bat" 20 ] } -- cgit