2010-01-04 17:06:14 +00:00
|
|
|
/* -*- mode: c++; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
|
|
|
|
* vim: ts=4 sw=4 noet ai cindent syntax=cpp
|
2009-07-28 21:44:22 +00:00
|
|
|
*
|
|
|
|
* Conky, a system monitor, based on torsmo
|
2005-08-05 01:06:17 +00:00
|
|
|
*
|
2007-08-10 19:53:44 +00:00
|
|
|
* Any original torsmo code is licensed under the BSD license
|
|
|
|
*
|
|
|
|
* All code written since the fork of torsmo is licensed under the GPL
|
|
|
|
*
|
|
|
|
* Please see COPYING for details
|
|
|
|
*
|
|
|
|
* Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
|
2012-05-03 23:34:44 +00:00
|
|
|
* Copyright (c) 2005-2012 Brenden Matthews, Philip Kovacs, et. al.
|
2008-02-20 20:30:45 +00:00
|
|
|
* (see AUTHORS)
|
2007-08-10 19:53:44 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2008-02-20 20:30:45 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2005-08-05 01:06:17 +00:00
|
|
|
*
|
2008-12-09 23:35:49 +00:00
|
|
|
*/
|
2005-08-05 01:06:17 +00:00
|
|
|
|
2008-12-15 21:40:24 +00:00
|
|
|
#include "config.h"
|
2009-08-07 07:24:24 +00:00
|
|
|
#include "conky.h"
|
2010-04-17 13:19:44 +00:00
|
|
|
#include "core.h"
|
2008-12-15 21:40:24 +00:00
|
|
|
#include "fs.h"
|
|
|
|
#include "logging.h"
|
2009-10-17 14:43:12 +00:00
|
|
|
#include "net_stat.h"
|
2009-10-25 12:49:10 +00:00
|
|
|
#include "specials.h"
|
2009-11-26 00:38:53 +00:00
|
|
|
#include "temphelper.h"
|
2009-11-19 22:39:08 +00:00
|
|
|
#include "timeinfo.h"
|
2009-11-26 00:30:58 +00:00
|
|
|
#include "top.h"
|
2005-07-20 00:30:40 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
2011-09-17 11:07:16 +00:00
|
|
|
#include <time.h>
|
2009-03-28 23:15:20 +00:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <netinet/in.h>
|
2007-08-30 17:21:30 +00:00
|
|
|
#include <pthread.h>
|
2009-09-20 01:11:38 +00:00
|
|
|
#include <semaphore.h>
|
2009-03-30 05:35:42 +00:00
|
|
|
#include <unistd.h>
|
2009-07-22 20:08:31 +00:00
|
|
|
#include <fcntl.h>
|
2010-01-04 17:06:14 +00:00
|
|
|
#include <vector>
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2008-12-15 21:40:24 +00:00
|
|
|
/* check for OS and include appropriate headers */
|
|
|
|
#if defined(__linux__)
|
|
|
|
#include "linux.h"
|
|
|
|
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
|
|
|
|
#include "freebsd.h"
|
2012-06-28 16:56:01 +00:00
|
|
|
#elif defined(__DragonFly__)
|
|
|
|
#include "dragonfly.h"
|
2008-12-15 21:40:24 +00:00
|
|
|
#elif defined(__OpenBSD__)
|
|
|
|
#include "openbsd.h"
|
|
|
|
#endif
|
|
|
|
|
2010-12-31 12:39:54 +00:00
|
|
|
#include "update-cb.hh"
|
|
|
|
|
2010-12-31 15:43:49 +00:00
|
|
|
#ifdef BUILD_CURL
|
|
|
|
#include "ccurl_thread.h"
|
|
|
|
#endif /* BUILD_CURL */
|
|
|
|
|
2009-07-21 21:41:47 +00:00
|
|
|
/* folds a string over top of itself, like so:
|
|
|
|
*
|
|
|
|
* if start is "blah", and you call it with count = 1, the result will be "lah"
|
|
|
|
*/
|
|
|
|
void strfold(char *start, int count)
|
|
|
|
{
|
|
|
|
char *curplace;
|
|
|
|
for (curplace = start + count; *curplace != 0; curplace++) {
|
|
|
|
*(curplace - count) = *curplace;
|
|
|
|
}
|
|
|
|
*(curplace - count) = 0;
|
|
|
|
}
|
|
|
|
|
2008-04-02 19:46:09 +00:00
|
|
|
#ifndef HAVE_STRNDUP
|
|
|
|
// use our own strndup() if it's not available
|
|
|
|
char *strndup(const char *s, size_t n)
|
|
|
|
{
|
2008-12-08 23:12:23 +00:00
|
|
|
if (strlen(s) > n) {
|
|
|
|
char *ret = malloc(n + 1);
|
2008-04-02 19:46:09 +00:00
|
|
|
strncpy(ret, s, n);
|
2008-12-08 23:12:23 +00:00
|
|
|
ret[n] = 0;
|
2008-04-02 19:46:09 +00:00
|
|
|
return ret;
|
|
|
|
} else {
|
|
|
|
return strdup(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* HAVE_STRNDUP */
|
|
|
|
|
2010-05-05 16:46:04 +00:00
|
|
|
int update_uname(void)
|
2005-07-20 00:30:40 +00:00
|
|
|
{
|
2009-08-07 07:24:24 +00:00
|
|
|
uname(&info.uname_s);
|
2012-06-28 16:56:01 +00:00
|
|
|
|
|
|
|
#if defined(__DragonFly__)
|
|
|
|
{
|
|
|
|
size_t desc_n; char desc[256];
|
|
|
|
|
|
|
|
if (sysctlbyname("kern.version", NULL, &desc_n, NULL, 0) == -1 ||
|
|
|
|
sysctlbyname("kern.version", desc, &desc_n, NULL, 0) == -1)
|
|
|
|
perror("kern.version");
|
|
|
|
else {
|
|
|
|
char *start = desc;
|
|
|
|
strsep(&start, " ");
|
|
|
|
strcpy(info.uname_v, strsep(&start, " "));
|
|
|
|
}
|
|
|
|
|
2011-04-18 19:24:42 +00:00
|
|
|
if (errno == ENOMEM) printf("desc_n %zu\n", desc_n);
|
2012-06-28 16:56:01 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-05-05 16:46:04 +00:00
|
|
|
return 0;
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
|
2008-03-29 02:01:03 +00:00
|
|
|
double get_time(void)
|
2005-07-20 00:30:40 +00:00
|
|
|
{
|
2011-09-17 11:07:16 +00:00
|
|
|
struct timespec tv;
|
2016-04-13 17:33:33 +00:00
|
|
|
#ifdef _POSIX_MONOTONIC_CLOCK
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &tv);
|
|
|
|
#else
|
2016-02-08 21:41:30 +00:00
|
|
|
clock_gettime(CLOCK_REALTIME, &tv);
|
2016-04-13 17:33:33 +00:00
|
|
|
#endif
|
2011-09-17 11:07:16 +00:00
|
|
|
return tv.tv_sec + (tv.tv_nsec * 1e-9);
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
|
2010-03-04 10:42:38 +00:00
|
|
|
/* Converts '~/...' paths to '/home/blah/...'
|
|
|
|
* It's similar to variable_substitute, except only cheques for $HOME and ~/ in path */
|
|
|
|
std::string to_real_path(const std::string &source)
|
2009-05-25 04:33:47 +00:00
|
|
|
{
|
2010-03-04 10:42:38 +00:00
|
|
|
const char *homedir = getenv("HOME");
|
|
|
|
if(source.find("~/") == 0)
|
|
|
|
return homedir + source.substr(1);
|
|
|
|
else if(source.find("$HOME/") == 0)
|
|
|
|
return homedir + source.substr(5);
|
|
|
|
else
|
|
|
|
return source;
|
2009-05-25 04:33:47 +00:00
|
|
|
}
|
|
|
|
|
2009-07-22 20:08:31 +00:00
|
|
|
int open_fifo(const char *file, int *reported)
|
|
|
|
{
|
|
|
|
int fd = 0;
|
|
|
|
|
|
|
|
fd = open(file, O_RDONLY | O_NONBLOCK);
|
|
|
|
|
|
|
|
if (fd == -1) {
|
|
|
|
if (!reported || *reported == 0) {
|
2009-08-01 18:45:43 +00:00
|
|
|
NORM_ERR("can't open %s: %s", file, strerror(errno));
|
2009-07-22 20:08:31 +00:00
|
|
|
if (reported) {
|
|
|
|
*reported = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
FILE *open_file(const char *file, int *reported)
|
|
|
|
{
|
2009-05-25 04:33:47 +00:00
|
|
|
FILE *fp = 0;
|
|
|
|
|
|
|
|
fp = fopen(file, "r");
|
2008-02-20 20:30:45 +00:00
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
if (!fp) {
|
|
|
|
if (!reported || *reported == 0) {
|
2009-08-01 18:45:43 +00:00
|
|
|
NORM_ERR("can't open %s: %s", file, strerror(errno));
|
2008-02-20 20:30:45 +00:00
|
|
|
if (reported) {
|
2005-07-20 00:30:40 +00:00
|
|
|
*reported = 1;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
2009-07-22 20:08:31 +00:00
|
|
|
return NULL;
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return fp;
|
|
|
|
}
|
|
|
|
|
2010-08-26 18:03:53 +00:00
|
|
|
std::string variable_substitute(std::string s)
|
2005-07-20 00:30:40 +00:00
|
|
|
{
|
2010-08-26 18:03:53 +00:00
|
|
|
std::string::size_type pos = 0;
|
|
|
|
while((pos = s.find('$', pos)) != std::string::npos) {
|
|
|
|
if(pos + 1 >= s.size())
|
|
|
|
break;
|
|
|
|
|
|
|
|
if(s[pos+1] == '$') {
|
|
|
|
s.erase(pos, 1);
|
|
|
|
++pos;
|
|
|
|
} else {
|
|
|
|
std::string var;
|
|
|
|
std::string::size_type l = 0;
|
|
|
|
|
|
|
|
if(isalpha(s[pos+1])) {
|
|
|
|
l = 1;
|
|
|
|
while(pos+l < s.size() && isalnum(s[pos+l]))
|
|
|
|
++l;
|
|
|
|
var = s.substr(pos+1, l-1);
|
|
|
|
} else if(s[pos+1] == '{') {
|
|
|
|
l = s.find('}', pos);
|
|
|
|
if(l == std::string::npos)
|
|
|
|
break;
|
|
|
|
l -= pos - 1;
|
|
|
|
var = s.substr(pos+2, l-3);
|
|
|
|
} else
|
|
|
|
++pos;
|
|
|
|
|
|
|
|
if(l) {
|
|
|
|
s.erase(pos, l);
|
|
|
|
const char *val = getenv(var.c_str());
|
|
|
|
if(val) {
|
|
|
|
s.insert(pos, val);
|
|
|
|
pos += strlen(val);
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-26 18:03:53 +00:00
|
|
|
return s;
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
|
2008-06-19 07:04:11 +00:00
|
|
|
void format_seconds(char *buf, unsigned int n, long seconds)
|
2005-07-20 00:30:40 +00:00
|
|
|
{
|
2008-06-19 07:04:11 +00:00
|
|
|
long days;
|
|
|
|
int hours, minutes;
|
|
|
|
|
2010-03-05 13:20:04 +00:00
|
|
|
if (times_in_seconds.get(*state)) {
|
2009-11-19 22:39:08 +00:00
|
|
|
snprintf(buf, n, "%ld", seconds);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-06-19 07:04:11 +00:00
|
|
|
days = seconds / 86400;
|
|
|
|
seconds %= 86400;
|
|
|
|
hours = seconds / 3600;
|
|
|
|
seconds %= 3600;
|
|
|
|
minutes = seconds / 60;
|
|
|
|
seconds %= 60;
|
|
|
|
|
|
|
|
if (days > 0) {
|
|
|
|
snprintf(buf, n, "%ldd %dh %dm", days, hours, minutes);
|
2008-02-20 20:30:45 +00:00
|
|
|
} else {
|
2008-06-19 07:04:11 +00:00
|
|
|
snprintf(buf, n, "%dh %dm %lds", hours, minutes, seconds);
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
|
2008-06-19 07:04:11 +00:00
|
|
|
void format_seconds_short(char *buf, unsigned int n, long seconds)
|
2005-07-20 00:30:40 +00:00
|
|
|
{
|
2008-06-19 07:04:11 +00:00
|
|
|
long days;
|
|
|
|
int hours, minutes;
|
|
|
|
|
2010-03-05 13:20:04 +00:00
|
|
|
if (times_in_seconds.get(*state)) {
|
2009-11-19 22:39:08 +00:00
|
|
|
snprintf(buf, n, "%ld", seconds);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-06-19 07:04:11 +00:00
|
|
|
days = seconds / 86400;
|
|
|
|
seconds %= 86400;
|
|
|
|
hours = seconds / 3600;
|
|
|
|
seconds %= 3600;
|
|
|
|
minutes = seconds / 60;
|
|
|
|
seconds %= 60;
|
|
|
|
|
|
|
|
if (days > 0) {
|
|
|
|
snprintf(buf, n, "%ldd %dh", days, hours);
|
|
|
|
} else if (hours > 0) {
|
|
|
|
snprintf(buf, n, "%dh %dm", hours, minutes);
|
2008-02-20 20:30:45 +00:00
|
|
|
} else {
|
2008-06-19 07:04:11 +00:00
|
|
|
snprintf(buf, n, "%dm %lds", minutes, seconds);
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
|
2010-03-07 12:29:30 +00:00
|
|
|
conky::simple_config_setting<bool> no_buffers("no_buffers", true, true);
|
2009-08-07 07:24:24 +00:00
|
|
|
|
|
|
|
void update_stuff(void)
|
2005-07-20 00:30:40 +00:00
|
|
|
{
|
2012-01-08 14:45:08 +00:00
|
|
|
/* clear speeds, addresses and up status in case device was removed and
|
|
|
|
* doesn't get updated */
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2009-05-24 15:41:35 +00:00
|
|
|
#ifdef HAVE_OPENMP
|
2009-07-18 19:46:36 +00:00
|
|
|
#pragma omp parallel for schedule(dynamic,10)
|
2009-05-24 15:41:35 +00:00
|
|
|
#endif /* HAVE_OPENMP */
|
2016-01-09 17:23:36 +00:00
|
|
|
for (int i = 0; i < MAX_NET_INTERFACES; ++i) {
|
2005-07-20 00:30:40 +00:00
|
|
|
if (netstats[i].dev) {
|
|
|
|
netstats[i].up = 0;
|
|
|
|
netstats[i].recv_speed = 0.0;
|
|
|
|
netstats[i].trans_speed = 0.0;
|
2012-01-08 14:45:08 +00:00
|
|
|
netstats[i].addr.sa_data[2] = 0;
|
|
|
|
netstats[i].addr.sa_data[3] = 0;
|
|
|
|
netstats[i].addr.sa_data[4] = 0;
|
|
|
|
netstats[i].addr.sa_data[5] = 0;
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-09 17:23:36 +00:00
|
|
|
/* this is a stub on all platforms except solaris */
|
2005-07-20 00:30:40 +00:00
|
|
|
prepare_update();
|
|
|
|
|
2016-01-09 17:23:36 +00:00
|
|
|
/* if you registered a callback with conky::register_cb, this will run it */
|
2010-12-31 12:39:54 +00:00
|
|
|
conky::run_all_callbacks();
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2009-09-06 23:05:33 +00:00
|
|
|
/* XXX: move the following into the update_meminfo() functions? */
|
2010-03-07 12:20:16 +00:00
|
|
|
if (no_buffers.get(*state)) {
|
2009-09-06 23:05:33 +00:00
|
|
|
info.mem -= info.bufmem;
|
|
|
|
info.memeasyfree += info.bufmem;
|
2008-06-21 20:37:58 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
2005-08-30 14:59:37 +00:00
|
|
|
|
2009-05-18 05:30:10 +00:00
|
|
|
/* Ohkie to return negative values for temperatures */
|
|
|
|
int round_to_int_temp(float f)
|
2005-08-30 14:59:37 +00:00
|
|
|
{
|
2008-02-19 03:33:12 +00:00
|
|
|
if (f >= 0.0) {
|
|
|
|
return (int) (f + 0.5);
|
|
|
|
} else {
|
|
|
|
return (int) (f - 0.5);
|
|
|
|
}
|
2005-08-30 14:59:37 +00:00
|
|
|
}
|
2009-05-18 05:30:10 +00:00
|
|
|
/* Don't return negative values for cpugraph, bar, gauge, percentage.
|
|
|
|
* Causes unreasonable numbers to show */
|
|
|
|
unsigned int round_to_int(float f)
|
|
|
|
{
|
|
|
|
if (f >= 0.0) {
|
|
|
|
return (int) (f + 0.5);
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2009-07-13 05:31:57 +00:00
|
|
|
|
2009-10-25 12:49:10 +00:00
|
|
|
void scan_loadavg_arg(struct text_object *obj, const char *arg)
|
|
|
|
{
|
2009-10-25 13:13:26 +00:00
|
|
|
obj->data.i = 0;
|
|
|
|
if (arg && !arg[1] && isdigit(arg[0])) {
|
|
|
|
obj->data.i = atoi(arg);
|
|
|
|
if (obj->data.i > 3 || obj->data.i < 1) {
|
|
|
|
NORM_ERR("loadavg arg needs to be in range (1,3)");
|
|
|
|
obj->data.i = 0;
|
2009-10-25 12:49:10 +00:00
|
|
|
}
|
|
|
|
}
|
2009-10-25 13:13:26 +00:00
|
|
|
/* convert to array index (or the default (-1)) */
|
|
|
|
obj->data.i--;
|
2009-10-25 12:49:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void print_loadavg(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
|
|
|
float *v = info.loadavg;
|
|
|
|
|
2009-10-25 13:13:26 +00:00
|
|
|
if (obj->data.i < 0) {
|
|
|
|
snprintf(p, p_max_size, "%.2f %.2f %.2f", v[0], v[1], v[2]);
|
|
|
|
} else {
|
|
|
|
snprintf(p, p_max_size, "%.2f", v[obj->data.i]);
|
2009-10-25 12:49:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-17 13:19:44 +00:00
|
|
|
void scan_no_update(struct text_object *obj, const char *arg)
|
|
|
|
{
|
2010-11-20 16:25:06 +00:00
|
|
|
obj->data.s = (char*) malloc(text_buffer_size.get(*state));
|
2010-11-20 16:18:34 +00:00
|
|
|
evaluate(arg, obj->data.s, text_buffer_size.get(*state));
|
2010-04-17 13:19:44 +00:00
|
|
|
obj->data.s = (char*) realloc(obj->data.s, strlen(obj->data.s) + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void free_no_update(struct text_object *obj) {
|
|
|
|
free(obj->data.s);
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_no_update(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
|
|
|
snprintf(p, p_max_size, "%s", obj->data.s);
|
|
|
|
}
|
|
|
|
|
2010-01-07 02:38:12 +00:00
|
|
|
#ifdef BUILD_X11
|
2009-10-25 12:49:10 +00:00
|
|
|
void scan_loadgraph_arg(struct text_object *obj, const char *arg)
|
|
|
|
{
|
|
|
|
char *buf = 0;
|
2009-10-25 13:13:26 +00:00
|
|
|
|
2009-11-05 23:05:08 +00:00
|
|
|
buf = scan_graph(obj, arg, 0);
|
2010-02-23 14:48:18 +00:00
|
|
|
free_and_zero(buf);
|
2009-10-25 12:49:10 +00:00
|
|
|
}
|
|
|
|
|
2009-12-04 00:49:31 +00:00
|
|
|
double loadgraphval(struct text_object *obj)
|
2009-10-25 12:49:10 +00:00
|
|
|
{
|
2009-11-23 23:24:17 +00:00
|
|
|
(void)obj;
|
2009-11-15 23:54:15 +00:00
|
|
|
|
2009-12-04 00:49:31 +00:00
|
|
|
return info.loadavg[0];
|
2009-10-25 12:49:10 +00:00
|
|
|
}
|
2010-01-07 02:38:12 +00:00
|
|
|
#endif /* BUILD_X11 */
|
2009-11-22 17:02:56 +00:00
|
|
|
|
2009-11-25 00:30:21 +00:00
|
|
|
uint8_t cpu_percentage(struct text_object *obj)
|
|
|
|
{
|
|
|
|
if (obj->data.i > info.cpu_count) {
|
|
|
|
NORM_ERR("obj->data.i %i info.cpu_count %i",
|
|
|
|
obj->data.i, info.cpu_count);
|
|
|
|
CRIT_ERR(NULL, NULL, "attempting to use more CPUs than you have!");
|
|
|
|
}
|
2010-01-05 15:59:50 +00:00
|
|
|
if (info.cpu_usage) {
|
|
|
|
return round_to_int(info.cpu_usage[obj->data.i] * 100.0);
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2009-11-25 00:30:21 +00:00
|
|
|
}
|
|
|
|
|
2009-12-04 00:19:07 +00:00
|
|
|
double cpu_barval(struct text_object *obj)
|
2009-11-22 17:02:56 +00:00
|
|
|
{
|
2010-01-05 15:59:50 +00:00
|
|
|
if (info.cpu_usage) {
|
|
|
|
return info.cpu_usage[obj->data.i];
|
|
|
|
} else {
|
|
|
|
return 0.;
|
|
|
|
}
|
2009-11-23 22:15:34 +00:00
|
|
|
}
|
|
|
|
|
2009-11-25 23:09:38 +00:00
|
|
|
#define PRINT_HR_GENERATOR(name) \
|
|
|
|
void print_##name(struct text_object *obj, char *p, int p_max_size) \
|
|
|
|
{ \
|
|
|
|
(void)obj; \
|
|
|
|
human_readable(info.name * 1024, p, p_max_size); \
|
|
|
|
}
|
|
|
|
|
|
|
|
PRINT_HR_GENERATOR(mem)
|
2010-02-18 17:17:27 +00:00
|
|
|
PRINT_HR_GENERATOR(memwithbuffers)
|
2009-11-25 23:09:38 +00:00
|
|
|
PRINT_HR_GENERATOR(memeasyfree)
|
|
|
|
PRINT_HR_GENERATOR(memfree)
|
|
|
|
PRINT_HR_GENERATOR(memmax)
|
2011-01-04 13:48:33 +00:00
|
|
|
PRINT_HR_GENERATOR(memdirty)
|
2009-11-25 23:09:38 +00:00
|
|
|
PRINT_HR_GENERATOR(swap)
|
|
|
|
PRINT_HR_GENERATOR(swapfree)
|
|
|
|
PRINT_HR_GENERATOR(swapmax)
|
|
|
|
|
2009-11-25 00:33:13 +00:00
|
|
|
uint8_t mem_percentage(struct text_object *obj)
|
|
|
|
{
|
|
|
|
(void)obj;
|
|
|
|
|
|
|
|
return (info.memmax ? round_to_int(info.mem * 100 / info.memmax) : 0);
|
|
|
|
}
|
|
|
|
|
2009-12-04 00:22:36 +00:00
|
|
|
double mem_barval(struct text_object *obj)
|
2009-11-23 22:15:34 +00:00
|
|
|
{
|
|
|
|
(void)obj;
|
|
|
|
|
2009-12-04 00:22:36 +00:00
|
|
|
return info.memmax ? ((double)info.mem / info.memmax) : 0;
|
2009-11-22 17:02:56 +00:00
|
|
|
}
|
2009-11-30 22:55:48 +00:00
|
|
|
|
2010-02-18 17:17:27 +00:00
|
|
|
double mem_with_buffers_barval(struct text_object *obj)
|
|
|
|
{
|
|
|
|
(void)obj;
|
|
|
|
|
|
|
|
return info.memmax ? ((double)info.memwithbuffers / info.memmax) : 0;
|
|
|
|
}
|
|
|
|
|
2009-11-25 00:34:54 +00:00
|
|
|
uint8_t swap_percentage(struct text_object *obj)
|
|
|
|
{
|
|
|
|
(void)obj;
|
|
|
|
|
|
|
|
return (info.swapmax ? round_to_int(info.swap * 100 / info.swapmax) : 0);
|
|
|
|
}
|
|
|
|
|
2009-12-04 00:25:41 +00:00
|
|
|
double swap_barval(struct text_object *obj)
|
2009-11-30 22:55:48 +00:00
|
|
|
{
|
|
|
|
(void)obj;
|
|
|
|
|
2009-12-04 00:25:41 +00:00
|
|
|
return info.swapmax ? ((double)info.swap / info.swapmax) : 0;
|
2009-11-30 22:55:48 +00:00
|
|
|
}
|
2009-11-25 23:12:44 +00:00
|
|
|
|
|
|
|
void print_kernel(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
|
|
|
(void)obj;
|
|
|
|
snprintf(p, p_max_size, "%s", info.uname_s.release);
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_machine(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
|
|
|
(void)obj;
|
|
|
|
snprintf(p, p_max_size, "%s", info.uname_s.machine);
|
|
|
|
}
|
2009-11-25 23:22:25 +00:00
|
|
|
|
|
|
|
void print_nodename(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
|
|
|
(void)obj;
|
|
|
|
snprintf(p, p_max_size, "%s", info.uname_s.nodename);
|
|
|
|
}
|
2009-11-25 23:29:50 +00:00
|
|
|
|
2010-04-14 16:41:25 +00:00
|
|
|
void print_nodename_short(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
|
|
|
(void)obj;
|
|
|
|
snprintf(p, p_max_size, "%s", info.uname_s.nodename);
|
|
|
|
for(int i=0; p[i] != 0; i++) {
|
|
|
|
if(p[i] == '.') {
|
|
|
|
p[i] = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-25 23:29:50 +00:00
|
|
|
void print_sysname(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
|
|
|
(void)obj;
|
|
|
|
snprintf(p, p_max_size, "%s", info.uname_s.sysname);
|
|
|
|
}
|
2009-11-25 23:34:01 +00:00
|
|
|
|
2012-06-28 16:56:01 +00:00
|
|
|
#if defined(__DragonFly__)
|
|
|
|
void print_version(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
|
|
|
(void)obj;
|
|
|
|
snprintf(p, p_max_size, "%s", info.uname_v);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-11-25 23:34:01 +00:00
|
|
|
void print_uptime(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
|
|
|
(void)obj;
|
|
|
|
format_seconds(p, p_max_size, (int)info.uptime);
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_uptime_short(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
|
|
|
(void)obj;
|
|
|
|
format_seconds_short(p, p_max_size, (int)info.uptime);
|
|
|
|
}
|
2009-11-26 00:06:53 +00:00
|
|
|
|
|
|
|
void print_processes(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
|
|
|
(void)obj;
|
|
|
|
spaced_print(p, p_max_size, "%hu", 4, info.procs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_running_processes(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
|
|
|
(void)obj;
|
|
|
|
spaced_print(p, p_max_size, "%hu", 4, info.run_procs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_running_threads(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
|
|
|
(void)obj;
|
|
|
|
spaced_print(p, p_max_size, "%hu", 4, info.run_threads);
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_threads(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
|
|
|
(void)obj;
|
|
|
|
spaced_print(p, p_max_size, "%hu", 4, info.threads);
|
|
|
|
}
|
2009-11-26 00:10:19 +00:00
|
|
|
|
|
|
|
void print_buffers(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
|
|
|
(void)obj;
|
|
|
|
human_readable(info.buffers * 1024, p, p_max_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_cached(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
|
|
|
(void)obj;
|
|
|
|
human_readable(info.cached * 1024, p, p_max_size);
|
|
|
|
}
|
2009-11-26 00:15:33 +00:00
|
|
|
|
|
|
|
void print_evaluate(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
2016-05-20 15:43:17 +00:00
|
|
|
std::vector<char> buf(text_buffer_size.get(*state));
|
2016-05-06 11:24:30 +00:00
|
|
|
evaluate(obj->data.s, &buf[0], buf.size());
|
|
|
|
evaluate(&buf[0], p, p_max_size);
|
2009-11-26 00:15:33 +00:00
|
|
|
}
|
2009-11-26 00:30:58 +00:00
|
|
|
|
|
|
|
int if_empty_iftest(struct text_object *obj)
|
|
|
|
{
|
2010-08-29 18:01:05 +00:00
|
|
|
std::vector<char> buf(max_user_text.get(*state));
|
2009-11-26 00:30:58 +00:00
|
|
|
int result = 1;
|
|
|
|
|
2010-08-29 18:01:05 +00:00
|
|
|
generate_text_internal(&(buf[0]), max_user_text.get(*state), *obj->sub);
|
2009-11-26 00:30:58 +00:00
|
|
|
|
2010-01-04 17:06:14 +00:00
|
|
|
if (strlen(&(buf[0])) != 0) {
|
2009-11-26 00:30:58 +00:00
|
|
|
result = 0;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int check_contains(char *f, char *s)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
FILE *where = open_file(f, 0);
|
|
|
|
|
|
|
|
if (where) {
|
|
|
|
char buf1[256];
|
|
|
|
|
|
|
|
while (fgets(buf1, 256, where)) {
|
|
|
|
if (strstr(buf1, s)) {
|
|
|
|
ret = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(where);
|
|
|
|
} else {
|
|
|
|
NORM_ERR("Could not open the file");
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int if_existing_iftest(struct text_object *obj)
|
|
|
|
{
|
|
|
|
char *spc;
|
2012-12-26 22:22:34 +00:00
|
|
|
int result = 0;
|
2009-11-26 00:30:58 +00:00
|
|
|
|
|
|
|
spc = strchr(obj->data.s, ' ');
|
2012-07-20 11:44:53 +00:00
|
|
|
if(spc != NULL) *spc = 0;
|
|
|
|
if (access(obj->data.s, F_OK) == 0) {
|
|
|
|
if(spc == NULL || check_contains(obj->data.s, spc + 1)) result = 1;
|
2012-12-26 22:22:34 +00:00
|
|
|
}
|
2012-07-20 11:44:53 +00:00
|
|
|
if(spc != NULL) *spc = ' ';
|
2009-11-26 00:30:58 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int if_running_iftest(struct text_object *obj)
|
|
|
|
{
|
|
|
|
#ifdef __linux__
|
|
|
|
if (!get_process_by_name(obj->data.s)) {
|
|
|
|
#else
|
|
|
|
if ((obj->data.s) && system(obj->data.s)) {
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
2009-11-26 00:38:53 +00:00
|
|
|
|
|
|
|
#ifndef __OpenBSD__
|
|
|
|
void print_acpitemp(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
|
|
|
temp_print(p, p_max_size, get_acpi_temperature(obj->data.i), TEMP_CELSIUS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void free_acpitemp(struct text_object *obj)
|
|
|
|
{
|
|
|
|
close(obj->data.i);
|
|
|
|
}
|
|
|
|
#endif /* !__OpenBSD__ */
|
2009-11-26 00:42:54 +00:00
|
|
|
|
|
|
|
void print_freq(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
|
|
|
static int ok = 1;
|
|
|
|
if (ok) {
|
|
|
|
ok = get_freq(p, p_max_size, "%.0f", 1, obj->data.i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_freq_g(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
|
|
|
static int ok = 1;
|
|
|
|
if (ok) {
|
|
|
|
#ifndef __OpenBSD__
|
|
|
|
ok = get_freq(p, p_max_size, "%'.2f", 1000, obj->data.i);
|
|
|
|
#else
|
|
|
|
/* OpenBSD has no such flag (SUSv2) */
|
|
|
|
ok = get_freq(p, p_max_size, "%.2f", 1000, obj->data.i);
|
|
|
|
#endif /* __OpenBSD */
|
|
|
|
}
|
|
|
|
}
|
2009-11-26 00:51:41 +00:00
|
|
|
|
|
|
|
#ifndef __OpenBSD__
|
|
|
|
void print_acpifan(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
|
|
|
(void)obj;
|
|
|
|
get_acpi_fan(p, p_max_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_acpiacadapter(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
2010-01-24 14:34:03 +00:00
|
|
|
get_acpi_ac_adapter(p, p_max_size, (const char *)obj->data.opaque);
|
2009-11-26 00:51:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void print_battery(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
|
|
|
get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_STATUS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_battery_time(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
|
|
|
get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TIME);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t battery_percentage(struct text_object *obj)
|
|
|
|
{
|
|
|
|
return get_battery_perct(obj->data.s);
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_battery_short(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
|
|
|
get_battery_short_status(p, p_max_size, obj->data.s);
|
|
|
|
}
|
|
|
|
#endif /* !__OpenBSD__ */
|
2009-11-29 11:47:11 +00:00
|
|
|
|
2009-11-29 11:56:09 +00:00
|
|
|
|
|
|
|
void print_blink(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
|
|
|
//blinking like this can look a bit ugly if the chars in the font don't have the same width
|
2010-08-29 18:01:05 +00:00
|
|
|
std::vector<char> buf(max_user_text.get(*state));
|
2009-11-29 11:56:09 +00:00
|
|
|
static int visible = 1;
|
|
|
|
static int last_len = 0;
|
2009-12-01 11:35:23 +00:00
|
|
|
int i;
|
2009-11-29 11:56:09 +00:00
|
|
|
|
|
|
|
if (visible) {
|
2010-08-29 18:01:05 +00:00
|
|
|
generate_text_internal(&(buf[0]), max_user_text.get(*state), *obj->sub);
|
2010-01-04 17:06:14 +00:00
|
|
|
last_len = strlen(&(buf[0]));
|
2009-11-29 11:56:09 +00:00
|
|
|
} else {
|
2009-12-01 11:35:23 +00:00
|
|
|
for (i = 0; i < last_len; i++)
|
2009-11-29 11:56:09 +00:00
|
|
|
buf[i] = ' ';
|
|
|
|
}
|
|
|
|
|
2010-01-04 17:06:14 +00:00
|
|
|
snprintf(p, p_max_size, "%s", &(buf[0]));
|
2009-11-29 11:56:09 +00:00
|
|
|
visible = !visible;
|
|
|
|
}
|
2009-11-29 11:59:04 +00:00
|
|
|
|
|
|
|
void print_include(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
2010-08-29 18:01:05 +00:00
|
|
|
std::vector<char> buf(max_user_text.get(*state));
|
2009-11-29 11:59:04 +00:00
|
|
|
|
|
|
|
if (!obj->sub)
|
|
|
|
return;
|
|
|
|
|
2010-08-29 18:01:05 +00:00
|
|
|
generate_text_internal(&(buf[0]), max_user_text.get(*state), *obj->sub);
|
2010-01-04 17:06:14 +00:00
|
|
|
snprintf(p, p_max_size, "%s", &(buf[0]));
|
2009-11-29 11:59:04 +00:00
|
|
|
}
|
2009-11-29 18:48:24 +00:00
|
|
|
|
2010-04-19 12:54:03 +00:00
|
|
|
#ifdef BUILD_CURL
|
2010-04-19 12:45:05 +00:00
|
|
|
void print_stock(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
|
|
|
if( ! obj->data.s) {
|
|
|
|
p[0] = 0;
|
|
|
|
return;
|
|
|
|
}
|
2010-04-22 11:52:55 +00:00
|
|
|
ccurl_process_info(p, p_max_size, obj->data.s, 1);
|
2010-04-19 12:45:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void free_stock(struct text_object *obj)
|
|
|
|
{
|
|
|
|
free(obj->data.s);
|
|
|
|
}
|
2010-04-19 12:54:03 +00:00
|
|
|
#endif /* BUILD_CURL */
|
2010-04-19 12:45:05 +00:00
|
|
|
|
2010-01-04 17:06:14 +00:00
|
|
|
void print_to_bytes(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
2010-08-29 18:01:05 +00:00
|
|
|
std::vector<char> buf(max_user_text.get(*state));
|
2016-03-23 07:42:16 +00:00
|
|
|
long double bytes;
|
2010-01-04 17:06:14 +00:00
|
|
|
char unit[16]; // 16 because we can also have long names (like mega-bytes)
|
|
|
|
|
2010-08-29 18:01:05 +00:00
|
|
|
generate_text_internal(&(buf[0]), max_user_text.get(*state), *obj->sub);
|
2016-03-23 07:42:16 +00:00
|
|
|
if(sscanf(&(buf[0]), "%Lf%s", &bytes, unit) == 2 && strlen(unit) < 16){
|
|
|
|
if(strncasecmp("b", unit, 1) == 0) snprintf(&(buf[0]), max_user_text.get(*state), "%Lf", bytes);
|
|
|
|
else if(strncasecmp("k", unit, 1) == 0) snprintf(&(buf[0]), max_user_text.get(*state), "%Lf", bytes * 1024);
|
|
|
|
else if(strncasecmp("m", unit, 1) == 0) snprintf(&(buf[0]), max_user_text.get(*state), "%Lf", bytes * 1024 * 1024);
|
|
|
|
else if(strncasecmp("g", unit, 1) == 0) snprintf(&(buf[0]), max_user_text.get(*state), "%Lf", bytes * 1024 * 1024 * 1024);
|
|
|
|
else if(strncasecmp("t", unit, 1) == 0) snprintf(&(buf[0]), max_user_text.get(*state), "%Lf", bytes * 1024 * 1024 * 1024 * 1024);
|
2010-01-04 17:06:14 +00:00
|
|
|
}
|
|
|
|
snprintf(p, p_max_size, "%s", &(buf[0]));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-29 18:48:24 +00:00
|
|
|
void print_updates(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
|
|
|
(void)obj;
|
|
|
|
snprintf(p, p_max_size, "%d", get_total_updates());
|
|
|
|
}
|
2009-11-29 18:50:59 +00:00
|
|
|
|
|
|
|
int updatenr_iftest(struct text_object *obj)
|
|
|
|
{
|
|
|
|
if (get_total_updates() % get_updatereset() != obj->data.i - 1)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|