wmmemload: Consistent formatting.

Format the source so as to minimize warnings from checkpatch.pl in the
Window Maker source tree.
This commit is contained in:
Doug Torrance 2014-10-09 23:50:18 -05:00 committed by Carlos R. Mafra
parent 917d454f0d
commit ea2cf286ef
8 changed files with 722 additions and 741 deletions

View file

@ -36,8 +36,8 @@ Bool dockapp_isbrokenwm = False;
/* private */ /* private */
static Window window = None; static Window window = None;
static Window icon_window = None; static Window icon_window = None;
static GC gc = NULL; static GC gc;
static int depth = 0; static int depth;
static Atom delete_win; static Atom delete_win;
static int width, height; static int width, height;
static int offset_w, offset_h; static int offset_w, offset_h;
@ -46,204 +46,203 @@ void
dockapp_open_window(char *display_specified, char *appname, dockapp_open_window(char *display_specified, char *appname,
unsigned w, unsigned h, int argc, char **argv) unsigned w, unsigned h, int argc, char **argv)
{ {
XClassHint *classhint; XClassHint *classhint;
XWMHints *wmhints; XWMHints *wmhints;
XTextProperty title; XTextProperty title;
XSizeHints sizehints; XSizeHints sizehints;
Window root; Window root;
int ww, wh; int ww, wh;
/* Open Connection to X Server */ /* Open Connection to X Server */
display = XOpenDisplay(display_specified); display = XOpenDisplay(display_specified);
if (!display) { if (!display) {
fprintf(stderr, "%s: could not open display %s!\n", argv[0], fprintf(stderr, "%s: could not open display %s!\n", argv[0],
XDisplayName(display_specified)); XDisplayName(display_specified));
exit(1); exit(1);
} }
root = DefaultRootWindow(display); root = DefaultRootWindow(display);
width = w; width = w;
height = h; height = h;
if (dockapp_iswindowed) { if (dockapp_iswindowed) {
offset_w = (WINDOWED_SIZE_W - w) / 2; offset_w = (WINDOWED_SIZE_W - w) / 2;
offset_h = (WINDOWED_SIZE_H - h) / 2; offset_h = (WINDOWED_SIZE_H - h) / 2;
ww = WINDOWED_SIZE_W; ww = WINDOWED_SIZE_W;
wh = WINDOWED_SIZE_H; wh = WINDOWED_SIZE_H;
} else { } else {
offset_w = offset_h = 0; offset_w = offset_h = 0;
ww = w; ww = w;
wh = h; wh = h;
} }
/* Create Windows */ /* Create Windows */
icon_window = XCreateSimpleWindow(display, root, 0, 0, ww, wh, 0, 0, 0); icon_window = XCreateSimpleWindow(display, root, 0, 0, ww, wh, 0, 0, 0);
if (dockapp_isbrokenwm) { if (dockapp_isbrokenwm)
window = XCreateSimpleWindow(display, root, 0, 0, ww, wh, 0, 0, 0); window = XCreateSimpleWindow(display, root, 0, 0, ww, wh, 0, 0, 0);
} else { else
window = XCreateSimpleWindow(display, root, 0, 0, 1, 1, 0, 0, 0); window = XCreateSimpleWindow(display, root, 0, 0, 1, 1, 0, 0, 0);
}
/* Set ClassHint */ /* Set ClassHint */
classhint = XAllocClassHint(); classhint = XAllocClassHint();
if (classhint == NULL) { if (classhint == NULL) {
fprintf(stderr, "%s: can't allocate memory for wm hints!\n", argv[0]); fprintf(stderr, "%s: can't allocate memory for wm hints!\n", argv[0]);
exit(1); exit(1);
} }
classhint->res_class = "DockApp"; classhint->res_class = "DockApp";
classhint->res_name = appname; classhint->res_name = appname;
XSetClassHint(display, window, classhint); XSetClassHint(display, window, classhint);
XFree(classhint); XFree(classhint);
/* Set WMHints */ /* Set WMHints */
wmhints = XAllocWMHints(); wmhints = XAllocWMHints();
if (wmhints == NULL) { if (wmhints == NULL) {
fprintf(stderr, "%s: can't allocate memory for wm hints!\n", argv[0]); fprintf(stderr, "%s: can't allocate memory for wm hints!\n", argv[0]);
exit(1); exit(1);
} }
wmhints->flags = IconWindowHint | WindowGroupHint; wmhints->flags = IconWindowHint | WindowGroupHint;
if (!dockapp_iswindowed) { if (!dockapp_iswindowed) {
wmhints->flags |= StateHint; wmhints->flags |= StateHint;
wmhints->initial_state = WithdrawnState; wmhints->initial_state = WithdrawnState;
} }
wmhints->window_group = window; wmhints->window_group = window;
wmhints->icon_window = icon_window; wmhints->icon_window = icon_window;
XSetWMHints(display, window, wmhints); XSetWMHints(display, window, wmhints);
XFree(wmhints); XFree(wmhints);
/* Set WM Protocols */ /* Set WM Protocols */
delete_win = XInternAtom(display, "WM_DELETE_WINDOW", False); delete_win = XInternAtom(display, "WM_DELETE_WINDOW", False);
XSetWMProtocols (display, icon_window, &delete_win, 1); XSetWMProtocols(display, icon_window, &delete_win, 1);
/* Set Size Hints */ /* Set Size Hints */
sizehints.flags = USSize; sizehints.flags = USSize;
if (!dockapp_iswindowed) { if (!dockapp_iswindowed) {
sizehints.flags |= USPosition; sizehints.flags |= USPosition;
sizehints.x = sizehints.y = 0; sizehints.x = sizehints.y = 0;
} else { } else {
sizehints.flags |= PMinSize | PMaxSize; sizehints.flags |= PMinSize | PMaxSize;
sizehints.min_width = sizehints.max_width = WINDOWED_SIZE_W; sizehints.min_width = sizehints.max_width = WINDOWED_SIZE_W;
sizehints.min_height = sizehints.max_height = WINDOWED_SIZE_H; sizehints.min_height = sizehints.max_height = WINDOWED_SIZE_H;
} }
sizehints.width = ww; sizehints.width = ww;
sizehints.height = wh; sizehints.height = wh;
XSetWMNormalHints(display, icon_window, &sizehints); XSetWMNormalHints(display, icon_window, &sizehints);
/* Set WindowTitle for AfterStep Wharf */ /* Set WindowTitle for AfterStep Wharf */
XStringListToTextProperty(&appname, 1, &title); XStringListToTextProperty(&appname, 1, &title);
XSetWMName(display, window, &title); XSetWMName(display, window, &title);
XSetWMName(display, icon_window, &title); XSetWMName(display, icon_window, &title);
/* Set Command to start the app so it can be docked properly */ /* Set Command to start the app so it can be docked properly */
XSetCommand(display, window, argv, argc); XSetCommand(display, window, argv, argc);
depth = DefaultDepth(display, DefaultScreen(display)); depth = DefaultDepth(display, DefaultScreen(display));
gc = DefaultGC(display, DefaultScreen(display)); gc = DefaultGC(display, DefaultScreen(display));
XFlush(display); XFlush(display);
} }
void void
dockapp_set_eventmask(long mask) dockapp_set_eventmask(long mask)
{ {
XSelectInput(display, icon_window, mask); XSelectInput(display, icon_window, mask);
XSelectInput(display, window, mask); XSelectInput(display, window, mask);
} }
static Pixmap static Pixmap
create_bg_pixmap(void) create_bg_pixmap(void)
{ {
Pixmap bg; Pixmap bg;
bg = XCreatePixmap(display, icon_window, WINDOWED_SIZE_W, WINDOWED_SIZE_H, bg = XCreatePixmap(display, icon_window, WINDOWED_SIZE_W, WINDOWED_SIZE_H,
depth); depth);
XSetForeground(display, gc, dockapp_getcolor("rgb:ae/aa/ae")); XSetForeground(display, gc, dockapp_getcolor("rgb:ae/aa/ae"));
XFillRectangle(display, bg, gc, 0, 0, WINDOWED_SIZE_W, WINDOWED_SIZE_H); XFillRectangle(display, bg, gc, 0, 0, WINDOWED_SIZE_W, WINDOWED_SIZE_H);
XSetForeground(display, gc, dockapp_getcolor("rgb:ff/ff/ff")); XSetForeground(display, gc, dockapp_getcolor("rgb:ff/ff/ff"));
XDrawLine(display, bg, gc, 0, 0, 0, 63); XDrawLine(display, bg, gc, 0, 0, 0, 63);
XDrawLine(display, bg, gc, 1, 0, 1, 62); XDrawLine(display, bg, gc, 1, 0, 1, 62);
XDrawLine(display, bg, gc, 2, 0, 63, 0); XDrawLine(display, bg, gc, 2, 0, 63, 0);
XDrawLine(display, bg, gc, 2, 1, 62, 1); XDrawLine(display, bg, gc, 2, 1, 62, 1);
XSetForeground(display, gc, dockapp_getcolor("rgb:52/55/52")); XSetForeground(display, gc, dockapp_getcolor("rgb:52/55/52"));
XDrawLine(display, bg, gc, 1, 63, 63, 63); XDrawLine(display, bg, gc, 1, 63, 63, 63);
XDrawLine(display, bg, gc, 2, 62, 63, 62); XDrawLine(display, bg, gc, 2, 62, 63, 62);
XDrawLine(display, bg, gc, 63, 1, 63, 61); XDrawLine(display, bg, gc, 63, 1, 63, 61);
XDrawLine(display, bg, gc, 62, 2, 62, 61); XDrawLine(display, bg, gc, 62, 2, 62, 61);
return bg; return bg;
} }
void void
dockapp_set_background(Pixmap pixmap) dockapp_set_background(Pixmap pixmap)
{ {
if (dockapp_iswindowed) { if (dockapp_iswindowed) {
Pixmap bg; Pixmap bg;
bg = create_bg_pixmap(); bg = create_bg_pixmap();
XCopyArea(display, pixmap, bg, gc, 0, 0, width, height, XCopyArea(display, pixmap, bg, gc, 0, 0, width, height,
offset_w, offset_w); offset_w, offset_w);
XSetWindowBackgroundPixmap(display, icon_window, bg); XSetWindowBackgroundPixmap(display, icon_window, bg);
XSetWindowBackgroundPixmap(display, window, bg); XSetWindowBackgroundPixmap(display, window, bg);
XFreePixmap(display, bg); XFreePixmap(display, bg);
} else { } else {
XSetWindowBackgroundPixmap(display, icon_window, pixmap); XSetWindowBackgroundPixmap(display, icon_window, pixmap);
XSetWindowBackgroundPixmap(display, window, pixmap); XSetWindowBackgroundPixmap(display, window, pixmap);
} }
XClearWindow(display, icon_window); XClearWindow(display, icon_window);
XFlush(display); XFlush(display);
} }
void void
dockapp_show(void) dockapp_show(void)
{ {
if (!dockapp_iswindowed) if (!dockapp_iswindowed)
XMapRaised(display, window); XMapRaised(display, window);
else else
XMapRaised(display, icon_window); XMapRaised(display, icon_window);
XFlush(display); XFlush(display);
} }
Bool Bool
dockapp_xpm2pixmap(char **data, Pixmap *pixmap, Pixmap *mask, dockapp_xpm2pixmap(char **data, Pixmap *pixmap, Pixmap *mask,
XpmColorSymbol * colorSymbol, unsigned int nsymbols) XpmColorSymbol *colorSymbol, unsigned int nsymbols)
{ {
XpmAttributes xpmAttr; XpmAttributes xpmAttr;
xpmAttr.valuemask = XpmCloseness; xpmAttr.valuemask = XpmCloseness;
xpmAttr.closeness = 40000; xpmAttr.closeness = 40000;
if (nsymbols) { if (nsymbols) {
xpmAttr.colorsymbols = colorSymbol; xpmAttr.colorsymbols = colorSymbol;
xpmAttr.numsymbols = nsymbols; xpmAttr.numsymbols = nsymbols;
xpmAttr.valuemask |= XpmColorSymbols; xpmAttr.valuemask |= XpmColorSymbols;
} }
if (XpmCreatePixmapFromData(display, icon_window, data, pixmap, mask, &xpmAttr) != 0) if (XpmCreatePixmapFromData(display, icon_window, data, pixmap, mask, &xpmAttr) != 0)
return False; return False;
return True; return True;
} }
Pixmap Pixmap
dockapp_XCreatePixmap(int w, int h) dockapp_XCreatePixmap(int w, int h)
{ {
return (XCreatePixmap(display, icon_window, w, h, depth)); return XCreatePixmap(display, icon_window, w, h, depth);
} }
void void
dockapp_setshape(Pixmap mask, int x_ofs, int y_ofs) dockapp_setshape(Pixmap mask, int x_ofs, int y_ofs)
{ {
XShapeCombineMask(display, icon_window, ShapeBounding, -x_ofs, -y_ofs, XShapeCombineMask(display, icon_window, ShapeBounding, -x_ofs, -y_ofs,
mask, ShapeSet); mask, ShapeSet);
XShapeCombineMask(display, window, ShapeBounding, -x_ofs, -y_ofs, XShapeCombineMask(display, window, ShapeBounding, -x_ofs, -y_ofs,
mask, ShapeSet); mask, ShapeSet);
XFlush(display); XFlush(display);
} }
@ -251,140 +250,137 @@ void
dockapp_copyarea(Pixmap src, Pixmap dist, int x_src, int y_src, int w, int h, dockapp_copyarea(Pixmap src, Pixmap dist, int x_src, int y_src, int w, int h,
int x_dist, int y_dist) int x_dist, int y_dist)
{ {
XCopyArea(display, src, dist, gc, x_src, y_src, w, h, x_dist, y_dist); XCopyArea(display, src, dist, gc, x_src, y_src, w, h, x_dist, y_dist);
} }
void void
dockapp_copy2window (Pixmap src) dockapp_copy2window(Pixmap src)
{ {
if (dockapp_isbrokenwm) { if (dockapp_isbrokenwm) {
XCopyArea(display, src, window, gc, 0, 0, width, height, offset_w, XCopyArea(display, src, window, gc, 0, 0, width, height, offset_w,
offset_h); offset_h);
} else { } else {
XCopyArea(display, src, icon_window, gc, 0, 0, width, height, offset_w, XCopyArea(display, src, icon_window, gc, 0, 0, width, height, offset_w,
offset_h); offset_h);
} }
} }
Bool Bool
dockapp_nextevent_or_timeout(XEvent *event, unsigned long miliseconds) dockapp_nextevent_or_timeout(XEvent *event, unsigned long miliseconds)
{ {
struct timeval timeout; struct timeval timeout;
fd_set rset; fd_set rset;
XSync(display, False); XSync(display, False);
if (XPending(display)) { if (XPending(display)) {
XNextEvent(display, event); XNextEvent(display, event);
return True; return True;
}
timeout.tv_sec = miliseconds / 1000;
timeout.tv_usec = (miliseconds % 1000) * 1000;
FD_ZERO(&rset);
FD_SET(ConnectionNumber(display), &rset);
if (select(ConnectionNumber(display)+1, &rset, NULL, NULL, &timeout) > 0) {
XNextEvent(display, event);
if (event->type == ClientMessage) {
if (event->xclient.data.l[0] == delete_win) {
XDestroyWindow(display,event->xclient.window);
XCloseDisplay(display);
exit(0);
}
} }
if (dockapp_iswindowed) {
event->xbutton.x -= offset_w;
event->xbutton.y -= offset_h;
}
return True;
}
return False; timeout.tv_sec = miliseconds / 1000;
timeout.tv_usec = (miliseconds % 1000) * 1000;
FD_ZERO(&rset);
FD_SET(ConnectionNumber(display), &rset);
if (select(ConnectionNumber(display)+1, &rset, NULL, NULL, &timeout) > 0) {
XNextEvent(display, event);
if (event->type == ClientMessage) {
if (event->xclient.data.l[0] == delete_win) {
XDestroyWindow(display, event->xclient.window);
XCloseDisplay(display);
exit(0);
}
}
if (dockapp_iswindowed) {
event->xbutton.x -= offset_w;
event->xbutton.y -= offset_h;
}
return True;
}
return False;
} }
unsigned long unsigned long
dockapp_getcolor(char *color_name) dockapp_getcolor(char *color_name)
{ {
XColor color; XColor color;
if (!XParseColor(display, DefaultColormap(display, DefaultScreen(display)), if (!XParseColor(display, DefaultColormap(display, DefaultScreen(display)),
color_name, &color)) color_name, &color))
fprintf(stderr, "can't parse color %s\n", color_name), exit(1); fprintf(stderr, "can't parse color %s\n", color_name), exit(1);
if (!XAllocColor(display, DefaultColormap(display, DefaultScreen(display)), if (!XAllocColor(display, DefaultColormap(display, DefaultScreen(display)),
&color)) { &color)) {
fprintf(stderr, "can't allocate color %s. Using black\n", color_name); fprintf(stderr, "can't allocate color %s. Using black\n", color_name);
return BlackPixel(display, DefaultScreen(display)); return BlackPixel(display, DefaultScreen(display));
} }
return color.pixel; return color.pixel;
} }
unsigned long unsigned long
dockapp_blendedcolor(char *color_name, int r, int g, int b, float fac) dockapp_blendedcolor(char *color_name, int r, int g, int b, float fac)
{ {
XColor color; XColor color;
if ((r < -255 || r > 255)||(g < -255 || g > 255)||(b < -255 || b > 255)){ if ((r < -255 || r > 255) || (g < -255 || g > 255) || (b < -255 || b > 255)) {
fprintf(stderr, "r:%d,g:%d,b:%d (r,g,b must be 0 to 255)", r, g, b); fprintf(stderr, "r:%d,g:%d,b:%d (r,g,b must be 0 to 255)", r, g, b);
exit(1); exit(1);
} }
r *= 255; r *= 255;
g *= 255; g *= 255;
b *= 255; b *= 255;
if (!XParseColor(display, DefaultColormap(display, DefaultScreen(display)), if (!XParseColor(display, DefaultColormap(display, DefaultScreen(display)),
color_name, &color)) color_name, &color))
fprintf(stderr, "can't parse color %s\n", color_name), exit(1); fprintf(stderr, "can't parse color %s\n", color_name), exit(1);
if (!XAllocColor(display, DefaultColormap(display, DefaultScreen(display)), if (!XAllocColor(display, DefaultColormap(display, DefaultScreen(display)),
&color)) { &color)) {
fprintf(stderr, "can't allocate color %s. Using black\n", color_name); fprintf(stderr, "can't allocate color %s. Using black\n", color_name);
return BlackPixel(display, DefaultScreen(display)); return BlackPixel(display, DefaultScreen(display));
} }
if (DefaultDepth(display, DefaultScreen(display)) < 16)
return color.pixel;
/* red */
if (color.red + r > 0xffff)
color.red = 0xffff;
else if (color.red + r < 0)
color.red = 0;
else
color.red = (unsigned short)(fac * color.red + r);
/* green */
if (color.green + g > 0xffff)
color.green = 0xffff;
else if (color.green + g < 0)
color.green = 0;
else
color.green = (unsigned short)(fac * color.green + g);
/* blue */
if (color.blue + b > 0xffff)
color.blue = 0xffff;
else if (color.blue + b < 0)
color.blue = 0;
else
color.blue = (unsigned short)(fac * color.blue + b);
color.flags = DoRed | DoGreen | DoBlue;
if (!XAllocColor(display, DefaultColormap(display, DefaultScreen(display)),
&color)) {
fprintf(stderr, "can't allocate color %s. Using black\n", color_name);
return BlackPixel(display, DefaultScreen(display));
}
if (DefaultDepth(display, DefaultScreen(display)) < 16)
return color.pixel; return color.pixel;
/* red */
if (color.red + r > 0xffff) {
color.red = 0xffff;
} else if (color.red + r < 0) {
color.red = 0;
} else {
color.red = (unsigned short)(fac * color.red + r);
}
/* green */
if (color.green + g > 0xffff) {
color.green = 0xffff;
} else if (color.green + g < 0) {
color.green = 0;
} else {
color.green = (unsigned short)(fac * color.green + g);
}
/* blue */
if (color.blue + b > 0xffff) {
color.blue = 0xffff;
} else if (color.blue + b < 0) {
color.blue = 0;
} else {
color.blue = (unsigned short)(fac * color.blue + b);
}
color.flags = DoRed | DoGreen | DoBlue;
if (!XAllocColor(display, DefaultColormap(display, DefaultScreen(display)),
&color)) {
fprintf(stderr, "can't allocate color %s. Using black\n", color_name);
return BlackPixel(display, DefaultScreen(display));
}
return color.pixel;
} }

