Update copyright stuff, fix conky.conf weirdness.
[monky] / src / linux.c
index f3a8ef9..4703304 100644 (file)
@@ -8,7 +8,7 @@
  *
  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
  * Copyright (c) 2007 Toni Spets
- * Copyright (c) 2005-2008 Brenden Matthews, Philip Kovacs, et. al.
+ * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
  *     (see AUTHORS)
  * All rights reserved.
  *
@@ -211,48 +211,6 @@ char *get_ioscheduler(char *disk)
        return strndup("n/a", text_buffer_size);
 }
 
-int interface_up(const char *dev)
-{
-       int fd;
-       struct ifreq ifr;
-
-       if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
-               CRIT_ERR("could not create sockfd");
-               return 0;
-       }
-       strncpy(ifr.ifr_name, dev, IFNAMSIZ);
-       if (ioctl(fd, SIOCGIFFLAGS, &ifr)) {
-               /* if device does not exist, treat like not up */
-               if (errno != ENODEV)
-                       perror("SIOCGIFFLAGS");
-               goto END_FALSE;
-       }
-
-       if (!(ifr.ifr_flags & IFF_UP)) /* iface is not up */
-               goto END_FALSE;
-       if (ifup_strictness == IFUP_UP)
-               goto END_TRUE;
-
-       if (!(ifr.ifr_flags & IFF_RUNNING))
-               goto END_FALSE;
-       if (ifup_strictness == IFUP_LINK)
-               goto END_TRUE;
-
-       if (ioctl(fd, SIOCGIFADDR, &ifr)) {
-               perror("SIOCGIFADDR");
-               goto END_FALSE;
-       }
-       if (((struct sockaddr_in *)&(ifr.ifr_ifru.ifru_addr))->sin_addr.s_addr)
-               goto END_TRUE;
-
-END_FALSE:
-       close(fd);
-       return 0;
-END_TRUE:
-       close(fd);
-       return 1;
-}
-
 #define COND_FREE(x) if(x) free(x); x = 0
 #define SAVE_SET_STRING(x, y) \
        if (x && strcmp((char *)x, (char *)y)) { \
@@ -319,6 +277,7 @@ void update_net_stats(void)
 {
        FILE *net_dev_fp;
        static int rep = 0;
+       static char first = 1;
 
        // FIXME: arbitrary size chosen to keep code simple.
        int i, i2;
@@ -380,10 +339,7 @@ void update_net_stats(void)
                ns->up = 1;
                memset(&(ns->addr.sa_data), 0, 14);
 
-               if(NULL == ns->addrs)
-                       ns->addrs = (char*) malloc(17 * 16 + 1);
-               if(NULL != ns->addrs)
-                       memset(ns->addrs, 0, 17 * 16 + 1); /* Up to 17 chars per ip, max 16 interfaces. Nasty memory usage... */
+               memset(ns->addrs, 0, 17 * 16 + 1); /* Up to 17 chars per ip, max 16 interfaces. Nasty memory usage... */
 
                last_recv = ns->recv;
                last_trans = ns->trans;
@@ -423,17 +379,15 @@ void update_net_stats(void)
                                break;
 
                        ns2 = get_net_stat(
-                               ((struct ifreq *) conf.ifc_buf)[k].ifr_ifrn.ifrn_name);
+                                       ((struct ifreq *) conf.ifc_buf)[k].ifr_ifrn.ifrn_name);
                        ns2->addr = ((struct ifreq *) conf.ifc_buf)[k].ifr_ifru.ifru_addr;
-                       if(NULL != ns2->addrs) {
-                               sprintf(temp_addr, "%u.%u.%u.%u, ",
+                       sprintf(temp_addr, "%u.%u.%u.%u, ",
                                        ns2->addr.sa_data[2] & 255,
                                        ns2->addr.sa_data[3] & 255,
                                        ns2->addr.sa_data[4] & 255,
                                        ns2->addr.sa_data[5] & 255);
-                               if(NULL == strstr(ns2->addrs, temp_addr))
-                                       strncpy(ns2->addrs + strlen(ns2->addrs), temp_addr, 17);
-                       }
+                       if(NULL == strstr(ns2->addrs, temp_addr))
+                               strncpy(ns2->addrs + strlen(ns2->addrs), temp_addr, 17);
                }
 
                close((long) i);
@@ -442,9 +396,12 @@ void update_net_stats(void)
 
                /*** end ip addr patch ***/
 
-               /* calculate speeds */
-               ns->net_rec[0] = (ns->recv - last_recv) / delta;
-               ns->net_trans[0] = (ns->trans - last_trans) / delta;
+               if (!first) {
+                       /* calculate speeds */
+                       ns->net_rec[0] = (ns->recv - last_recv) / delta;
+                       ns->net_trans[0] = (ns->trans - last_trans) / delta;
+               }
+
                curtmp1 = 0;
                curtmp2 = 0;
                // get an average
@@ -525,6 +482,7 @@ void update_net_stats(void)
                free(winfo);
 #endif
        }
