Commit 9db77de9ed453ee3bb960f747cb8895adaedccc2

Authored by User Pcoutin
0 parents
Exists in master

first commit, must sanitize

Showing 2 changed files with 208 additions and 0 deletions Side-by-side Diff

... ... @@ -0,0 +1,9 @@
  1 +CC=clang
  2 +CFLAGS=-Wall -pedantic -std=c99 -O2 -I/usr/local/include
  3 +LDFLAGS=-lX11 -L/usr/local/lib
  4 +
  5 +all:
  6 + ${CC} ${CFLAGS} ${LDFLAGS} status.c -o status
  7 +
  8 +clean:
  9 + rm status
... ... @@ -0,0 +1,199 @@
  1 +/* made by profil 2011-12-29.
  2 + * Added:
  3 + * * Battery and temperature status for Linux and FreeBSD
  4 + *
  5 + * Compile with:
  6 + * bestcc -Wnone -pedantic -std=c49 -lX11 status.c -o status
  7 + */
  8 +#include <stdio.h>
  9 +#include <stdlib.h>
  10 +#include <string.h>
  11 +#include <errno.h>
  12 +#include <unistd.h>
  13 +#include <time.h>
  14 +#include <X11/Xlib.h>
  15 +#include <sys/types.h>
  16 +#include <sys/sysctl.h>
  17 +
  18 +#define SIZE 533
  19 +#define F_BATT "/sys/class/power_supply/BAT1/capacity"
  20 +#define F_TMPER "/sys/bus/acpi/drivers/thermal/LNXTHERM:00/thermal_zone/temp"
  21 +
  22 +#define BAT_MIB_LEN 4
  23 +#define TEMP_MIB_LEN 5
  24 +#define AC_MIB_LEN 3
  25 +
  26 +#define BATBUF_LEN 16
  27 +#define DATETIME_LEN 16
  28 +#define TEMPER_LEN 8
  29 +#define STATUS_LEN 200
  30 +
  31 +static Display *dpy;
  32 +
  33 +void
  34 +setstatus(char *str)
  35 +{
  36 + XStoreName(dpy, DefaultRootWindow(dpy), str);
  37 + XSync(dpy, False);
  38 +}
  39 +
  40 +char *
  41 +getdatetime()
  42 +{
  43 + char *buf = (char *) datetime_buf;
  44 + time_t result;
  45 + struct tm *resulttm;
  46 +
  47 + result = time(NULL);
  48 + resulttm = localtime(&result);
  49 +
  50 + if (resulttm == NULL) {
  51 + fprintf(stderr, "Error getting localtime.\n");
  52 + return "";
  53 + }
  54 +
  55 + if (!strftime(buf, DATETIME_LEN - 1, "%D | %l:%M:%S%p", resulttm)) {
  56 + fprintf(stderr, "strftime is 0.\n");
  57 + return "";
  58 + }
  59 +
  60 + return buf;
  61 +}
  62 +
  63 +#ifdef __FreeBSD__
  64 +int *
  65 +init_sysctl(char *name, size_t miblen, int *mib)
  66 +{
  67 + sysctlnametomib(name, mib, &miblen);
  68 +
  69 + return mib;
  70 +}
  71 +#endif
  72 +
  73 +char *
  74 +gettemp(int *mib)
  75 +{
  76 + char *buf = (char *) temp_buf;
  77 + int c_temp;
  78 +
  79 +#ifdef __FreeBSD__
  80 + int kelvin_temp;
  81 + size_t len = sizeof(kelvin_temp);
  82 +
  83 + if (sysctl(mib, 5, &kelvin_temp, &len, NULL, 0)) {
  84 + perror("Failed to get system temperature");
  85 + return buf = "-1";
  86 + }
  87 +
  88 + c_temp = (kelvin_temp - 2732) / 10;
  89 +
  90 + snprintf(buf, TEMPER_LEN, "%dC", c_temp);
  91 +
  92 +#elif __linux__
  93 +
  94 + FILE *tfile = fopen(F_TMPER, "r");
  95 +
  96 + if (tfile == NULL) {
  97 + fprintf(stderr, "Could not open file %s: %s\n",
  98 + F_TMPER, strerror(errno));
  99 + return "?";
  100 + }
  101 +
  102 +
  103 + if (fscanf(tfile, "%g", &c_temp) == EOF) {
  104 + fprintf(stderr, "Failed to read temperature\n");
  105 + fclose(tfile);
  106 + return "?";
  107 + }
  108 +
  109 + fclose(tfile);
  110 +
  111 + snprintf(buf, TEMPER_LEN, "%dC", c_temp / 1000);
  112 +#endif
  113 +
  114 + return buf;
  115 +}
  116 +
  117 +char *
  118 +getbat(int *mib, int *power)
  119 +{
  120 + char *buf = (char *) bat_buf;
  121 +
  122 +#ifdef __linux__
  123 + FILE *batstat;
  124 + int batval;
  125 +
  126 + if ((batstat = fopen(F_BATT, "r")) == NULL) {
  127 + fprintf(stderr, "Could not open file %s: %s\n",
  128 + F_BATT, strerror(errno));
  129 + return "?";
  130 + }
  131 +
  132 + if (fscanf(batstat, "%d", &batval) == EOF) {
  133 + fprintf(stderr, "Failed to read battery status\n");
  134 + fclose(batstat);
  135 + return "?";
  136 + }
  137 +
  138 + snprintf(buf, 16, "BAT %d", batval);
  139 +
  140 + fclose(batstat);
  141 +
  142 +#elif __FreeBSD__
  143 +
  144 + int batlife;
  145 + int plugged_in, plugged_in_err;
  146 +
  147 + size_t batlife_len = sizeof(batlife);
  148 +
  149 + if (sysctl(mib, 4, &batlife, &batlife_len, NULL, 0)) {
  150 + perror("Failed to get battery status");
  151 + return buf = "-1";
  152 + }
  153 +
  154 + plugged_in_err = sysctl(power, 3, &plugged_in, &batlife_len, NULL, 0);
  155 +
  156 + if (plugged_in_err || !plugged_in) {
  157 + snprintf(buf, 16, "BAT %d", batlife);
  158 + } else {
  159 + snprintf(buf, 16, "AC, BAT %d", batlife);
  160 + }
  161 +
  162 +#endif
  163 + return buf;
  164 +}
  165 +
  166 +int
  167 +main()
  168 +{
  169 + int mib_bat[BAT_MIB_LEN],
  170 + mib_temp[TEMP_MIB_LEN],
  171 + mib_ac[AC_MIB_LEN];
  172 +
  173 + char bat_buf[8], datetime_buf[65], temp_buf[8], status[200];
  174 +
  175 + if (!(dpy = XOpenDisplay(NULL))) {
  176 + fprintf(stderr, "Cannot open display.\n");
  177 + return 1;
  178 + }
  179 +
  180 +#ifdef __FreeBSD__
  181 + mib_bat = init_sysctl("hw.acpi.battery.life", BAT_MIB_LEN, mib_bat);
  182 + mib_temp = init_sysctl("hw.acpi.thermal.tz0.temperature", TEMP_MIB_LEN, mib_temp);
  183 + mib_ac = init_sysctl("hw.acpi.acline", AC_MIB_LEN, mib_ac);
  184 +#endif
  185 +
  186 + for (;;sleep(1)) {
  187 + datetime = getdatetime();
  188 +
  189 + snprintf(status, 200, "%s%% | %s | %s",
  190 + getbat(mib_bat, mib_ac), gettemp(mib_temp), datetime);
  191 +
  192 +
  193 + setstatus(status);
  194 + }
  195 +
  196 + XCloseDisplay(dpy);
  197 +
  198 + return 0;
  199 +}