remove infopipe kruft
[monky] / src / freebsd.c
1 /*
2  * freebsd.c
3  * Contains FreeBSD specific stuff
4  *
5  * $Id$
6  */
7
8 #include <sys/dkstat.h>
9 #include <sys/param.h>
10 #include <sys/resource.h>
11 #include <sys/socket.h>
12 #include <sys/sysctl.h>
13 #include <sys/time.h>
14 #include <sys/types.h>
15 #include <sys/vmmeter.h>
16 #include <sys/user.h>
17 #include <sys/ioctl.h>
18
19 #include <net/if.h>
20 #include <net/if_mib.h>
21 #include <net/if_media.h>
22 #include <net/if_var.h>
23 #include <netinet/in.h>
24
25 #include <devstat.h>
26 #include <fcntl.h>
27 #include <ifaddrs.h>
28 #include <limits.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33
34 #include <dev/wi/if_wavelan_ieee.h>
35
36 #include "conky.h"
37
38 #define GETSYSCTL(name, var)    getsysctl(name, &(var), sizeof (var))
39 #define KELVTOC(x)              ((x - 2732) / 10.0)
40 #define MAXSHOWDEVS             16
41
42 #if 0
43 #define FREEBSD_DEBUG
44 #endif
45
46 inline void proc_find_top(struct process **cpu, struct process **mem);
47
48 u_int64_t diskio_prev = 0;
49 static short cpu_setup = 0;
50 static short diskio_setup = 0;
51
52 static int getsysctl(char *name, void *ptr, size_t len)
53 {
54         size_t nlen = len;
55         if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) {
56                 return (-1);
57         }
58
59         if (nlen != len) {
60                 return (-1);
61         }
62
63         return (0);
64 }
65
66 struct ifmibdata *data = NULL;
67 size_t len = 0;
68
69 static int swapmode(int *retavail, int *retfree)
70 {
71         int n;
72         int pagesize = getpagesize();
73         struct kvm_swap swapary[1];
74
75         *retavail = 0;
76         *retfree = 0;
77
78 #define CONVERT(v)      ((quad_t)(v) * pagesize / 1024)
79
80         n = kvm_getswapinfo(kd, swapary, 1, 0);
81         if (n < 0 || swapary[0].ksw_total == 0)
82                 return (0);
83
84         *retavail = CONVERT(swapary[0].ksw_total);
85         *retfree = CONVERT(swapary[0].ksw_total - swapary[0].ksw_used);
86
87         n = (int) ((double) swapary[0].ksw_used * 100.0 /
88                 (double) swapary[0].ksw_total);
89
90         return (n);
91 }
92
93 void
94 prepare_update()
95 {
96 }
97
98 void
99 update_uptime()
100 {
101         int mib[2] = { CTL_KERN, KERN_BOOTTIME };
102         struct timeval boottime;
103         time_t now;
104         size_t size = sizeof (boottime);
105
106         if ((sysctl(mib, 2, &boottime, &size, NULL, 0) != -1) &&
107                         (boottime.tv_sec != 0)) {
108                 time(&now);
109                 info.uptime = now - boottime.tv_sec;
110         } else {
111                 fprintf(stderr, "Could not get uptime\n");
112                 info.uptime = 0;
113         }
114 }
115
116 void
117 update_meminfo()
118 {
119         int total_pages, inactive_pages, free_pages;
120         int swap_avail, swap_free;
121
122         int pagesize = getpagesize();
123
124         if (GETSYSCTL("vm.stats.vm.v_page_count", total_pages))
125                 fprintf(stderr,
126                         "Cannot read sysctl \"vm.stats.vm.v_page_count\"");
127
128         if (GETSYSCTL("vm.stats.vm.v_free_count", free_pages))
129                 fprintf(stderr,
130                         "Cannot read sysctl \"vm.stats.vm.v_free_count\"");
131
132         if (GETSYSCTL("vm.stats.vm.v_inactive_count", inactive_pages))
133                 fprintf(stderr,
134                         "Cannot read sysctl \"vm.stats.vm.v_inactive_count\"");
135
136         info.memmax = (total_pages * pagesize) >> 10;
137         info.mem =
138             ((total_pages - free_pages - inactive_pages) * pagesize) >> 10;
139
140
141         if ((swapmode(&swap_avail, &swap_free)) >= 0) {
142                 info.swapmax = swap_avail;
143                 info.swap = (swap_avail - swap_free);
144         } else {
145                 info.swapmax = 0;
146                 info.swap = 0;
147         }
148 }
149
150 void
151 update_net_stats()
152 {
153         struct net_stat *ns;
154         double delta;
155         long long r, t, last_recv, last_trans;
156         struct ifaddrs *ifap, *ifa;
157         struct if_data *ifd;
158
159
160         /* get delta */
161         delta = current_update_time - last_update_time;
162         if (delta <= 0.0001)
163                 return;
164
165         if (getifaddrs(&ifap) < 0)
166                 return;
167
168         for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
169                 ns = get_net_stat((const char *) ifa->ifa_name);
170
171                 if (ifa->ifa_flags & IFF_UP) {
172                         struct ifaddrs *iftmp;
173
174                         ns->up = 1;
175                         ns->linkstatus = 1;
176                         last_recv = ns->recv;
177                         last_trans = ns->trans;
178
179                         if (ifa->ifa_addr->sa_family != AF_LINK)
180                                 continue;
181
182                         for (iftmp = ifa->ifa_next; iftmp != NULL &&
183                                 strcmp(ifa->ifa_name, iftmp->ifa_name) == 0;
184                                 iftmp = iftmp->ifa_next)
185                                 if (iftmp->ifa_addr->sa_family == AF_INET)
186                                         memcpy(&(ns->addr), iftmp->ifa_addr,
187                                                 iftmp->ifa_addr->sa_len);
188
189                         ifd = (struct if_data *) ifa->ifa_data;
190                         r = ifd->ifi_ibytes;
191                         t = ifd->ifi_obytes;
192
193                         if (r < ns->last_read_recv)
194                                 ns->recv +=
195                                     ((long long) 4294967295U -
196                                         ns->last_read_recv) + r;
197                         else
198                                 ns->recv += (r - ns->last_read_recv);
199
200                         ns->last_read_recv = r;
201
202                         if (t < ns->last_read_trans)
203                                 ns->trans +=
204                                     ((long long) 4294967295U -
205                                         ns->last_read_trans) + t;
206                         else
207                                 ns->trans += (t - ns->last_read_trans);
208
209                         ns->last_read_trans = t;
210
211                         /* calculate speeds */
212                         ns->recv_speed = (ns->recv - last_recv) / delta;
213                         ns->trans_speed = (ns->trans - last_trans) / delta;
214                 } else {
215                         ns->up = 0;
216                         ns->linkstatus = 0;
217                 }
218         }
219
220         freeifaddrs(ifap);
221 }
222
223 void
224 update_total_processes()
225 {
226         int n_processes;
227
228         kvm_getprocs(kd, KERN_PROC_ALL, 0, &n_processes);
229
230         info.procs = n_processes;
231 }
232
233 void
234 update_running_processes()
235 {
236         struct kinfo_proc *p;
237         int n_processes;
238         int i, cnt = 0;
239
240         p = kvm_getprocs(kd, KERN_PROC_ALL, 0, &n_processes);
241         for (i = 0; i < n_processes; i++) {
242 #if __FreeBSD__ < 5
243                 if (p[i].kp_proc.p_stat == SRUN)
244 #else
245                 if (p[i].ki_stat == SRUN)
246 #endif
247                         cnt++;
248         }
249
250         info.run_procs = cnt;
251 }
252
253 struct cpu_load_struct {
254         unsigned long load[5];
255 };
256
257 struct cpu_load_struct fresh = { {0, 0, 0, 0, 0} };
258 long cpu_used, oldtotal, oldused;
259
260 void
261 get_cpu_count()
262 {
263         /* int cpu_count = 0; */
264
265         /*
266          * XXX
267          * FreeBSD doesn't allow to get per CPU load stats
268          * on SMP machines. It's possible to get a CPU count,
269          * but as we fulfil only info.cpu_usage[0], it's better
270          * to report there's only one CPU. It should fix some bugs
271          * (e.g. cpugraph)
272          */
273 #if 0
274         if (GETSYSCTL("hw.ncpu", cpu_count) == 0)
275                 info.cpu_count = cpu_count;
276 #endif
277         info.cpu_count = 1;
278
279         info.cpu_usage = malloc(info.cpu_count * sizeof (float));
280         if (info.cpu_usage == NULL)
281                 CRIT_ERR("malloc");
282 }
283
284 /* XXX: SMP support */
285 void
286 update_cpu_usage()
287 {
288         long used, total;
289         long cp_time[CPUSTATES];
290         size_t len = sizeof (cp_time);
291
292         if (cpu_setup == 0) {
293                 get_cpu_count();
294                 cpu_setup = 1;
295         }
296
297         if (sysctlbyname("kern.cp_time", &cp_time, &len, NULL, 0) < 0) {
298                 (void) fprintf(stderr, "Cannot get kern.cp_time");
299         }
300
301         fresh.load[0] = cp_time[CP_USER];
302         fresh.load[1] = cp_time[CP_NICE];
303         fresh.load[2] = cp_time[CP_SYS];
304         fresh.load[3] = cp_time[CP_IDLE];
305         fresh.load[4] = cp_time[CP_IDLE];
306
307         used = fresh.load[0] + fresh.load[1] + fresh.load[2];
308         total =
309             fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
310
311         if ((total - oldtotal) != 0) {
312                 info.cpu_usage[0] = ((double) (used - oldused)) /
313                         (double) (total - oldtotal);
314         } else {
315                 info.cpu_usage[0] = 0;
316         }
317
318         oldused = used;
319         oldtotal = total;
320 }
321
322 double
323 get_i2c_info(int *fd, int arg, char *devtype, char *type)
324 {
325         return (0);
326 }
327
328 void
329 update_load_average()
330 {
331         double v[3];
332         getloadavg(v, 3);
333
334         info.loadavg[0] = (float) v[0];
335         info.loadavg[1] = (float) v[1];
336         info.loadavg[2] = (float) v[2];
337 }
338
339 double
340 get_acpi_temperature(int fd)
341 {
342         int temp;
343
344         if (GETSYSCTL("hw.acpi.thermal.tz0.temperature", temp)) {
345                 fprintf(stderr,
346                 "Cannot read sysctl \"hw.acpi.thermal.tz0.temperature\"\n");
347                 return (0.0);
348         }
349
350         return (KELVTOC(temp));
351 }
352
353 void
354 get_battery_stuff(char *buf, unsigned int n, const char *bat)
355 {
356         int battime, batcapacity, batstate, ac;
357
358         if (GETSYSCTL("hw.acpi.battery.time", battime))
359                 (void) fprintf(stderr,
360                         "Cannot read sysctl \"hw.acpi.battery.time\"\n");
361         if (GETSYSCTL("hw.acpi.battery.life", batcapacity))
362                 (void) fprintf(stderr,
363                                            "Cannot read sysctl \"hw.acpi.battery.life\"\n");
364
365         if (GETSYSCTL("hw.acpi.battery.state", batstate))
366                 (void) fprintf(stderr,
367                                            "Cannot read sysctl \"hw.acpi.battery.state\"\n");
368
369         if (GETSYSCTL("hw.acpi.acline", ac))
370                 (void) fprintf(stderr,
371                                            "Cannot read sysctl \"hw.acpi.acline\"\n");
372
373         if (batstate == 1) {
374                 if (battime != -1)
375                         snprintf(buf, n, "remaining %d%% (%d:%2.2d)",
376                                         batcapacity, battime / 60, battime % 60);
377                 else
378                         /* no time estimate available yet */
379                         snprintf(buf, n, "remaining %d%%",
380                                         batcapacity);
381                 if (ac == 1)
382                         (void) fprintf(stderr, "Discharging while on AC!\n");
383         } else {
384                 snprintf(buf, n, batstate == 2 ? "charging (%d%%)" : "charged (%d%%)", batcapacity);
385                 if (batstate != 2 && batstate != 0)
386                         (void) fprintf(stderr, "Unknow battery state %d!\n", batstate);
387                 if (ac == 0)
388                         (void) fprintf(stderr, "Charging while not on AC!\n");
389         }
390
391 }
392
393 int
394 open_i2c_sensor(const char *dev, const char *type, int n, int *div,
395                 char *devtype)
396 {
397         return (0);
398 }
399
400 int
401 open_acpi_temperature(const char *name)
402 {
403         return (0);
404 }
405
406 void
407 get_acpi_ac_adapter(char *p_client_buffer, size_t client_buffer_size)
408 {
409         int state;
410
411         if (!p_client_buffer || client_buffer_size <= 0)
412                 return;
413
414         if (GETSYSCTL("hw.acpi.acline", state)) {
415                 fprintf(stderr,
416                         "Cannot read sysctl \"hw.acpi.acline\"\n");
417                 return;
418         }
419
420
421         if (state)
422                 strncpy(p_client_buffer, "Running on AC Power",
423                                 client_buffer_size);
424         else
425                 strncpy(p_client_buffer, "Running on battery",
426                                 client_buffer_size);
427
428 }
429
430 void
431 get_acpi_fan(char *p_client_buffer, size_t client_buffer_size)
432 {
433         if (!p_client_buffer || client_buffer_size <= 0)
434                 return;
435
436         /* not implemented */
437         memset(p_client_buffer, 0, client_buffer_size);
438 }
439
440 void
441 get_adt746x_cpu(char *p_client_buffer, size_t client_buffer_size)
442 {
443         if (!p_client_buffer || client_buffer_size <= 0)
444                 return;
445
446         /* not implemented */
447         memset(p_client_buffer, 0, client_buffer_size);
448 }
449
450 void
451 get_adt746x_fan(char *p_client_buffer, size_t client_buffer_size)
452 {
453         if (!p_client_buffer || client_buffer_size <= 0)
454                 return;
455
456         /* not implemented */
457         memset(p_client_buffer, 0, client_buffer_size);
458 }
459
460 /* rdtsc() and get_freq_dynamic() copied from linux.c */
461
462 #if  defined(__i386) || defined(__x86_64)
463 __inline__ unsigned long long int
464 rdtsc()
465 {
466         unsigned long long int x;
467         __asm__ volatile(".byte 0x0f, 0x31":"=A" (x));
468         return (x);
469 }
470 #endif
471
472 /* return system frequency in MHz (use divisor=1) or GHz (use divisor=1000) */
473 void
474 get_freq_dynamic(char *p_client_buffer, size_t client_buffer_size,
475                 char *p_format, int divisor)
476 {
477 #if  defined(__i386) || defined(__x86_64)
478         struct timezone tz;
479         struct timeval tvstart, tvstop;
480         unsigned long long cycles[2];   /* gotta be 64 bit */
481         unsigned int microseconds;      /* total time taken */
482
483         memset(&tz, 0, sizeof (tz));
484
485         /* get this function in cached memory */
486         gettimeofday(&tvstart, &tz);
487         cycles[0] = rdtsc();
488         gettimeofday(&tvstart, &tz);
489
490         /* we don't trust that this is any specific length of time */
491         usleep(100);
492         cycles[1] = rdtsc();
493         gettimeofday(&tvstop, &tz);
494         microseconds = ((tvstop.tv_sec - tvstart.tv_sec) * 1000000) +
495                 (tvstop.tv_usec - tvstart.tv_usec);
496
497         snprintf(p_client_buffer, client_buffer_size, p_format,
498                 (float)((cycles[1] - cycles[0]) / microseconds) / divisor);
499 #else
500         get_freq(p_client_buffer, client_buffer_size, p_format, divisor);
501 #endif
502 }
503
504 /*void*/
505 char
506 get_freq(char *p_client_buffer, size_t client_buffer_size,
507                 char *p_format, int divisor, unsigned int cpu)
508 {
509         int freq;
510         char *freq_sysctl;
511
512         freq_sysctl = (char *)calloc(16, sizeof(char));
513         if (freq_sysctl == NULL)
514                 exit(-1);
515
516         snprintf(freq_sysctl, 16, "dev.cpu.%d.freq", cpu);
517         
518         if (!p_client_buffer || client_buffer_size <= 0 ||
519                         !p_format || divisor <= 0)
520                 return 0;
521
522         if (GETSYSCTL(freq_sysctl, freq) == 0)
523                 snprintf(p_client_buffer, client_buffer_size,
524                                 p_format, (float)freq/divisor);
525         else
526                 snprintf(p_client_buffer, client_buffer_size, p_format, 0.0f);
527
528         free(freq_sysctl);
529         return 1;
530 }
531
532 void
533 update_top()
534 {
535         proc_find_top(info.cpu, info.memu);
536 }
537
538 void
539 update_wifi_stats()
540 {
541         struct ifreq ifr;               /* interface stats */
542         struct wi_req wireq;
543         struct net_stat * ns;
544         struct ifaddrs *ifap, *ifa;
545         struct ifmediareq ifmr;
546         int s;
547
548         /*
549          * Get iface table
550          */
551         if (getifaddrs(&ifap) < 0)
552                 return;
553
554         for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
555                 ns = get_net_stat((const char *) ifa->ifa_name);
556
557                 if (!strcmp(ifa->ifa_name, "lo0") ||
558                         !strncmp(ifa->ifa_name, "fwe", 3) ||
559                         !strncmp(ifa->ifa_name, "tun", 3))
560                         continue;
561
562                 s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
563
564                 /* Get media type */
565                 bzero(&ifmr, sizeof(ifmr));
566                 strlcpy(ifmr.ifm_name, ifa->ifa_name, IFNAMSIZ);
567                 if (ioctl(s, SIOCGIFMEDIA, (caddr_t) &ifmr) < 0) {
568                         perror("ioctl (getting media type)");
569                         exit(1);
570                 }
571                 /*
572                  * We can monitor only wireless interfaces
573                  * which not in hostap mode
574                  */
575                 if ((ifmr.ifm_active & IFM_IEEE80211) &&
576                                 !(ifmr.ifm_active & IFM_IEEE80211_HOSTAP)) {
577                         /* Get wi status */
578                         bzero(&ifr, sizeof(ifr));
579                         strlcpy(ifr.ifr_name, ifa->ifa_name, IFNAMSIZ);
580                         wireq.wi_type   = WI_RID_COMMS_QUALITY;
581                         wireq.wi_len    = WI_MAX_DATALEN;
582                         ifr.ifr_data    = (void *) &wireq;
583
584                         if (ioctl(s, SIOCGWAVELAN, (caddr_t) &ifr) < 0) {
585                                 perror("ioctl (getting wi status)");
586                                 exit(1);
587                         }
588
589                         /*
590                          * wi_val[0] = quality
591                          * wi_val[1] = signal
592                          * wi_val[2] = noise
593                          */
594                         ns->linkstatus = (int) wireq.wi_val[1];
595                 }
596                 
597                 close(s);
598         }
599 }
600 void
601 update_diskio()
602 {
603         int devs_count,
604             num_selected,
605             num_selections;
606         struct device_selection *dev_select = NULL;
607         long select_generation;
608         int dn;
609         static struct statinfo  statinfo_cur;
610         u_int64_t diskio_current = 0;
611
612         bzero(&statinfo_cur, sizeof (statinfo_cur));
613         statinfo_cur.dinfo = (struct devinfo *)malloc(sizeof (struct devinfo));
614         bzero(statinfo_cur.dinfo, sizeof (struct devinfo));
615
616         if (devstat_getdevs(NULL, &statinfo_cur) < 0)
617                 return;
618
619         devs_count = statinfo_cur.dinfo->numdevs;
620         if (devstat_selectdevs(&dev_select, &num_selected, &num_selections,
621                         &select_generation, statinfo_cur.dinfo->generation,
622                         statinfo_cur.dinfo->devices, devs_count, NULL, 0,
623                         NULL, 0, DS_SELECT_ONLY, MAXSHOWDEVS, 1) >= 0) {
624                 for (dn = 0; dn < devs_count; ++dn) {
625                         int di;
626                         struct devstat  *dev;
627
628                         di = dev_select[dn].position;
629                         dev = &statinfo_cur.dinfo->devices[di];
630
631                         diskio_current += dev->bytes[DEVSTAT_READ] +
632                                 dev->bytes[DEVSTAT_WRITE];
633                 }
634
635                 free(dev_select);
636         }
637
638         /*
639          * Since we return (diskio_total_current - diskio_total_old), first
640          * frame will be way too high (it will be equal to
641          * diskio_total_current, i.e. all disk I/O since boot). That's why
642          * it is better to return 0 first time;
643          */
644         if (diskio_setup == 0) {
645                 diskio_setup = 1;
646                 diskio_value = 0;
647         } else
648                 diskio_value = (unsigned int)((diskio_current - diskio_prev)/
649                                 1024);
650         diskio_prev = diskio_current;
651
652         free(statinfo_cur.dinfo);
653 }
654
655 /*
656  * While topless is obviously better, top is also not bad.
657  */
658
659 int
660 comparecpu(const void *a, const void *b)
661 {
662         if (((struct process *)a)->amount > ((struct process *)b)->amount)
663                 return (-1);
664
665         if (((struct process *)a)->amount < ((struct process *)b)->amount)
666                 return (1);
667
668         return (0);
669 }
670
671 int
672 comparemem(const void *a, const void *b)
673 {
674         if (((struct process *)a)->totalmem > ((struct process *)b)->totalmem)
675                 return (-1);
676
677         if (((struct process *)a)->totalmem < ((struct process *)b)->totalmem)
678                 return (1);
679
680         return (0);
681 }
682
683 inline void
684 proc_find_top(struct process **cpu, struct process **mem)
685 {
686         struct kinfo_proc *p;
687         int n_processes;
688         int i, j = 0;
689         struct process *processes;
690
691         int total_pages;
692
693         /* we get total pages count again to be sure it is up to date */
694         if (GETSYSCTL("vm.stats.vm.v_page_count", total_pages) != 0)
695                 CRIT_ERR("Cannot read sysctl"
696                         "\"vm.stats.vm.v_page_count\"");
697
698         p = kvm_getprocs(kd, KERN_PROC_PROC, 0, &n_processes);
699         processes = malloc(n_processes * sizeof (struct process));
700
701         for (i = 0; i < n_processes; i++) {
702                 if (!((p[i].ki_flag & P_SYSTEM)) &&
703                                 p[i].ki_comm != NULL) {
704                         processes[j].pid = p[i].ki_pid;
705                         processes[j].name =  strdup(p[i].ki_comm);
706                         processes[j].amount = 100.0 *
707                                 p[i].ki_pctcpu / FSCALE;
708                         processes[j].totalmem = (float)(p[i].ki_rssize /
709                                         (float)total_pages) * 100.0;
710                         j++;
711                 }
712         }
713
714         qsort(processes, j - 1, sizeof (struct process), comparemem);
715         for (i = 0; i < 10; i++) {
716                 struct process *tmp, *ttmp;
717
718                 tmp = malloc(sizeof (struct process));
719                 tmp->pid = processes[i].pid;
720                 tmp->amount = processes[i].amount;
721                 tmp->totalmem = processes[i].totalmem;
722                 tmp->name = strdup(processes[i].name);
723
724                 ttmp = mem[i];
725                 mem[i] = tmp;
726                 if (ttmp != NULL) {
727                         free(ttmp->name);
728                         free(ttmp);
729                 }
730         }
731
732         qsort(processes, j - 1, sizeof (struct process), comparecpu);
733         for (i = 0; i < 10; i++) {
734                 struct process *tmp, *ttmp;
735
736                 tmp = malloc(sizeof (struct process));
737                 tmp->pid = processes[i].pid;
738                 tmp->amount = processes[i].amount;
739                 tmp->totalmem = processes[i].totalmem;
740                 tmp->name = strdup(processes[i].name);
741
742                 ttmp = cpu[i];
743                 cpu[i] = tmp;
744                 if (ttmp != NULL) {
745                         free(ttmp->name);
746                         free(ttmp);
747                 }
748         }
749
750 #if defined(FREEBSD_DEBUG)
751         printf("=====\nmem\n");
752         for (i = 0; i < 10; i++) {
753                 printf("%d: %s(%d) %.2f\n", i, mem[i]->name,
754                                 mem[i]->pid, mem[i]->totalmem);
755         }
756 #endif
757
758         for (i = 0; i < j; free(processes[i++].name));
759         free(processes);
760 }
761
762 #if     defined(i386) || defined(__i386__)
763 #define APMDEV          "/dev/apm"
764 #define APM_UNKNOWN     255
765
766 int
767 apm_getinfo(int fd, apm_info_t aip)
768 {
769         if (ioctl(fd, APMIO_GETINFO, aip) == -1)
770                 return (-1);
771
772         return (0);
773 }
774
775 char
776 *get_apm_adapter()
777 {
778         int fd;
779         struct apm_info info;
780
781         fd = open(APMDEV, O_RDONLY);
782         if (fd < 0)
783                 return ("ERR");
784
785         if (apm_getinfo(fd, &info) != 0) {
786                 close(fd);
787                 return ("ERR");
788         }
789         close(fd);
790
791         switch (info.ai_acline) {
792                 case 0:
793                         return ("off-line");
794                         break;
795                 case 1:
796                         if (info.ai_batt_stat == 3)
797                                 return ("charging");
798                         else
799                                 return ("on-line");
800                         break;
801                 default:
802                         return ("unknown");
803                         break;
804         }
805 }
806
807 char
808 *get_apm_battery_life()
809 {
810         int fd;
811         u_int batt_life;
812         struct apm_info info;
813         char *out;
814
815         out = (char *)calloc(16, sizeof (char));
816
817         fd = open(APMDEV, O_RDONLY);
818         if (fd < 0) {
819                 strncpy(out, "ERR", 16);
820                 return (out);
821         }
822
823         if (apm_getinfo(fd, &info) != 0) {
824                 close(fd);
825                 strncpy(out, "ERR", 16);
826                 return (out);
827         }
828         close(fd);
829
830         batt_life = info.ai_batt_life;
831         if (batt_life == APM_UNKNOWN)
832                 strncpy(out, "unknown", 16);
833         else if (batt_life <= 100) {
834                 snprintf(out, 16, "%d%%", batt_life);
835                 return (out);
836         } else
837                 strncpy(out, "ERR", 16);
838
839         return (out);
840 }
841
842 char
843 *get_apm_battery_time()
844 {
845         int fd;
846         int batt_time;
847         int h, m, s;
848         struct apm_info info;
849         char *out;
850
851         out = (char *)calloc(16, sizeof (char));
852
853         fd = open(APMDEV, O_RDONLY);
854         if (fd < 0) {
855                 strncpy(out, "ERR", 16);
856                 return (out);
857         }
858
859         if (apm_getinfo(fd, &info) != 0) {
860                 close(fd);
861                 strncpy(out, "ERR", 16);
862                 return (out);
863         }
864         close(fd);
865
866         batt_time = info.ai_batt_time;
867
868         if (batt_time == -1)
869                 strncpy(out, "unknown", 16);
870         else {
871                 h = batt_time;
872                 s = h % 60;
873                 h /= 60;
874                 m = h % 60;
875                 h /= 60;
876                 snprintf(out, 16, "%2d:%02d:%02d", h, m, s);
877         }
878
879         return (out);
880 }
881
882 #endif
883
884 /* empty stub so conky links */
885 void
886 free_all_processes(void)
887 {
888 }