Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 367 lines (331 sloc) 11.898 kb
5927a38c »
2012-08-03 Name Change
1 /* bipolarbar.c
52bf26d3 »
2012-07-20 first commit
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 */
17
18 #include <X11/Xlib.h>
19 #include <X11/Xatom.h>
20 #include <X11/Xlocale.h>
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <locale.h>
25 #include <string.h>
26 #include <sys/select.h>
27 #include <unistd.h>
28
29 typedef struct {
30 unsigned long color;
31 GC gc;
32 } Theme;
33 static Theme theme[10];
34
35 typedef struct {
36 XFontStruct *font; /* font structure */
37 XFontSet fontset; /* fontset structure */
38 int height; /* height of the font */
39 int width;
40 unsigned int fh; /* Y coordinate to draw characters */
41 unsigned int ascent;
42 unsigned int descent;
43 } Iammanyfonts;
44
6da74312 »
2012-08-12 Added config.h.def to help with PKGBUILD
45 #include "config.h"
46
52bf26d3 »
2012-07-20 first commit
47 static void get_font();
48 static void update_right();
49 static void print_text();
50 static void print_right_text();
13972428 »
2012-08-21 fine tuning and makefile options
51 static int wc_size(char *string, unsigned int num);
52bf26d3 »
2012-07-20 first commit
52
4950e52c »
2012-07-29 Added changing the background colour and positional markers
53 static const char *defaultcolor[] = { colour0, colour1, colour2, colour3, colour4, colour5, colour6, colour7, colour8, colour9, };
52bf26d3 »
2012-07-20 first commit
54 static const char *font_list = FONT;
55
4950e52c »
2012-07-29 Added changing the background colour and positional markers
56 static unsigned int count, counted, j, k, m, bg;
57 static unsigned int text_length, text_start, new_length;
58 static unsigned int old_length;
13972428 »
2012-08-21 fine tuning and makefile options
59 static char output[256];
60 static char right[256];
52bf26d3 »
2012-07-20 first commit
61
62 static Display *dis;
13972428 »
2012-08-21 fine tuning and makefile options
63 static unsigned int sw, sh, height, width, screen;
64 static Window root, barwin;
52bf26d3 »
2012-07-20 first commit
65 static Drawable winbar;
66
67 static Iammanyfonts font;
68
69 void get_font() {
70 char *def, **missing;
71 int i, n;
72
73 missing = NULL;
74 if(strlen(font_list) > 0)
75 font.fontset = XCreateFontSet(dis, (char *)font_list, &missing, &n, &def);
76 if(missing) {
77 if(FONTS_ERROR < 1)
78 while(n--)
5927a38c »
2012-08-03 Name Change
79 fprintf(stderr, ":: bipolarbar :: missing fontset: %s\n", missing[n]);
52bf26d3 »
2012-07-20 first commit
80 XFreeStringList(missing);
81 }
82 if(font.fontset) {
83 XFontStruct **xfonts;
84 char **font_names;
85
86 font.ascent = font.descent = 0;
87 n = XFontsOfFontSet(font.fontset, &xfonts, &font_names);
13972428 »
2012-08-21 fine tuning and makefile options
88 for(i = 0, font.ascent = 0, font.descent = 0; i < n; ++i) {
52bf26d3 »
2012-07-20 first commit
89 if (font.ascent < (*xfonts)->ascent) font.ascent = (*xfonts)->ascent;
90 if (font.descent < (*xfonts)->descent) font.descent = (*xfonts)->descent;
13972428 »
2012-08-21 fine tuning and makefile options
91 ++xfonts;
52bf26d3 »
2012-07-20 first commit
92 }
93 font.width = XmbTextEscapement(font.fontset, " ", 1);
94 } else {
5927a38c »
2012-08-03 Name Change
95 fprintf(stderr, ":: bipolarbar :: Font '%s' Not Found\n:: bipolarbar :: Trying Font 'Fixed'\n", font_list);
52bf26d3 »
2012-07-20 first commit
96 if(!(font.font = XLoadQueryFont(dis, font_list))
97 && !(font.font = XLoadQueryFont(dis, "fixed")))
5927a38c »
2012-08-03 Name Change
98 fprintf(stderr, ":: bipolarbar :: Error, cannot load font: '%s'\n", font_list);
52bf26d3 »
2012-07-20 first commit
99 font.ascent = font.font->ascent;
100 font.descent = font.font->descent;
101 font.width = XTextWidth(font.font, " ", 1);
102 }
103 font.height = font.ascent + font.descent;
104 }
105
4950e52c »
2012-07-29 Added changing the background colour and positional markers
106 void update_output() {
13972428 »
2012-08-21 fine tuning and makefile options
107 j=1; bg = count = k = 0;
52bf26d3 »
2012-07-20 first commit
108 text_length = 0;
4950e52c »
2012-07-29 Added changing the background colour and positional markers
109 unsigned int n;
52bf26d3 »
2012-07-20 first commit
110 ssize_t num;
111 char win_name[256];
112
13972428 »
2012-08-21 fine tuning and makefile options
113 memset(output, '\0', 256);
4950e52c »
2012-07-29 Added changing the background colour and positional markers
114 if(!(num = read(STDIN_FILENO, output, sizeof(output)))) {
5927a38c »
2012-08-03 Name Change
115 fprintf(stderr, "bipolarbar :: FAILED TO READ STDIN!!\n");
4950e52c »
2012-07-29 Added changing the background colour and positional markers
116 strncpy(output, "FAILED TO READ STDIN!!", 24);
117 exit(1);
52bf26d3 »
2012-07-20 first commit
118 }
4950e52c »
2012-07-29 Added changing the background colour and positional markers
119
52bf26d3 »
2012-07-20 first commit
120 text_length = strlen(output);
13972428 »
2012-08-21 fine tuning and makefile options
121 for(n=0;n<=text_length;++n) {
4950e52c »
2012-07-29 Added changing the background colour and positional markers
122 while(output[n] == '&') {
123 if(output[n+1]-'0' < 10 && output[n+1]-'0' >= 0)
124 n += 2;
125 if(output[n+1] == 'B' && output[n+2]-'0' < 10 && output[n+2]-'0' >= 0)
126 n += 3;
127 }
128 if(output[n] == '\n' || output[n] == '\r') break;
52bf26d3 »
2012-07-20 first commit
129 win_name[count] = output[n];
13972428 »
2012-08-21 fine tuning and makefile options
130 ++count;
52bf26d3 »
2012-07-20 first commit
131 }
4950e52c »
2012-07-29 Added changing the background colour and positional markers
132 new_length = wc_size(win_name, count);
52bf26d3 »
2012-07-20 first commit
133 XFillRectangle(dis, winbar, theme[0].gc, 0, 0, old_length, height);
13972428 »
2012-08-21 fine tuning and makefile options
134 for(count=0;count<=text_length;++count) {
52bf26d3 »
2012-07-20 first commit
135 print_text();
136 }
0715579c »
2012-08-08 Fix for end of piped text not clearing properly sometimes with text l…
137 text_length = (old_length > new_length) ? old_length : new_length;
138 XCopyArea(dis, winbar, barwin, theme[1].gc, 0, 0, text_length, height, 0, 0);
52bf26d3 »
2012-07-20 first commit
139 XSync(dis, False);
140 old_length = new_length;
141 return;
142 }
143
144 void update_right() {
4950e52c »
2012-07-29 Added changing the background colour and positional markers
145 unsigned int text_l=0, p_length, q=0, n;
52bf26d3 »
2012-07-20 first commit
146 char bstring[256];
147 char *root_name;
4950e52c »
2012-07-29 Added changing the background colour and positional markers
148 j=1;m=0; bg = 0;
52bf26d3 »
2012-07-20 first commit
149
150 if(!(XFetchName(dis, root, &root_name))) {
5927a38c »
2012-08-03 Name Change
151 strcpy(right, "&1bipolarbar ");
4950e52c »
2012-07-29 Added changing the background colour and positional markers
152 text_l = 14;
52bf26d3 »
2012-07-20 first commit
153 } else {
4950e52c »
2012-07-29 Added changing the background colour and positional markers
154 while(root_name[text_l] != '\0' && text_l < 256) {
155 right[text_l] = root_name[text_l];
13972428 »
2012-08-21 fine tuning and makefile options
156 ++text_l;
52bf26d3 »
2012-07-20 first commit
157 }
4950e52c »
2012-07-29 Added changing the background colour and positional markers
158 right[text_l] = '\0';
52bf26d3 »
2012-07-20 first commit
159 }
160 XFree(root_name);
161
4950e52c »
2012-07-29 Added changing the background colour and positional markers
162 XFillRectangle(dis, winbar, theme[0].gc, old_length, 0, width-old_length, height);
05947105 »
2012-07-29 Fix for left alignment for piped text and add a space after the piped…
163 text_start = old_length+font.width;
52bf26d3 »
2012-07-20 first commit
164 // i=pos on screen q=pos in text
13972428 »
2012-08-21 fine tuning and makefile options
165 for(counted=0;counted<=text_l;++counted) {
4950e52c »
2012-07-29 Added changing the background colour and positional markers
166 if(right[counted] == '&' && (right[counted+1] == 'C' || right[counted+1] == 'R')) {
167 counted += 2; q=0; m=0;
13972428 »
2012-08-21 fine tuning and makefile options
168 for(n=counted;n<text_l;++n) {
4950e52c »
2012-07-29 Added changing the background colour and positional markers
169 while(right[n] == '&') {
170 if(right[n+1]-'0' < 10 && right[n+1]-'0' >= 0) {
171 n += 2;
172 } else if(right[n+1] == 'B' && right[n+2]-'0' < 10 && right[n+2]-'0' >= 0) {
173 n += 3;
174 } else if(right[n+1] == 'R') {
175 break;
176 } else break;
177 }
13972428 »
2012-08-21 fine tuning and makefile options
178 bstring[q] = right[n]; ++q;
4950e52c »
2012-07-29 Added changing the background colour and positional markers
179 }
180 bstring[q] = '\0';
181 p_length = wc_size(bstring, q);
182 if(right[counted-1] == 'C')
183 text_start = (width/2)-(p_length/2)+old_length;
184 if(right[counted-1] == 'R')
185 text_start = width-p_length;
186 }
52bf26d3 »
2012-07-20 first commit
187 print_right_text();
188 }
189
4950e52c »
2012-07-29 Added changing the background colour and positional markers
190 XCopyArea(dis, winbar, barwin, theme[1].gc, old_length, 0, width-old_length, height, old_length, 0);
13972428 »
2012-08-21 fine tuning and makefile options
191 memset(right, '\0', 256);
52bf26d3 »
2012-07-20 first commit
192 XSync(dis, False);
193 return;
194 }
195
196 void print_right_text() {
197 char bstring[256];
4950e52c »
2012-07-29 Added changing the background colour and positional markers
198 unsigned int wsize, n=0;
52bf26d3 »
2012-07-20 first commit
199
200 while(right[counted] == '&') {
4950e52c »
2012-07-29 Added changing the background colour and positional markers
201 if(right[counted+1] == 'C' || right[counted+1] == 'R') {
202 counted--;
203 break;
204 }
205 if(right[counted+1]-'0' < 10 && right[counted+1]-'0' >= 0) {
52bf26d3 »
2012-07-20 first commit
206 j = right[counted+1]-'0';
207 counted += 2;
4950e52c »
2012-07-29 Added changing the background colour and positional markers
208 } else if(right[counted+1] == 'B' && right[counted+2]-'0' < 10 && right[counted+2]-'0' >= 0) {
209 bg = right[counted+2]-'0';
210 counted += 3;
211 } else break;
52bf26d3 »
2012-07-20 first commit
212 }
213 if(right[counted] == '&') {
214 bstring[n] = right[counted];
13972428 »
2012-08-21 fine tuning and makefile options
215 ++n;++counted;
52bf26d3 »
2012-07-20 first commit
216 }
217 while(right[counted] != '&' && right[counted] != '\0' && right[counted] != '\n' && right[counted] != '\r') {
218 bstring[n] = right[counted];
13972428 »
2012-08-21 fine tuning and makefile options
219 ++n;++counted;
52bf26d3 »
2012-07-20 first commit
220 }
221 if(n < 1) return;
222 bstring[n] = '\0';
223 wsize = wc_size(bstring, n);
4950e52c »
2012-07-29 Added changing the background colour and positional markers
224 XFillRectangle(dis, winbar, theme[bg].gc, text_start+m, 0, wsize, height);
52bf26d3 »
2012-07-20 first commit
225 if(font.fontset)
4950e52c »
2012-07-29 Added changing the background colour and positional markers
226 XmbDrawString(dis, winbar, font.fontset, theme[j].gc, text_start+m, font.fh, bstring, n);
52bf26d3 »
2012-07-20 first commit
227 else
4950e52c »
2012-07-29 Added changing the background colour and positional markers
228 XDrawString(dis, winbar, theme[j].gc, text_start+m, font.fh, bstring, n);
52bf26d3 »
2012-07-20 first commit
229 m += wsize;
13972428 »
2012-08-21 fine tuning and makefile options
230 memset(bstring, '\0', n);
52bf26d3 »
2012-07-20 first commit
231 counted--;
232 }
233
234 void print_text() {
235 char astring[256];
4950e52c »
2012-07-29 Added changing the background colour and positional markers
236 unsigned int wsize, n=0;
52bf26d3 »
2012-07-20 first commit
237
238 while(output[count] == '&') {
4950e52c »
2012-07-29 Added changing the background colour and positional markers
239 if(output[count+1]-'0' < 10 && output[count+1]-'0' >= 0) {
52bf26d3 »
2012-07-20 first commit
240 j = output[count+1]-'0';
241 count += 2;
4950e52c »
2012-07-29 Added changing the background colour and positional markers
242 } else if(output[count+1] == 'B' && output[count+2]-'0' < 10 && output[count+2]-'0' >= 0) {
243 bg = output[count+2]-'0';
244 count += 3;
245 } else break;
52bf26d3 »
2012-07-20 first commit
246 }
247 if(output[count] == '&') {
248 astring[n] = output[count];
13972428 »
2012-08-21 fine tuning and makefile options
249 ++n;++count;
52bf26d3 »
2012-07-20 first commit
250 }
251 while(output[count] != '&' && output[count] != '\0' && output[count] != '\n' && output[count] != '\r') {
252 astring[n] = output[count];
13972428 »
2012-08-21 fine tuning and makefile options
253 ++n;++count;
52bf26d3 »
2012-07-20 first commit
254 }
255 if(n < 1) return;
256 astring[n] = '\0';
257 wsize = wc_size(astring, n);
4950e52c »
2012-07-29 Added changing the background colour and positional markers
258 XFillRectangle(dis, winbar, theme[bg].gc, k, 0, wsize, height);
52bf26d3 »
2012-07-20 first commit
259 if(font.fontset)
4950e52c »
2012-07-29 Added changing the background colour and positional markers
260 XmbDrawString(dis, winbar, font.fontset, theme[j].gc, k, font.fh, astring, n);
52bf26d3 »
2012-07-20 first commit
261 else
4950e52c »
2012-07-29 Added changing the background colour and positional markers
262 XDrawString(dis, winbar, theme[j].gc, k, font.fh, astring, n);
52bf26d3 »
2012-07-20 first commit
263 k += wsize;
13972428 »
2012-08-21 fine tuning and makefile options
264 memset(astring, '\0', n);
52bf26d3 »
2012-07-20 first commit
265 count--;
266 }
267
13972428 »
2012-08-21 fine tuning and makefile options
268 int wc_size(char *string, unsigned int num) {
52bf26d3 »
2012-07-20 first commit
269 XRectangle rect;
270
271 if(font.fontset) {
272 XmbTextExtents(font.fontset, string, num, NULL, &rect);
273 return rect.width;
274 } else {
275 return XTextWidth(font.font, string, num);
276 }
277 }
278
279 unsigned long getcolor(const char* color) {
280 XColor c;
281 Colormap map = DefaultColormap(dis,screen);
282
283 if(!XAllocNamedColor(dis,map,color,&c,&c)) {
5927a38c »
2012-08-03 Name Change
284 fprintf(stderr, "\033[0;31mbipolarbar :: Error parsing color!");
52bf26d3 »
2012-07-20 first commit
285 return 1;
286 }
287 return c.pixel;
288 }
289
290 int main(int argc, char ** argv){
291 unsigned int i, y = 0;
292 XEvent ev;
293 XSetWindowAttributes attr;
294 char *loc;
295 fd_set readfds;
296 struct timeval tv;
297
298 dis = XOpenDisplay(NULL);
5927a38c »
2012-08-03 Name Change
299 if (!dis) {fprintf(stderr, "bipolarbar :: unable to connect to display");return 7;}
52bf26d3 »
2012-07-20 first commit
300
301 root = DefaultRootWindow(dis);
302 screen = DefaultScreen(dis);
303 sw = XDisplayWidth(dis,screen);
304 sh = XDisplayHeight(dis,screen);
305 loc = setlocale(LC_ALL, "");
306 if (!loc || !strcmp(loc, "C") || !strcmp(loc, "POSIX") || !XSupportsLocale())
5927a38c »
2012-08-03 Name Change
307 fprintf(stderr, "bipolarbar :: LOCALE FAILED\n");
52bf26d3 »
2012-07-20 first commit
308 get_font();
309 height = (BAR_HEIGHT > font.height) ? BAR_HEIGHT : font.height+2;
310 font.fh = ((height - font.height)/2) + font.ascent;
bec149bd »
2012-07-30 Fix for colour9 not working and minor tweaks
311 width = (BAR_WIDTH == 0) ? sw : BAR_WIDTH;
312 if (TOP_BAR != 0) y = sh - height;
52bf26d3 »
2012-07-20 first commit
313
13972428 »
2012-08-21 fine tuning and makefile options
314 for(i=0;i<10;++i)
52bf26d3 »
2012-07-20 first commit
315 theme[i].color = getcolor(defaultcolor[i]);
316 XGCValues values;
317
13972428 »
2012-08-21 fine tuning and makefile options
318 for(i=0;i<10;++i) {
0715579c »
2012-08-08 Fix for end of piped text not clearing properly sometimes with text l…
319 values.background = theme[0].color;
52bf26d3 »
2012-07-20 first commit
320 values.foreground = theme[i].color;
321 values.line_width = 2;
322 values.line_style = LineSolid;
323 if(font.fontset) {
0715579c »
2012-08-08 Fix for end of piped text not clearing properly sometimes with text l…
324 theme[i].gc = XCreateGC(dis, root, GCBackground|GCForeground|GCLineWidth|GCLineStyle,&values);
52bf26d3 »
2012-07-20 first commit
325 } else {
326 values.font = font.font->fid;
0715579c »
2012-08-08 Fix for end of piped text not clearing properly sometimes with text l…
327 theme[i].gc = XCreateGC(dis, root, GCBackground|GCForeground|GCLineWidth|GCLineStyle|GCFont,&values);
52bf26d3 »
2012-07-20 first commit
328 }
329 }
4950e52c »
2012-07-29 Added changing the background colour and positional markers
330 old_length = 0;
52bf26d3 »
2012-07-20 first commit
331
332 winbar = XCreatePixmap(dis, root, width, height, DefaultDepth(dis, screen));
333 XFillRectangle(dis, winbar, theme[0].gc, 0, 0, width, height);
69e9035e »
2012-07-30 Set borderwidth zero
334 barwin = XCreateSimpleWindow(dis, root, 0, y, width, height, 0, theme[0].color,theme[0].color);
52bf26d3 »
2012-07-20 first commit
335 attr.override_redirect = True;
336 XChangeWindowAttributes(dis, barwin, CWOverrideRedirect, &attr);
337 XSelectInput(dis,barwin,ExposureMask);
338 XSelectInput(dis,root,PropertyChangeMask);
339 XMapWindow(dis, barwin);
340 int x11_fd = ConnectionNumber(dis);
341 while(1){
342 FD_ZERO(&readfds);
343 FD_SET(x11_fd, &readfds);
344 FD_SET(STDIN_FILENO, &readfds);
345 tv.tv_sec = 0;
346 tv.tv_usec = 200000;
347 select(x11_fd+1, &readfds, NULL, NULL, &tv);
348
349 if (FD_ISSET(STDIN_FILENO, &readfds))
4950e52c »
2012-07-29 Added changing the background colour and positional markers
350 update_output();
52bf26d3 »
2012-07-20 first commit
351 while(XPending(dis) != 0) {
352 XNextEvent(dis, &ev);
353 switch(ev.type){
354 case Expose:
4950e52c »
2012-07-29 Added changing the background colour and positional markers
355 XCopyArea(dis, winbar, barwin, theme[1].gc, 0, 0, width, height, 0, 0);
52bf26d3 »
2012-07-20 first commit
356 XSync(dis, False);
357 break;
358 case PropertyNotify:
359 if(ev.xproperty.window == root && ev.xproperty.atom == XA_WM_NAME)
360 update_right();
361 break;
362 }
363 }
364 }
365
366 return (0);
367 }
Something went wrong with that request. Please try again.