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