1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-11-05 21:07:52 +00:00

cleaned code; adding initial graph stuff, I will probably finish this later, but I wanted to get started

git-svn-id: https://conky.svn.sourceforge.net/svnroot/conky/trunk/conky@22 7f574dfc-610e-0410-a909-a81674777703
This commit is contained in:
Brenden Matthews 2005-07-28 04:48:27 +00:00
parent d898d143ef
commit b6632fd510
14 changed files with 3178 additions and 2673 deletions

217
cairo.c
View File

@ -23,69 +23,65 @@
/* Fill the given area with checks in the standard style /* Fill the given area with checks in the standard style
* for showing compositing effects. * for showing compositing effects.
*/ */
static void static void fill_checks(cairo_t * cr, int x, int y, int width, int height)
fill_checks (cairo_t *cr,
int x, int y,
int width, int height)
{ {
cairo_surface_t *check; cairo_surface_t *check;
cairo_pattern_t *check_pattern; cairo_pattern_t *check_pattern;
cairo_save (cr); cairo_save(cr);
#define CHECK_SIZE 32 #define CHECK_SIZE 32
check = cairo_surface_create_similar (cairo_current_target_surface (cr), check =
CAIRO_FORMAT_RGB24, cairo_surface_create_similar(cairo_current_target_surface(cr),
2 * CHECK_SIZE, 2 * CHECK_SIZE); CAIRO_FORMAT_RGB24,
cairo_surface_set_repeat (check, 1); 2 * CHECK_SIZE, 2 * CHECK_SIZE);
cairo_surface_set_repeat(check, 1);
/* Draw the check */ /* Draw the check */
{ {
cairo_save (cr); cairo_save(cr);
cairo_set_target_surface (cr, check); cairo_set_target_surface(cr, check);
cairo_set_operator (cr, CAIRO_OPERATOR_SRC); cairo_set_operator(cr, CAIRO_OPERATOR_SRC);
cairo_set_rgb_color (cr, 0.4, 0.4, 0.4); cairo_set_rgb_color(cr, 0.4, 0.4, 0.4);
cairo_rectangle (cr, 0, 0, 2 * CHECK_SIZE, 2 * CHECK_SIZE); cairo_rectangle(cr, 0, 0, 2 * CHECK_SIZE, 2 * CHECK_SIZE);
cairo_fill (cr); cairo_fill(cr);
cairo_set_rgb_color (cr, 0.7, 0.7, 0.7); cairo_set_rgb_color(cr, 0.7, 0.7, 0.7);
cairo_rectangle (cr, x, y, CHECK_SIZE, CHECK_SIZE); cairo_rectangle(cr, x, y, CHECK_SIZE, CHECK_SIZE);
cairo_fill (cr); cairo_fill(cr);
cairo_rectangle (cr, x + CHECK_SIZE, y + CHECK_SIZE, CHECK_SIZE, CHECK_SIZE); cairo_rectangle(cr, x + CHECK_SIZE, y + CHECK_SIZE,
cairo_fill (cr); CHECK_SIZE, CHECK_SIZE);
cairo_fill(cr);
cairo_restore (cr); cairo_restore(cr);
} }
/* Fill the whole surface with the check */ /* Fill the whole surface with the check */
check_pattern = cairo_pattern_create_for_surface (check); check_pattern = cairo_pattern_create_for_surface(check);
cairo_set_pattern (cr, check_pattern); cairo_set_pattern(cr, check_pattern);
cairo_rectangle (cr, 0, 0, width, height); cairo_rectangle(cr, 0, 0, width, height);
cairo_fill (cr); cairo_fill(cr);
cairo_pattern_destroy (check_pattern); cairo_pattern_destroy(check_pattern);
cairo_surface_destroy (check); cairo_surface_destroy(check);
cairo_restore (cr); cairo_restore(cr);
} }
static void draw_pee (cairo_t *cr, double xc, double yc) static void draw_pee(cairo_t * cr, double xc, double yc)
{ {
cairo_set_rgb_color (cr, 0, 0, 0); cairo_set_rgb_color(cr, 0, 0, 0);
cairo_show_text (cr, "Conky"); cairo_show_text(cr, "Conky");
} }
static void static void draw(cairo_t * cr, int width, int height)
draw (cairo_t *cr,
int width,
int height)
{ {
cairo_surface_t *overlay; cairo_surface_t *overlay;
@ -93,30 +89,30 @@ static void
double xc = width / 2.; double xc = width / 2.;
double yc = height / 2.; double yc = height / 2.;
overlay = cairo_surface_create_similar (cairo_current_target_surface (cr), overlay =
CAIRO_FORMAT_ARGB32, cairo_surface_create_similar(cairo_current_target_surface(cr),
width, height); CAIRO_FORMAT_ARGB32, width,
height);
if (overlay == NULL) if (overlay == NULL)
return; return;
fill_checks (cr, 0, 0, width, height); fill_checks(cr, 0, 0, width, height);
cairo_save (cr); cairo_save(cr);
cairo_set_target_surface (cr, overlay); cairo_set_target_surface(cr, overlay);
cairo_set_alpha (cr, 0.5); cairo_set_alpha(cr, 0.5);
cairo_set_operator (cr, CAIRO_OPERATOR_OVER); cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
draw_pee (cr, xc, yc); draw_pee(cr, xc, yc);
cairo_restore (cr); cairo_restore(cr);
cairo_show_surface (cr, overlay, width, height); cairo_show_surface(cr, overlay, width, height);
cairo_surface_destroy (overlay); cairo_surface_destroy(overlay);
} }
int int do_it(void)
do_it (void)
{ {
Display *dpy; Display *dpy;
int screen; int screen;
@ -133,98 +129,103 @@ int
int width = 400; int width = 400;
int height = 400; int height = 400;
dpy = XOpenDisplay (NULL); dpy = XOpenDisplay(NULL);
screen = DefaultScreen (dpy); screen = DefaultScreen(dpy);
w = XCreateSimpleWindow (dpy, RootWindow (dpy, screen), w = XCreateSimpleWindow(dpy, RootWindow(dpy, screen),
0, 0, width, height, 0, 0, 0, width, height, 0,
BlackPixel (dpy, screen), WhitePixel (dpy, screen)); BlackPixel(dpy, screen), WhitePixel(dpy,
screen));
normalhints = XAllocSizeHints (); normalhints = XAllocSizeHints();
normalhints->flags = 0; normalhints->flags = 0;
normalhints->x = 0; normalhints->x = 0;
normalhints->y = 0; normalhints->y = 0;
normalhints->width = width; normalhints->width = width;
normalhints->height = height; normalhints->height = height;
classhint = XAllocClassHint (); classhint = XAllocClassHint();
classhint->res_name = "cairo-knockout"; classhint->res_name = "cairo-knockout";
classhint->res_class = "Cairo-knockout"; classhint->res_class = "Cairo-knockout";
wmhints = XAllocWMHints (); wmhints = XAllocWMHints();
wmhints->flags = InputHint; wmhints->flags = InputHint;
wmhints->input = True; wmhints->input = True;
XmbSetWMProperties (dpy, w, title, "cairo-knockout", 0, 0, XmbSetWMProperties(dpy, w, title, "cairo-knockout", 0, 0,
normalhints, wmhints, classhint); normalhints, wmhints, classhint);
XFree (wmhints); XFree(wmhints);
XFree (classhint); XFree(classhint);
XFree (normalhints); XFree(normalhints);
pixmap = XCreatePixmap (dpy, w, width, height, DefaultDepth (dpy, screen)); pixmap =
gc = XCreateGC (dpy, pixmap, 0, NULL); XCreatePixmap(dpy, w, width, height,
DefaultDepth(dpy, screen));
gc = XCreateGC(dpy, pixmap, 0, NULL);
quit_keycode = XKeysymToKeycode(dpy, XStringToKeysym("Q")); quit_keycode = XKeysymToKeycode(dpy, XStringToKeysym("Q"));
XSelectInput (dpy, w, ExposureMask | StructureNotifyMask | ButtonPressMask | KeyPressMask); XSelectInput(dpy, w,
XMapWindow (dpy, w); ExposureMask | StructureNotifyMask | ButtonPressMask |
KeyPressMask);
XMapWindow(dpy, w);
needs_redraw = 1; needs_redraw = 1;
while (1) { while (1) {
XEvent xev; XEvent xev;
/* Only do the redraw if there are no events pending. This /* Only do the redraw if there are no events pending. This
* avoids us getting behind doing several redraws for several * avoids us getting behind doing several redraws for several
* consecutive resize events for example. * consecutive resize events for example.
*/ */
if (!XPending (dpy) && needs_redraw) { if (!XPending(dpy) && needs_redraw) {
cairo_t *cr = cairo_create (); cairo_t *cr = cairo_create();
cairo_set_target_drawable (cr, dpy, pixmap); cairo_set_target_drawable(cr, dpy, pixmap);
draw (cr, width, height); draw(cr, width, height);
cairo_destroy (cr); cairo_destroy(cr);
XCopyArea (dpy, pixmap, w, gc, XCopyArea(dpy, pixmap, w, gc,
0, 0, 0, 0, width, height, 0, 0);
width, height,
0, 0);
needs_redraw = 0; needs_redraw = 0;
} }
XNextEvent (dpy, &xev); XNextEvent(dpy, &xev);
switch (xev.xany.type) { switch (xev.xany.type) {
case ButtonPress: case ButtonPress:
/* A click on the canvas ends the program */ /* A click on the canvas ends the program */
goto DONE;
case KeyPress:
if (xev.xkey.keycode == quit_keycode)
goto DONE; goto DONE;
case KeyPress: break;
if (xev.xkey.keycode == quit_keycode) case ConfigureNotify:
goto DONE; /* Note new size and create new pixmap. */
break; width = xev.xconfigure.width;
case ConfigureNotify: height = xev.xconfigure.height;
/* Note new size and create new pixmap. */ XFreePixmap(dpy, pixmap);
width = xev.xconfigure.width; pixmap =
height = xev.xconfigure.height; XCreatePixmap(dpy, w, width, height,
XFreePixmap (dpy, pixmap); DefaultDepth(dpy, screen));
pixmap = XCreatePixmap (dpy, w, width, height, DefaultDepth (dpy, screen)); needs_redraw = 1;
needs_redraw = 1; break;
break; case Expose:
case Expose: XCopyArea(dpy, pixmap, w, gc,
XCopyArea (dpy, pixmap, w, gc, xev.xexpose.x, xev.xexpose.y,
xev.xexpose.x, xev.xexpose.y, xev.xexpose.width, xev.xexpose.height,
xev.xexpose.width, xev.xexpose.height, xev.xexpose.x, xev.xexpose.y);
xev.xexpose.x, xev.xexpose.y); break;
break;
} }
} }
DONE: DONE:
XFreeGC (dpy, gc); XFreeGC(dpy, gc);
XCloseDisplay (dpy); XCloseDisplay(dpy);
return 0; return 0;
} }

