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