diff options
author | Tiago Vignatti <tiago.vignatti@intel.com> | 2012-03-12 19:40:08 -0300 |
---|---|---|
committer | Kristian Høgsberg <krh@bitplanet.net> | 2012-03-20 22:43:54 -0400 |
commit | 1b079abe0e2c075ec4d600c492a72ec980e03baa (patch) | |
tree | f8280e9fabe9f9bc481ee3cc2a632d6b92a6c323 /tests | |
parent | 07b2606c7a2ba785b7281d58a58b20cc9172c3ab (diff) |
tests: add backlight test
Signed-off-by: Tiago Vignatti <tiago.vignatti@intel.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Makefile.am | 9 | ||||
-rw-r--r-- | tests/setbacklight.c | 191 |
2 files changed, 199 insertions, 1 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am index ff64060..1df2c79 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,6 +1,6 @@ if BUILD_TESTS -noinst_PROGRAMS = matrix-test +noinst_PROGRAMS = matrix-test setbacklight endif @@ -13,3 +13,10 @@ matrix_test_SOURCES = \ $(top_srcdir)/src/matrix.c \ $(top_srcdir)/src/matrix.h matrix_test_LDADD = -lm -lrt + +setbacklight_SOURCES = \ + setbacklight.c \ + $(top_srcdir)/src/libbacklight.c \ + $(top_srcdir)/src/libbacklight.h + +setbacklight_LDADD = -ludev -ldrm diff --git a/tests/setbacklight.c b/tests/setbacklight.c new file mode 100644 index 0000000..f7b298c --- /dev/null +++ b/tests/setbacklight.c @@ -0,0 +1,191 @@ +/* + * Copyright © 2012 Intel Corporation + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that the above copyright notice appear in all copies and that both that + * copyright notice and this permission notice appear in supporting + * documentation, and that the name of the copyright holders not be used in + * advertising or publicity pertaining to distribution of the software + * without specific, written prior permission. The copyright holders make + * no representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + * + * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS + * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY + * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER + * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF + * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN + * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + * Author: Tiago Vignatti + */ +/* + * \file setbacklight.c + * Test program to get a backlight connector and set its brightness value. + * Queries for the connectors id can be performed using drm/tests/modeprint + * program. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <fcntl.h> +#include <xf86drm.h> +#include <xf86drmMode.h> + +#include "libbacklight.h" + +static uint32_t +get_drm_connector_type(struct udev_device *drm_device, uint32_t connector_id) +{ + const char *filename; + int fd, i, connector_type; + drmModeResPtr res; + drmModeConnectorPtr connector; + + filename = udev_device_get_devnode(drm_device); + fd = open(filename, O_RDWR | O_CLOEXEC); + if (fd < 0) { + printf("couldn't open drm_device\n"); + return -1; + } + + res = drmModeGetResources(fd); + if (res == 0) { + printf("Failed to get resources from card\n"); + close(fd); + return -1; + } + + for (i = 0; i < res->count_connectors; i++) { + connector = drmModeGetConnector(fd, res->connectors[i]); + if (!connector) + continue; + + if ((connector->connection == DRM_MODE_DISCONNECTED) || + (connector->connector_id != connector_id)) { + drmModeFreeConnector(connector); + continue; + } + + connector_type = connector->connector_type; + drmModeFreeConnector(connector); + drmModeFreeResources(res); + + return connector_type; + } + + drmModeFreeResources(res); + return -1; +} + +/* returns a value between 0-255 range, where higher is brighter */ +static uint32_t +get_normalized_backlight(struct backlight *backlight) +{ + long brightness, max_brightness; + long norm; + + brightness = backlight_get_brightness(backlight); + max_brightness = backlight_get_max_brightness(backlight); + + /* convert it to a scale of 0 to 255 */ + norm = (brightness * 255)/(max_brightness); + + return (int) norm; +} + +static void +set_backlight(struct udev_device *drm_device, int connector_id, int blight) +{ + int connector_type; + long max_brightness, brightness, actual_brightness; + struct backlight *backlight; + long new_blight; + + connector_type = get_drm_connector_type(drm_device, connector_id); + if (connector_type < 0) + return; + + backlight = backlight_init(drm_device, connector_type); + if (!backlight) { + printf("backlight adjust failed\n"); + return; + } + + max_brightness = backlight_get_max_brightness(backlight); + printf("Max backlight: %ld\n", max_brightness); + + brightness = backlight_get_brightness(backlight); + printf("Cached backlight: %ld\n", brightness); + + actual_brightness = backlight_get_actual_brightness(backlight); + printf("Hardware backlight: %ld\n", actual_brightness); + + printf("normalized current brightness: %d\n", + get_normalized_backlight(backlight)); + + /* denormalized value */ + new_blight = (blight * max_brightness) / 255; + + backlight_set_brightness(backlight, new_blight); + printf("Setting brightness to: %ld (norm: %d)\n", new_blight, blight); + + backlight_destroy(backlight); +} + +int +main(int argc, char **argv) +{ + int blight, connector_id; + const char *default_seat = "seat0"; + const char *path, *device_seat; + struct udev *udev; + struct udev_enumerate *e; + struct udev_list_entry *entry; + struct udev_device *device, *drm_device; + + if (argc < 3) { + printf("Please add connector_id and brightness values from 0-255\n"); + return 1; + } + + connector_id = atoi(argv[1]); + blight = atoi(argv[2]); + + udev = udev_new(); + if (udev == NULL) { + printf("failed to initialize udev context\n"); + return 1; + } + + e = udev_enumerate_new(udev); + udev_enumerate_add_match_subsystem(e, "drm"); + udev_enumerate_add_match_sysname(e, "card[0-9]*"); + + udev_enumerate_scan_devices(e); + drm_device = NULL; + udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) { + path = udev_list_entry_get_name(entry); + device = udev_device_new_from_syspath(udev, path); + device_seat = + udev_device_get_property_value(device, "ID_SEAT"); + if (!device_seat) + device_seat = default_seat; + + drm_device = device; + break; + } + + if (drm_device == NULL) { + printf("no drm device found\n"); + return 1; + } + + set_backlight(drm_device, connector_id, blight); + + udev_device_unref(drm_device); + return 0; +} |