Multiple changes:
- Wrap topbar in own class for customization purposes. - Add stub methods for popup creation - Add method and shortcut to display the WM class in a notification - Add keyboard backlight control module, to control my keyboard backlight based on which app has focus - Add debugging bar to easily display debugging messages - Add display brightness shortcut keys - Move some global vars into the theme class - Lower update intervals for widgets to lower CPU usage - Add debugging configuration for use with Xephyr
This commit is contained in:
		
							parent
							
								
									19de16c8b7
								
							
						
					
					
						commit
						b9224b667d
					
				
					 8 changed files with 755 additions and 30 deletions
				
			
		
							
								
								
									
										0
									
								
								kuro/utils/__init__.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								kuro/utils/__init__.py
									
										
									
									
									
										Normal file
									
								
							
							
								
								
									
										491
									
								
								kuro/utils/general.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										491
									
								
								kuro/utils/general.py
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,491 @@
 | 
			
		|||
import os
 | 
			
		||||
import re
 | 
			
		||||
import subprocess
 | 
			
		||||
 | 
			
		||||
import cairocffi
 | 
			
		||||
import notify2
 | 
			
		||||
from libqtile import widget, bar
 | 
			
		||||
from libqtile.bar import Bar
 | 
			
		||||
from libqtile.utils import catch_exception_and_warn, UnixCommandNotFound
 | 
			
		||||
from libqtile.widget import base
 | 
			
		||||
from libqtile.widget.battery import default_icon_path
 | 
			
		||||
from libqtile.widget.check_updates import CheckUpdates
 | 
			
		||||
from libqtile.widget.image import Image
 | 
			
		||||
from libqtile.widget.sensors import ThermalSensor
 | 
			
		||||
from libqtile.widget.volume import Volume
 | 
			
		||||
from libqtile.widget.wlan import get_status
 | 
			
		||||
from libqtile.log_utils import logger
 | 
			
		||||
from notify2 import Notification, URGENCY_NORMAL
 | 
			
		||||
 | 
			
		||||
notify2.init("QTileWM")
 | 
			
		||||
 | 
			
		||||
BUTTON_LEFT = 1
 | 
			
		||||
BUTTON_MIDDLE = 2
 | 
			
		||||
BUTTON_RIGHT = 3
 | 
			
		||||
BUTTON_SCROLL_UP = 4
 | 
			
		||||