View File

@ -53,9 +53,8 @@ void variable_substitute(const char *s, char *dest, unsigned int n)
s++; s++;
} else { } else {
a = s; a = s;
while (*s while (*s && (isalnum((int) *s)
&& (isalnum((int) *s) || *s == '_'))
|| *s == '_'))
s++; s++;
} }
@ -197,7 +196,8 @@ void update_stuff()
update_top(); update_top();
#ifdef MLDONKEY #ifdef MLDONKEY
if (NEED(INFO_MLDONKEY)) get_mldonkey_status(&mlconfig, &mlinfo); if (NEED(INFO_MLDONKEY))
get_mldonkey_status(&mlconfig, &mlinfo);
#endif #endif
#ifdef SETI #ifdef SETI
@ -214,10 +214,12 @@ void update_stuff()
update_load_average(); update_load_average();
#ifdef METAR #ifdef METAR
if (NEED(INFO_METAR) && current_update_time - last_metar_update > 1200.9 && info.looped) { if (NEED(INFO_METAR)
update_metar(); && current_update_time - last_metar_update > 1200.9
last_metar_update = current_update_time; && info.looped) {
} update_metar();
last_metar_update = current_update_time;
}
#endif #endif

2951
conky.c

File diff suppressed because it is too large Load Diff

26
conky.h
View File

