可以用C改变屏幕亮度吗?

发布于 2024-11-03 10:36:53 字数 93 浏览 5 评论 0原文

在Linux中使用C语言,可以改变观看屏幕的亮度吗?除了在我安装的 Ubuntu 和 Fedora 上运行之外,预计它不会是可移植的。我问的是如何与操作系统交互来改变亮度。

Using C in Linux, would it be possible to change the brightness of the viewing screen? This isn't expected to be portable other than running on my installs of Ubuntu and Fedora. I'm asking about how I would interface with the os to change the brightness.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

無處可尋 2024-11-10 10:36:53

我首先从下面的 ubuntu 软件包列表中进行选择,该工具允许您管理屏幕亮度(提示:这取决于品牌)

nvidia-settings - Tool of configuring the NVIDIA graphics driver
smartdimmer - Change LCD brightness on Geforce cards
armada-backlight - adjust backlight of Compaq Armada laptops (E300, M500, M700)
ddccontrol - a program to control monitor parameters
eeepc-acpi-scripts - Scripts to support suspend and hotkeys on the Asus Eee PC laptop
fnfxd - ACPI and hotkey daemon for Toshiba laptops
gddccontrol - a program to control monitor parameters
spicctrl - Sony Vaio controller program to set LCD backlight brightness
tpb - program to use the IBM ThinkPad(tm) special keys
xfce4-power-manager - power manager for Xfce desktop
xfce4-power-manager-plugins - power manager plugins for Xfce panel
xvattr - Utility to change Xv attributes

一旦您选择了它,

sudo apt-get build-dep <pkgname>
apt-get source --compile <pkgname> 

应该会让您走上正确的道路

I'd start with selecting from the following list of ubuntu pacakges, the tool that allows you to manage your screen's brightness (hint: it depends on the brand)

nvidia-settings - Tool of configuring the NVIDIA graphics driver
smartdimmer - Change LCD brightness on Geforce cards
armada-backlight - adjust backlight of Compaq Armada laptops (E300, M500, M700)
ddccontrol - a program to control monitor parameters
eeepc-acpi-scripts - Scripts to support suspend and hotkeys on the Asus Eee PC laptop
fnfxd - ACPI and hotkey daemon for Toshiba laptops
gddccontrol - a program to control monitor parameters
spicctrl - Sony Vaio controller program to set LCD backlight brightness
tpb - program to use the IBM ThinkPad(tm) special keys
xfce4-power-manager - power manager for Xfce desktop
xfce4-power-manager-plugins - power manager plugins for Xfce panel
xvattr - Utility to change Xv attributes

Once you have selected it,

sudo apt-get build-dep <pkgname>
apt-get source --compile <pkgname> 

should get you on the right track

手长情犹 2024-11-10 10:36:53

/sys/class/backlight/*/brightness。是的,即使是在 C 语言中。

Poke /sys/class/backlight/*/brightness. Yes, even in C.

回忆凄美了谁 2024-11-10 10:36:53

是的,但不可移植——你需要一个特定于平台的函数,C 标准库中没有任何东西。

Yes, but not portably -- you need a platform-specific function, there is nothing in the C standard library.

世态炎凉 2024-11-10 10:36:53

查看 xbacklight 源。例如,以下代码将屏幕亮度设置为 50%。

// brightness.c
// gcc -o brightness brightness.c -lXrandr -lX11

#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/extensions/Xrandr.h>

#define BRIGHTNESS 0.5 // Target brightness between 0.0 and 1.0

int main(int argc, char *argv[])
{
        Display *dpy;
        static Atom backlight;
        int screen = 0, o = 0;
        Window root;
        XRRScreenResources *resources;
        RROutput output;
        XRRPropertyInfo *info;
        double min, max;
        long value;

        dpy = XOpenDisplay(NULL);
        backlight = XInternAtom (dpy, "Backlight", True);
        root = RootWindow(dpy, screen);
        resources = XRRGetScreenResources(dpy, root);
        output = resources->outputs[o];
        info = XRRQueryOutputProperty(dpy, output, backlight);
        min = info->values[0];
        max = info->values[1];
        XFree(info); // Don't need this anymore
        XRRFreeScreenResources(resources); // or this

        value = BRIGHTNESS * (max - min) + min;

        XRRChangeOutputProperty(dpy, output, backlight, XA_INTEGER,
                        32, PropModeReplace, (unsigned char *) &value, 1);
        XFlush(dpy);
        XSync(dpy, False);
        return 0;
}

Check out the xbacklight source. For example, the following code sets the screen brightness to 50%.

// brightness.c
// gcc -o brightness brightness.c -lXrandr -lX11

#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/extensions/Xrandr.h>

#define BRIGHTNESS 0.5 // Target brightness between 0.0 and 1.0

int main(int argc, char *argv[])
{
        Display *dpy;
        static Atom backlight;
        int screen = 0, o = 0;
        Window root;
        XRRScreenResources *resources;
        RROutput output;
        XRRPropertyInfo *info;
        double min, max;
        long value;

        dpy = XOpenDisplay(NULL);
        backlight = XInternAtom (dpy, "Backlight", True);
        root = RootWindow(dpy, screen);
        resources = XRRGetScreenResources(dpy, root);
        output = resources->outputs[o];
        info = XRRQueryOutputProperty(dpy, output, backlight);
        min = info->values[0];
        max = info->values[1];
        XFree(info); // Don't need this anymore
        XRRFreeScreenResources(resources); // or this

        value = BRIGHTNESS * (max - min) + min;

        XRRChangeOutputProperty(dpy, output, backlight, XA_INTEGER,
                        32, PropModeReplace, (unsigned char *) &value, 1);
        XFlush(dpy);
        XSync(dpy, False);
        return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文