/* See LICENSE file for copyright and license details. */

/* appearance */
static const char font[]            = "-*-terminus-medium-r-normal-*-14-*-*-*-*-*-*-*";
static const char normbordercolor[] = "#4D5D53";
static const char normbgcolor[]     = "#4D5D53";
static const char normfgcolor[]     = "#FFA62F";
static const char selbordercolor[]  = "#58564a";
static const char selbgcolor[]      = "#58564a";
static const char selfgcolor[]      = "#FFA62F";
static unsigned int borderpx        = 1;        /* border pixel of windows */
static unsigned int snap            = 32;       /* snap pixel */
static Bool showbar                 = True;     /* False means no bar */
static Bool topbar                  = True;     /* False means bottom bar */


static const int nmaster      = 1;    /* number of clients in master area */




void tile_bt(Monitor *m)
{
      int x, y, h, w,mh;
      unsigned int i, n;
      Client *c;

      for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);

      if (n == 0)
            return;

      /* master */
      c = nexttiled(m->clients);

      mh = (int)(m->mfact * (double)m->wh);

      resize(c, m->wx , (n==1)?m->wy:(m->wy + mh) , m->ww - c->bw,  (n==1)?(m->wh-(2*c->bw)):(m->wh - mh-(2*c->bw))  , False);

      if (--n == 0)
            return;

      /* tile stack */
      x = m->wx;

      y = m->wy;

      w = m->ww /n;

      h = mh;

      if (h < bh)
            h = m->wh;

      for (i = 0, c = nexttiled(c->next); c; c = nexttiled(c->next), i++) {
            resize(c, x, y,(i + 1 == n)? (m->wx + m->ww - x -  (2*c->bw)) : (w -  (2*c->bw)) ,h - (2*c->bw), False);

            if (w != m->ww)
                  x = c->x + WIDTH(c);
      }
}

void fibonacci(Monitor *mon, int s) {
	unsigned int i, n, nx, ny, nw, nh;
	Client *c;

	for(n = 0, c = nexttiled(mon->clients); c; c = nexttiled(c->next), n++);
	if(n == 0)
		return;
	
	nx = mon->wx;
	ny = 0;
	nw = mon->ww;
	nh = mon->wh;
	
	for(i = 0, c = nexttiled(mon->clients); c; c = nexttiled(c->next)) {
		if((i % 2 && nh / 2 > 2 * c->bw)
		   || (!(i % 2) && nw / 2 > 2 * c->bw)) {
			if(i < n - 1) {
				if(i % 2)
					nh /= 2;
				else
					nw /= 2;
				if((i % 4) == 2 && !s)
					nx += nw;
				else if((i % 4) == 3 && !s)
					ny += nh;
			}
			if((i % 4) == 0) {
				if(s)
					ny += nh;
				else
					ny -= nh;
			}
			else if((i % 4) == 1)
				nx += nw;
			else if((i % 4) == 2)
				ny += nh;
			else if((i % 4) == 3) {
				if(s)
					nx += nw;
				else
					nx -= nw;
			}
			if(i == 0)
			{
				if(n != 1)
					nw = mon->ww * mon->mfact;
				ny = mon->wy;
			}
			else if(i == 1)
				nw = mon->ww - nw;
			i++;
		}
		resize(c, nx, ny, nw - 2 * c->bw, nh - 2 * c->bw, False);
	}
}

void dwindle(Monitor *mon) {
	fibonacci(mon, 1);
}

void spiral(Monitor *mon) {
	fibonacci(mon, 0);
}

/* tagging */
static const char *tags[] = {"1", "2", "3", "4", "5","6"};

static const Rule rules[] = {
      /* class      instance    title       tags mask     isfloating   monitor */
      { "Gimp",     NULL,       NULL,       0,            True,        -1 },
      { "conky",    NULL,       NULL,       0,            True,        -1 }
};

/* layout(s) */
static const float mfact      = 0.55; /* factor of master area size [0.05..0.95] */
static const Bool resizehints = True; /* False means respect size hints in tiled resizals */

static const Layout layouts[] = {
      { "[V]",     tile_bt},    
      { "[]->",    tile},    
      { "[@]",     spiral},   
      { "|@|",     dwindle},
      { "><>",     NULL},   
      { "[M]",     monocle},
};