@ -72,6 +72,8 @@ struct mpd_s {
char host[128]; char host[128];
float progress; float progress;
int bitrate; int bitrate;
int length;
int elapsed;
}; };
#endif #endif
@ -98,12 +100,12 @@ enum {
#endif #endif
INFO_TOP = 16, INFO_TOP = 16,
#ifdef METAR #ifdef METAR
INFO_METAR = 17, INFO_METAR = 17,
#endif #endif
#ifdef MLDONKEY #ifdef MLDONKEY
INFO_MLDONKEY = 18, INFO_MLDONKEY = 18,
#endif #endif
INFO_WIFI = 19, INFO_WIFI = 19,
}; };
@ -355,15 +357,15 @@ typedef struct mldonkey_config {
/* The MLDonkey status returned */ /* The MLDonkey status returned */
typedef struct mldonkey_info { typedef struct mldonkey_info {
int64 upload_counter; int64 upload_counter;
int64 download_counter; int64 download_counter;
int nshared_files; int nshared_files;
int64 shared_counter; int64 shared_counter;
int tcp_upload_rate; int tcp_upload_rate;
int tcp_download_rate; int tcp_download_rate;
int udp_upload_rate; int udp_upload_rate;
int udp_download_rate; int udp_download_rate;
int ndownloaded_files; int ndownloaded_files;
int ndownloading_files; int ndownloading_files;
int nconnected_networks; int nconnected_networks;
int connected_networks[1]; int connected_networks[1];
@ -372,7 +374,7 @@ typedef struct mldonkey_info {
extern mldonkey_info mlinfo; extern mldonkey_info mlinfo;
extern mldonkey_config mlconfig; extern mldonkey_config mlconfig;
int get_mldonkey_status(mldonkey_config *config, mldonkey_info *info); int get_mldonkey_status(mldonkey_config * config, mldonkey_info * info);
#endif #endif
@ -385,4 +387,10 @@ extern int show_nice_processes;
extern int do_it(void); extern int do_it(void);
/* struct for graphs */
typedef struct graph {
unsigned int *value;
} graph;
#endif #endif

View File

@ -26,22 +26,23 @@
#if defined(i386) || defined(__i386__) #if defined(i386) || defined(__i386__)
static unsigned int get_timer(); static unsigned int get_timer();
static unsigned int get_cpu_speed(void); static unsigned int get_cpu_speed(void);
static inline unsigned long long int rdtsc( void ); static inline unsigned long long int rdtsc(void);
/* cpu frequency detection code based on mplayer's one */ /* cpu frequency detection code based on mplayer's one */
static unsigned int get_timer() { static unsigned int get_timer()
{
struct timeval tv; struct timeval tv;
struct timezone tz; struct timezone tz;
gettimeofday(&tv,&tz); gettimeofday(&tv, &tz);
return (tv.tv_sec*1000000+tv.tv_usec); return (tv.tv_sec * 1000000 + tv.tv_usec);
} }
static inline unsigned long long int rdtsc( void ) static inline unsigned long long int rdtsc(void)
{ {
unsigned long long int retval; unsigned long long int retval;
__asm __volatile ("rdtsc":"=A"(retval)::"memory"); __asm __volatile("rdtsc":"=A"(retval)::"memory");
return retval; return retval;
} }
@ -56,7 +57,7 @@ static unsigned int get_cpu_speed(void)
stop = get_timer(); stop = get_timer();
tscstop = rdtsc(); tscstop = rdtsc();
return((tscstop-tscstart)/((stop-start)/1000.0)); return ((tscstop - tscstart) / ((stop - start) / 1000.0));
} }
#endif #endif
@ -410,29 +411,33 @@ char *get_acpi_fan()
return ""; return "";
} }
char* get_adt746x_cpu() { char *get_adt746x_cpu()
{
return ""; return "";
} }
char* get_adt746x_fan() { char *get_adt746x_fan()
{
return ""; return "";
} }
char* get_freq() { char *get_freq()
{
#if defined(i386) || defined(__i386__) #if defined(i386) || defined(__i386__)
int i; int i;
char *cpuspeed; char *cpuspeed;
if ((cpuspeed = (char *)malloc(16)) == NULL) if ((cpuspeed = (char *) malloc(16)) == NULL)
exit(1); exit(1);
i = 0; i = 0;
if ((i = get_cpu_speed()) > 0) { if ((i = get_cpu_speed()) > 0) {
if (i < 1000000) { if (i < 1000000) {
i += 50; /* for rounding */ i += 50; /* for rounding */
snprintf(cpuspeed, 15, "%d.%d MHz", i/1000, (i/100)%10); snprintf(cpuspeed, 15, "%d.%d MHz", i / 1000,
(i / 100) % 10);
} else { } else {
snprintf(cpuspeed, 15, "%d MHz", i/1000); snprintf(cpuspeed, 15, "%d MHz", i / 1000);
} }
} else { } else {
cpuspeed = ""; cpuspeed = "";

1423
ftp.c

File diff suppressed because it is too large Load Diff

12
ftp.h
View File

@ -8,13 +8,13 @@
#ifndef __MIRRORS_FTP_H__ #ifndef __MIRRORS_FTP_H__
#define __MIRRORS_FTP_H__ #define __MIRRORS_FTP_H__
typedef void (*ftpListCallback) (void *userData, typedef void (*ftpListCallback) (void *userData,
const char *filename, const char* attrib, const char *filename, const char *attrib,
const char *owner, const char *group, const char *owner, const char *group,
unsigned long size, int links, int year, unsigned long size, int links, int year,
const char *month, int day, int minute); const char *month, int day, int minute);
typedef void (*ftpDataCallback) (void *userData, typedef void (*ftpDataCallback) (void *userData,
const char *data, int len); const char *data, int len);
extern void initFtp(void); extern void initFtp(void);
@ -23,4 +23,4 @@ extern int changeFtpDirectory(char *directory);
extern int disconnectFtp(void); extern int disconnectFtp(void);
int getFtp(ftpDataCallback, void *, const char *); int getFtp(ftpDataCallback, void *, const char *);
#endif /* __MIRRORS_FTP_H__ */ #endif /* __MIRRORS_FTP_H__ */

154
linux.c
View File

@ -41,7 +41,7 @@ static void update_sysinfo()
/* there was some problem with these */ /* there was some problem with these */
#if 0 #if 0
// info.loadavg[0] = s_info.loads[0] / 100000.0f; // info.loadavg[0] = s_info.loads[0] / 100000.0f;
info.loadavg[1] = s_info.loads[1] / 100000.0f; info.loadavg[1] = s_info.loads[1] / 100000.0f;
info.loadavg[2] = s_info.loads[2] / 100000.0f; info.loadavg[2] = s_info.loads[2] / 100000.0f;
gkrelltop_process_find_top_three info.mask |= 1 << INFO_LOADAVG; gkrelltop_process_find_top_three info.mask |= 1 << INFO_LOADAVG;
@ -64,7 +64,7 @@ static void update_sysinfo()
void update_uptime() void update_uptime()
{ {
/* prefers sysinfo() for uptime, I don't really know which one is better /* prefers sysinfo() for uptime, I don't really know which one is better
* (=faster?) */ * (=faster?) */
#ifdef USE_PROC_UPTIME #ifdef USE_PROC_UPTIME
static int rep; static int rep;
FILE *fp = open_file("/proc/uptime", &rep); FILE *fp = open_file("/proc/uptime", &rep);
@ -91,7 +91,7 @@ void update_meminfo()
char buf[256]; char buf[256];
info.mem = info.memmax = info.swap = info.swapmax = info.bufmem = info.mem = info.memmax = info.swap = info.swapmax = info.bufmem =
info.buffers = info.cached = 0; info.buffers = info.cached = 0;
if (meminfo_fp == NULL) if (meminfo_fp == NULL)
meminfo_fp = open_file("/proc/meminfo", &rep); meminfo_fp = open_file("/proc/meminfo", &rep);
@ -194,16 +194,16 @@ inline void update_net_stats()
if (r < ns->last_read_recv) if (r < ns->last_read_recv)
ns->recv += ns->recv +=
((long long) 4294967295U - ((long long) 4294967295U -
ns->last_read_recv) + r; ns->last_read_recv) + r;
else else
ns->recv += (r - ns->last_read_recv); ns->recv += (r - ns->last_read_recv);
ns->last_read_recv = r; ns->last_read_recv = r;
if (t < ns->last_read_trans) if (t < ns->last_read_trans)
ns->trans += ns->trans +=
((long long) 4294967295U - ((long long) 4294967295U -
ns->last_read_trans) + t; ns->last_read_trans) + t;
else else
ns->trans += (t - ns->last_read_trans); ns->trans += (t - ns->last_read_trans);
ns->last_read_trans = t; ns->last_read_trans = t;
@ -215,15 +215,18 @@ inline void update_net_stats()
conf.ifc_len = sizeof(struct ifreq) * 16; conf.ifc_len = sizeof(struct ifreq) * 16;
ioctl((long)i, SIOCGIFCONF, &conf); ioctl((long) i, SIOCGIFCONF, &conf);
for (k=0; k < conf.ifc_len / sizeof(struct ifreq); k++) { for (k = 0; k < conf.ifc_len / sizeof(struct ifreq); k++) {
struct net_stat *ns; struct net_stat *ns;
ns = get_net_stat(((struct ifreq*)conf.ifc_buf)[k].ifr_ifrn.ifrn_name); ns = get_net_stat(((struct ifreq *) conf.
ns->addr = ((struct ifreq*)conf.ifc_buf)[k].ifr_ifru.ifru_addr; ifc_buf)[k].ifr_ifrn.ifrn_name);
ns->addr =
((struct ifreq *) conf.ifc_buf)[k].ifr_ifru.
ifru_addr;
} }
close((long)i); close((long) i);
free(conf.ifc_buf); free(conf.ifc_buf);
@ -247,7 +250,7 @@ inline void update_net_stats()
for (i = info.net_avg_samples; i > 1; i--) { for (i = info.net_avg_samples; i > 1; i--) {
ns->net_rec[i - 1] = ns->net_rec[i - 2]; ns->net_rec[i - 1] = ns->net_rec[i - 2];
ns->net_trans[i - 1] = ns->net_trans[i - 1] =
ns->net_trans[i - 2]; ns->net_trans[i - 2];
} }
} }
@ -258,45 +261,49 @@ inline void update_net_stats()
/* fclose(net_dev_fp); net_dev_fp = NULL; */ /* fclose(net_dev_fp); net_dev_fp = NULL; */
} }
inline void update_wifi_stats() { inline void update_wifi_stats()
{
/** wireless stats patch by Bobby Beckmann **/ /** wireless stats patch by Bobby Beckmann **/
static int rep; static int rep;
int i; int i;
char buf[256]; char buf[256];
/*open file and ignore first two lines sorry, this code sucks ass right now, i'll clean it up later*/ /*open file and ignore first two lines sorry, this code sucks ass right now, i'll clean it up later */
if (net_wireless_fp == NULL) if (net_wireless_fp == NULL)
net_wireless_fp = open_file("/proc/net/wireless", &rep); net_wireless_fp = open_file("/proc/net/wireless", &rep);
else else
fseek(net_wireless_fp, 0, SEEK_SET); fseek(net_wireless_fp, 0, SEEK_SET);
if (!net_wireless_fp) return; if (!net_wireless_fp)
return;
fgets(buf, 255, net_wireless_fp); /* garbage */ fgets(buf, 255, net_wireless_fp); /* garbage */
fgets(buf, 255, net_wireless_fp); /* garbage (field names) */ fgets(buf, 255, net_wireless_fp); /* garbage (field names) */
/* read each interface */ /* read each interface */
for (i=0; i<16; i++) { for (i = 0; i < 16; i++) {
struct net_stat *ns; struct net_stat *ns;
char *s, *p; char *s, *p;
int l, m, n; int l, m, n;
if (fgets(buf, 255, net_wireless_fp) == NULL) break; if (fgets(buf, 255, net_wireless_fp) == NULL)
break;
p = buf; p = buf;
while (isspace((int) *p)) p++; while (isspace((int) *p))
p++;
s = p; s = p;
while (*p && *p != ':') p++; while (*p && *p != ':')
if (*p == '\0') continue; p++;
if (*p == '\0')
continue;
*p = '\0'; *p = '\0';
p++; p++;
ns = get_net_stat(s); ns = get_net_stat(s);
sscanf(p, sscanf(p, "%*d %d. %d. %d", &l, &m, &n);
"%*d %d. %d. %d",
&l, &m, &n);
ns->linkstatus = (int)(log(l) / log(92) * 100); ns->linkstatus = (int) (log(l) / log(92) * 100);
} }
/*** end wireless patch ***/ /*** end wireless patch ***/
@ -359,9 +366,9 @@ inline static void update_stat()
clock_ticks = sysconf(_SC_CLK_TCK); clock_ticks = sysconf(_SC_CLK_TCK);
curtmp = 0; curtmp = 0;
cpu_val[0] = cpu_val[0] =
(cpu_user + cpu_nice + cpu_system - (cpu_user + cpu_nice + cpu_system -
last_cpu_sum) / delta / (double) clock_ticks / last_cpu_sum) / delta / (double) clock_ticks /
info.cpu_count; info.cpu_count;
for (i = 0; i < info.cpu_avg_samples; i++) for (i = 0; i < info.cpu_avg_samples; i++)
curtmp += cpu_val[i]; curtmp += cpu_val[i];
info.cpu_usage = curtmp / info.cpu_avg_samples; info.cpu_usage = curtmp / info.cpu_avg_samples;
@ -428,7 +435,7 @@ static int no_dots(const struct dirent *d)
} }
static int static int
get_first_file_in_a_directory(const char *dir, char *s, int *rep) get_first_file_in_a_directory(const char *dir, char *s, int *rep)
{ {
struct dirent **namelist; struct dirent **namelist;
int i, n; int i, n;
@ -480,9 +487,9 @@ open_i2c_sensor(const char *dev, const char *type, int n, int *div,
type = "in"; type = "in";
if (strcmp(type, "tempf") == 0) { if (strcmp(type, "tempf") == 0) {
snprintf(path, 255, I2C_DIR "%s/%s%d_input", dev, "temp", n); snprintf(path, 255, I2C_DIR "%s/%s%d_input", dev, "temp",
} n);
else { } else {
snprintf(path, 255, I2C_DIR "%s/%s%d_input", dev, type, n); snprintf(path, 255, I2C_DIR "%s/%s%d_input", dev, type, n);
} }
strcpy(devtype, path); strcpy(devtype, path);
@ -492,7 +499,8 @@ open_i2c_sensor(const char *dev, const char *type, int n, int *div,
if (fd < 0) if (fd < 0)
ERR("can't open '%s': %s", path, strerror(errno)); ERR("can't open '%s': %s", path, strerror(errno));
if (strcmp(type, "in") == 0 || strcmp(type, "temp") == 0 || strcmp(type, "tempf") == 0) if (strcmp(type, "in") == 0 || strcmp(type, "temp") == 0
|| strcmp(type, "tempf") == 0)
*div = 1; *div = 1;
else else
*div = 0; *div = 0;
@ -502,9 +510,9 @@ open_i2c_sensor(const char *dev, const char *type, int n, int *div,
/* test if *_div file exist, open it and use it as divisor */ /* test if *_div file exist, open it and use it as divisor */
if (strcmp(type, "tempf") == 0) { if (strcmp(type, "tempf") == 0) {
snprintf(path, 255, I2C_DIR "%s/%s%d_div", "one", "two", n); snprintf(path, 255, I2C_DIR "%s/%s%d_div", "one", "two",
} n);
else { } else {
snprintf(path, 255, I2C_DIR "%s/%s%d_div", dev, type, n); snprintf(path, 255, I2C_DIR "%s/%s%d_div", dev, type, n);
} }
@ -515,7 +523,7 @@ open_i2c_sensor(const char *dev, const char *type, int n, int *div,
unsigned int divn; unsigned int divn;
divn = read(divfd, divbuf, 63); divn = read(divfd, divbuf, 63);
/* should read until n == 0 but I doubt that kernel will give these /* should read until n == 0 but I doubt that kernel will give these
* in multiple pieces. :) */ * in multiple pieces. :) */
divbuf[divn] = '\0'; divbuf[divn] = '\0';
*div = atoi(divbuf); *div = atoi(divbuf);
} }
@ -540,7 +548,7 @@ double get_i2c_info(int *fd, int div, char *devtype, char *type)
unsigned int n; unsigned int n;
n = read(*fd, buf, 63); n = read(*fd, buf, 63);
/* should read until n == 0 but I doubt that kernel will give these /* should read until n == 0 but I doubt that kernel will give these
* in multiple pieces. :) */ * in multiple pieces. :) */
buf[n] = '\0'; buf[n] = '\0';
val = atoi(buf); val = atoi(buf);
} }
@ -552,8 +560,8 @@ double get_i2c_info(int *fd, int div, char *devtype, char *type)
ERR("can't open '%s': %s", devtype, strerror(errno)); ERR("can't open '%s': %s", devtype, strerror(errno));
/* My dirty hack for computing CPU value /* My dirty hack for computing CPU value
* Filedil, from forums.gentoo.org * Filedil, from forums.gentoo.org
*/ */
/* if (strstr(devtype, "temp1_input") != NULL) /* if (strstr(devtype, "temp1_input") != NULL)
return -15.096+1.4893*(val / 1000.0); */ return -15.096+1.4893*(val / 1000.0); */
@ -562,13 +570,12 @@ double get_i2c_info(int *fd, int div, char *devtype, char *type)
/* or if any other divisor is given, use that */ /* or if any other divisor is given, use that */
if (strcmp(type, "tempf") == 0) { if (strcmp(type, "tempf") == 0) {
if (div > 1) if (div > 1)
return ((val / div + 40)*9.0/5)-40; return ((val / div + 40) * 9.0 / 5) - 40;
else if (div) else if (div)
return ((val / 1000.0 + 40)*9.0/5)-40; return ((val / 1000.0 + 40) * 9.0 / 5) - 40;
else else
return ((val + 40)*9.0/5)-40; return ((val + 40) * 9.0 / 5) - 40;
} } else {
else {
if (div > 1) if (div > 1)
return val / div; return val / div;
else if (div) else if (div)
@ -643,7 +650,7 @@ char *get_adt746x_cpu()
__inline__ unsigned long long int rdtsc() __inline__ unsigned long long int rdtsc()
{ {
unsigned long long int x; unsigned long long int x;
__asm__ volatile (".byte 0x0f, 0x31" : "=A" (x)); __asm__ volatile (".byte 0x0f, 0x31":"=A" (x));
return x; return x;
} }
@ -655,8 +662,8 @@ char *get_freq()
buffer = malloc(64); buffer = malloc(64);
struct timezone tz; struct timezone tz;
struct timeval tvstart, tvstop; struct timeval tvstart, tvstop;
unsigned long long cycles[2]; /* gotta be 64 bit */ unsigned long long cycles[2]; /* gotta be 64 bit */
unsigned int microseconds; /* total time taken */ unsigned int microseconds; /* total time taken */
memset(&tz, 0, sizeof(tz)); memset(&tz, 0, sizeof(tz));
@ -670,11 +677,10 @@ char *get_freq()
cycles[1] = rdtsc(); cycles[1] = rdtsc();
gettimeofday(&tvstop, &tz); gettimeofday(&tvstop, &tz);
microseconds = ((tvstop.tv_sec-tvstart.tv_sec)*1000000) + microseconds = ((tvstop.tv_sec - tvstart.tv_sec) * 1000000) +
(tvstop.tv_usec-tvstart.tv_usec); (tvstop.tv_usec - tvstart.tv_usec);
sprintf(buffer, "%lldMHz", sprintf(buffer, "%lldMHz", (cycles[1] - cycles[0]) / microseconds);
(cycles[1]-cycles[0])/microseconds);
return buffer; return buffer;
} }
@ -772,7 +778,7 @@ int open_acpi_temperature(const char *name)
if (name == NULL || strcmp(name, "*") == 0) { if (name == NULL || strcmp(name, "*") == 0) {
static int rep; static int rep;
if (!get_first_file_in_a_directory if (!get_first_file_in_a_directory
(ACPI_THERMAL_DIR, buf, &rep)) (ACPI_THERMAL_DIR, buf, &rep))
return -1; return -1;
name = buf; name = buf;
} }
@ -910,8 +916,8 @@ void get_battery_stuff(char *buf, unsigned int n, const char *bat)
break; break;
if (sscanf if (sscanf
(b, "last full capacity: %d", (b, "last full capacity: %d",
&acpi_last_full) != 0) &acpi_last_full) != 0)
break; break;
} }
@ -947,45 +953,47 @@ void get_battery_stuff(char *buf, unsigned int n, const char *bat)
format_seconds(last_battery_str + 9, format_seconds(last_battery_str + 9,
63 - 9, 63 - 9,
(acpi_last_full - (acpi_last_full -
remaining_capacity) * 60 * remaining_capacity) * 60 *
60 / present_rate); 60 / present_rate);
} else if (acpi_last_full != 0 } else if (acpi_last_full != 0
&& present_rate <= 0) { && present_rate <= 0) {
sprintf(last_battery_str, "charging %d%%", sprintf(last_battery_str, "charging %d%%",
remaining_capacity * 100 / remaining_capacity * 100 /
acpi_last_full); acpi_last_full);
} else { } else {
strcpy(last_battery_str, "charging"); strcpy(last_battery_str, "charging");
} }
} }
/* discharging */ /* discharging */
else if (strcmp(charging_state, "discharging") == 0) { else if (strcmp(charging_state, "discharging") == 0) {
if (present_rate > 0) if (present_rate > 0)
format_seconds(last_battery_str, 63, format_seconds(last_battery_str, 63,
(remaining_capacity * 60 * (remaining_capacity * 60 *
60) / present_rate); 60) / present_rate);
else else
sprintf(last_battery_str, sprintf(last_battery_str,
"discharging %d%%", "discharging %d%%",
remaining_capacity * 100 / remaining_capacity * 100 /
acpi_last_full); acpi_last_full);
} }
/* charged */ /* charged */
/* thanks to Lukas Zapletal <lzap@seznam.cz> */ /* thanks to Lukas Zapletal <lzap@seznam.cz> */
else if (strcmp(charging_state, "charged") == 0) { else if (strcmp(charging_state, "charged") == 0) {
if (acpi_last_full != 0 && remaining_capacity != acpi_last_full) if (acpi_last_full != 0
&& remaining_capacity != acpi_last_full)
sprintf(last_battery_str, "charged %d%%", sprintf(last_battery_str, "charged %d%%",
remaining_capacity * 100 / acpi_last_full); remaining_capacity * 100 /
acpi_last_full);
else else
strcpy(last_battery_str, "charged"); strcpy(last_battery_str, "charged");
} }
/* unknown, probably full / AC */ /* unknown, probably full / AC */
else { else {
if (acpi_last_full != 0 if (acpi_last_full != 0
&& remaining_capacity != acpi_last_full) && remaining_capacity != acpi_last_full)
sprintf(last_battery_str, "unknown %d%%", sprintf(last_battery_str, "unknown %d%%",
remaining_capacity * 100 / remaining_capacity * 100 /
acpi_last_full); acpi_last_full);
else else
strcpy(last_battery_str, "AC"); strcpy(last_battery_str, "AC");
} }
@ -1013,7 +1021,7 @@ void get_battery_stuff(char *buf, unsigned int n, const char *bat)
} }
/* it seemed to buffer it so file must be closed (or could use syscalls /* it seemed to buffer it so file must be closed (or could use syscalls
* directly but I don't feel like coding it now) */ * directly but I don't feel like coding it now) */
fclose(apm_bat_fp); fclose(apm_bat_fp);
apm_bat_fp = NULL; apm_bat_fp = NULL;
} }

