2 * Copyright © 2012 Intel Corporation
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 * Author: Tiago Vignatti
25 * \file setbacklight.c
26 * Test program to get a backlight connector and set its brightness value.
27 * Queries for the connectors id can be performed using drm/tests/modeprint
36 #include <xf86drmMode.h>
38 #include "libbacklight.h"
41 get_drm_connector_type(struct udev_device *drm_device, uint32_t connector_id)
44 int fd, i, connector_type;
46 drmModeConnectorPtr connector;
48 filename = udev_device_get_devnode(drm_device);
49 fd = open(filename, O_RDWR | O_CLOEXEC);
51 printf("couldn't open drm_device\n");
55 res = drmModeGetResources(fd);
57 printf("Failed to get resources from card\n");
62 for (i = 0; i < res->count_connectors; i++) {
63 connector = drmModeGetConnector(fd, res->connectors[i]);
67 if ((connector->connection == DRM_MODE_DISCONNECTED) ||
68 (connector->connector_id != connector_id)) {
69 drmModeFreeConnector(connector);
73 connector_type = connector->connector_type;
74 drmModeFreeConnector(connector);
75 drmModeFreeResources(res);
77 return connector_type;
80 drmModeFreeResources(res);
84 /* returns a value between 0-255 range, where higher is brighter */
86 get_normalized_backlight(struct backlight *backlight)
88 long brightness, max_brightness;
91 brightness = backlight_get_brightness(backlight);
92 max_brightness = backlight_get_max_brightness(backlight);
94 /* convert it to a scale of 0 to 255 */
95 norm = (brightness * 255)/(max_brightness);
101 set_backlight(struct udev_device *drm_device, int connector_id, int blight)
104 long max_brightness, brightness, actual_brightness;
105 struct backlight *backlight;
108 connector_type = get_drm_connector_type(drm_device, connector_id);
109 if (connector_type < 0)
112 backlight = backlight_init(drm_device, connector_type);
114 printf("backlight adjust failed\n");
118 max_brightness = backlight_get_max_brightness(backlight);
119 printf("Max backlight: %ld\n", max_brightness);
121 brightness = backlight_get_brightness(backlight);
122 printf("Cached backlight: %ld\n", brightness);
124 actual_brightness = backlight_get_actual_brightness(backlight);
125 printf("Hardware backlight: %ld\n", actual_brightness);
127 printf("normalized current brightness: %d\n",
128 get_normalized_backlight(backlight));
130 /* denormalized value */
131 new_blight = (blight * max_brightness) / 255;
133 backlight_set_brightness(backlight, new_blight);
134 printf("Setting brightness to: %ld (norm: %d)\n", new_blight, blight);
136 backlight_destroy(backlight);
140 main(int argc, char **argv)
142 int blight, connector_id;
145 struct udev_enumerate *e;
146 struct udev_list_entry *entry;
147 struct udev_device *drm_device;
150 printf("Please add connector_id and brightness values from 0-255\n");
154 connector_id = atoi(argv[1]);
155 blight = atoi(argv[2]);
159 printf("failed to initialize udev context\n");
163 e = udev_enumerate_new(udev);
164 udev_enumerate_add_match_subsystem(e, "drm");
165 udev_enumerate_add_match_sysname(e, "card[0-9]*");
167 udev_enumerate_scan_devices(e);
169 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
170 path = udev_list_entry_get_name(entry);
171 drm_device = udev_device_new_from_syspath(udev, path);
175 if (drm_device == NULL) {
176 printf("no drm device found\n");
180 set_backlight(drm_device, connector_id, blight);
182 udev_device_unref(drm_device);