backlight.c (1264B)
1 /* See LICENSE for license details. */ 2 struct linux_backlight_data {i64 max_brightness; char *brightness_path;}; 3 4 static BLOCK_UPDATE_FN(backlight_update) 5 { 6 struct linux_backlight_data *lbd = b->user_data; 7 i64 current = read_i64(lbd->brightness_path); 8 f32 percent = 100 * current / (f32)lbd->max_brightness + 0.5; 9 i64 len = snprintf(buffer, sizeof(buffer), "%d%%", (i32)percent); 10 buffer[len] = 0; 11 b->len = snprintf(b->data, sizeof(b->data), b->fmt, buffer); 12 } 13 14 static BLOCK_INIT_FN(backlight_init) 15 { 16 struct linux_backlight_data *lbd; 17 b->user_data = lbd = push_struct(a, struct linux_backlight_data); 18 19 Arena tmp = *a; 20 Stream path = stream_alloc(&tmp, KB(1)); 21 stream_push_s8(&path, s8("/sys/class/backlight/")); 22 stream_push_s8(&path, *(s8 *)b->arg); 23 size sidx = path.write_index; 24 stream_push_s8(&path, s8("/max_brightness")); 25 lbd->max_brightness = read_i64(stream_ensure_c_str(&path)); 26 if (!lbd->max_brightness) 27 die("backlight_init: failed to read max brightness\n"); 28 path.write_index = sidx; 29 30 stream_push_s8(&path, s8("/brightness")); 31 path.buffer[path.write_index++] = 0; 32 lbd->brightness_path = (char *)path.buffer; 33 a->beg += path.write_index; 34 35 backlight_update(b); 36 add_file_watch(a, lbd->brightness_path, block_index, backlight_update); 37 }