Correctly handle the order of items

This commit is contained in:
Michael Stapelberg 2009-07-21 20:23:08 +02:00
parent 6fda988f36
commit 34ba9fa908
5 changed files with 74 additions and 52 deletions

View File

@ -61,17 +61,18 @@ struct battery_head batteries;
/* socket file descriptor for general purposes */
int general_socket;
const char *wlan_interface;
const char *eth_interface;
const char *wmii_path;
const char *time_format;
bool use_colors;
bool get_ethspeed;
bool get_cpu_temperature;
char *thermal_zone;
const char *wlan_interface = NULL;
const char *eth_interface = NULL;
const char *wmii_path = NULL;
const char *time_format = NULL;
bool use_colors = false;
bool get_ethspeed = false;
bool get_ipv6 = false;
bool get_cpu_temperature = false;
char *thermal_zone = NULL;
const char *wmii_normcolors = "#222222 #333333";
char order[MAX_ORDER][2];
const char **run_watches;
int order[MAX_ORDER];
const char **run_watches = NULL;
unsigned int num_run_watches;
unsigned int interval = 1;
@ -89,6 +90,7 @@ int main(int argc, char *argv[]) {
char part[512],
pathbuf[512];
unsigned int i;
int j;
char *configfile = PREFIX "/etc/i3status.conf";
int o, option_index = 0;
@ -123,6 +125,14 @@ int main(int argc, char *argv[]) {
die("Could not create socket\n");
while (1) {
for (j = 0; j < MAX_ORDER; j++) {
generate_order(wlan_interface, ORDER_WLAN, "wlan", get_wireless_info());
generate_order(eth_interface, ORDER_ETH, "eth", get_eth_info());
generate_order(get_ipv6, ORDER_IPV6, "ipv6", get_ipv6_addr());
generate_order(get_cpu_temperature, ORDER_CPU_TEMPERATURE, "cpu_temperature", get_cpu_temperature_info());
generate_order(true, ORDER_LOAD, "load", get_load());
if (j == order[ORDER_RUN]) {
for (i = 0; i < num_run_watches; i += 2) {
bool running = process_runs(run_watches[i+1]);
if (use_colors)
@ -131,29 +141,24 @@ int main(int argc, char *argv[]) {
run_watches[i],
(running ? "yes" : "no"));
else snprintf(part, sizeof(part), "%s: %s", run_watches[i], (running ? "yes" : "no"));
snprintf(pathbuf, sizeof(pathbuf), "%s%s", order[ORDER_RUN], run_watches[i]);
snprintf(pathbuf, sizeof(pathbuf), "%d%s", order[ORDER_RUN], run_watches[i]);
write_to_statusbar(pathbuf, part, false);
}
if (wlan_interface)
write_to_statusbar(concat(order[ORDER_WLAN], "wlan"), get_wireless_info(), false);
if (eth_interface)
write_to_statusbar(concat(order[ORDER_ETH], "eth"), get_eth_info(), false);
struct battery *current_battery;
SIMPLEQ_FOREACH(current_battery, &batteries, batteries) {
write_to_statusbar(concat(order[ORDER_BATTERY], "battery"), get_battery_info(current_battery), false);
}
if (get_cpu_temperature)
write_to_statusbar(concat(order[ORDER_CPU_TEMPERATURE], "cpu_temperature"), get_cpu_temperature_info(), false);
write_to_statusbar(concat(order[ORDER_LOAD], "load"), get_load(), !time_format);
if (j == order[ORDER_BATTERY]) {
struct battery *current;
SIMPLEQ_FOREACH(current, &batteries, batteries)
generate(ORDER_BATTERY, "battery", get_battery_info(current));
}
if (time_format) {
if (j == order[ORDER_TIME] && time_format != NULL) {
/* Get date & time */
time_t current_time = time(NULL);
struct tm *current_tm = localtime(&current_time);
(void)strftime(part, sizeof(part), time_format, current_tm);
write_to_statusbar(concat(order[ORDER_TIME], "time"), part, true);
generate(ORDER_TIME, "time", part);
}
}
sleep(interval);

View File

@ -9,8 +9,20 @@
#define BEGINS_WITH(haystack, needle) (strncmp(haystack, needle, strlen(needle)) == 0)
#define max(a, b) (a > b ? a : b)
#define generate(orderidx, name, function) \
do { \
write_to_statusbar(order_to_str(order[orderidx], name), function, (j == (MAX_ORDER-1))); \
} while (0)
#define generate_order(condition, orderidx, name, function) \
do { \
if (j == order[orderidx] && condition) \
generate(orderidx, name, function); \
} while (0)
typedef enum { CS_DISCHARGING, CS_CHARGING, CS_FULL } charging_status_t;
enum { ORDER_RUN, ORDER_WLAN, ORDER_ETH, ORDER_BATTERY, ORDER_CPU_TEMPERATURE, ORDER_LOAD, ORDER_TIME, MAX_ORDER };
enum { ORDER_RUN, ORDER_WLAN, ORDER_ETH, ORDER_BATTERY, ORDER_CPU_TEMPERATURE, ORDER_LOAD, ORDER_TIME, ORDER_IPV6, MAX_ORDER };
struct battery {
char *path;
@ -22,8 +34,8 @@ struct battery {
/* src/general.c */
char *skip_character(char *input, char character, int amount);
void die(const char *fmt, ...);
char *concat(const char *str1, const char *str2);
void create_file(const char *name);
char *order_to_str(int number, char *name);
void setup(void);
void write_to_statusbar(const char *name, const char *message, bool final_entry);
void slurp(char *filename, char *destination, int size);
@ -57,10 +69,11 @@ extern const char *wmii_path;
extern const char *time_format;
extern bool use_colors;
extern bool get_ethspeed;
extern bool get_ipv6;
extern bool get_cpu_temperature;
extern char *thermal_zone;
extern const char *wmii_normcolors;
extern char order[MAX_ORDER][2];
extern int order[MAX_ORDER];
extern const char **run_watches;
extern unsigned int num_run_watches;
extern unsigned int interval;

View File

@ -60,6 +60,8 @@ int load_configuration(const char *configfile) {
SIMPLEQ_INSERT_TAIL(&batteries, new, batteries);
} OPT("color")
use_colors = true;
OPT("get_ipv6")
get_ipv6 = true;
OPT("get_ethspeed")
get_ethspeed = true;
OPT("get_cpu_temperature") {
@ -108,7 +110,7 @@ int load_configuration(const char *configfile) {
}
OPT("order")
{
#define SET_ORDER(opt, idx) { if (strcasecmp(token, opt) == 0) sprintf(order[idx], "%d", c++); }
#define SET_ORDER(opt, idx) { if (strcasecmp(token, opt) == 0) order[idx] = c++; }
char *walk, *token;
int c = 0;
walk = token = dest_value;
@ -117,6 +119,7 @@ int load_configuration(const char *configfile) {
walk++;
*(walk++) = '\0';
SET_ORDER("run", ORDER_RUN);
SET_ORDER("ipv6", ORDER_IPV6);
SET_ORDER("wlan", ORDER_WLAN);
SET_ORDER("eth", ORDER_ETH);
SET_ORDER("battery", ORDER_BATTERY);

View File

@ -63,8 +63,8 @@ void die(const char *fmt, ...) {
* Otherwise, the buffer size would have to be increased.
*
*/
char *concat(const char *str1, const char *str2) {
static char concatbuf[32];
(void)snprintf(concatbuf, sizeof(concatbuf), "%s%s", str1, str2);
return concatbuf;
char *order_to_str(int number, char *name) {
static char buf[32];
(void)snprintf(buf, sizeof(buf), "%d%s", number, name);
return buf;
}

View File

@ -103,19 +103,20 @@ void setup(void) {
/* Wait until wmii_path/rbar exists */
for (; stat(wmii_path, &statbuf) < 0; sleep(interval));
#endif
#define cf(orderidx, name) create_file(order_to_str(order[orderidx], name));
cleanup_rbar_dir();
if (wlan_interface)
create_file(concat(order[ORDER_WLAN],"wlan"));
cf(ORDER_WLAN, "wlan");
if (eth_interface)
create_file(concat(order[ORDER_ETH],"eth"));
cf(ORDER_ETH, "eth");
if (get_cpu_temperature)
create_file(concat(order[ORDER_CPU_TEMPERATURE], "cpu_temperature"));
create_file(concat(order[ORDER_LOAD],"load"));
cf(ORDER_CPU_TEMPERATURE, "cpu_temperature");
cf(ORDER_LOAD, "load");
if (time_format)
create_file(concat(order[ORDER_TIME],"time"));
cf(ORDER_TIME, "time");
for (i = 0; i < num_run_watches; i += 2) {
snprintf(pathbuf, sizeof(pathbuf), "%s%s", order[ORDER_RUN], run_watches[i]);
snprintf(pathbuf, sizeof(pathbuf), "%d%s", order[ORDER_RUN], run_watches[i]);
create_file(pathbuf);
}
}