/* AntiRight (c) 2003-2005 Jeffrey Bedard jefbed@e-list.net This file is part of AntiRight. AntiRight is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. AntiRight is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with AntiRight; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include #include #include #include #include #include #include /* This is a debug marking function. */ #define ARBUG \ { \ fprintf(stderr, "%s: %d\n", __FILE__, __LINE__); \ fflush(stdout); \ } Display *display; Window window; GC gc; char *Geometry=""; XSizeHints mysizehints; XWMHints mywmhints; Window iconwin; char *swap; char *users; char commands[17][16]; char* antiright_pipe_read(char *command_string) { char *text_string; char buffer[BUFSIZ]; FILE *pipe_fp=popen(command_string, "r"); fgets(buffer, BUFSIZ, pipe_fp); asprintf(&text_string, "%s", buffer); while(fgets(buffer, BUFSIZ, pipe_fp)) asprintf(&text_string, "%s%s", text_string, buffer); pclose(pipe_fp); return(text_string); } void arshell_set_font(char* fontname) { XFontStruct *font=XLoadQueryFont(display, fontname); if(font==NULL) { font=XLoadQueryFont(display, "fixed"); } XSetFont(display, gc, font->fid); XFreeFont(display, font); } unsigned long arshell_get_pixel(int red, int green, int blue) { XColor color; Colormap colormap=DefaultColormap(display, DefaultScreen(display)); color.flags=DoRed|DoGreen|DoBlue; color.red=red*256; color.green=green*256; color.blue=blue*256; XAllocColor(display, colormap, &color); return(color.pixel); } void arshell_set_background(int red, int green, int blue) { XSetWindowBackground(display, iconwin, arshell_get_pixel(red, green, blue)); } void arshell_set_foreground(int red, int green, int blue) { XSetForeground(display, gc, arshell_get_pixel(red, green, blue)); } void expose() { char *string; double load[3]; char *hostname=getenv("HOSTNAME"); arshell_set_foreground(220, 218, 213); XFillRectangle(display, iconwin, gc, 0, 0, 64, 64); getloadavg(load, 3); arshell_set_foreground(0, 0, 0); XDrawRectangle(display, iconwin, gc, 0, 0, 64, 64); XFillRectangle(display, iconwin, gc, 4, 4, 56, 56); arshell_set_font("5x7"); arshell_set_foreground(200, 250, 200); XDrawString(display, iconwin, gc, 6, 16, hostname, strlen(hostname)); asprintf(&string, "%sM fr/sw", swap); XDrawString(display, iconwin, gc, 6, 24, string, strlen(string)); free(string); asprintf(&string, "%s usrs", users); XDrawString(display, iconwin, gc, 6, 32, string, strlen(string)); free(string); asprintf(&string, "%d%% 1m", (int)(load[0]*100)); XDrawString(display, iconwin, gc, 6, 40, string, strlen(string)); free(string); asprintf(&string, "%d%% 5m", (int)(load[1]*100)); XDrawString(display, iconwin, gc, 6, 48, string, strlen(string)); free(string); asprintf(&string, "%d%% 15m", (int)(load[2]*100)); XDrawString(display, iconwin, gc, 6, 56, string, strlen(string)); free(string); XFlush(display); } int main(int argc, char **argv) { int dummy=0; char *wname=argv[0]; XClassHint classHint; /*XTextProperty name;*/ XGCValues gcv; unsigned long gcm; int done=0; /*XEvent event;*/ display=XOpenDisplay((char*)getenv("DISPLAY")); if(display == NULL) { puts("Error Connecting to X server"); exit(1); } mysizehints.flags=USSize | USPosition; mysizehints.x = 0; mysizehints.y = 0; XWMGeometry(display, DefaultScreen(display), Geometry, NULL, 0, &mysizehints, &mysizehints.x, &mysizehints.y, &mysizehints.width, &mysizehints.height, &dummy); mysizehints.width=64; mysizehints.height=64; window=XCreateSimpleWindow(display, DefaultRootWindow(display), mysizehints.x, mysizehints.y, mysizehints.width, mysizehints.height, 0, BlackPixel(display, DefaultScreen(display)), WhitePixel(display, DefaultScreen(display))); iconwin=XCreateSimpleWindow(display, window, mysizehints.x, mysizehints.y, mysizehints.width, mysizehints.height, 0, BlackPixel(display, DefaultScreen(display)), WhitePixel(display, DefaultScreen(display))); XSetWMNormalHints(display, window, &mysizehints); classHint.res_name=wname; classHint.res_class=wname; XSetClassHint(display, window, &classHint); XStoreName(display, window, "wmar"); XSelectInput(display, window, ButtonPressMask | ExposureMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask | EnterWindowMask | LeaveWindowMask | KeyPressMask | KeyReleaseMask); XSelectInput(display, iconwin, ButtonPressMask | ExposureMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask | EnterWindowMask | LeaveWindowMask | KeyPressMask | KeyReleaseMask); /*XSetWMName(display, window, &name);*/ gcm = GCForeground | GCBackground | GCGraphicsExposures; gcv.foreground = BlackPixel(display, DefaultScreen(display)); gcv.background = WhitePixel(display, DefaultScreen(display)); gcv.graphics_exposures = 0; gc = XCreateGC(display, DefaultRootWindow(display), gcm, &gcv); mywmhints.initial_state = WithdrawnState; mywmhints.icon_window = iconwin; mywmhints.icon_x = mysizehints.x; mywmhints.icon_y = mysizehints.y; mywmhints.window_group = window; mywmhints.flags = StateHint | IconWindowHint | IconPositionHint | WindowGroupHint; XSetWMHints(display, window, &mywmhints); XSetCommand(display, window, argv, argc); XMapWindow(display, window); expose(); while(!done) { if(swap != NULL) { free(swap); } swap= antiright_pipe_read("echo `free | grep Swap" " | awk '{print $4}'`/1024 | bc"); swap[strlen(swap)-1]='\0'; if(users != NULL) { free(users); } users=antiright_pipe_read("users | wc | awk '{print $2}'"); users[strlen(users)-1]='\0'; expose(); sleep(1); } XCloseDisplay(display); }