/* key definitions */
#define MODKEY Mod4Mask
#define TAGKEYS(KEY,TAG) \
      { MODKEY,                KEY,      view,           {.ui = 1 << TAG} }, \
      { MODKEY|ControlMask,           KEY,      toggleview,     {.ui = 1 << TAG} }, \
      { MODKEY|ShiftMask,             KEY,      tag,            {.ui = 1 << TAG} }, \
      { MODKEY|ControlMask|ShiftMask, KEY,      toggletag,      {.ui = 1 << TAG} },

/* helper for spawning shell commands in the pre dwm-5.0 fashion */
#define SHCMD(cmd) { .v = (const char*[]){ "/bin/sh", "-c", cmd, NULL } }

/* commands */
static const char *dmenucmd[] = { "dmenu_run", "-fn", font, "-nb", normbgcolor, "-nf", normfgcolor, "-sb", selbgcolor, "-sf", selfgcolor, NULL };
static const char *termcmd[]  = { "xterm", NULL };
static const char * volpluscmd[] = {"/bin/volume", "+1"};
static const char * volmincmd[] = {"/bin/volume", "-1"};


static Key keys[] = {
      /* modifier                     key        function        argument */
      { MODKEY,                       XK_d,      spawn,          {.v = dmenucmd } },
      { MODKEY|ShiftMask,             XK_Return, spawn,          {.v = termcmd } },
      { MODKEY,                       XK_F7,     spawn,          {.v =  volmincmd } },
      { MODKEY,                       XK_F8,     spawn,          {.v = volpluscmd } },
      { MODKEY,                       XK_j,      focusstack,     {.i = +1 } },
      { MODKEY,                       XK_k,      focusstack,     {.i = -1 } },
      { MODKEY,                       XK_h,      setmfact,       {.f = -0.01} },
      { MODKEY,                       XK_l,      setmfact,       {.f = +0.01} },
      { MODKEY,                       XK_Return, zoom,           {0} },
      { MODKEY,                       XK_Tab,    view,           {0} },
      { MODKEY          ,             XK_q,      killclient,     {0} },
      { MODKEY,                       XK_b,      setlayout,      {.v = &layouts[0]} },
      { MODKEY,                       XK_t,      setlayout,      {.v = &layouts[1]} },
      { MODKEY,                       XK_s,      setlayout,      {.v = &layouts[2]} },
      { MODKEY,                       XK_m,      setlayout,      {.v = &layouts[4]} },
      { MODKEY,                       XK_space,  setlayout,      {0} },
      { MODKEY|ShiftMask,             XK_space,  togglefloating, {0} },
      { MODKEY,                       XK_0,      view,           {.ui = ~0 } },
      { MODKEY|ShiftMask,             XK_0,      tag,            {.ui = ~0 } },
      { MODKEY,                       XK_comma,  focusmon,       {.i = -1 } },
      { MODKEY,                       XK_period, focusmon,       {.i = +1 } },
      { MODKEY|ShiftMask,             XK_comma,  tagmon,         {.i = -1 } },
      { MODKEY|ShiftMask,             XK_period, tagmon,         {.i = +1 } },
      TAGKEYS(                        XK_F1,                      0)
      TAGKEYS(                        XK_F2,                      1)
      TAGKEYS(                        XK_F3,                      2)
      TAGKEYS(                        XK_F4,                      3)
      TAGKEYS(                        XK_F5,                      4)
      TAGKEYS(                        XK_F6,                      5)
            
      
};

/* button definitions */
/* click can be a tag number (starting at 0),
 * ClkLtSymbol, ClkStatusText, ClkWinTitle, ClkClientWin, or ClkRootWin */
static Button buttons[] = {
      /* click                event mask      button          function        argument */
      { ClkLtSymbol,          0,              Button1,        setlayout,      {.v = &layouts[0]} },
      { ClkLtSymbol,          0,              Button2,        setlayout,      {.v = &layouts[3]} },
      { ClkLtSymbol,          0,              Button3,        setlayout,      {.v = &layouts[2]} },
      { ClkWinTitle,          0,              Button2,        zoom,           {0} },
      { ClkStatusText,        0,              Button2,        spawn,          {.v = termcmd } },
      { ClkClientWin,         MODKEY,         Button1,        movemouse,      {0} },
      { ClkClientWin,         MODKEY,         Button2,        togglefloating, {0} },
      { ClkClientWin,         MODKEY,         Button3,        resizemouse,    {0} },
      { ClkTagBar,            0,              Button1,        view,           {0} },
      { ClkTagBar,            0,              Button3,        toggleview,     {0} },
      { ClkTagBar,            MODKEY,         Button1,        tag,            {0} },
      { ClkTagBar,            MODKEY,         Button3,        toggletag,      {0} },
};

