Add new widget and only show thermal widget if setting is set
This commit is contained in:
		
							parent
							
								
									5c19e442fa
								
							
						
					
					
						commit
						99d4919539
					
				
					 4 changed files with 246 additions and 63 deletions
				
			
		| 
						 | 
				
			
			@ -105,6 +105,9 @@ class Config(BaseConfig):
 | 
			
		|||
    wifi_theme_path = "/home/kevin/.config/qtile/kuro/resources/wifi"
 | 
			
		||||
    wifi_update_interval = 5
 | 
			
		||||
 | 
			
		||||
    # GPU variables
 | 
			
		||||
    gpu_theme_path = "/home/kevin/.config/qtile/kuro/resources/gpu"
 | 
			
		||||
 | 
			
		||||
    # Normal volume icon variables
 | 
			
		||||
    volume_font = "Noto Sans"
 | 
			
		||||
    volume_fontsize = 11
 | 
			
		||||
| 
						 | 
				
			
			@ -133,7 +136,8 @@ class Config(BaseConfig):
 | 
			
		|||
    updates_colour_available = '#f4d742'
 | 
			
		||||
 | 
			
		||||
    # Screen organization
 | 
			
		||||
    laptop_screen = "eDP-1-1"
 | 
			
		||||
    laptop_screen_nvidia = "eDP-1-1"
 | 
			
		||||
    laptop_screen_intel = "eDP1"
 | 
			
		||||
 | 
			
		||||
    # Keyboard colors
 | 
			
		||||
    do_keyboard_updates = False
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -2,16 +2,27 @@ import json
 | 
			
		|||
import os
 | 
			
		||||
import random
 | 
			
		||||
 | 
			
		||||
# Initialize logging
 | 
			
		||||
from libqtile.log_utils import logger
 | 
			
		||||
 | 
			
		||||
logger.error("Importing qtile theme requirements...")
 | 
			
		||||
 | 
			
		||||
from libqtile.config import Key, Screen, Group, Drag, Click
 | 
			
		||||
from libqtile.command import lazy
 | 
			
		||||
from libqtile import layout, bar, widget
 | 
			
		||||
 | 
			
		||||
logger.error("Importing theme util functions...")
 | 
			
		||||
 | 
			
		||||
# Import theme util functions
 | 
			
		||||
from xcffib.xproto import WindowError
 | 
			
		||||
 | 
			
		||||
logger.error("Importing kuro utils...")
 | 
			
		||||
 | 
			
		||||
import kuro.utils.widgets
 | 
			
		||||
from kuro.utils import general as utils
 | 
			
		||||
 | 
			
		||||
logger.error("Importing variables and other utils...")
 | 
			
		||||
 | 
			
		||||
# Import variables
 | 
			
		||||
from kuro.base import BaseTheme
 | 
			
		||||
from kuro.utils.general import display_wm_class, test_popups
 | 
			
		||||
| 
						 | 
				
			
			@ -19,6 +30,8 @@ from kuro.utils.kb_backlight import handle_focus_change as kb_handle_focus_chang
 | 
			
		|||
from kuro.utils import layouts as kuro_layouts
 | 
			
		||||
from kuro.utils.windows import KuroStatic
 | 
			
		||||
 | 
			
		||||
logger.error("Importing configuration...")
 | 
			
		||||
 | 
			
		||||
try:
 | 
			
		||||
    from kuro.config import Config
 | 
			
		||||
except ImportError:
 | 
			
		||||
| 
						 | 
				
			
			@ -28,9 +41,7 @@ except ImportError:
 | 
			
		|||
        Config = None
 | 
			
		||||
        raise ImportError("Could not load theme Config or BaseConfig!")
 | 
			
		||||
 | 
			
		||||
# Initialize logging
 | 
			
		||||
from libqtile.log_utils import logger
 | 
			
		||||
 | 
			
		||||
logger.error("Imports done")
 | 
			
		||||
 | 
			
		||||
class Kuro(BaseTheme):
 | 
			
		||||
    # Shorthand for modifier key
 | 
			
		||||
