convert all thread activity to timed thread abstraction
[monky] / src / conky.h
1 /*
2  * Conky, a system monitor, based on torsmo
3  *
4  * This program is licensed under BSD license, read COPYING
5  *
6  *  $Id$
7  */
8
9 #ifndef _conky_h_
10 #define _conky_h_
11
12 #if defined(HAS_MCHECK_H)
13 #include <mcheck.h>
14 #endif /* HAS_MCHECK_H */
15 #include "config.h"
16 #include <sys/utsname.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <locale.h>
21 #include <langinfo.h>
22 #include <wchar.h>
23 #include <sys/param.h>
24 #if defined(__FreeBSD__)
25 #include <sys/mount.h>
26 #include <sys/ucred.h>
27 #include <fcntl.h>
28 #include <kvm.h>
29 #endif /* __FreeBSD__ */
30
31 #if defined(__FreeBSD__) && (defined(i386) || defined(__i386__))
32 #include <machine/apm_bios.h>
33 #endif /* __FreeBSD__ */
34
35 #ifdef AUDACIOUS
36 #include "audacious.h"
37 #endif
38
39 #ifdef XMMS2
40 #include <xmmsclient/xmmsclient.h>
41 #endif
42
43 #include "timed_thread.h"
44
45 #define TOP_CPU 1
46 #define TOP_NAME 2
47 #define TOP_PID 3
48 #define TOP_MEM 4
49
50 #define TEXT_BUFFER_SIZE 1280
51 #define P_MAX_SIZE ((TEXT_BUFFER_SIZE * 4) - 2) 
52
53 /* maximum size of config TEXT buffer, i.e. below TEXT line. */
54 #define MAX_USER_TEXT_DEFAULT 16384
55
56 #include <sys/socket.h>
57
58 #define ERR(s, varargs...) \
59 fprintf(stderr, "Conky: " s "\n", ##varargs)
60
61 /* critical error */
62 #define CRIT_ERR(s, varargs...) \
63 { fprintf(stderr, "Conky: " s "\n", ##varargs);  exit(EXIT_FAILURE); }
64
65 struct i8k_struct {
66         char *version;
67         char *bios;
68         char *serial;
69         char *cpu_temp;
70         char *left_fan_status;
71         char *right_fan_status;
72         char *left_fan_rpm;
73         char *right_fan_rpm;
74         char *ac_status;
75         char *buttons_status;
76 };
77
78 struct i8k_struct i8k;
79
80 struct net_stat {
81         const char *dev;
82         int up;
83         long long last_read_recv, last_read_trans;
84         long long recv, trans;
85         double recv_speed, trans_speed;
86         struct sockaddr addr;
87         int linkstatus;
88         double net_rec[15], net_trans[15];
89 };
90
91 unsigned int diskio_value;
92
93 struct fs_stat {
94         char *path;
95         long long size;
96         long long avail;
97         long long free;
98 };
99
100 struct mail_s {                 // for imap and pop3
101         unsigned long unseen;
102         unsigned long messages;
103         unsigned long used;
104         unsigned long quota;
105         unsigned long port;
106         float interval;
107         double last_update;
108         char host[128];
109         char user[128];
110         char pass[128];
111         char command[1024];
112         char folder[128];
113         char secure;
114         timed_thread *p_timed_thread;
115 } mail;
116
117 /*struct cpu_stat {
118         unsigned int user, nice, system, idle, iowait, irq, softirq;
119         int cpu_avg_samples;
120 };*/
121
122 #ifdef MPD
123 struct mpd_s {
124         char *title;
125         char *artist;
126         char *album;
127         char *status;
128         char *random;
129         char *repeat;
130         char *track;
131         char *name;
132         char *file;
133         int volume;
134         unsigned int port;
135         char host[128];
136         char password[128];
137         float progress;
138         int bitrate;
139         int length;
140         int elapsed;
141 };
142 #endif
143
144 #ifdef XMMS2
145 struct xmms2_s {
146     char* artist;
147     char* album;
148     char* title;
149     char* genre;
150     char* comment;
151     char* decoder;
152     char* transport;
153     char* url;
154     char* date;
155     int tracknr;
156     int bitrate;
157     unsigned int id;
158     int duration;
159     int elapsed;
160     float size;
161
162     float progress;
163     char* status;
164 };
165 #endif
166
167 #ifdef AUDACIOUS
168 struct audacious_s {
169         audacious_t items;              /* e.g. items[AUDACIOUS_STATUS] */
170         timed_thread *p_timed_thread;
171 };
172 #endif
173
174 #ifdef BMPX
175 void update_bmpx();
176 struct bmpx_s {
177         char *title;
178         char *artist;
179         char *album;
180         char *uri;
181         int bitrate;
182         int track;
183 };
184 #endif
185
186 #ifdef TCP_PORT_MONITOR
187 #include "libtcp-portmon.h"
188 #define MIN_PORT_MONITORS_DEFAULT 16
189 #define MIN_PORT_MONITOR_CONNECTIONS_DEFAULT 256
190 #endif
191
192 enum {
193         INFO_CPU = 0,
194         INFO_MAIL = 1,
195         INFO_MEM = 2,
196         INFO_NET = 3,
197         INFO_PROCS = 4,
198         INFO_RUN_PROCS = 5,
199         INFO_UPTIME = 6,
200         INFO_BUFFERS = 7,
201         INFO_FS = 8,
202         INFO_I2C = 9,
203         INFO_MIXER = 10,
204         INFO_LOADAVG = 11,
205         INFO_UNAME = 12,
206         INFO_FREQ = 13,
207 #ifdef MPD
208         INFO_MPD = 14,
209 #endif
210         INFO_TOP = 15,
211         INFO_WIFI = 16,
212         INFO_DISKIO = 17,
213         INFO_I8K = 18,
214 #ifdef TCP_PORT_MONITOR
215         INFO_TCP_PORT_MONITOR = 19,
216 #endif
217 #ifdef AUDACIOUS
218         INFO_AUDACIOUS = 20,
219 #endif
220 #ifdef BMPX
221         INFO_BMPX = 21,
222 #endif
223 #ifdef XMMS2
224         INFO_XMMS2 = 22,
225 #endif
226 };
227
228
229 #ifdef MPD
230 #include "libmpdclient.h"
231 #endif
232
233 volatile int g_signal_pending;
234
235 struct information {
236         unsigned int mask;
237
238         struct utsname uname_s;
239
240         char freq[10];
241
242         double uptime;
243
244         /* memory information in kilobytes */
245         unsigned long mem, memmax, swap, swapmax;
246         unsigned long bufmem, buffers, cached;
247
248         unsigned short procs;
249         unsigned short run_procs;
250
251         float *cpu_usage;
252         /*      struct cpu_stat cpu_summed; what the hell is this? */
253         unsigned int cpu_count;
254         unsigned int cpu_avg_samples;
255
256         unsigned int net_avg_samples;
257
258         float loadavg[3];
259
260         int new_mail_count, mail_count;
261         struct mail_s* mail;
262         int mail_running;
263 #ifdef MPD
264         struct mpd_s mpd;
265         mpd_Connection *conn;
266 #endif
267 #ifdef XMMS2
268         struct xmms2_s xmms2;
269         int xmms2_conn_state;
270         xmms_socket_t xmms2_fd; 
271         fd_set xmms2_fdset;
272         xmmsc_connection_t *xmms2_conn;
273 #endif
274 #ifdef AUDACIOUS
275         struct audacious_s audacious;
276 #endif
277 #ifdef BMPX
278         struct bmpx_s bmpx;
279 #endif
280         struct process *cpu[10];
281         struct process *memu[10];
282         struct process *first_process;
283         unsigned long looped;
284 #ifdef TCP_PORT_MONITOR
285         tcp_port_monitor_collection_t * p_tcp_port_monitor_collection;
286 #endif
287         short kflags;  /* kernel settings, see enum KFLAG */
288 };
289
290 enum {
291         KFLAG_IS_LONGSTAT = 0x01,         /* set to true if kernel uses "long" format for /proc/stats */
292         KFLAG_PROC_IS_THREADS=0x02       /* set to true if kernel shows # of threads for the proc value in sysinfo() call */
293 /*      KFLAG_NEXT_ONE=0x04                 bits 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 available for future use */
294      }; 
295
296 #define KFLAG_SETON(a) info.kflags |= a 
297 #define KFLAG_SETOFF(a) info.kflags &= (~a)
298 #define KFLAG_FLIP(a) info.kflags ^= a
299 #define KFLAG_ISSET(a) info.kflags & a
300
301
302 int out_to_console;
303
304 int top_cpu;
305 int top_mem;
306
307 int use_spacer;
308
309 char tmpstring1[TEXT_BUFFER_SIZE];
310 char tmpstring2[TEXT_BUFFER_SIZE];
311
312 #ifdef X11
313 /* in x11.c */
314
315 #include <X11/Xlib.h>
316 #include <X11/Xlib.h>
317 #include <X11/Xatom.h>
318
319 #ifdef XFT
320 #include <X11/Xft/Xft.h>
321 #endif
322
323 #ifdef HAVE_XDBE
324 #include <X11/extensions/Xdbe.h>
325 #endif
326
327 #define ATOM(a) XInternAtom(display, #a, False)
328
329 #ifdef OWN_WINDOW
330 enum _window_type {
331         TYPE_NORMAL = 0,
332         TYPE_DESKTOP,
333         TYPE_OVERRIDE
334 };
335
336 enum _window_hints {
337         HINT_UNDECORATED = 0,
338         HINT_BELOW,
339         HINT_ABOVE,
340         HINT_STICKY,
341         HINT_SKIP_TASKBAR,
342         HINT_SKIP_PAGER
343 };
344 #define SET_HINT(mask,hint)     (mask |= (1<<hint))
345 #define TEST_HINT(mask,hint)    (mask & (1<<hint))
346 #endif
347 struct conky_window {
348         Window root,window,desktop;
349         Drawable drawable;
350         GC gc;
351 #ifdef HAVE_XDBE
352         XdbeBackBuffer back_buffer;
353 #endif
354 #ifdef XFT
355         XftDraw *xftdraw;
356 #endif
357
358         int width;
359         int height;
360 #ifdef OWN_WINDOW
361         char wm_class_name[256];
362         int x;
363         int y;
364         unsigned int type;
365         unsigned long hints;
366 #endif
367 };
368
369 #ifdef HAVE_XDBE
370 extern int use_xdbe;
371 #endif
372
373
374 #ifdef XFT
375 extern int use_xft;
376 #endif
377
378 extern Display *display;
379 extern int display_width;
380 extern int display_height;
381 extern int screen;
382
383 extern int workarea[4];
384
385 extern struct conky_window window;
386
387 void init_X11();
388 void init_window(int use_own_window, int width, int height, int set_trans, int back_colour, char * nodename,
389                  char **argv, int argc);
390 void create_gc();
391 void set_transparent_background(Window win);
392 long get_x11_color(const char *);
393
394 #endif /* X11 */
395
396 /* in common.c */
397
398 /* struct that has all info */
399 struct information info;
400
401 void signal_handler(int);
402 void reload_config(void);
403 void clean_up(void);
404
405 void update_uname();
406 double get_time(void);
407 FILE *open_file(const char *file, int *reported);
408 void variable_substitute(const char *s, char *dest, unsigned int n);
409 void format_seconds(char *buf, unsigned int n, long t);
410 void format_seconds_short(char *buf, unsigned int n, long t);
411 struct net_stat *get_net_stat(const char *dev);
412
413 void update_stuff();
414
415 int round_to_int(float f);
416
417 #define SET_NEED(a) need_mask |= 1 << (a)
418 extern unsigned long long need_mask;
419
420 extern double current_update_time, last_update_time;
421
422 extern int no_buffers;
423
424 /* system dependant (in linux.c) */
425
426 void update_diskio(void);
427 void prepare_update(void);
428 void update_uptime(void);
429 void update_meminfo(void);
430 void update_net_stats(void);
431 void update_wifi_stats(void);
432 void update_cpu_usage(void);
433 void update_total_processes(void);
434 void update_running_processes(void);
435 void update_i8k(void);
436 char get_freq( char *, size_t, char *, int, unsigned int ); 
437 void get_freq_dynamic( char *, size_t, char *, int ); 
438 char get_voltage(char *, size_t, char *, int, unsigned int ); /* ptarjan */
439 void update_load_average();
440 int open_i2c_sensor(const char *dev, const char *type, int n, int *div,
441                     char *devtype);
442 double get_i2c_info(int *fd, int arg, char *devtype, char *type);
443
444 void get_adt746x_cpu( char *, size_t ); 
445 void get_adt746x_fan( char *, size_t ); 
446 unsigned int get_diskio(void);
447
448 int open_acpi_temperature(const char *name);
449 double get_acpi_temperature(int fd);
450 void get_acpi_ac_adapter( char *, size_t ); 
451 void get_acpi_fan( char *, size_t ); 
452 void get_battery_stuff(char *buf, unsigned int n, const char *bat);
453 void get_ibm_acpi_fan(char *buf, size_t client_buffer_size);
454 void get_ibm_acpi_temps(void);
455 void get_ibm_acpi_volume(char *buf, size_t client_buffer_size);
456 void get_ibm_acpi_brightness(char *buf, size_t client_buffer_size);
457 void get_cpu_count();
458
459 struct ibm_acpi_struct {
460     unsigned int temps[8];
461 };
462
463 struct ibm_acpi_struct ibm_acpi;
464
465 enum { PB_BATT_STATUS, PB_BATT_PERCENT, PB_BATT_TIME};
466 void get_powerbook_batt_info(char*, size_t, int);
467
468 struct process {
469         struct process *next;
470         struct process *previous;
471
472         pid_t pid;
473         char *name;
474         float amount;
475         unsigned long user_time;
476         unsigned long total;
477         unsigned long kernel_time;
478         unsigned long previous_user_time;
479         unsigned long previous_kernel_time;
480         unsigned int vsize;
481         unsigned int rss;
482         unsigned int time_stamp;
483         unsigned int counted;
484         unsigned int changed;
485         float totalmem;
486 };
487
488 void update_top();
489 void free_all_processes();
490 struct process *get_first_process();
491
492 /* fs-stuff is possibly system dependant (in fs.c) */
493
494 void update_fs_stats(void);
495 struct fs_stat *prepare_fs_stat(const char *path);
496 void clear_fs_stats(void);
497
498 /* in mixer.c */
499
500 int mixer_init(const char *);
501 int mixer_get_avg(int);
502 int mixer_get_left(int);
503 int mixer_get_right(int);
504
505 /* in mail.c */
506
507 extern char *current_mail_spool;
508
509 void update_mail_count();
510
511 /* in freebsd.c */
512 #if defined(__FreeBSD__)
513 kvm_t *kd;
514 #endif
515
516 #if defined(__FreeBSD__) && (defined(i386) || defined(__i386__))
517 int apm_getinfo(int fd, apm_info_t aip);
518 char *get_apm_adapter(void);
519 char *get_apm_battery_life(void);
520 char *get_apm_battery_time(void);
521 #endif
522
523 /* in mpd.c */
524 #ifdef MPD
525 void update_mpd();
526 #endif
527
528 /* in xmms2.c */
529 #ifdef XMMS2
530 void update_xmms2();
531 #endif
532
533 /* in hddtemp.c */
534 #ifdef HDDTEMP
535 int scan_hddtemp(const char *arg, char **dev, char **addr, int *port);
536 char *get_hddtemp_info(char *dev, char *addr, int port, char *unit);
537 #endif /* HDDTEMP */
538
539 /* in linux.c */
540
541 #endif
542
543