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