View file

@ -83,14 +83,14 @@ void dockapp_open_window(char *display_specified, char *appname,
void dockapp_set_eventmask(long mask); void dockapp_set_eventmask(long mask);
void dockapp_set_background(Pixmap pixmap); void dockapp_set_background(Pixmap pixmap);
void dockapp_show(void); void dockapp_show(void);
Bool dockapp_xpm2pixmap(char **data, Pixmap * pixmap, Pixmap * mask, Bool dockapp_xpm2pixmap(char **data, Pixmap *pixmap, Pixmap *mask,
XpmColorSymbol * colorSymbol, XpmColorSymbol *colorSymbol,
unsigned int nsymbols); unsigned int nsymbols);
Pixmap dockapp_XCreatePixmap(int w, int h); Pixmap dockapp_XCreatePixmap(int w, int h);
void dockapp_setshape(Pixmap mask, int x_ofs, int y_ofs); void dockapp_setshape(Pixmap mask, int x_ofs, int y_ofs);
void dockapp_copyarea(Pixmap src, Pixmap dist, int x_src, int y_src, void dockapp_copyarea(Pixmap src, Pixmap dist, int x_src, int y_src,
int w, int h, int x_dist, int y_dist); int w, int h, int x_dist, int y_dist);
void dockapp_copy2window(Pixmap src); void dockapp_copy2window(Pixmap src);
Bool dockapp_nextevent_or_timeout(XEvent * event, unsigned long miliseconds); Bool dockapp_nextevent_or_timeout(XEvent *event, unsigned long miliseconds);
unsigned long dockapp_getcolor(char *color); unsigned long dockapp_getcolor(char *color);
unsigned long dockapp_blendedcolor(char *color, int r, int g, int b, float fac); unsigned long dockapp_blendedcolor(char *color, int r, int g, int b, float fac);

View file

@ -44,13 +44,13 @@ Pixmap backdrop_off;
Pixmap parts; Pixmap parts;
Pixmap mask; Pixmap mask;
static char *display_name = ""; static char *display_name = "";
static char *light_color = NULL; /* back-light color */ static char *light_color; /* back-light color */
static unsigned update_interval = 1; static unsigned update_interval = 1;
static light backlight = LIGHTOFF; static light backlight = LIGHTOFF;
static struct mem_options mem_opts; static struct mem_options mem_opts;
static int mem_usage = 0; static int mem_usage;
static int swap_usage = 0; static int swap_usage;
static unsigned alarm_mem = 101; static unsigned alarm_mem = 101;
static unsigned alarm_swap = 101; static unsigned alarm_swap = 101;
@ -64,330 +64,305 @@ static void print_help(char *prog);
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
XEvent event; XEvent event;
XpmColorSymbol colors[2] = { {"Back0", NULL, 0}, {"Back1", NULL, 0} }; XpmColorSymbol colors[2] = { {"Back0", NULL, 0}, {"Back1", NULL, 0} };
int ncolor = 0; int ncolor = 0;
/* Parse CommandLine */ /* Parse CommandLine */
mem_opts.ignore_buffers = mem_opts.ignore_cached mem_opts.ignore_buffers = mem_opts.ignore_cached
= mem_opts.ignore_wired = False; = mem_opts.ignore_wired = False;
parse_arguments(argc, argv); parse_arguments(argc, argv);
/* Initialize Application */ /* Initialize Application */
mem_init(); mem_init();
dockapp_open_window(display_name, PACKAGE, SIZE, SIZE, argc, argv); dockapp_open_window(display_name, PACKAGE, SIZE, SIZE, argc, argv);
dockapp_set_eventmask(ButtonPressMask); dockapp_set_eventmask(ButtonPressMask);
if(light_color) if (light_color) {
{ colors[0].pixel = dockapp_getcolor(light_color);
colors[0].pixel = dockapp_getcolor(light_color); colors[1].pixel = dockapp_blendedcolor(light_color, -24, -24, -24, 1.0);
colors[1].pixel = dockapp_blendedcolor(light_color, -24, -24, -24, 1.0); ncolor = 2;
ncolor = 2; }
}
/* change raw xpm data to pixmap */ /* change raw xpm data to pixmap */
if(dockapp_iswindowed) if (dockapp_iswindowed)
backlight_on_xpm[1] = backlight_off_xpm[1] = WINDOWED_BG; backlight_on_xpm[1] = backlight_off_xpm[1] = WINDOWED_BG;
if(!dockapp_xpm2pixmap(backlight_on_xpm, &backdrop_on, &mask, colors, ncolor)) if (!dockapp_xpm2pixmap(backlight_on_xpm, &backdrop_on, &mask, colors, ncolor)) {
{ fprintf(stderr, "Error initializing backlit background image.\n");
fprintf(stderr, "Error initializing backlit background image.\n"); exit(1);
exit(1); }
} if (!dockapp_xpm2pixmap(backlight_off_xpm, &backdrop_off, NULL, NULL, 0)) {
if(!dockapp_xpm2pixmap(backlight_off_xpm, &backdrop_off, NULL, NULL, 0)) fprintf(stderr, "Error initializing background image.\n");
{ exit(1);
fprintf(stderr, "Error initializing background image.\n"); }
exit(1); if (!dockapp_xpm2pixmap(parts_xpm, &parts, NULL, colors, ncolor)) {
} fprintf(stderr, "Error initializing parts image.\n");
if(!dockapp_xpm2pixmap(parts_xpm, &parts, NULL, colors, ncolor)) exit(1);
{ }
fprintf(stderr, "Error initializing parts image.\n");
exit(1);
}
/* shape window */ /* shape window */
if(!dockapp_iswindowed) if (!dockapp_iswindowed)
dockapp_setshape(mask, 0, 0); dockapp_setshape(mask, 0, 0);
if(mask) XFreePixmap(display, mask); if (mask)
XFreePixmap(display, mask);
/* pixmap : draw area */ /* pixmap : draw area */
pixmap = dockapp_XCreatePixmap(SIZE, SIZE); pixmap = dockapp_XCreatePixmap(SIZE, SIZE);
/* Initialize pixmap */ /* Initialize pixmap */
if(backlight == LIGHTON) if (backlight == LIGHTON)
dockapp_copyarea(backdrop_on, pixmap, 0, 0, SIZE, SIZE, 0, 0); dockapp_copyarea(backdrop_on, pixmap, 0, 0, SIZE, SIZE, 0, 0);
else else
dockapp_copyarea(backdrop_off, pixmap, 0, 0, SIZE, SIZE, 0, 0); dockapp_copyarea(backdrop_off, pixmap, 0, 0, SIZE, SIZE, 0, 0);
dockapp_set_background(pixmap); dockapp_set_background(pixmap);
dockapp_show(); dockapp_show();
/* Main loop */ /* Main loop */
while(1) while (1) {
{ if (dockapp_nextevent_or_timeout(&event, update_interval * 1000)) {
if (dockapp_nextevent_or_timeout(&event, update_interval * 1000)) /* Next Event */
{ switch (event.type) {
/* Next Event */ case ButtonPress:
switch(event.type) switch_light();
{ break;
case ButtonPress: default: /* make gcc happy */
switch_light(); break;
break; }
default: /* make gcc happy */ } else {
break; /* Time Out */
} update();
} }
else }
{
/* Time Out */
update();
}
}
return 0; return 0;
} }
/* called by timer */ /* called by timer */
static void update(void) static void update(void)
{ {
static light pre_backlight; static light pre_backlight;
static Bool in_alarm_mode = False; static Bool in_alarm_mode = False;
/* get current cpu usage in percent */ /* get current cpu usage in percent */
mem_getusage(&mem_usage, &swap_usage, &mem_opts); mem_getusage(&mem_usage, &swap_usage, &mem_opts);
/* alarm mode */ /* alarm mode */
if(mem_usage >= alarm_mem || swap_usage >= alarm_swap) if (mem_usage >= alarm_mem || swap_usage >= alarm_swap) {
{ if (!in_alarm_mode) {
if(!in_alarm_mode) in_alarm_mode = True;
{ pre_backlight = backlight;
in_alarm_mode = True; }
pre_backlight = backlight; if (backlight == LIGHTOFF) {
} switch_light();
if(backlight == LIGHTOFF) return;
{ }
switch_light(); } else {
return; if (in_alarm_mode) {
} in_alarm_mode = False;
} if (backlight != pre_backlight) {
else switch_light();
{ return;
if(in_alarm_mode) }
{ }
in_alarm_mode = False; }
if (backlight != pre_backlight)
{
switch_light();
return;
}
}
}
/* all clear */ /* all clear */
if (backlight == LIGHTON) if (backlight == LIGHTON)
dockapp_copyarea(backdrop_on, pixmap, 0, 0, 58, 58, 0, 0); dockapp_copyarea(backdrop_on, pixmap, 0, 0, 58, 58, 0, 0);
else else
dockapp_copyarea(backdrop_off, pixmap, 0, 0, 58, 58, 0, 0); dockapp_copyarea(backdrop_off, pixmap, 0, 0, 58, 58, 0, 0);
/* draw digit */ /* draw digit */
draw_memdigit(mem_usage); draw_memdigit(mem_usage);
draw_swapdigit(swap_usage); draw_swapdigit(swap_usage);
/* show */ /* show */
dockapp_copy2window(pixmap); dockapp_copy2window(pixmap);
} }
/* called when mouse button pressed */ /* called when mouse button pressed */
static void switch_light(void) static void switch_light(void)
{ {
switch (backlight) switch (backlight) {
{ case LIGHTOFF:
case LIGHTOFF: backlight = LIGHTON;
backlight = LIGHTON; dockapp_copyarea(backdrop_on, pixmap, 0, 0, 58, 58, 0, 0);
dockapp_copyarea(backdrop_on, pixmap, 0, 0, 58, 58, 0, 0); break;
break; case LIGHTON:
case LIGHTON: backlight = LIGHTOFF;
backlight = LIGHTOFF; dockapp_copyarea(backdrop_off, pixmap, 0, 0, 58, 58, 0, 0);
dockapp_copyarea(backdrop_off, pixmap, 0, 0, 58, 58, 0, 0); break;
break; }
}
/* redraw digit */ /* redraw digit */
mem_getusage(&mem_usage, &swap_usage, &mem_opts); mem_getusage(&mem_usage, &swap_usage, &mem_opts);
draw_memdigit(mem_usage); draw_memdigit(mem_usage);
draw_swapdigit(swap_usage); draw_swapdigit(swap_usage);
/* show */ /* show */
dockapp_copy2window(pixmap); dockapp_copy2window(pixmap);
} }
static void draw_memdigit(int per) static void draw_memdigit(int per)
{ {
int v100, v10, v1; int v100, v10, v1;
int y = 0; int y = 0;
if (per < 0) per = 0; if (per < 0)
if (per > 100) per = 100; per = 0;
if (per > 100)
per = 100;
v100 = per / 100; v100 = per / 100;
v10 = (per - v100 * 100) / 10; v10 = (per - v100 * 100) / 10;
v1 = (per - v100 * 100 - v10 * 10); v1 = (per - v100 * 100 - v10 * 10);
if (backlight == LIGHTON) y = 20; if (backlight == LIGHTON)
y = 20;
/* draw digit */ /* draw digit */
dockapp_copyarea(parts, pixmap, v1 * 10, y, 10, 20, 29, 7); dockapp_copyarea(parts, pixmap, v1 * 10, y, 10, 20, 29, 7);
if (v10 != 0) if (v10 != 0)
dockapp_copyarea(parts, pixmap, v10 * 10, y, 10, 20, 17, 7); dockapp_copyarea(parts, pixmap, v10 * 10, y, 10, 20, 17, 7);
if (v100 == 1) if (v100 == 1) {
{ dockapp_copyarea(parts, pixmap, 10, y, 10, 20, 5, 7);
dockapp_copyarea(parts, pixmap, 10, y, 10, 20, 5, 7); dockapp_copyarea(parts, pixmap, 0, y, 10, 20, 17, 7);
dockapp_copyarea(parts, pixmap, 0, y, 10, 20, 17, 7); }
}
} }
static void draw_swapdigit(int per) static void draw_swapdigit(int per)
{ {
int v100, v10, v1; int v100, v10, v1;
int y = 0; int y = 0;
if (per < 0) per = 0; if (per < 0)
if (per > 100) per = 100; per = 0;
if (per > 100)
per = 100;
v100 = per / 100; v100 = per / 100;
v10 = (per - v100 * 100) / 10; v10 = (per - v100 * 100) / 10;
v1 = (per - v100 * 100 - v10 * 10); v1 = (per - v100 * 100 - v10 * 10);
if (backlight == LIGHTON) y = 20; if (backlight == LIGHTON)
y = 20;
/* draw digit */ /* draw digit */
dockapp_copyarea(parts, pixmap, v1 * 10, y, 10, 20, 29, 34); dockapp_copyarea(parts, pixmap, v1 * 10, y, 10, 20, 29, 34);
if (v10 != 0) if (v10 != 0)
dockapp_copyarea(parts, pixmap, v10 * 10, y, 10, 20, 17, 34); dockapp_copyarea(parts, pixmap, v10 * 10, y, 10, 20, 17, 34);
if (v100 == 1) if (v100 == 1) {
{ dockapp_copyarea(parts, pixmap, 10, y, 10, 20, 5, 34);
dockapp_copyarea(parts, pixmap, 10, y, 10, 20, 5, 34); dockapp_copyarea(parts, pixmap, 0, y, 10, 20, 17, 34);
dockapp_copyarea(parts, pixmap, 0, y, 10, 20, 17, 34); }
}
} }
static void parse_arguments(int argc, char **argv) static void parse_arguments(int argc, char **argv)
{ {
int i; int i;
int integer; int integer;
for (i = 1; i < argc; i++) for (i = 1; i < argc; i++) {
{ if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h"))
if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h")) print_help(argv[0]), exit(0);
print_help(argv[0]), exit(0); else if (!strcmp(argv[i], "--version") || !strcmp(argv[i], "-v"))
else if (!strcmp(argv[i], "--version") || !strcmp(argv[i], "-v")) printf("%s version %s\n", PACKAGE, VERSION), exit(0);
printf("%s version %s\n", PACKAGE, VERSION), exit(0); else if (!strcmp(argv[i], "--display") || !strcmp(argv[i], "-d")) {
else if (!strcmp(argv[i], "--display") || !strcmp(argv[i], "-d")) display_name = argv[i + 1];
{ i++;
display_name = argv[i + 1]; } else if (!strcmp(argv[i], "--alarm-mem") || !strcmp(argv[i], "-am")) {
i++; if (argc == i + 1)
} alarm_mem = 90;
else if (!strcmp(argv[i], "--alarm-mem") || !strcmp(argv[i], "-am")) else if (sscanf(argv[i + 1], "%i", &integer) != 1)
{ alarm_mem = 90;
if (argc == i + 1) else if (integer < 0 || integer > 100)
alarm_mem = 90; fprintf(stderr, "%s: argument %s must be from 0 to 100\n",
else if (sscanf(argv[i + 1], "%i", &integer) != 1) argv[0], argv[i]), exit(1);
alarm_mem = 90; else
else if (integer < 0 || integer > 100) alarm_mem = integer, i++;
fprintf(stderr, "%s: argument %s must be from 0 to 100\n", } else if (!strcmp(argv[i], "--alarm-swap") || !strcmp(argv[i], "-as")) {
argv[0], argv[i]), exit(1); if (argc == i + 1)
else alarm_swap = 50;
alarm_mem = integer, i++; else if (sscanf(argv[i + 1], "%i", &integer) != 1)
} alarm_swap = 50;
else if (!strcmp(argv[i], "--alarm-swap") || !strcmp(argv[i], "-as")) else if (integer < 0 || integer > 100)
{ fprintf(stderr, "%s: argument %s must be from 0 to 100\n",
if (argc == i + 1) argv[0], argv[i]), exit(1);
alarm_swap = 50; else
else if (sscanf(argv[i + 1], "%i", &integer) != 1) alarm_swap = integer, i++;
alarm_swap = 50; } else if (!strcmp(argv[i], "--backlight") || !strcmp(argv[i], "-bl"))
else if (integer < 0 || integer > 100) backlight = LIGHTON;
fprintf(stderr, "%s: argument %s must be from 0 to 100\n", else if (!strcmp(argv[i], "--light-color") || !strcmp(argv[i], "-lc")) {
argv[0], argv[i]), exit(1); light_color = argv[i + 1];
else i++;
alarm_swap = integer, i++; } else if (!strcmp(argv[i], "--interval") || !strcmp(argv[i], "-i")) {
} if (argc == i + 1)
else if (!strcmp(argv[i], "--backlight") || !strcmp(argv[i], "-bl")) fprintf(stderr, "%s: error parsing argument for option %s\n",
backlight = LIGHTON; argv[0], argv[i]), exit(1);
else if (!strcmp(argv[i], "--light-color") || !strcmp(argv[i], "-lc")) if (sscanf(argv[i + 1], "%i", &integer) != 1)
{ fprintf(stderr, "%s: error parsing argument for option %s\n",
light_color = argv[i + 1]; argv[0], argv[i]), exit(1);
i++; if (integer < 1)
} fprintf(stderr, "%s: argument %s must be >=1\n",
else if (!strcmp(argv[i], "--interval") || !strcmp(argv[i], "-i")) argv[0], argv[i]), exit(1);
{ update_interval = integer;
if (argc == i + 1) i++;
fprintf(stderr, "%s: error parsing argument for option %s\n", } else if (!strcmp(argv[i], "--windowed") || !strcmp(argv[i], "-w"))
argv[0], argv[i]), exit(1); dockapp_iswindowed = True;
if (sscanf(argv[i + 1], "%i", &integer) != 1) else if (!strcmp(argv[i], "--broken-wm") || !strcmp(argv[i], "-bw"))
fprintf(stderr, "%s: error parsing argument for option %s\n", dockapp_isbrokenwm = True;
argv[0], argv[i]), exit(1);
if (integer < 1)
fprintf(stderr, "%s: argument %s must be >=1\n",
argv[0], argv[i]), exit(1);
update_interval = integer;
i++;
}
else if (!strcmp(argv[i], "--windowed") || !strcmp(argv[i], "-w"))
dockapp_iswindowed = True;
else if (!strcmp(argv[i], "--broken-wm") || !strcmp(argv[i], "-bw"))
dockapp_isbrokenwm = True;
#ifdef IGNORE_BUFFERS #ifdef IGNORE_BUFFERS
else if (!strcmp(argv[i], "--ignore-buffers") || !strcmp(argv[i], "-b")) else if (!strcmp(argv[i], "--ignore-buffers") || !strcmp(argv[i], "-b"))
mem_opts.ignore_buffers = True; mem_opts.ignore_buffers = True;
#endif #endif
#ifdef IGNORE_CACHED #ifdef IGNORE_CACHED
else if (!strcmp(argv[i], "--ignore-cached") || !strcmp(argv[i], "-c")) else if (!strcmp(argv[i], "--ignore-cached") || !strcmp(argv[i], "-c"))
mem_opts.ignore_cached = True; mem_opts.ignore_cached = True;
#endif #endif
#ifdef IGNORE_WIRED #ifdef IGNORE_WIRED
else if (!strcmp(argv[i], "--ignore-wired") || !strcmp(argv[i], "-wr")) else if (!strcmp(argv[i], "--ignore-wired") || !strcmp(argv[i], "-wr"))
mem_opts.ignore_wired = True; mem_opts.ignore_wired = True;
#endif #endif
else else {
{ fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0], argv[i]);
fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0], argv[i]); print_help(argv[0]), exit(1);
print_help(argv[0]), exit(1); }
} }
} if (alarm_mem != 101 && alarm_swap != 101) {
if (alarm_mem != 101 && alarm_swap != 101) fprintf(stderr,
{ "%s: select either '-am, --alarm-mem' or '-as, --alarm-swap'\n",
fprintf(stderr, argv[0]);
"%s: select either '-am, --alarm-mem' or '-as, --alarm-swap'\n", exit(1);
argv[0]); }
exit(1);
}
} }
static void print_help(char *prog) static void print_help(char *prog)
{ {
printf("Usage : %s [OPTIONS]\n", prog); printf("Usage : %s [OPTIONS]\n", prog);
printf("WMMemMon - Window Maker memory/swap monitor dockapp\n"); printf("WMMemMon - Window Maker memory/swap monitor dockapp\n");
printf(" -d, --display <string> display to use\n"); printf(" -d, --display <string> display to use\n");
printf(" -bl, --backlight turn on back-light\n"); printf(" -bl, --backlight turn on back-light\n");
printf(" -lc, --light-color <string> back-light color(rgb:6E/C6/3B is default)\n"); printf(" -lc, --light-color <string> back-light color(rgb:6E/C6/3B is default)\n");
printf(" -i, --interval <number> number of secs between updates (1 is default)\n"); printf(" -i, --interval <number> number of secs between updates (1 is default)\n");
#ifdef IGNORE_BUFFERS #ifdef IGNORE_BUFFERS
printf(" -b, --ignore-buffers ignore buffers\n"); printf(" -b, --ignore-buffers ignore buffers\n");
#endif #endif
#ifdef IGNORE_CACHED #ifdef IGNORE_CACHED
printf(" -c, --ignore-cached ignore cached pages\n"); printf(" -c, --ignore-cached ignore cached pages\n");
#endif #endif
#ifdef IGNORE_WIRED #ifdef IGNORE_WIRED
printf(" -wr, --ignore-wired ignore wired pages\n"); printf(" -wr, --ignore-wired ignore wired pages\n");
#endif #endif
printf(" -h, --help show this help text and exit\n"); printf(" -h, --help show this help text and exit\n");
printf(" -v, --version show program version and exit\n"); printf(" -v, --version show program version and exit\n");
printf(" -w, --windowed run the application in windowed mode\n"); printf(" -w, --windowed run the application in windowed mode\n");
printf(" -bw, --broken-wm activate broken window manager fix\n"); printf(" -bw, --broken-wm activate broken window manager fix\n");
printf(" -am, --alarm-mem <percentage> activate alarm mode of memory. <percentage>\n"); printf(" -am, --alarm-mem <percentage> activate alarm mode of memory. <percentage>\n");
printf(" is threshold of percentage from 0 to 100.\n"); printf(" is threshold of percentage from 0 to 100.\n");
printf(" (90 is default)\n"); printf(" (90 is default)\n");
printf(" -as, --alarm-swap <percentage> activate alarm mode of swap. <percentage> is\n"); printf(" -as, --alarm-swap <percentage> activate alarm mode of swap. <percentage> is\n");
printf(" threshold of percentage from 0 to 100.\n"); printf(" threshold of percentage from 0 to 100.\n");
printf(" (50 is default)\n"); printf(" (50 is default)\n");
} }

