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