also update rcoords on configure request
[awesome.git] / statusbar.c
blobeed524d382d5e6d85991b5123c788a7c8ef5a8ff
1 /*
2 * statusbar.c - statusbar functions
4 * Copyright © 2007 Julien Danjou <julien@danjou.info>
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>
23 #include <math.h>
25 #include "layout.h"
26 #include "statusbar.h"
27 #include "draw.h"
28 #include "screen.h"
29 #include "util.h"
30 #include "layouts/tile.h"
32 /** Check if at least a client is tagged with tag number t and is on screen
33 * screen
34 * \param t tag number
35 * \param screen screen number
36 * \return True or False
38 static Bool
39 isoccupied(Client **head, unsigned int t, int screen)
41 Client *c;
43 for(c = *head; c; c = c->next)
44 if(c->tags[t] && c->screen == screen)
45 return True;
46 return False;
49 void
50 drawstatusbar(awesome_config *awesomeconf)
52 int z, i, x = 0, y = 0, w;
53 Client *sel = get_current_tag(awesomeconf->tags, awesomeconf->ntags)->client_sel;
54 Drawable drawable;
56 /* don't waste our time */
57 if(awesomeconf->statusbar.position == BarOff)
58 return;
60 drawable = XCreatePixmap(awesomeconf->display,
61 RootWindow(awesomeconf->display, awesomeconf->phys_screen),
62 awesomeconf->statusbar.width,
63 awesomeconf->statusbar.height,
64 DefaultDepth(awesomeconf->display, awesomeconf->phys_screen));
66 for(i = 0; i < awesomeconf->ntags; i++)
68 w = textwidth(awesomeconf->display, awesomeconf->font,
69 awesomeconf->tags[i].name);
70 if(awesomeconf->tags[i].selected)
72 drawtext(awesomeconf->display, awesomeconf->phys_screen,
73 x, y, w,
74 awesomeconf->statusbar.height,
75 drawable,
76 awesomeconf->statusbar.width,
77 awesomeconf->statusbar.height,
78 awesomeconf->font,
79 awesomeconf->tags[i].name, awesomeconf->colors_selected);
80 if(isoccupied(awesomeconf->clients, i, awesomeconf->screen))
81 drawrectangle(awesomeconf->display, awesomeconf->phys_screen,
82 x, y,
83 (awesomeconf->font->height + 2) / 4,
84 (awesomeconf->font->height + 2) / 4,
85 drawable,
86 awesomeconf->statusbar.width,
87 awesomeconf->statusbar.height,
88 sel && sel->tags[i],
89 awesomeconf->colors_selected[ColFG]);
91 else
93 drawtext(awesomeconf->display, awesomeconf->phys_screen,
94 x, y, w,
95 awesomeconf->statusbar.height,
96 drawable,
97 awesomeconf->statusbar.width,
98 awesomeconf->statusbar.height,
99 awesomeconf->font,
100 awesomeconf->tags[i].name, awesomeconf->colors_normal);
101 if(isoccupied(awesomeconf->clients, i, awesomeconf->screen))
102 drawrectangle(awesomeconf->display, awesomeconf->phys_screen,
103 x, y,
104 (awesomeconf->font->height + 2) / 4,
105 (awesomeconf->font->height + 2) / 4,
106 drawable,
107 awesomeconf->statusbar.width,
108 awesomeconf->statusbar.height,
109 sel && sel->tags[i],
110 awesomeconf->colors_normal[ColFG]);
112 x += w;
114 drawtext(awesomeconf->display, awesomeconf->phys_screen,
115 x, y, awesomeconf->statusbar.txtlayoutwidth,
116 awesomeconf->statusbar.height,
117 drawable,
118 awesomeconf->statusbar.width,
119 awesomeconf->statusbar.height,
120 awesomeconf->font,
121 get_current_layout(awesomeconf->tags, awesomeconf->ntags)->symbol,
122 awesomeconf->colors_normal);
123 z = x + awesomeconf->statusbar.txtlayoutwidth;
124 w = textwidth(awesomeconf->display, awesomeconf->font, awesomeconf->statustext);
125 x = awesomeconf->statusbar.width - w;
126 if(x < z)
128 x = z;
129 w = awesomeconf->statusbar.width - z;
131 drawtext(awesomeconf->display, awesomeconf->phys_screen,
132 x, y, w,
133 awesomeconf->statusbar.height,
134 drawable,
135 awesomeconf->statusbar.width,
136 awesomeconf->statusbar.height,
137 awesomeconf->font,
138 awesomeconf->statustext, awesomeconf->colors_normal);
139 if((w = x - z) > awesomeconf->statusbar.height)
141 x = z;
142 if(sel && sel->screen == awesomeconf->screen)
144 drawtext(awesomeconf->display, awesomeconf->phys_screen,
145 x, y, w,
146 awesomeconf->statusbar.height,
147 drawable,
148 awesomeconf->statusbar.width,
149 awesomeconf->statusbar.height,
150 awesomeconf->font,
151 sel->name, awesomeconf->colors_selected);
152 if(sel->isfloating)
153 drawcircle(awesomeconf->display, awesomeconf->phys_screen,
154 x, y,
155 (awesomeconf->font->height + 2) / 4,
156 drawable,
157 awesomeconf->statusbar.width,
158 awesomeconf->statusbar.height,
159 sel->ismax,
160 awesomeconf->colors_selected[ColFG]);
162 else
163 drawtext(awesomeconf->display, awesomeconf->phys_screen,
164 x, y, w,
165 awesomeconf->statusbar.height,
166 drawable,
167 awesomeconf->statusbar.width,
168 awesomeconf->statusbar.height,
169 awesomeconf->font,
170 NULL, awesomeconf->colors_normal);
172 if(awesomeconf->statusbar.position == BarRight
173 || awesomeconf->statusbar.position == BarLeft)
175 Drawable d;
176 if(awesomeconf->statusbar.position == BarRight)
177 d = draw_rotate(awesomeconf->display, awesomeconf->phys_screen, drawable,
178 awesomeconf->statusbar.width, awesomeconf->statusbar.height,
179 M_PI_2, awesomeconf->statusbar.height, 0);
180 else
181 d = draw_rotate(awesomeconf->display, awesomeconf->phys_screen, drawable,
182 awesomeconf->statusbar.width, awesomeconf->statusbar.height,
183 - M_PI_2, 0, awesomeconf->statusbar.width);
184 XCopyArea(awesomeconf->display, d,
185 awesomeconf->statusbar.window,
186 DefaultGC(awesomeconf->display, awesomeconf->phys_screen), 0, 0,
187 awesomeconf->statusbar.height, awesomeconf->statusbar.width, 0, 0);
188 XFreePixmap(awesomeconf->display, d);
190 else
191 XCopyArea(awesomeconf->display, drawable,
192 awesomeconf->statusbar.window,
193 DefaultGC(awesomeconf->display, awesomeconf->phys_screen), 0, 0,
194 awesomeconf->statusbar.width, awesomeconf->statusbar.height, 0, 0);
195 XFreePixmap(awesomeconf->display, drawable);
196 XSync(awesomeconf->display, False);
199 void
200 initstatusbar(Display *disp, int screen, Statusbar *statusbar, Cursor cursor, XftFont *font, Layout *layouts, int nlayouts)
202 XSetWindowAttributes wa;
203 int i, phys_screen = get_phys_screen(disp, screen);
204 ScreenInfo *si = get_screen_info(disp, screen, NULL);
206 statusbar->height = font->height * 1.5;
208 if(statusbar->position == BarRight || statusbar->position == BarLeft)
209 statusbar->width = si[screen].height;
210 else
211 statusbar->width = si[screen].width;
213 p_delete(&si);
215 statusbar->screen = screen;
217 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
218 | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
219 wa.cursor = cursor;
220 wa.override_redirect = 1;
221 wa.background_pixmap = ParentRelative;
222 wa.event_mask = ButtonPressMask | ExposureMask;
223 if(statusbar->dposition == BarRight || statusbar->dposition == BarLeft)
224 statusbar->window = XCreateWindow(disp, RootWindow(disp, phys_screen), 0, 0,
225 statusbar->height,
226 statusbar->width,
227 0, DefaultDepth(disp, phys_screen), CopyFromParent,
228 DefaultVisual(disp, phys_screen),
229 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
230 else
231 statusbar->window = XCreateWindow(disp, RootWindow(disp, phys_screen), 0, 0,
232 statusbar->width,
233 statusbar->height,
234 0, DefaultDepth(disp, phys_screen), CopyFromParent,
235 DefaultVisual(disp, phys_screen),
236 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
237 XDefineCursor(disp, statusbar->window, cursor);
238 updatebarpos(disp, *statusbar);
239 XMapRaised(disp, statusbar->window);
241 for(i = 0; i < nlayouts; i++)
242 statusbar->txtlayoutwidth = MAX(statusbar->txtlayoutwidth,
243 (textwidth(disp, font, layouts[i].symbol)));
246 void
247 updatebarpos(Display *disp, Statusbar statusbar)
249 XEvent ev;
250 ScreenInfo *si = get_screen_info(disp, statusbar.screen, NULL);
252 XMapRaised(disp, statusbar.window);
253 switch (statusbar.position)
255 default:
256 XMoveWindow(disp, statusbar.window, si[statusbar.screen].x_org, si[statusbar.screen].y_org);
257 break;
258 case BarRight:
259 XMoveWindow(disp, statusbar.window, si[statusbar.screen].x_org + (si[statusbar.screen].width - statusbar.height), si[statusbar.screen].y_org);
260 break;
261 case BarBot:
262 XMoveWindow(disp, statusbar.window, si[statusbar.screen].x_org, si[statusbar.screen].height - statusbar.height);
263 break;
264 case BarOff:
265 XUnmapWindow(disp, statusbar.window);
266 break;
268 p_delete(&si);
269 XSync(disp, False);
270 while(XCheckMaskEvent(disp, EnterWindowMask, &ev));
273 void
274 uicb_togglebar(awesome_config *awesomeconf,
275 const char *arg __attribute__ ((unused)))
277 if(awesomeconf->statusbar.position == BarOff)
278 awesomeconf->statusbar.position = (awesomeconf->statusbar.dposition == BarOff) ? BarTop : awesomeconf->statusbar.dposition;
279 else
280 awesomeconf->statusbar.position = BarOff;
281 updatebarpos(awesomeconf->display, awesomeconf->statusbar);
282 arrange(awesomeconf);
286 void
287 uicb_setstatustext(awesome_config *awesomeconf, const char *arg)
289 if(!arg)
290 return;
291 a_strncpy(awesomeconf->statustext, sizeof(awesomeconf->statustext), arg, a_strlen(arg));
293 drawstatusbar(awesomeconf);
295 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99