View file

@ -7,9 +7,9 @@
*/ */
struct mem_options { struct mem_options {
int ignore_buffers; int ignore_buffers;
int ignore_cached; int ignore_cached;
int ignore_wired; int ignore_wired;
}; };
void mem_init(void); void mem_init(void);

View file

@ -32,112 +32,116 @@
#ifdef DEBUG #ifdef DEBUG
# define INLINE_STATIC static # define INLINE_STATIC static
#else #else
# define INLINE_STATIC inline static # define INLINE_STATIC static inline
#endif #endif
/* initialize function */ /* initialize function */
void mem_init(void) void mem_init(void)
{ {
struct utsname un; struct utsname un;
int version, patchlevel; int version, patchlevel;
/* get kernel version */ /* get kernel version */
if (uname(&un) == -1) if (uname(&un) == -1)
perror ("uname()"); perror("uname()");
sscanf (un.release, "%d.%d", &version, &patchlevel); sscanf(un.release, "%d.%d", &version, &patchlevel);
} }
INLINE_STATIC char * skip_line (const char *p) INLINE_STATIC char *skip_line(const char *p)
{ {
while (*p != '\n') p++; while (*p != '\n')
return (char *) ++p; p++;
return (char *) ++p;
} }
INLINE_STATIC char * skip_token (const char *p) INLINE_STATIC char *skip_token(const char *p)
{ {
while (isspace(*p)) p++; while (isspace(*p))
while (*p && !isspace(*p)) p++; p++;
return (char *)p; while (*p && !isspace(*p))
p++;
return (char *)p;
} }
INLINE_STATIC char * skip_multiple_token (const char *p, int count) INLINE_STATIC char *skip_multiple_token(const char *p, int count)
{ {
int i; int i;
for (i = 0; i < count; i++) p = skip_token (p); for (i = 0; i < count; i++)
return (char *)p; p = skip_token(p);
return (char *)p;
} }
/* return mem/swap usage in percent 0 to 100 */ /* return mem/swap usage in percent 0 to 100 */
void mem_getusage(int *per_mem, int *per_swap, const struct mem_options *opts) void mem_getusage(int *per_mem, int *per_swap, const struct mem_options *opts)
{ {
char buffer[BUFSIZ], *p; char buffer[BUFSIZ], *p;
int fd, len, i; int fd, len, i;
u_int64_t mtotal, mused, mfree, mbuffer, mcached; u_int64_t mtotal, mused, mfree, mbuffer, mcached;
u_int64_t stotal, sused, sfree, scached = 0; u_int64_t stotal, sused, sfree, scached = 0;
/* read /proc/meminfo */ /* read /proc/meminfo */
fd = open("/proc/meminfo", O_RDONLY); fd = open("/proc/meminfo", O_RDONLY);
if (fd < 0) { if (fd < 0) {
perror("can't open /proc/meminfo"); perror("can't open /proc/meminfo");
exit(1); exit(1);
} }
len = read(fd, buffer, BUFSIZ - 1); len = read(fd, buffer, BUFSIZ - 1);
if (len < 0) { if (len < 0) {
perror("can't read /proc/meminfo"); perror("can't read /proc/meminfo");
exit(1); exit(1);
} }
close(fd); close(fd);
buffer[len] = '\0'; buffer[len] = '\0';
p = buffer; p = buffer;
p = skip_token(p); p = skip_token(p);
/* examine each line of file */ /* examine each line of file */
mtotal = strtoul(p, &p, 0); p = skip_multiple_token(p, 2); mtotal = strtoul(p, &p, 0); p = skip_multiple_token(p, 2);
mfree = strtoul(p, &p, 0); p = skip_multiple_token(p, 2); mfree = strtoul(p, &p, 0); p = skip_multiple_token(p, 2);
mbuffer = strtoul(p, &p, 0); p = skip_multiple_token(p, 2); mbuffer = strtoul(p, &p, 0); p = skip_multiple_token(p, 2);
mcached = strtoul(p, &p, 0); p = skip_multiple_token(p, 2); mcached = strtoul(p, &p, 0); p = skip_multiple_token(p, 2);
scached = strtoul(p, &p, 0); scached = strtoul(p, &p, 0);
/* skip N lines and examine info about swap */ /* skip N lines and examine info about swap */
while (isprint(p[0])) { while (isprint(p[0])) {
p = skip_line(p); p = skip_line(p);
if (strncmp(p, "SwapTotal", 9) == 0) break; if (strncmp(p, "SwapTotal", 9) == 0)
} break;
}
p = skip_token(p); p = skip_token(p);
stotal = strtoul(p, &p, 0); p = skip_multiple_token(p, 2); stotal = strtoul(p, &p, 0); p = skip_multiple_token(p, 2);
sfree = strtoul(p, &p, 0); sfree = strtoul(p, &p, 0);
/* calculate memory usage in percent */ /* calculate memory usage in percent */
mused = mtotal - mfree; mused = mtotal - mfree;
if (opts->ignore_buffers) if (opts->ignore_buffers)
mused -= mbuffer; mused -= mbuffer;
if (opts->ignore_cached) if (opts->ignore_cached)
mused -= mcached; mused -= mcached;
*per_mem = 100 * (double) mused / (double) mtotal; *per_mem = 100 * (double) mused / (double) mtotal;
/* calculate swap usage in percent */ /* calculate swap usage in percent */
sused = stotal - sfree; sused = stotal - sfree;
if(opts->ignore_cached) if (opts->ignore_cached)
sused -= scached; sused -= scached;
if (!stotal) { if (!stotal)
*per_swap = 0; *per_swap = 0;
} else { else
*per_swap = 100 * (double) sused / (double) stotal; *per_swap = 100 * (double) sused / (double) stotal;
}
#if DEBUG #if DEBUG
printf("-----------------------\n"); printf("-----------------------\n");
printf("MemTotal: %12ld\n", (unsigned long)mtotal); printf("MemTotal: %12ld\n", (unsigned long)mtotal);
printf("MemFree: %12ld\n", (unsigned long)mfree); printf("MemFree: %12ld\n", (unsigned long)mfree);
printf("Buffers: %12ld\n", (unsigned long)mbuffer); printf("Buffers: %12ld\n", (unsigned long)mbuffer);
printf("Cached: %12ld\n", (unsigned long)mcached); printf("Cached: %12ld\n", (unsigned long)mcached);
printf("SwapTotal: %12ld\n", (unsigned long)stotal); printf("SwapTotal: %12ld\n", (unsigned long)stotal);
printf("SwapFree: %12ld\n", (unsigned long)sfree); printf("SwapFree: %12ld\n", (unsigned long)sfree);
printf("SwapCached:%12ld\n", (unsigned long)scached); printf("SwapCached:%12ld\n", (unsigned long)scached);
printf("-----------------------\n\n"); printf("-----------------------\n\n");
#endif #endif
} }

