2008-01-10 23:33:57 -08:00
|
|
|
#include "e.h"
|
2017-07-22 06:23:33 -07:00
|
|
|
#include <sys/select.h>
|
2008-01-10 23:33:57 -08:00
|
|
|
|
|
|
|
struct _E_Powersave_Deferred_Action
|
|
|
|
{
|
2012-08-12 23:51:24 -07:00
|
|
|
void (*func)(void *data);
|
|
|
|
const void *data;
|
2008-01-10 23:33:57 -08:00
|
|
|
unsigned char delete_me : 1;
|
|
|
|
};
|
|
|
|
|
2017-07-22 06:23:33 -07:00
|
|
|
struct _E_Powersave_Sleeper
|
|
|
|
{
|
|
|
|
Ecore_Pipe *pipe;
|
|
|
|
int fd;
|
|
|
|
};
|
|
|
|
|
2008-01-10 23:33:57 -08:00
|
|
|
/* local subsystem functions */
|
2017-07-09 17:05:12 -07:00
|
|
|
static void _e_powersave_sleeper_cb_dummy(void *data EINA_UNUSED, void *buffer EINA_UNUSED, unsigned int bytes EINA_UNUSED);
|
2010-06-24 09:19:12 -07:00
|
|
|
static Eina_Bool _e_powersave_cb_deferred_timer(void *data);
|
2012-08-12 23:51:24 -07:00
|
|
|
static void _e_powersave_mode_eval(void);
|
2015-03-18 15:09:09 -07:00
|
|
|
static void _e_powersave_event_update_free(void *data EINA_UNUSED, void *event);
|
2017-07-09 17:05:12 -07:00
|
|
|
static void _e_powersave_event_change_send(E_Powersave_Mode mode);
|
|
|
|
static void _e_powersave_sleepers_wake(void);
|
2008-01-10 23:33:57 -08:00
|
|
|
|
|
|
|
/* local subsystem globals */
|
2015-05-07 12:13:10 -07:00
|
|
|
E_API int E_EVENT_POWERSAVE_UPDATE = 0;
|
|
|
|
E_API int E_EVENT_POWERSAVE_CONFIG_UPDATE = 0;
|
2008-01-10 23:33:57 -08:00
|
|
|
static int walking_deferred_actions = 0;
|
2008-10-22 04:49:33 -07:00
|
|
|
static Eina_List *deferred_actions = NULL;
|
2008-01-10 23:33:57 -08:00
|
|
|
static Ecore_Timer *deferred_timer = NULL;
|
|
|
|
static E_Powersave_Mode powersave_mode = E_POWERSAVE_MODE_LOW;
|
2017-07-09 17:05:12 -07:00
|
|
|
static E_Powersave_Mode powersave_mode_force = E_POWERSAVE_MODE_NONE;
|
2008-01-10 23:33:57 -08:00
|
|
|
static double defer_time = 5.0;
|
2017-07-09 17:05:12 -07:00
|
|
|
static Eina_Bool powersave_force = EINA_FALSE;
|
|
|
|
static Eina_List *powersave_sleepers = NULL;
|
2008-01-10 23:33:57 -08:00
|
|
|
|
|
|
|
/* externally accessible functions */
|
2010-11-22 07:21:32 -08:00
|
|
|
EINTERN int
|
2008-01-10 23:33:57 -08:00
|
|
|
e_powersave_init(void)
|
|
|
|
{
|
|
|
|
_e_powersave_mode_eval();
|
2008-11-24 00:32:34 -08:00
|
|
|
E_EVENT_POWERSAVE_UPDATE = ecore_event_type_new();
|
2012-05-03 13:16:34 -07:00
|
|
|
E_EVENT_POWERSAVE_CONFIG_UPDATE = ecore_event_type_new();
|
2008-01-10 23:33:57 -08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-11-22 07:21:32 -08:00
|
|
|
EINTERN int
|
2008-01-10 23:33:57 -08:00
|
|
|
e_powersave_shutdown(void)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-05-07 12:13:10 -07:00
|
|
|
E_API E_Powersave_Deferred_Action *
|
2012-08-12 23:51:24 -07:00
|
|
|
e_powersave_deferred_action_add(void (*func)(void *data), const void *data)
|
2008-01-10 23:33:57 -08:00
|
|
|
{
|
|
|
|
E_Powersave_Deferred_Action *pa;
|
2012-05-03 13:16:34 -07:00
|
|
|
|
2008-01-10 23:33:57 -08:00
|
|
|
pa = calloc(1, sizeof(E_Powersave_Deferred_Action));
|
|
|
|
if (!pa) return NULL;
|
|
|
|
if (deferred_timer) ecore_timer_del(deferred_timer);
|
2017-02-03 13:52:28 -08:00
|
|
|
deferred_timer = ecore_timer_loop_add(defer_time,
|
2017-07-09 17:05:12 -07:00
|
|
|
_e_powersave_cb_deferred_timer,
|
|
|
|
NULL);
|
2008-01-10 23:33:57 -08:00
|
|
|
pa->func = func;
|
|
|
|
pa->data = data;
|
2008-10-22 04:49:33 -07:00
|
|
|
deferred_actions = eina_list_append(deferred_actions, pa);
|
2008-01-10 23:33:57 -08:00
|
|
|
return pa;
|
|
|
|
}
|
|
|
|
|
2015-05-07 12:13:10 -07:00
|
|
|
E_API void
|
2008-01-10 23:33:57 -08:00
|
|
|
e_powersave_deferred_action_del(E_Powersave_Deferred_Action *pa)
|
|
|
|
{
|
|
|
|
if (walking_deferred_actions)
|
|
|
|
{
|
2012-08-12 23:51:24 -07:00
|
|
|
pa->delete_me = 1;
|
|
|
|
return;
|
2008-01-10 23:33:57 -08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-08-12 23:51:24 -07:00
|
|
|
deferred_actions = eina_list_remove(deferred_actions, pa);
|
|
|
|
free(pa);
|
|
|
|
if (!deferred_actions)
|
|
|
|
{
|
|
|
|
if (deferred_timer)
|
|
|
|
{
|
|
|
|
ecore_timer_del(deferred_timer);
|
|
|
|
deferred_timer = NULL;
|
|
|
|
}
|
|
|
|
}
|
2008-01-10 23:33:57 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-07 12:13:10 -07:00
|
|
|
E_API void
|
2008-01-10 23:33:57 -08:00
|
|
|
e_powersave_mode_set(E_Powersave_Mode mode)
|
|
|
|
{
|
2012-05-03 13:16:34 -07:00
|
|
|
if (mode < e_config->powersave.min) mode = e_config->powersave.min;
|
2017-07-09 17:05:12 -07:00
|
|
|
else if (mode > e_config->powersave.max) mode = e_config->powersave.max;
|
|
|
|
|
2008-03-11 22:43:36 -07:00
|
|
|
if (powersave_mode == mode) return;
|
2008-01-10 23:33:57 -08:00
|
|
|
powersave_mode = mode;
|
2008-11-24 00:32:34 -08:00
|
|
|
|
2017-07-09 17:05:12 -07:00
|
|
|
if (powersave_force) return;
|
|
|
|
_e_powersave_event_change_send(powersave_mode);
|
2008-01-10 23:33:57 -08:00
|
|
|
_e_powersave_mode_eval();
|
|
|
|
}
|
|
|
|
|
2015-05-07 12:13:10 -07:00
|
|
|
E_API E_Powersave_Mode
|
2012-05-03 13:16:34 -07:00
|
|
|
e_powersave_mode_get(void)
|
2008-01-10 23:33:57 -08:00
|
|
|
{
|
2017-07-09 17:05:12 -07:00
|
|
|
if (powersave_force) return powersave_mode_force;
|
2012-05-03 13:16:34 -07:00
|
|
|
return powersave_mode;
|
2008-01-10 23:33:57 -08:00
|
|
|
}
|
|
|
|
|
2017-07-09 17:05:12 -07:00
|
|
|
E_API void
|
|
|
|
e_powersave_mode_force(E_Powersave_Mode mode)
|
2008-01-10 23:33:57 -08:00
|
|
|
{
|
2017-07-09 17:05:12 -07:00
|
|
|
if (mode == powersave_mode_force) return;
|
|
|
|
powersave_force = EINA_TRUE;
|
|
|
|
powersave_mode_force = mode;
|
|
|
|
_e_powersave_event_change_send(powersave_mode_force);
|
|
|
|
_e_powersave_mode_eval();
|
2008-01-10 23:33:57 -08:00
|
|
|
}
|
|
|
|
|
2017-07-09 17:05:12 -07:00
|
|
|
E_API void
|
|
|
|
e_powersave_mode_unforce(void)
|
2008-01-10 23:33:57 -08:00
|
|
|
{
|
2017-07-09 17:05:12 -07:00
|
|
|
if (!powersave_force) return;
|
|
|
|
powersave_force = EINA_FALSE;
|
|
|
|
if (powersave_mode_force != powersave_mode)
|
|
|
|
{
|
|
|
|
_e_powersave_event_change_send(powersave_mode);
|
|
|
|
_e_powersave_mode_eval();
|
|
|
|
}
|
|
|
|
powersave_mode_force = E_POWERSAVE_MODE_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
E_API E_Powersave_Sleeper *
|
|
|
|
e_powersave_sleeper_new(void)
|
|
|
|
{
|
2017-07-22 06:23:33 -07:00
|
|
|
E_Powersave_Sleeper *sleeper;
|
2017-07-09 17:05:12 -07:00
|
|
|
|
2017-07-22 06:23:33 -07:00
|
|
|
sleeper = E_NEW(E_Powersave_Sleeper, 1);
|
|
|
|
sleeper->pipe = ecore_pipe_add(_e_powersave_sleeper_cb_dummy, NULL);
|
2017-07-23 01:28:53 -07:00
|
|
|
if (!sleeper->pipe)
|
|
|
|
{
|
|
|
|
eina_freeq_ptr_add(eina_freeq_main_get(), sleeper, free, sizeof(*sleeper));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
sleeper->fd = ecore_pipe_read_fd(sleeper->pipe);
|
2017-07-22 06:23:33 -07:00
|
|
|
ecore_pipe_freeze(sleeper->pipe);
|
|
|
|
powersave_sleepers = eina_list_append(powersave_sleepers, sleeper);
|
|
|
|
return (E_Powersave_Sleeper *)sleeper;
|
2017-07-09 17:05:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
E_API void
|
|
|
|
e_powersave_sleeper_free(E_Powersave_Sleeper *sleeper)
|
|
|
|
{
|
2017-07-22 06:23:33 -07:00
|
|
|
if (!sleeper) return;
|
|
|
|
ecore_pipe_del(sleeper->pipe);
|
|
|
|
powersave_sleepers = eina_list_remove(powersave_sleepers, sleeper);
|
|
|
|
eina_freeq_ptr_add(eina_freeq_main_get(), sleeper, free, sizeof(*sleeper));
|
2017-07-09 17:05:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
E_API void
|
|
|
|
e_powersave_sleeper_sleep(E_Powersave_Sleeper *sleeper, int poll_interval)
|
|
|
|
{
|
2017-07-22 06:23:33 -07:00
|
|
|
unsigned int tim;
|
|
|
|
fd_set rfds, wfds, exfds;
|
|
|
|
struct timeval tv;
|
|
|
|
int ret;
|
|
|
|
char buf[1] = { 1 };
|
2017-07-09 17:05:12 -07:00
|
|
|
|
2017-07-22 06:23:33 -07:00
|
|
|
if (!sleeper) return;
|
2017-07-09 17:05:12 -07:00
|
|
|
if (e_powersave_mode_get() == E_POWERSAVE_MODE_FREEZE) tim = 3600;
|
|
|
|
else tim = (double)poll_interval / 8.0;
|
2017-07-22 06:23:33 -07:00
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_ZERO(&wfds);
|
|
|
|
FD_ZERO(&exfds);
|
|
|
|
FD_SET(sleeper->fd, &rfds);
|
|
|
|
|
|
|
|
tim = (fmod(ecore_time_get(), tim) * 1000000.0);
|
|
|
|
tv.tv_sec = tim / 1000000;
|
|
|
|
tv.tv_usec = tim % 1000000;
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
ret = select(sleeper->fd + 1, &rfds, &wfds, &exfds, &tv);
|
|
|
|
if ((ret == 1) && (FD_ISSET(sleeper->fd, &rfds)))
|
|
|
|
{
|
|
|
|
read(sleeper->fd, buf, 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (ret == 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2008-01-10 23:33:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* local subsystem functions */
|
|
|
|
|
2017-07-09 17:05:12 -07:00
|
|
|
static void
|
|
|
|
_e_powersave_sleepers_wake(void)
|
|
|
|
{
|
2017-07-22 06:23:33 -07:00
|
|
|
E_Powersave_Sleeper *sleeper;
|
2017-07-09 17:05:12 -07:00
|
|
|
Eina_List *l;
|
|
|
|
char buf[1] = { 1 };
|
|
|
|
|
2017-07-22 06:23:33 -07:00
|
|
|
EINA_LIST_FOREACH(powersave_sleepers, l, sleeper)
|
2017-07-09 17:05:12 -07:00
|
|
|
{
|
2017-07-22 06:23:33 -07:00
|
|
|
write(ecore_pipe_write_fd(sleeper->pipe), buf, 1);
|
|
|
|
ecore_pipe_write(sleeper->pipe, buf, 1);
|
2017-07-09 17:05:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_e_powersave_sleeper_cb_dummy(void *data EINA_UNUSED, void *buffer EINA_UNUSED, unsigned int bytes EINA_UNUSED)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-06-24 09:19:12 -07:00
|
|
|
static Eina_Bool
|
2015-03-18 15:09:09 -07:00
|
|
|
_e_powersave_cb_deferred_timer(void *data EINA_UNUSED)
|
2008-01-10 23:33:57 -08:00
|
|
|
{
|
2009-08-21 08:08:49 -07:00
|
|
|
E_Powersave_Deferred_Action *pa;
|
|
|
|
|
2008-01-10 23:33:57 -08:00
|
|
|
walking_deferred_actions++;
|
2009-08-21 08:08:49 -07:00
|
|
|
EINA_LIST_FREE(deferred_actions, pa)
|
2008-01-10 23:33:57 -08:00
|
|
|
{
|
2012-08-12 23:51:24 -07:00
|
|
|
if (!pa->delete_me) pa->func((void *)pa->data);
|
|
|
|
free(pa);
|
2008-01-10 23:33:57 -08:00
|
|
|
}
|
|
|
|
walking_deferred_actions--;
|
|
|
|
if (!deferred_actions) deferred_timer = NULL;
|
2010-06-24 09:19:12 -07:00
|
|
|
return ECORE_CALLBACK_CANCEL;
|
2008-01-10 23:33:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_e_powersave_mode_eval(void)
|
|
|
|
{
|
|
|
|
double t = 0.0;
|
2017-07-09 17:05:12 -07:00
|
|
|
E_Powersave_Mode mode;
|
2012-05-03 13:16:34 -07:00
|
|
|
|
2017-07-09 17:05:12 -07:00
|
|
|
if (powersave_force) mode = powersave_mode_force;
|
|
|
|
else mode = powersave_mode;
|
|
|
|
|
|
|
|
switch (mode)
|
2008-01-10 23:33:57 -08:00
|
|
|
{
|
|
|
|
case E_POWERSAVE_MODE_NONE:
|
2012-08-12 23:51:24 -07:00
|
|
|
t = e_config->powersave.none; /* time to defer "power expensive" activities */
|
|
|
|
break;
|
|
|
|
|
2008-01-10 23:33:57 -08:00
|
|
|
case E_POWERSAVE_MODE_LOW:
|
2012-08-12 23:51:24 -07:00
|
|
|
t = e_config->powersave.low;
|
|
|
|
break;
|
|
|
|
|
2008-01-10 23:33:57 -08:00
|
|
|
case E_POWERSAVE_MODE_MEDIUM:
|
2012-08-12 23:51:24 -07:00
|
|
|
t = e_config->powersave.medium;
|
|
|
|
break;
|
|
|
|
|
2008-01-10 23:33:57 -08:00
|
|
|
case E_POWERSAVE_MODE_HIGH:
|
2012-08-12 23:51:24 -07:00
|
|
|
t = e_config->powersave.high;
|
|
|
|
break;
|
|
|
|
|
2008-01-10 23:33:57 -08:00
|
|
|
case E_POWERSAVE_MODE_EXTREME:
|
2012-08-12 23:51:24 -07:00
|
|
|
t = e_config->powersave.extreme;
|
|
|
|
break;
|
|
|
|
|
2017-07-09 17:05:12 -07:00
|
|
|
case E_POWERSAVE_MODE_FREEZE:
|
|
|
|
t = 3600;
|
|
|
|
break;
|
|
|
|
|
2008-01-10 23:33:57 -08:00
|
|
|
default:
|
2012-08-12 23:51:24 -07:00
|
|
|
return;
|
|
|
|
break;
|
2008-01-10 23:33:57 -08:00
|
|
|
}
|
2017-01-06 21:05:17 -08:00
|
|
|
if (!EINA_DBL_EQ(t, defer_time))
|
2008-01-10 23:33:57 -08:00
|
|
|
{
|
2012-08-12 23:51:24 -07:00
|
|
|
if (deferred_timer) ecore_timer_del(deferred_timer);
|
2017-02-03 13:52:28 -08:00
|
|
|
deferred_timer = ecore_timer_loop_add(defer_time,
|
2017-07-09 17:05:12 -07:00
|
|
|
_e_powersave_cb_deferred_timer,
|
|
|
|
NULL);
|
2012-08-12 23:51:24 -07:00
|
|
|
defer_time = t;
|
2008-01-10 23:33:57 -08:00
|
|
|
}
|
|
|
|
}
|
2008-11-24 00:32:34 -08:00
|
|
|
|
|
|
|
static void
|
2015-03-18 15:09:09 -07:00
|
|
|
_e_powersave_event_update_free(void *data EINA_UNUSED, void *event)
|
2008-11-24 00:32:34 -08:00
|
|
|
{
|
|
|
|
free(event);
|
|
|
|
}
|
2012-08-12 23:51:24 -07:00
|
|
|
|
2017-07-09 17:05:12 -07:00
|
|
|
static void
|
|
|
|
_e_powersave_event_change_send(E_Powersave_Mode mode)
|
|
|
|
{
|
|
|
|
E_Event_Powersave_Update *ev;
|
|
|
|
|
|
|
|
printf("CHANGE PW SAVE MODE TO %i / %i\n",
|
|
|
|
(int)mode, E_POWERSAVE_MODE_EXTREME);
|
|
|
|
ev = E_NEW(E_Event_Powersave_Update, 1);
|
|
|
|
ev->mode = mode;
|
|
|
|
ecore_event_add(E_EVENT_POWERSAVE_UPDATE, ev, _e_powersave_event_update_free, NULL);
|
|
|
|
_e_powersave_sleepers_wake();
|
|
|
|
}
|