i3
commands.c
Go to the documentation of this file.
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009-2012 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  * commands.c: all command functions (see commands_parser.c)
8  *
9  */
10 #include <float.h>
11 #include <stdarg.h>
12 
13 #include "all.h"
14 
20 #define HANDLE_EMPTY_MATCH do { \
21  if (match_is_empty(current_match)) { \
22  owindow *ow = smalloc(sizeof(owindow)); \
23  ow->con = focused; \
24  TAILQ_INIT(&owindows); \
25  TAILQ_INSERT_TAIL(&owindows, ow, owindows); \
26  } \
27 } while (0)
28 
29 static owindows_head owindows;
30 
31 /*
32  * Returns true if a is definitely greater than b (using the given epsilon)
33  *
34  */
35 static bool definitelyGreaterThan(float a, float b, float epsilon) {
36  return (a - b) > ( (fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
37 }
38 
39 /*
40  * Returns an 'output' corresponding to one of left/right/down/up or a specific
41  * output name.
42  *
43  */
44 static Output *get_output_from_string(Output *current_output, const char *output_str) {
45  Output *output;
46 
47  if (strcasecmp(output_str, "left") == 0) {
48  output = get_output_next(D_LEFT, current_output);
49  if (!output)
50  output = get_output_most(D_RIGHT, current_output);
51  } else if (strcasecmp(output_str, "right") == 0) {
52  output = get_output_next(D_RIGHT, current_output);
53  if (!output)
54  output = get_output_most(D_LEFT, current_output);
55  } else if (strcasecmp(output_str, "up") == 0) {
56  output = get_output_next(D_UP, current_output);
57  if (!output)
58  output = get_output_most(D_DOWN, current_output);
59  } else if (strcasecmp(output_str, "down") == 0) {
60  output = get_output_next(D_DOWN, current_output);
61  if (!output)
62  output = get_output_most(D_UP, current_output);
63  } else output = get_output_by_name(output_str);
64 
65  return output;
66 }
67 
68 // This code is commented out because we might recycle it for popping up error
69 // messages on parser errors.
70 #if 0
71 static pid_t migration_pid = -1;
72 
73 /*
74  * Handler which will be called when we get a SIGCHLD for the nagbar, meaning
75  * it exited (or could not be started, depending on the exit code).
76  *
77  */
78 static void nagbar_exited(EV_P_ ev_child *watcher, int revents) {
79  ev_child_stop(EV_A_ watcher);
80  if (!WIFEXITED(watcher->rstatus)) {
81  fprintf(stderr, "ERROR: i3-nagbar did not exit normally.\n");
82  return;
83  }
84 
85  int exitcode = WEXITSTATUS(watcher->rstatus);
86  printf("i3-nagbar process exited with status %d\n", exitcode);
87  if (exitcode == 2) {
88  fprintf(stderr, "ERROR: i3-nagbar could not be found. Is it correctly installed on your system?\n");
89  }
90 
91  migration_pid = -1;
92 }
93 
94 /* We need ev >= 4 for the following code. Since it is not *that* important (it
95  * only makes sure that there are no i3-nagbar instances left behind) we still
96  * support old systems with libev 3. */
97 #if EV_VERSION_MAJOR >= 4
98 /*
99  * Cleanup handler. Will be called when i3 exits. Kills i3-nagbar with signal
100  * SIGKILL (9) to make sure there are no left-over i3-nagbar processes.
101  *
102  */
103 static void nagbar_cleanup(EV_P_ ev_cleanup *watcher, int revent) {
104  if (migration_pid != -1) {
105  LOG("Sending SIGKILL (9) to i3-nagbar with PID %d\n", migration_pid);
106  kill(migration_pid, SIGKILL);
107  }
108 }
109 #endif
110 
111 void cmd_MIGRATION_start_nagbar(void) {
112  if (migration_pid != -1) {
113  fprintf(stderr, "i3-nagbar already running.\n");
114  return;
115  }
116  fprintf(stderr, "Starting i3-nagbar, command parsing differs from expected output.\n");
117  ELOG("Please report this on IRC or in the bugtracker. Make sure to include the full debug level logfile:\n");
118  ELOG("i3-dump-log | gzip -9c > /tmp/i3.log.gz\n");
119  ELOG("FYI: Your i3 version is " I3_VERSION "\n");
120  migration_pid = fork();
121  if (migration_pid == -1) {
122  warn("Could not fork()");
123  return;
124  }
125 
126  /* child */
127  if (migration_pid == 0) {
128  char *pageraction;
129  sasprintf(&pageraction, "i3-sensible-terminal -e i3-sensible-pager \"%s\"", errorfilename);
130  char *argv[] = {
131  NULL, /* will be replaced by the executable path */
132  "-t",
133  "error",
134  "-m",
135  "You found a parsing error. Please, please, please, report it!",
136  "-b",
137  "show errors",
138  pageraction,
139  NULL
140  };
141  exec_i3_utility("i3-nagbar", argv);
142  }
143 
144  /* parent */
145  /* install a child watcher */
146  ev_child *child = smalloc(sizeof(ev_child));
147  ev_child_init(child, &nagbar_exited, migration_pid, 0);
148  ev_child_start(main_loop, child);
149 
150 /* We need ev >= 4 for the following code. Since it is not *that* important (it
151  * only makes sure that there are no i3-nagbar instances left behind) we still
152  * support old systems with libev 3. */
153 #if EV_VERSION_MAJOR >= 4
154  /* install a cleanup watcher (will be called when i3 exits and i3-nagbar is
155  * still running) */
156  ev_cleanup *cleanup = smalloc(sizeof(ev_cleanup));
157  ev_cleanup_init(cleanup, nagbar_cleanup);
158  ev_cleanup_start(main_loop, cleanup);
159 #endif
160 }
161 
162 #endif
163 
164 /*******************************************************************************
165  * Criteria functions.
166  ******************************************************************************/
167 
168 /*
169  * Initializes the specified 'Match' data structure and the initial state of
170  * commands.c for matching target windows of a command.
171  *
172  */
174  Con *con;
175  owindow *ow;
176 
177  DLOG("Initializing criteria, current_match = %p\n", current_match);
179  while (!TAILQ_EMPTY(&owindows)) {
180  ow = TAILQ_FIRST(&owindows);
182  free(ow);
183  }
185  /* copy all_cons */
187  ow = smalloc(sizeof(owindow));
188  ow->con = con;
190  }
191 }
192 
193 /*
194  * A match specification just finished (the closing square bracket was found),
195  * so we filter the list of owindows.
196  *
197  */
199  owindow *next, *current;
200 
201  DLOG("match specification finished, matching...\n");
202  /* copy the old list head to iterate through it and start with a fresh
203  * list which will contain only matching windows */
204  struct owindows_head old = owindows;
206  for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
207  /* make a copy of the next pointer and advance the pointer to the
208  * next element as we are going to invalidate the element’s
209  * next/prev pointers by calling TAILQ_INSERT_TAIL later */
210  current = next;
211  next = TAILQ_NEXT(next, owindows);
212 
213  DLOG("checking if con %p / %s matches\n", current->con, current->con->name);
214  if (current_match->con_id != NULL) {
215  if (current_match->con_id == current->con) {
216  DLOG("matches container!\n");
217  TAILQ_INSERT_TAIL(&owindows, current, owindows);
218  }
219  } else if (current_match->mark != NULL && current->con->mark != NULL &&
220  regex_matches(current_match->mark, current->con->mark)) {
221  DLOG("match by mark\n");
222  TAILQ_INSERT_TAIL(&owindows, current, owindows);
223  } else {
224  if (current->con->window == NULL)
225  continue;
226  if (match_matches_window(current_match, current->con->window)) {
227  DLOG("matches window!\n");
228  TAILQ_INSERT_TAIL(&owindows, current, owindows);
229  } else {
230  DLOG("doesnt match\n");
231  free(current);
232  }
233  }
234  }
235 
236  TAILQ_FOREACH(current, &owindows, owindows) {
237  DLOG("matching: %p / %s\n", current->con, current->con->name);
238  }
239 }
240 
241 /*
242  * Interprets a ctype=cvalue pair and adds it to the current match
243  * specification.
244  *
245  */
246 void cmd_criteria_add(I3_CMD, char *ctype, char *cvalue) {
247  DLOG("ctype=*%s*, cvalue=*%s*\n", ctype, cvalue);
248 
249  if (strcmp(ctype, "class") == 0) {
250  current_match->class = regex_new(cvalue);
251  return;
252  }
253 
254  if (strcmp(ctype, "instance") == 0) {
255  current_match->instance = regex_new(cvalue);
256  return;
257  }
258 
259  if (strcmp(ctype, "window_role") == 0) {
260  current_match->role = regex_new(cvalue);
261  return;
262  }
263 
264  if (strcmp(ctype, "con_id") == 0) {
265  char *end;
266  long parsed = strtol(cvalue, &end, 10);
267  if (parsed == LONG_MIN ||
268  parsed == LONG_MAX ||
269  parsed < 0 ||
270  (end && *end != '\0')) {
271  ELOG("Could not parse con id \"%s\"\n", cvalue);
272  } else {
273  current_match->con_id = (Con*)parsed;
274  printf("id as int = %p\n", current_match->con_id);
275  }
276  return;
277  }
278 
279  if (strcmp(ctype, "id") == 0) {
280  char *end;
281  long parsed = strtol(cvalue, &end, 10);
282  if (parsed == LONG_MIN ||
283  parsed == LONG_MAX ||
284  parsed < 0 ||
285  (end && *end != '\0')) {
286  ELOG("Could not parse window id \"%s\"\n", cvalue);
287  } else {
288  current_match->id = parsed;
289  printf("window id as int = %d\n", current_match->id);
290  }
291  return;
292  }
293 
294  if (strcmp(ctype, "con_mark") == 0) {
295  current_match->mark = regex_new(cvalue);
296  return;
297  }
298 
299  if (strcmp(ctype, "title") == 0) {
300  current_match->title = regex_new(cvalue);
301  return;
302  }
303 
304  if (strcmp(ctype, "urgent") == 0) {
305  if (strcasecmp(cvalue, "latest") == 0 ||
306  strcasecmp(cvalue, "newest") == 0 ||
307  strcasecmp(cvalue, "recent") == 0 ||
308  strcasecmp(cvalue, "last") == 0) {
309  current_match->urgent = U_LATEST;
310  } else if (strcasecmp(cvalue, "oldest") == 0 ||
311  strcasecmp(cvalue, "first") == 0) {
312  current_match->urgent = U_OLDEST;
313  }
314  return;
315  }
316 
317  ELOG("Unknown criterion: %s\n", ctype);
318 }
319 
320 /*
321  * Implementation of 'move [window|container] [to] workspace
322  * next|prev|next_on_output|prev_on_output'.
323  *
324  */
325 void cmd_move_con_to_workspace(I3_CMD, char *which) {
326  owindow *current;
327 
328  DLOG("which=%s\n", which);
329 
331 
332  /* get the workspace */
333  Con *ws;
334  if (strcmp(which, "next") == 0)
335  ws = workspace_next();
336  else if (strcmp(which, "prev") == 0)
337  ws = workspace_prev();
338  else if (strcmp(which, "next_on_output") == 0)
340  else if (strcmp(which, "prev_on_output") == 0)
342  else {
343  ELOG("BUG: called with which=%s\n", which);
344  cmd_output->json_output = sstrdup("{\"sucess\": false}");
345  return;
346  }
347 
348  TAILQ_FOREACH(current, &owindows, owindows) {
349  DLOG("matching: %p / %s\n", current->con, current->con->name);
350  con_move_to_workspace(current->con, ws, true, false);
351  }
352 
353  cmd_output->needs_tree_render = true;
354  // XXX: default reply for now, make this a better reply
355  cmd_output->json_output = sstrdup("{\"success\": true}");
356 }
357 
358 /*
359  * Implementation of 'move [window|container] [to] workspace <name>'.
360  *
361  */
363  if (strncasecmp(name, "__i3_", strlen("__i3_")) == 0) {
364  LOG("You cannot switch to the i3 internal workspaces.\n");
365  cmd_output->json_output = sstrdup("{\"sucess\": false}");
366  return;
367  }
368 
369  owindow *current;
370 
371  /* Error out early to not create a non-existing workspace (in
372  * workspace_get()) if we are not actually able to move anything. */
373  if (match_is_empty(current_match) && focused->type == CT_WORKSPACE) {
374  cmd_output->json_output = sstrdup("{\"sucess\": false}");
375  return;
376  }
377 
378  LOG("should move window to workspace %s\n", name);
379  /* get the workspace */
380  Con *ws = workspace_get(name, NULL);
381 
383 
384  TAILQ_FOREACH(current, &owindows, owindows) {
385  DLOG("matching: %p / %s\n", current->con, current->con->name);
386  con_move_to_workspace(current->con, ws, true, false);
387  }
388 
389  cmd_output->needs_tree_render = true;
390  // XXX: default reply for now, make this a better reply
391  cmd_output->json_output = sstrdup("{\"success\": true}");
392 }
393 
394 /*
395  * Implementation of 'move [window|container] [to] workspace number <number>'.
396  *
397  */
399  owindow *current;
400 
401  /* Error out early to not create a non-existing workspace (in
402  * workspace_get()) if we are not actually able to move anything. */
403  if (match_is_empty(current_match) && focused->type == CT_WORKSPACE) {
404  cmd_output->json_output = sstrdup("{\"sucess\": false}");
405  return;
406  }
407 
408  LOG("should move window to workspace with number %d\n", which);
409  /* get the workspace */
410  Con *output, *workspace = NULL;
411 
412  char *endptr = NULL;
413  long parsed_num = strtol(which, &endptr, 10);
414  if (parsed_num == LONG_MIN ||
415  parsed_num == LONG_MAX ||
416  parsed_num < 0 ||
417  *endptr != '\0') {
418  LOG("Could not parse \"%s\" as a number.\n", which);
419  cmd_output->json_output = sstrdup("{\"success\": false, "
420  "\"error\": \"Could not parse number\"}");
421  return;
422  }
423 
424  TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
425  GREP_FIRST(workspace, output_get_content(output),
426  child->num == parsed_num);
427 
428  if (!workspace) {
429  cmd_output->json_output = sstrdup("{\"success\": false, "
430  "\"error\": \"No such workspace\"}");
431  return;
432  }
433 
435 
436  TAILQ_FOREACH(current, &owindows, owindows) {
437  DLOG("matching: %p / %s\n", current->con, current->con->name);
438  con_move_to_workspace(current->con, workspace, true, false);
439  }
440 
441  cmd_output->needs_tree_render = true;
442  // XXX: default reply for now, make this a better reply
443  cmd_output->json_output = sstrdup("{\"success\": true}");
444 }
445 
446 static void cmd_resize_floating(I3_CMD, char *way, char *direction, Con *floating_con, int px) {
447  LOG("floating resize\n");
448  if (strcmp(direction, "up") == 0) {
449  floating_con->rect.y -= px;
450  floating_con->rect.height += px;
451  } else if (strcmp(direction, "down") == 0) {
452  floating_con->rect.height += px;
453  } else if (strcmp(direction, "left") == 0) {
454  floating_con->rect.x -= px;
455  floating_con->rect.width += px;
456  } else {
457  floating_con->rect.width += px;
458  }
459 }
460 
461 static void cmd_resize_tiling_direction(I3_CMD, char *way, char *direction, int ppt) {
462  LOG("tiling resize\n");
463  /* get the appropriate current container (skip stacked/tabbed cons) */
464  Con *current = focused;
465  while (current->parent->layout == L_STACKED ||
466  current->parent->layout == L_TABBED)
467  current = current->parent;
468 
469  /* Then further go up until we find one with the matching orientation. */
470  orientation_t search_orientation =
471  (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0 ? HORIZ : VERT);
472 
473  while (current->type != CT_WORKSPACE &&
474  current->type != CT_FLOATING_CON &&
475  current->parent->orientation != search_orientation)
476  current = current->parent;
477 
478  /* get the default percentage */
479  int children = con_num_children(current->parent);
480  Con *other;
481  LOG("ins. %d children\n", children);
482  double percentage = 1.0 / children;
483  LOG("default percentage = %f\n", percentage);
484 
485  orientation_t orientation = current->parent->orientation;
486 
487  if ((orientation == HORIZ &&
488  (strcmp(direction, "up") == 0 || strcmp(direction, "down") == 0)) ||
489  (orientation == VERT &&
490  (strcmp(direction, "left") == 0 || strcmp(direction, "right") == 0))) {
491  LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
492  (orientation == HORIZ ? "horizontal" : "vertical"));
493  cmd_output->json_output = sstrdup("{\"sucess\": false}");
494  return;
495  }
496 
497  if (strcmp(direction, "up") == 0 || strcmp(direction, "left") == 0) {
498  other = TAILQ_PREV(current, nodes_head, nodes);
499  } else {
500  other = TAILQ_NEXT(current, nodes);
501  }
502  if (other == TAILQ_END(workspaces)) {
503  LOG("No other container in this direction found, cannot resize.\n");
504  cmd_output->json_output = sstrdup("{\"sucess\": false}");
505  return;
506  }
507  LOG("other->percent = %f\n", other->percent);
508  LOG("current->percent before = %f\n", current->percent);
509  if (current->percent == 0.0)
510  current->percent = percentage;
511  if (other->percent == 0.0)
512  other->percent = percentage;
513  double new_current_percent = current->percent + ((double)ppt / 100.0);
514  double new_other_percent = other->percent - ((double)ppt / 100.0);
515  LOG("new_current_percent = %f\n", new_current_percent);
516  LOG("new_other_percent = %f\n", new_other_percent);
517  /* Ensure that the new percentages are positive and greater than
518  * 0.05 to have a reasonable minimum size. */
519  if (definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON) &&
520  definitelyGreaterThan(new_other_percent, 0.05, DBL_EPSILON)) {
521  current->percent += ((double)ppt / 100.0);
522  other->percent -= ((double)ppt / 100.0);
523  LOG("current->percent after = %f\n", current->percent);
524  LOG("other->percent after = %f\n", other->percent);
525  } else {
526  LOG("Not resizing, already at minimum size\n");
527  }
528 }
529 
530 static void cmd_resize_tiling_width_height(I3_CMD, char *way, char *direction, int ppt) {
531  LOG("width/height resize\n");
532  /* get the appropriate current container (skip stacked/tabbed cons) */
533  Con *current = focused;
534  while (current->parent->layout == L_STACKED ||
535  current->parent->layout == L_TABBED)
536  current = current->parent;
537 
538  /* Then further go up until we find one with the matching orientation. */
539  orientation_t search_orientation =
540  (strcmp(direction, "width") == 0 ? HORIZ : VERT);
541 
542  while (current->type != CT_WORKSPACE &&
543  current->type != CT_FLOATING_CON &&
544  current->parent->orientation != search_orientation)
545  current = current->parent;
546 
547  /* get the default percentage */
548  int children = con_num_children(current->parent);
549  LOG("ins. %d children\n", children);
550  double percentage = 1.0 / children;
551  LOG("default percentage = %f\n", percentage);
552 
553  orientation_t orientation = current->parent->orientation;
554 
555  if ((orientation == HORIZ &&
556  strcmp(direction, "height") == 0) ||
557  (orientation == VERT &&
558  strcmp(direction, "width") == 0)) {
559  LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
560  (orientation == HORIZ ? "horizontal" : "vertical"));
561  cmd_output->json_output = sstrdup("{\"sucess\": false}");
562  return;
563  }
564 
565  if (children == 1) {
566  LOG("This is the only container, cannot resize.\n");
567  cmd_output->json_output = sstrdup("{\"sucess\": false}");
568  return;
569  }
570 
571  /* Ensure all the other children have a percentage set. */
572  Con *child;
573  TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
574  LOG("child->percent = %f (child %p)\n", child->percent, child);
575  if (child->percent == 0.0)
576  child->percent = percentage;
577  }
578 
579  double new_current_percent = current->percent + ((double)ppt / 100.0);
580  double subtract_percent = ((double)ppt / 100.0) / (children - 1);
581  LOG("new_current_percent = %f\n", new_current_percent);
582  LOG("subtract_percent = %f\n", subtract_percent);
583  /* Ensure that the new percentages are positive and greater than
584  * 0.05 to have a reasonable minimum size. */
585  TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
586  if (child == current)
587  continue;
588  if (!definitelyGreaterThan(child->percent - subtract_percent, 0.05, DBL_EPSILON)) {
589  LOG("Not resizing, already at minimum size (child %p would end up with a size of %.f\n", child, child->percent - subtract_percent);
590  cmd_output->json_output = sstrdup("{\"sucess\": false}");
591  return;
592  }
593  }
594  if (!definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON)) {
595  LOG("Not resizing, already at minimum size\n");
596  cmd_output->json_output = sstrdup("{\"sucess\": false}");
597  return;
598  }
599 
600  current->percent += ((double)ppt / 100.0);
601  LOG("current->percent after = %f\n", current->percent);
602 
603  TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
604  if (child == current)
605  continue;
606  child->percent -= subtract_percent;
607  LOG("child->percent after (%p) = %f\n", child, child->percent);
608  }
609 }
610 
611 /*
612  * Implementation of 'resize grow|shrink <direction> [<px> px] [or <ppt> ppt]'.
613  *
614  */
615 void cmd_resize(I3_CMD, char *way, char *direction, char *resize_px, char *resize_ppt) {
616  /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
617  DLOG("resizing in way %s, direction %s, px %s or ppt %s\n", way, direction, resize_px, resize_ppt);
618  // TODO: We could either handle this in the parser itself as a separate token (and make the stack typed) or we need a better way to convert a string to a number with error checking
619  int px = atoi(resize_px);
620  int ppt = atoi(resize_ppt);
621  if (strcmp(way, "shrink") == 0) {
622  px *= -1;
623  ppt *= -1;
624  }
625 
626  Con *floating_con;
627  if ((floating_con = con_inside_floating(focused))) {
628  cmd_resize_floating(current_match, cmd_output, way, direction, floating_con, px);
629  } else {
630  if (strcmp(direction, "width") == 0 ||
631  strcmp(direction, "height") == 0)
632  cmd_resize_tiling_width_height(current_match, cmd_output, way, direction, ppt);
633  else cmd_resize_tiling_direction(current_match, cmd_output, way, direction, ppt);
634  }
635 
636  cmd_output->needs_tree_render = true;
637  // XXX: default reply for now, make this a better reply
638  cmd_output->json_output = sstrdup("{\"success\": true}");
639 }
640 
641 /*
642  * Implementation of 'border normal|none|1pixel|toggle'.
643  *
644  */
645 void cmd_border(I3_CMD, char *border_style_str) {
646  DLOG("border style should be changed to %s\n", border_style_str);
647  owindow *current;
648 
650 
651  TAILQ_FOREACH(current, &owindows, owindows) {
652  DLOG("matching: %p / %s\n", current->con, current->con->name);
653  int border_style = current->con->border_style;
654  if (strcmp(border_style_str, "toggle") == 0) {
655  border_style++;
656  border_style %= 3;
657  } else {
658  if (strcmp(border_style_str, "normal") == 0)
659  border_style = BS_NORMAL;
660  else if (strcmp(border_style_str, "none") == 0)
661  border_style = BS_NONE;
662  else if (strcmp(border_style_str, "1pixel") == 0)
663  border_style = BS_1PIXEL;
664  else {
665  ELOG("BUG: called with border_style=%s\n", border_style_str);
666  cmd_output->json_output = sstrdup("{\"sucess\": false}");
667  return;
668  }
669  }
670  con_set_border_style(current->con, border_style);
671  }
672 
673  cmd_output->needs_tree_render = true;
674  // XXX: default reply for now, make this a better reply
675  cmd_output->json_output = sstrdup("{\"success\": true}");
676 }
677 
678 /*
679  * Implementation of 'nop <comment>'.
680  *
681  */
682 void cmd_nop(I3_CMD, char *comment) {
683  LOG("-------------------------------------------------\n");
684  LOG(" NOP: %s\n", comment);
685  LOG("-------------------------------------------------\n");
686 }
687 
688 /*
689  * Implementation of 'append_layout <path>'.
690  *
691  */
692 void cmd_append_layout(I3_CMD, char *path) {
693  LOG("Appending layout \"%s\"\n", path);
694  tree_append_json(path);
695 
696  cmd_output->needs_tree_render = true;
697  // XXX: default reply for now, make this a better reply
698  cmd_output->json_output = sstrdup("{\"success\": true}");
699 }
700 
701 /*
702  * Implementation of 'workspace next|prev|next_on_output|prev_on_output'.
703  *
704  */
705 void cmd_workspace(I3_CMD, char *which) {
706  Con *ws;
707 
708  DLOG("which=%s\n", which);
709 
710  if (strcmp(which, "next") == 0)
711  ws = workspace_next();
712  else if (strcmp(which, "prev") == 0)
713  ws = workspace_prev();
714  else if (strcmp(which, "next_on_output") == 0)
716  else if (strcmp(which, "prev_on_output") == 0)
718  else {
719  ELOG("BUG: called with which=%s\n", which);
720  cmd_output->json_output = sstrdup("{\"sucess\": false}");
721  return;
722  }
723 
724  workspace_show(ws);
725 
726  cmd_output->needs_tree_render = true;
727  // XXX: default reply for now, make this a better reply
728  cmd_output->json_output = sstrdup("{\"success\": true}");
729 }
730 
731 /*
732  * Implementation of 'workspace number <number>'
733  *
734  */
735 void cmd_workspace_number(I3_CMD, char *which) {
736  Con *output, *workspace = NULL;
737 
738  char *endptr = NULL;
739  long parsed_num = strtol(which, &endptr, 10);
740  if (parsed_num == LONG_MIN ||
741  parsed_num == LONG_MAX ||
742  parsed_num < 0 ||
743  *endptr != '\0') {
744  LOG("Could not parse \"%s\" as a number.\n", which);
745  cmd_output->json_output = sstrdup("{\"success\": false, "
746  "\"error\": \"Could not parse number\"}");
747  return;
748  }
749 
750  TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
751  GREP_FIRST(workspace, output_get_content(output),
752  child->num == parsed_num);
753 
754  if (!workspace) {
755  LOG("There is no workspace with number %d.\n", parsed_num);
756  cmd_output->json_output = sstrdup("{\"success\": false, "
757  "\"error\": \"No such workspace\"}");
758  return;
759  }
760 
761  workspace_show(workspace);
762 
763  cmd_output->needs_tree_render = true;
764  // XXX: default reply for now, make this a better reply
765  cmd_output->json_output = sstrdup("{\"success\": true}");
766 }
767 
768 /*
769  * Implementation of 'workspace back_and_forth'.
770  *
771  */
774 
775  cmd_output->needs_tree_render = true;
776  // XXX: default reply for now, make this a better reply
777  cmd_output->json_output = sstrdup("{\"success\": true}");
778 }
779 
780 /*
781  * Implementation of 'workspace <name>'
782  *
783  */
784 void cmd_workspace_name(I3_CMD, char *name) {
785  if (strncasecmp(name, "__i3_", strlen("__i3_")) == 0) {
786  LOG("You cannot switch to the i3 internal workspaces.\n");
787  cmd_output->json_output = sstrdup("{\"sucess\": false}");
788  return;
789  }
790 
791  DLOG("should switch to workspace %s\n", name);
792 
794 
795  /* Check if the command wants to switch to the current workspace */
796  if (strcmp(ws->name, name) == 0) {
797  DLOG("This workspace is already focused.\n");
800  tree_render();
801  }
802  cmd_output->json_output = sstrdup("{\"sucess\": false}");
803  return;
804  }
805 
807 
808  cmd_output->needs_tree_render = true;
809  // XXX: default reply for now, make this a better reply
810  cmd_output->json_output = sstrdup("{\"success\": true}");
811 }
812 
813 /*
814  * Implementation of 'mark <mark>'
815  *
816  */
817 void cmd_mark(I3_CMD, char *mark) {
818  DLOG("Clearing all windows which have that mark first\n");
819 
820  Con *con;
822  if (con->mark && strcmp(con->mark, mark) == 0)
823  FREE(con->mark);
824  }
825 
826  DLOG("marking window with str %s\n", mark);
827  owindow *current;
828 
830 
831  TAILQ_FOREACH(current, &owindows, owindows) {
832  DLOG("matching: %p / %s\n", current->con, current->con->name);
833  current->con->mark = sstrdup(mark);
834  }
835 
836  cmd_output->needs_tree_render = true;
837  // XXX: default reply for now, make this a better reply
838  cmd_output->json_output = sstrdup("{\"success\": true}");
839 }
840 
841 /*
842  * Implementation of 'mode <string>'.
843  *
844  */
845 void cmd_mode(I3_CMD, char *mode) {
846  DLOG("mode=%s\n", mode);
847  switch_mode(mode);
848 
849  // XXX: default reply for now, make this a better reply
850  cmd_output->json_output = sstrdup("{\"success\": true}");
851 }
852 
853 /*
854  * Implementation of 'move [window|container] [to] output <str>'.
855  *
856  */
857 void cmd_move_con_to_output(I3_CMD, char *name) {
858  owindow *current;
859 
860  DLOG("should move window to output %s\n", name);
861 
863 
864  /* get the output */
865  Output *current_output = NULL;
866  Output *output;
867 
868  // TODO: fix the handling of criteria
869  TAILQ_FOREACH(current, &owindows, owindows)
870  current_output = get_output_containing(current->con->rect.x, current->con->rect.y);
871 
872  assert(current_output != NULL);
873 
874  // TODO: clean this up with commands.spec as soon as we switched away from the lex/yacc command parser
875  if (strcasecmp(name, "up") == 0)
876  output = get_output_next(D_UP, current_output);
877  else if (strcasecmp(name, "down") == 0)
878  output = get_output_next(D_DOWN, current_output);
879  else if (strcasecmp(name, "left") == 0)
880  output = get_output_next(D_LEFT, current_output);
881  else if (strcasecmp(name, "right") == 0)
882  output = get_output_next(D_RIGHT, current_output);
883  else
884  output = get_output_by_name(name);
885 
886  if (!output) {
887  LOG("No such output found.\n");
888  cmd_output->json_output = sstrdup("{\"sucess\": false}");
889  return;
890  }
891 
892  /* get visible workspace on output */
893  Con *ws = NULL;
895  if (!ws) {
896  cmd_output->json_output = sstrdup("{\"sucess\": false}");
897  return;
898  }
899 
900  TAILQ_FOREACH(current, &owindows, owindows) {
901  DLOG("matching: %p / %s\n", current->con, current->con->name);
902  con_move_to_workspace(current->con, ws, true, false);
903  }
904 
905  cmd_output->needs_tree_render = true;
906  // XXX: default reply for now, make this a better reply
907  cmd_output->json_output = sstrdup("{\"success\": true}");
908 }
909 
910 /*
911  * Implementation of 'floating enable|disable|toggle'
912  *
913  */
914 void cmd_floating(I3_CMD, char *floating_mode) {
915  owindow *current;
916 
917  DLOG("floating_mode=%s\n", floating_mode);
918 
920 
921  TAILQ_FOREACH(current, &owindows, owindows) {
922  DLOG("matching: %p / %s\n", current->con, current->con->name);
923  if (strcmp(floating_mode, "toggle") == 0) {
924  DLOG("should toggle mode\n");
925  toggle_floating_mode(current->con, false);
926  } else {
927  DLOG("should switch mode to %s\n", floating_mode);
928  if (strcmp(floating_mode, "enable") == 0) {
929  floating_enable(current->con, false);
930  } else {
931  floating_disable(current->con, false);
932  }
933  }
934  }
935 
936  cmd_output->needs_tree_render = true;
937  // XXX: default reply for now, make this a better reply
938  cmd_output->json_output = sstrdup("{\"success\": true}");
939 }
940 
941 /*
942  * Implementation of 'move workspace to [output] <str>'.
943  *
944  */
946  DLOG("should move workspace to output %s\n", name);
947 
949 
950  owindow *current;
951  TAILQ_FOREACH(current, &owindows, owindows) {
952  Output *current_output = get_output_containing(current->con->rect.x,
953  current->con->rect.y);
954  Output *output = get_output_from_string(current_output, name);
955  if (!output) {
956  LOG("No such output\n");
957  cmd_output->json_output = sstrdup("{\"sucess\": false}");
958  return;
959  }
960 
961  Con *content = output_get_content(output->con);
962  LOG("got output %p with content %p\n", output, content);
963 
964  Con *ws = con_get_workspace(current->con);
965  LOG("should move workspace %p / %s\n", ws, ws->name);
966 
967  if (con_num_children(ws->parent) == 1) {
968  LOG("Creating a new workspace to replace \"%s\" (last on its output).\n", ws->name);
969 
970  /* check if we can find a workspace assigned to this output */
971  bool used_assignment = false;
972  struct Workspace_Assignment *assignment;
974  if (strcmp(assignment->output, current_output->name) != 0)
975  continue;
976 
977  /* check if this workspace is already attached to the tree */
978  Con *workspace = NULL, *out;
979  TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
980  GREP_FIRST(workspace, output_get_content(out),
981  !strcasecmp(child->name, assignment->name));
982  if (workspace != NULL)
983  continue;
984 
985  /* so create the workspace referenced to by this assignment */
986  LOG("Creating workspace from assignment %s.\n", assignment->name);
987  workspace_get(assignment->name, NULL);
988  used_assignment = true;
989  break;
990  }
991 
992  /* if we couldn't create the workspace using an assignment, create
993  * it on the output */
994  if (!used_assignment)
995  create_workspace_on_output(current_output, ws->parent);
996 
997  /* notify the IPC listeners */
998  ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
999  }
1000 
1001  /* detach from the old output and attach to the new output */
1002  bool workspace_was_visible = workspace_is_visible(ws);
1003  Con *old_content = ws->parent;
1004  con_detach(ws);
1005  if (workspace_was_visible) {
1006  /* The workspace which we just detached was visible, so focus
1007  * the next one in the focus-stack. */
1008  Con *focus_ws = TAILQ_FIRST(&(old_content->focus_head));
1009  LOG("workspace was visible, focusing %p / %s now\n", focus_ws, focus_ws->name);
1010  workspace_show(focus_ws);
1011  }
1012  con_attach(ws, content, false);
1013 
1014  /* fix the coordinates of the floating containers */
1015  Con *floating_con;
1016  TAILQ_FOREACH(floating_con, &(ws->floating_head), floating_windows)
1017  floating_fix_coordinates(floating_con, &(old_content->rect), &(content->rect));
1018 
1019  ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"move\"}");
1020  if (workspace_was_visible) {
1021  /* Focus the moved workspace on the destination output. */
1022  workspace_show(ws);
1023  }
1024  }
1025 
1026  cmd_output->needs_tree_render = true;
1027  // XXX: default reply for now, make this a better reply
1028  cmd_output->json_output = sstrdup("{\"success\": true}");
1029 }
1030 
1031 /*
1032  * Implementation of 'split v|h|vertical|horizontal'.
1033  *
1034  */
1035 void cmd_split(I3_CMD, char *direction) {
1036  /* TODO: use matches */
1037  LOG("splitting in direction %c\n", direction[0]);
1038  tree_split(focused, (direction[0] == 'v' ? VERT : HORIZ));
1039 
1040  cmd_output->needs_tree_render = true;
1041  // XXX: default reply for now, make this a better reply
1042  cmd_output->json_output = sstrdup("{\"success\": true}");
1043 }
1044 
1045 /*
1046  * Implementaiton of 'kill [window|client]'.
1047  *
1048  */
1049 void cmd_kill(I3_CMD, char *kill_mode_str) {
1050  if (kill_mode_str == NULL)
1051  kill_mode_str = "window";
1052  owindow *current;
1053 
1054  DLOG("kill_mode=%s\n", kill_mode_str);
1055 
1056  int kill_mode;
1057  if (strcmp(kill_mode_str, "window") == 0)
1058  kill_mode = KILL_WINDOW;
1059  else if (strcmp(kill_mode_str, "client") == 0)
1060  kill_mode = KILL_CLIENT;
1061  else {
1062  ELOG("BUG: called with kill_mode=%s\n", kill_mode_str);
1063  cmd_output->json_output = sstrdup("{\"sucess\": false}");
1064  return;
1065  }
1066 
1067  /* check if the match is empty, not if the result is empty */
1069  tree_close_con(kill_mode);
1070  else {
1071  TAILQ_FOREACH(current, &owindows, owindows) {
1072  DLOG("matching: %p / %s\n", current->con, current->con->name);
1073  tree_close(current->con, kill_mode, false, false);
1074  }
1075  }
1076 
1077  cmd_output->needs_tree_render = true;
1078  // XXX: default reply for now, make this a better reply
1079  cmd_output->json_output = sstrdup("{\"success\": true}");
1080 }
1081 
1082 /*
1083  * Implementation of 'exec [--no-startup-id] <command>'.
1084  *
1085  */
1086 void cmd_exec(I3_CMD, char *nosn, char *command) {
1087  bool no_startup_id = (nosn != NULL);
1088 
1089  DLOG("should execute %s, no_startup_id = %d\n", command, no_startup_id);
1090  start_application(command, no_startup_id);
1091 
1092  // XXX: default reply for now, make this a better reply
1093  cmd_output->json_output = sstrdup("{\"success\": true}");
1094 }
1095 
1096 /*
1097  * Implementation of 'focus left|right|up|down'.
1098  *
1099  */
1100 void cmd_focus_direction(I3_CMD, char *direction) {
1101  if (focused &&
1102  focused->type != CT_WORKSPACE &&
1103  focused->fullscreen_mode != CF_NONE) {
1104  LOG("Cannot change focus while in fullscreen mode.\n");
1105  cmd_output->json_output = sstrdup("{\"sucess\": false}");
1106  return;
1107  }
1108 
1109  DLOG("direction = *%s*\n", direction);
1110 
1111  if (strcmp(direction, "left") == 0)
1112  tree_next('p', HORIZ);
1113  else if (strcmp(direction, "right") == 0)
1114  tree_next('n', HORIZ);
1115  else if (strcmp(direction, "up") == 0)
1116  tree_next('p', VERT);
1117  else if (strcmp(direction, "down") == 0)
1118  tree_next('n', VERT);
1119  else {
1120  ELOG("Invalid focus direction (%s)\n", direction);
1121  cmd_output->json_output = sstrdup("{\"sucess\": false}");
1122  return;
1123  }
1124 
1125  cmd_output->needs_tree_render = true;
1126  // XXX: default reply for now, make this a better reply
1127  cmd_output->json_output = sstrdup("{\"success\": true}");
1128 }
1129 
1130 /*
1131  * Implementation of 'focus tiling|floating|mode_toggle'.
1132  *
1133  */
1134 void cmd_focus_window_mode(I3_CMD, char *window_mode) {
1135  if (focused &&
1136  focused->type != CT_WORKSPACE &&
1137  focused->fullscreen_mode != CF_NONE) {
1138  LOG("Cannot change focus while in fullscreen mode.\n");
1139  cmd_output->json_output = sstrdup("{\"sucess\": false}");
1140  return;
1141  }
1142 
1143  DLOG("window_mode = %s\n", window_mode);
1144 
1145  Con *ws = con_get_workspace(focused);
1146  Con *current;
1147  if (ws != NULL) {
1148  if (strcmp(window_mode, "mode_toggle") == 0) {
1149  current = TAILQ_FIRST(&(ws->focus_head));
1150  if (current != NULL && current->type == CT_FLOATING_CON)
1151  window_mode = "tiling";
1152  else window_mode = "floating";
1153  }
1154  TAILQ_FOREACH(current, &(ws->focus_head), focused) {
1155  if ((strcmp(window_mode, "floating") == 0 && current->type != CT_FLOATING_CON) ||
1156  (strcmp(window_mode, "tiling") == 0 && current->type == CT_FLOATING_CON))
1157  continue;
1158 
1159  con_focus(con_descend_focused(current));
1160  break;
1161  }
1162  }
1163 
1164  cmd_output->needs_tree_render = true;
1165  // XXX: default reply for now, make this a better reply
1166  cmd_output->json_output = sstrdup("{\"success\": true}");
1167 }
1168 
1169 /*
1170  * Implementation of 'focus parent|child'.
1171  *
1172  */
1173 void cmd_focus_level(I3_CMD, char *level) {
1174  if (focused &&
1175  focused->type != CT_WORKSPACE &&
1176  focused->fullscreen_mode != CF_NONE) {
1177  LOG("Cannot change focus while in fullscreen mode.\n");
1178  cmd_output->json_output = sstrdup("{\"sucess\": false}");
1179  return;
1180  }
1181 
1182  DLOG("level = %s\n", level);
1183 
1184  if (strcmp(level, "parent") == 0)
1185  level_up();
1186  else level_down();
1187 
1188  cmd_output->needs_tree_render = true;
1189  // XXX: default reply for now, make this a better reply
1190  cmd_output->json_output = sstrdup("{\"success\": true}");
1191 }
1192 
1193 /*
1194  * Implementation of 'focus'.
1195  *
1196  */
1198  DLOG("current_match = %p\n", current_match);
1199  if (focused &&
1200  focused->type != CT_WORKSPACE &&
1201  focused->fullscreen_mode != CF_NONE) {
1202  LOG("Cannot change focus while in fullscreen mode.\n");
1203  cmd_output->json_output = sstrdup("{\"sucess\": false}");
1204  return;
1205  }
1206 
1207  owindow *current;
1208 
1210  ELOG("You have to specify which window/container should be focused.\n");
1211  ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
1212 
1213  sasprintf(&(cmd_output->json_output),
1214  "{\"success\":false, \"error\":\"You have to "
1215  "specify which window/container should be focused\"}");
1216  return;
1217  }
1218 
1219  int count = 0;
1220  TAILQ_FOREACH(current, &owindows, owindows) {
1221  Con *ws = con_get_workspace(current->con);
1222  /* If no workspace could be found, this was a dock window.
1223  * Just skip it, you cannot focus dock windows. */
1224  if (!ws)
1225  continue;
1226 
1227  /* If the container is not on the current workspace,
1228  * workspace_show() will switch to a different workspace and (if
1229  * enabled) trigger a mouse pointer warp to the currently focused
1230  * container (!) on the target workspace.
1231  *
1232  * Therefore, before calling workspace_show(), we make sure that
1233  * 'current' will be focused on the workspace. However, we cannot
1234  * just con_focus(current) because then the pointer will not be
1235  * warped at all (the code thinks we are already there).
1236  *
1237  * So we focus 'current' to make it the currently focused window of
1238  * the target workspace, then revert focus. */
1239  Con *currently_focused = focused;
1240  con_focus(current->con);
1241  con_focus(currently_focused);
1242 
1243  /* Now switch to the workspace, then focus */
1244  workspace_show(ws);
1245  LOG("focusing %p / %s\n", current->con, current->con->name);
1246  con_focus(current->con);
1247  count++;
1248  }
1249 
1250  if (count > 1)
1251  LOG("WARNING: Your criteria for the focus command matches %d containers, "
1252  "while only exactly one container can be focused at a time.\n", count);
1253 
1254  cmd_output->needs_tree_render = true;
1255  // XXX: default reply for now, make this a better reply
1256  cmd_output->json_output = sstrdup("{\"success\": true}");
1257 }
1258 
1259 /*
1260  * Implementation of 'fullscreen [global]'.
1261  *
1262  */
1263 void cmd_fullscreen(I3_CMD, char *fullscreen_mode) {
1264  if (fullscreen_mode == NULL)
1265  fullscreen_mode = "output";
1266  DLOG("toggling fullscreen, mode = %s\n", fullscreen_mode);
1267  owindow *current;
1268 
1270 
1271  TAILQ_FOREACH(current, &owindows, owindows) {
1272  printf("matching: %p / %s\n", current->con, current->con->name);
1273  con_toggle_fullscreen(current->con, (strcmp(fullscreen_mode, "global") == 0 ? CF_GLOBAL : CF_OUTPUT));
1274  }
1275 
1276  cmd_output->needs_tree_render = true;
1277  // XXX: default reply for now, make this a better reply
1278  cmd_output->json_output = sstrdup("{\"success\": true}");
1279 }
1280 
1281 /*
1282  * Implementation of 'move <direction> [<pixels> [px]]'.
1283  *
1284  */
1285 void cmd_move_direction(I3_CMD, char *direction, char *move_px) {
1286  // TODO: We could either handle this in the parser itself as a separate token (and make the stack typed) or we need a better way to convert a string to a number with error checking
1287  int px = atoi(move_px);
1288 
1289  /* TODO: make 'move' work with criteria. */
1290  DLOG("moving in direction %s, px %s\n", direction, move_px);
1291  if (con_is_floating(focused)) {
1292  DLOG("floating move with %d pixels\n", px);
1293  Rect newrect = focused->parent->rect;
1294  if (strcmp(direction, "left") == 0) {
1295  newrect.x -= px;
1296  } else if (strcmp(direction, "right") == 0) {
1297  newrect.x += px;
1298  } else if (strcmp(direction, "up") == 0) {
1299  newrect.y -= px;
1300  } else if (strcmp(direction, "down") == 0) {
1301  newrect.y += px;
1302  }
1303  floating_reposition(focused->parent, newrect);
1304  } else {
1305  tree_move((strcmp(direction, "right") == 0 ? D_RIGHT :
1306  (strcmp(direction, "left") == 0 ? D_LEFT :
1307  (strcmp(direction, "up") == 0 ? D_UP :
1308  D_DOWN))));
1309  cmd_output->needs_tree_render = true;
1310  }
1311 
1312  // XXX: default reply for now, make this a better reply
1313  cmd_output->json_output = sstrdup("{\"success\": true}");
1314 }
1315 
1316 /*
1317  * Implementation of 'layout default|stacked|stacking|tabbed'.
1318  *
1319  */
1320 void cmd_layout(I3_CMD, char *layout_str) {
1321  if (strcmp(layout_str, "stacking") == 0)
1322  layout_str = "stacked";
1323  DLOG("changing layout to %s\n", layout_str);
1324  owindow *current;
1325  int layout = (strcmp(layout_str, "default") == 0 ? L_DEFAULT :
1326  (strcmp(layout_str, "stacked") == 0 ? L_STACKED :
1327  L_TABBED));
1328 
1329  /* check if the match is empty, not if the result is empty */
1331  con_set_layout(focused->parent, layout);
1332  else {
1333  TAILQ_FOREACH(current, &owindows, owindows) {
1334  DLOG("matching: %p / %s\n", current->con, current->con->name);
1335  con_set_layout(current->con, layout);
1336  }
1337  }
1338 
1339  cmd_output->needs_tree_render = true;
1340  // XXX: default reply for now, make this a better reply
1341  cmd_output->json_output = sstrdup("{\"success\": true}");
1342 }
1343 
1344 /*
1345  * Implementaiton of 'exit'.
1346  *
1347  */
1349  LOG("Exiting due to user command.\n");
1350  exit(0);
1351 
1352  /* unreached */
1353 }
1354 
1355 /*
1356  * Implementaiton of 'reload'.
1357  *
1358  */
1360  LOG("reloading\n");
1361  kill_configerror_nagbar(false);
1362  load_configuration(conn, NULL, true);
1363  x_set_i3_atoms();
1364  /* Send an IPC event just in case the ws names have changed */
1365  ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"reload\"}");
1366 
1367  // XXX: default reply for now, make this a better reply
1368  cmd_output->json_output = sstrdup("{\"success\": true}");
1369 }
1370 
1371 /*
1372  * Implementaiton of 'restart'.
1373  *
1374  */
1376  LOG("restarting i3\n");
1377  i3_restart(false);
1378 
1379  // XXX: default reply for now, make this a better reply
1380  cmd_output->json_output = sstrdup("{\"success\": true}");
1381 }
1382 
1383 /*
1384  * Implementaiton of 'open'.
1385  *
1386  */
1388  LOG("opening new container\n");
1389  Con *con = tree_open_con(NULL, NULL);
1390  con_focus(con);
1391  sasprintf(&(cmd_output->json_output),
1392  "{\"success\":true, \"id\":%ld}", (long int)con);
1393 
1394  cmd_output->needs_tree_render = true;
1395 }
1396 
1397 /*
1398  * Implementation of 'focus output <output>'.
1399  *
1400  */
1402  owindow *current;
1403 
1404  DLOG("name = %s\n", name);
1405 
1407 
1408  /* get the output */
1409  Output *current_output = NULL;
1410  Output *output;
1411 
1412  TAILQ_FOREACH(current, &owindows, owindows)
1413  current_output = get_output_containing(current->con->rect.x, current->con->rect.y);
1414  assert(current_output != NULL);
1415 
1416  output = get_output_from_string(current_output, name);
1417 
1418  if (!output) {
1419  LOG("No such output found.\n");
1420  cmd_output->json_output = sstrdup("{\"sucess\": false}");
1421  return;
1422  }
1423 
1424  /* get visible workspace on output */
1425  Con *ws = NULL;
1426  GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
1427  if (!ws) {
1428  cmd_output->json_output = sstrdup("{\"sucess\": false}");
1429  return;
1430  }
1431 
1432  workspace_show(ws);
1433 
1434  cmd_output->needs_tree_render = true;
1435  // XXX: default reply for now, make this a better reply
1436  cmd_output->json_output = sstrdup("{\"success\": true}");
1437 }
1438 
1439 /*
1440  * Implementation of 'move [window|container] [to] [absolute] position <px> [px] <px> [px]
1441  *
1442  */
1443 void cmd_move_window_to_position(I3_CMD, char *method, char *cx, char *cy) {
1444 
1445  int x = atoi(cx);
1446  int y = atoi(cy);
1447 
1448  if (!con_is_floating(focused)) {
1449  ELOG("Cannot change position. The window/container is not floating\n");
1450  sasprintf(&(cmd_output->json_output),
1451  "{\"success\":false, \"error\":\"Cannot change position. "
1452  "The window/container is not floating.\"}");
1453  return;
1454  }
1455 
1456  if (strcmp(method, "absolute") == 0) {
1457  focused->parent->rect.x = x;
1458  focused->parent->rect.y = y;
1459 
1460  DLOG("moving to absolute position %d %d\n", x, y);
1462  cmd_output->needs_tree_render = true;
1463  }
1464 
1465  if (strcmp(method, "position") == 0) {
1466  Rect newrect = focused->parent->rect;
1467 
1468  DLOG("moving to position %d %d\n", x, y);
1469  newrect.x = x;
1470  newrect.y = y;
1471 
1472  floating_reposition(focused->parent, newrect);
1473  }
1474 
1475  // XXX: default reply for now, make this a better reply
1476  cmd_output->json_output = sstrdup("{\"success\": true}");
1477 }
1478 
1479 /*
1480  * Implementation of 'move [window|container] [to] [absolute] position center
1481  *
1482  */
1483 void cmd_move_window_to_center(I3_CMD, char *method) {
1484 
1485  if (!con_is_floating(focused)) {
1486  ELOG("Cannot change position. The window/container is not floating\n");
1487  sasprintf(&(cmd_output->json_output),
1488  "{\"success\":false, \"error\":\"Cannot change position. "
1489  "The window/container is not floating.\"}");
1490  }
1491 
1492  if (strcmp(method, "absolute") == 0) {
1493  Rect *rect = &focused->parent->rect;
1494 
1495  DLOG("moving to absolute center\n");
1496  rect->x = croot->rect.width/2 - rect->width/2;
1497  rect->y = croot->rect.height/2 - rect->height/2;
1498 
1500  cmd_output->needs_tree_render = true;
1501  }
1502 
1503  if (strcmp(method, "position") == 0) {
1504  Rect *wsrect = &con_get_workspace(focused)->rect;
1505  Rect newrect = focused->parent->rect;
1506 
1507  DLOG("moving to center\n");
1508  newrect.x = wsrect->width/2 - newrect.width/2;
1509  newrect.y = wsrect->height/2 - newrect.height/2;
1510 
1511  floating_reposition(focused->parent, newrect);
1512  }
1513 
1514  // XXX: default reply for now, make this a better reply
1515  cmd_output->json_output = sstrdup("{\"success\": true}");
1516 }
1517 
1518 /*
1519  * Implementation of 'move scratchpad'.
1520  *
1521  */
1523  DLOG("should move window to scratchpad\n");
1524  owindow *current;
1525 
1527 
1528  TAILQ_FOREACH(current, &owindows, owindows) {
1529  DLOG("matching: %p / %s\n", current->con, current->con->name);
1530  scratchpad_move(current->con);
1531  }
1532 
1533  cmd_output->needs_tree_render = true;
1534  // XXX: default reply for now, make this a better reply
1535  cmd_output->json_output = sstrdup("{\"success\": true}");
1536 }
1537 
1538 /*
1539  * Implementation of 'scratchpad show'.
1540  *
1541  */
1543  DLOG("should show scratchpad window\n");
1544  owindow *current;
1545 
1547  scratchpad_show(NULL);
1548  } else {
1549  TAILQ_FOREACH(current, &owindows, owindows) {
1550  DLOG("matching: %p / %s\n", current->con, current->con->name);
1551  scratchpad_show(current->con);
1552  }
1553  }
1554 
1555  cmd_output->needs_tree_render = true;
1556  // XXX: default reply for now, make this a better reply
1557  cmd_output->json_output = sstrdup("{\"success\": true}");
1558 }
1559 
1560 /*
1561  * Implementation of 'rename workspace <name> to <name>'
1562  *
1563  */
1564 void cmd_rename_workspace(I3_CMD, char *old_name, char *new_name) {
1565  LOG("Renaming workspace \"%s\" to \"%s\"\n", old_name, new_name);
1566 
1567  Con *output, *workspace = NULL;
1568  TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1569  GREP_FIRST(workspace, output_get_content(output),
1570  !strcasecmp(child->name, old_name));
1571 
1572  if (!workspace) {
1573  // TODO: we should include the old workspace name here and use yajl for
1574  // generating the reply.
1575  cmd_output->json_output = sstrdup("{\"success\": false, "
1576  "\"error\":\"Old workspace not found\"}");
1577  return;
1578  }
1579 
1580  Con *check_dest = NULL;
1581  TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
1582  GREP_FIRST(check_dest, output_get_content(output),
1583  !strcasecmp(child->name, new_name));
1584 
1585  if (check_dest != NULL) {
1586  // TODO: we should include the new workspace name here and use yajl for
1587  // generating the reply.
1588  cmd_output->json_output = sstrdup("{\"success\": false, "
1589  "\"error\":\"New workspace already exists\"}");
1590  return;
1591  }
1592 
1593  /* Change the name and try to parse it as a number. */
1594  FREE(workspace->name);
1595  workspace->name = sstrdup(new_name);
1596  char *endptr = NULL;
1597  long parsed_num = strtol(new_name, &endptr, 10);
1598  if (parsed_num == LONG_MIN ||
1599  parsed_num == LONG_MAX ||
1600  parsed_num < 0 ||
1601  endptr == new_name)
1602  workspace->num = -1;
1603  else workspace->num = parsed_num;
1604  LOG("num = %d\n", workspace->num);
1605 
1606  /* By re-attaching, the sort order will be correct afterwards. */
1607  Con *previously_focused = focused;
1608  Con *parent = workspace->parent;
1609  con_detach(workspace);
1610  con_attach(workspace, parent, false);
1611  /* Restore the previous focus since con_attach messes with the focus. */
1612  con_focus(previously_focused);
1613 
1614  cmd_output->needs_tree_render = true;
1615  cmd_output->json_output = sstrdup("{\"success\": true}");
1616 
1617  ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"rename\"}");
1618 }