1// Copyright 2018 The Fuchsia Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#include <fcntl.h> 6#include <stdio.h> 7#include <stdlib.h> 8#include <string.h> 9#include <zircon/device/backlight.h> 10 11static void usage(char* argv[]) { 12 printf("Usage: %s [--read|--off|<brightness-val>]\n", argv[0]); 13 printf("options:\n <brightness-val>: 0-255\n"); 14} 15 16int main(int argc, char* argv[]) { 17 if (argc != 2) { 18 usage(argv); 19 return -1; 20 } 21 22 if (strcmp(argv[1], "--read") == 0) { 23 int fd = open("/dev/class/backlight/000", O_RDONLY); 24 if (fd < 0) { 25 printf("Failed to open backlight\n"); 26 return -1; 27 } 28 29 backlight_state_t state; 30 ssize_t ret = ioctl_backlight_get_state(fd, &state); 31 if (ret < 0) { 32 printf("Get backlight state ioctl failed\n"); 33 return -1; 34 } 35 printf("Backlight:%s Brightness:%d\n", state.on ? "on" : "off", 36 state.brightness); 37 return 0; 38 } 39 40 bool on; 41 uint32_t brightness; 42 if (strcmp(argv[1], "--off") == 0) { 43 on = false; 44 brightness = 0; 45 } else { 46 char* endptr; 47 brightness = strtoul(argv[1], &endptr, 10); 48 if (endptr == argv[1] || *endptr != '\0') { 49 usage(argv); 50 return -1; 51 } 52 if (brightness > 255) { 53 printf("Invalid brightness %d\n", brightness); 54 return -1; 55 } 56 on = true; 57 } 58 59 int fd = open("/dev/class/backlight/000", O_RDWR); 60 if (fd < 0) { 61 printf("Failed to open backlight\n"); 62 return -1; 63 } 64 65 backlight_state_t state = { .on = on, .brightness = (uint8_t) brightness }; 66 ssize_t ret = ioctl_backlight_set_state(fd, &state); 67 if (ret < 0) { 68 printf("Set brightness ioctl failed\n"); 69 return -1; 70 } 71 72 return 0; 73} 74