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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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