Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
modules: display: also use dpms when (un)blanking
When using DRM for display management, as opposed to just fbdev, just
blanking /dev/fb0 is not enough to make devices actually enter deeper
idle states, DPMS does the trick by telling X11 to send the right DRM
ioctls.
  • Loading branch information
MerlijnWajer committed Mar 28, 2020
1 parent cab11b8 commit 57786e7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -79,7 +79,7 @@ MODULE_CFLAGS := $(COMMON_CFLAGS)
MODULE_CFLAGS += -fPIC -shared
MODULE_CFLAGS += -I.
MODULE_CFLAGS += $$(pkg-config glib-2.0 gmodule-2.0 dbus-1 dbus-glib-1 gconf-2.0 upower-glib --cflags)
MODULE_LDFLAGS := $$(pkg-config glib-2.0 gmodule-2.0 dbus-1 dbus-glib-1 gconf-2.0 upower-glib libcal --libs)
MODULE_LDFLAGS := $$(pkg-config glib-2.0 gmodule-2.0 dbus-1 dbus-glib-1 gconf-2.0 upower-glib libcal x11 xext --libs)
MODULE_LIBS := datapipe.c mce-hal.c mce-log.c mce-dbus.c mce-conf.c mce-gconf.c median_filter.c mce-lib.c
MODULE_HEADERS := datapipe.h mce-hal.h mce-log.h mce-dbus.h mce-conf.h mce-gconf.h mce.h median_filter.h mce-lib.h

Expand Down
39 changes: 39 additions & 0 deletions modules/display.c
Expand Up @@ -28,6 +28,8 @@
#include <string.h>
#include <unistd.h>
#include <linux/fb.h>
#include <X11/Xlib.h>
#include <X11/extensions/dpms.h>
#include <sys/ioctl.h>
#include <mce/mode-names.h>
#include "mce.h"
Expand Down Expand Up @@ -365,6 +367,38 @@ static gboolean backlight_ioctl(int value)
return status;
}

/* Set DPMS mode for X11 display to on or off
*
* @params dpms_on To force the mode into on or off (blanked) mode.
*/
static void dpms_set_display_on(gboolean dpms_on)
{
Display* dpy = NULL;

dpy = XOpenDisplay(NULL);
if (dpy == NULL) {
dpy = XOpenDisplay(":0.0");
}

if (dpy == NULL) {
mce_log(LL_INFO, "%s: unable to open display", __func__);
return;
}

if (DPMSCapable(dpy)) {
DPMSEnable(dpy);
if (dpms_on) {
DPMSForceLevel(dpy, DPMSModeOn);
} else {
usleep(100000); // from xset
DPMSForceLevel(dpy, DPMSModeOff);
}
}

XCloseDisplay(dpy);
}


/**
* Timeout callback for the brightness fade
*
Expand All @@ -380,6 +414,7 @@ static gboolean brightness_fade_timeout_cb(gpointer data)

if ((cached_brightness <= 0) && (target_brightness != 0)) {
backlight_ioctl(FB_BLANK_UNBLANK);
dpms_set_display_on(TRUE);
}

if ((cached_brightness == -1) ||
Expand All @@ -398,6 +433,7 @@ static gboolean brightness_fade_timeout_cb(gpointer data)

if (cached_brightness == 0) {
backlight_ioctl(FB_BLANK_POWERDOWN);
dpms_set_display_on(FALSE);
}

if (retval == FALSE)
Expand Down Expand Up @@ -479,6 +515,7 @@ static void display_blank(void)
target_brightness = 0;
mce_write_number_string_to_file(brightness_file, 0);
backlight_ioctl(FB_BLANK_POWERDOWN);
dpms_set_display_on(FALSE);
}

/**
Expand All @@ -488,6 +525,7 @@ static void display_dim(void)
{
if (cached_brightness == 0) {
backlight_ioctl(FB_BLANK_UNBLANK);
dpms_set_display_on(TRUE);
}

update_brightness_fade((maximum_display_brightness *
Expand All @@ -506,6 +544,7 @@ static void display_unblank(void)
backlight_ioctl(FB_BLANK_UNBLANK);
mce_write_number_string_to_file(brightness_file,
set_brightness);
dpms_set_display_on(TRUE);
} else {
update_brightness_fade(set_brightness);
}
Expand Down

0 comments on commit 57786e7

Please sign in to comment.