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