| 
						 | 
				
			
			@ -84,7 +95,8 @@ class Kuro(BaseTheme):
 | 
			
		|||
        for field in self.debug_textfields:
 | 
			
		||||
            field.text = text
 | 
			
		||||
        for bar in self.debug_bars:
 | 
			
		||||
            bar.draw()
 | 
			
		||||
            if self.qtile is not None:
 | 
			
		||||
                bar.draw()
 | 
			
		||||
 | 
			
		||||
    def log_debug(self, text):
 | 
			
		||||
        if Config.get('verbose', False):
 | 
			
		||||
| 
						 | 
				
			
			@ -96,6 +108,7 @@ class Kuro(BaseTheme):
 | 
			
		|||
        logger.info(text)
 | 
			
		||||
 | 
			
		||||
    def initialize(self):
 | 
			
		||||
        logger.error("Initializing Kuro theme...")
 | 
			
		||||
        self.log_debug("Initializing Kuro Theme...")
 | 
			
		||||
 | 
			
		||||
        # Update color scheme
 | 
			
		||||
| 
						 | 
				
			
			@ -313,18 +326,23 @@ class Kuro(BaseTheme):
 | 
			
		|||
                kuro.utils.widgets.MediaWidget(),
 | 
			
		||||
 | 
			
		||||
                kuro.utils.widgets.SeparatorWidget(),
 | 
			
		||||
            ])
 | 
			
		||||
 | 
			
		||||
                kuro.utils.widgets.ThermalSensorWidget(
 | 
			
		||||
                    font=Config.get('font_topbar', 'Arial'),
 | 
			
		||||
                    fontsize=Config.get('fontsize_topbar', 16),
 | 
			
		||||
                    foreground=Config.get('thermal_colour', '#ffffff'),
 | 
			
		||||
                    foreground_alert=Config.get('thermal_colour_alert', '#ff0000'),
 | 
			
		||||
                    tag_sensor=Config.get('thermal_sensor', 'temp1'),
 | 
			
		||||
                    chip=Config.get('thermal_chip', None),
 | 
			
		||||
                    threshold=Config.get('thermal_threshold', 70),
 | 
			
		||||
                    update_interval=5,
 | 
			
		||||
                ),
 | 
			
		||||
            if Config.get('show_temperature', False):
 | 
			
		||||
                widgets.append(
 | 
			
		||||
                    kuro.utils.widgets.ThermalSensorWidget(
 | 
			
		||||
                        font=Config.get('font_topbar', 'Arial'),
 | 
			
		||||
                        fontsize=Config.get('fontsize_topbar', 16),
 | 
			
		||||
                        foreground=Config.get('thermal_colour', '#ffffff'),
 | 
			
		||||
                        foreground_alert=Config.get('thermal_colour_alert', '#ff0000'),
 | 
			
		||||
                        tag_sensor=Config.get('thermal_sensor', 'temp1'),
 | 
			
		||||
                        chip=Config.get('thermal_chip', None),
 | 
			
		||||
                        threshold=Config.get('thermal_threshold', 70),
 | 
			
		||||
                        update_interval=5,
 | 
			
		||||
                    )
 | 
			
		||||
                )
 | 
			
		||||
 | 
			
		||||
            widgets.extend([
 | 
			
		||||
                widget.CPUGraph(
 | 
			
		||||
                    width=Config.get('cpu_width', 25),
 | 
			
		||||
                    border_color=Config.get('cpu_border_colour', "#000000"),
 | 
			
		||||
| 
						 | 
				
			
			@ -374,6 +392,11 @@ class Kuro(BaseTheme):
 | 
			
		|||
                    update_delay=Config.get('battery_update_delay', 30)
 | 
			
		||||
                ),
 | 
			
		||||
 | 
			
		||||
                kuro.utils.widgets.GPUStatusWidget(
 | 
			
		||||
                    theme_path=Config.get('gpu_theme_path', '/home/docs/checkouts/readthedocs.org/user_builds/qtile'
 | 
			
		||||
                                                            '/checkouts/latest/libqtile/resources/battery-icons'),
 | 
			
		||||
                ),
 | 
			
		||||
 | 
			
		||||
                kuro.utils.widgets.WifiIconWidget(
 | 
			
		||||
                    interface=Config.get('wifi_interface', 'wlp4s0'),
 | 
			
		||||
                    theme_path=Config.get('wifi_theme_path', '/home/docs/checkouts/readthedocs.org/user_builds/qtile'
 | 
			
		||||
| 
						 | 
				
			
			@ -519,12 +542,25 @@ class Kuro(BaseTheme):
 | 
			
		|||
    @staticmethod
 | 
			
		||||
    def update_screens(qtile):
 | 
			
		||||
        out = utils.call_process(["xrandr", "--current"])
 | 
			
		||||
        mode_out = utils.call_process(["optimus-manager", "--print-mode"])
 | 
			
		||||
        if "nvidia" in mode_out:
 | 
			
		||||
            video_mode = "nvidia"
 | 
			
		||||
        elif "intel" in mode_out:
 | 
			
		||||
            video_mode = "intel"
 | 
			
		||||
        else:
 | 
			
		||||
            video_mode = "unknown"
 | 
			
		||||
        laptop_screen = None
 | 
			
		||||
        screens = []
 | 
			
		||||
        for x in out.split("\n"):
 | 
			
		||||
            if " connected " in x:
 | 
			
		||||
                if Config.get("laptop_screen", None) is not None and Config.get("laptop_screen", None) in x:
 | 
			
		||||
                    laptop_screen = x
 | 
			
		||||
                if Config.get("laptop_screen_nvidia", None) is not None \
 | 
			
		||||
                        and Config.get("laptop_screen_intel", None) is not None:
 | 
			
		||||
                    if video_mode == "nvidia" and Config.get("laptop_screen_nvidia", None) in x:
 | 
			
		||||
                        laptop_screen = x
 | 
			
		||||
                    elif video_mode == "intel" and Config.get("laptop_screen_intel", None) in x:
 | 
			
		||||
                        laptop_screen = x
 | 
			
		||||
                    else:
 | 
			
		||||
                        screens.append(x)
 | 
			
		||||
                else:
 | 
			
		||||
                    screens.append(x)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -534,7 +570,7 @@ class Kuro(BaseTheme):
 | 
			
		|||
            other = screens[0].split()[0]
 | 
			
		||||
            utils.call_process(["xrandr", "--output", laptop, "--below", other])
 | 
			
		||||
            qtile.cmd_restart()
 | 
			
		||||
        elif laptop_screen is not None and len(screens) > 1:
 | 
			
		||||
        else:
 | 
			
		||||
            utils.execute("arandr")
 | 
			
		||||
 | 
			
		||||
    def reinitialize_visualizers(self, qtile=None):
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,4 +1,5 @@
 | 
			
		|||
import os
 | 
			
		||||
import re
 | 
			
		||||
import subprocess
 | 
			
		||||
 | 
			
		||||
import cairocffi
 | 
			
		||||
| 
						 | 
				
			
			@ -677,3 +678,133 @@ class KuroTaskList(TaskList):
 | 
			
		|||
 | 
			
		||||
        return "%s%s" % (state, window_name)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class GPUStatusWidget(base._TextBox):
 | 
			
		||||
    """Displays the currently used GPU."""
 | 
			
		||||
 | 
			
		||||
    orientations = base.ORIENTATION_HORIZONTAL
 | 
			
		||||
    defaults = [
 | 
			
		||||
        ('check_command', 'optimus-manager --print-mode', 'The command that shows the current mode.'),
 | 
			
		||||
        ('update_interval', 60, 'The update interval in seconds.'),
 | 
			
		||||
        ('theme_path', default_icon_path(), 'Path of the icons'),
 | 
			
		||||
        ('custom_icons', {}, 'dict containing key->filename icon map'),
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
    def __init__(self, **config):
 | 
			
		||||
        super(GPUStatusWidget, self).__init__("GPU", bar.CALCULATED, **config)
 | 
			
		||||
        self.add_defaults(GPUStatusWidget.defaults)
 | 
			
		||||
 | 
			
		||||
        if self.theme_path:
 | 
			
		||||
            self.length_type = bar.STATIC
 | 
			
		||||
            self.length = 0
 | 
			
		||||
        self.surfaces = {}
 | 
			
		||||
        self.current_icon = 'gpu-unknown'
 | 
			
		||||
        self.icons = dict([(x, '{0}.png'.format(x)) for x in (
 | 
			
		||||
            'gpu-intel',
 | 
			
		||||
            'gpu-nvidia',
 | 
			
		||||
            'gpu-unknown',
 | 
			
		||||
        )])
 | 
			
		||||
        self.current_status = "Unknown"
 | 
			
		||||
        self.icons.update(self.custom_icons)
 | 
			
		||||
 | 
			
		||||
    def _get_info(self):
 | 
			
		||||
        output = self.call_process(self.check_command, shell=True)
 | 
			
		||||
        mode = "nvidia" if "nvidia" in output else "intel" if "intel" in output else "unknown"
 | 
			
		||||
        return {'error': False, 'mode': mode}
 | 
			
		||||
 | 
			
		||||
    def timer_setup(self):
 | 
			
		||||
        self.update()
 | 
			
		||||
        self.timeout_add(self.update_interval, self.timer_setup)
 | 
			
		||||
 | 
			
		||||
    def _configure(self, qtile, bar):
 | 
			
		||||
        super(GPUStatusWidget, self)._configure(qtile, bar)
 | 
			
		||||
        self.setup_images()
 | 
			
		||||
 | 
			
		||||
    def _get_icon_key(self):
 | 
			
		||||
        key = 'gpu'
 | 
			
		||||
        info = self._get_info()
 | 
			
		||||
        if info.get('mode') == "intel":
 | 
			
		||||
            key += '-intel'
 | 
			
		||||
            self.current_status = "Intel"
 | 
			
		||||
        elif info.get('mode') == "nvidia":
 | 
			
		||||
            key += '-nvidia'
 | 
			
		||||
            self.current_status = "NVidia"
 | 
			
		||||
        else:
 | 
			
		||||
            key += '-unknown'
 | 
			
		||||
            self.current_status = "Unknown"
 | 
			
		||||
        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('GPU Status 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
 | 
			
		||||
 | 
			
		||||
    def button_press(self, x, y, button):
 | 
			
		||||
        if button == BUTTON_LEFT:
 | 
			
		||||
            if self.current_status == "Unknown":
 | 
			
		||||
                notify("GPU Status", "The currently used GPU is unknown.",
 | 
			
		||||
                       image=os.path.join(self.theme_path, "gpu-unknown.png"))
 | 
			
		||||
            else:
 | 
			
		||||
                notify("GPU Status", "The system is currently running on the {} GPU.\n"
 | 
			
		||||
                                     "Press the middle mouse button on this icon to switch GPUs.".format(
 | 
			
		||||
                                         self.current_status
 | 
			
		||||
                                     ),
 | 
			
		||||
                       image=os.path.join(self.theme_path, "gpu-{}.png".format(self.current_status.lower()))
 | 
			
		||||
                       )
 | 
			
		||||
 | 
			
		||||
        if button == BUTTON_MIDDLE:
 | 
			
		||||
            command = ["optimus-manager", "--no-confirm", "--switch", "auto"]
 | 
			
		||||
            output = self.call_process(command)
 | 
			
		||||
            if "nvidia" in output:
 | 
			
		||||
                notify("GPU Switched", "The GPU has been switched from Intel to NVidia.\n"
 | 
			
		||||
                                       "Please log out and log back in to apply the changes to the session.",
 | 
			
		||||
                       image=os.path.join(self.theme_path, "gpu-nvidia.png"))
 | 
			
		||||
            elif "intel" in output:
 | 
			
		||||
                notify("GPU Switched", "The GPU has been switched from NVidia to Intel.\n"
 | 
			
		||||
                                       "Please log out and log back in to apply the changes to the session.",
 | 
			
		||||
                       image=os.path.join(self.theme_path, "gpu-intel.png"))
 | 
			
		||||
            else:
 | 
			
		||||
                notify("GPU Switch Error", "I could not determine if the GPU was switched successfully.\n"
 | 
			
		||||
                                           "Please log out and log back in to clear up the inconsistency.",
 | 
			
		||||
                       image=os.path.join(self.theme_path, "gpu-unknown.png"))
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue