Brightness Controller-源码记录

📅 2026/7/16 19:17:12
Brightness Controller-源码记录
Brightness Controller 亮度控制一、概述二、ddcutil 与 xrandr1. ddcutil2. xrandr三、部分代码解析1. icons2. ui3. utilinit.py一、概述项目https://github.com/LordAmit/Brightness.git原理Brightness Controlle 是我在 Ubuntu 发现上调节亮度的一个工具我觉得很好用我后面对它的实现比较感兴趣就去找了他的源项目来满足我的好奇心也许后面做项目时候会使用特此记录。这个小工具是用的python3实现界面使用pyqt5控制显示器功能用的命令完成的会用到ddcutil和xrandr。在亮度调节后台的命令优先使用 ddcutil其次才使用 xrandr。二、ddcutil 与 xrandr这里简单介绍一下 ddcutil1. ddcutilddcutil 是一个 Linux 命令行工具用于通过 DDC/CIDisplay Data Channel Command Interface 协议与支持该协议的显示器进行通信。它允许用户通过软件控制显示器的硬件设置如亮度、对比度、输入源等。. 调整显示器设置亮度、对比度、音量等。切换输入源如 HDMI、DP、VGA。调整色彩设置如 RGB 值。查询显示器信息获取显示器的制造商、型号、支持的 DDC/CI 功能等。自动化控制通过脚本批量调整多台显示器的设置。2. xrandrxrandr 是一个 Linux 命令行工具用于管理和配置显示器的分辨率、刷新率、旋转、镜像以及多显示器布局。它是 X Window SystemXorg的一部分主要用于与 X Server 交互因此仅适用于运行 Xorg 的 Linux 系统。三、部分代码解析1. icons图标文件夹2. ui界面文件夹包含ui文件以及通过uic工具对ui文件生成的pyqt脚本文件。3. util包含了基础的工具集check_displays.py查询连接的显示器工具通过运行 xrandr --query 命令再对输出用正则匹配出显示器名称importsubprocessimportshleximportredefquery_xrandr():queryxrandr --queryxrandr_outputsubprocess.Popen(shlex.split(query),stdoutsubprocess.PIPE,stderrsubprocess.STDOUT)stdout,stderrxrandr_output.communicate()returnstr(stdout,utf-8)defextract_displays(output):patternre.compile(r\b({0})\b.format(connected),flagsre.IGNORECASE)linesoutput.splitlines()connected[lineforlineinlinesifpattern.search(line)]connected_displayslist(map(lambdadisplay:display.split()[0],connected))returnconnected_displaysdefdetect_display_devices(): Detects available displays. returns connected_displays This contains the available device names compatible with xrandr returnextract_displays(query_xrandr())if__name____main__:print(detect_display_devices())executor.py封装的进程控制器是调节亮度这些的后台命令importsubprocessdefexecute_command(string_cmd):subprocess.check_output(string_cmd,shellTrue)使用的例子如下defchange_value_pr(self,value):Changes Primary Display Red ratiocmd_valuexrandr\--output%s \--brightness%s\--gamma%s:%s:%s%\(self.display1,self.values[self.ui.primary_brightness.value()-1],self.values[value],self.values[self.ui.primary_green.value()],self.values[self.ui.primary_blue.value()])Executor.execute_command(cmd_value)read_config.py 和 write_config.py读写配置没有特别内容QtSingleApplication.py封装的单例工具它的核心作用是确保同一时间只有一个应用程序实例运行并支持在多个实例尝试启动时将消息传递给已经运行的实例并激活窗口。使用QLocalSocket 作为进程间通信的工具。classQtSingleApplication(QApplication):messageReceivedSignal(str)def__init__(self,id,*argv):super(QtSingleApplication,self).__init__(*argv)self._ididself._activationWindowNoneself._activateOnMessageFalse# Is there another instance running?self._outSocketQLocalSocket()self._outSocket.connectToServer(self._id)self._isRunningself._outSocket.waitForConnected()ifself._isRunning:# Yes, there is.self._outStreamQTextStream(self._outSocket)self._outStream.setCodec(UTF-8)else:# No, there isnt.self._outSocketNoneself._outStreamNoneself._inSocketNoneself._inStreamNoneself._serverQLocalServer()self._server.listen(self._id)self._server.newConnection.connect(self._onNewConnection)defisRunning(self):returnself._isRunningdefid(self):returnself._iddefactivationWindow(self):returnself._activationWindowdefsetActivationWindow(self,activationWindow,activateOnMessageTrue):self._activationWindowactivationWindow self._activateOnMessageactivateOnMessagedefactivateWindow(self):ifnotself._activationWindow:returnself._activationWindow.setWindowState(self._activationWindow.windowState()~Qt.WindowMinimized)self._activationWindow.raise_()self._activationWindow.activateWindow()defsendMessage(self,msg):ifnotself._outStream:returnFalseself._outStreammsg\nself._outStream.flush()returnself._outSocket.waitForBytesWritten()def_onNewConnection(self):ifself._inSocket:self._inSocket.readyRead.disconnect(self._onReadyRead)self._inSocketself._server.nextPendingConnection()ifnotself._inSocket:returnself._inStreamQTextStream(self._inSocket)self._inStream.setCodec(UTF-8)self._inSocket.readyRead.connect(self._onReadyRead)ifself._activateOnMessage:self.activateWindow()def_onReadyRead(self):whileTrue:msgself._inStream.readLine()ifnotmsg:breakself.messageReceived.emit(msg)使用例子defmain():UUIDPHIR-HWOH-MEIZ-AHTAAPPQtSingleApplication(UUID,sys.argv)ifAPP.isRunning():sys.exit(0)WINDOWMyApplication()WINDOW.APPAPP APP.setActivationWindow(WINDOW)WINDOW.show()sys.exit(APP.exec_())if__name____main__:main()init.py此文件为完整的应用main为启动函数class HelpForm 帮助界面class AboutForm 关于界面class LicenseForm 版权页面MyApplication为主应用下面change_value_p 开头为 调节主屏幕下面change_value_s 开头为 调节副屏幕pbr 是主屏亮度prpgpb 分别代表 RGB分量看看调节主屏幕代码调节色彩分量均是使用 xrandr