]> git.sur5r.net Git - i3/i3status/blob - i3status.c
Use own files for each function, add get_ipv6_addr.c
[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;
65 const char *eth_interface;
66 const char *wmii_path;
67 const char *time_format;
68 bool use_colors;
69 bool get_ethspeed;
70 bool get_cpu_temperature;
71 char *thermal_zone;
72 const char *wmii_normcolors = "#222222 #333333";
73 char order[MAX_ORDER][2];
74 const char **run_watches;
75 unsigned int num_run_watches;
76 unsigned int interval = 1;
77
78 /*
79  * Exit upon SIGPIPE because when we have nowhere to write to, gathering
80  * system information is pointless.
81  *
82  */
83 void sigpipe(int signum) {
84         fprintf(stderr, "Received SIGPIPE, exiting\n");
85         exit(1);
86 }
87
88 int main(int argc, char *argv[]) {
89         char part[512],
90              pathbuf[512];
91         unsigned int i;
92
93         char *configfile = PREFIX "/etc/i3status.conf";
94         int o, option_index = 0;
95         struct option long_options[] = {
96                 {"config", required_argument, 0, 'c'},
97                 {"help", no_argument, 0, 'h'},
98                 {0, 0, 0, 0}
99         };
100
101         struct sigaction action;
102         memset(&action, 0, sizeof(struct sigaction));
103         action.sa_handler = sigpipe;
104         sigaction(SIGPIPE, &action, NULL);
105
106         SIMPLEQ_INIT(&batteries);
107
108         while ((o = getopt_long(argc, argv, "c:h", long_options, &option_index)) != -1)
109                 if ((char)o == 'c')
110                         configfile = optarg;
111                 else if ((char)o == 'h') {
112                         printf("i3status (c) 2008-2009 Michael Stapelberg\n"
113                                 "Syntax: %s [-c <configfile>]\n", argv[0]);
114                         return 0;
115                 }
116
117         if (load_configuration(configfile) < 0)
118                 return EXIT_FAILURE;
119
120         setup();
121
122         if ((general_socket = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
123                 die("Could not create socket\n");
124
125         while (1) {
126                 for (i = 0; i < num_run_watches; i += 2) {
127                         bool running = process_runs(run_watches[i+1]);
128                         if (use_colors)
129                                 snprintf(part, sizeof(part), "%s%s: %s",
130                                         (running ? color("#00FF00") : color("#FF0000")),
131                                         run_watches[i],
132                                         (running ? "yes" : "no"));
133                         else snprintf(part, sizeof(part), "%s: %s", run_watches[i], (running ? "yes" : "no"));
134                         snprintf(pathbuf, sizeof(pathbuf), "%s%s", order[ORDER_RUN], run_watches[i]);
135                         write_to_statusbar(pathbuf, part, false);
136                 }
137
138                 if (wlan_interface)
139                         write_to_statusbar(concat(order[ORDER_WLAN], "wlan"), get_wireless_info(), false);
140                 if (eth_interface)
141                         write_to_statusbar(concat(order[ORDER_ETH], "eth"), get_eth_info(), false);
142                 struct battery *current_battery;
143                 SIMPLEQ_FOREACH(current_battery, &batteries, batteries) {
144                         write_to_statusbar(concat(order[ORDER_BATTERY], "battery"), get_battery_info(current_battery), false);
145                 }
146                 if (get_cpu_temperature)
147                         write_to_statusbar(concat(order[ORDER_CPU_TEMPERATURE], "cpu_temperature"), get_cpu_temperature_info(), false);
148
149                 write_to_statusbar(concat(order[ORDER_LOAD], "load"), get_load(), !time_format);
150
151                 if (time_format) {
152                         /* Get date & time */
153                         time_t current_time = time(NULL);
154                         struct tm *current_tm = localtime(&current_time);
155                         (void)strftime(part, sizeof(part), time_format, current_tm);
156                         write_to_statusbar(concat(order[ORDER_TIME], "time"), part, true);
157                 }
158
159                 sleep(interval);
160         }
161 }