Advertisement
Guest User

Tweak caps2esc to turn caps into left_alt instead of control

a guest
Nov 23rd, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.81 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #include <linux/input.h>
  5.  
  6. // clang-format off
  7. const struct input_event
  8. esc_up          = {.type = EV_KEY, .code = KEY_ESC,      .value = 0},
  9. ctrl_up         = {.type = EV_KEY, .code = KEY_LEFTCTRL, .value = 0},
  10. capslock_up     = {.type = EV_KEY, .code = KEY_CAPSLOCK, .value = 0},
  11. esc_down        = {.type = EV_KEY, .code = KEY_ESC,      .value = 1},
  12. ctrl_down       = {.type = EV_KEY, .code = KEY_LEFTCTRL, .value = 1},
  13. capslock_down   = {.type = EV_KEY, .code = KEY_CAPSLOCK, .value = 1},
  14. esc_repeat      = {.type = EV_KEY, .code = KEY_ESC,      .value = 2},
  15. ctrl_repeat     = {.type = EV_KEY, .code = KEY_LEFTCTRL, .value = 2},
  16. capslock_repeat = {.type = EV_KEY, .code = KEY_CAPSLOCK, .value = 2},
  17.  
  18. alt_up      = {.type = EV_KEY, .code = KEY_LEFTALT,  .value = 0},
  19. alt_down    = {.type = EV_KEY, .code = KEY_LEFTALT,  .value = 1},
  20. alt_repeat  = {.type = EV_KEY, .code = KEY_LEFTALT,  .value = 2}
  21. ;
  22.  
  23. // clang-format on
  24.  
  25. int equal(const struct input_event *first, const struct input_event *second) {
  26.     return first->type == second->type && first->code == second->code &&
  27.            first->value == second->value;
  28. }
  29.  
  30. int read_event(struct input_event *event) {
  31.     return fread(event, sizeof(struct input_event), 1, stdin) == 1;
  32. }
  33.  
  34. void write_event(const struct input_event *event) {
  35.     if (fwrite(event, sizeof(struct input_event), 1, stdout) != 1)
  36.         exit(EXIT_FAILURE);
  37. }
  38.  
  39. int main(void) {
  40.     int capslock_is_down = 0, esc_give_up = 0;
  41.     struct input_event input;
  42.  
  43.     setbuf(stdin, NULL), setbuf(stdout, NULL);
  44.  
  45.     while (read_event(&input)) {
  46.         if (input.type != EV_KEY) {
  47.             write_event(&input);
  48.             continue;
  49.         }
  50.  
  51.         if (capslock_is_down) {
  52.             if (equal(&input, &capslock_down) ||
  53.                 equal(&input, &capslock_repeat))
  54.                 continue;
  55.  
  56.             if (equal(&input, &capslock_up)) {
  57.                 capslock_is_down = 0;
  58.                 if (esc_give_up) {
  59.                     esc_give_up = 0;
  60.             // use left_alt instead of control so that it plays nice iwth gnome tweak.
  61.                     //write_event(&ctrl_up);
  62.             write_event(&alt_up);
  63.                     continue;
  64.                 }
  65.                 write_event(&esc_down);
  66.                 write_event(&esc_up);
  67.                 continue;
  68.             }
  69.  
  70.             if (!esc_give_up && input.value) {
  71.                 esc_give_up = 1;
  72.                 //write_event(&ctrl_down);
  73.         write_event(&alt_down);
  74.             }
  75.         } else if (equal(&input, &capslock_down)) {
  76.             capslock_is_down = 1;
  77.             continue;
  78.         }
  79.    
  80.     // commenting this out because I want escape to stay escape
  81.         //if (input.code == KEY_ESC)
  82.         //    input.code = KEY_CAPSLOCK;
  83.         write_event(&input);
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement