* Applied 12 patches:
[monky] / src / conky.h
1 /* Conky, a system monitor, based on torsmo
2  *
3  * Any original torsmo code is licensed under the BSD license
4  *
5  * All code written since the fork of torsmo is licensed under the GPL
6  *
7  * Please see COPYING for details
8  *
9  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
10  * Copyright (c) 2005-2007 Brenden Matthews, Philip Kovacs, et. al.
11  *      (see AUTHORS)
12  * All rights reserved.
13  *
14  * This program is free software: you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation, either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  * You should have received a copy of the GNU General Public License
24  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25  *
26  * $Id$
27  *
28  */
29
30 #ifndef _conky_h_
31 #define _conky_h_
32
33 #if defined(HAS_MCHECK_H)
34 #include <mcheck.h>
35 #endif /* HAS_MCHECK_H */
36
37 #include "config.h"
38 #include <sys/utsname.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <locale.h>
43 #include <langinfo.h>
44 #include <wchar.h>
45 #include <sys/param.h>
46
47 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
48 #include <sys/mount.h>
49 #include <sys/ucred.h>
50 #include <fcntl.h>
51 #include <kvm.h>
52 #if (defined(i386) || defined(__i386__))
53 #include <machine/apm_bios.h>
54 #endif /* i386 || __i386__ */
55 #endif /* __FreeBSD__ */
56
57 #if defined(__OpenBSD__)
58 #include <sys/sysctl.h>
59 #include <sys/sensors.h>
60 #include <machine/apmvar.h>
61 #endif /* __OpenBSD__ */
62
63 #ifdef AUDACIOUS
64 #include "audacious.h"
65 #endif
66
67 #ifdef XMMS2
68 #include <xmmsclient/xmmsclient.h>
69 #endif
70
71 #ifdef RSS
72 #include "prss.h"
73 #endif
74
75 #ifdef SMAPI
76 #include "smapi.h"
77 #endif
78
79 #include "mboxscan.h"
80 #include "timed_thread.h"
81
82 #define TOP_CPU 1
83 #define TOP_NAME 2
84 #define TOP_PID 3
85 #define TOP_MEM 4
86 #define TOP_TIME 5
87
88 #define TEXT_BUFFER_SIZE 1280
89 #define P_MAX_SIZE ((TEXT_BUFFER_SIZE * 4) - 2)
90 extern unsigned int text_buffer_size;
91
92 /* maximum number of special things, e.g. fonts, offsets, aligns, etc. */
93 #define MAX_SPECIALS_DEFAULT 512
94
95 /* maximum size of config TEXT buffer, i.e. below TEXT line. */
96 #define MAX_USER_TEXT_DEFAULT 16384
97
98 #include <sys/socket.h>
99
100 #define ERR(s, varargs...) fprintf(stderr, "Conky: " s "\n", ##varargs)
101
102 /* critical error */
103 #define CRIT_ERR(s, varargs...) \
104         { fprintf(stderr, "Conky: " s "\n", ##varargs); exit(EXIT_FAILURE); }
105
106 struct i8k_struct {
107         char *version;
108         char *bios;
109         char *serial;
110         char *cpu_temp;
111         char *left_fan_status;
112         char *right_fan_status;
113         char *left_fan_rpm;
114         char *right_fan_rpm;
115         char *ac_status;
116         char *buttons_status;
117 };
118
119 struct i8k_struct i8k;
120
121 struct net_stat {
122         const char *dev;
123         int up;
124         long long last_read_recv, last_read_trans;
125         long long recv, trans;
126         double recv_speed, trans_speed;
127         struct sockaddr addr;
128     char* addrs;
129         double net_rec[15], net_trans[15];
130         // wireless extensions
131         char essid[32];
132         char bitrate[16];
133         char mode[16];
134         int link_qual;
135         int link_qual_max;
136         char ap[18];
137 };
138
139 unsigned int diskio_value;
140 unsigned int diskio_read_value;
141 unsigned int diskio_write_value;
142
143 struct fs_stat {
144         char *path;
145         char *type;
146         long long size;
147         long long avail;
148         long long free;
149 };
150
151 #include "diskio.h"
152
153 struct mail_s {                 // for imap and pop3
154         unsigned long unseen;
155         unsigned long messages;
156         unsigned long used;
157         unsigned long quota;
158         unsigned long port;
159         float interval;
160         double last_update;
161         char host[128];
162         char user[128];
163         char pass[128];
164         char command[1024];
165         char folder[128];
166         timed_thread *p_timed_thread;
167         char secure;
168 } mail;
169
170 /* struct cpu_stat {
171         unsigned int user, nice, system, idle, iowait, irq, softirq;
172         int cpu_avg_samples;
173 }; */
174
175 #ifdef MPD
176 struct mpd_s {
177         char *title;
178         char *artist;
179         char *album;
180         char *status;
181         char *random;
182         char *repeat;
183         char *track;
184         char *name;
185         char *file;
186         int volume;
187         unsigned int port;
188         char host[128];
189         char password[128];
190         float progress;
191         int bitrate;
192         int length;
193         int elapsed;
194         int max_title_len;              /* e.g. ${mpd_title 50} */
195 };
196
197 #endif
198
199 #ifdef XMMS2
200 struct xmms2_s {
201         char *artist;
202         char *album;
203         char *title;
204         char *genre;
205         char *comment;
206         char *url;
207         char *date;
208         char* playlist;
209         int tracknr;
210         int bitrate;
211         unsigned int id;
212         int duration;
213         int elapsed;
214         int timesplayed;
215         float size;
216
217         float progress;
218         char *status;
219 };
220 #endif
221
222 #ifdef AUDACIOUS
223 struct audacious_s {
224         audacious_t items;      /* e.g. items[AUDACIOUS_STATUS] */
225         int max_title_len;      /* e.g. ${audacious_title 50} */
226         timed_thread *p_timed_thread;
227 };
228 #endif
229
230 #ifdef BMPX
231 void update_bmpx();
232 struct bmpx_s {
233         char *title;
234         char *artist;
235         char *album;
236         char *uri;
237         int bitrate;
238         int track;
239 };
240 #endif
241
242 void update_entropy();
243 struct entropy_s {
244         unsigned int entropy_avail;
245         unsigned int poolsize;
246 };
247
248 struct usr_info {
249         char *names;
250         char *times;
251         char *terms;
252         int number;
253 };
254
255 #ifdef TCP_PORT_MONITOR
256 #include "libtcp-portmon.h"
257 #define MAX_PORT_MONITOR_CONNECTIONS_DEFAULT 256
258 #endif
259
260 enum {
261         INFO_CPU = 0,
262         INFO_MAIL = 1,
263         INFO_MEM = 2,
264         INFO_NET = 3,
265         INFO_PROCS = 4,
266         INFO_RUN_PROCS = 5,
267         INFO_UPTIME = 6,
268         INFO_BUFFERS = 7,
269         INFO_FS = 8,
270         INFO_SYSFS = 9,
271         INFO_MIXER = 10,
272         INFO_LOADAVG = 11,
273         INFO_UNAME = 12,
274         INFO_FREQ = 13,
275 #ifdef MPD
276         INFO_MPD = 14,
277 #endif
278         INFO_TOP = 15,
279         INFO_WIFI = 16,
280         INFO_DISKIO = 17,
281         INFO_I8K = 18,
282 #ifdef TCP_PORT_MONITOR
283         INFO_TCP_PORT_MONITOR = 19,
284 #endif
285 #ifdef AUDACIOUS
286         INFO_AUDACIOUS = 20,
287 #endif
288 #ifdef BMPX
289         INFO_BMPX = 21,
290 #endif
291 #ifdef XMMS2
292         INFO_XMMS2 = 22,
293 #endif
294         INFO_ENTROPY = 23,
295 #ifdef RSS
296         INFO_RSS = 24,
297 #endif
298 #ifdef SMAPI
299         INFO_SMAPI = 25,
300 #endif
301         INFO_USERS = 26,
302 };
303
304 /* get_battery_stuff() item selector */
305 enum {
306         BATTERY_STATUS,
307         BATTERY_TIME
308 };
309
310 #ifdef MPD
311 #include "libmpdclient.h"
312 #endif
313
314 /* Update interval */
315 double update_interval;
316
317 volatile int g_signal_pending;
318
319 struct information {
320         unsigned int mask;
321
322         struct utsname uname_s;
323
324         char freq[10];
325
326         double uptime;
327
328         /* memory information in kilobytes */
329         unsigned long long mem, memmax, swap, swapmax;
330         unsigned long long bufmem, buffers, cached;
331
332         unsigned short procs;
333         unsigned short run_procs;
334
335         float *cpu_usage;
336         /* struct cpu_stat cpu_summed; what the hell is this? */
337         unsigned int cpu_count;
338         unsigned int cpu_avg_samples;
339
340         unsigned int net_avg_samples;
341
342         float loadavg[3];
343
344         struct mail_s *mail;
345         int mail_running;
346 #ifdef MPD
347         struct mpd_s mpd;
348         mpd_Connection *conn;
349 #endif
350 #ifdef XMMS2
351         struct xmms2_s xmms2;
352         int xmms2_conn_state;
353         xmms_socket_t xmms2_fd;
354         fd_set xmms2_fdset;
355         xmmsc_connection_t *xmms2_conn;
356 #endif
357 #ifdef AUDACIOUS
358         struct audacious_s audacious;
359 #endif
360 #ifdef BMPX
361         struct bmpx_s bmpx;
362 #endif
363         struct usr_info users;
364         struct process *cpu[10];
365         struct process *memu[10];
366         struct process *first_process;
367         unsigned long looped;
368 #ifdef TCP_PORT_MONITOR
369         tcp_port_monitor_collection_t *p_tcp_port_monitor_collection;
370 #endif
371         struct entropy_s entropy;
372         double music_player_interval;
373
374         short kflags;   /* kernel settings, see enum KFLAG */
375 };
376
377 enum {
378         /* set to true if kernel uses "long" format for /proc/stats */
379         KFLAG_IS_LONGSTAT = 0x01,
380         /* set to true if kernel shows # of threads for the proc value
381          * in sysinfo() call */
382         KFLAG_PROC_IS_THREADS = 0x02
383         /* bits 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 available for future use */
384         /* KFLAG_NEXT_ONE = 0x04 */
385 };
386
387 #define KFLAG_SETON(a) info.kflags |= a
388 #define KFLAG_SETOFF(a) info.kflags &= (~a)
389 #define KFLAG_FLIP(a) info.kflags ^= a
390 #define KFLAG_ISSET(a) info.kflags & a
391
392 int out_to_console;
393
394 int top_cpu;
395 int top_mem;
396
397 int use_spacer;
398
399 enum spacer_opts { NO_SPACER = 0, LEFT_SPACER, RIGHT_SPACER };
400
401 char tmpstring1[TEXT_BUFFER_SIZE];
402 char tmpstring2[TEXT_BUFFER_SIZE];
403
404 #ifdef X11
405 /* in x11.c */
406
407 #include <X11/Xlib.h>
408 #include <X11/Xlib.h>
409 #include <X11/Xatom.h>
410
411 #ifdef XFT
412 #include <X11/Xft/Xft.h>
413 #endif
414
415 #ifdef HAVE_XDBE
416 #include <X11/extensions/Xdbe.h>
417 #endif
418
419 #define ATOM(a) XInternAtom(display, #a, False)
420
421 #ifdef OWN_WINDOW
422 enum _window_type {
423         TYPE_NORMAL = 0,
424         TYPE_DESKTOP,
425         TYPE_OVERRIDE
426 };
427
428 enum _window_hints {
429         HINT_UNDECORATED = 0,
430         HINT_BELOW,
431         HINT_ABOVE,
432         HINT_STICKY,
433         HINT_SKIP_TASKBAR,
434         HINT_SKIP_PAGER
435 };
436
437 #define SET_HINT(mask, hint)    (mask |= (1 << hint))
438 #define TEST_HINT(mask, hint)   (mask & (1 << hint))
439 #endif
440
441 struct conky_window {
442         Window root, window, desktop;
443         Drawable drawable;
444         GC gc;
445 #ifdef HAVE_XDBE
446         XdbeBackBuffer back_buffer;
447 #endif
448 #ifdef XFT
449         XftDraw *xftdraw;
450 #endif
451
452         int width;
453         int height;
454 #ifdef OWN_WINDOW
455         char class_name[256];
456         char title[256];
457         int x;
458         int y;
459         unsigned int type;
460         unsigned long hints;
461 #endif
462 };
463
464 #ifdef HAVE_XDBE
465 extern int use_xdbe;
466 #endif
467
468 #ifdef XFT
469 extern int use_xft;
470 #endif
471
472 extern Display *display;
473 extern int display_width;
474 extern int display_height;
475 extern int screen;
476
477 extern int workarea[4];
478
479 extern struct conky_window window;
480
481 void init_X11();
482 void init_window(int use_own_window, int width, int height, int set_trans,
483         int back_colour, char **argv, int argc);
484 void create_gc();
485 void set_transparent_background(Window win);
486 long get_x11_color(const char *);
487
488 #endif /* X11 */
489
490 int cpu_separate;
491 int short_units;
492
493 /* in common.c */
494
495 /* struct that has all info */
496 struct information info;
497
498 void signal_handler(int);
499 void reload_config(void);
500 void clean_up(void);
501
502 void update_uname();
503 double get_time(void);
504 FILE *open_file(const char *file, int *reported);
505 void variable_substitute(const char *s, char *dest, unsigned int n);
506 void format_seconds(char *buf, unsigned int n, long t);
507 void format_seconds_short(char *buf, unsigned int n, long t);
508 struct net_stat *get_net_stat(const char *dev);
509 void clear_net_stats(void);
510 void update_users();
511
512 void update_stuff();
513
514 int round_to_int(float f);
515
516 extern unsigned long long need_mask;
517
518 extern double current_update_time, last_update_time;
519
520 extern int no_buffers;
521
522 /* system dependant (in linux.c) */
523
524 int check_mount(char *s);
525 void update_diskio(void);
526 void prepare_update(void);
527 void update_uptime(void);
528 void update_meminfo(void);
529 void update_net_stats(void);
530 void update_cpu_usage(void);
531 void update_total_processes(void);
532 void update_running_processes(void);
533 void update_i8k(void);
534 char get_freq(char *, size_t, char *, int, unsigned int);
535 void get_freq_dynamic(char *, size_t, char *, int);
536 char get_voltage(char *, size_t, char *, int, unsigned int);    /* ptarjan */
537 void update_load_average();
538
539 int open_sysfs_sensor(const char *dir, const char *dev, const char *type, int n,
540         int *div, char *devtype);
541
542 #define open_i2c_sensor(dev, type, n, div, devtype) \
543         open_sysfs_sensor("/sys/bus/i2c/devices/", dev, type, n, div, devtype)
544 #define open_platform_sensor(dev, type, n, div, devtype) \
545         open_sysfs_sensor("/sys/bus/platform/devices/", dev, type, n, div, devtype)
546 #define open_hwmon_sensor(dev, type, n, div, devtype) \
547         open_sysfs_sensor("/sys/class/hwmon/", dev, type, n, div, devtype)
548
549 double get_sysfs_info(int *fd, int arg, char *devtype, char *type);
550
551 void get_adt746x_cpu(char *, size_t);
552 void get_adt746x_fan(char *, size_t);
553 unsigned int get_diskio(void);
554
555 int open_acpi_temperature(const char *name);
556 double get_acpi_temperature(int fd);
557 void get_acpi_ac_adapter(char *, size_t);
558 void get_acpi_fan(char *, size_t);
559 void get_battery_stuff(char *buf, unsigned int n, const char *bat, int item);
560 int get_battery_perct(const char *bat);
561 int get_battery_perct_bar(const char *bat);
562 void get_ibm_acpi_fan(char *buf, size_t client_buffer_size);
563 void get_ibm_acpi_temps(void);
564 void get_ibm_acpi_volume(char *buf, size_t client_buffer_size);
565 void get_ibm_acpi_brightness(char *buf, size_t client_buffer_size);
566 void get_cpu_count();
567
568 struct ibm_acpi_struct {
569         unsigned int temps[8];
570 };
571
572 struct ibm_acpi_struct ibm_acpi;
573
574 #if defined(__OpenBSD__)
575 void update_obsd_sensors(void);
576 void get_obsd_vendor(char *buf, size_t client_buffer_size);
577 void get_obsd_product(char *buf, size_t client_buffer_size);
578
579 #define OBSD_MAX_SENSORS 256
580 struct obsd_sensors_struct {
581         int device;
582         float temp[MAXSENSORDEVICES][OBSD_MAX_SENSORS];
583         unsigned int fan[MAXSENSORDEVICES][OBSD_MAX_SENSORS];
584         float volt[MAXSENSORDEVICES][OBSD_MAX_SENSORS];
585 };
586 struct obsd_sensors_struct obsd_sensors;
587 #endif /* __OpenBSD__ */
588
589 enum { PB_BATT_STATUS, PB_BATT_PERCENT, PB_BATT_TIME };
590 void get_powerbook_batt_info(char *, size_t, int);
591
592 struct process {
593         struct process *next;
594         struct process *previous;
595
596         pid_t pid;
597         char *name;
598         float amount;
599         unsigned long user_time;
600         unsigned long total;
601         unsigned long kernel_time;
602         unsigned long previous_user_time;
603         unsigned long previous_kernel_time;
604         unsigned long total_cpu_time;
605         unsigned int vsize;
606         unsigned int rss;
607         unsigned int time_stamp;
608         unsigned int counted;
609         unsigned int changed;
610         float totalmem;
611 };
612
613 struct local_mail_s {
614         char *box;
615         int mail_count;
616         int new_mail_count;
617         float interval;
618         time_t last_mtime;
619         double last_update;
620 };
621
622 void update_top();
623 void free_all_processes();
624 struct process *get_first_process();
625
626 /* fs-stuff is possibly system dependant (in fs.c) */
627
628 void update_fs_stats(void);
629 struct fs_stat *prepare_fs_stat(const char *path);
630 void clear_fs_stats(void);
631
632 /* in mixer.c */
633
634 int mixer_init(const char *);
635 int mixer_get_avg(int);
636 int mixer_get_left(int);
637 int mixer_get_right(int);
638
639 /* in mail.c */
640
641 extern char *current_mail_spool;
642
643 void update_mail_count();
644
645 /* in freebsd.c */
646 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
647 kvm_t *kd;
648 #endif
649
650 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
651                 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
652 #ifdef __OpenBSD__
653 typedef struct apm_power_info *apm_info_t;
654 #endif
655 int apm_getinfo(int fd, apm_info_t aip);
656 char *get_apm_adapter(void);
657 char *get_apm_battery_life(void);
658 char *get_apm_battery_time(void);
659 #endif
660
661 /* in mpd.c */
662 #ifdef MPD
663 extern void init_mpd_stats(struct information *current_info);
664 void *update_mpd(void);
665 extern timed_thread *mpd_timed_thread;
666 #endif /* MPD */
667
668 /* in xmms2.c */
669 #ifdef XMMS2
670 void update_xmms2();
671 #endif
672
673 /* in hddtemp.c */
674 #ifdef HDDTEMP
675 int scan_hddtemp(const char *arg, char **dev, char **addr, int *port);
676 char *get_hddtemp_info(char *dev, char *addr, int port, char *unit);
677 #endif /* HDDTEMP */
678
679 /* in rss.c */
680 #ifdef RSS
681 PRSS *get_rss_info(char *uri, int delay);
682 void init_rss_info();
683 void free_rss_info();
684 #endif /* RSS */
685
686 /* in linux.c */
687
688 #endif