File: batt.c

package info (click to toggle)
lcdproc 0.5.9-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 5,064 kB
  • sloc: ansic: 59,645; sh: 1,740; perl: 681; makefile: 417
file content (158 lines) | stat: -rw-r--r-- 4,218 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/** \file clients/lcdproc/batt.c
 * Implements the 'battery' screen showing the APM battery status.
 */

/*-
 * This file is part of lcdproc, the lcdproc client.
 *
 * This file is released under the GNU General Public License.
 * Refer to the COPYING file distributed with this package.
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/utsname.h>

#include "shared/sockets.h"

#include "main.h"
#include "mode.h"
#include "batt.h"
#include "machine.h"

/** Map status code > status text */
typedef struct {
	int status;
	const char *name;
} NameTable;

/**
 * Converts a numeric AC power status into a status text.
 *
 * \param status  Numeric status as returned by machine_get_battstat
 * \return  Name of the status
 */
static const char *
ac_status(int status)
{
	const NameTable ac_table[] = {
		{ LCDP_AC_OFF,     "Off"     },
		{ LCDP_AC_ON,      "On"      },
		{ LCDP_AC_BACKUP,  "Backup"  },
		{ LCDP_AC_UNKNOWN, "Unknown" },
		{ 0, NULL }
	};
	int i;

	for (i = 0; ac_table[i].name != NULL; i++)
		if (status == ac_table[i].status)
			return ac_table[i].name;

	return ac_table[LCDP_AC_UNKNOWN].name;
}

/**
 * Converts a numeric battery status into a status text.
 *
 * \param status  Numeric status as returned by machine_get_battstat
 * \return  Name of the status
 */
static const char *
battery_status(int status)
{
	const NameTable batt_table[] = {
		{ LCDP_BATT_HIGH,     "High"     },
		{ LCDP_BATT_LOW,      "Low"      },
		{ LCDP_BATT_CRITICAL, "Critical" },
		{ LCDP_BATT_CHARGING, "Charging" },
		{ LCDP_BATT_ABSENT,   "Absent"   },
		{ LCDP_BATT_UNKNOWN,  "Unknown"  },
		{ 0, NULL }
	};
	int i;

	for (i = 0; batt_table[i].name != NULL; i++)
		if (status == batt_table[i].status)
			return batt_table[i].name;

	return batt_table[LCDP_AC_UNKNOWN].name;
}

/**
 * Battery Screen shows apm battery status...
 *
 *\verbatim
 *
 * +--------------------+	+--------------------+
 * |## AC: 100%: myho #@|	|## AC: 100%: myho #@|
 * |AC: On              |	|AC, Batt: Absent    |
 * |Batt: Absent        |	+--------------------+
 * |E------------------F|
 * +--------------------+
 *
 *\endverbatim
 *
 * \param rep        Time since last screen update
 * \param display    1 if screen is visible or data should be updated
 * \param flags_ptr  Mode flags
 * \return  Always 0
 */
int
battery_screen(int rep, int display, int *flags_ptr)
{
	int acstat = 0, battstat = 0, percent = 0;
	int gauge_wid = lcd_wid - 2;

	if ((*flags_ptr & INITIALIZED) == 0) {
		*flags_ptr |= INITIALIZED;

		sock_send_string(sock, "screen_add B\n");
		sock_printf(sock, "screen_set B -name {APM stats: %s}\n", get_hostname());
		sock_send_string(sock, "widget_add B title title\n");
		sock_printf(sock, "widget_set B title {LCDPROC %s}\n", version);
		sock_send_string(sock, "widget_add B one string\n");
		if (lcd_hgt >= 4) {
			sock_send_string(sock, "widget_add B two string\n");
			sock_send_string(sock, "widget_add B three string\n");
			sock_send_string(sock, "widget_add B gauge hbar\n");

			sock_send_string(sock, "widget_set B one 1 2 {AC: Unknown}\n");
			sock_send_string(sock, "widget_set B two 1 3 {Batt: Unknown}\n");
			sock_printf(sock, "widget_set B three 1 4 {E%*sF}\n", gauge_wid, "");
			sock_send_string(sock, "widget_set B gauge 2 4 0\n");
		}
	}

	machine_get_battstat(&acstat, &battstat, &percent);

	if (display) {
		char tmp[20];

		if (percent >= 0)
			sprintf(tmp, "%d%%", percent);
		else
			sprintf(tmp, "??%%");
		sock_printf(sock, "widget_set B title {%s: %s: %s}\n",
				(acstat == LCDP_AC_ON && battstat == LCDP_BATT_ABSENT) ? "AC" : "Batt",
				tmp, get_hostname());

		if (lcd_hgt >= 4) {		/* 4-line version of the screen */
			sock_printf(sock, "widget_set B one 1 2 {AC: %s}\n", ac_status(acstat));
			sock_printf(sock, "widget_set B two 1 3 {Batt: %s}\n", battery_status(battstat));
			if (percent > 0)
				sock_printf(sock, "widget_set B gauge 2 4 %d\n",
						(percent * gauge_wid * lcd_cellwid) / 100);
		}
		else {				/* two-line version of the screen */
			sock_printf(sock, "widget_set B one 1 2 {%sBatt: %s}\n",
					(acstat == LCDP_AC_ON) ? "AC, " : "",
					battery_status(battstat));
		}
	}

	return 0;
}