xref: /haiku/src/apps/powerstatus/APMDriverInterface.cpp (revision e04d859b348c162706f0cde7cdf79fe258c9607e)
1 /*
2  * Copyright 2009-2015, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Clemens Zeidler, haiku@clemens-zeidler.de
7  */
8 
9 
10 #include "APMDriverInterface.h"
11 
12 #include <arch/x86/apm_defs.h>
13 #include <generic_syscall_defs.h>
14 #include <syscalls.h>
15 
16 
17 const bigtime_t kUpdateInterval = 2000000;
18 		// every two seconds
19 
20 
~APMDriverInterface()21 APMDriverInterface::~APMDriverInterface()
22 {
23 }
24 
25 
26 status_t
Connect()27 APMDriverInterface::Connect()
28 {
29 	uint32 version = 0;
30 	status_t status = _kern_generic_syscall(APM_SYSCALLS, B_SYSCALL_INFO,
31 		&version, sizeof(version));
32 	if (status == B_OK) {
33 		apm_battery_info info;
34 		status = _kern_generic_syscall(APM_SYSCALLS, APM_GET_BATTERY_INFO,
35 			&info, sizeof(apm_battery_info));
36 	}
37 
38 	return status;
39 }
40 
41 
42 status_t
GetBatteryInfo(int32 index,battery_info * info)43 APMDriverInterface::GetBatteryInfo(int32 index, battery_info* info)
44 {
45 	if (index != 0)
46 		return B_BAD_VALUE;
47 
48 	info->current_rate = -1;
49 
50 	apm_battery_info apmInfo;
51 	status_t status = _kern_generic_syscall(APM_SYSCALLS, APM_GET_BATTERY_INFO,
52 		&apmInfo, sizeof(apm_battery_info));
53 	if (status == B_OK) {
54 		info->state = apmInfo.online ? BATTERY_CHARGING : BATTERY_DISCHARGING;
55 		info->capacity = apmInfo.percent;
56 		info->full_capacity = 100;
57 		info->time_left = apmInfo.time_left;
58 	}
59 
60 	return status;
61 }
62 
63 
64 status_t
GetExtendedBatteryInfo(int32 index,acpi_extended_battery_info * info)65 APMDriverInterface::GetExtendedBatteryInfo(int32 index,
66 	acpi_extended_battery_info* info)
67 {
68 	return B_NOT_SUPPORTED;
69 }
70 
71 
72 int32
GetBatteryCount()73 APMDriverInterface::GetBatteryCount()
74 {
75 	return 1;
76 }
77 
78 
79 void
_WatchPowerStatus()80 APMDriverInterface::_WatchPowerStatus()
81 {
82 	while (atomic_get(&fIsWatching) > 0) {
83 		Broadcast(kMsgUpdate);
84 		acquire_sem_etc(fWaitSem, 1, B_RELATIVE_TIMEOUT, kUpdateInterval);
85 	}
86 }
87