]> git.sur5r.net Git - i3/i3/blob - src/cmdparse.y
Bugfix: Don’t focus new cons when there is a fullscreen con (Thanks dothebart)
[i3/i3] / src / cmdparse.y
1 %{
2 /*
3  * vim:ts=4:sw=4:expandtab
4  *
5  * i3 - an improved dynamic tiling window manager
6  * © 2009-2010 Michael Stapelberg and contributors (see also: LICENSE)
7  *
8  * cmdparse.y: the parser for commands you send to i3 (or bind on keys)
9  *
10
11  */
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <unistd.h>
15 #include <fcntl.h>
16
17 #include "all.h"
18
19 typedef struct yy_buffer_state *YY_BUFFER_STATE;
20 extern int cmdyylex(struct context *context);
21 extern int cmdyyparse(void);
22 extern FILE *cmdyyin;
23 YY_BUFFER_STATE cmdyy_scan_string(const char *);
24
25 static struct bindings_head *current_bindings;
26 static struct context *context;
27 static Match current_match;
28
29 /*
30  * Helper data structure for an operation window (window on which the operation
31  * will be performed). Used to build the TAILQ owindows.
32  *
33  */
34 typedef struct owindow {
35     Con *con;
36     TAILQ_ENTRY(owindow) owindows;
37 } owindow;
38 static TAILQ_HEAD(owindows_head, owindow) owindows;
39
40 /* Holds the JSON which will be returned via IPC or NULL for the default return
41  * message */
42 static char *json_output;
43
44 /* We don’t need yydebug for now, as we got decent error messages using
45  * yyerror(). Should you ever want to extend the parser, it might be handy
46  * to just comment it in again, so it stays here. */
47 //int cmdyydebug = 1;
48
49 void cmdyyerror(const char *error_message) {
50     ELOG("\n");
51     ELOG("CMD: %s\n", error_message);
52     ELOG("CMD: in command:\n");
53     ELOG("CMD:   %s\n", context->line_copy);
54     ELOG("CMD:   ");
55     for (int c = 1; c <= context->last_column; c++)
56         if (c >= context->first_column)
57                 printf("^");
58         else printf(" ");
59     printf("\n");
60     ELOG("\n");
61     context->compact_error = sstrdup(error_message);
62 }
63
64 int cmdyywrap() {
65     return 1;
66 }
67
68 char *parse_cmd(const char *new) {
69     cmdyy_scan_string(new);
70
71     match_init(&current_match);
72     context = scalloc(sizeof(struct context));
73     context->filename = "cmd";
74     FREE(json_output);
75     if (cmdyyparse() != 0) {
76         fprintf(stderr, "Could not parse command\n");
77         asprintf(&json_output, "{\"success\":false, \"error\":\"%s at position %d\"}",
78                  context->compact_error, context->first_column);
79         FREE(context->line_copy);
80         FREE(context->compact_error);
81         free(context);
82         return json_output;
83     }
84     printf("done, json output = %s\n", json_output);
85
86     FREE(context->line_copy);
87     FREE(context->compact_error);
88     free(context);
89     return json_output;
90 }
91
92 %}
93
94 %error-verbose
95 %lex-param { struct context *context }
96
97 %union {
98     char *string;
99     char chr;
100     int number;
101 }
102
103 %token TOK_ATTACH "attach"
104 %token TOK_EXEC "exec"
105 %token TOK_EXIT "exit"
106 %token TOK_RELOAD "reload"
107 %token TOK_RESTART "restart"
108 %token TOK_KILL "kill"
109 %token TOK_FULLSCREEN "fullscreen"
110 %token TOK_GLOBAL "global"
111 %token TOK_LAYOUT "layout"
112 %token TOK_DEFAULT "default"
113 %token TOK_STACKED "stacked"
114 %token TOK_TABBED "tabbed"
115 %token TOK_BORDER "border"
116 %token TOK_NORMAL "normal"
117 %token TOK_NONE "none"
118 %token TOK_1PIXEL "1pixel"
119 %token TOK_MODE "mode"
120 %token TOK_TILING "tiling"
121 %token TOK_FLOATING "floating"
122 %token TOK_WORKSPACE "workspace"
123 %token TOK_TOGGLE "toggle"
124 %token TOK_FOCUS "focus"
125 %token TOK_MOVE "move"
126 %token TOK_OPEN "open"
127 %token TOK_NEXT "next"
128 %token TOK_PREV "prev"
129 %token TOK_SPLIT "split"
130 %token TOK_HORIZONTAL "horizontal"
131 %token TOK_VERTICAL "vertical"
132 %token TOK_LEVEL "level"
133 %token TOK_UP "up"
134 %token TOK_DOWN "down"
135 %token TOK_LEFT "left"
136 %token TOK_RIGHT "right"
137 %token TOK_RESTORE "restore"
138 %token TOK_MARK "mark"
139 %token TOK_RESIZE "resize"
140 %token TOK_GROW "grow"
141 %token TOK_SHRINK "shrink"
142 %token TOK_PX "px"
143 %token TOK_OR "or"
144 %token TOK_PPT "ppt"
145 %token TOK_NOP "nop"
146
147 %token TOK_CLASS "class"
148 %token TOK_ID "id"
149 %token TOK_CON_ID "con_id"
150
151 %token WHITESPACE "<whitespace>"
152 %token STR "<string>"
153 %token NUMBER "<number>"
154
155 %%
156
157 commands: /* empty */
158     | commands optwhitespace ';' optwhitespace command
159     | command
160     {
161         owindow *current;
162
163         printf("single command completely parsed, dropping state...\n");
164         while (!TAILQ_EMPTY(&owindows)) {
165             current = TAILQ_FIRST(&owindows);
166             TAILQ_REMOVE(&owindows, current, owindows);
167             free(current);
168         }
169         match_init(&current_match);
170     }
171     ;
172
173 optwhitespace:
174     | WHITESPACE
175     ;
176
177 command:
178     match optwhitespace operations
179     ;
180
181 match:
182     | matchstart optwhitespace criteria optwhitespace matchend
183     {
184         printf("match parsed\n");
185     }
186     ;
187
188 matchstart:
189     '['
190     {
191         printf("start\n");
192         match_init(&current_match);
193         TAILQ_INIT(&owindows);
194         /* copy all_cons */
195         Con *con;
196         TAILQ_FOREACH(con, &all_cons, all_cons) {
197             owindow *ow = smalloc(sizeof(owindow));
198             ow->con = con;
199             TAILQ_INSERT_TAIL(&owindows, ow, owindows);
200         }
201     }
202     ;
203
204 matchend:
205     ']'
206     {
207         owindow *next, *current;
208
209         printf("match specification finished, matching...\n");
210         /* copy the old list head to iterate through it and start with a fresh
211          * list which will contain only matching windows */
212         struct owindows_head old = owindows;
213         TAILQ_INIT(&owindows);
214         for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
215             /* make a copy of the next pointer and advance the pointer to the
216              * next element as we are going to invalidate the element’s
217              * next/prev pointers by calling TAILQ_INSERT_TAIL later */
218             current = next;
219             next = TAILQ_NEXT(next, owindows);
220
221             printf("checking if con %p / %s matches\n", current->con, current->con->name);
222             if (current_match.con_id != NULL) {
223                 if (current_match.con_id == current->con) {
224                     printf("matches container!\n");
225                     TAILQ_INSERT_TAIL(&owindows, current, owindows);
226
227                 }
228             } else if (current_match.mark != NULL && current->con->mark != NULL &&
229                     strcasecmp(current_match.mark, current->con->mark) == 0) {
230                 printf("match by mark\n");
231                     TAILQ_INSERT_TAIL(&owindows, current, owindows);
232
233             } else {
234                 if (current->con->window == NULL)
235                     continue;
236                 if (match_matches_window(&current_match, current->con->window)) {
237                     printf("matches window!\n");
238                     TAILQ_INSERT_TAIL(&owindows, current, owindows);
239                 } else {
240                     printf("doesnt match\n");
241                     free(current);
242                 }
243             }
244         }
245
246         TAILQ_FOREACH(current, &owindows, owindows) {
247             printf("matching: %p / %s\n", current->con, current->con->name);
248         }
249
250     }
251     ;
252
253 criteria:
254     TOK_CLASS '=' STR
255     {
256         printf("criteria: class = %s\n", $<string>3);
257         current_match.class = $<string>3;
258     }
259     | TOK_CON_ID '=' STR
260     {
261         printf("criteria: id = %s\n", $<string>3);
262         /* TODO: correctly parse number */
263         current_match.con_id = (Con*)atoi($<string>3);
264         printf("id as int = %p\n", current_match.con_id);
265     }
266     | TOK_ID '=' STR
267     {
268         printf("criteria: window id = %s\n", $<string>3);
269         /* TODO: correctly parse number */
270         current_match.id = atoi($<string>3);
271         printf("window id as int = %d\n", current_match.id);
272     }
273     | TOK_MARK '=' STR
274     {
275         printf("criteria: mark = %s\n", $<string>3);
276         current_match.mark = $<string>3;
277     }
278     ;
279
280 operations:
281     operation
282     | operation optwhitespace
283     | operations ',' optwhitespace operation
284     ;
285
286 operation:
287     exec
288     | exit
289     | restart
290     | reload
291     | border
292     | layout
293     | restore
294     | move
295     | workspace
296     | attach
297     | focus
298     | kill
299     | open
300     | fullscreen
301     | next
302     | prev
303     | split
304     | mode
305     | level
306     | mark
307     | resize
308     | nop
309     ;
310
311 exec:
312     TOK_EXEC WHITESPACE STR
313     {
314         printf("should execute %s\n", $<string>3);
315         start_application($<string>3);
316         free($<string>3);
317     }
318     ;
319
320 exit:
321     TOK_EXIT
322     {
323         printf("exit, bye bye\n");
324         exit(0);
325     }
326     ;
327
328 reload:
329     TOK_RELOAD
330     {
331         printf("reloading\n");
332         load_configuration(conn, NULL, true);
333         /* Send an IPC event just in case the ws names have changed */
334         ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"reload\"}");
335     }
336     ;
337
338 restart:
339     TOK_RESTART
340     {
341         printf("restarting i3\n");
342         i3_restart(false);
343     }
344     ;
345
346 attach:
347     TOK_ATTACH
348     {
349         printf("should attach\n");
350     }
351     ;
352
353 focus:
354     TOK_FOCUS
355     {
356         owindow *current;
357
358         printf("should focus\n");
359         if (match_is_empty(&current_match)) {
360             /* TODO: better error message */
361             LOG("Error: The focus command requires you to use some criteria.\n");
362             break;
363         }
364
365         /* TODO: warning if the match contains more than one entry. does not
366          * make so much sense when focusing */
367         TAILQ_FOREACH(current, &owindows, owindows) {
368             LOG("focusing %p / %s\n", current->con, current->con->name);
369             con_focus(current->con);
370         }
371     }
372     ;
373
374 kill:
375     TOK_KILL
376     {
377         owindow *current;
378
379         printf("killing!\n");
380         /* check if the match is empty, not if the result is empty */
381         if (match_is_empty(&current_match))
382             tree_close_con();
383         else {
384             TAILQ_FOREACH(current, &owindows, owindows) {
385                 printf("matching: %p / %s\n", current->con, current->con->name);
386                 tree_close(current->con, true, false);
387             }
388         }
389
390     }
391     ;
392
393 workspace:
394     TOK_WORKSPACE WHITESPACE STR
395     {
396         printf("should switch to workspace %s\n", $<string>3);
397         workspace_show($<string>3);
398         free($<string>3);
399     }
400     ;
401
402 open:
403     TOK_OPEN
404     {
405         printf("opening new container\n");
406         Con *con = tree_open_con(NULL);
407         con_focus(con);
408         asprintf(&json_output, "{\"success\":true, \"id\":%ld}", (long int)con);
409     }
410     ;
411
412 fullscreen:
413     TOK_FULLSCREEN
414     {
415         printf("toggling fullscreen\n");
416         owindow *current;
417
418         /* check if the match is empty, not if the result is empty */
419         if (match_is_empty(&current_match))
420             con_toggle_fullscreen(focused);
421         else {
422             TAILQ_FOREACH(current, &owindows, owindows) {
423                 printf("matching: %p / %s\n", current->con, current->con->name);
424                 con_toggle_fullscreen(current->con);
425             }
426         }
427
428     }
429     ;
430
431 next:
432     TOK_NEXT WHITESPACE direction
433     {
434         /* TODO: use matches */
435         printf("should select next window in direction %c\n", $<chr>3);
436         tree_next('n', ($<chr>3 == 'v' ? VERT : HORIZ));
437     }
438     ;
439
440 prev:
441     TOK_PREV WHITESPACE direction
442     {
443         /* TODO: use matches */
444         printf("should select prev window in direction %c\n", $<chr>3);
445         tree_next('p', ($<chr>3 == 'v' ? VERT : HORIZ));
446     }
447     ;
448
449 split:
450     TOK_SPLIT WHITESPACE direction
451     {
452         /* TODO: use matches */
453         printf("splitting in direction %c\n", $<chr>3);
454         tree_split(focused, ($<chr>3 == 'v' ? VERT : HORIZ));
455     }
456     ;
457
458 direction:
459     TOK_HORIZONTAL  { $<chr>$ = 'h'; }
460     | 'h'           { $<chr>$ = 'h'; }
461     | TOK_VERTICAL  { $<chr>$ = 'v'; }
462     | 'v'           { $<chr>$ = 'v'; }
463     ;
464
465 mode:
466     TOK_MODE WHITESPACE window_mode
467     {
468         if ($<number>3 == TOK_TOGGLE) {
469             printf("should toggle mode\n");
470             toggle_floating_mode(focused, false);
471         } else {
472             printf("should switch mode to %s\n", ($<number>3 == TOK_FLOATING ? "floating" : "tiling"));
473             if ($<number>3 == TOK_FLOATING) {
474                 floating_enable(focused, false);
475             } else {
476                 floating_disable(focused, false);
477             }
478         }
479     }
480     ;
481
482 window_mode:
483     TOK_FLOATING    { $<number>$ = TOK_FLOATING; }
484     | TOK_TILING    { $<number>$ = TOK_TILING; }
485     | TOK_TOGGLE    { $<number>$ = TOK_TOGGLE; }
486     ;
487
488 border:
489     TOK_BORDER WHITESPACE border_style
490     {
491         printf("border style should be changed to %d\n", $<number>3);
492         owindow *current;
493
494         /* check if the match is empty, not if the result is empty */
495         if (match_is_empty(&current_match))
496             focused->border_style = $<number>3;
497         else {
498             TAILQ_FOREACH(current, &owindows, owindows) {
499                 printf("matching: %p / %s\n", current->con, current->con->name);
500                 current->con->border_style = $<number>3;
501             }
502         }
503     }
504     ;
505
506 border_style:
507     TOK_NORMAL      { $<number>$ = BS_NORMAL; }
508     | TOK_NONE      { $<number>$ = BS_NONE; }
509     | TOK_1PIXEL    { $<number>$ = BS_1PIXEL; }
510     ;
511
512
513 level:
514     TOK_LEVEL WHITESPACE level_direction
515     {
516         printf("level %c\n", $<chr>3);
517         if ($<chr>3 == 'u')
518             level_up();
519         else level_down();
520     }
521     ;
522
523 level_direction:
524     TOK_UP     { $<chr>$ = 'u'; }
525     | TOK_DOWN { $<chr>$ = 'd'; }
526     ;
527
528 move:
529     TOK_MOVE WHITESPACE direction
530     {
531         printf("moving in direction %d\n", $<number>3);
532         tree_move($<number>3);
533     }
534     | TOK_MOVE WHITESPACE TOK_WORKSPACE WHITESPACE STR
535     {
536         owindow *current;
537
538         printf("should move window to workspace %s\n", $<string>5);
539         /* get the workspace */
540         Con *ws = workspace_get($<string>5);
541         free($<string>5);
542
543         /* check if the match is empty, not if the result is empty */
544         if (match_is_empty(&current_match))
545             con_move_to_workspace(focused, ws);
546         else {
547             TAILQ_FOREACH(current, &owindows, owindows) {
548                 printf("matching: %p / %s\n", current->con, current->con->name);
549                 con_move_to_workspace(current->con, ws);
550             }
551         }
552     }
553     ;
554
555 restore:
556     TOK_RESTORE WHITESPACE STR
557     {
558         printf("restoring \"%s\"\n", $<string>3);
559         tree_append_json($<string>3);
560         free($<string>3);
561     }
562     ;
563
564 layout:
565     TOK_LAYOUT WHITESPACE layout_mode
566     {
567         printf("changing layout to %d\n", $<number>3);
568         owindow *current;
569
570         /* check if the match is empty, not if the result is empty */
571         if (match_is_empty(&current_match))
572             con_set_layout(focused->parent, $<number>3);
573         else {
574             TAILQ_FOREACH(current, &owindows, owindows) {
575                 printf("matching: %p / %s\n", current->con, current->con->name);
576                 con_set_layout(current->con, $<number>3);
577             }
578         }
579
580     }
581     ;
582
583 layout_mode:
584     TOK_DEFAULT   { $<number>$ = L_DEFAULT; }
585     | TOK_STACKED { $<number>$ = L_STACKED; }
586     | TOK_TABBED  { $<number>$ = L_TABBED; }
587     ;
588
589 mark:
590     TOK_MARK WHITESPACE STR
591     {
592         printf("marking window with str %s\n", $<string>3);
593         owindow *current;
594
595         /* check if the match is empty, not if the result is empty */
596         if (match_is_empty(&current_match))
597             focused->mark = sstrdup($<string>3);
598         else {
599             TAILQ_FOREACH(current, &owindows, owindows) {
600                 printf("matching: %p / %s\n", current->con, current->con->name);
601                 current->con->mark = sstrdup($<string>3);
602             }
603         }
604
605         free($<string>3);
606     }
607     ;
608
609 nop:
610     TOK_NOP WHITESPACE STR
611     {
612         printf("-------------------------------------------------\n");
613         printf("  NOP: %s\n", $<string>3);
614         printf("-------------------------------------------------\n");
615         free($<string>3);
616     }
617     ;
618
619 resize:
620     TOK_RESIZE WHITESPACE resize_way WHITESPACE direction resize_px resize_tiling
621     {
622         /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
623         printf("resizing in way %d, direction %d, px %d or ppt %d\n", $<number>3, $<number>5, $<number>6, $<number>7);
624         int direction = $<number>5;
625         int px = $<number>6;
626         int ppt = $<number>7;
627         if ($<number>3 == TOK_SHRINK) {
628             px *= -1;
629             ppt *= -1;
630         }
631
632         if (con_is_floating(focused)) {
633             printf("floating resize\n");
634             if (direction == TOK_UP) {
635                 focused->parent->rect.y -= px;
636                 focused->parent->rect.height += px;
637             } else if (direction == TOK_DOWN) {
638                 focused->rect.height += px;
639             } else if (direction == TOK_LEFT) {
640                 focused->rect.x -= px;
641                 focused->rect.width += px;
642             } else {
643                 focused->rect.width += px;
644             }
645         } else {
646             LOG("tiling resize\n");
647             /* get the default percentage */
648             int children = con_num_children(focused->parent);
649             Con *other;
650             LOG("ins. %d children\n", children);
651             double percentage = 1.0 / children;
652             LOG("default percentage = %f\n", percentage);
653
654             if (direction == TOK_UP || direction == TOK_LEFT) {
655                 other = TAILQ_PREV(focused, nodes_head, nodes);
656             } else {
657                 other = TAILQ_NEXT(focused, nodes);
658             }
659             if (other == TAILQ_END(workspaces)) {
660                 LOG("No other container in this direction found, cannot resize.\n");
661                 return 0;
662             }
663             LOG("other->percent = %f\n", other->percent);
664             LOG("focused->percent before = %f\n", focused->percent);
665             if (focused->percent == 0.0)
666                 focused->percent = percentage;
667             if (other->percent == 0.0)
668                 other->percent = percentage;
669             focused->percent += ((double)ppt / 100.0);
670             other->percent -= ((double)ppt / 100.0);
671             LOG("focused->percent after = %f\n", focused->percent);
672             LOG("other->percent after = %f\n", other->percent);
673         }
674     }
675     ;
676
677 resize_px:
678     /* empty */
679     {
680         $<number>$ = 10;
681     }
682     | WHITESPACE NUMBER WHITESPACE TOK_PX
683     {
684         $<number>$ = $<number>2;
685     }
686     ;
687
688 resize_tiling:
689     /* empty */
690     {
691         $<number>$ = 10;
692     }
693     | WHITESPACE TOK_OR WHITESPACE NUMBER WHITESPACE TOK_PPT
694     {
695         $<number>$ = $<number>4;
696     }
697     ;
698
699 resize_way:
700     TOK_GROW        { $<number>$ = TOK_GROW; }
701     | TOK_SHRINK    { $<number>$ = TOK_SHRINK; }
702     ;
703
704 direction:
705     TOK_UP          { $<number>$ = TOK_UP; }
706     | TOK_DOWN      { $<number>$ = TOK_DOWN; }
707     | TOK_LEFT      { $<number>$ = TOK_LEFT; }
708     | TOK_RIGHT     { $<number>$ = TOK_RIGHT; }
709     ;