如何在Linux中更改屏幕分辨率时执行命令


1

我在VM中运行Linux,并且我想在X服务器分辨率发生变化时运行脚本(通常是在VM中进入/退出全屏模式)。我怎样才能做到这一点?

Answers:


1

写下这段代码

#include <iostream>
#include <X11/Xlib.h>

Display* disp = XOpenDisplay(NULL);
Screen*  scrn = DefaultScreenOfDisplay(disp);
int height = scrn->height;
int width  = scrn->width;

int main() {
    std::cout << "width " << width << " height " << height << "\n";
}

编译它

g++ test.c -o test -lX11

运行

rbabchis@haze:~$ ./test
width 1920 height 1080
rbabchis@haze:~$ 

您必须从那里对其进行修改,或者将其包装在另一种编程/脚本语言中,以便在更改分辨率时检查并执行脚本。这应该很容易。

使用此脚本不断检查更改并执行外部脚本(./external_script)

#!/bin/bash

resolution=$(./test)

while true; do {
        newResolution=$(./test)
        if [ "$newResolution" != "$resolution" ]; then {
                echo "Resolution change: $newResolution"
                resolution=$newResolution
                ./external_script
        } fi

        sleep 1
} done


这是否回答你的问题?你需要更多信息吗?
Ryan Babchishin

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.