rework IS_TILED with screen
[awesome.git] / layouts / tile.c
blob949395eb242a662e8e00d22075d9d4839fa60c2b
1 /*
2 * tile.c - tile layout
4 * Copyright © 2007 Julien Danjou <julien@danjou.info>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <stdio.h>
24 #include "util.h"
25 #include "screen.h"
26 #include "awesome.h"
27 #include "tag.h"
28 #include "layout.h"
29 #include "layouts/tile.h"
31 /* extern */
32 extern Client *sel, *clients;
34 void
35 uicb_setnmaster(Display *disp,
36 int screen,
37 DC * drawcontext,
38 awesome_config *awesomeconf,
39 const char * arg)
41 if(!arg || (!IS_ARRANGE(tile) && !IS_ARRANGE(tileleft)))
42 return;
44 if((awesomeconf->nmaster = (int) compute_new_value_from_arg(arg, (double) awesomeconf->nmaster)) < 0)
45 awesomeconf->nmaster = 0;
47 arrange(disp, screen, drawcontext, awesomeconf);
50 void
51 uicb_setncols(Display *disp,
52 int screen,
53 DC * drawcontext,
54 awesome_config *awesomeconf,
55 const char * arg)
57 if(!arg || (!IS_ARRANGE(tile) && !IS_ARRANGE(tileleft)))
58 return;
60 if((awesomeconf->ncols = (int) compute_new_value_from_arg(arg, (double) awesomeconf->ncols)) < 1)
61 awesomeconf->ncols = 1;
63 arrange(disp, screen, drawcontext, awesomeconf);
66 void
67 uicb_setmwfact(Display *disp,
68 int screen,
69 DC *drawcontext,
70 awesome_config * awesomeconf,
71 const char *arg)
73 if(!IS_ARRANGE(tile) && !IS_ARRANGE(tileleft))
74 return;
76 if((awesomeconf->mwfact = compute_new_value_from_arg(arg, awesomeconf->mwfact)) < 0.1)
77 awesomeconf->mwfact = 0.1;
78 else if(awesomeconf->mwfact > 0.9)
79 awesomeconf->mwfact = 0.9;
81 arrange(disp, screen, drawcontext, awesomeconf);
84 static void
85 _tile(Display *disp, int screen, awesome_config *awesomeconf, const Bool right)
87 /* windows area geometry */
88 int wah = 0, waw = 0, wax = 0, way = 0;
89 /* new coordinates */
90 unsigned int nx, ny, nw, nh;
91 /* master size */
92 unsigned int mw = 0, mh = 0;
93 int n, i, li, last_i = 0, masterwin = 0, otherwin = 0;
94 int screen_numbers = 1, use_screen = -1;
95 int real_ncols = 1, win_by_col = 1, current_col = 0;
96 ScreenInfo *screens_info = NULL;
97 Client *c;
99 screens_info = get_screen_info(disp, screen, awesomeconf->statusbar, &screen_numbers);
101 for(n = 0, c = clients; c; c = c->next)
102 if(IS_TILED(c, screen, awesomeconf->selected_tags, awesomeconf->ntags))
103 n++;
105 for(i = 0, c = clients; c; c = c->next)
107 if(!IS_TILED(c, screen, awesomeconf->selected_tags, awesomeconf->ntags))
108 continue;
110 if(use_screen == -1
111 || (screen_numbers > 1
112 && i
113 && ((i - last_i) >= masterwin + otherwin
114 || n == screen_numbers)))
116 use_screen++;
117 last_i = i;
119 wah = screens_info[use_screen].height;
120 waw = screens_info[use_screen].width;
121 wax = screens_info[use_screen].x_org;
122 way = screens_info[use_screen].y_org;
124 if(n >= awesomeconf->nmaster * screen_numbers)
126 masterwin = awesomeconf->nmaster;
127 otherwin = (n - (awesomeconf->nmaster * screen_numbers)) / screen_numbers;
128 if(use_screen == 0)
129 otherwin += (n - (awesomeconf->nmaster * screen_numbers)) % screen_numbers;
131 else
133 masterwin = n / screen_numbers;
134 /* first screen takes more master */
135 if(use_screen == 0)
136 masterwin += n % screen_numbers;
137 otherwin = 0;
140 if(awesomeconf->nmaster)
142 mh = masterwin ? wah / masterwin : waw;
143 mw = otherwin ? waw * awesomeconf->mwfact : waw;
145 else
146 mh = mw = 0;
148 mw -= 2 * c->border;
149 mh -= 2 * c->border;
151 if(otherwin < awesomeconf->ncols)
152 real_ncols = otherwin;
153 else
154 real_ncols = awesomeconf->ncols;
156 current_col = 0;
159 c->ismax = False;
160 li = last_i ? i - last_i : i;
161 if(li < awesomeconf->nmaster)
162 { /* master */
163 ny = way + li * (mh + 2 * c->border);
164 nx = wax + (right ? 0 : waw - (mw + 2 * c->border));
165 resize(c, nx, ny, mw, mh, awesomeconf->resize_hints);
167 else
168 { /* tile window */
169 win_by_col = otherwin / real_ncols;
171 if((li - awesomeconf->nmaster) && (li - awesomeconf->nmaster) % win_by_col == 0 && current_col < real_ncols - 1)
172 current_col++;
174 if(current_col == real_ncols - 1)
175 win_by_col += otherwin % real_ncols;
177 if(otherwin <= real_ncols)
178 nh = wah - 2 * c->border;
179 else
180 nh = (wah / win_by_col) - 2 * c->border;
182 nw = (waw - (mw + 2 * c->border)) / real_ncols - 2 * c->border;
184 if(li == awesomeconf->nmaster || otherwin <= real_ncols || (li - awesomeconf->nmaster) % win_by_col == 0)
185 ny = way;
186 else
187 ny = way + ((li - awesomeconf->nmaster) % win_by_col) * (nh + 2 * c->border);
189 nx = wax + current_col * nw + (right ? mw + 2 * c->border : 0);
190 resize(c, nx, ny, nw, nh, awesomeconf->resize_hints);
192 i++;
194 XFree(screens_info);
197 void
198 tile(Display *disp, int screen, awesome_config *awesomeconf)
200 _tile(disp, screen, awesomeconf, True);
203 void
204 tileleft(Display *disp, int screen, awesome_config *awesomeconf)
206 _tile(disp, screen, awesomeconf, False);