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