Tizen 2.1 base
[framework/uifw/ecore.git] / src / lib / ecore / ecore_throttle.c
1 #ifdef HAVE_CONFIG_H
2 # include <config.h>
3 #endif
4
5 #include <stdlib.h>
6 #include <unistd.h>
7
8 #include "Ecore.h"
9 #include "ecore_private.h"
10
11 static int throttle_val = 0;
12
13 /**
14  * @addtogroup Ecore_Throttle_Group Ecore Throttle functions
15  *
16  * @{
17  */
18
19 /**
20  * Increase throttle amount
21  *
22  * This will increase or decrease (if @p amount is positive or negative) the
23  * amount of "voluntary throttling" ecore will do to its main loop while
24  * running. This is intended to be used to limit animations and wakeups when
25  * in a strict power management state. The higher the current throttle value
26  * (which can be retrieved by ecore_throttle_get() ), the more throttling
27  * takes place. If the current throttle value is 0, then no throttling takes
28  * place at all.
29  *
30  * The value represents how long the ecore main loop will sleep (in seconds)
31  * before it goes into a fully idle state waiting for events, input or
32  * timing events to wake it up. For example, if the current throttle level
33  * is 0.5, then after every time the main loop cycles and goes into idle
34  * affter processing all events, the main loop will explicitly sleep for 0.5
35  * seconds before sitting and waiting for incoming events or timeouts, thus
36  * preventing animation, async IO and network handling etc. for that period
37  * of time. Of course these events, data and timeouts will be buffered,
38  * thus not losing anything, simply delaying when they get handled by the
39  * throttle value.
40  *
41  * Example:
42  * @code
43  * void enter_powersave(void) {
44  *    ecore_throttle_adjust(0.2);
45  *    printf("Now at throttle level: %1.3f\n", ecore_throttle_get());
46  * }
47  *
48  * void enter_deep_powersave(void) {
49  *    ecore_throttle_adjust(0.5);
50  *    printf("Now at throttle level: %1.3f\n", ecore_throttle_get());
51  * }
52  *
53  * void exit_powersave(void) {
54  *    ecore_throttle_adjust(-0.2);
55  *    printf("Now at throttle level: %1.3f\n", ecore_throttle_get());
56  * }
57  *
58  * void exit_deep_powersave(void) {
59  *    ecore_throttle_adjust(-0.5);
60  *    printf("Now at throttle level: %1.3f\n", ecore_throttle_get());
61  * }
62  * @endcode
63  *
64  * @param   amount Amount (in seconds) to adjust by
65  */
66 EAPI void
67 ecore_throttle_adjust(double amount)
68 {
69    EINA_MAIN_LOOP_CHECK_RETURN;
70    int adj = amount * 1000000.0;
71    throttle_val += adj;
72    if (throttle_val < 0) throttle_val = 0;
73 }
74
75 /**
76  * Get current throttle level
77  *
78  * This gets the current throttling level, which can be adjusted by
79  * ecore_throttle_adjust(). The value is in seconds. Please see
80  * ecore_throttle_adjust() for more information.
81  *
82  * @return  The current throttle level
83  */
84 EAPI double
85 ecore_throttle_get(void)
86 {
87    EINA_MAIN_LOOP_CHECK_RETURN_VAL(0.0);
88    return (double)throttle_val / 1000000.0;
89 }
90
91 /**
92  * @}
93  */
94
95 void
96 _ecore_throttle(void)
97 {
98    if (throttle_val <= 0) return;
99    usleep(throttle_val);
100 }
101