]> git.sur5r.net Git - i3/i3/blob - src/cmdparse.y
Re-implement the 'mode' command
[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_PREV            "prev"
146 %token              TOK_SPLIT           "split"
147 %token              TOK_HORIZONTAL      "horizontal"
148 %token              TOK_VERTICAL        "vertical"
149 %token              TOK_UP              "up"
150 %token              TOK_DOWN            "down"
151 %token              TOK_LEFT            "left"
152 %token              TOK_RIGHT           "right"
153 %token              TOK_PARENT          "parent"
154 %token              TOK_CHILD           "child"
155 %token              TOK_APPEND_LAYOUT   "append_layout"
156 %token              TOK_MARK            "mark"
157 %token              TOK_RESIZE          "resize"
158 %token              TOK_GROW            "grow"
159 %token              TOK_SHRINK          "shrink"
160 %token              TOK_PX              "px"
161 %token              TOK_OR              "or"
162 %token              TOK_PPT             "ppt"
163 %token              TOK_NOP             "nop"
164
165 %token              TOK_CLASS           "class"
166 %token              TOK_ID              "id"
167 %token              TOK_CON_ID          "con_id"
168 %token              TOK_TITLE           "title"
169
170 %token  <string>    STR                 "<string>"
171 %token  <number>    NUMBER              "<number>"
172
173 %type   <number>    direction
174 %type   <number>    split_direction
175 %type   <number>    level
176 %type   <number>    boolean
177 %type   <number>    border_style
178 %type   <number>    layout_mode
179 %type   <number>    resize_px
180 %type   <number>    resize_way
181 %type   <number>    resize_tiling
182 %type   <number>    optional_kill_mode
183
184 %%
185
186 commands:
187     commands ';' command
188     | command
189     {
190         owindow *current;
191
192         printf("single command completely parsed, dropping state...\n");
193         while (!TAILQ_EMPTY(&owindows)) {
194             current = TAILQ_FIRST(&owindows);
195             TAILQ_REMOVE(&owindows, current, owindows);
196             free(current);
197         }
198         match_init(&current_match);
199     }
200     ;
201
202 command:
203     match operations
204     ;
205
206 match:
207     | matchstart criteria matchend
208     {
209         printf("match parsed\n");
210     }
211     ;
212
213 matchstart:
214     '['
215     {
216         printf("start\n");
217         match_init(&current_match);
218         TAILQ_INIT(&owindows);
219         /* copy all_cons */
220         Con *con;
221         TAILQ_FOREACH(con, &all_cons, all_cons) {
222             owindow *ow = smalloc(sizeof(owindow));
223             ow->con = con;
224             TAILQ_INSERT_TAIL(&owindows, ow, owindows);
225         }
226     }
227     ;
228
229 matchend:
230     ']'
231     {
232         owindow *next, *current;
233
234         printf("match specification finished, matching...\n");
235         /* copy the old list head to iterate through it and start with a fresh
236          * list which will contain only matching windows */
237         struct owindows_head old = owindows;
238         TAILQ_INIT(&owindows);
239         for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
240             /* make a copy of the next pointer and advance the pointer to the
241              * next element as we are going to invalidate the element’s
242              * next/prev pointers by calling TAILQ_INSERT_TAIL later */
243             current = next;
244             next = TAILQ_NEXT(next, owindows);
245
246             printf("checking if con %p / %s matches\n", current->con, current->con->name);
247             if (current_match.con_id != NULL) {
248                 if (current_match.con_id == current->con) {
249                     printf("matches container!\n");
250                     TAILQ_INSERT_TAIL(&owindows, current, owindows);
251
252                 }
253             } else if (current_match.mark != NULL && current->con->mark != NULL &&
254                     strcasecmp(current_match.mark, current->con->mark) == 0) {
255                 printf("match by mark\n");
256                     TAILQ_INSERT_TAIL(&owindows, current, owindows);
257
258             } else {
259                 if (current->con->window == NULL)
260                     continue;
261                 if (match_matches_window(&current_match, current->con->window)) {
262                     printf("matches window!\n");
263                     TAILQ_INSERT_TAIL(&owindows, current, owindows);
264                 } else {
265                     printf("doesnt match\n");
266                     free(current);
267                 }
268             }
269         }
270
271         TAILQ_FOREACH(current, &owindows, owindows) {
272             printf("matching: %p / %s\n", current->con, current->con->name);
273         }
274
275     }
276     ;
277
278 criteria:
279     criteria criterion
280     | criterion
281     ;
282
283 criterion:
284     TOK_CLASS '=' STR
285     {
286         printf("criteria: class = %s\n", $3);
287         current_match.class = $3;
288     }
289     | TOK_CON_ID '=' STR
290     {
291         printf("criteria: id = %s\n", $3);
292         char *end;
293         long parsed = strtol($3, &end, 10);
294         if (parsed == LONG_MIN ||
295             parsed == LONG_MAX ||
296             parsed < 0 ||
297             (end && *end != '\0')) {
298             ELOG("Could not parse con id \"%s\"\n", $3);
299         } else {
300             current_match.con_id = (Con*)parsed;
301             printf("id as int = %p\n", current_match.con_id);
302         }
303     }
304     | TOK_ID '=' STR
305     {
306         printf("criteria: window id = %s\n", $3);
307         char *end;
308         long parsed = strtol($3, &end, 10);
309         if (parsed == LONG_MIN ||
310             parsed == LONG_MAX ||
311             parsed < 0 ||
312             (end && *end != '\0')) {
313             ELOG("Could not parse window id \"%s\"\n", $3);
314         } else {
315             current_match.id = parsed;
316             printf("window id as int = %d\n", current_match.id);
317         }
318     }
319     | TOK_MARK '=' STR
320     {
321         printf("criteria: mark = %s\n", $3);
322         current_match.mark = $3;
323     }
324     | TOK_TITLE '=' STR
325     {
326         printf("criteria: title = %s\n", $3);
327         current_match.title = $3;
328     }
329     ;
330
331 operations:
332     operation
333     | operations ',' operation
334     ;
335
336 operation:
337     exec
338     | exit
339     | restart
340     | reload
341     | border
342     | layout
343     | append_layout
344     | move
345     | workspace
346     | focus
347     | kill
348     | open
349     | fullscreen
350     | split
351     | floating
352     | mark
353     | resize
354     | nop
355     | mode
356     ;
357
358 exec:
359     TOK_EXEC STR
360     {
361         printf("should execute %s\n", $2);
362         start_application($2);
363         free($2);
364     }
365     ;
366
367 exit:
368     TOK_EXIT
369     {
370         printf("exit, bye bye\n");
371         exit(0);
372     }
373     ;
374
375 reload:
376     TOK_RELOAD
377     {
378         printf("reloading\n");
379         load_configuration(conn, NULL, true);
380         x_set_i3_atoms();
381         /* Send an IPC event just in case the ws names have changed */
382         ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"reload\"}");
383     }
384     ;
385
386 restart:
387     TOK_RESTART
388     {
389         printf("restarting i3\n");
390         i3_restart(false);
391     }
392     ;
393
394 focus:
395     TOK_FOCUS
396     {
397         owindow *current;
398
399         if (match_is_empty(&current_match)) {
400             ELOG("You have to specify which window/container should be focused.\n");
401             ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
402
403             asprintf(&json_output, "{\"success\":false, \"error\":\"You have to "
404                      "specify which window/container should be focused\"}");
405             break;
406         }
407
408         int count = 0;
409         TAILQ_FOREACH(current, &owindows, owindows) {
410             LOG("focusing %p / %s\n", current->con, current->con->name);
411             con_focus(current->con);
412             count++;
413         }
414
415         if (count > 1)
416             LOG("WARNING: Your criteria for the focus command matches %d containers, "
417                 "while only exactly one container can be focused at a time.\n", count);
418
419         tree_render();
420     }
421     | TOK_FOCUS direction
422     {
423         int direction = $2;
424         switch (direction) {
425             case TOK_LEFT:
426                 LOG("Focusing left\n");
427                 tree_next('p', HORIZ);
428                 break;
429             case TOK_RIGHT:
430                 LOG("Focusing right\n");
431                 tree_next('n', HORIZ);
432                 break;
433             case TOK_UP:
434                 LOG("Focusing up\n");
435                 tree_next('p', VERT);
436                 break;
437             case TOK_DOWN:
438                 LOG("Focusing down\n");
439                 tree_next('n', VERT);
440                 break;
441             default:
442                 ELOG("Invalid focus direction (%d)\n", direction);
443                 break;
444         }
445
446         tree_render();
447     }
448     | TOK_FOCUS level
449     {
450         if ($2 == TOK_PARENT)
451             level_up();
452         else level_down();
453
454         tree_render();
455     }
456     ;
457
458 level:
459     TOK_PARENT  { $$ = TOK_PARENT; }
460     | TOK_CHILD { $$ = TOK_CHILD;  }
461     ;
462
463 kill:
464     TOK_KILL optional_kill_mode
465     {
466         owindow *current;
467
468         printf("killing!\n");
469         /* check if the match is empty, not if the result is empty */
470         if (match_is_empty(&current_match))
471             tree_close_con($2);
472         else {
473             TAILQ_FOREACH(current, &owindows, owindows) {
474                 printf("matching: %p / %s\n", current->con, current->con->name);
475                 tree_close(current->con, $2, false);
476             }
477         }
478
479         tree_render();
480     }
481     ;
482
483 optional_kill_mode:
484     /* empty */             { $$ = KILL_WINDOW; }
485     | TOK_WINDOW { $$ = KILL_WINDOW; }
486     | TOK_CLIENT { $$ = KILL_CLIENT; }
487     ;
488
489 workspace:
490     TOK_WORKSPACE STR
491     {
492         printf("should switch to workspace %s\n", $2);
493         workspace_show($2);
494         free($2);
495
496         tree_render();
497     }
498     ;
499
500 open:
501     TOK_OPEN
502     {
503         printf("opening new container\n");
504         Con *con = tree_open_con(NULL, NULL);
505         con_focus(con);
506         asprintf(&json_output, "{\"success\":true, \"id\":%ld}", (long int)con);
507
508         tree_render();
509     }
510     ;
511
512 fullscreen:
513     TOK_FULLSCREEN
514     {
515         printf("toggling fullscreen\n");
516         owindow *current;
517
518
519         HANDLE_EMPTY_MATCH;
520
521         TAILQ_FOREACH(current, &owindows, owindows) {
522             printf("matching: %p / %s\n", current->con, current->con->name);
523             con_toggle_fullscreen(current->con);
524         }
525
526         tree_render();
527     }
528     ;
529
530 split:
531     TOK_SPLIT split_direction
532     {
533         /* TODO: use matches */
534         printf("splitting in direction %c\n", $2);
535         tree_split(focused, ($2 == 'v' ? VERT : HORIZ));
536
537         tree_render();
538     }
539     ;
540
541 split_direction:
542     TOK_HORIZONTAL  { $$ = 'h'; }
543     | 'h'           { $$ = 'h'; }
544     | TOK_VERTICAL  { $$ = 'v'; }
545     | 'v'           { $$ = 'v'; }
546     ;
547
548 floating:
549     TOK_FLOATING boolean
550     {
551         HANDLE_EMPTY_MATCH;
552
553         owindow *current;
554         TAILQ_FOREACH(current, &owindows, owindows) {
555             printf("matching: %p / %s\n", current->con, current->con->name);
556             if ($2 == TOK_TOGGLE) {
557                 printf("should toggle mode\n");
558                 toggle_floating_mode(current->con, false);
559             } else {
560                 printf("should switch mode to %s\n", ($2 == TOK_FLOATING ? "floating" : "tiling"));
561                 if ($2 == TOK_ENABLE) {
562                     floating_enable(current->con, false);
563                 } else {
564                     floating_disable(current->con, false);
565                 }
566             }
567         }
568
569         tree_render();
570     }
571     ;
572
573 boolean:
574     TOK_ENABLE    { $$ = TOK_ENABLE; }
575     | TOK_DISABLE { $$ = TOK_DISABLE; }
576     | TOK_TOGGLE  { $$ = TOK_TOGGLE; }
577     ;
578
579 border:
580     TOK_BORDER border_style
581     {
582         printf("border style should be changed to %d\n", $2);
583         owindow *current;
584
585         HANDLE_EMPTY_MATCH;
586
587         TAILQ_FOREACH(current, &owindows, owindows) {
588             printf("matching: %p / %s\n", current->con, current->con->name);
589             current->con->border_style = $2;
590         }
591
592         tree_render();
593     }
594     ;
595
596 border_style:
597     TOK_NORMAL      { $$ = BS_NORMAL; }
598     | TOK_NONE      { $$ = BS_NONE; }
599     | TOK_1PIXEL    { $$ = BS_1PIXEL; }
600     ;
601
602 move:
603     TOK_MOVE direction
604     {
605         printf("moving in direction %d\n", $2);
606         tree_move($2);
607
608         tree_render();
609     }
610     | TOK_MOVE TOK_WORKSPACE STR
611     {
612         owindow *current;
613
614         printf("should move window to workspace %s\n", $3);
615         /* get the workspace */
616         Con *ws = workspace_get($3, NULL);
617         free($3);
618
619         HANDLE_EMPTY_MATCH;
620
621         TAILQ_FOREACH(current, &owindows, owindows) {
622             printf("matching: %p / %s\n", current->con, current->con->name);
623             con_move_to_workspace(current->con, ws);
624         }
625
626         tree_render();
627     }
628     ;
629
630 append_layout:
631     TOK_APPEND_LAYOUT STR
632     {
633         printf("restoring \"%s\"\n", $2);
634         tree_append_json($2);
635         free($2);
636         tree_render();
637     }
638     ;
639
640 layout:
641     TOK_LAYOUT layout_mode
642     {
643         printf("changing layout to %d\n", $2);
644         owindow *current;
645
646         /* check if the match is empty, not if the result is empty */
647         if (match_is_empty(&current_match))
648             con_set_layout(focused->parent, $2);
649         else {
650             TAILQ_FOREACH(current, &owindows, owindows) {
651                 printf("matching: %p / %s\n", current->con, current->con->name);
652                 con_set_layout(current->con, $2);
653             }
654         }
655
656         tree_render();
657     }
658     ;
659
660 layout_mode:
661     TOK_DEFAULT   { $$ = L_DEFAULT; }
662     | TOK_STACKED { $$ = L_STACKED; }
663     | TOK_TABBED  { $$ = L_TABBED; }
664     ;
665
666 mark:
667     TOK_MARK STR
668     {
669         printf("marking window with str %s\n", $2);
670         owindow *current;
671
672         HANDLE_EMPTY_MATCH;
673
674         TAILQ_FOREACH(current, &owindows, owindows) {
675             printf("matching: %p / %s\n", current->con, current->con->name);
676             current->con->mark = sstrdup($2);
677         }
678
679         free($<string>2);
680
681         tree_render();
682     }
683     ;
684
685 nop:
686     TOK_NOP STR
687     {
688         printf("-------------------------------------------------\n");
689         printf("  NOP: %s\n", $2);
690         printf("-------------------------------------------------\n");
691         free($2);
692     }
693     ;
694
695 resize:
696     TOK_RESIZE resize_way direction resize_px resize_tiling
697     {
698         /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
699         printf("resizing in way %d, direction %d, px %d or ppt %d\n", $2, $3, $4, $5);
700         int direction = $3;
701         int px = $4;
702         int ppt = $5;
703         if ($2 == TOK_SHRINK) {
704             px *= -1;
705             ppt *= -1;
706         }
707
708         if (con_is_floating(focused)) {
709             printf("floating resize\n");
710             if (direction == TOK_UP) {
711                 focused->parent->rect.y -= px;
712                 focused->parent->rect.height += px;
713             } else if (direction == TOK_DOWN) {
714                 focused->rect.height += px;
715             } else if (direction == TOK_LEFT) {
716                 focused->rect.x -= px;
717                 focused->rect.width += px;
718             } else {
719                 focused->rect.width += px;
720             }
721         } else {
722             LOG("tiling resize\n");
723             /* get the default percentage */
724             int children = con_num_children(focused->parent);
725             Con *other;
726             LOG("ins. %d children\n", children);
727             double percentage = 1.0 / children;
728             LOG("default percentage = %f\n", percentage);
729
730             if (direction == TOK_UP || direction == TOK_LEFT) {
731                 other = TAILQ_PREV(focused, nodes_head, nodes);
732             } else {
733                 other = TAILQ_NEXT(focused, nodes);
734             }
735             if (other == TAILQ_END(workspaces)) {
736                 LOG("No other container in this direction found, cannot resize.\n");
737                 return 0;
738             }
739             LOG("other->percent = %f\n", other->percent);
740             LOG("focused->percent before = %f\n", focused->percent);
741             if (focused->percent == 0.0)
742                 focused->percent = percentage;
743             if (other->percent == 0.0)
744                 other->percent = percentage;
745             focused->percent += ((double)ppt / 100.0);
746             other->percent -= ((double)ppt / 100.0);
747             LOG("focused->percent after = %f\n", focused->percent);
748             LOG("other->percent after = %f\n", other->percent);
749         }
750
751         tree_render();
752     }
753     ;
754
755 resize_px:
756     /* empty */
757     {
758         $$ = 10;
759     }
760     | NUMBER TOK_PX
761     {
762         $$ = $1;
763     }
764     ;
765
766 resize_tiling:
767     /* empty */
768     {
769         $$ = 10;
770     }
771     | TOK_OR NUMBER TOK_PPT
772     {
773         $$ = $2;
774     }
775     ;
776
777 resize_way:
778     TOK_GROW        { $$ = TOK_GROW; }
779     | TOK_SHRINK    { $$ = TOK_SHRINK; }
780     ;
781
782 direction:
783     TOK_UP          { $$ = TOK_UP; }
784     | TOK_DOWN      { $$ = TOK_DOWN; }
785     | TOK_LEFT      { $$ = TOK_LEFT; }
786     | TOK_RIGHT     { $$ = TOK_RIGHT; }
787     ;
788
789 mode:
790     TOK_MODE STR
791     {
792         switch_mode($2);
793     }
794     ;