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