BUTTON_SCROLL_DOWN = 5
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def is_running(process):
 | 
			
		||||
    s = subprocess.Popen(["ps", "axuw"], stdout=subprocess.PIPE)
 | 
			
		||||
    for x in s.stdout:
 | 
			
		||||
        if re.search(process, x.decode('utf-8')):
 | 
			
		||||
            return True
 | 
			
		||||
    return False
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def execute(process):
 | 
			
		||||
    return subprocess.Popen(process.split())
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def execute_once(process):
 | 
			
		||||
    if not is_running(process):
 | 
			
		||||
        return subprocess.Popen(process.split())
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def get_screen_count():
 | 
			
		||||
    try:
 | 
			
		||||
        output = subprocess.check_output("xrandr -q".split()).decode('utf-8')
 | 
			
		||||
        output = [x for x in output.split("\n") if " connected" in x]
 | 
			
		||||
    except subprocess.CalledProcessError:
 | 
			
		||||
        return 1
 | 
			
		||||
 | 
			
		||||
    if output:
 | 
			
		||||
        return len(output)
 | 
			
		||||
    else:
 | 
			
		||||
        return 1
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def bar_separator(config):
 | 
			
		||||
    return widget.Sep(foreground=config.get('colour_spacer_background', '#777777'),
 | 
			
		||||
                      linewidth=config.get('width_spacer', 1),
 | 
			
		||||
                      padding=config.get('padding_spacer', 4),
 | 
			
		||||
                      )
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def notify(title, content, urgency=URGENCY_NORMAL, timeout=5000, image=None):
 | 
			
		||||
    if image is not None:
 | 
			
		||||
        notification = Notification(
 | 
			
		||||
            summary=title, message=content,
 | 
			
		||||
            icon=image
 | 
			
		||||
        )
 | 
			
		||||
    else:
 | 
			
		||||
        notification = Notification(
 | 
			
		||||
            summary=title, message=content
 | 
			
		||||
        )
 | 
			
		||||
    notification.set_timeout(timeout)
 | 
			
		||||
    notification.set_urgency(urgency)
 | 
			
		||||
 | 
			
		||||
    return notification.show()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def spawn_popup(qtile, x, y, text):
 | 
			
		||||
    # Create textwidget for in window
 | 
			
		||||
    pass
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    # window.Internal.create(
 | 
			
		||||
    #     qtile, x, y, width, height, opacity=1
 | 
			
		||||
    # )
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def display_wm_class(qtile):
 | 
			
		||||
    window = qtile.currentWindow if qtile else None
 | 
			
		||||
 | 
			
		||||
    if window:
 | 
			
		||||
        wm_class = window.window.get_wm_class() or None
 | 
			
		||||
        name = window.name
 | 
			
		||||
 | 
			
		||||
        if wm_class:
 | 
			
		||||
            notify(title="WM_Class of {}".format(name),
 | 
			
		||||
                   content="{}".format(wm_class),
 | 
			
		||||
                   urgency=notify2.URGENCY_CRITICAL)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def bluetooth_audio_sink():
 | 
			
		||||
    try:
 | 
			
		||||
        output = subprocess.check_output("pamixer --list-sinks".split()).decode("utf-8")
 | 
			
		||||
        output = [x for x in output.split('\n') if "blue" in x.lower()]
 | 
			
		||||
    except subprocess.CalledProcessError:
 | 
			
		||||
        return -1
 | 
			
		||||
 | 
			
		||||
    sink = -1
 | 
			
		||||
    try:
 | 
			
		||||
        sink = int(output[0].split()[0])
 | 
			
		||||
    except IndexError:
 | 
			
		||||
        pass
 | 
			
		||||
    except AttributeError:
 | 
			
		||||
        pass
 | 
			
		||||
    except ValueError:
 | 
			
		||||
        pass
 | 
			
		||||
 | 
			
		||||
    return sink
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def bluetooth_audio_connected():
 | 
			
		||||
    return bluetooth_audio_sink() != -1
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class KuroTopBar(Bar):
 | 
			
		||||
    def __init__(self, theme, widgets, size, **config):
 | 
			
		||||
        self.theme = theme
 | 
			
		||||
        super(KuroTopBar, self).__init__(widgets, size, **config)
 | 
			
		||||
 | 
			
		||||
    def _configure(self, qtile, screen):
 | 
			
		||||
        super(KuroTopBar, self)._configure(qtile, screen)
 | 
			
		||||
        self.window.handle_EnterNotify = self.handle_enter_notify
 | 
			
		||||
        self.window.handle_LeaveNotify = self.handle_leave_notify
 | 
			
		||||
 | 
			
		||||
    def handle_enter_notify(self, e):
 | 
			
		||||
        # self.theme.log_debug("Bar HandleEnterNotify")
 | 
			
		||||
        #
 | 
			
		||||
        # self.window.opacity = Config.get('bar_hover_opacity', 1.0)
 | 
			
		||||
        # print("Bar Hover Enter")
 | 
			
		||||
        #
 | 
			
		||||
        # try:
 | 
			
		||||
        #     hovered_widget = [x for x in self.widgets if (x.offsetx + x.width) >= e.event_x][0]
 | 
			
		||||
        # except IndexError:
 | 
			
		||||
        #     hovered_widget = None
 | 
			
		||||
        #
 | 
			
		||||
        # self.theme.log_debug("Hovered over {}".format(hovered_widget))
 | 
			
		||||
        #
 | 
			
		||||
        # if hasattr(hovered_widget, "handle_hover_enter"):
 | 
			
		||||
        #     hovered_widget.handle_hover_enter(e)
 | 
			
		||||
 | 
			
		||||
        self.draw()
 | 
			
		||||
 | 
			
		||||
    def handle_leave_notify(self, e):
 | 
			
		||||
        # self.theme.log_debug("Bar HandleLeaveNotify")
 | 
			
		||||
        #
 | 
			
		||||
        # self.window.opacity = Config.get('bar_opacity', 1.0)
 | 
			
		||||
        # print("Bar Hover Leave")
 | 
			
		||||
        #
 | 
			
		||||
        # try:
 | 
			
		||||
        #     hovered_widget = [x for x in self.widgets if (x.offsetx + x.width) >= e.event_x][0]
 | 
			
		||||
        # except IndexError:
 | 
			
		||||
        #     hovered_widget = None
 | 
			
		||||
        #
 | 
			
		||||
        # self.theme.log_debug("Hovered over {}".format(hovered_widget))
 | 
			
		||||
        #
 | 
			
		||||
        # if hasattr(hovered_widget, "handle_hover_leave"):
 | 
			
		||||
        #     hovered_widget.handle_hover_leave(e)
 | 
			
		||||
 | 
			
		||||
        self.draw()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class AppLauncherIcon(Image):
 | 
			
		||||
    def button_press(self, x, y, button):
 | 
			
		||||
        if button == BUTTON_LEFT:
 | 
			
		||||
            execute("dmenu_run -i -p '»' -nb '#000000' -fn 'Noto Sans-11' -nf '#777777' -sb '#1793d0' -sf '#ffffff'")
 | 
			
		||||
 | 
			
		||||
    def handle_hover(self, event):
 | 
			
		||||
        spawn_popup(self.qtile, self.offsetx, self.offsety, "Hovered over AppLauncherIcon!")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class CheckUpdatesYaourt(CheckUpdates):
 | 
			
		||||
    def __init__(self, **config):
 | 
			
		||||
        super(CheckUpdatesYaourt, self).__init__(**config)
 | 
			
		||||
        # Override command and output with yaourt command
 | 
			
		||||
        self.cmd = "yaourt -Qua".split()
 | 
			
		||||
        self.status_cmd = "yaourt -Qua".split()
 | 
			
		||||
        self.update_cmd = "sudo yaourt -Sya".split()
 | 
			
		||||
        self.subtr = 0
 | 
			
		||||
 | 
			
		||||
    def _check_updates(self):
 | 
			
		||||
        #subprocess.check_output(self.update_cmd)
 | 
			
		||||
        res = super(CheckUpdatesYaourt, self)._check_updates()
 | 
			
		||||
        return res
 | 
			
		||||
 | 
			
		||||
    def button_press(self, x, y, button):
 | 
			
		||||
        if button == BUTTON_LEFT:
 | 
			
		||||
            output = subprocess.check_output(self.status_cmd).decode('utf-8').split('\n')
 | 
			
		||||
 | 
			
		||||
            num_updates = len(output)-1
 | 
			
		||||
            msg = "{} updates available.".format(num_updates)
 | 
			
		||||
 | 
			
		||||
            if num_updates > 0:
 | 
			
		||||
                msg += "\n\n"
 | 
			
		||||
                for x in range(min(num_updates, 9)):
 | 
			
		||||
                    msg += output[x] + "\n"
 | 
			
		||||
                if num_updates > 9:
 | 
			
		||||
                    msg += "and {} more...".format(num_updates-9)
 | 
			
		||||
 | 
			
		||||
            notify(
 | 
			
		||||
                "System updates",
 | 
			
		||||
                msg
 | 
			
		||||
            )
 | 
			
		||||
 | 
			
		||||
        elif button == BUTTON_MIDDLE and self.execute is not None:
 | 
			
		||||
            subprocess.Popen(self.execute, shell=True)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class PulseVolumeWidget(Volume):
 | 
			
		||||
 | 
			
		||||
    defaults = [
 | 
			
		||||
        ("cardid", None, "Card Id"),
 | 
			
		||||
        ("device", "default", "Device Name"),
 | 
			
		||||
        ("channel", "Master", "Channel"),
 | 
			
		||||
        ("padding", 3, "Padding left and right. Calculated if None."),
 | 
			
		||||
        ("theme_path", None, "Path of the icons"),
 | 
			
		||||
        ("update_interval", 0.2, "Update time in seconds."),
 | 
			
		||||
        ("emoji", False, "Use emoji to display volume states, only if ``theme_path`` is not set."
 | 
			
		||||
                         "The specified font needs to contain the correct unicode characters."),
 | 
			
		||||
        ("mute_command", None, "Mute command"),
 | 
			
		||||
        ("volume_up_command", None, "Volume up command"),
 | 
			
		||||
        ("volume_down_command", None, "Volume down command"),
 | 
			
		||||
        ("get_volume_command", None, "Command to get the current volume"),
 | 
			
		||||
        ("is_bluetooth_icon", False, "Is this icon for a Bluetooth Audio device?"),
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
    _old_length = 0
 | 
			
		||||
 | 
			
		||||
    def __init__(self, **config):
 | 
			
		||||
        super(PulseVolumeWidget, self).__init__(**config)
 | 
			
		||||
        self._old_length = self._length
 | 
			
		||||
 | 
			
		||||
        # Augment commands with bluetooth sink ID if this is a bluetooth icon
 | 
			
		||||
        if self.is_bluetooth_icon and bluetooth_audio_connected():
 | 
			
		||||
            bsink = bluetooth_audio_sink()
 | 
			
		||||
            self.mute_command = " ".join(self._user_config['mute_command']).format(bsink=bsink).split()
 | 
			
		||||
            self.volume_up_command = " ".join(self._user_config['volume_up_command']).format(bsink=bsink).split()
 | 
			
		||||
            self.volume_down_command = " ".join(self._user_config['volume_down_command']).format(bsink=bsink).split()
 | 
			
		||||
            self.get_volume_command = " ".join(self._user_config['get_volume_command']).format(bsink=bsink).split()
 | 
			
		||||
            logger.info("Updated bluetooth commands with bluetooth sink {}".format(bsink))
 | 
			
		||||
            self._length = self._old_length
 | 
			
		||||
            self.commands_need_reset = False
 | 
			
		||||
        elif self.is_bluetooth_icon:
 | 
			
		||||
            self.commands_need_reset = True
 | 
			
		||||
        else:
 | 
			
		||||
            self.commands_need_reset = False
 | 
			
		||||
 | 
			
		||||
        self._old_length = self._length
 | 
			
		||||
 | 
			
		||||
    def reset_bluetooth_commands(self):
 | 
			
		||||
        if self.is_bluetooth_icon and bluetooth_audio_connected():
 | 
			
		||||
            bsink = 0 if bluetooth_audio_sink() == -1 else bluetooth_audio_sink()
 | 
			
		||||
            self.mute_command = " ".join(self._user_config['mute_command']).format(bsink=bsink).split()
 | 
			
		||||
            self.volume_up_command = " ".join(self._user_config['volume_up_command']).format(bsink=bsink).split()
 | 
			
		||||
            self.volume_down_command = " ".join(self._user_config['volume_down_command']).format(bsink=bsink).split()
 | 
			
		||||
            self.get_volume_command = " ".join(self._user_config['get_volume_command']).format(bsink=bsink).split()
 | 
			
		||||
            logger.info("Updated bluetooth commands with bluetooth sink {}".format(bsink))
 | 
			
		||||
            self._length = self._old_length
 | 
			
		||||
            self.commands_need_reset = False
 | 
			
		||||
 | 
			
		||||
    def get_volume(self):
 | 
			
		||||
        try:
 | 
			
		||||
            get_volume_cmd = "echo 0".split()
 | 
			
		||||
 | 
			
		||||
            if self.get_volume_command:
 | 
			
		||||
                if self.is_bluetooth_icon and bluetooth_audio_sink() == -1:
 | 
			
		||||
                    pass
 | 
			
		||||
                else:
 | 
			
		||||
                    get_volume_cmd = self.get_volume_command
 | 
			
		||||
 | 
			
		||||
            mixer_out = self.call_process(get_volume_cmd)
 | 
			
		||||
        except subprocess.CalledProcessError:
 | 
			
		||||
            return -1
 | 
			
		||||
 | 
			
		||||
        try:
 | 
			
		||||
            return int(mixer_out.strip())
 | 
			
		||||
        except ValueError:
 | 
			
		||||
            return -1
 | 
			
		||||
 | 
			
		||||
    def _update_drawer(self):
 | 
			
		||||
        super(PulseVolumeWidget, self)._update_drawer()
 | 
			
		||||
        self.text = ""
 | 
			
		||||
        if self.is_bluetooth_icon and not bluetooth_audio_connected():
 | 
			
		||||
            self._length = 0
 | 
			
		||||
 | 
			
		||||
    def draw(self):
 | 
			
		||||
        if self.is_bluetooth_icon and not bluetooth_audio_connected():
 | 
			
		||||
            if not self.commands_need_reset:
 | 
			
		||||
                logger.info("Bluetooth device disconnected. Hiding bluetooth audio mixer")
 | 
			
		||||
                self.commands_need_reset = True
 | 
			
		||||
            base._TextBox.draw(self)
 | 
			
		||||
        else:
 | 
			
		||||
            if self.commands_need_reset:
 | 
			
		||||
                self.reset_bluetooth_commands()
 | 
			
		||||
            if self.theme_path:
 | 
			
		||||
                self.drawer.draw(offsetx=self.offset, width=self.length)
 | 
			
		||||
            else:
 | 
			
		||||
                base._TextBox.draw(self)
 | 
			
		||||
 | 
			
		||||
    def button_press(self, x, y, button):
 | 
			
		||||
        if button == BUTTON_LEFT:
 | 
			
		||||
            volume = self.get_volume()
 | 
			
		||||
 | 
			
		||||
            width = 15
 | 
			
		||||
 | 
			
		||||
            if volume >= 0:
 | 
			
		||||
                volume_amount = round((volume/100)*width)
 | 
			
		||||
            else:
 | 
			
		||||
                volume_amount = 0
 | 
			
		||||
 | 
			
		||||
            msg = "[{}{}]".format(
 | 
			
		||||
                "".join(["#" for x in range(volume_amount)]),
 | 
			
		||||
                "".join(["-" for x in range(width-volume_amount)])
 | 
			
		||||
            )
 | 
			
		||||
 | 
			
		||||
            notify(
 | 
			
		||||
                "{}Volume : {}%".format("Bluetooth " if self.is_bluetooth_icon else "", volume),
 | 
			
		||||
                msg
 | 
			
		||||
            )
 | 
			
		||||
        else:
 | 
			
		||||
            super(PulseVolumeWidget, self).button_press(x, y, button)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class WifiIconWidget(base._TextBox):
 | 
			
		||||
    """WiFi connection strength indicator widget."""
 | 
			
		||||
 | 
			
		||||
    orientations = base.ORIENTATION_HORIZONTAL
 | 
			
		||||
    defaults = [
 | 
			
		||||
        ('interface', 'wlan0', 'The interface to monitor'),
 | 
			
		||||
        ('update_interval', 1, 'The update interval.'),
 | 
			
		||||
        ('theme_path', default_icon_path(), 'Path of the icons'),
 | 
			
		||||
        ('custom_icons', {}, 'dict containing key->filename icon map'),
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
    def __init__(self, **config):
 | 
			
		||||
        super(WifiIconWidget, self).__init__("WLAN", bar.CALCULATED, **config)
 | 
			
		||||
        self.add_defaults(WifiIconWidget.defaults)
 | 
			
		||||
 | 
			
		||||
        if self.theme_path:
 | 
			
		||||
            self.length_type = bar.STATIC
 | 
			
		||||
            self.length = 0
 | 
			
		||||
        self.surfaces = {}
 | 
			
		||||
        self.current_icon = 'wireless-disconnected'
 | 
			
		||||
        self.icons = dict([(x, '{0}.png'.format(x)) for x in (
 | 
			
		||||
            'wireless-disconnected',
 | 
			
		||||
            'wireless-none',
 | 
			
		||||
            'wireless-low',
 | 
			
		||||
            'wireless-medium',
 | 
			
		||||
            'wireless-high',
 | 
			
		||||
            'wireless-full',
 | 
			
		||||
        )])
 | 
			
		||||
        self.icons.update(self.custom_icons)
 | 
			
		||||
 | 
			
		||||
    def _get_info(self):
 | 
			
		||||
        try:
 | 
			
		||||
            essid, quality = get_status(self.interface)
 | 
			
		||||
            disconnected = essid is None
 | 
			
		||||
            if disconnected:
 | 
			
		||||
                return self.disconnected_message
 | 
			
		||||
 | 
			
		||||
            return {
 | 
			
		||||
                'error': False,
 | 
			
		||||
                'essid': essid,
 | 
			
		||||
                'quality': quality,
 | 
			
		||||
                'percent': (quality / 70)
 | 
			
		||||
            }
 | 
			
		||||
        except EnvironmentError:
 | 
			
		||||
            logger.error(
 | 
			
		||||
                '%s: Probably your wlan device is switched off or '
 | 
			
		||||
                ' otherwise not present in your system.',
 | 
			
		||||
                self.__class__.__name__)
 | 
			
		||||
 | 
			
		||||
        return {'error': True}
 | 
			
		||||
 | 
			
		||||
    def timer_setup(self):
 | 
			
		||||
        self.update()
 | 
			
		||||
        self.timeout_add(self.update_interval, self.timer_setup)
 | 
			
		||||
 | 
			
		||||
    def _configure(self, qtile, bar):
 | 
			
		||||
        super(WifiIconWidget, self)._configure(qtile, bar)
 | 
			
		||||
        self.setup_images()
 | 
			
		||||
 | 
			
		||||
    def _get_icon_key(self):
 | 
			
		||||
        key = 'wireless'
 | 
			
		||||
        info = self._get_info()
 | 
			
		||||
        if info is False or info.get('error'):
 | 
			
		||||
            key += '-none'
 | 
			
		||||
        elif info.get('essid') is None:
 | 
			
		||||
            key += '-disconnected'
 | 
			
		||||
        else:
 | 
			
		||||
            percent = info['percent']
 | 
			
		||||
            if percent < 0.2:
 | 
			
		||||
                key += '-low'
 | 
			
		||||
            elif percent < 0.4:
 | 
			
		||||
                key += '-medium'
 | 
			
		||||
            elif percent < 0.8:
 | 
			
		||||
                key += '-high'
 | 
			
		||||
            else:
 | 
			
		||||
                key += '-full'
 | 
			
		||||
 | 
			
		||||
        return key
 | 
			
		||||
 | 
			
		||||
    def update(self):
 | 
			
		||||
        icon = self._get_icon_key()
 | 
			
		||||
        if icon != self.current_icon:
 | 
			
		||||
            self.current_icon = icon
 | 
			
		||||
            self.draw()
 | 
			
		||||
 | 
			
		||||
    def draw(self):
 | 
			
		||||
        if self.theme_path:
 | 
			
		||||
            self.drawer.clear(self.background or self.bar.background)
 | 
			
		||||
            self.drawer.ctx.set_source(self.surfaces[self.current_icon])
 | 
			
		||||
            self.drawer.ctx.paint()
 | 
			
		||||
            self.drawer.draw(offsetx=self.offset, width=self.length)
 | 
			
		||||
        else:
 | 
			
		||||
            self.text = self.current_icon[8:]
 | 
			
		||||
            base._TextBox.draw(self)
 | 
			
		||||
 | 
			
		||||
    def setup_images(self):
 | 
			
		||||
        for key, name in self.icons.items():
 | 
			
		||||
            try:
 | 
			
		||||
                path = os.path.join(self.theme_path, name)
 | 
			
		||||
                img = cairocffi.ImageSurface.create_from_png(path)
 | 
			
		||||
            except cairocffi.Error:
 | 
			
		||||
                self.theme_path = None
 | 
			
		||||
                logger.warning('Wireless Icon switching to text mode')
 | 
			
		||||
                return
 | 
			
		||||
            input_width = img.get_width()
 | 
			
		||||
            input_height = img.get_height()
 | 
			
		||||
 | 
			
		||||
            sp = input_height / (self.bar.height - 1)
 | 
			
		||||
 | 
			
		||||
            width = input_width / sp
 | 
			
		||||
            if width > self.length:
 | 
			
		||||
                # cast to `int` only after handling all potentially-float values
 | 
			
		||||
                self.length = int(width + self.actual_padding * 2)
 | 
			
		||||
 | 
			
		||||
            imgpat = cairocffi.SurfacePattern(img)
 | 
			
		||||
 | 
			
		||||
            scaler = cairocffi.Matrix()
 | 
			
		||||
 | 
			
		||||
            scaler.scale(sp, sp)
 | 
			
		||||
            scaler.translate(self.actual_padding * -1, 0)
 | 
			
		||||
            imgpat.set_matrix(scaler)
 | 
			
		||||
 | 
			
		||||
            imgpat.set_filter(cairocffi.FILTER_BEST)
 | 
			
		||||
            self.surfaces[key] = imgpat
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class ThermalSensorWidget(ThermalSensor):
 | 
			
		||||
    defaults = [
 | 
			
		||||
        ('metric', True, 'True to use metric/C, False to use imperial/F'),
 | 
			
		||||
        ('show_tag', False, 'Show tag sensor'),
 | 
			
		||||
        ('update_interval', 2, 'Update interval in seconds'),
 | 
			
		||||
        ('tag_sensor', None,
 | 
			
		||||
            'Tag of the temperature sensor. For example: "temp1" or "Core 0"'),
 | 
			
		||||
        ('chip', None, 'Chip argument for sensors command'),
 | 
			
		||||
        (
 | 
			
		||||
            'threshold',
 | 
			
		||||
            70,
 | 
			
		||||
            'If the current temperature value is above, '
 | 
			
		||||
            'then change to foreground_alert colour'
 | 
			
		||||
        ),
 | 
			
		||||
        ('foreground_alert', 'ff0000', 'Foreground colour alert'),
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
    @catch_exception_and_warn(warning=UnixCommandNotFound, excepts=OSError)
 | 
			
		||||
    def get_temp_sensors(self):
 | 
			
		||||
        """calls the unix `sensors` command with `-f` flag if user has specified that
 | 
			
		||||
        the output should be read in Fahrenheit.
 | 
			
		||||
        """
 | 
			
		||||
        command = ["sensors", ]
 | 
			
		||||
        if self.chip:
 | 
			
		||||
            command.append(self.chip)
 | 
			
		||||
        if not self.metric:
 | 
			
		||||
            command.append("-f")
 | 
			
		||||
        sensors_out = self.call_process(command)
 | 
			
		||||
        return self._format_sensors_output(sensors_out)
 | 
			
		||||
							
								
								
									
										356
									
								
								kuro/utils/kb_backlight.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										356
									
								
								kuro/utils/kb_backlight.py
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,356 @@
 | 
			
		|||
import subprocess
 | 
			
		||||
 | 
			
		||||
# Initialize logging
 | 
			
		||||
from libqtile.log_utils import logger
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class State:
 | 
			
		||||
    ON = "on"
 | 
			
		||||
    OFF = "off"
 | 
			
		||||
    LIST = ["on", "off"]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Mode:
 | 
			
		||||
    RANDOM = "random"
 | 
			
		||||
    CUSTOM = "custom"
 | 
			
		||||
    BREATHE = "breathe"
 | 
			
		||||
    CYCLE = "cycle"
 | 
			
		||||
    WAVE = "wave"
 | 
			
		||||
    DANCE = "dance"
 | 
			
		||||
    TEMPO = "tempo"
 | 
			
		||||
    FLASH = "flash"
 | 
			
		||||
    LIST = ["random", "custom", "breathe", "cycle", "wave", "dance", "tempo", "flash"]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Brightness:
 | 
			
		||||
    LOW = 0
 | 
			
		||||
    MEDIUM = 1
 | 
			
		||||
    HIGH = 2
 | 
			
		||||
    FULL = 3
 | 
			
		||||
    LIST = [0, 1, 2, 3]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Side:
 | 
			
		||||
    LEFT = "left"
 | 
			
		||||
    MIDDLE = "middle"
 | 
			
		||||
    RIGHT = "right"
 | 
			
		||||
    ALL = "all"
 | 
			
		||||
    LIST = ["left", "middle", "right", "all"]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Color:
 | 
			
		||||
    BLACK = "black"
 | 
			
		||||
    BLUE = "blue"
 | 
			
		||||
    RED = "red"
 | 
			
		||||
    MAGENTA = "magenta"
 | 
			
		||||
    GREEN = "green"
 | 
			
		||||
    CYAN = "cyan"
 | 
			
		||||
    YELLOW = "yellow"
 | 
			
		||||
    WHITE = "white"
 | 
			
		||||
    LIST = ["black", "blue", "red", "magenta", "green", "cyan", "yellow", "white"]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def handle_focus_change(theme):
 | 
			
		||||
    qtile = theme.qtile
 | 
			
		||||
    window = qtile.currentWindow if qtile else None
 | 
			
		||||
 | 
			
		||||
    if window:
 | 
			
		||||
        wm_class = window.window.get_wm_class() or None
 | 
			
		||||
        name = window.name
 | 
			
		||||
 | 
			
		||||
        if wm_class:
 | 
			
		||||
            theme.log_info(str(wm_class))
 | 
			
		||||
 | 
			
		||||
            # Check which window we entered and do some special effects if it is a special window.
 | 
			
		||||
 | 
			
		||||
            # Make keyboard red/white (pink) while in Osu!
 | 
			
		||||
            if "osu!.exe" in wm_class[0]:
 | 
			
		||||
                BacklightController.reset_backlight(state=KeyboardState(values={
 | 
			
		||||
                    'brightness': Brightness.FULL,
 | 
			
		||||
                    'left': Color.WHITE,
 | 
			
		||||
                    'middle': Color.RED,
 | 
			
		||||
                    'right': Color.WHITE,
 | 
			
		||||
                }))
 | 
			
		||||
            elif "chromium" in wm_class[0]:
 | 
			
		||||
                BacklightController.reset_backlight(state=KeyboardState(values={
 | 
			
		||||
                    'brightness': Brightness.FULL,
 | 
			
		||||
                    'left': Color.WHITE,
 | 
			
		||||
                    'middle': Color.BLUE,
 | 
			
		||||
                    'right': Color.WHITE,
 | 
			
		||||
                }))
 | 
			
		||||
            elif "pycharm" in wm_class[1]:
 | 
			
		||||
                BacklightController.reset_backlight(state=KeyboardState(values={
 | 
			
		||||
                    'brightness': Brightness.MEDIUM,
 | 
			
		||||
                    'left': Color.WHITE,
 | 
			
		||||
                    'middle': Color.GREEN,
 | 
			
		||||
                    'right': Color.WHITE,
 | 
			
		||||
                }))
 | 
			
		||||
            elif "franz" in wm_class[0]:
 | 
			
		||||
                BacklightController.reset_backlight(state=KeyboardState(values={
 | 
			
		||||
                    'brightness': Brightness.MEDIUM,
 | 
			
		||||
                    'left': Color.BLUE,
 | 
			
		||||
                    'middle': Color.WHITE,
 | 
			
		||||
                    'right': Color.BLUE,
 | 
			
		||||
                }))
 | 
			
		||||
            else:
 | 
			
		||||
                BacklightController.reset_backlight()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class KeyboardState:
 | 
			
		||||
    _instance = None
 | 
			
		||||
 | 
			
		||||
    state = State.ON
 | 
			
		||||
    mode = Mode.CUSTOM
 | 
			
		||||
    brightness = Brightness.LOW
 | 
			
		||||
    left = Color.WHITE
 | 
			
		||||
    middle = Color.WHITE
 | 
			
		||||
    right = Color.WHITE
 | 
			
		||||
 | 
			
		||||
    def __init__(self, values=None):
 | 
			
		||||
        """
 | 
			
		||||
        :param values: Default values
 | 
			
		||||
        :type values: dict
 | 
			
		||||
        """
 | 
			
		||||
        if values is not None:
 | 
			
		||||
            keys = values.keys()
 | 
			
		||||
            if 'state' in keys:
 | 
			
		||||
                self.state = values['state']
 | 
			
		||||
            if 'mode' in keys:
 | 
			
		||||
                self.mode = values['mode']
 | 
			
		||||
            if 'brightness' in keys:
 | 
			
		||||
                self.brightness = values['brightness']
 | 
			
		||||
            if 'left' in keys:
 | 
			
		||||
                self.left = values['left']
 | 
			
		||||
            if 'middle' in keys:
 | 
			
		||||
                self.middle = values['middle']
 | 
			
		||||
            if 'right' in keys:
 | 
			
		||||
                self.right = values['right']
 | 
			
		||||
 | 
			
		||||
    def __str__(self):
 | 
			
		||||
        return "KBState({}, {}, {}, {}, {}, {})".format(
 | 
			
		||||
            self.state, self.mode, self.brightness, self.left, self.middle, self.right
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
    def get_copy(self):
 | 
			
		||||
        c = KeyboardState()
 | 
			
		||||
        c.state = self.state
 | 
			
		||||
        c.mode = self.mode
 | 
			
		||||
        c.brightness = self.brightness
 | 
			
		||||
        c.left = self.left
 | 
			
		||||
        c.middle = self.middle
 | 
			
		||||
        c.right = self.right
 | 
			
		||||
        return c
 | 
			
		||||
 | 
			
		||||
    @classmethod
 | 
			
		||||
    def get_instance(cls):
 | 
			
		||||
        """
 | 
			
		||||
        :rtype: KeyboardState
 | 
			
		||||
        """
 | 
			
		||||
        if cls._instance is None:
 | 
			
		||||
            cls._instance = KeyboardState()
 | 
			
		||||
        return cls._instance
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class BacklightController:
 | 
			
		||||
 | 
			
		||||
    @staticmethod
 | 
			
		||||
    def reset_backlight(force=False, state=None):
 | 
			
		||||
        """
 | 
			
		||||
        Resets the keyboard backlight to the default colors / states
 | 
			
		||||
        :param force: Force the reset
 | 
			
		||||
        :type force: bool
 | 
			
		||||
        :param state: A state to reset to
 | 
			
		||||
        :type state: KeyboardState
 | 
			
		||||
        """
 | 
			
		||||
        if state is None:
 | 
			
		||||
            # Create state with default values.
 | 
			
		||||
            state = KeyboardState()
 | 
			
		||||
 | 
			
		||||
        logger.debug("Resetting KB backlight to {}".format(state))
 | 
			
		||||
 | 
			
		||||
        flags = [BacklightController.set_colors([state.left, state.middle, state.right], force),
 | 
			
		||||
                 BacklightController.set_brightness(state.brightness, force),
 | 
			
		||||
                 BacklightController.set_state(state.state, force),
 | 
			
		||||
                 BacklightController.set_mode(state.mode, force)]
 | 
			
		||||
 | 
			
		||||
        BacklightController.exec_flags(flags)
 | 
			
		||||
 | 
			
		||||
    @staticmethod
 | 
			
		||||
    def exec_flags(flags):
 | 
			
		||||
        """
 | 
			
		||||
        Removes duplicate flags and executes the command with the resulting flags, and
 | 
			
		||||
        updates the current keyboard state.
 | 
			
		||||
        :param flags: List of list of flags, to be executed.
 | 
			
		||||
        :return: The return code of the execution
 | 
			
		||||
        """
 | 
			
		||||
        final_flags = {}
 | 
			
		||||
        changes = {}
 | 
			
		||||
        for flag in flags:
 | 
			
		||||
            for (k, v) in flag:
 | 
			
		||||
                final_flags[k] = v
 | 
			
		||||
                if k == "-p":
 | 
			
		||||
                    changes['state'] = v
 | 
			
		||||
                elif k == "-t":
 | 
			
		||||
                    changes['mode'] = v
 | 
			
		||||
                elif k == "-b":
 | 
			
		||||
                    changes['brightness'] = v
 | 
			
		||||
                elif k == "-l":
 | 
			
		||||
                    changes['left'] = v
 | 
			
		||||
                elif k == "-m":
 | 
			
		||||
                    changes['middle'] = v
 | 
			
		||||
                elif k == "-r":
 | 
			
		||||
                    changes['right'] = v
 | 
			
		||||
                elif k == "-c":
 | 
			
		||||
                    changes['left'] = v
 | 
			
		||||
                    changes['middle'] = v
 | 
			
		||||
                    changes['right'] = v
 | 
			
		||||
 | 
			
		||||
        args = []
 | 
			
		||||
        for (k, v) in final_flags.items():
 | 
			
		||||
            args.append(k)
 | 
			
		||||
            args.append(v)
 | 
			
		||||
 | 
			
		||||
        res = BacklightController._call(args)
 | 
			
		||||
        if res == 0:
 | 
			
		||||
            # Update state
 | 
			
		||||
            css = KeyboardState.get_instance()
 | 
			
		||||
            for (k, v) in changes.items():
 | 
			
		||||
                css.__setattr__(k, v)
 | 
			
		||||
 | 
			
		||||
    @staticmethod
 | 
			
		||||
    def set_state(state, force=False):
 | 
			
		||||
        """
 | 
			
		||||
        Turns the backlight on or off
 | 
			
		||||
        :param state: State you want ('on' or 'off')
 | 
			
		||||
        :type state: str
 | 
			
		||||
        :param force: Force execution.
 | 
			
		||||
        :type force: bool
 | 
			
		||||
        """
 | 
			
		||||
        if state not in State.LIST:
 | 
			
		||||
            return
 | 
			
		||||
 | 
			
		||||
        logger.debug("Setting KB state to {}".format(state))
 | 
			
		||||
 | 
			
		||||
        css = KeyboardState.get_instance()
 | 
			
		||||
 | 
			
		||||
        if css.state != state or force:
 | 
			
		||||
            return [('-p', state)]
 | 
			
		||||
 | 
			
		||||
        return []
 | 
			
		||||
 | 
			
		||||
    @staticmethod
 | 
			
		||||
    def set_mode(mode, force=False):
 | 
			
		||||
        """
 | 
			
		||||
        Set the backlight mode
 | 
			
		||||
        :param mode: One of "random", "custom", "breathe", "cycle", "wave", "dance", "tempo" or "flash"
 | 
			
		||||
        :type mode: str
 | 
			
		||||
        :param force: Force execution.
 | 
			
		||||
        :type force: bool
 | 
			
		||||
        """
 | 
			
		||||
        if mode not in Mode.LIST:
 | 
			
		||||
            return
 | 
			
		||||
 | 
			
		||||
        logger.debug("Setting KB mode to {}".format(mode))
 | 
			
		||||
 | 
			
		||||
        css = KeyboardState.get_instance()
 | 
			
		||||
        if css.mode != mode or force:
 | 
			
		||||
            return [('-t', mode)]
 | 
			
		||||
 | 
			
		||||
        return []
 | 
			
		||||
 | 
			
		||||
    @staticmethod
 | 
			
		||||
    def set_brightness(level, force=False):
 | 
			
		||||
        """
 | 
			
		||||
        Set the brightness level
 | 
			
		||||
        :param level: Brightness (0 to 3)
 | 
			
		||||
        :type level: int
 | 
			
		||||
        :param force: Force execution.
 | 
			
		||||
        :type force: bool
 | 
			
		||||
        """
 | 
			
		||||
        if level not in Brightness.LIST:
 | 
			
		||||
            return
 | 
			
		||||
 | 
			
		||||
        logger.debug("Setting KB brightness to {}".format(level))
 | 
			
		||||
 | 
			
		||||
        css = KeyboardState.get_instance()
 | 
			
		||||
        if css.brightness != level or force:
 | 
			
		||||
            return [('-b', '{}'.format(level))]
 | 
			
		||||
 | 
			
		||||
        return []
 | 
			
		||||
 | 
			
		||||
    @staticmethod
 | 
			
		||||
    def set_color(side, color, force=False):
 | 
			
		||||
        """
 | 
			
		||||
        Set the backlight color
 | 
			
		||||
        :param side: Side of backlight to change, from left, middle, right or all.
 | 
			
		||||
        :type side: str
 | 
			
		||||
        :param color: The new color, one of "black", "blue", "red", "magenta", "green", "cyan", "yellow" or "white"
 | 
			
		||||
        :type color: str
 | 
			
		||||
        :param force: Force execution.
 | 
			
		||||
        :type force: bool
 | 
			
		||||
        """
 | 
			
		||||
        if side not in Side.LIST:
 | 
			
		||||
            return
 | 
			
		||||
 | 
			
		||||
        if color not in Color.LIST:
 | 
			
		||||
            return
 | 
			
		||||
 | 
			
		||||
        logger.debug("Setting KB side {} to color {}".format(side, color))
 | 
			
		||||
 | 
			
		||||
        css = KeyboardState.get_instance()
 | 
			
		||||
 | 
			
		||||
        if side == "all":
 | 
			
		||||
            if css.left != color or css.right != color or css.right != color or force:
 | 
			
		||||
                return [('-c', color)]
 | 
			
		||||
        elif side == "left":
 | 
			
		||||
            if css.left != color or force:
 | 
			
		||||
                return [('-l', color)]
 | 
			
		||||
        elif side == "right":
 | 
			
		||||
            if css.right != color or force:
 | 
			
		||||
                return [('-r', color)]
 | 
			
		||||
        elif side == "middle":
 | 
			
		||||
            if css.middle != color or force:
 | 
			
		||||
                return [('-m', color)]
 | 
			
		||||
 | 
			
		||||
        return []
 | 
			
		||||
 | 
			
		||||
    @staticmethod
 | 
			
		||||
    def set_colors(colors, force=False):
 | 
			
		||||
        """
 | 
			
		||||
        Set the backlight colors in one go
 | 
			
		||||
        :param colors: The new colors, list of three colors, [left, middle, right]. Colors must be one of
 | 
			
		||||
                        "black", "blue", "red", "magenta", "green", "cyan", "yellow" or "white"
 | 
			
		||||
        :type colors: list
 | 
			
		||||
        :param force: Force execution.
 | 
			
		||||
        :type force: bool
 | 
			
		||||
        """
 | 
			
		||||
        if len(colors) != 3:
 | 
			
		||||
            return
 | 
			
		||||
 | 
			
		||||
        for color in colors:
 | 
			
		||||
            if color not in Color.LIST:
 | 
			
		||||
                return
 | 
			
		||||
 | 
			
		||||
        logger.debug("Setting KB colors to {}, {}, {}".format(colors[0], colors[1], colors[2]))
 | 
			
		||||
 | 
			
		||||
        css = KeyboardState.get_instance()
 | 
			
		||||
 | 
			
		||||
        if css.left != colors[0] or css.middle != colors[1] or css.right != colors[2] or force:
 | 
			
		||||
            return [('-l', '{}'.format(colors[0])),
 | 
			
		||||
                    ('-m', '{}'.format(colors[1])),
 | 
			
		||||
                    ('-r', '{}'.format(colors[2]))]
 | 
			
		||||
 | 
			
		||||
        return []
 | 
			
		||||
 | 
			
		||||
    @staticmethod
 | 
			
		||||
    def _call(args):
 | 
			
		||||
        """
 | 
			
		||||
        Call the script.
 | 
			
		||||
        :param args: Arguments to the script
 | 
			
		||||
        :type args: list
 | 
			
		||||
        :return The exit code of the script
 | 
			
		||||
        :rtype: int
 | 
			
		||||
        """
 | 
			
		||||
        logger.debug("Calling kb_backlight' with args {}".format(args))
 | 
			
		||||
        return subprocess.call(["sudo", "/home/kevin/bin/kb_backlight"] + args)
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue