]> git.sur5r.net Git - i3/i3status/blob - i3status.c
Update i3status.conf
[i3/i3status] / i3status.c
1 /*
2  * vim:ts=8:expandtab
3  *
4  * i3status – Generates a status line for dzen2 or wmii
5  *
6  *
7  * Copyright © 2008-2009 Michael Stapelberg and contributors
8  * Copyright © 2009 Thorsten Toepper <atsutane at freethoughts dot de>
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without modification,
12  * are permitted provided that the following conditions are met:
13  *
14  * * Redistributions of source code must retain the above copyright notice, this
15  *   list of conditions and the following disclaimer.
16  *
17  * * Redistributions in binary form must reproduce the above copyright notice, this
18  *   list of conditions and the following disclaimer in the documentation and/or other
19  *   materials provided with the distribution.
20  *
21  * * Neither the name of Michael Stapelberg nor the names of contributors
22  *   may be used to endorse or promote products derived from this software without
23  *   specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
26  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
28  * SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
30  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
31  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
34  * DAMAGE.
35  *
36  */
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <fcntl.h>
40 #include <string.h>
41 #include <stdio.h>
42 #include <time.h>
43 #include <stdbool.h>
44 #include <stdarg.h>
45 #include <unistd.h>
46 #include <stdlib.h>
47 #include <limits.h>
48 #include <ctype.h>
49 #include <sys/ioctl.h>
50 #include <sys/socket.h>
51 #include <netinet/in.h>
52 #include <getopt.h>
53 #include <signal.h>
54
55 #include "queue.h"
56
57 #include "i3status.h"
58
59 struct battery_head batteries;
60
61 /* socket file descriptor for general purposes */
62 int general_socket;
63
64 const char *wlan_interface = NULL;
65 const char *eth_interface = NULL;
66 const char *wmii_path = NULL;
67 const char *time_format = NULL;
68 bool use_colors = false;
69 bool get_ethspeed = false;
70 bool get_ipv6 = false;
71 bool get_cpu_temperature = false;
72 char *thermal_zone = NULL;
73 const char *wmii_normcolors = "#222222 #333333";
74 int order[MAX_ORDER];
75 const char **run_watches = NULL;
76 unsigned int num_run_watches;
77 unsigned int interval = 1;
78
79 /*
80  * Exit upon SIGPIPE because when we have nowhere to write to, gathering
81  * system information is pointless.
82  *
83  */
84 void sigpipe(int signum) {
85         fprintf(stderr, "Received SIGPIPE, exiting\n");
86         exit(1);
87 }
88
89 int main(int argc, char *argv[]) {
90         char part[512],
91              pathbuf[512];
92         unsigned int i;
93         int j;
94
95         char *configfile = PREFIX "/etc/i3status.conf";
96         int o, option_index = 0;
97         struct option long_options[] = {
98                 {"config", required_argument, 0, 'c'},
99                 {"help", no_argument, 0, 'h'},
100                 {0, 0, 0, 0}
101         };
102
103         struct sigaction action;
104         memset(&action, 0, sizeof(struct sigaction));
105         action.sa_handler = sigpipe;
106         sigaction(SIGPIPE, &action, NULL);
107
108         SIMPLEQ_INIT(&batteries);
109
110         while ((o = getopt_long(argc, argv, "c:h", long_options, &option_index)) != -1)
111                 if ((char)o == 'c')
112                         configfile = optarg;
113                 else if ((char)o == 'h') {
114                         printf("i3status (c) 2008-2009 Michael Stapelberg\n"
115                                 "Syntax: %s [-c <configfile>]\n", argv[0]);
116                         return 0;
117                 }
118
119         if (load_configuration(configfile) < 0)
120                 return EXIT_FAILURE;
121
122         setup();
123
124         if ((general_socket = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
125                 die("Could not create socket\n");
126
127         while (1) {
128                 for (j = 0; j < MAX_ORDER; j++) {
129                         generate_order(wlan_interface, ORDER_WLAN, "wlan", get_wireless_info());
130                         generate_order(eth_interface, ORDER_ETH, "eth", get_eth_info());
131                         generate_order(get_ipv6, ORDER_IPV6, "ipv6", get_ipv6_addr());
132                         generate_order(get_cpu_temperature, ORDER_CPU_TEMPERATURE, "cpu_temperature", get_cpu_temperature_info());
133                         generate_order(true, ORDER_LOAD, "load", get_load());
134
135                         if (j == order[ORDER_RUN]) {
136                                 for (i = 0; i < num_run_watches; i += 2) {
137                                         bool running = process_runs(run_watches[i+1]);
138                                         if (use_colors)
139                                                 snprintf(part, sizeof(part), "%s%s: %s",
140                                                         (running ? color("#00FF00") : color("#FF0000")),
141                                                         run_watches[i],
142                                                         (running ? "yes" : "no"));
143                                         else snprintf(part, sizeof(part), "%s: %s", run_watches[i], (running ? "yes" : "no"));
144                                         snprintf(pathbuf, sizeof(pathbuf), "%d%s", order[ORDER_RUN], run_watches[i]);
145                                         write_to_statusbar(pathbuf, part, false);
146                                 }
147                         }
148
149                         if (j == order[ORDER_BATTERY]) {
150                                 struct battery *current;
151                                 SIMPLEQ_FOREACH(current, &batteries, batteries)
152                                         generate(ORDER_BATTERY, "battery", get_battery_info(current));
153                         }
154
155                         if (j == order[ORDER_TIME] && time_format != NULL) {
156                                 /* Get date & time */
157                                 time_t current_time = time(NULL);
158                                 struct tm *current_tm = localtime(&current_time);
159                                 (void)strftime(part, sizeof(part), time_format, current_tm);
160                                 generate(ORDER_TIME, "time", part);
161                         }
162                 }
163
164                 sleep(interval);
165         }
166 }