diff options
author | Luminarys <kizunanohikari@gmail.com> | 2015-08-20 11:30:32 -0500 |
---|---|---|
committer | Luminarys <kizunanohikari@gmail.com> | 2015-08-20 11:30:32 -0500 |
commit | bc3babf566f1f8ad9db4d1f7b70a859f54ba6c48 (patch) | |
tree | 2de760a0246bc071f561065bc46c4c1007b5e10e | |
parent | Added in basic resize command (diff) | |
download | sway-bc3babf566f1f8ad9db4d1f7b70a859f54ba6c48.tar.gz sway-bc3babf566f1f8ad9db4d1f7b70a859f54ba6c48.tar.zst sway-bc3babf566f1f8ad9db4d1f7b70a859f54ba6c48.zip |
Added in basic resize command
-rw-r--r-- | include/ipc.h | 12 | ||||
-rw-r--r-- | sway/commands.c | 145 | ||||
-rw-r--r-- | sway/layout.c | 32 |
3 files changed, 183 insertions, 6 deletions
diff --git a/include/ipc.h b/include/ipc.h index 0b6441f6..2d71c666 100644 --- a/include/ipc.h +++ b/include/ipc.h | |||
@@ -2,14 +2,14 @@ | |||
2 | #define _SWAY_IPC_H | 2 | #define _SWAY_IPC_H |
3 | 3 | ||
4 | enum ipc_command_type { | 4 | enum ipc_command_type { |
5 | IPC_COMMAND = 0, | 5 | IPC_COMMAND = 0, |
6 | IPC_GET_WORKSPACES = 1, | 6 | IPC_GET_WORKSPACES = 1, |
7 | IPC_SUBSCRIBE = 2, | 7 | IPC_SUBSCRIBE = 2, |
8 | IPC_GET_OUTPUTS = 3, | 8 | IPC_GET_OUTPUTS = 3, |
9 | IPC_GET_TREE = 4, | 9 | IPC_GET_TREE = 4, |
10 | IPC_GET_MARKS = 5, | 10 | IPC_GET_MARKS = 5, |
11 | IPC_GET_BAR_CONFIG = 6, | 11 | IPC_GET_BAR_CONFIG = 6, |
12 | IPC_GET_VERSION = 7, | 12 | IPC_GET_VERSION = 7, |
13 | }; | 13 | }; |
14 | 14 | ||
15 | void ipc_init(void); | 15 | void ipc_init(void); |
diff --git a/sway/commands.c b/sway/commands.c index 644b8005..cdc80a0b 100644 --- a/sway/commands.c +++ b/sway/commands.c | |||
@@ -422,6 +422,150 @@ static bool cmd_reload(struct sway_config *config, int argc, char **argv) { | |||
422 | return true; | 422 | return true; |
423 | } | 423 | } |
424 | 424 | ||
425 | static bool cmd_resize(struct sway_config *config, int argc, char **argv) { | ||
426 | if (!checkarg(argc, "resize", EXPECTED_AT_LEAST, 3)) { | ||
427 | return false; | ||
428 | } | ||
429 | char *end; | ||
430 | int amount = (int)strtol(argv[2], &end, 10); | ||
431 | if (errno == ERANGE || amount == 0) { | ||
432 | errno = 0; | ||
433 | return false; | ||
434 | } | ||
435 | if (strcmp(argv[0], "shrink") != 0 && strcmp(argv[0], "grow") != 0) { | ||
436 | return false; | ||
437 | } | ||
438 | if (strcmp(argv[0], "shrink") == 0) { | ||
439 | amount *= -1; | ||
440 | } | ||
441 | |||
442 | swayc_t *parent = get_focused_view(active_workspace); | ||
443 | swayc_t *focused = parent; | ||
444 | swayc_t *sibling; | ||
445 | if (!parent) { | ||
446 | return true; | ||
447 | } | ||
448 | // Find the closest possible sibling and resize using that edge | ||
449 | int i; | ||
450 | if (strcmp(argv[1], "width") == 0) { | ||
451 | int lnumber = 0; | ||
452 | int rnumber = 0; | ||
453 | while (parent->parent) { | ||
454 | if (parent->parent->layout == L_HORIZ) { | ||
455 | for (i = 0; i < parent->parent->children->length; i++) { | ||
456 | sibling = parent->parent->children->items[i]; | ||
457 | if (sibling->x != focused->x) { | ||
458 | if (sibling->x < parent->x) { | ||
459 | lnumber++; | ||
460 | } else if (sibling->x > parent->x) { | ||
461 | rnumber++; | ||
462 | } | ||
463 | } | ||
464 | } | ||
465 | if (rnumber || lnumber) { | ||
466 | break; | ||
467 | } | ||
468 | } | ||
469 | parent = parent->parent; | ||
470 | } | ||
471 | if (parent == &root_container) { | ||
472 | return true; | ||
473 | } | ||
474 | sway_log(L_DEBUG, "Found the proper parent: %p. It has %d l conts, and %d r conts", parent->parent, lnumber, rnumber); | ||
475 | //TODO: Ensure rounding is done in such a way that there are NO pixel leaks | ||
476 | for (i = 0; i < parent->parent->children->length; i++) { | ||
477 | sibling = parent->parent->children->items[i]; | ||
478 | if (sibling->x != focused->x) { | ||
479 | if (sibling->x < parent->x) { | ||
480 | double pixels = -1 * (amount/lnumber); | ||
481 | if (lnumber) { | ||
482 | recursive_resize(sibling, pixels/2, MOVE_RIGHT); | ||
483 | } else { | ||
484 | recursive_resize(sibling, pixels, MOVE_RIGHT); | ||
485 | } | ||
486 | } else if (sibling->x > parent->x) { | ||
487 | double pixels = -1 * (amount/rnumber); | ||
488 | if (rnumber) { | ||
489 | recursive_resize(sibling, pixels/2, MOVE_LEFT); | ||
490 | } else { | ||
491 | recursive_resize(sibling, pixels, MOVE_LEFT); | ||
492 | } | ||
493 | } | ||
494 | } else { | ||
495 | if (rnumber != 0 && lnumber != 0) { | ||
496 | recursive_resize(parent, amount/2, MOVE_LEFT); | ||
497 | recursive_resize(parent, amount/2, MOVE_RIGHT); | ||
498 | } else if (rnumber) { | ||
499 | recursive_resize(parent, amount, MOVE_RIGHT); | ||
500 | } else if (lnumber) { | ||
501 | recursive_resize(parent, amount, MOVE_LEFT); | ||
502 | } | ||
503 | } | ||
504 | } | ||
505 | arrange_windows(active_workspace, -1, -1); | ||
506 | return true; | ||
507 | } else if (strcmp(argv[1], "height") == 0) { | ||
508 | int tnumber = 0; | ||
509 | int bnumber = 0; | ||
510 | while (parent->parent) { | ||
511 | if (parent->parent->layout == L_VERT) { | ||
512 | for (i = 0; i < parent->parent->children->length; i++) { | ||
513 | sibling = parent->parent->children->items[i]; | ||
514 | if (sibling->y != focused->y) { | ||
515 | if (sibling->y < parent->y) { | ||
516 | bnumber++; | ||
517 | } else if (sibling->y > parent->y) { | ||
518 | tnumber++; | ||
519 | } | ||
520 | } | ||
521 | } | ||
522 | if (bnumber || tnumber) { | ||
523 | break; | ||
524 | } | ||
525 | } | ||
526 | parent = parent->parent; | ||
527 | } | ||
528 | if (parent == &root_container) { | ||
529 | return true; | ||
530 | } | ||
531 | sway_log(L_DEBUG, "Found the proper parent: %p. It has %d b conts, and %d t conts", parent->parent, bnumber, tnumber); | ||
532 | //TODO: Ensure rounding is done in such a way that there are NO pixel leaks | ||
533 | for (i = 0; i < parent->parent->children->length; i++) { | ||
534 | sibling = parent->parent->children->items[i]; | ||
535 | if (sibling->y != focused->y) { | ||
536 | if (sibling->y < parent->y) { | ||
537 | double pixels = -1 * (amount/bnumber); | ||
538 | if (tnumber) { | ||
539 | recursive_resize(sibling, pixels/2, MOVE_UP); | ||
540 | } else { | ||
541 | recursive_resize(sibling, pixels, MOVE_UP); | ||
542 | } | ||
543 | } else if (sibling->x > parent->x) { | ||
544 | double pixels = -1 * (amount/tnumber); | ||
545 | if (bnumber) { | ||
546 | recursive_resize(sibling, pixels/2, MOVE_DOWN); | ||
547 | } else { | ||
548 | recursive_resize(sibling, pixels, MOVE_DOWN); | ||
549 | } | ||
550 | } | ||
551 | } else { | ||
552 | if (bnumber != 0 && tnumber != 0) { | ||
553 | recursive_resize(parent, amount/2, MOVE_UP); | ||
554 | recursive_resize(parent, amount/2, MOVE_DOWN); | ||
555 | } else if (tnumber) { | ||
556 | recursive_resize(parent, amount, MOVE_UP); | ||
557 | } else if (bnumber) { | ||
558 | recursive_resize(parent, amount, MOVE_DOWN); | ||
559 | } | ||
560 | } | ||
561 | } | ||
562 | arrange_windows(active_workspace, -1, -1); | ||
563 | return true; | ||
564 | } | ||
565 | sway_log(L_INFO, "Done with resize"); | ||
566 | return true; | ||
567 | } | ||
568 | |||
425 | static bool cmd_set(struct sway_config *config, int argc, char **argv) { | 569 | static bool cmd_set(struct sway_config *config, int argc, char **argv) { |
426 | if (!checkarg(argc, "set", EXPECTED_EQUAL_TO, 2)) { | 570 | if (!checkarg(argc, "set", EXPECTED_EQUAL_TO, 2)) { |
427 | return false; | 571 | return false; |
@@ -587,6 +731,7 @@ static struct cmd_handler handlers[] = { | |||
587 | { "layout", cmd_layout }, | 731 | { "layout", cmd_layout }, |
588 | { "log_colors", cmd_log_colors }, | 732 | { "log_colors", cmd_log_colors }, |
589 | { "reload", cmd_reload }, | 733 | { "reload", cmd_reload }, |
734 | { "resize", cmd_resize }, | ||
590 | { "set", cmd_set }, | 735 | { "set", cmd_set }, |
591 | { "split", cmd_split }, | 736 | { "split", cmd_split }, |
592 | { "splith", cmd_splith }, | 737 | { "splith", cmd_splith }, |
diff --git a/sway/layout.c b/sway/layout.c index 78b3dd27..b70e1041 100644 --- a/sway/layout.c +++ b/sway/layout.c | |||
@@ -370,3 +370,35 @@ swayc_t *get_swayc_in_direction(swayc_t *container, enum movement_direction dir) | |||
370 | } | 370 | } |
371 | } | 371 | } |
372 | } | 372 | } |
373 | |||
374 | void recursive_resize(swayc_t *container, double amount, enum movement_direction dir) { | ||
375 | int i; | ||
376 | bool layout_match = true; | ||
377 | if (dir == MOVE_LEFT) { | ||
378 | container->x += (int) amount; | ||
379 | container->width += (int) amount; | ||
380 | layout_match = container->layout == L_HORIZ; | ||
381 | } else if(dir == MOVE_RIGHT) { | ||
382 | container->width += (int) amount; | ||
383 | layout_match = container->layout == L_HORIZ; | ||
384 | } else if(dir == MOVE_UP) { | ||
385 | container->y += (int) amount; | ||
386 | container->height += (int) amount; | ||
387 | layout_match = container->layout == L_VERT; | ||
388 | } else if(dir == MOVE_DOWN) { | ||
389 | container->height += (int) amount; | ||
390 | layout_match = container->layout == L_VERT; | ||
391 | } | ||
392 | if (container->type == C_VIEW) { | ||
393 | return; | ||
394 | } | ||
395 | if (layout_match) { | ||
396 | for (i = 0; i < container->children->length; i++) { | ||
397 | recursive_resize(container->children->items[i], amount/container->children->length, dir); | ||
398 | } | ||
399 | } else { | ||
400 | for (i = 0; i < container->children->length; i++) { | ||
401 | recursive_resize(container->children->items[i], amount, dir); | ||
402 | } | ||
403 | } | ||
404 | } | ||