1 /* 2 * modifiers - asserts (un)pressed modifier keys 3 * (c) 2002, Francois Revol, revol@free.fr 4 * for Haiku 5 * compile with: 6 * LDFLAGS=-lbe make modifiers 7 */ 8 9 #include <OS.h> 10 #include <stdio.h> 11 #include <string.h> 12 #include <strings.h> 13 #include <InterfaceDefs.h> 14 15 16 int32 modifier_bits[] = { 17 B_SHIFT_KEY, 18 B_COMMAND_KEY, 19 B_CONTROL_KEY, 20 B_CAPS_LOCK, 21 B_SCROLL_LOCK, 22 B_NUM_LOCK, 23 B_OPTION_KEY, 24 B_MENU_KEY, 25 B_LEFT_SHIFT_KEY, 26 B_RIGHT_SHIFT_KEY, 27 B_LEFT_COMMAND_KEY, 28 B_RIGHT_COMMAND_KEY, 29 B_LEFT_CONTROL_KEY, 30 B_RIGHT_CONTROL_KEY, 31 B_LEFT_OPTION_KEY, 32 B_RIGHT_OPTION_KEY, 33 0 34 }; 35 36 37 const char *modifier_names[] = { "shift", "command", "control", "capslock", "scrolllock", "numlock", "option", "menu", "lshift", "rshift", "lcommand", "rcommand", "lcontrol", "rcontrol", "loption", "roption", NULL }; 38 39 40 int 41 usage(char *progname, int error) 42 { 43 FILE *outstr; 44 outstr = error?stderr:stdout; 45 fprintf(outstr, "Usage: %s [-help] [-list] [-/+][[|l|r][shift|control|command|option]|capslock|scrolllock|numlock|menu]\n", progname); 46 fprintf(outstr, "\t- asserts unpressed modifier,\n"); 47 fprintf(outstr, "\t+ asserts pressed modifier,\n"); 48 return error; 49 } 50 51 52 int 53 list_modifiers(int mods) 54 { 55 int i; 56 int gotone = 0; 57 for (i = 0; modifier_names[i]; i++) { 58 if (mods & modifier_bits[i]) { 59 if (gotone) 60 printf(","); 61 printf("%s", modifier_names[i]); 62 gotone = 1; 63 } 64 if (modifier_names[i+1] == NULL) 65 printf("\n"); 66 } 67 return 0; 68 } 69 70 71 int 72 main(int argc, char **argv) 73 { 74 int32 mods; 75 int32 mask_xor = 0x0; 76 int32 mask_and = 0x0; 77 int i, j; 78 79 mods = modifiers(); 80 if (argc == 1) 81 return usage(argv[0], 1); 82 for (i=1; i<argc; i++) { 83 if (!strncmp(argv[i], "-h", 2)) 84 return usage(argv[0], 0); 85 else if (!strcmp(argv[i], "-list")) 86 return list_modifiers(mods); 87 else if (strlen(argv[i]) > 1 && (*argv[i] == '-' || *argv[i] == '+')) { 88 for (j=0; modifier_names[j]; j++) { 89 if (!strcmp(argv[i]+1, modifier_names[j])) { 90 if (*argv[i] == '-') 91 mask_and |= modifier_bits[j]; 92 else 93 mask_xor |= modifier_bits[j]; 94 break; 95 } 96 } 97 if (modifier_names[j] == NULL) 98 return usage(argv[0], 1); 99 } else 100 return usage(argv[0], 1); 101 } 102 mask_and |= mask_xor; 103 return (((mods & mask_and) ^ mask_xor) != 0); 104 } 105 106