1 /* 2 * Access the keypad to modify the backlight on the Letux 400. 3 * 4 * Fn+Volume Down/Up adjusts the brightness. 5 * 6 * Copyright (C) 2018, 2020 Paul Boddie <paul@boddie.org.uk> 7 * 8 * This program is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU General Public License as 10 * published by the Free Software Foundation; either version 2 of 11 * the License, or (at your option) any later version. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 * 18 * You should have received a copy of the GNU General Public License 19 * along with this program; if not, write to the Free Software 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 21 * Boston, MA 02110-1301, USA 22 */ 23 24 #include <l4/devices/input-keypad-client.h> 25 #include <l4/devices/keypad-loader.h> 26 27 #include <l4/re/env> 28 #include <l4/sys/capability> 29 30 #include <l4/re/event_enums.h> 31 #include <l4/util/util.h> 32 33 #include <stdio.h> 34 35 #include "backlight_client.h" 36 37 /* Backlight level. */ 38 39 enum Letux400_backlight_levels 40 { 41 Letux400_backlight_min_level = 0, 42 Letux400_backlight_max_level = 300, 43 Letux400_backlight_step = 25, 44 }; 45 46 static int backlight_level = 250; 47 48 /* Key state. */ 49 50 static int modifier_set = 0; 51 52 /* Backlight references. */ 53 54 Backlight *backlight_device; 55 56 57 58 /* Input event handler. */ 59 60 static void handler(Input_event event, void *priv) 61 { 62 (void) priv; 63 64 /* Track the state of the modifier key. */ 65 66 if (event.code == L4RE_KEY_SLEEP) 67 modifier_set = event.value; 68 69 if (!event.value || !modifier_set) 70 return; 71 72 /* Upon keypress events, test controls and update the backlight. */ 73 74 switch (event.code) 75 { 76 case L4RE_KEY_DOWN: 77 if (backlight_level < Letux400_backlight_min_level + Letux400_backlight_step) 78 backlight_level = Letux400_backlight_min_level; 79 else 80 backlight_level -= Letux400_backlight_step; 81 break; 82 83 case L4RE_KEY_UP: 84 if (backlight_level > Letux400_backlight_max_level - Letux400_backlight_step) 85 backlight_level = Letux400_backlight_max_level; 86 else 87 backlight_level += Letux400_backlight_step; 88 break; 89 90 default: return; 91 } 92 93 /* Use the backlight device to update the backlight level. */ 94 95 backlight_device->set_brightness(backlight_level); 96 } 97 98 99 100 int main(void) 101 { 102 /* Load the keypad library using details from the configured library. */ 103 104 Keypad *keypad = (Keypad *) load_keypad(); 105 Input_keypad_client client(keypad); 106 107 /* Obtain a reference to the backlight device. */ 108 109 l4_cap_idx_t backlight = l4re_env_get_cap("backlight"); 110 if (!l4_is_valid_cap(backlight)) return 1; 111 112 /* Use only the backlight interface with the backlight. */ 113 114 client_Backlight backlight_obj(backlight); 115 backlight_device = &backlight_obj; 116 117 client.attach(handler, 0); 118 119 l4_sleep_forever(); 120 121 return 0; 122 }