fix awesomeconf struct handling in resizemouse() and movemouse(), fix bug on Xinerama
[awesome.git] / statusbar.c
blob63c414698447542fbc0ba384a2b2373bc8129ec5
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.width,
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,
178 drawable, awesomeconf->statusbar.width,
179 awesomeconf->statusbar.height, M_PI_2,
180 awesomeconf->statusbar.height, 0);
181 else
182 d = draw_rotate(awesomeconf->display, awesomeconf->phys_screen,
183 drawable, awesomeconf->statusbar.width,
184 awesomeconf->statusbar.height, - M_PI_2,
185 0, awesomeconf->statusbar.width);
186 XCopyArea(awesomeconf->display, d,
187 awesomeconf->statusbar.window,
188 DefaultGC(awesomeconf->display, awesomeconf->phys_screen), 0, 0,
189 awesomeconf->statusbar.height, awesomeconf->statusbar.width, 0, 0);
190 XFreePixmap(awesomeconf->display, d);
192 else
193 XCopyArea(awesomeconf->display, drawable,
194 awesomeconf->statusbar.window,
195 DefaultGC(awesomeconf->display, awesomeconf->phys_screen), 0, 0,
196 awesomeconf->statusbar.width, awesomeconf->statusbar.height, 0, 0);
197 XFreePixmap(awesomeconf->display, drawable);
198 XSync(awesomeconf->display, False);
201 void
202 initstatusbar(Display *disp, int screen, Statusbar *statusbar, Cursor cursor, XftFont *font, Layout *layouts, int nlayouts)
204 XSetWindowAttributes wa;
205 int i, phys_screen = get_phys_screen(disp, screen);
206 ScreenInfo *si = get_screen_info(disp, screen, NULL);
208 statusbar->height = font->height * 1.5;
210 if(statusbar->position == BarRight || statusbar->position == BarLeft)
211 statusbar->width = si[screen].height;
212 else
213 statusbar->width = si[screen].width;
215 p_delete(&si);
217 statusbar->screen = screen;
219 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
220 | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
221 wa.cursor = cursor;
222 wa.override_redirect = 1;
223 wa.background_pixmap = ParentRelative;
224 wa.event_mask = ButtonPressMask | ExposureMask;
225 if(statusbar->dposition == BarRight || statusbar->dposition == BarLeft)
226 statusbar->window = XCreateWindow(disp, RootWindow(disp, phys_screen), 0, 0,
227 statusbar->height,
228 statusbar->width,
229 0, DefaultDepth(disp, phys_screen), CopyFromParent,
230 DefaultVisual(disp, phys_screen),
231 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
232 else
233 statusbar->window = XCreateWindow(disp, RootWindow(disp, phys_screen), 0, 0,
234 statusbar->width,
235 statusbar->height,
236 0, DefaultDepth(disp, phys_screen), CopyFromParent,
237 DefaultVisual(disp, phys_screen),
238 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
239 XDefineCursor(disp, statusbar->window, cursor);
240 updatebarpos(disp, *statusbar);
241 XMapRaised(disp, statusbar->window);
243 for(i = 0; i < nlayouts; i++)
244 statusbar->txtlayoutwidth = MAX(statusbar->txtlayoutwidth,
245 (textwidth(disp, font, layouts[i].symbol)));
248 void
249 updatebarpos(Display *disp, Statusbar statusbar)
251 XEvent ev;
252 ScreenInfo *si = get_screen_info(disp, statusbar.screen, NULL);
254 XMapRaised(disp, statusbar.window);
255 switch (statusbar.position)
257 default:
258 XMoveWindow(disp, statusbar.window, si[statusbar.screen].x_org, si[statusbar.screen].y_org);
259 break;
260 case BarRight:
261 XMoveWindow(disp, statusbar.window, si[statusbar.screen].x_org + (si[statusbar.screen].width - statusbar.height), si[statusbar.screen].y_org);
262 break;
263 case BarBot:
264 XMoveWindow(disp, statusbar.window, si[statusbar.screen].x_org, si[statusbar.screen].height - statusbar.height);
265 break;
266 case BarOff:
267 XUnmapWindow(disp, statusbar.window);
268 break;
270 p_delete(&si);
271 XSync(disp, False);
272 while(XCheckMaskEvent(disp, EnterWindowMask, &ev));
275 void
276 uicb_togglebar(awesome_config *awesomeconf,
277 const char *arg __attribute__ ((unused)))
279 if(awesomeconf->statusbar.position == BarOff)
280 awesomeconf->statusbar.position = (awesomeconf->statusbar.dposition == BarOff) ? BarTop : awesomeconf->statusbar.dposition;
281 else
282 awesomeconf->statusbar.position = BarOff;
283 updatebarpos(awesomeconf->display, awesomeconf->statusbar);
284 arrange(awesomeconf);
288 void
289 uicb_setstatustext(awesome_config *awesomeconf, const char *arg)
291 if(!arg)
292 return;
293 a_strncpy(awesomeconf->statustext, sizeof(awesomeconf->statustext), arg, a_strlen(arg));
295 drawstatusbar(awesomeconf);
297 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99