2 * Copyright © 2012 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * Author: Tiago Vignatti
28 * \file setbacklight.c
29 * Test program to get a backlight connector and set its brightness value.
30 * Queries for the connectors id can be performed using drm/tests/modeprint
41 #include <xf86drmMode.h>
43 #include "libbacklight.h"
46 get_drm_connector_type(struct udev_device *drm_device, uint32_t connector_id)
49 int fd, i, connector_type;
51 drmModeConnectorPtr connector;
53 filename = udev_device_get_devnode(drm_device);
54 fd = open(filename, O_RDWR | O_CLOEXEC);
56 printf("couldn't open drm_device\n");
60 res = drmModeGetResources(fd);
62 printf("Failed to get resources from card\n");
67 for (i = 0; i < res->count_connectors; i++) {
68 connector = drmModeGetConnector(fd, res->connectors[i]);
72 if ((connector->connection == DRM_MODE_DISCONNECTED) ||
73 (connector->connector_id != connector_id)) {
74 drmModeFreeConnector(connector);
78 connector_type = connector->connector_type;
79 drmModeFreeConnector(connector);
80 drmModeFreeResources(res);
83 return connector_type;
87 drmModeFreeResources(res);
91 /* returns a value between 0-255 range, where higher is brighter */
93 get_normalized_backlight(struct backlight *backlight)
95 long brightness, max_brightness;
98 brightness = backlight_get_brightness(backlight);
99 max_brightness = backlight_get_max_brightness(backlight);
101 /* convert it to a scale of 0 to 255 */
102 norm = (brightness * 255)/(max_brightness);
108 set_backlight(struct udev_device *drm_device, int connector_id, int blight)
111 long max_brightness, brightness, actual_brightness;
112 struct backlight *backlight;
115 connector_type = get_drm_connector_type(drm_device, connector_id);
116 if (connector_type < 0)
119 backlight = backlight_init(drm_device, connector_type);
121 printf("backlight adjust failed\n");
125 max_brightness = backlight_get_max_brightness(backlight);
126 printf("Max backlight: %ld\n", max_brightness);
128 brightness = backlight_get_brightness(backlight);
129 printf("Cached backlight: %ld\n", brightness);
131 actual_brightness = backlight_get_actual_brightness(backlight);
132 printf("Hardware backlight: %ld\n", actual_brightness);
134 printf("normalized current brightness: %d\n",
135 get_normalized_backlight(backlight));
137 /* denormalized value */
138 new_blight = (blight * max_brightness) / 255;
140 backlight_set_brightness(backlight, new_blight);
141 printf("Setting brightness to: %ld (norm: %d)\n", new_blight, blight);
143 backlight_destroy(backlight);
147 main(int argc, char **argv)
149 int blight, connector_id;
152 struct udev_enumerate *e;
153 struct udev_list_entry *entry;
154 struct udev_device *drm_device;
157 printf("Please add connector_id and brightness values from 0-255\n");
161 connector_id = atoi(argv[1]);
162 blight = atoi(argv[2]);
166 printf("failed to initialize udev context\n");
170 e = udev_enumerate_new(udev);
171 udev_enumerate_add_match_subsystem(e, "drm");
172 udev_enumerate_add_match_sysname(e, "card[0-9]*");
174 udev_enumerate_scan_devices(e);
176 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
177 path = udev_list_entry_get_name(entry);
178 drm_device = udev_device_new_from_syspath(udev, path);
182 if (drm_device == NULL) {
183 printf("no drm device found\n");
187 set_backlight(drm_device, connector_id, blight);
189 udev_device_unref(drm_device);