View file

@ -28,140 +28,143 @@
#include <sys/utsname.h> #include <sys/utsname.h>
#include "mem.h" #include "mem.h"
static int isnewformat = 0; /* for kernel 2.5.1 or later */ static int isnewformat; /* for kernel 2.5.1 or later */
#ifdef DEBUG #ifdef DEBUG
# define INLINE_STATIC static # define INLINE_STATIC static
#else #else
# define INLINE_STATIC inline static # define INLINE_STATIC static inline
#endif #endif
/* initialize function */ /* initialize function */
void mem_init(void) void mem_init(void)
{ {
struct utsname un; struct utsname un;
int version, patchlevel, sublevel; int version, patchlevel, sublevel;
/* get kernel version */ /* get kernel version */
if (uname(&un) == -1) if (uname(&un) == -1)
perror ("uname()"); perror("uname()");
sscanf (un.release, "%d.%d.%d", &version, &patchlevel, &sublevel); sscanf(un.release, "%d.%d.%d", &version, &patchlevel, &sublevel);
/* new format ? (kernel >= 2.5.1pre?) */ /* new format ? (kernel >= 2.5.1pre?) */
/* see linux/fs/proc/proc_misc.c */ /* see linux/fs/proc/proc_misc.c */
if ((version == 2 && patchlevel >= 5 && sublevel >= 1) || \ if ((version == 2 && patchlevel >= 5 && sublevel >= 1) ||
(version == 2 && patchlevel >= 6 && sublevel >= 0) || \ (version == 2 && patchlevel >= 6 && sublevel >= 0) ||
version >2 ) version > 2)
isnewformat = 1; isnewformat = 1;
} }
INLINE_STATIC char * skip_line (const char *p) INLINE_STATIC char *skip_line(const char *p)
{ {
while (*p != '\n') p++; while (*p != '\n')
return (char *) ++p; p++;
return (char *) ++p;
} }
INLINE_STATIC char * skip_token (const char *p) INLINE_STATIC char *skip_token(const char *p)
{ {
while (isspace(*p)) p++; while (isspace(*p))
while (*p && !isspace(*p)) p++; p++;
return (char *)p; while (*p && !isspace(*p))
p++;
return (char *)p;
} }
INLINE_STATIC char * skip_multiple_token (const char *p, int count) INLINE_STATIC char *skip_multiple_token(const char *p, int count)
{ {
int i; int i;
for (i = 0; i < count; i++) p = skip_token (p); for (i = 0; i < count; i++)
return (char *)p; p = skip_token(p);
return (char *)p;
} }
/* return mem/swap usage in percent 0 to 100 */ /* return mem/swap usage in percent 0 to 100 */
void mem_getusage(int *per_mem, int *per_swap, const struct mem_options *opts) void mem_getusage(int *per_mem, int *per_swap, const struct mem_options *opts)
{ {
char buffer[BUFSIZ], *p; char buffer[BUFSIZ], *p;
int fd, len, i; int fd, len, i;
u_int64_t mtotal, mused, mfree, mbuffer, mcached; u_int64_t mtotal, mused, mfree, mbuffer, mcached;
u_int64_t stotal, sused, sfree, scached = 0; u_int64_t stotal, sused, sfree, scached = 0;
/* read /proc/meminfo */ /* read /proc/meminfo */
fd = open("/proc/meminfo", O_RDONLY); fd = open("/proc/meminfo", O_RDONLY);
if (fd < 0) { if (fd < 0) {
perror("can't open /proc/meminfo"); perror("can't open /proc/meminfo");
exit(1); exit(1);
} }
len = read(fd, buffer, BUFSIZ - 1); len = read(fd, buffer, BUFSIZ - 1);
if (len < 0) { if (len < 0) {
perror("can't read /proc/meminfo"); perror("can't read /proc/meminfo");
exit(1); exit(1);
} }
close(fd); close(fd);
buffer[len] = '\0'; buffer[len] = '\0';
p = buffer; p = buffer;
if (!isnewformat) { if (!isnewformat) {
/* skip 3 lines */ /* skip 3 lines */
for (i = 0; i < 3; i++) for (i = 0; i < 3; i++)
p = skip_line(p); p = skip_line(p);
p = skip_token(p); p = skip_token(p);
/* examine each line of file */ /* examine each line of file */
mtotal = strtoul(p, &p, 0); p = skip_multiple_token(p, 2); mtotal = strtoul(p, &p, 0); p = skip_multiple_token(p, 2);
mfree = strtoul(p, &p, 0); p = skip_multiple_token(p, 5); mfree = strtoul(p, &p, 0); p = skip_multiple_token(p, 5);
mbuffer = strtoul(p, &p, 0); p = skip_multiple_token(p, 2); mbuffer = strtoul(p, &p, 0); p = skip_multiple_token(p, 2);
mcached = strtoul(p, &p, 0); p = skip_multiple_token(p, 2); mcached = strtoul(p, &p, 0); p = skip_multiple_token(p, 2);
scached = strtoul(p, &p, 0); scached = strtoul(p, &p, 0);
} } else {
else { p = skip_token(p);
p = skip_token(p); /* examine each line of file */
/* examine each line of file */ mtotal = strtoul(p, &p, 0); p = skip_multiple_token(p, 2);
mtotal = strtoul(p, &p, 0); p = skip_multiple_token(p, 2); mfree = strtoul(p, &p, 0); p = skip_multiple_token(p, 2);
mfree = strtoul(p, &p, 0); p = skip_multiple_token(p, 2); mbuffer = strtoul(p, &p, 0); p = skip_multiple_token(p, 2);
mbuffer = strtoul(p, &p, 0); p = skip_multiple_token(p, 2); mcached = strtoul(p, &p, 0); p = skip_multiple_token(p, 2);
mcached = strtoul(p, &p, 0); p = skip_multiple_token(p, 2); scached = strtoul(p, &p, 0);
scached = strtoul(p, &p, 0); }
}
/* skip N lines and examine info about swap */ /* skip N lines and examine info about swap */
/* kernel-2.4.2:N=8 2.4.16:N=7 */ /* kernel-2.4.2:N=8 2.4.16:N=7 */
while (isprint(p[0])) { while (isprint(p[0])) {
p = skip_line(p); p = skip_line(p);
if (strncmp(p, "SwapTotal", 9) == 0) break; if (strncmp(p, "SwapTotal", 9) == 0)
} break;
}
p = skip_token(p); p = skip_token(p);
stotal = strtoul(p, &p, 0); p = skip_multiple_token(p, 2); stotal = strtoul(p, &p, 0); p = skip_multiple_token(p, 2);
sfree = strtoul(p, &p, 0); sfree = strtoul(p, &p, 0);
/* calculate memory usage in percent */ /* calculate memory usage in percent */
mused = mtotal - mfree; mused = mtotal - mfree;
if (opts->ignore_buffers) if (opts->ignore_buffers)
mused -= mbuffer; mused -= mbuffer;
if (opts->ignore_cached) if (opts->ignore_cached)
mused -= mcached; mused -= mcached;
*per_mem = 100 * (double) mused / (double) mtotal; *per_mem = 100 * (double) mused / (double) mtotal;
/* calculate swap usage in percent */ /* calculate swap usage in percent */
sused = stotal - sfree; sused = stotal - sfree;
if(opts->ignore_cached) if (opts->ignore_cached)
sused -= scached; sused -= scached;
if (!stotal) { if (!stotal)
*per_swap = 0; *per_swap = 0;
} else { else
*per_swap = 100 * (double) sused / (double) stotal; *per_swap = 100 * (double) sused / (double) stotal;
}
#if DEBUG #if DEBUG
printf("-----------------------\n"); printf("-----------------------\n");
printf("MemTotal: %12ld\n", (unsigned long)mtotal); printf("MemTotal: %12ld\n", (unsigned long)mtotal);
printf("MemFree: %12ld\n", (unsigned long)mfree); printf("MemFree: %12ld\n", (unsigned long)mfree);
printf("Buffers: %12ld\n", (unsigned long)mbuffer); printf("Buffers: %12ld\n", (unsigned long)mbuffer);
printf("Cached: %12ld\n", (unsigned long)mcached); printf("Cached: %12ld\n", (unsigned long)mcached);
printf("SwapTotal: %12ld\n", (unsigned long)stotal); printf("SwapTotal: %12ld\n", (unsigned long)stotal);
printf("SwapFree: %12ld\n", (unsigned long)sfree); printf("SwapFree: %12ld\n", (unsigned long)sfree);
printf("SwapCached:%12ld\n", (unsigned long)scached); printf("SwapCached:%12ld\n", (unsigned long)scached);
printf("-----------------------\n\n"); printf("-----------------------\n\n");
#endif #endif
} }