View File

@ -10,58 +10,67 @@
static int status = 0; static int status = 0;
int calculateRelativeHumidity(int temp, int dew) { int calculateRelativeHumidity(int temp, int dew)
{
float rh = 0.0; float rh = 0.0;
rh = 100.0 * powf ((112 - (0.1 * temp)+dew ) / (112 + (0.9 * temp)), 8); rh = 100.0 * powf((112 - (0.1 * temp) + dew) /
return (int)rh; (112 + (0.9 * temp)), 8);
return (int) rh;
} }
int calculateWindChill(int temperatureC, int windKn) { int calculateWindChill(int temperatureC, int windKn)
{
double windKmh = knTokph(windKn); double windKmh = knTokph(windKn);
return (int)(13.112+0.6215*temperatureC - 11.37*powf(windKmh,.16) + 0.3965*temperatureC*powf(windKmh,.16) ); return (int) (13.112 + 0.6215 * temperatureC -
11.37 * powf(windKmh,
.16) +
0.3965 * temperatureC * powf(windKmh, .16));
} }
int knTokph(int knSpeed ){ int knTokph(int knSpeed)
{
return (knSpeed * 1.852); return (knSpeed * 1.852);
} }
const char *calculateWindDirectionString(int degree) { const char *calculateWindDirectionString(int degree)
{
if (degree < 22.5) { if (degree < 22.5) {
return "North"; return "North";
} else if (degree < 67.5){ } else if (degree < 67.5) {
return "Northeast"; return "Northeast";
} else if (degree < 112.5) { } else if (degree < 112.5) {
return "East"; return "East";
} else if (degree < 157.5) { } else if (degree < 157.5) {
return "Southeast"; return "Southeast";
} else if (degree < 202.5){ } else if (degree < 202.5) {
return "South"; return "South";
} else if (degree < 247.5) { } else if (degree < 247.5) {
return "Southwest"; return "Southwest";
} else if (degree < 292.5) { } else if (degree < 292.5) {
return "West"; return "West";
} else if (degree < 337.5){ } else if (degree < 337.5) {
return "Northwest"; return "Northwest";
} else { } else {
return "North"; return "North";
} }
} }
const char*calculateShortWindDirectionString(int degree){ const char *calculateShortWindDirectionString(int degree)
{
if (degree < 22.5) { if (degree < 22.5) {
return "N"; return "N";
} else if (degree < 67.5){ } else if (degree < 67.5) {
return "NE"; return "NE";
} else if (degree < 112.5) { } else if (degree < 112.5) {
return "E"; return "E";
} else if (degree < 157.5) { } else if (degree < 157.5) {
return "SE"; return "SE";
} else if (degree < 202.5){ } else if (degree < 202.5) {
return "S"; return "S";
} else if (degree < 247.5) { } else if (degree < 247.5) {
return "SW"; return "SW";
} else if (degree < 292.5) { } else if (degree < 292.5) {
return "W"; return "W";
} else if (degree < 337.5){ } else if (degree < 337.5) {
return "NW"; return "NW";
} else { } else {
return "N"; return "N";
@ -70,35 +79,38 @@ const char*calculateShortWindDirectionString(int degree){
void ftpData(void *userData, const char *data, int len) void ftpData(void *userData, const char *data, int len)
{ {
if(userData) {} // do nothing to make stupid warning go away if (userData) {
} // do nothing to make stupid warning go away
if (len <= 0) if (len <= 0)
return; return;
if ( data != NULL ){ if (data != NULL) {
line = strdup(data); line = strdup(data);
} }
} }
extern unsigned int sleep(unsigned int); extern unsigned int sleep(unsigned int);
void *fetch_ftp( ) { void *fetch_ftp()
// try three times if it fails {
// try three times if it fails
int tries = 0; int tries = 0;
do { do {
if (tries > 0) { if (tries > 0) {
/* this happens too often, so i'll leave it commented fprintf(stderr, "METAR update failed for some reason, retry %i\n", tries); */ /* this happens too often, so i'll leave it commented fprintf(stderr, "METAR update failed for some reason, retry %i\n", tries); */
sleep(15); /* give it some time in case the server is busy or down */ sleep(15); /* give it some time in case the server is busy or down */
} }
tries++; tries++;
if ( strlen(metar_station) != 8 ){ if (strlen(metar_station) != 8) {
ERR("You didn't supply a valid METAR station code\n"); ERR("You didn't supply a valid METAR station code\n");
return NULL; return NULL;
} }
if (metar_server == NULL) if (metar_server == NULL)
metar_server = strdup("weather.noaa.gov"); metar_server = strdup("weather.noaa.gov");
if (metar_path == NULL) if (metar_path == NULL)
metar_path = strdup("/data/observations/metar/stations"); metar_path =
strdup("/data/observations/metar/stations");
int res; int res;
initFtp(); initFtp();
@ -110,7 +122,8 @@ void *fetch_ftp( ) {
} }
res = changeFtpDirectory(metar_path); res = changeFtpDirectory(metar_path);
if (res < 0) { if (res < 0) {
ERR("Metar update failed (couldn't CWD to %s)\n", metar_path); ERR("Metar update failed (couldn't CWD to %s)\n",
metar_path);
disconnectFtp(); disconnectFtp();
status = 1; status = 1;
return NULL; return NULL;
@ -125,14 +138,13 @@ void *fetch_ftp( ) {
} }
disconnectFtp(); disconnectFtp();
if ( line != NULL ){ if (line != NULL) {
dcdNetMETAR(line, &data); dcdNetMETAR(line, &data);
free(line); free(line);
line = NULL; line = NULL;
ftp_ok = 1; ftp_ok = 1;
metar_worked = 1; metar_worked = 1;
} } else {
else {
ftp_ok = 0; ftp_ok = 0;
} }
@ -143,19 +155,18 @@ void *fetch_ftp( ) {
static pthread_t thread1; static pthread_t thread1;
void update_metar() { void update_metar()
int iret1; {
int iret1;
if (!status) { if (!status) {
status = 2; status = 2;
iret1 = pthread_create( &thread1, NULL, fetch_ftp, NULL); iret1 = pthread_create(&thread1, NULL, fetch_ftp, NULL);
} } else if (status == 2) { /* thread is still running. what else can we do? */
else if (status == 2) { /* thread is still running. what else can we do? */
return; return;
} } else {
else { pthread_join(thread1, NULL);
pthread_join( thread1, NULL);
status = 2; status = 2;
iret1 = pthread_create( &thread1, NULL, fetch_ftp, NULL); iret1 = pthread_create(&thread1, NULL, fetch_ftp, NULL);
} }
} }

View File

@ -8,11 +8,11 @@ int knTokph(int);
const char *calculateWindDirectionString(int); const char *calculateWindDirectionString(int);
const char *calculateShortWindDirectionString(int); const char *calculateShortWindDirectionString(int);
char *line = NULL; char *line = NULL;
char *metar_station = NULL; char *metar_station = NULL;
char *metar_server = NULL; char *metar_server = NULL;
char *metar_path = NULL; char *metar_path = NULL;
char ftp_ok = 0; char ftp_ok = 0;
char metar_worked = 0; char metar_worked = 0;
Decoded_METAR data; Decoded_METAR data;

View File

@ -1,10 +1,11 @@
#include "conky.h"
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <errno.h>
#include <netdb.h> #include <netdb.h>
#include <fcntl.h> #include <fcntl.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include "conky.h"
int64 buf_to_int(char *buf, int pos, int size); int64 buf_to_int(char *buf, int pos, int size);
void int_to_buf(int64 i, char *buf, int pos, int size); void int_to_buf(int64 i, char *buf, int pos, int size);
@ -28,56 +29,56 @@ mldonkey_config mlconfig;
* -1 otherwise. */ * -1 otherwise. */
enum to_gui { enum to_gui {
CoreProtocol, /* 0 */ CoreProtocol, /* 0 */
Options_info, Options_info,
RESERVED2, RESERVED2,
DefineSearches, DefineSearches,
Result_info, Result_info,
Search_result, Search_result,
Search_waiting, Search_waiting,
File_info, File_info,
File_downloaded, File_downloaded,
File_availability, File_availability,
File_source, /* 10 */ File_source, /* 10 */
Server_busy, Server_busy,
Server_user, Server_user,
Server_state, Server_state,
Server_info, Server_info,
Client_info, Client_info,
Client_state, Client_state,
Client_friend, Client_friend,
Client_file, Client_file,
Console, Console,
Network_info, /* 20 */ Network_info, /* 20 */
User_info, User_info,
Room_info, Room_info,
Room_message, Room_message,
Room_add_user, Room_add_user,
Client_stats, Client_stats,
Server_info_v2, Server_info_v2,
MessageFromClient, MessageFromClient,
ConnectedServers, ConnectedServers,
DownloadFiles, DownloadFiles,
DownloadedFiles, /* 30 */ DownloadedFiles, /* 30 */
Room_info_v2, Room_info_v2,
Room_remove_user, Room_remove_user,
Shared_file_info, Shared_file_info,
Shared_file_upload, Shared_file_upload,
Shared_file_unshared, Shared_file_unshared,
Add_section_option, Add_section_option,
Client_stats_v2, Client_stats_v2,
Add_plugin_option, Add_plugin_option,
Client_stats_v3, Client_stats_v3,
File_info_v2, /* 40 */ File_info_v2, /* 40 */
DownloadFiles_v2, DownloadFiles_v2,
DownloadedFiles_v2, DownloadedFiles_v2,
File_info_v3, File_info_v3,
DownloadFiles_v3, DownloadFiles_v3,
DownloadedFiles_v3, DownloadedFiles_v3,
File_downloaded_v2, File_downloaded_v2,
BadPassword, BadPassword,
Shared_file_info_v2, Shared_file_info_v2,
Client_stats_v4, /* 49 */ Client_stats_v4, /* 49 */
}; };
#define MLDONKEY_DISCONNECTED 0 #define MLDONKEY_DISCONNECTED 0
@ -98,89 +99,93 @@ static mldonkey_config *old_config = NULL;
int64 buf_to_int(char *buf, int pos, int size) int64 buf_to_int(char *buf, int pos, int size)
{ {
int i; int i;
int64 res = 0; int64 res = 0;
for(i = 0; i < size; i++){ for (i = 0; i < size; i++) {
res += (buf[pos + i] & 0xFF) << (8 * i); res += (buf[pos + i] & 0xFF) << (8 * i);
} }
return res; return res;
} }
void int_to_buf(int64 i, char *buf, int pos, int size) void int_to_buf(int64 i, char *buf, int pos, int size)
{ {
int j; int j;
for(j = 0; j < size; j++){ for (j = 0; j < size; j++) {
buf[pos + j] = (i & (-1)) >> (8 * j); buf[pos + j] = (i & (-1)) >> (8 * j);
} }
} }
/* Write operations --------------------- */ /* Write operations --------------------- */
void init_message() void init_message()
{ write_pos = 0; } {
write_pos = 0;
}
void write_int8(int code) void write_int8(int code)
{ {
write_buf[write_pos++] = code; write_buf[write_pos++] = code;
} }
void write_opcode(int code) void write_opcode(int code)
{ {
write_buf[write_pos++] = code; write_buf[write_pos++] = code;
} }
void write_int16(int code) void write_int16(int code)
{ {
INT_TO_BUF16(code, write_buf, write_pos); INT_TO_BUF16(code, write_buf, write_pos);
write_pos += 2; write_pos += 2;
} }
void write_int32(int code) void write_int32(int code)
{ {
INT_TO_BUF32(code, write_buf, write_pos); INT_TO_BUF32(code, write_buf, write_pos);
write_pos += 4; write_pos += 4;
} }
void write_int64(int64 code) void write_int64(int64 code)
{ {
INT_TO_BUF64(code, write_buf, write_pos); INT_TO_BUF64(code, write_buf, write_pos);
write_pos += 8; write_pos += 8;
} }
void write_string(char *str) void write_string(char *str)
{ {
if(str == NULL){ if (str == NULL) {
write_int16(0); write_int16(0);
} else { } else {
int len = strlen(str); int len = strlen(str);
write_int16(len); write_int16(len);
memcpy((void *) (write_buf + write_pos), (void *) str, (size_t) len); memcpy((void *) (write_buf + write_pos), (void *) str,
write_pos += len; (size_t) len);
} write_pos += len;
}
} }
int write_message(char *mtype) int write_message(char *mtype)
{ {
char header[4]; char header[4];
INT_TO_BUF32(write_pos, header, 0); INT_TO_BUF32(write_pos, header, 0);
if(4 != write(mldonkey_sock, header, 4) || if (4 != write(mldonkey_sock, header, 4) ||
write_pos != write(mldonkey_sock,(void *)write_buf,(size_t)write_pos)){ write_pos != write(mldonkey_sock, (void *) write_buf,
ERR("Error in transmitting %s\n",mtype); (size_t) write_pos)) {
write_pos = 0; ERR("Error in transmitting %s\n", mtype);
write_pos = 0;
/* Immediatly close the connection */ /* Immediatly close the connection */
close(mldonkey_sock); close(mldonkey_sock);
mldonkey_state = MLDONKEY_DISCONNECTED; mldonkey_state = MLDONKEY_DISCONNECTED;
mldonkey_sock = -1; mldonkey_sock = -1;
return -1; return -1;
} else { } else {
write_pos = 0; write_pos = 0;
return 0; return 0;
} }
} }
@ -188,44 +193,44 @@ int write_message(char *mtype)
int read_int8() int read_int8()
{ {
return read_buf[read_pos++]; return read_buf[read_pos++];
} }
int read_int16() int read_int16()
{ {
int i = BUF16_TO_INT(read_buf, read_pos); int i = BUF16_TO_INT(read_buf, read_pos);
read_pos += 2; read_pos += 2;
return i; return i;
} }
int read_int32() int read_int32()
{ {
int i = BUF32_TO_INT(read_buf, read_pos); int i = BUF32_TO_INT(read_buf, read_pos);
read_pos += 4; read_pos += 4;
return i; return i;
} }
int64 read_int64() int64 read_int64()
{ {
int64 i = BUF64_TO_INT(read_buf, read_pos); int64 i = BUF64_TO_INT(read_buf, read_pos);
read_pos += 8; read_pos += 8;
return i; return i;
} }
char* read_string() char *read_string()
{ {
char *buf; char *buf;
int len; int len;
len = BUF16_TO_INT(read_buf, read_pos); len = BUF16_TO_INT(read_buf, read_pos);
read_pos += 2; read_pos += 2;
buf = (char *) malloc((size_t) len+1); buf = (char *) malloc((size_t) len + 1);
memmove(read_buf + read_pos, buf, len); memmove(read_buf + read_pos, buf, len);
buf[len] = 0; buf[len] = 0;
read_pos += len; read_pos += len;
return buf; return buf;
} }
/* protocol impl. ----------------------------- */ /* protocol impl. ----------------------------- */
@ -236,197 +241,216 @@ void close_sock();
-1 on error. */ -1 on error. */
int cut_messages(int reinit) int cut_messages(int reinit)
{ {
int nread; int nread;
static int toread = 0; static int toread = 0;
static int pos = 0; static int pos = 0;
if(reinit){ if (reinit) {
toread = 0; toread = 0;
pos = 0; pos = 0;
read_pos = 0; read_pos = 0;
return 0; return 0;
} }
while(1){ while (1) {
if(toread == 0){ if (toread == 0) {
nread = read(mldonkey_sock, read_buf+pos,4-pos); nread =
if(nread <= 0){ read(mldonkey_sock, read_buf + pos, 4 - pos);
if(errno == EAGAIN) { if (nread <= 0) {
return 0; if (errno == EAGAIN) {
} else { return 0;
close_sock(); } else {
pos = 0; toread = 0; return -1; } close_sock();
} pos = 0;
pos += nread; toread = 0;
if(pos == 4){ return -1;
toread = BUF32_TO_INT(read_buf,0); }
pos = 0; }
} pos += nread;
} else { if (pos == 4) {
nread = read(mldonkey_sock, read_buf+pos, toread - pos); toread = BUF32_TO_INT(read_buf, 0);
if(nread <= 0){ pos = 0;
if(errno == EAGAIN) return 0; else { }
pos = 0; toread = 0; } else {
close_sock(); nread =
return -1; } read(mldonkey_sock, read_buf + pos,
} toread - pos);
pos += nread; if (nread <= 0) {
if(pos == toread){ if (errno == EAGAIN)
/* We have one message !!! */ return 0;
int old_pos = pos; else {
read_pos = 0; pos = 0;
pos = 0; toread = 0;
toread = 0; close_sock();
return -1;
}
}
pos += nread;
if (pos == toread) {
/* We have one message !!! */
int old_pos = pos;
read_pos = 0;
pos = 0;
toread = 0;
return old_pos; return old_pos;
} }
} }
} }
} }
void close_sock() void close_sock()
{ {
old_config = NULL; old_config = NULL;
if(mldonkey_sock >= 0) close(mldonkey_sock); if (mldonkey_sock >= 0)
mldonkey_sock = -1; close(mldonkey_sock);
mldonkey_state = MLDONKEY_DISCONNECTED; mldonkey_sock = -1;
cut_messages(1); mldonkey_state = MLDONKEY_DISCONNECTED;
cut_messages(1);
} }
int mldonkey_connect(mldonkey_config *config) int mldonkey_connect(mldonkey_config * config)
{ {
if(config != old_config){ if (config != old_config) {
struct sockaddr_in sa; struct sockaddr_in sa;
int retcode; int retcode;
close_sock(); close_sock();
old_config=config; old_config = config;
/* resolve hostname */ /* resolve hostname */
memset(&sa, 0, sizeof(sa)); memset(&sa, 0, sizeof(sa));
if(config->mldonkey_hostname == NULL) if (config->mldonkey_hostname == NULL)
config->mldonkey_hostname = "127.0.0.1"; config->mldonkey_hostname = "127.0.0.1";
if(config->mldonkey_hostname[0] >= '0' && if (config->mldonkey_hostname[0] >= '0' &&
config->mldonkey_hostname[0] <= '9'){ config->mldonkey_hostname[0] <= '9') {
#ifdef HAS_INET_ATON #ifdef HAS_INET_ATON
if (inet_aton(config->mldonkey_hostname, &sa.sin_addr) == 0) return -1; if (inet_aton
#else (config->mldonkey_hostname, &sa.sin_addr) == 0)
return -1;
#else
sa.sin_addr.s_addr = inet_addr(config->mldonkey_hostname); sa.sin_addr.s_addr =
if (sa.sin_addr.s_addr == (unsigned int) -1) return -1; inet_addr(config->mldonkey_hostname);
#endif if (sa.sin_addr.s_addr == (unsigned int) -1)
return -1;
#endif
} else { } else {
struct hostent * hp; struct hostent *hp;
hp = gethostbyname(config->mldonkey_hostname); hp = gethostbyname(config->mldonkey_hostname);
if (hp == (struct hostent *) NULL) return -1; if (hp == (struct hostent *) NULL)
sa.sin_addr.s_addr = (unsigned long)hp->h_addr_list[0]; return -1;
} sa.sin_addr.s_addr =
(unsigned long) hp->h_addr_list[0];
}
sa.sin_port = htons(config->mldonkey_port); sa.sin_port = htons(config->mldonkey_port);
sa.sin_family = AF_INET; sa.sin_family = AF_INET;
if ((mldonkey_sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) { if ((mldonkey_sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
ERR("Opening socket"); ERR("Opening socket");
close_sock(); close_sock();
return -1; return -1;
} }
if( connect(mldonkey_sock, (struct sockaddr *) &sa, sizeof(sa)) < 0) { if (connect
if (errno != EAGAIN && (mldonkey_sock, (struct sockaddr *) &sa,
errno != EINTR && sizeof(sa)) < 0) {
errno != EINPROGRESS && if (errno != EAGAIN && errno != EINTR
errno != EWOULDBLOCK) { && errno != EINPROGRESS
&& errno != EWOULDBLOCK) {
// ERR("Connection failed"); // ERR("Connection failed");
close_sock(); close_sock();
return -1; return -1;
} }
} }
retcode = fcntl(mldonkey_sock, F_GETFL, 0); retcode = fcntl(mldonkey_sock, F_GETFL, 0);
if (retcode == -1 || if (retcode == -1 ||
fcntl(mldonkey_sock, F_SETFL, retcode | O_NONBLOCK) == -1){ fcntl(mldonkey_sock, F_SETFL,
return -1; retcode | O_NONBLOCK) == -1) {
} return -1;
}
mldonkey_state = MLDONKEY_CONNECTING; mldonkey_state = MLDONKEY_CONNECTING;
return 0; return 0;
} }
return 0; return 0;
} }
int mldonkey_can_read() int mldonkey_can_read()
{ {
return cut_messages(0); return cut_messages(0);
} }
int mldonkey_info_message(mldonkey_info *info) int mldonkey_info_message(mldonkey_info * info)
{ {
int opcode = read_int16(); int opcode = read_int16();
switch(opcode){ switch (opcode) {
case CoreProtocol : case CoreProtocol:
init_message(); init_message();
write_int16(0); /* GUI protocol */ write_int16(0); /* GUI protocol */
write_int32(10); /* Version 10 ! */ write_int32(10); /* Version 10 ! */
write_message("GuiProtocol"); write_message("GuiProtocol");
write_int16(47); /* GUI protocol */ write_int16(47); /* GUI protocol */
write_int16(1); write_int16(1);
write_int32(1); write_int32(1);
write_int8(1); write_int8(1);
write_message("GuiExtensions"); write_message("GuiExtensions");
init_message(); init_message();
write_int16(5); /* Password */ write_int16(5); /* Password */
write_string(old_config->mldonkey_password); write_string(old_config->mldonkey_password);
write_message("Password"); write_message("Password");
break; break;
case BadPassword : case BadPassword:
ERR("Bad Password\n"); ERR("Bad Password\n");
close_sock(); close_sock();
break; break;
case Client_stats: case Client_stats:
case Client_stats_v2: case Client_stats_v2:
case Client_stats_v3: case Client_stats_v3:
ERR("Client stats format too old...\n"); ERR("Client stats format too old...\n");
break; break;
case Client_stats_v4: case Client_stats_v4:
mldonkey_state = MLDONKEY_CONNECTED; mldonkey_state = MLDONKEY_CONNECTED;
info->upload_counter = read_int64(); info->upload_counter = read_int64();
info->download_counter = read_int64(); info->download_counter = read_int64();
info->shared_counter = read_int64(); info->shared_counter = read_int64();
info->nshared_files = read_int32(); info->nshared_files = read_int32();
info->tcp_upload_rate = read_int32(); info->tcp_upload_rate = read_int32();
info->tcp_download_rate = read_int32(); info->tcp_download_rate = read_int32();
info->udp_upload_rate = read_int32(); info->udp_upload_rate = read_int32();
info->udp_download_rate = read_int32(); info->udp_download_rate = read_int32();
info->ndownloading_files = read_int32(); info->ndownloading_files = read_int32();
info->ndownloaded_files = read_int32(); info->ndownloaded_files = read_int32();
break; break;
} }
return 0; return 0;
} }
int get_mldonkey_status(mldonkey_config *config, mldonkey_info *info) int get_mldonkey_status(mldonkey_config * config, mldonkey_info * info)
{ {
if( mldonkey_connect(config) >= 0){ if (mldonkey_connect(config) >= 0) {
while(mldonkey_can_read() > 0){ while (mldonkey_can_read() > 0) {
mldonkey_info_message(info); mldonkey_info_message(info);
} }
} }
return mldonkey_state; return mldonkey_state;
} }

75
mpd.c
View File

@ -14,65 +14,74 @@ void update_mpd()
if (current_info->conn->error) { if (current_info->conn->error) {
//fprintf(stderr, "%s\n", current_info->conn->errorStr); //fprintf(stderr, "%s\n", current_info->conn->errorStr);
mpd_closeConnection(current_info->conn); mpd_closeConnection(current_info->conn);
if(current_info->mpd.artist == NULL) if (current_info->mpd.artist == NULL)
current_info->mpd.artist = malloc(TEXT_BUFFER_SIZE); current_info->mpd.artist =
if(current_info->mpd.album == NULL) malloc(TEXT_BUFFER_SIZE);
if (current_info->mpd.album == NULL)
current_info->mpd.album = malloc(TEXT_BUFFER_SIZE); current_info->mpd.album = malloc(TEXT_BUFFER_SIZE);
if(current_info->mpd.title == NULL) if (current_info->mpd.title == NULL)
current_info->mpd.title = malloc(TEXT_BUFFER_SIZE); current_info->mpd.title = malloc(TEXT_BUFFER_SIZE);
strcpy(current_info->mpd.artist, "Unknown"); strcpy(current_info->mpd.artist, "Unknown");
strcpy(current_info->mpd.album, "Unknown"); strcpy(current_info->mpd.album, "Unknown");
strcpy(current_info->mpd.title, "Unknown"); strcpy(current_info->mpd.title, "Unknown");
current_info->mpd.status = "MPD not responding"; current_info->mpd.status = "MPD not responding";
current_info->mpd.bitrate = 0;
current_info->mpd.progress = 0;
current_info->mpd.elapsed = 0;
current_info->mpd.length = 0;
return; return;
} }
mpd_Status * status; mpd_Status *status;
mpd_InfoEntity *entity; mpd_InfoEntity *entity;
mpd_sendCommandListOkBegin(current_info->conn); mpd_sendCommandListOkBegin(current_info->conn);
mpd_sendStatusCommand(current_info->conn); mpd_sendStatusCommand(current_info->conn);
mpd_sendCurrentSongCommand(current_info->conn); mpd_sendCurrentSongCommand(current_info->conn);
mpd_sendCommandListEnd(current_info->conn); mpd_sendCommandListEnd(current_info->conn);
if ((status = mpd_getStatus(current_info->conn)) == NULL) { if ((status = mpd_getStatus(current_info->conn)) == NULL) {
//fprintf(stderr, "%s\n", current_info->conn->errorStr); //fprintf(stderr, "%s\n", current_info->conn->errorStr);
mpd_closeConnection(current_info->conn); mpd_closeConnection(current_info->conn);
if(current_info->mpd.artist == NULL) if (current_info->mpd.artist == NULL)
current_info->mpd.artist = malloc(TEXT_BUFFER_SIZE); current_info->mpd.artist =
if(current_info->mpd.album == NULL) malloc(TEXT_BUFFER_SIZE);
if (current_info->mpd.album == NULL)
current_info->mpd.album = malloc(TEXT_BUFFER_SIZE); current_info->mpd.album = malloc(TEXT_BUFFER_SIZE);
if(current_info->mpd.title == NULL) if (current_info->mpd.title == NULL)
current_info->mpd.title = malloc(TEXT_BUFFER_SIZE); current_info->mpd.title = malloc(TEXT_BUFFER_SIZE);
strcpy(current_info->mpd.artist, "Unknown"); strcpy(current_info->mpd.artist, "Unknown");
strcpy(current_info->mpd.album, "Unknown"); strcpy(current_info->mpd.album, "Unknown");
strcpy(current_info->mpd.title, "Unknown"); strcpy(current_info->mpd.title, "Unknown");
current_info->mpd.status = "MPD not responding"; current_info->mpd.status = "MPD not responding";
current_info->mpd.bitrate = 0;
current_info->mpd.progress = 0;
current_info->mpd.elapsed = 0;
current_info->mpd.length = 0;
return; return;
} }
current_info->mpd.volume = status->volume; current_info->mpd.volume = status->volume;
//if (status->error) //if (status->error)
//printf("error: %s\n", status->error); //printf("error: %s\n", status->error);
if(status->state == MPD_STATUS_STATE_PLAY) if (status->state == MPD_STATUS_STATE_PLAY) {
{
current_info->mpd.status = "Playing"; current_info->mpd.status = "Playing";
} }
if(status->state == MPD_STATUS_STATE_STOP) if (status->state == MPD_STATUS_STATE_STOP) {
{
current_info->mpd.status = "Stopped"; current_info->mpd.status = "Stopped";
} }
if(status->state == MPD_STATUS_STATE_PAUSE) if (status->state == MPD_STATUS_STATE_PAUSE) {
{
current_info->mpd.status = "Paused"; current_info->mpd.status = "Paused";
} }
if(status->state == MPD_STATUS_STATE_UNKNOWN) if (status->state == MPD_STATUS_STATE_UNKNOWN) {
{
current_info->mpd.status = "Unknown"; current_info->mpd.status = "Unknown";
} }
if(status->state == MPD_STATUS_STATE_PLAY || if (status->state == MPD_STATUS_STATE_PLAY ||
status->state == MPD_STATUS_STATE_PAUSE) { status->state == MPD_STATUS_STATE_PAUSE) {
current_info->mpd.bitrate = status->bitRate; current_info->mpd.bitrate = status->bitRate;
current_info->mpd.progress = (float)status->elapsedTime/status->totalTime; current_info->mpd.progress =
} (float) status->elapsedTime / status->totalTime;
current_info->mpd.elapsed = status->elapsedTime;
current_info->mpd.length = status->totalTime;
}
if (current_info->conn->error) { if (current_info->conn->error) {
@ -84,34 +93,32 @@ void update_mpd()
mpd_nextListOkCommand(current_info->conn); mpd_nextListOkCommand(current_info->conn);
while ((entity = mpd_getNextInfoEntity(current_info->conn))) { while ((entity = mpd_getNextInfoEntity(current_info->conn))) {
mpd_Song * song = entity->info.song; mpd_Song *song = entity->info.song;
if (entity->type != MPD_INFO_ENTITY_TYPE_SONG) { if (entity->type != MPD_INFO_ENTITY_TYPE_SONG) {
mpd_freeInfoEntity(entity); mpd_freeInfoEntity(entity);
continue; continue;
} }
if(current_info->mpd.artist == NULL) if (current_info->mpd.artist == NULL)
current_info->mpd.artist = malloc(TEXT_BUFFER_SIZE); current_info->mpd.artist =
if(current_info->mpd.album == NULL) malloc(TEXT_BUFFER_SIZE);
if (current_info->mpd.album == NULL)
current_info->mpd.album = malloc(TEXT_BUFFER_SIZE); current_info->mpd.album = malloc(TEXT_BUFFER_SIZE);
if(current_info->mpd.title == NULL) if (current_info->mpd.title == NULL)
current_info->mpd.title = malloc(TEXT_BUFFER_SIZE); current_info->mpd.title = malloc(TEXT_BUFFER_SIZE);
if (song->artist) { if (song->artist) {
strcpy(current_info->mpd.artist, song->artist); strcpy(current_info->mpd.artist, song->artist);
} } else {
else {
strcpy(current_info->mpd.artist, "Unknown"); strcpy(current_info->mpd.artist, "Unknown");
} }
if (song->album) { if (song->album) {
strcpy(current_info->mpd.album, song->album); strcpy(current_info->mpd.album, song->album);
} } else {
else {
strcpy(current_info->mpd.album, "Unknown"); strcpy(current_info->mpd.album, "Unknown");
} }
if (song->title) { if (song->title) {
strcpy(current_info->mpd.title, song->title); strcpy(current_info->mpd.title, song->title);
} } else {
else {
strcpy(current_info->mpd.title, "Unknown"); strcpy(current_info->mpd.title, "Unknown");
} }
if (entity != NULL) { if (entity != NULL) {

182
top.c
View File

@ -25,8 +25,8 @@ static struct process *new_process(int p)
process = malloc(sizeof(struct process)); process = malloc(sizeof(struct process));
/* /*
* Do stitching necessary for doubly linked list * Do stitching necessary for doubly linked list
*/ */
process->name = 0; process->name = 0;
process->previous = 0; process->previous = 0;
process->next = first_process; process->next = first_process;
@ -40,7 +40,7 @@ static struct process *new_process(int p)
process->previous_kernel_time = INT_MAX; process->previous_kernel_time = INT_MAX;
process->counted = 1; process->counted = 1;
/* process_find_name(process);*/ /* process_find_name(process); */
return process; return process;
} }
@ -86,13 +86,13 @@ static int process_parse_stat(struct process *process)
ps = open(filename, O_RDONLY); ps = open(filename, O_RDONLY);
if (ps < 0) if (ps < 0)
/* /*
* The process must have finished in the last few jiffies! * The process must have finished in the last few jiffies!
*/ */
return 1; return 1;
/* /*
* Mark process as up-to-date. * Mark process as up-to-date.
*/ */
process->time_stamp = g_time; process->time_stamp = g_time;
rc = read(ps, line, sizeof(line)); rc = read(ps, line, sizeof(line));
@ -101,8 +101,8 @@ static int process_parse_stat(struct process *process)
return 1; return 1;
/* /*
* Extract cpu times from data in /proc filesystem * Extract cpu times from data in /proc filesystem
*/ */
rc = sscanf(line, rc = sscanf(line,
"%*s %s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %d %d %*s %*s %*s %d %*s %*s %*s %d %d", "%*s %s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %d %d %*s %*s %*s %d %*s %*s %*s %d %d",
procname, &process->user_time, &process->kernel_time, procname, &process->user_time, &process->kernel_time,
@ -110,8 +110,8 @@ static int process_parse_stat(struct process *process)
if (rc < 5) if (rc < 5)
return 1; return 1;
/* /*
* Remove parentheses from the process name stored in /proc/ under Linux... * Remove parentheses from the process name stored in /proc/ under Linux...
*/ */
r = procname + 1; r = procname + 1;
/* remove any "kdeinit: " */ /* remove any "kdeinit: " */
if (r == strstr(r, "kdeinit")) { if (r == strstr(r, "kdeinit")) {
@ -121,8 +121,8 @@ static int process_parse_stat(struct process *process)
ps = open(filename, O_RDONLY); ps = open(filename, O_RDONLY);
if (ps < 0) if (ps < 0)
/* /*
* The process must have finished in the last few jiffies! * The process must have finished in the last few jiffies!
*/ */
return 1; return 1;
endl = read(ps, line, sizeof(line)); endl = read(ps, line, sizeof(line));
@ -153,10 +153,10 @@ static int process_parse_stat(struct process *process)
process->name = strdup(deparenthesised_name); process->name = strdup(deparenthesised_name);
process->rss *= getpagesize(); process->rss *= getpagesize();
if(!cur->memmax) if (!cur->memmax)
update_total_processes(); update_total_processes();
process->totalmem = ( (float) process->rss / cur->memmax ) / 10; process->totalmem = ((float) process->rss / cur->memmax) / 10;
if (process->previous_user_time == INT_MAX) if (process->previous_user_time == INT_MAX)
process->previous_user_time = process->user_time; process->previous_user_time = process->user_time;
@ -194,15 +194,15 @@ static int update_process_table()
++g_time; ++g_time;
/* /*
* Get list of processes from /proc directory * Get list of processes from /proc directory
*/ */
while ((entry = readdir(dir))) { while ((entry = readdir(dir))) {
pid_t pid; pid_t pid;
if (!entry) { if (!entry) {
/* /*
* Problem reading list of processes * Problem reading list of processes
*/ */
closedir(dir); closedir(dir);
return 1; return 1;
} }
@ -240,14 +240,14 @@ static int calculate_cpu(struct process *process)
if (rc) if (rc)
return 1; return 1;
/*rc = process_parse_statm(process); /*rc = process_parse_statm(process);
if (rc) if (rc)
return 1;*/ return 1; */
/* /*
* Check name against the exclusion list * Check name against the exclusion list
*/ */
if (process->counted && exclusion_expression if (process->counted && exclusion_expression
&& !regexec(exclusion_expression, process->name, 0, 0, 0)) && !regexec(exclusion_expression, process->name, 0, 0, 0))
process->counted = 0; process->counted = 0;
return 0; return 0;
@ -270,8 +270,8 @@ static void process_cleanup()
p = p->next; p = p->next;
/* /*
* Delete processes that have died * Delete processes that have died
*/ */
if (current->time_stamp != g_time) if (current->time_stamp != g_time)
delete_process(current); delete_process(current);
} }
@ -287,14 +287,14 @@ static void delete_process(struct process *p)
assert(p->id == 0x0badfeed); assert(p->id == 0x0badfeed);
/* /*
* Ensure that deleted processes aren't reused. * Ensure that deleted processes aren't reused.
*/ */
p->id = 0x007babe; p->id = 0x007babe;
#endif /* defined(PARANOID) */ #endif /* defined(PARANOID) */
/* /*
* Maintain doubly linked list. * Maintain doubly linked list.
*/ */
if (p->next) if (p->next)
p->next->previous = p->previous; p->next->previous = p->previous;
if (p->previous) if (p->previous)
@ -345,9 +345,10 @@ inline static void calc_cpu_each(int total)
struct process *p = first_process; struct process *p = first_process;
while (p) { while (p) {
/*p->amount = total ? /*p->amount = total ?
(100.0 * (float) (p->user_time + p->kernel_time) / (100.0 * (float) (p->user_time + p->kernel_time) /
total) : 0;*/ total) : 0; */
p->amount = (100.0 * (p->user_time + p->kernel_time) / total); p->amount =
(100.0 * (p->user_time + p->kernel_time) / total);
/* if (p->amount > 100) /* if (p->amount > 100)
p->amount = 0;*/ p->amount = 0;*/
@ -370,7 +371,7 @@ inline void process_find_top(struct process **cpu, struct process **mem)
{ {
struct process *pr; struct process *pr;
if (sorttmp == NULL) { if (sorttmp == NULL) {
sorttmp = malloc(sizeof(struct process)*sorttmp_size); sorttmp = malloc(sizeof(struct process) * sorttmp_size);
assert(sorttmp != NULL); assert(sorttmp != NULL);
} }
int total; int total;
@ -383,64 +384,64 @@ inline void process_find_top(struct process **cpu, struct process **mem)
process_cleanup(); /* cleanup list from exited processes */ process_cleanup(); /* cleanup list from exited processes */
/* /*
* this is really ugly, * this is really ugly,
* not to mention probably not too efficient. * not to mention probably not too efficient.
* the main problem is that there could be any number of processes, * the main problem is that there could be any number of processes,
* however we have to use a fixed size for the "best" array. * however we have to use a fixed size for the "best" array.
* right now i can't think of a better way to do this, * right now i can't think of a better way to do this,
* although i'm sure there is one. * although i'm sure there is one.
* Perhaps just using a linked list would be more effecient? * Perhaps just using a linked list would be more effecient?
* I'm too fucking lazy to do that right now. * I'm too fucking lazy to do that right now.
*/ */
if(top_cpu) { if (top_cpu) {
pr = first_process; pr = first_process;
i = 0; i = 0;
while(pr) { while (pr) {
if(i<sorttmp_size && pr->counted) { if (i < sorttmp_size && pr->counted) {
sorttmp[i] = pr; sorttmp[i] = pr;
i++; i++;
} } else if (i == sorttmp_size && pr->counted) {
else if (i == sorttmp_size && pr->counted) {
sorttmp_size++; sorttmp_size++;
sorttmp = realloc(sorttmp, sizeof(struct process)*sorttmp_size); sorttmp =
realloc(sorttmp,
sizeof(struct process) *
sorttmp_size);
sorttmp[i] = pr; sorttmp[i] = pr;
i++; i++;
} }
pr = pr->next; pr = pr->next;
} }
if (i+1 < sorttmp_size) { if (i + 1 < sorttmp_size) {
sorttmp = realloc(sorttmp, sizeof(struct process)*sorttmp_size); sorttmp =
realloc(sorttmp,
sizeof(struct process) * sorttmp_size);
} }
max = i; max = i;
for(i=0;i<max-1;i++) for (i = 0; i < max - 1; i++) {
{ while (sorttmp[i + 1]->amount > sorttmp[i]->amount) {
while (sorttmp[i+1]->amount > sorttmp[i]->amount)
{
pr = sorttmp[i]; pr = sorttmp[i];
sorttmp[i] = sorttmp[i+1]; sorttmp[i] = sorttmp[i + 1];
sorttmp[i+1] = pr; sorttmp[i + 1] = pr;
if (i>0) if (i > 0)
i--; i--;
else else
break; break;
} }
} }
for(i=max;i>1;i--); for (i = max; i > 1; i--);
{ {
while (sorttmp[i]->amount > sorttmp[i-1]->amount) while (sorttmp[i]->amount > sorttmp[i - 1]->amount) {
{
pr = sorttmp[i]; pr = sorttmp[i];
sorttmp[i] = sorttmp[i-1]; sorttmp[i] = sorttmp[i - 1];
sorttmp[i-1] = pr; sorttmp[i - 1] = pr;
if (i<max) if (i < max)
i++; i++;
else else
break; break;
} }
} }
for(i=0;i<10;i++) for (i = 0; i < 10; i++) {
{
cpu[i] = sorttmp[i]; cpu[i] = sorttmp[i];
} }
@ -448,51 +449,54 @@ inline void process_find_top(struct process **cpu, struct process **mem)
if (top_mem) { if (top_mem) {
pr = first_process; pr = first_process;
i = 0; i = 0;
while(pr) { while (pr) {
if(i<sorttmp_size && pr->counted) { if (i < sorttmp_size && pr->counted) {
sorttmp[i] = pr; sorttmp[i] = pr;
i++; i++;
} } else if (i == sorttmp_size && pr->counted) {
else if (i == sorttmp_size && pr->counted) {
sorttmp_size++; sorttmp_size++;
sorttmp = realloc(sorttmp, sizeof(struct process)*sorttmp_size); sorttmp =
realloc(sorttmp,
sizeof(struct process) *
sorttmp_size);
sorttmp[i] = pr; sorttmp[i] = pr;
i++; i++;
} }
pr = pr->next; pr = pr->next;
} }
if (i+1 < sorttmp_size) { if (i + 1 < sorttmp_size) {
sorttmp = realloc(sorttmp, sizeof(struct process)*sorttmp_size); sorttmp =
} max = i; realloc(sorttmp,
for(i=0;i<max-1;i++) sizeof(struct process) * sorttmp_size);
{ }
while (sorttmp[i+1]->totalmem > sorttmp[i]->totalmem) max = i;
{ for (i = 0; i < max - 1; i++) {
while (sorttmp[i + 1]->totalmem >
sorttmp[i]->totalmem) {
pr = sorttmp[i]; pr = sorttmp[i];
sorttmp[i] = sorttmp[i+1]; sorttmp[i] = sorttmp[i + 1];
sorttmp[i+1] = pr; sorttmp[i + 1] = pr;
if (i>0) if (i > 0)
i--; i--;
else else
break; break;
} }
} }
for(i=max;i>1;i--); for (i = max; i > 1; i--);
{ {
while (sorttmp[i]->totalmem > sorttmp[i-1]->totalmem) while (sorttmp[i]->totalmem >
{ sorttmp[i - 1]->totalmem) {
pr = sorttmp[i]; pr = sorttmp[i];
sorttmp[i] = sorttmp[i-1]; sorttmp[i] = sorttmp[i - 1];
sorttmp[i-1] = pr; sorttmp[i - 1] = pr;
if (i<max) if (i < max)
i++; i++;
else else
break; break;
} }
} }
for(i=0;i<10;i++) for (i = 0; i < 10; i++) {
{
mem[i] = sorttmp[i]; mem[i] = sorttmp[i];
} }

9
x11.c
View File

@ -311,11 +311,12 @@ static Window find_subwindow(Window win, int w, int h)
long get_x11_color(const char *name) long get_x11_color(const char *name)
{ {
XColor color; XColor color;
color.pixel = 0; color.pixel = 0;
if (!XParseColor(display, DefaultColormap(display, screen), name, &color)) { if (!XParseColor
ERR("can't parse X color '%s'", name); (display, DefaultColormap(display, screen), name, &color)) {
return 0xFF00FF; ERR("can't parse X color '%s'", name);
return 0xFF00FF;
} }
if (!XAllocColor if (!XAllocColor
(display, DefaultColormap(display, screen), &color)) (display, DefaultColormap(display, screen), &color))