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