forked from lynks--/lifebar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lifebar.h
325 lines (257 loc) · 9.64 KB
/
lifebar.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
* Lifebar - i3wm workspace status and tool bar.
*
* Copyright (c) 2013 [email protected]
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License v3 as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Xft/Xft.h>
#include <X11/extensions/Xrandr.h>
#include <sys/socket.h>
#include <sys/sysinfo.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/statvfs.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <cairo.h>
#include <cairo-xlib.h>
#include <time.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <unistd.h>
#include <ifaddrs.h>
#include <curl/curl.h>
//i3 ipc message types
#define COMMAND 0
#define GET_WORKSPACES 1
#define SUBSCRIBE 2
#define GET_OUTPUTS 3
#define GET_TREE 4
#define GET_MASKS 5
#define GET_BAR_CONFIG 6
#define GET_VERSION 7
//i3 header length
#define I3_HEADER_LENGTH 14
//config.position options
#define TOP 0
#define BOTTOM 1
//render function lean directions
#define LEFT 0
#define RIGHT 1
//divider line styles
#define LINE 0
#define GROOVE 1
//ws wrapping
#define WSWRAP_ON 0
#define WSWRAP_OFF 1
//output message types
#define BAD_MSG "[ \x1b[31m:(\x1b[0m ] "
#define GOOD_MSG "[ \x1b[92m:)\x1b[0m ] "
//battery state
#define CHARGING 0
#define DISCHARGING 1
#define FULL 2
#define EMPTY 3
#define UNKNOWN 4
//max workspaces tracked for click events
#define MAX_WORKSPACES 128
#define MAX_WS_NAME_LENGTH 64
//how often in seconds do we perform the expensive lookups
//NOTE: currently this is used to decrement alarm, so it should stay at 1
#define EXPENSIVE_TIME 10
//ping our external ip every x seconds, 300 = 5 minutes
#define EXTERNAL_IP_TIME 300
//how many readings (1 per second) do we average net speed over
#define NET_SPEED_AVERAGE 3
// the valid configuration options for the config key 'modules' which does
// not have any suffix.
static char **valid_static = (char *[]){"fsone", "fstwo", "extip", "ifone",
"iftwo", "date", "time", "uptime", "ifinfo", "volinfo", "bluetooth", NULL};
// the valid configuration options for the config key 'modules' which does
// have a suffix like bat0 or therm1.
static char **valid_suffix = (char *[]){"bat", "therm", NULL};
struct batt_info {
uint32_t index; //battery number, as in BAT0
uint32_t percent; //how full the battery is 0-100 inc
uint32_t status; //CHARGING DISCHARGING FULL EMPTY
char time_left[64]; //how long until FULL/EMPTY in the format hh:mm:ss
};
struct thermal_info {
uint32_t index; //thermal zone index
uint32_t temp_c; //temp in c
};
struct net_speed_info {
uint64_t down_bytes; //these are just current totals, we have to
uint64_t up_bytes; //compare to previous readings for speed
};
struct net_info {
char name[64]; //SSID of the network
char signal_level[4]; //The signal level in dB
};
struct i3_output {
char name[16];
char active[16];
uint32_t x;
uint32_t y;
uint32_t width;
uint32_t height;
struct i3_output *next;
};
struct i3_workspace {
char name[64];
char visible[16];
char focused[16];
char urgent[16];
char output[16];
struct i3_workspace *next;
};
struct ws_layout {
char ws_name[MAX_WORKSPACES][MAX_WS_NAME_LENGTH];
uint32_t x_max[MAX_WORKSPACES];
char active[MAX_WORKSPACES];
};
struct time_layout {
uint32_t x_min;
uint32_t x_max;
};
struct module {
char name[64];
struct module *next;
};
struct instance {
Window w;
XImage *bg; //background
Pixmap bb; //backbuffer pixmap
cairo_surface_t *cairo_s_bb; //cairo surface for the backbuffer
cairo_t *cairo; //cairo context for the backbuffer
GC gc; //graphics context
uint32_t max_wsx; //max x coord for mouse wheel ws switching
struct i3_output *output; //output info for this instance
struct ws_layout *ws_layout; //store positions of ws dividers for clicks
struct time_layout *time_layout;//store position of time for mousewheel
struct instance *next;
};
struct colour {
unsigned char red;
unsigned char green;
unsigned char blue;
unsigned char alpha;
};
struct curl_writedata {
char *buffer;
size_t size;
};
struct volume_info {
int volume_percent;
int is_muted;
};
struct config {
uint32_t position; //TOP or BOTTOM
uint32_t depth; //bar depth, eg 20
int32_t text_nudge; //tweak text vertical centre
char datefmt[64]; //date strftime format string
char timefmt[64]; //time strftime format string
uint32_t rpadding; //padding on right screen edge
uint32_t lpadding; //padding on left screen edge
uint32_t kvpadding; //padding between key and value
uint32_t divpadding; //padding either side of divider lines
uint32_t divwidth; //divider line width
uint32_t divstyle; //divider line style (LINE or GROOVE)
uint32_t divgap; //for line divider, gap to edge of bar
uint32_t wswrap; //whether wsmousewheel wraps WSWRAP_ON/OFF
char ifone[32]; //interface name, eg eth0
char iftwo[32]; //interface name, eg eth0
char ifinfo[32]; //interface name, eg eth0
char fsone[32]; //fs location, eg /home
char fstwo[32]; //fs location, eg /home
uint32_t alarm_increment_s; //alarm increment in seconds eg 300
struct colour *tintcol; //transparency tint colour
struct colour *alarmtintcol; //background tint for alarm flash
struct colour *keycol; //main key text colour
struct colour *valcol; //main value text colour
struct colour *timecol; //time text colour
struct colour *datecol; //date text colour
struct colour *alarmcol; //alarm text colour
struct colour *divcol; //divider line colour for line divider style
struct colour *viswscol; //text colour for visible workspace
struct colour *inviswscol; //text colour for invisible workspace
struct colour *groove_light; //light side of groove overlay
struct colour *groove_dark; //dark side of groove overlay
struct colour *urgent; //Urgent workspace colour
struct colour *urgent_visible;//Urgent workspace colour when the workspace is visible
cairo_font_face_t *keyfont; //main key text font
double keyfontsize; //main key text size
cairo_font_face_t *valfont; //main value text font
double valfontsize; //main value text size
cairo_font_face_t *datefont; //date text font
double datefontsize; //date text size
cairo_font_face_t *timefont; //time text font
double timefontsize; //time text size
cairo_font_face_t *wsfont; //workspace text font
double wsfontsize; //workspace text size
struct module *modules; //the modules in appropriate order
uint32_t batt_alarm; //batt percentage for alarm
uint32_t external; //whether or not to make external ip calls
};
extern struct config *conf;
void check_module_list(struct module *);
int valid_module_static(char *, char **);
int valid_module_suffix(char *, char **);
int get_module_suffix(char *, char **);
struct colour *prepare_colour(int, int, int, int);
void set_cairo_source_colour(cairo_t *, struct colour*);
int is_key_label(char *);
void handle_value_label(struct i3_output *, char *, char *);
void debug_i3_output(struct i3_output *);
void i3_ipc_send(char **, int, int, char *);
void free_ipc_result(char *);
void free_workspaces_list(struct i3_workspace *);
struct i3_output *get_i3_outputs();
struct i3_workspace *get_i3_workspaces();
void get_i3_sockpath(char **);
struct colour *parse_config_colour(char *);
void parse_config_font(const char *, char *);
int count_acpi_batteries();
void read_acpi_battery(int, struct batt_info *);
int count_acpi_thermal();
void read_acpi_thermal(int, struct thermal_info *);
int render_divider(cairo_t *, int, int);
int render_workspace(cairo_t *, int, int, struct i3_workspace *, int);
int render_time(cairo_t *, int, int, int);
int render_alarm(cairo_t *, uint32_t, int, int, int);
int render_uptime(cairo_t *, uint32_t, int, int, int);
int render_date(cairo_t *, int, int, int);
int render_interface(cairo_t *, int, int, struct ifaddrs *,
struct net_speed_info[], uint32_t, int);
int render_interface_info(cairo_t *, int, int,
struct net_info, int);
int render_volume(cairo_t *, int, int, struct volume_info, int);
int render_filesystem(cairo_t *, int, int, struct statvfs *, char *, int);
int render_battery(cairo_t *, int, int, struct batt_info *, int);
int render_thermal(cairo_t *, int, int, struct thermal_info *, int);
int render_keyvalue(cairo_t *, int, int, char *, char *, int);
int render_bluetooth(cairo_t *c, int x, int y, int connected, int d);
void read_net_speed(char *, struct net_speed_info *);
void read_net_info(char *, struct net_info *);
size_t curl_writeback(void *, size_t, size_t, void *);
void get_alsa_master_info(struct volume_info *);
int bluetooth_device_connected();