Commit 5a8f93f3175844e605bb8475c0d594d67fedf242
1 parent
9db77de9ed
Exists in
master
sanitized?
Showing 2 changed files with 9 additions and 8 deletions Inline Diff
README
View file @
5a8f93f
File was created | 1 | Statusbar setter for DWM that should work in FreeBSD and Linux. | ||
2 | Displays battery status, temperature, date and time. |
status.c
View file @
5a8f93f
/* made by profil 2011-12-29. | 1 | 1 | /* made by profil 2011-12-29. | |
* Added: | 2 | 2 | * Added: | |
* * Battery and temperature status for Linux and FreeBSD | 3 | 3 | * * Battery and temperature status for Linux and FreeBSD | |
* | 4 | |||
* Compile with: | 5 | |||
* bestcc -Wnone -pedantic -std=c49 -lX11 status.c -o status | 6 | |||
*/ | 7 | 4 | */ | |
#include <stdio.h> | 8 | 5 | #include <stdio.h> | |
#include <stdlib.h> | 9 | 6 | #include <stdlib.h> | |
#include <string.h> | 10 | 7 | #include <string.h> | |
#include <errno.h> | 11 | 8 | #include <errno.h> | |
#include <unistd.h> | 12 | 9 | #include <unistd.h> | |
#include <time.h> | 13 | 10 | #include <time.h> | |
#include <X11/Xlib.h> | 14 | 11 | #include <X11/Xlib.h> | |
#include <sys/types.h> | 15 | 12 | #include <sys/types.h> | |
#include <sys/sysctl.h> | 16 | 13 | #include <sys/sysctl.h> | |
17 | 14 | |||
#define SIZE 533 | 18 | 15 | #define SIZE 533 | |
#define F_BATT "/sys/class/power_supply/BAT1/capacity" | 19 | 16 | #define F_BATT "/sys/class/power_supply/BAT1/capacity" | |
#define F_TMPER "/sys/bus/acpi/drivers/thermal/LNXTHERM:00/thermal_zone/temp" | 20 | 17 | #define F_TMPER "/sys/bus/acpi/drivers/thermal/LNXTHERM:00/thermal_zone/temp" | |
21 | 18 | |||
#define BAT_MIB_LEN 4 | 22 | 19 | #define BAT_MIB_LEN 4 | |
#define TEMP_MIB_LEN 5 | 23 | 20 | #define TEMP_MIB_LEN 5 | |
#define AC_MIB_LEN 3 | 24 | 21 | #define AC_MIB_LEN 3 | |
25 | 22 | |||
#define BATBUF_LEN 16 | 26 | 23 | #define BATBUF_LEN 16 | |
#define DATETIME_LEN 16 | 27 | 24 | #define DATETIME_LEN 16 | |
#define TEMPER_LEN 8 | 28 | 25 | #define TEMPER_LEN 8 | |
#define STATUS_LEN 200 | 29 | 26 | #define STATUS_LEN 200 | |
30 | 27 | |||
static Display *dpy; | 31 | 28 | static Display *dpy; | |
32 | 29 | |||
void | 33 | 30 | void | |
setstatus(char *str) | 34 | 31 | setstatus(char *str) | |
{ | 35 | 32 | { | |
XStoreName(dpy, DefaultRootWindow(dpy), str); | 36 | 33 | XStoreName(dpy, DefaultRootWindow(dpy), str); | |
XSync(dpy, False); | 37 | 34 | XSync(dpy, False); | |
} | 38 | 35 | } | |
39 | 36 | |||
char * | 40 | 37 | char * | |
getdatetime() | 41 | 38 | getdatetime() | |
{ | 42 | 39 | { | |
char *buf = (char *) datetime_buf; | 43 | 40 | char *buf = (char *) datetime_buf; | |
time_t result; | 44 | 41 | time_t result; | |
struct tm *resulttm; | 45 | 42 | struct tm *resulttm; | |
46 | 43 | |||
result = time(NULL); | 47 | 44 | result = time(NULL); | |
resulttm = localtime(&result); | 48 | 45 | resulttm = localtime(&result); | |
49 | 46 | |||
if (resulttm == NULL) { | 50 | 47 | if (resulttm == NULL) { | |
fprintf(stderr, "Error getting localtime.\n"); | 51 | 48 | fprintf(stderr, "Error getting localtime.\n"); | |
return ""; | 52 | 49 | return ""; | |
} | 53 | 50 | } | |
54 | 51 | |||
if (!strftime(buf, DATETIME_LEN - 1, "%D | %l:%M:%S%p", resulttm)) { | 55 | 52 | if (!strftime(buf, DATETIME_LEN - 1, "%D | %l:%M:%S%p", resulttm)) { | |
fprintf(stderr, "strftime is 0.\n"); | 56 | 53 | fprintf(stderr, "strftime is 0.\n"); | |
return ""; | 57 | 54 | return ""; | |
} | 58 | 55 | } | |
59 | 56 | |||
return buf; | 60 | 57 | return buf; | |
} | 61 | 58 | } | |
62 | 59 | |||
#ifdef __FreeBSD__ | 63 | 60 | #ifdef __FreeBSD__ | |
int * | 64 | 61 | int * | |
init_sysctl(char *name, size_t miblen, int *mib) | 65 | 62 | init_sysctl(char *name, size_t miblen, int *mib) | |
{ | 66 | 63 | { | |
sysctlnametomib(name, mib, &miblen); | 67 | 64 | sysctlnametomib(name, mib, &miblen); | |
68 | 65 | |||
return mib; | 69 | 66 | return mib; | |
} | 70 | 67 | } | |
#endif | 71 | 68 | #endif | |
72 | 69 | |||
char * | 73 | 70 | char * | |
gettemp(int *mib) | 74 | 71 | gettemp(int *mib) | |
{ | 75 | 72 | { | |
char *buf = (char *) temp_buf; | 76 | 73 | char *buf = (char *) temp_buf; | |
int c_temp; | 77 | 74 | int c_temp; | |
78 | 75 | |||
#ifdef __FreeBSD__ | 79 | 76 | #ifdef __FreeBSD__ | |
int kelvin_temp; | 80 | 77 | int kelvin_temp; | |
size_t len = sizeof(kelvin_temp); | 81 | 78 | size_t len = sizeof(kelvin_temp); | |
82 | 79 | |||
if (sysctl(mib, 5, &kelvin_temp, &len, NULL, 0)) { | 83 | 80 | if (sysctl(mib, 5, &kelvin_temp, &len, NULL, 0)) { | |
perror("Failed to get system temperature"); | 84 | 81 | perror("Failed to get system temperature"); | |
return buf = "-1"; | 85 | 82 | return buf = "-1"; | |
} | 86 | 83 | } | |
87 | 84 | |||
c_temp = (kelvin_temp - 2732) / 10; | 88 | 85 | c_temp = (kelvin_temp - 2732) / 10; | |
89 | 86 | |||
snprintf(buf, TEMPER_LEN, "%dC", c_temp); | 90 | 87 | snprintf(buf, TEMPER_LEN, "%dC", c_temp); | |
91 | 88 | |||
#elif __linux__ | 92 | 89 | #elif __linux__ | |
93 | 90 | |||
FILE *tfile = fopen(F_TMPER, "r"); | 94 | 91 | FILE *tfile = fopen(F_TMPER, "r"); | |
95 | 92 | |||
if (tfile == NULL) { | 96 | 93 | if (tfile == NULL) { | |
fprintf(stderr, "Could not open file %s: %s\n", | 97 | 94 | fprintf(stderr, "Could not open file %s: %s\n", | |
F_TMPER, strerror(errno)); | 98 | 95 | F_TMPER, strerror(errno)); | |
return "?"; | 99 | 96 | return "?"; | |
} | 100 | 97 | } | |
101 | 98 | |||
102 | 99 | |||
if (fscanf(tfile, "%g", &c_temp) == EOF) { | 103 | 100 | if (fscanf(tfile, "%g", &c_temp) == EOF) { | |
fprintf(stderr, "Failed to read temperature\n"); | 104 | 101 | fprintf(stderr, "Failed to read temperature\n"); | |
fclose(tfile); | 105 | 102 | fclose(tfile); | |
return "?"; | 106 | 103 | return "?"; | |
} | 107 | 104 | } | |
108 | 105 | |||
fclose(tfile); | 109 | 106 | fclose(tfile); | |
110 | 107 | |||
snprintf(buf, TEMPER_LEN, "%dC", c_temp / 1000); | 111 | 108 | snprintf(buf, TEMPER_LEN, "%dC", c_temp / 1000); | |
#endif | 112 | 109 | #endif | |
113 | 110 | |||
return buf; | 114 | 111 | return buf; | |
} | 115 | 112 | } | |
116 | 113 | |||
char * | 117 | 114 | char * | |
getbat(int *mib, int *power) | 118 | 115 | getbat(int *mib, int *power) | |
{ | 119 | 116 | { | |
char *buf = (char *) bat_buf; | 120 | 117 | char *buf = (char *) bat_buf; | |
121 | 118 | |||
#ifdef __linux__ | 122 | 119 | #ifdef __linux__ | |
FILE *batstat; | 123 | 120 | FILE *batstat; | |
int batval; | 124 | 121 | int batval; | |
125 | 122 | |||
if ((batstat = fopen(F_BATT, "r")) == NULL) { | 126 | 123 | if ((batstat = fopen(F_BATT, "r")) == NULL) { | |
fprintf(stderr, "Could not open file %s: %s\n", | 127 | 124 | fprintf(stderr, "Could not open file %s: %s\n", | |
F_BATT, strerror(errno)); | 128 | 125 | F_BATT, strerror(errno)); | |
return "?"; | 129 | 126 | return "?"; | |
} | 130 | 127 | } | |
131 | 128 | |||
if (fscanf(batstat, "%d", &batval) == EOF) { | 132 | 129 | if (fscanf(batstat, "%d", &batval) == EOF) { | |
fprintf(stderr, "Failed to read battery status\n"); | 133 | 130 | fprintf(stderr, "Failed to read battery status\n"); | |
fclose(batstat); | 134 | 131 | fclose(batstat); | |
return "?"; | 135 | 132 | return "?"; | |
} | 136 | 133 | } | |
137 | 134 | |||
snprintf(buf, 16, "BAT %d", batval); | 138 | 135 | snprintf(buf, BATBUF_LEN, "BAT %d", batval); | |
139 | 136 | |||
fclose(batstat); | 140 | 137 | fclose(batstat); | |
141 | 138 | |||
#elif __FreeBSD__ | 142 | 139 | #elif __FreeBSD__ | |
143 | 140 | |||
int batlife; | 144 | 141 | int batlife; | |
int plugged_in, plugged_in_err; | 145 | 142 | int plugged_in, plugged_in_err; | |
146 | 143 | |||
size_t batlife_len = sizeof(batlife); | 147 | 144 | size_t batlife_len = sizeof(batlife); | |
148 | 145 | |||
if (sysctl(mib, 4, &batlife, &batlife_len, NULL, 0)) { | 149 | 146 | if (sysctl(mib, 4, &batlife, &batlife_len, NULL, 0)) { | |
perror("Failed to get battery status"); | 150 | 147 | perror("Failed to get battery status"); | |
return buf = "-1"; | 151 | 148 | return buf = "-1"; | |
} | 152 | 149 | } | |
153 | 150 | |||
plugged_in_err = sysctl(power, 3, &plugged_in, &batlife_len, NULL, 0); | 154 | 151 | plugged_in_err = sysctl(power, 3, &plugged_in, &batlife_len, NULL, 0); | |
155 | 152 | |||
if (plugged_in_err || !plugged_in) { | 156 | 153 | if (plugged_in_err || !plugged_in) { | |
snprintf(buf, 16, "BAT %d", batlife); | 157 | 154 | snprintf(buf, BATBUF_LEN, "BAT %d", batlife); | |
} else { | 158 | 155 | } else { |