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
				
			
		| 
						 | 
				
			
			@ -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