/* © 2006-2007 Riccardo Murri <riccardo.murri@gmail.com>
 * See LICENSE file for license details. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <X11/Xutil.h>

void
pileright(void) {
        unsigned int i, n, np, nx, ny, nw, nh, mw, mh, tw, th;
        Client *c;
        Window *winstack;

        domwfact = dozoom = True;
        dorestack = False; /* arrange() does the restacking */

        /* count tiled clients */
        for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
                n++;

        /* window geoms */
        mw = (n == 1) ? waw : mwfact * waw;
        if (n > 1) {
          th = wah / (n - 1);
          if(th < bh)
            th = bh;
        }
        else
          th = 0;

        winstack = emallocz((n+1)*sizeof(Window));
        winstack[0] = barwin; /* barwin always on top */
        np = 0;
        nx = wax;
        ny = way;
        for(i = 0, c = nexttiled(clients); c; i++, c = nexttiled(c->next)) {
                c->ismax = False;
                if(i == 0) { /* master */
                  nw = mw - 2 * c->border;
                  nh = wah - 2 * c->border;
                }
                else {  /* pile window */
                  if(1 == i)
                    nx += mw;
                  ny = way + i*th - wah;
                  nw = c->w;
                  nh = c->h;
                }
                winstack[++np] = c->win;
                resize(c, nx, ny, nw, nh, False);
                /* FIXME: if `resize(c, nx, ny, nw, nh, False);` only
                *  is used here, then windows are not moved back to
                *  the master area when the client in master area is
                *  killed.... why??
                */
                XMoveResizeWindow(dpy, c->win, nx, ny, nw, nh);
        }

        /* restack piled windows */
        if (np > 0)
          XRestackWindows(dpy, winstack, np+1);
        free(winstack);
}

void
pilebottom(void) {
        unsigned int i, n, np, nx, ny, nw, nh, mw, mh, tw, th;
        Client *c;
        Window *winstack;

        domwfact = dozoom = True;
        dorestack = False; /* arrange() does the restacking */

        /* count tiled clients */
        for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
                n++;

        /* window geoms */
        mh = (n == 1) ? wah : (mwfact * wah);
        if (n > 1) {
          tw = waw / (n - 1);
          if(tw < 2 * BORDERPX)
            tw = 2 * BORDERPX;
        }
        else
          tw = 0;

        winstack = emallocz((n+1)*sizeof(Window));
        winstack[0] = barwin; /* barwin always on top */
        np = 0;
        for(i = 0, c = nexttiled(clients); c; i++, c = nexttiled(c->next)) {
                c->ismax = False;
                if(i == 0) { /* master */
                  nx = wax;
                  ny = way;
                  nh = mh - 2*c->border;
                  nw = waw - 2*c->border;
                }
                else {  /* pile window */
                  nw = c->w;
                  nh = c->h;
                  nx = wax + (n-i-1)*tw;
                  ny = way + wah - nh - 2*c->border;
                }
                winstack[++np] = c->win;
                resize(c, nx, ny, nw, nh, False);
                /* FIXME: if `resize(c, nx, ny, nw, nh, False);` only
                *  is used here, then windows are not moved back to
                *  the master area when the client in master area is
                *  killed.... why??
                */
                XMoveResizeWindow(dpy, c->win, nx, ny, nw, nh);
        }

        /* restack piled windows */
        if (np > 0)
          XRestackWindows(dpy, winstack, np+1);
        free(winstack);
}


/** Move currently-selected client on top of visibility stack */
void
show(const char* arg) {
  if(!sel)
    return;

  XRaiseWindow(dpy, sel->win);
}


/** Move all floating clients on top of visibility stack */
void 
showfloating(const char *arg) {
  Client *c;

  if(!clients)
    return;

  for(c = clients; c; c = c->next)
    if(c->isfloating)
      XRaiseWindow(dpy, c->win);
}

