Tiago Vignatti | 1b079ab | 2012-03-12 19:40:08 -0300 | [diff] [blame^] | 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 | return connector_type; |
| 78 | } |
| 79 | |
| 80 | drmModeFreeResources(res); |
| 81 | return -1; |
| 82 | } |
| 83 | |
| 84 | /* returns a value between 0-255 range, where higher is brighter */ |
| 85 | static uint32_t |
| 86 | get_normalized_backlight(struct backlight *backlight) |
| 87 | { |
| 88 | long brightness, max_brightness; |
| 89 | long norm; |
| 90 | |
| 91 | brightness = backlight_get_brightness(backlight); |
| 92 | max_brightness = backlight_get_max_brightness(backlight); |
| 93 | |
| 94 | /* convert it to a scale of 0 to 255 */ |
| 95 | norm = (brightness * 255)/(max_brightness); |
| 96 | |
| 97 | return (int) norm; |
| 98 | } |
| 99 | |
| 100 | static void |
| 101 | set_backlight(struct udev_device *drm_device, int connector_id, int blight) |
| 102 | { |
| 103 | int connector_type; |
| 104 | long max_brightness, brightness, actual_brightness; |
| 105 | struct backlight *backlight; |
| 106 | long new_blight; |
| 107 | |
| 108 | connector_type = get_drm_connector_type(drm_device, connector_id); |
| 109 | if (connector_type < 0) |
| 110 | return; |
| 111 | |
| 112 | backlight = backlight_init(drm_device, connector_type); |
| 113 | if (!backlight) { |
| 114 | printf("backlight adjust failed\n"); |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | max_brightness = backlight_get_max_brightness(backlight); |
| 119 | printf("Max backlight: %ld\n", max_brightness); |
| 120 | |
| 121 | brightness = backlight_get_brightness(backlight); |
| 122 | printf("Cached backlight: %ld\n", brightness); |
| 123 | |
| 124 | actual_brightness = backlight_get_actual_brightness(backlight); |
| 125 | printf("Hardware backlight: %ld\n", actual_brightness); |
| 126 | |
| 127 | printf("normalized current brightness: %d\n", |
| 128 | get_normalized_backlight(backlight)); |
| 129 | |
| 130 | /* denormalized value */ |
| 131 | new_blight = (blight * max_brightness) / 255; |
| 132 | |
| 133 | backlight_set_brightness(backlight, new_blight); |
| 134 | printf("Setting brightness to: %ld (norm: %d)\n", new_blight, blight); |
| 135 | |
| 136 | backlight_destroy(backlight); |
| 137 | } |
| 138 | |
| 139 | int |
| 140 | main(int argc, char **argv) |
| 141 | { |
| 142 | int blight, connector_id; |
| 143 | const char *default_seat = "seat0"; |
| 144 | const char *path, *device_seat; |
| 145 | struct udev *udev; |
| 146 | struct udev_enumerate *e; |
| 147 | struct udev_list_entry *entry; |
| 148 | struct udev_device *device, *drm_device; |
| 149 | |
| 150 | if (argc < 3) { |
| 151 | printf("Please add connector_id and brightness values from 0-255\n"); |
| 152 | return 1; |
| 153 | } |
| 154 | |
| 155 | connector_id = atoi(argv[1]); |
| 156 | blight = atoi(argv[2]); |
| 157 | |
| 158 | udev = udev_new(); |
| 159 | if (udev == NULL) { |
| 160 | printf("failed to initialize udev context\n"); |
| 161 | return 1; |
| 162 | } |
| 163 | |
| 164 | e = udev_enumerate_new(udev); |
| 165 | udev_enumerate_add_match_subsystem(e, "drm"); |
| 166 | udev_enumerate_add_match_sysname(e, "card[0-9]*"); |
| 167 | |
| 168 | udev_enumerate_scan_devices(e); |
| 169 | drm_device = NULL; |
| 170 | udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) { |
| 171 | path = udev_list_entry_get_name(entry); |
| 172 | device = udev_device_new_from_syspath(udev, path); |
| 173 | device_seat = |
| 174 | udev_device_get_property_value(device, "ID_SEAT"); |
| 175 | if (!device_seat) |
| 176 | device_seat = default_seat; |
| 177 | |
| 178 | drm_device = device; |
| 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 | } |