Fix the shutdown sequence for maemo, SDL and simulator builds
[maemo-rb.git] / uisimulator / common / powermgmt-sim.c
blob30b04d6a0b8d47ba8eb05770e9f83a291ed239dd
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Heikki Hannikainen, Uwe Freese
11 * Revisions copyright (C) 2005 by Gerald Van Baren
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
22 #include "config.h"
23 #include "system.h"
24 #include <time.h>
25 #include "kernel.h"
26 #include "powermgmt.h"
27 #include <SDL_events.h>
29 #define BATT_MINMVOLT 2500 /* minimum millivolts of battery */
30 #define BATT_MAXMVOLT 4500 /* maximum millivolts of battery */
31 #define BATT_MAXRUNTIME (10 * 60) /* maximum runtime with full battery in
32 minutes */
34 extern void send_battery_level_event(void);
35 extern int last_sent_battery_level;
36 extern int battery_percent;
38 static unsigned int battery_millivolts = BATT_MAXMVOLT;
39 /* estimated remaining time in minutes */
40 static int powermgmt_est_runningtime_min = BATT_MAXRUNTIME;
42 static void battery_status_update(void)
44 static time_t last_change = 0;
45 static bool charging = false;
46 time_t now;
48 time(&now);
50 if (last_change < now) {
51 last_change = now;
53 /* change the values: */
54 if (charging) {
55 if (battery_millivolts >= BATT_MAXMVOLT) {
56 /* Pretend the charger was disconnected */
57 charging = false;
58 queue_broadcast(SYS_CHARGER_DISCONNECTED, 0);
59 last_sent_battery_level = 100;
62 else {
63 if (battery_millivolts <= BATT_MINMVOLT) {
64 /* Pretend the charger was connected */
65 charging = true;
66 queue_broadcast(SYS_CHARGER_CONNECTED, 0);
67 last_sent_battery_level = 0;
71 if (charging) {
72 battery_millivolts += (BATT_MAXMVOLT - BATT_MINMVOLT) / 50;
74 else {
75 battery_millivolts -= (BATT_MAXMVOLT - BATT_MINMVOLT) / 100;
78 battery_percent = 100 * (battery_millivolts - BATT_MINMVOLT) /
79 (BATT_MAXMVOLT - BATT_MINMVOLT);
81 powermgmt_est_runningtime_min =
82 battery_percent * BATT_MAXRUNTIME / 100;
85 send_battery_level_event();
88 void battery_read_info(int *voltage, int *level)
90 battery_status_update();
92 if (voltage)
93 *voltage = battery_millivolts;
95 if (level)
96 *level = battery_percent;
99 unsigned int battery_voltage(void)
101 battery_status_update();
102 return battery_millivolts;
105 int battery_level(void)
107 battery_status_update();
108 return battery_percent;
111 int battery_time(void)
113 battery_status_update();
114 return powermgmt_est_runningtime_min;
117 bool battery_level_safe(void)
119 return battery_level() >= 10;
122 void set_poweroff_timeout(int timeout)
124 (void)timeout;
127 void set_battery_capacity(int capacity)
129 (void)capacity;
132 #if BATTERY_TYPES_COUNT > 1
133 void set_battery_type(int type)
135 (void)type;
137 #endif
139 #ifdef HAVE_ACCESSORY_SUPPLY
140 void accessory_supply_set(bool enable)
142 (void)enable;
144 #endif
146 #ifdef HAVE_LINEOUT_POWEROFF
147 void lineout_set(bool enable)
149 (void)enable;
151 #endif
153 void reset_poweroff_timer(void)
157 void shutdown_hw(void)
159 /* Shut down SDL event loop */
160 SDL_Event event;
161 memset(&event, 0, sizeof(SDL_Event));
162 event.type = SDL_USEREVENT;
163 SDL_PushEvent(&event);
166 void cancel_shutdown(void)