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