Split the geometry information from weston_surface out into weston_view
[platform/upstream/weston.git] / tests / setbacklight.c
1 /*
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
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <fcntl.h>
35 #include <xf86drm.h>
36 #include <xf86drmMode.h>
37
38 #include "libbacklight.h"
39
40 static uint32_t
41 get_drm_connector_type(struct udev_device *drm_device, uint32_t connector_id)
42 {
43         const char *filename;
44         int fd, i, connector_type;
45         drmModeResPtr res;
46         drmModeConnectorPtr connector;
47
48         filename = udev_device_get_devnode(drm_device);
49         fd = open(filename, O_RDWR | O_CLOEXEC);
50         if (fd < 0) {
51                 printf("couldn't open drm_device\n");
52                 return -1;
53         }
54
55         res = drmModeGetResources(fd);
56         if (res == 0) {
57                 printf("Failed to get resources from card\n");
58                 close(fd);
59                 return -1;
60         }
61
62         for (i = 0; i < res->count_connectors; i++) {
63                 connector = drmModeGetConnector(fd, res->connectors[i]);
64                 if (!connector)
65                         continue;
66
67                 if ((connector->connection == DRM_MODE_DISCONNECTED) ||
68                     (connector->connector_id != connector_id)) {
69                         drmModeFreeConnector(connector);
70                         continue;
71                 }
72
73                 connector_type = connector->connector_type;
74                 drmModeFreeConnector(connector);
75                 drmModeFreeResources(res);
76
77                 close(fd);
78                 return connector_type;
79         }
80
81         close(fd);
82         drmModeFreeResources(res);
83         return -1;
84 }
85
86 /* returns a value between 0-255 range, where higher is brighter */
87 static uint32_t
88 get_normalized_backlight(struct backlight *backlight)
89 {
90         long brightness, max_brightness;
91         long norm;
92
93         brightness = backlight_get_brightness(backlight);
94         max_brightness = backlight_get_max_brightness(backlight);
95
96         /* convert it to a scale of 0 to 255 */
97         norm = (brightness * 255)/(max_brightness);
98
99         return (int) norm;
100 }
101
102 static void
103 set_backlight(struct udev_device *drm_device, int connector_id, int blight)
104 {
105         int connector_type;
106         long max_brightness, brightness, actual_brightness;
107         struct backlight *backlight;
108         long new_blight;
109
110         connector_type = get_drm_connector_type(drm_device, connector_id);
111         if (connector_type < 0)
112                 return;
113
114         backlight = backlight_init(drm_device, connector_type);
115         if (!backlight) {
116                 printf("backlight adjust failed\n");
117                 return;
118         }
119
120         max_brightness = backlight_get_max_brightness(backlight);
121         printf("Max backlight: %ld\n", max_brightness);
122
123         brightness = backlight_get_brightness(backlight);
124         printf("Cached backlight: %ld\n", brightness);
125
126         actual_brightness = backlight_get_actual_brightness(backlight);
127         printf("Hardware backlight: %ld\n", actual_brightness);
128
129         printf("normalized current brightness: %d\n",
130                get_normalized_backlight(backlight));
131
132         /* denormalized value */
133         new_blight = (blight * max_brightness) / 255;
134
135         backlight_set_brightness(backlight, new_blight);
136         printf("Setting brightness to: %ld (norm: %d)\n", new_blight, blight);
137
138         backlight_destroy(backlight);
139 }
140
141 int
142 main(int argc, char **argv)
143 {
144         int blight, connector_id;
145         const char *path;
146         struct udev *udev;
147         struct udev_enumerate *e;
148         struct udev_list_entry *entry;
149         struct udev_device *drm_device;
150
151         if (argc < 3) {
152                 printf("Please add connector_id and brightness values from 0-255\n");
153                 return 1;
154         }
155
156         connector_id = atoi(argv[1]);
157         blight = atoi(argv[2]);
158
159         udev = udev_new();
160         if (udev == NULL) {
161                 printf("failed to initialize udev context\n");
162                 return 1;
163         }
164
165         e = udev_enumerate_new(udev);
166         udev_enumerate_add_match_subsystem(e, "drm");
167         udev_enumerate_add_match_sysname(e, "card[0-9]*");
168
169         udev_enumerate_scan_devices(e);
170         drm_device = NULL;
171         udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
172                 path = udev_list_entry_get_name(entry);
173                 drm_device = udev_device_new_from_syspath(udev, path);
174                 break;
175         }
176
177         if (drm_device == NULL) {
178                 printf("no drm device found\n");
179                 return 1;
180         }
181
182         set_backlight(drm_device, connector_id, blight);
183
184         udev_device_unref(drm_device);
185         return 0;
186 }