2022-11-09 14:24:48 +00:00
|
|
|
|
from __future__ import annotations
|
|
|
|
|
from typing import Callable, Any
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
import time
|
|
|
|
|
import logging
|
|
|
|
|
import psutil
|
|
|
|
|
import subprocess
|
2022-11-23 16:55:05 +00:00
|
|
|
|
import dbus
|
2022-11-23 16:54:34 +00:00
|
|
|
|
import docker
|
|
|
|
|
|
|
|
|
|
docker_client = docker.from_env()
|
2022-11-09 14:24:48 +00:00
|
|
|
|
|
|
|
|
|
from newm.layout import Layout
|
|
|
|
|
from newm.helper import BacklightManager, WobRunner, PaCtl
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
2022-11-11 13:42:31 +00:00
|
|
|
|
debug_windows = False
|
|
|
|
|
|
2022-11-09 14:24:48 +00:00
|
|
|
|
|
|
|
|
|
def run_shell(command: str):
|
|
|
|
|
"""Run a shell command asynchronously
|
|
|
|
|
If the shell command doesn’t already end with "&", add it.
|
|
|
|
|
"""
|
|
|
|
|
if not command.endswith("&"):
|
|
|
|
|
command = command + " &"
|
|
|
|
|
os.system(command)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def command_is_running(command: str):
|
|
|
|
|
for proc in psutil.process_iter():
|
|
|
|
|
try:
|
|
|
|
|
cmdline = proc.cmdline()
|
|
|
|
|
except psutil.NoSuchProcess:
|
|
|
|
|
continue
|
|
|
|
|
if command in cmdline[0]:
|
|
|
|
|
return True
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def on_reconfigure():
|
|
|
|
|
run_shell('notify-send newm "Reloaded config"')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def on_startup():
|
|
|
|
|
run_shell("mpc stop")
|
|
|
|
|
run_shell("dunst")
|
|
|
|
|
run_shell("pactl load-module module-switch-on-connect")
|
|
|
|
|
run_shell("xfce-polkit")
|
|
|
|
|
run_shell("kdeconnectd")
|
|
|
|
|
os.system(
|
|
|
|
|
"systemctl --user import-environment DISPLAY \
|
|
|
|
|
WAYLAND_DISPLAY XDG_CURRENT_DESKTOP"
|
|
|
|
|
)
|
|
|
|
|
os.system(
|
|
|
|
|
"dbus-update-activation-environment && \
|
|
|
|
|
dbus-update-activation-environment --systemd DISPLAY \
|
|
|
|
|
WAYLAND_DISPLAY XDG_CURRENT_DESKTOP"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
background = {
|
|
|
|
|
"path": os.environ["HOME"] + "/Pictures/Wallpapers/1561879941868.jpg",
|
|
|
|
|
"anim": True,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pywm = {
|
|
|
|
|
"xkb_layout": "fr",
|
|
|
|
|
"xkb_variant": "bepo_afnor",
|
|
|
|
|
"xkb_options": "caps:ctrl_modifier",
|
|
|
|
|
"enable_xwayland": True,
|
|
|
|
|
"natural_scroll": False,
|
|
|
|
|
"focus_follows_mouse": True,
|
|
|
|
|
"encourage_csd": False,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
outputs = [
|
|
|
|
|
{"name": "eDP-1"},
|
|
|
|
|
{"name": "HDMI-A-1", "pos_x": -2560, "pos_y": 0, "width": 2560, "height": 1080},
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
wob_runner = WobRunner("wob -a bottom -M 100")
|
|
|
|
|
backlight_manager = BacklightManager(anim_time=1.0, bar_display=wob_runner)
|
|
|
|
|
kbdlight_manager = BacklightManager(
|
|
|
|
|
args="--device='*::kbd_backlight'", anim_time=1.0, bar_display=wob_runner
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def synchronous_update() -> None:
|
|
|
|
|
backlight_manager.update()
|
|
|
|
|
kbdlight_manager.update()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def rules(m_view):
|
|
|
|
|
blur_apps = ("kitty", "wofi", "emacsclient", "emacs")
|
2022-11-21 10:35:33 +00:00
|
|
|
|
float_apps = ("Rofi", "xfce-polkit")
|
2022-11-11 13:42:58 +00:00
|
|
|
|
nonfloat_apps = ("discord",)
|
2022-11-11 13:42:31 +00:00
|
|
|
|
if debug_windows:
|
|
|
|
|
with open("/tmp/newm_windows.txt", "a", encoding="utf-8") as file:
|
|
|
|
|
file.write(str(m_view.app_id))
|
|
|
|
|
file.write("\n")
|
2022-11-09 14:24:48 +00:00
|
|
|
|
m_rules = {}
|
|
|
|
|
if m_view.app_id in blur_apps:
|
|
|
|
|
m_rules.update({"blur": {"radius": 6, "passes": 2}})
|
|
|
|
|
if m_view.app_id in float_apps:
|
|
|
|
|
m_rules.update({"float": True})
|
2022-11-11 13:42:58 +00:00
|
|
|
|
if m_view.app_id in nonfloat_apps:
|
|
|
|
|
m_rules.update({"float": False})
|
2022-11-09 14:24:48 +00:00
|
|
|
|
return m_rules
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pactl = PaCtl(0, wob_runner)
|
|
|
|
|
|
|
|
|
|
focus = {
|
|
|
|
|
"animate_on_change": True,
|
|
|
|
|
"distance": 4,
|
|
|
|
|
"width": 2,
|
|
|
|
|
"color": "#5E81ACDD",
|
|
|
|
|
"anim_time": 0.1,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
view = {
|
|
|
|
|
"corner_radius": 8,
|
|
|
|
|
"padding": 10,
|
|
|
|
|
"rules": rules,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
energy = {"idle_times": [5 * 60, 30 * 60, 24 * 60 * 60]}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
leader: str = "L-Spc "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def key_bindings(layout: Layout) -> list[tuple[str, Callable[[], Any]]]:
|
|
|
|
|
return [
|
|
|
|
|
("L-Return", lambda: os.system("kitty &")),
|
|
|
|
|
(leader + "a r", lambda: run_shell("wofi --show drun")),
|
|
|
|
|
(leader + "a b", lambda: run_shell("firefox")),
|
|
|
|
|
(leader + "a d", lambda: run_shell("discord")),
|
|
|
|
|
(leader + "a e", lambda: run_shell("emacsclient -c")),
|
2022-11-21 16:20:09 +00:00
|
|
|
|
(leader + "l", layout.ensure_locked),
|
2022-11-09 14:24:48 +00:00
|
|
|
|
(leader + "w f", layout.toggle_fullscreen),
|
|
|
|
|
(leader + "w v", layout.toggle_focused_view_floating),
|
|
|
|
|
(leader + "w +", lambda: layout.basic_scale(1)),
|
|
|
|
|
(leader + "w -", lambda: layout.basic_scale(-1)),
|
2022-11-21 10:35:55 +00:00
|
|
|
|
("L-o", layout.move_workspace),
|
|
|
|
|
("L-O", layout.move_workspace),
|
2022-11-09 14:24:48 +00:00
|
|
|
|
(leader + "w c", lambda: layout.move(-1, 0)),
|
|
|
|
|
(leader + "w t", lambda: layout.move(0, 1)),
|
|
|
|
|
(leader + "w s", lambda: layout.move(0, -1)),
|
|
|
|
|
(leader + "w r", lambda: layout.move(1, 0)),
|
|
|
|
|
(leader + "w n", lambda: layout.move_in_stack(1)),
|
|
|
|
|
("L-Tab", lambda: layout.move_in_stack(1)),
|
|
|
|
|
(leader + "w p", lambda: layout.move_in_stack(-1)),
|
|
|
|
|
(leader + "w C", lambda: layout.move_focused_view(-1, 0)),
|
|
|
|
|
(leader + "w T", lambda: layout.move_focused_view(0, 1)),
|
|
|
|
|
(leader + "w S", lambda: layout.move_focused_view(0, -1)),
|
|
|
|
|
(leader + "w R", lambda: layout.move_focused_view(1, 0)),
|
|
|
|
|
(leader + "b d", layout.close_focused_view),
|
|
|
|
|
(leader + "q l", lambda: layout.ensure_locked(dim=False)),
|
|
|
|
|
(leader + "q q", layout.terminate),
|
|
|
|
|
(leader + "u", layout.update_config),
|
|
|
|
|
("L-c", lambda: layout.move(-1, 0)),
|
|
|
|
|
("L-t", lambda: layout.move(0, 1)),
|
|
|
|
|
("L-s", lambda: layout.move(0, -1)),
|
|
|
|
|
("L-r", lambda: layout.move(1, 0)),
|
|
|
|
|
("L-plus", lambda: layout.basic_scale(-1)),
|
|
|
|
|
("L-minus", lambda: layout.basic_scale(1)),
|
|
|
|
|
("L-C", lambda: layout.move_focused_view(-1, 0)),
|
|
|
|
|
("L-T", lambda: layout.move_focused_view(0, 1)),
|
|
|
|
|
("L-S", lambda: layout.move_focused_view(0, -1)),
|
|
|
|
|
("L-R", lambda: layout.move_focused_view(1, 0)),
|
|
|
|
|
(leader + "w r c", lambda: layout.resize_focused_view(-1, 0)),
|
|
|
|
|
(leader + "w r t", lambda: layout.resize_focused_view(0, 1)),
|
|
|
|
|
(leader + "w r s", lambda: layout.resize_focused_view(0, -1)),
|
|
|
|
|
(leader + "w r r", lambda: layout.resize_focused_view(1, 0)),
|
2022-11-21 10:35:33 +00:00
|
|
|
|
("L-", lambda: layout.toggle_overview(only_active_workspace=True)),
|
2022-11-09 14:24:48 +00:00
|
|
|
|
(
|
|
|
|
|
"XF86MonBrightnessUp",
|
|
|
|
|
lambda: backlight_manager.set(backlight_manager.get() + 0.1),
|
|
|
|
|
),
|
|
|
|
|
(
|
|
|
|
|
"XF86MonBrightnessDown",
|
|
|
|
|
lambda: backlight_manager.set(backlight_manager.get() - 0.1),
|
|
|
|
|
),
|
|
|
|
|
(
|
|
|
|
|
"XF86KbdBrightnessUp",
|
|
|
|
|
lambda: kbdlight_manager.set(kbdlight_manager.get() + 0.1),
|
|
|
|
|
),
|
|
|
|
|
(
|
|
|
|
|
"XF86KbdBrightnessDown",
|
|
|
|
|
lambda: kbdlight_manager.set(kbdlight_manager.get() - 0.1),
|
|
|
|
|
),
|
|
|
|
|
("XF86AudioRaiseVolume", lambda: pactl.volume_adj(5)),
|
|
|
|
|
("XF86AudioLowerVolume", lambda: pactl.volume_adj(-5)),
|
|
|
|
|
("XF86AudioMute", pactl.mute),
|
2022-11-23 16:52:43 +00:00
|
|
|
|
("Print", lambda: run_shell("env XDG_CURRENT_DESKTOP=Sway flameshot gui")),
|
2022-11-09 14:24:48 +00:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
2022-11-23 16:54:04 +00:00
|
|
|
|
battery_icons = {
|
|
|
|
|
100: {True: "", False: ""},
|
|
|
|
|
90: {True: "", False: ""},
|
|
|
|
|
80: {True: "", False: ""},
|
|
|
|
|
70: {True: "", False: ""},
|
|
|
|
|
60: {True: "", False: ""},
|
|
|
|
|
50: {True: "", False: ""},
|
|
|
|
|
40: {True: "", False: ""},
|
|
|
|
|
30: {True: "", False: ""},
|
|
|
|
|
20: {True: "", False: ""},
|
|
|
|
|
10: {True: "", False: ""},
|
|
|
|
|
0: {True: "", False: ""},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-11-09 14:24:48 +00:00
|
|
|
|
def battery_status() -> str:
|
|
|
|
|
battery = psutil.sensors_battery()
|
|
|
|
|
percent = format(battery.percent, ".1f")
|
|
|
|
|
minutes = battery.secsleft // 60
|
|
|
|
|
remaining = "{0:0>2}:{1:0>2}".format(minutes // 60, minutes % 60)
|
2022-11-23 16:54:04 +00:00
|
|
|
|
icon = battery_icons[(int(float(percent)) // 10) * 10][battery.power_plugged]
|
|
|
|
|
return f"{icon} {percent}% ({remaining})"
|
2022-11-09 14:24:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def unread_emails() -> str:
|
|
|
|
|
unread = subprocess.run(
|
|
|
|
|
["mu", "find", "flag:unread AND (maildir:/Inbox OR maildir:/Junk)"],
|
|
|
|
|
capture_output=True,
|
|
|
|
|
text=True,
|
|
|
|
|
check=True,
|
|
|
|
|
).stdout
|
|
|
|
|
nbr_unread: int = len(str(unread).split("\n"))
|
2022-11-23 16:54:04 +00:00
|
|
|
|
return f" {nbr_unread}"
|
2022-11-09 14:24:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def cpu_usage() -> str:
|
2022-11-23 16:54:04 +00:00
|
|
|
|
cpu: str = format(psutil.cpu_percent(interval=1), ".1f")
|
2022-11-11 13:43:14 +00:00
|
|
|
|
return f" {cpu}%"
|
2022-11-09 14:24:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def mem_usage() -> str:
|
|
|
|
|
mem: str = format(psutil.virtual_memory().percent, ".1f")
|
2022-11-23 16:56:23 +00:00
|
|
|
|
return f" {mem}%"
|
2022-11-09 14:24:48 +00:00
|
|
|
|
|
|
|
|
|
|
2022-11-23 16:55:05 +00:00
|
|
|
|
def get_bluetooth_devices() -> str:
|
|
|
|
|
import xml.etree.ElementTree as ET
|
|
|
|
|
|
|
|
|
|
bus = dbus.SystemBus()
|
|
|
|
|
service_name = "org.bluez"
|
|
|
|
|
|
|
|
|
|
# Verify if bluetooth is turned on
|
|
|
|
|
proxy = bus.get_object(service_name, "/org/bluez/hci0")
|
|
|
|
|
props = dbus.Interface(proxy, "org.freedesktop.DBus.Properties")
|
|
|
|
|
if not props.Get("org.bluez.Adapter1", "Powered"):
|
|
|
|
|
return ""
|
|
|
|
|
|
|
|
|
|
# Grab all known devices
|
|
|
|
|
bt_intro_iface = dbus.Interface(proxy, "org.freedesktop.DBus.Introspectable")
|
|
|
|
|
bt_intro = str(bt_intro_iface.Introspect())
|
|
|
|
|
root_node = ET.fromstring(bt_intro)
|
|
|
|
|
known_devices = [n.get("name") for n in root_node.findall("node")]
|
|
|
|
|
|
|
|
|
|
# Check if all devices are connected
|
|
|
|
|
counter = 0
|
|
|
|
|
for device in known_devices:
|
|
|
|
|
object_path = f"/org/bluez/hci0/{device}"
|
|
|
|
|
proxy = bus.get_object(service_name, object_path)
|
|
|
|
|
props = dbus.Interface(proxy, "org.freedesktop.DBus.Properties")
|
|
|
|
|
if props.Get("org.bluez.Device1", "Connected"):
|
|
|
|
|
counter = counter + 1
|
|
|
|
|
return f" {counter}"
|
|
|
|
|
|
|
|
|
|
|
2022-11-23 16:55:55 +00:00
|
|
|
|
def get_wifi_name(iface) -> str:
|
|
|
|
|
bus = dbus.SystemBus()
|
|
|
|
|
service_name = "org.freedesktop.NetworkManager"
|
|
|
|
|
proxy = bus.get_object(service_name, "/org/freedesktop/NetworkManager/Settings")
|
|
|
|
|
|
|
|
|
|
# Get the device object path based on interface name
|
|
|
|
|
proxy = bus.get_object(service_name, "/org/freedesktop/NetworkManager")
|
|
|
|
|
nm = dbus.Interface(proxy, "org.freedesktop.NetworkManager")
|
|
|
|
|
devpath = nm.GetDeviceByIpIface(iface)
|
|
|
|
|
|
|
|
|
|
# Get a proxy to the wifi device and get the active access point's object path
|
|
|
|
|
proxy = bus.get_object(service_name, devpath)
|
|
|
|
|
props = dbus.Interface(proxy, "org.freedesktop.DBus.Properties")
|
|
|
|
|
active_ap_path = props.Get(
|
|
|
|
|
"org.freedesktop.NetworkManager.Device.Wireless", "ActiveAccessPoint"
|
|
|
|
|
)
|
|
|
|
|
if active_ap_path == "/":
|
|
|
|
|
return ""
|
|
|
|
|
|
|
|
|
|
# Get the active access point's SSID and BSSID
|
|
|
|
|
ap_proxy = bus.get_object(service_name, active_ap_path)
|
|
|
|
|
ap_props = dbus.Interface(ap_proxy, "org.freedesktop.DBus.Properties")
|
|
|
|
|
raw_ssid = ap_props.Get("org.freedesktop.NetworkManager.AccessPoint", "Ssid")
|
|
|
|
|
ssid = b"".join([bytes([v]) for v in raw_ssid]).decode("utf-8")
|
|
|
|
|
return ssid
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_local_address(wifi_iface, eth_iface) -> str:
|
|
|
|
|
if_addrs = psutil.net_if_addrs()
|
|
|
|
|
wifi = if_addrs.get(wifi_iface)
|
|
|
|
|
eth = if_addrs.get(eth_iface)
|
|
|
|
|
wifi_addr = wifi[0].address if wifi else ""
|
|
|
|
|
eth_addr = eth[0].address if eth else ""
|
|
|
|
|
wifi_addr = wifi_addr if not ":" in wifi_addr else ""
|
|
|
|
|
eth_addr = eth_addr if not ":" in eth_addr else ""
|
|
|
|
|
return wifi_addr or eth_addr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_network() -> str:
|
|
|
|
|
wifi_interface = "wlp8s0"
|
|
|
|
|
eth_interface = "enp9s0f1"
|
|
|
|
|
network_name = get_wifi_name(wifi_interface)
|
|
|
|
|
network_name = f" {network_name}" if network_name else " eth"
|
|
|
|
|
addr = get_local_address(wifi_interface, eth_interface)
|
|
|
|
|
return f"{network_name} ({addr})" if addr else "睊 disconnected"
|
|
|
|
|
|
|
|
|
|
|
2022-11-23 16:55:35 +00:00
|
|
|
|
def get_currently_playing():
|
|
|
|
|
bus = dbus.SessionBus()
|
|
|
|
|
service_name = "org.mpris.MediaPlayer2.playerctld"
|
|
|
|
|
service_props = "org.mpris.MediaPlayer2.Player"
|
|
|
|
|
proxy = bus.get_object(service_name, "/org/mpris/MediaPlayer2")
|
|
|
|
|
props = dbus.Interface(proxy, "org.freedesktop.DBus.Properties")
|
|
|
|
|
metadata = props.Get(service_props, "Metadata")
|
|
|
|
|
status = str(props.Get(service_props, "PlaybackStatus"))
|
|
|
|
|
if status != "Playing":
|
|
|
|
|
return ""
|
|
|
|
|
artist = ", ".join(metadata.get("xesam:artist"))
|
|
|
|
|
title = metadata.get("xesam:title")
|
|
|
|
|
return f" {artist} — {title}"
|
|
|
|
|
|
|
|
|
|
|
2022-11-23 16:54:34 +00:00
|
|
|
|
def display_docker() -> str:
|
|
|
|
|
containers = docker_client.containers.list(sparse=True)
|
|
|
|
|
return f" {len(containers)}"
|
|
|
|
|
|
|
|
|
|
|
2022-11-09 14:24:48 +00:00
|
|
|
|
def get_time() -> str:
|
|
|
|
|
return time.strftime("%a %Y-%m-%d %X")
|
|
|
|
|
|
|
|
|
|
|
2022-11-23 16:56:23 +00:00
|
|
|
|
def bar_text() -> str:
|
|
|
|
|
return " | ".join(
|
|
|
|
|
[
|
|
|
|
|
f
|
|
|
|
|
for f in [
|
|
|
|
|
get_currently_playing(),
|
|
|
|
|
get_network(),
|
|
|
|
|
display_docker(),
|
|
|
|
|
get_bluetooth_devices(),
|
|
|
|
|
unread_emails(),
|
|
|
|
|
cpu_usage(),
|
|
|
|
|
mem_usage(),
|
|
|
|
|
get_time(),
|
|
|
|
|
battery_status(),
|
|
|
|
|
]
|
|
|
|
|
if f
|
|
|
|
|
]
|
|
|
|
|
)
|
2022-11-09 14:24:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def max_width(strings: list[str]) -> int:
|
|
|
|
|
r_max_width: int = 0
|
|
|
|
|
for s in strings:
|
|
|
|
|
if len(s) > r_max_width:
|
|
|
|
|
r_max_width = len(s)
|
|
|
|
|
return r_max_width
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
panels = {
|
|
|
|
|
"lock": {
|
|
|
|
|
"cmd": "kitty -e newm-panel-basic lock",
|
|
|
|
|
},
|
|
|
|
|
"launcher": {"cmd": "kitty -e newm-panel-basic launcher"},
|
|
|
|
|
"top_bar": {
|
|
|
|
|
"native": {
|
2022-11-11 13:43:14 +00:00
|
|
|
|
"font": "JetBrainsMono Nerd Font",
|
2022-11-09 14:24:48 +00:00
|
|
|
|
"enabled": True,
|
|
|
|
|
"texts": lambda: [
|
2022-11-23 16:56:23 +00:00
|
|
|
|
bar_text(),
|
2022-11-09 14:24:48 +00:00
|
|
|
|
],
|
2022-11-11 13:43:14 +00:00
|
|
|
|
},
|
2022-11-09 14:24:48 +00:00
|
|
|
|
},
|
|
|
|
|
"bottom_bar": {
|
|
|
|
|
"native": {
|
|
|
|
|
"enabled": False,
|
|
|
|
|
"texts": lambda: ["newm", "powered by pywm"],
|
|
|
|
|
"color": (0.5, 0.5, 0.5, 0.1),
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
energy = {"idle_callback": backlight_manager.callback}
|