blob: c8eba436792ff1546adbe068f5c801ee2e3f88bc [file] [log] [blame]
Tiago Vignatti1b079ab2012-03-12 19:40:08 -03001/*
2 * Copyright © 2012 Intel Corporation
3 *
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.
13 *
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.
21 *
22 * Author: Tiago Vignatti
23 */
24/*
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
28 * program.
29 */
30
Andrew Wedgbury9cd661e2014-04-07 12:40:35 +010031#include "config.h"
32
Tiago Vignatti1b079ab2012-03-12 19:40:08 -030033#include <stdio.h>
34#include <stdlib.h>
35#include <unistd.h>
36#include <fcntl.h>
37#include <xf86drm.h>
38#include <xf86drmMode.h>
39
40#include "libbacklight.h"
41
42static uint32_t
43get_drm_connector_type(struct udev_device *drm_device, uint32_t connector_id)
44{
45 const char *filename;
46 int fd, i, connector_type;
47 drmModeResPtr res;
48 drmModeConnectorPtr connector;
49
50 filename = udev_device_get_devnode(drm_device);
51 fd = open(filename, O_RDWR | O_CLOEXEC);
52 if (fd < 0) {
53 printf("couldn't open drm_device\n");
54 return -1;
55 }
56
57 res = drmModeGetResources(fd);
58 if (res == 0) {
59 printf("Failed to get resources from card\n");
60 close(fd);
61 return -1;
62 }
63
64 for (i = 0; i < res->count_connectors; i++) {
65 connector = drmModeGetConnector(fd, res->connectors[i]);
66 if (!connector)
67 continue;
68
69 if ((connector->connection == DRM_MODE_DISCONNECTED) ||
70 (connector->connector_id != connector_id)) {
71 drmModeFreeConnector(connector);
72 continue;
73 }
74
75 connector_type = connector->connector_type;
76 drmModeFreeConnector(connector);
77 drmModeFreeResources(res);
78
Rob Bradfordc59a33a2013-01-10 19:48:53 +000079 close(fd);
Tiago Vignatti1b079ab2012-03-12 19:40:08 -030080 return connector_type;
81 }
82
Rob Bradfordc59a33a2013-01-10 19:48:53 +000083 close(fd);
Tiago Vignatti1b079ab2012-03-12 19:40:08 -030084 drmModeFreeResources(res);
85 return -1;
86}
87
88/* returns a value between 0-255 range, where higher is brighter */
89static uint32_t
90get_normalized_backlight(struct backlight *backlight)
91{
92 long brightness, max_brightness;
93 long norm;
94
95 brightness = backlight_get_brightness(backlight);
96 max_brightness = backlight_get_max_brightness(backlight);
97
98 /* convert it to a scale of 0 to 255 */
99 norm = (brightness * 255)/(max_brightness);
100
101 return (int) norm;
102}
103
104static void
105set_backlight(struct udev_device *drm_device, int connector_id, int blight)
106{
107 int connector_type;
108 long max_brightness, brightness, actual_brightness;
109 struct backlight *backlight;
110 long new_blight;
111
112 connector_type = get_drm_connector_type(drm_device, connector_id);
113 if (connector_type < 0)
114 return;
115
116 backlight = backlight_init(drm_device, connector_type);
117 if (!backlight) {
118 printf("backlight adjust failed\n");
119 return;
120 }
121
122 max_brightness = backlight_get_max_brightness(backlight);
123 printf("Max backlight: %ld\n", max_brightness);
124
125 brightness = backlight_get_brightness(backlight);
126 printf("Cached backlight: %ld\n", brightness);
127
128 actual_brightness = backlight_get_actual_brightness(backlight);
129 printf("Hardware backlight: %ld\n", actual_brightness);
130
131 printf("normalized current brightness: %d\n",
132 get_normalized_backlight(backlight));
133
134 /* denormalized value */
135 new_blight = (blight * max_brightness) / 255;
136
137 backlight_set_brightness(backlight, new_blight);
138 printf("Setting brightness to: %ld (norm: %d)\n", new_blight, blight);
139
140 backlight_destroy(backlight);
141}
142
143int
144main(int argc, char **argv)
145{
146 int blight, connector_id;
John Kåre Alsaker011a1ce2012-10-12 12:25:06 +0200147 const char *path;
Tiago Vignatti1b079ab2012-03-12 19:40:08 -0300148 struct udev *udev;
149 struct udev_enumerate *e;
150 struct udev_list_entry *entry;
John Kåre Alsaker011a1ce2012-10-12 12:25:06 +0200151 struct udev_device *drm_device;
Tiago Vignatti1b079ab2012-03-12 19:40:08 -0300152
153 if (argc < 3) {
154 printf("Please add connector_id and brightness values from 0-255\n");
155 return 1;
156 }
157
158 connector_id = atoi(argv[1]);
159 blight = atoi(argv[2]);
160
161 udev = udev_new();
162 if (udev == NULL) {
163 printf("failed to initialize udev context\n");
164 return 1;
165 }
166
167 e = udev_enumerate_new(udev);
168 udev_enumerate_add_match_subsystem(e, "drm");
169 udev_enumerate_add_match_sysname(e, "card[0-9]*");
170
171 udev_enumerate_scan_devices(e);
172 drm_device = NULL;
173 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
174 path = udev_list_entry_get_name(entry);
John Kåre Alsaker011a1ce2012-10-12 12:25:06 +0200175 drm_device = udev_device_new_from_syspath(udev, path);
Tiago Vignatti1b079ab2012-03-12 19:40:08 -0300176 break;
177 }
178
179 if (drm_device == NULL) {
180 printf("no drm device found\n");
181 return 1;
182 }
183
184 set_backlight(drm_device, connector_id, blight);
185
186 udev_device_unref(drm_device);
187 return 0;
188}