all repos — tint2 @ 405c2c9286a719481de59bda030dd1ee99a5f994

fork of the tint2 desktop panel for my custom setup - only minimized windows across all desktops for the taskbar

src/battery/openbsd.c (raw)

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
/**************************************************************************
*
* Tint2 : OpenBSD & NetBSD battery
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 2
* or any later version as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
**************************************************************************/

#if defined(__OpenBSD__) || defined(__NetBSD__)

#include <stdint.h>
#include <err.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <string.h>
#include <machine/apmvar.h>

#include "common.h"
#include "battery.h"

int apm_fd = -1;

gboolean battery_os_init()
{
    if (apm_fd > 0)
        close(apm_fd);

    apm_fd = open("/dev/apm", O_RDONLY);

    if (apm_fd < 0) {
        warn("ERROR: battery applet cannot open /dev/apm.");
        return FALSE;
    } else {
        return TRUE;
    }
}

void battery_os_free()
{
    if ((apm_fd != -1) && (close(apm_fd) == -1))
        warn("cannot close /dev/apm");
    apm_fd = -1;
}

int battery_os_update(BatteryState *state)
{
    struct apm_power_info info;

    if (apm_fd > 0 && ioctl(apm_fd, APM_IOC_GETPOWER, &(info)) == 0) {
        // best attempt at mapping to Linux battery states
        switch (info.battery_state) {
        case APM_BATT_CHARGING:
            state->state = BATTERY_CHARGING;
            break;
        default:
            state->state = BATTERY_DISCHARGING;
            break;
        }

        if (info.battery_life > 100)
            info.battery_life = 100;
        if (info.battery_life == 100)
            state->state = BATTERY_FULL;

        state->percentage = info.battery_life;
        if (info.minutes_left != -1)
            battery_state_set_time(state, info.minutes_left * 60);

        state->ac_connected = info.ac_state == APM_AC_ON;
    } else {
        warn("power update: APM_IOC_GETPOWER");
        return -1;
    }

    return 0;
}

char *battery_os_tooltip()
{
    GString *tooltip = g_string_new("");
    gchar *result;

    g_string_append_printf(tooltip, "Battery\n");

    gchar *state = (battery_state.state == BATTERY_UNKNOWN) ? "Level" : chargestate2str(battery_state.state);

    g_string_append_printf(tooltip, "\t%s: %d%%", state, battery_state.percentage);

    g_string_append_c(tooltip, '\n');
    g_string_append_printf(tooltip, "AC\n");
    g_string_append_printf(tooltip, battery_state.ac_connected ? "\tConnected" : "\tDisconnected");

    result = tooltip->str;
    g_string_free(tooltip, FALSE);

    return result;
}

#endif