View file

@ -24,41 +24,43 @@
void void
mem_init(void) mem_init(void)
{ {
return; return;
} }
/* get swap usage */ /* get swap usage */
static int static int
get_swap_usage(void) get_swap_usage(void)
{ {
struct swapent *swap_dev; struct swapent *swap_dev;
int num_swap; int num_swap;
int stotal, sused, i; int stotal, sused, i;
stotal = sused = 0; stotal = sused = 0;
if ((num_swap = swapctl(SWAP_NSWAP, 0, 0)) == 0) num_swap = swapctl(SWAP_NSWAP, 0, 0);
return 0; if (num_swap == 0)
return 0;
if ((swap_dev = malloc(num_swap * sizeof(*swap_dev))) == NULL) swap_dev = malloc(num_swap * sizeof(*swap_dev));
return 0; if (swap_dev == NULL)
return 0;
if (swapctl(SWAP_STATS, swap_dev, num_swap) == -1) if (swapctl(SWAP_STATS, swap_dev, num_swap) == -1)
return 0; return 0;
for (i = 0; i < num_swap; i++) { for (i = 0; i < num_swap; i++) {
if (swap_dev[i].se_flags & SWF_ENABLE) { if (swap_dev[i].se_flags & SWF_ENABLE) {
stotal += swap_dev[i].se_nblks; stotal += swap_dev[i].se_nblks;
sused += swap_dev[i].se_inuse; sused += swap_dev[i].se_inuse;
}
} }
}
free(swap_dev); free(swap_dev);
if (sused == 0) if (sused == 0)
return 0; return 0;
return (100 * (double) sused / (double) stotal); return (100 * (double) sused / (double) stotal);
} }
@ -67,30 +69,31 @@ void
mem_getusage(int *per_mem, int *per_swap, const struct mem_options *opts) mem_getusage(int *per_mem, int *per_swap, const struct mem_options *opts)
{ {
struct vmtotal vm; struct vmtotal vm;
size_t size = sizeof(vm); size_t size = sizeof(vm);
static int mib[] = { CTL_VM, VM_METER }; static int mib[] = { CTL_VM, VM_METER };
/* get mem usage */ /* get mem usage */
if (sysctl(mib, 2, &vm, &size, NULL, 0) < 0) if (sysctl(mib, 2, &vm, &size, NULL, 0) < 0)
bzero(&vm, sizeof(vm)); bzero(&vm, sizeof(vm));
/* calc mem usage in percent */ /* calc mem usage in percent */
if (vm.t_rm > 0) if (vm.t_rm > 0)
*per_mem = 100 * (double) vm.t_rm / (double) (vm.t_rm + vm.t_free); *per_mem = 100 * (double) vm.t_rm / (double) (vm.t_rm + vm.t_free);
if (*per_mem > 97) *per_mem = 100; if (*per_mem > 97)
*per_mem = 100;
/* swap */ /* swap */
*per_swap = get_swap_usage(); *per_swap = get_swap_usage();
#ifdef DEBUG #ifdef DEBUG
printf("t_rm total real memory in use %6d\n", vm.t_rm); printf("t_rm total real memory in use %6d\n", vm.t_rm);
printf("t_arm active real memory %6d\n", vm.t_arm); printf("t_arm active real memory %6d\n", vm.t_arm);
printf("t_rmshr shared real memory %6d\n", vm.t_rmshr); printf("t_rmshr shared real memory %6d\n", vm.t_rmshr);
printf("t_armshr active shared real memory %6d\n", vm.t_armshr); printf("t_armshr active shared real memory %6d\n", vm.t_armshr);
printf("t_free free memory pages %6d\n", vm.t_free); printf("t_free free memory pages %6d\n", vm.t_free);
#endif #endif
return; return;
} }

