3 * vim:ts=4:sw=4:expandtab
25 /* These are the same as in YYLTYPE */
32 typedef struct yy_buffer_state *YY_BUFFER_STATE;
33 extern int yylex(struct context *context);
34 extern int yyparse(void);
36 YY_BUFFER_STATE yy_scan_string(const char *);
38 static struct context *context;
40 /* We don’t need yydebug for now, as we got decent error messages using
41 * yyerror(). Should you ever want to extend the parser, it might be handy
42 * to just comment it in again, so it stays here. */
45 void yyerror(const char *error_message) {
46 fprintf(stderr, "\n");
47 fprintf(stderr, "CONFIG: %s\n", error_message);
48 fprintf(stderr, "CONFIG: line %d:\n",
49 context->line_number);
50 fprintf(stderr, "CONFIG: %s\n", context->line_copy);
51 fprintf(stderr, "CONFIG: ");
52 for (int c = 1; c <= context->last_column; c++)
53 if (c >= context->first_column)
55 else fprintf(stderr, " ");
56 fprintf(stderr, "\n");
57 fprintf(stderr, "\n");
64 char *rewrite_binding(const char *bindingline) {
67 context = calloc(sizeof(struct context), 1);
69 yy_scan_string(bindingline);
72 fprintf(stderr, "Could not parse configfile\n");
76 result = context->result;
78 if (context->line_copy)
79 free(context->line_copy);
85 /* XXX: does not work for combinations of modifiers yet */
86 static char *modifier_to_string(int modifiers) {
87 //printf("should convert %d to string\n", modifiers);
88 if (modifiers == (1 << 3))
89 return strdup("$mod+");
90 else if (modifiers == ((1 << 3) | (1 << 0)))
91 return strdup("$mod+Shift+");
92 else if (modifiers == (1 << 9))
93 return strdup("$mod+");
94 else if (modifiers == ((1 << 9) | (1 << 0)))
95 return strdup("$mod+Shift+");
96 else if (modifiers == (1 << 0))
97 return strdup("Shift+");
98 else return strdup("");
104 %lex-param { struct context *context }
111 %token <number>NUMBER "<number>"
112 %token <string>STR "<string>"
114 %token TOKMODVAR "$mod"
115 %token MODIFIER "<modifier>"
116 %token TOKCONTROL "control"
117 %token TOKSHIFT "shift"
118 %token WHITESPACE "<whitespace>"
123 | lines WHITESPACE bindcode
129 TOKBINDCODE WHITESPACE binding_modifiers NUMBER WHITESPACE STR
131 //printf("\tFound keycode binding mod%d with key %d and command %s\n", $<number>3, $4, $<string>6);
133 if (($<number>3 & (1 << 0))) {
134 /* When shift is included, we really need to use the second-level
135 * symbol (upper-case). The lower-case symbol could be on a
136 * different key than the upper-case one (unlikely for letters, but
137 * more likely for special characters). */
140 KeySym sym = XKeycodeToKeysym(dpy, $4, level);
141 char *str = XKeysymToString(sym);
142 char *modifiers = modifier_to_string($<number>3);
143 // TODO: modifier to string
144 asprintf(&(context->result), "bindsym %s%s %s\n", modifiers, str, $<string>6);
150 /* NULL */ { $<number>$ = 0; }
152 | binding_modifiers '+' binding_modifier { $<number>$ = $<number>1 | $<number>3; }
153 | binding_modifiers '+' { $<number>$ = $<number>1; }
157 MODIFIER { $<number>$ = $<number>1; }
158 | TOKMODVAR { $<number>$ = $<number>1; }
159 | TOKCONTROL { $<number>$ = (1 << 2); }
160 | TOKSHIFT { $<number>$ = (1 << 0); }