+       first = 0;
 
        fclose(net_dev_fp);
 
@@ -1665,8 +1623,8 @@ void get_battery_stuff(char *buffer, unsigned int n, const char *bat, int item)
                        else
                                strncpy(last_battery_str[idx], "AC", 64);
                }
-       } else if (acpi_bat_fp[idx] != NULL) {
-               /* ACPI */
+       } else if (acpi_bat_fp[idx] != NULL) {
+               /* ACPI */
                int present_rate = -1;
                int remaining_capacity = -1;
                char charging_state[64];
@@ -1688,7 +1646,7 @@ void get_battery_stuff(char *buffer, unsigned int n, const char *bat, int item)
                                                break;
                                        }
                                        if (sscanf(b, "last full capacity: %d",
-                                                       &acpi_last_full[idx]) != 0) {
+                                                               &acpi_last_full[idx]) != 0) {
                                                break;
                                        }
                                }
@@ -1728,54 +1686,54 @@ void get_battery_stuff(char *buffer, unsigned int n, const char *bat, int item)
                /* not present */
                if (strcmp(present, "no") == 0) {
                        strncpy(last_battery_str[idx], "not present", 64);
-               /* charging */
+                       /* charging */
                } else if (strcmp(charging_state, "charging") == 0) {
                        if (acpi_last_full[idx] != 0 && present_rate > 0) {
                                /* e.g. charging 75% */
                                snprintf(last_battery_str[idx],
-                                       sizeof(last_battery_str[idx]) - 1, "charging %i%%",
-                                       (int) ((remaining_capacity * 100) / acpi_last_full[idx]));
+                                               sizeof(last_battery_str[idx]) - 1, "charging %i%%",
+                                               (int) ((remaining_capacity * 100) / acpi_last_full[idx]));
                                /* e.g. 2h 37m */
                                format_seconds(last_battery_time_str[idx],
-                                       sizeof(last_battery_time_str[idx]) - 1,
-                                       (long) (((acpi_last_full[idx] - remaining_capacity) *
-                                       3600) / present_rate));
+                                               sizeof(last_battery_time_str[idx]) - 1,
+                                               (long) (((acpi_last_full[idx] - remaining_capacity) *
+                                                               3600) / present_rate));
                        } else if (acpi_last_full[idx] != 0 && present_rate <= 0) {
                                snprintf(last_battery_str[idx],
-                                       sizeof(last_battery_str[idx]) - 1, "charging %d%%",
-                                       (int) ((remaining_capacity * 100) / acpi_last_full[idx]));
+                                               sizeof(last_battery_str[idx]) - 1, "charging %d%%",
+                                               (int) ((remaining_capacity * 100) / acpi_last_full[idx]));
                                snprintf(last_battery_time_str[idx],
-                                       sizeof(last_battery_time_str[idx]) - 1, "unknown");
+                                               sizeof(last_battery_time_str[idx]) - 1, "unknown");
                        } else {
                                strncpy(last_battery_str[idx], "charging",
-                                       sizeof(last_battery_str[idx]) - 1);
+                                               sizeof(last_battery_str[idx]) - 1);
                                snprintf(last_battery_time_str[idx],
-                                       sizeof(last_battery_time_str[idx]) - 1, "unknown");
+                                               sizeof(last_battery_time_str[idx]) - 1, "unknown");
                        }