View file

@ -22,30 +22,30 @@
/* initialize function */ /* initialize function */
void mem_init(void) void mem_init(void)
{ {
/* solaris doesn't need initialization */ /* solaris doesn't need initialization */
return; return;
} }
/* return memory/swap usage in percent 0 to 100 */ /* return memory/swap usage in percent 0 to 100 */
void mem_getusage(int *per_mem, int *per_swap, const struct mem_options *opts) void mem_getusage(int *per_mem, int *per_swap, const struct mem_options *opts)
{ {
static int mem_total; static int mem_total;
static int mem_free; static int mem_free;
static int swap_total; static int swap_total;
static int swap_free; static int swap_free;
struct anoninfo anon; struct anoninfo anon;
/* get mem usage */ /* get mem usage */
mem_total = sysconf(_SC_PHYS_PAGES); mem_total = sysconf(_SC_PHYS_PAGES);
mem_free = sysconf(_SC_AVPHYS_PAGES); mem_free = sysconf(_SC_AVPHYS_PAGES);
/* get swap usage */ /* get swap usage */
if (swapctl(SC_AINFO, &anon) == -1) if (swapctl(SC_AINFO, &anon) == -1)
exit(1); exit(1);
swap_total = anon.ani_max; swap_total = anon.ani_max;
swap_free = anon.ani_max - anon.ani_resv; swap_free = anon.ani_max - anon.ani_resv;
/* calc mem/swap usage in percent */ /* calc mem/swap usage in percent */
*per_mem = 100 * (1 - ((double) mem_free / (double) mem_total)); *per_mem = 100 * (1 - ((double) mem_free / (double) mem_total));
*per_swap = 100 * (1 - ((double) swap_free / (double) swap_total)); *per_swap = 100 * (1 - ((double) swap_free / (double) swap_total));
} }