Battery: Split operating system specific code

This removes all operating system specific code from the main
battery file into their own source files. CMake will add the
correct implementation automatically.
This commit is contained in:
Sebastian Reichel
2015-08-07 04:31:31 +02:00
parent 66acd8ed38
commit 3c45cf29c7
7 changed files with 257 additions and 159 deletions

84
src/battery/openbsd.c Normal file
View File

@@ -0,0 +1,84 @@
/**************************************************************************
*
* 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 <machine/apmvar.h>
#include <err.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <string.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(struct batstate *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)
state->state = BATTERY_FULL;
if (info.minutes_left != -1)
batstate_set_time(state, info.minutes_left * 60);
state->percentage = info.battery_life;
} else {
warn("power update: APM_IOC_GETPOWER");
return -1;
}
return 0;
}
char* battery_os_tooltip() {
return strdup("Operating System not supported");
}
#endif