-               /* discharging */
+                       /* discharging */
                } else if (strncmp(charging_state, "discharging", 64) == 0) {
                        if (present_rate > 0) {
                                /* e.g. discharging 35% */
                                snprintf(last_battery_str[idx],
-                                       sizeof(last_battery_str[idx]) - 1, "discharging %i%%",
-                                       (int) ((remaining_capacity * 100) / acpi_last_full[idx]));
+                                               sizeof(last_battery_str[idx]) - 1, "discharging %i%%",
+                                               (int) ((remaining_capacity * 100) / acpi_last_full[idx]));
                                /* e.g. 1h 12m */
                                format_seconds(last_battery_time_str[idx],
-                                       sizeof(last_battery_time_str[idx]) - 1,
-                                       (long) ((remaining_capacity * 3600) / present_rate));
+                                               sizeof(last_battery_time_str[idx]) - 1,
+                                               (long) ((remaining_capacity * 3600) / present_rate));
                        } else if (present_rate == 0) { /* Thanks to Nexox for this one */
                                snprintf(last_battery_str[idx],
-                                       sizeof(last_battery_str[idx]) - 1, "full");
+                                               sizeof(last_battery_str[idx]) - 1, "full");
                                snprintf(last_battery_time_str[idx],
-                                       sizeof(last_battery_time_str[idx]) - 1, "unknown");
+                                               sizeof(last_battery_time_str[idx]) - 1, "unknown");
                        } else {
                                snprintf(last_battery_str[idx],
-                                       sizeof(last_battery_str[idx]) - 1, "discharging %d%%",
-                                       (int) ((remaining_capacity * 100) / acpi_last_full[idx]));
+                                               sizeof(last_battery_str[idx]) - 1, "discharging %d%%",
+                                               (int) ((remaining_capacity * 100) / acpi_last_full[idx]));
                                snprintf(last_battery_time_str[idx],
-                                       sizeof(last_battery_time_str[idx]) - 1, "unknown");
+                                               sizeof(last_battery_time_str[idx]) - 1, "unknown");
                        }
-               /* charged */
+                       /* charged */
                } else if (strncmp(charging_state, "charged", 64) == 0) {
                        /* thanks to Lukas Zapletal <lzap@seznam.cz> */
                        /* Below happens with the second battery on my X40,
@@ -1785,12 +1743,14 @@ void get_battery_stuff(char *buffer, unsigned int n, const char *bat, int item)
                        } else {
                                strcpy(last_battery_str[idx], "charged");
                        }
-               /* unknown, probably full / AC */
+                       /* unknown, probably full / AC */
                } else {
-                       if (acpi_last_full[idx] != 0
+                       if (strncmp(charging_state, "Full", 64) == 0) {
+                               strncpy(last_battery_str[idx], "full", 64);
+                       } else if (acpi_last_full[idx] != 0
                                        && remaining_capacity != acpi_last_full[idx]) {
                                snprintf(last_battery_str[idx], 64, "unknown %d%%",
-                                       (int) ((remaining_capacity * 100) / acpi_last_full[idx]));
+                                               (int) ((remaining_capacity * 100) / acpi_last_full[idx]));
                        } else {
                                strncpy(last_battery_str[idx], "AC", 64);
                        }
@@ -1842,6 +1802,18 @@ void set_return_value(char *buffer, unsigned int n, int item, int idx)
        }
 }
 
+void get_battery_short_status(char *buffer, unsigned int n, const char *bat)
+{
+       get_battery_stuff(buffer, n, bat, BATTERY_STATUS);
+       if (0 == strncmp("charging", buffer, 8)) {
+               buffer[0] = 'C';
+               memmove(buffer + 1, buffer + 8, n - 8);
+       } else if (0 == strncmp("discharging", buffer, 11)) {
+               buffer[0] = 'D';
+               memmove(buffer + 1, buffer + 11, n - 11);
+       }
+}
+
 int get_battery_perct(const char *bat)
 {
        static int rep = 0;
@@ -2090,232 +2062,6 @@ void update_top(void)
        info.first_process = get_first_process();
 }
 
