From 3a006806894291cd9b01dd285f66ad5431cf81d6 Mon Sep 17 00:00:00 2001 From: Guido Falsi Date: Tue, 4 Oct 2016 09:47:57 +0200 Subject: [PATCH] FreeBSD changes (#327) * llabs() is included in FreeBSD since FreeBSD 5.0. Oldest supported FreeBSD version is 9.3 now. * stdio.h needs to be explicitly included here for FreeBSD. I can wrap it in #ifdefs if needed, but I don't think it can hurt other OSes. * addr config variable works fine on FreeBSD. * FreeBSD 5.x is really an ancient version and long unsupported, so I think this check for it can be dropped. * Fix and simplify code to get battery state and charge. * Add required include on FreeBSD. * Add needed include on FreeBSD. * Also populate basename to avoid printing (null) process names. Repored by fellow FreeBSD user Szabolcs Grof. --- src/c++wrap.cc | 1 + src/conky.cc | 7 ------- src/core.cc | 5 +++-- src/freebsd.cc | 38 +++++++++++--------------------------- src/freebsd.h | 1 + src/luamm.hh | 2 ++ 6 files changed, 18 insertions(+), 36 deletions(-) diff --git a/src/c++wrap.cc b/src/c++wrap.cc index d4a0ec68..fcb2713f 100644 --- a/src/c++wrap.cc +++ b/src/c++wrap.cc @@ -26,6 +26,7 @@ #include "c++wrap.hh" #include +#include /* force use of POSIX strerror_r instead of non-portable GNU specific */ #ifdef _GNU_SOURCE diff --git a/src/conky.cc b/src/conky.cc index f7be3f70..6522ce1d 100644 --- a/src/conky.cc +++ b/src/conky.cc @@ -728,13 +728,6 @@ int percent_print(char *buf, int size, unsigned value) return spaced_print(buf, size, "%u", pad_percents.get(*state), value); } -#if defined(__FreeBSD__) -unsigned long long llabs(long long num) { - if(num < 0) return -num; - else return num; -} -#endif - /* converts from bytes to human readable format (K, M, G, T) * * The algorithm always divides by 1024, as unit-conversion of byte diff --git a/src/core.cc b/src/core.cc index d4b35967..3bbeb328 100644 --- a/src/core.cc +++ b/src/core.cc @@ -898,11 +898,12 @@ struct text_object *construct_text_object(char *s, const char *arg, return NULL; } } else -#ifdef __linux__ OBJ(addr, &update_net_stats) parse_net_stat_arg(obj, arg, free_at_crash); obj->callbacks.print = &print_addr; - END OBJ(addrs, &update_net_stats) + END +#ifdef __linux__ + OBJ(addrs, &update_net_stats) parse_net_stat_arg(obj, arg, free_at_crash); obj->callbacks.print = &print_addrs; #ifdef BUILD_IPV6 diff --git a/src/freebsd.cc b/src/freebsd.cc index 171e56d0..37be1592 100644 --- a/src/freebsd.cc +++ b/src/freebsd.cc @@ -295,11 +295,7 @@ int update_running_processes(void) std::lock_guard guard(kvm_proc_mutex); p = kvm_getprocs(kd, KERN_PROC_ALL, 0, &n_processes); for (i = 0; i < n_processes; i++) { -#if (__FreeBSD__ < 5) && !defined(__FreeBSD_kernel__) - if (p[i].kp_proc.p_stat == SRUN) { -#else if (p[i].ki_stat == SRUN) { -#endif cnt++; } } @@ -475,7 +471,7 @@ void get_battery_stuff(char *buf, unsigned int n, const char *bat, int item) break; case BATTERY_STATUS: if (batstate == 1) // Discharging - snprintf(buf, n, "remaining %d%%", batcapacity); + snprintf(buf, n, "remaining (%d%%)", batcapacity); else snprintf(buf, n, batstate == 2 ? "charging (%d%%)" : (batstate == 7 ? "absent/on AC" : "charged (%d%%)"), @@ -508,26 +504,10 @@ static int check_bat(const char *bat) int get_battery_perct(const char *bat) { - union acpi_battery_ioctl_arg battio; - int batnum, acpifd; - int designcap, lastfulcap, batperct; + int batcapacity; - if ((battio.unit = batnum = check_bat(bat)) < 0) - return 0; - if ((acpifd = open("/dev/acpi", O_RDONLY)) < 0) { - fprintf(stderr, "Can't open ACPI device\n"); - return 0; - } - if (ioctl(acpifd, ACPIIO_BATT_GET_BIF, &battio) == -1) { - fprintf(stderr, "Unable to get info for battery unit %d\n", batnum); - return 0; - } - close(acpifd); - designcap = battio.bif.dcap; - lastfulcap = battio.bif.lfcap; - batperct = (designcap > 0 && lastfulcap > 0) ? - (int) (((float) lastfulcap / designcap) * 100) : 0; - return batperct > 100 ? 100 : batperct; + get_battery_stats(NULL, &batcapacity, NULL, NULL); + return batcapacity; } double get_battery_perct_bar(struct text_object *obj) @@ -729,6 +709,7 @@ void get_top_info(void) proc->time_stamp = g_time; proc->name = strndup(p[i].ki_comm, text_buffer_size.get(*state)); + proc->basename = strndup(p[i].ki_comm, text_buffer_size.get(*state)); proc->amount = 100.0 * p[i].ki_pctcpu / FSCALE; proc->vsize = p[i].ki_size; proc->rss = (p[i].ki_rssize * getpagesize()); @@ -745,11 +726,14 @@ void get_battery_short_status(char *buffer, unsigned int n, const char *bat) if (0 == strncmp("charging", buffer, 8)) { buffer[0] = 'C'; memmove(buffer + 1, buffer + 8, n - 8); - } else if (0 == strncmp("discharging", buffer, 11)) { + } else if (0 == strncmp("remaining", buffer, 9)) { buffer[0] = 'D'; - memmove(buffer + 1, buffer + 11, n - 11); + memmove(buffer + 1, buffer + 9, n - 9); + } else if (0 == strncmp("charged", buffer, 7)) { + buffer[0] = 'F'; + memmove(buffer + 1, buffer + 7, n - 7); } else if (0 == strncmp("absent/on AC", buffer, 12)) { - buffer[0] = 'A'; + buffer[0] = 'N'; memmove(buffer + 1, buffer + 12, n - 12); } } diff --git a/src/freebsd.h b/src/freebsd.h index 790ec3e9..b6d4df85 100644 --- a/src/freebsd.h +++ b/src/freebsd.h @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #if (defined(i386) || defined(__i386__)) diff --git a/src/luamm.hh b/src/luamm.hh index 801c6a03..8973e95f 100644 --- a/src/luamm.hh +++ b/src/luamm.hh @@ -25,7 +25,9 @@ #include #include #include +#include #include +#include #include