-/* Here come the IBM ACPI-specific things. For reference, see
- * http://ibm-acpi.sourceforge.net/README
- * If IBM ACPI is installed, /proc/acpi/ibm contains the following files:
-bay
-beep
-bluetooth
-brightness
-cmos
-dock
-driver
-ecdump
-fan
-hotkey
-led
-light
-thermal
-video
-volume
- * The content of these files is described in detail in the aforementioned
- * README - some of them also in the following functions accessing them.
- * Peter Tarjan (ptarjan@citromail.hu) */
-
-#define IBM_ACPI_DIR "/proc/acpi/ibm"
-
-/* get fan speed on IBM/Lenovo laptops running the ibm acpi.
- * /proc/acpi/ibm/fan looks like this (3 lines):
-status:         disabled
-speed:          2944
-commands:       enable, disable
- * Peter Tarjan (ptarjan@citromail.hu) */
-
-void get_ibm_acpi_fan(char *p_client_buffer, size_t client_buffer_size)
-{
-       FILE *fp;
-       unsigned int speed = 0;
-       char fan[128];
-
-       if (!p_client_buffer || client_buffer_size <= 0) {
-               return;
-       }
-
-       snprintf(fan, 127, "%s/fan", IBM_ACPI_DIR);
-
-       fp = fopen(fan, "r");
-       if (fp != NULL) {
-               while (!feof(fp)) {
-                       char line[256];
-
-                       if (fgets(line, 255, fp) == NULL) {
-                               break;
-                       }
-                       if (sscanf(line, "speed: %u", &speed)) {
-                               break;
-                       }
-               }
-       } else {
-               CRIT_ERR("can't open '%s': %s\nYou are not using the IBM ACPI. Remove "
-                       "ibm* from your "PACKAGE_NAME" config file.", fan, strerror(errno));
-       }
-
-       fclose(fp);
-       snprintf(p_client_buffer, client_buffer_size, "%d", speed);
-}
-
-/* get the measured temperatures from the temperature sensors
- * on IBM/Lenovo laptops running the ibm acpi.
- * There are 8 values in /proc/acpi/ibm/thermal, and according to
- * http://ibm-acpi.sourceforge.net/README
- * these mean the following (at least on an IBM R51...)
- * 0:  CPU (also on the T series laptops)
- * 1:  Mini PCI Module (?)
- * 2:  HDD (?)
- * 3:  GPU (also on the T series laptops)
- * 4:  Battery (?)
- * 5:  N/A
- * 6:  Battery (?)
- * 7:  N/A
- * I'm not too sure about those with the question mark, but the values I'm
- * reading from *my* thermal file (on a T42p) look realistic for the
- * hdd and the battery.
- * #5 and #7 are always -128.
- * /proc/acpi/ibm/thermal looks like this (1 line):
-temperatures:   41 43 31 46 33 -128 29 -128
- * Peter Tarjan (ptarjan@citromail.hu) */
-
-static double last_ibm_acpi_temp_time;
-void get_ibm_acpi_temps(void)
-{
-
-       FILE *fp;
-       char thermal[128];
-
-       /* don't update too often */
-       if (current_update_time - last_ibm_acpi_temp_time < 10.00) {
-               return;
-       }
-       last_ibm_acpi_temp_time = current_update_time;
-
-       /* if (!p_client_buffer || client_buffer_size <= 0) {
-               return;
-       } */
-
-       snprintf(thermal, 127, "%s/thermal", IBM_ACPI_DIR);
-       fp = fopen(thermal, "r");
-
-       if (fp != NULL) {
-               while (!feof(fp)) {
-                       char line[256];
-
-                       if (fgets(line, 255, fp) == NULL) {
-                               break;
-                       }
-                       if (sscanf(line, "temperatures: %d %d %d %d %d %d %d %d",
-                                       &ibm_acpi.temps[0], &ibm_acpi.temps[1], &ibm_acpi.temps[2],
-                                       &ibm_acpi.temps[3], &ibm_acpi.temps[4], &ibm_acpi.temps[5],
-                                       &ibm_acpi.temps[6], &ibm_acpi.temps[7])) {
-                               break;
-                       }
-               }
-       } else {
-               CRIT_ERR("can't open '%s': %s\nYou are not using the IBM ACPI. Remove "
-                       "ibm* from your "PACKAGE_NAME" config file.", thermal, strerror(errno));
-       }
-
-       fclose(fp);
-}
-
-/* get volume (0-14) on IBM/Lenovo laptops running the ibm acpi.
- * "Volume" here is none of the mixer volumes, but a "master of masters"
- * volume adjusted by the IBM volume keys.
- * /proc/acpi/ibm/fan looks like this (4 lines):
-level:          4
-mute:           off
-commands:       up, down, mute
-commands:       level <level> (<level> is 0-15)
- * Peter Tarjan (ptarjan@citromail.hu) */
-
-void get_ibm_acpi_volume(char *p_client_buffer, size_t client_buffer_size)
-{
-       FILE *fp;
-       char volume[128];
-       unsigned int vol = -1;
-       char mute[3] = "";
-
-       if (!p_client_buffer || client_buffer_size <= 0) {
-               return;
-       }
-
-       snprintf(volume, 127, "%s/volume", IBM_ACPI_DIR);
-
-       fp = fopen(volume, "r");
-       if (fp != NULL) {
-               while (!feof(fp)) {
-                       char line[256];
-                       unsigned int read_vol = -1;
-
-                       if (fgets(line, 255, fp) == NULL) {
-                               break;
-                       }
-                       if (sscanf(line, "level: %u", &read_vol)) {
-                               vol = read_vol;
-                               continue;
-                       }
-                       if (sscanf(line, "mute: %s", mute)) {
-                               break;
-                       }
-               }
-       } else {
-               CRIT_ERR("can't open '%s': %s\nYou are not using the IBM ACPI. Remove "
-                       "ibm* from your "PACKAGE_NAME" config file.", volume, strerror(errno));
-       }
-
-       fclose(fp);
-
-       if (strcmp(mute, "on") == 0) {
-               snprintf(p_client_buffer, client_buffer_size, "%s", "mute");
-               return;
-       } else {
-               snprintf(p_client_buffer, client_buffer_size, "%d", vol);
-               return;
-       }
-}
-
-/* static FILE *fp = NULL; */
-
-/* get LCD brightness on IBM/Lenovo laptops running the ibm acpi.
- * /proc/acpi/ibm/brightness looks like this (3 lines):
-level:          7
-commands:       up, down
-commands:       level <level> (<level> is 0-7)
- * Peter Tarjan (ptarjan@citromail.hu) */
-
-void get_ibm_acpi_brightness(char *p_client_buffer, size_t client_buffer_size)
-{
-       FILE *fp;
-       unsigned int brightness = 0;
-       char filename[128];
-
-       if (!p_client_buffer || client_buffer_size <= 0) {
-               return;
-       }
-
-       snprintf(filename, 127, "%s/brightness", IBM_ACPI_DIR);
-
-       fp = fopen(filename, "r");
-       if (fp != NULL) {
-               while (!feof(fp)) {
-                       char line[256];
-
-                       if (fgets(line, 255, fp) == NULL) {
-                               break;
-                       }
-                       if (sscanf(line, "level: %u", &brightness)) {
-                               break;
-                       }
-               }
-       } else {
-               CRIT_ERR("can't open '%s': %s\nYou are not using the IBM ACPI. Remove "
-                       "ibm* from your "PACKAGE_NAME" config file.", filename, strerror(errno));
-       }
-
-       fclose(fp);
-
-       snprintf(p_client_buffer, client_buffer_size, "%d", brightness);
-}
-
 void update_entropy(void)
 {
        static int rep = 0;