summaryrefslogtreecommitdiff
path: root/pages/develop/tutorials/c/lifecycle.md.txt
blob: 4438594c316cd5f27ba6f08f7412722747a3033c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
---
~~Title: Lifecycle Management~~
---

# Lifecycle Management #

The [Hello World](/develop/tutorials/c/hello-world) tutorial explained how to create a desktop-like application using the ``EFL_MAIN()`` macro. Applications targeted at mobile or embedded devices, though, often have additional lifecycle constrains, which are explained in this tutorial.

## Prerequisites ##

* Finish the [Hello World](/develop/tutorials/c/hello-world) tutorial to know the basics of EFL application creation.

## Application Management Events ##

EFL will call some special methods in your application to inform you of events related to the application lifecycle, like it being paused, resumed or about to be closed.

If you use the ``EFL_MAIN()`` macro as explained in the [Hello World](/develop/tutorials/c/hello-world) tutorial, though, these methods are hidden from you. While this is usually convenient for desktop applications, you will probably need to use them in mobile or embedded applications. To do this, use the ``EFL_MAIN_EX()`` macro instead.

Create a ``lifecycle_main.c`` file and write this:

```c
#define EFL_EO_API_SUPPORT 1
#define EFL_BETA_API_SUPPORT 1

#include <Eina.h>
#include <Efl_Core.h>

EAPI_MAIN void
efl_main(void *data EINA_UNUSED, const Efl_Event *ev)
{
   printf("Lifecycle: launched\n");
}
EFL_MAIN_EX()
```

This looks like the Hello World tutorial, but using ``EFL_MAIN_EX()`` instead. If you try to compile it, though, it will complain (admittedly in a rather convoluted way) about missing symbols.

These are the new methods that you need to add. All of them have the same signature as ``efl_main()``:

| Method              | Purpose                                  |
| ------------------- | ---------------------------------------- |
| ``efl_pause()``     | Application is entering the paused state |
| ``efl_resume()``    | Application is leaving the paused state  |
| ``efl_terminate()`` | Application is about to be terminated    |

Add them with a simple ``printf()`` so we can keep track of the application state changes:

```c
EAPI_MAIN void
efl_pause(void *data EINA_UNUSED, const Efl_Event *ev EINA_UNUSED)
{
   printf("Lifecycle: paused\n");
}

EAPI_MAIN void
efl_resume(void *data EINA_UNUSED, const Efl_Event *ev EINA_UNUSED)
{
   printf("Lifecycle: resumed\n");
}

EAPI_MAIN void
efl_terminate(void *data EINA_UNUSED, const Efl_Event *ev EINA_UNUSED)
{
   printf("Lifecycle: terminated\n");
}
```

The system will typically put your application in the paused state when it leaves the foreground, therefore, you should free as many resources as you can in ``efl_pause()`` so they are available to other applications. In ``efl_resume()`` you can then reload them.

In ``efl_terminate()`` you normally will commit to permanent storage (like a disk file) the application state, so it can be retrieved next time the application is started.

## Simulating Application Management Events ##

The above section explains everything you need to know about application lifecycle management in EFL. However, the code provided so far will not demonstrate much when tested on a desktop environment.

This sections adds a bit of *simulation code* which will artificially trigger the application management events studied before, for the sake of this tutorial only.

Define this method in your code:

```c
static void
_lifecycle_simulation(void *data, const Efl_Event *ev EINA_UNUSED)
{
   Efl_Loop *loop = data;
   static int called = 0;

   switch (called)
     {
        case 0:
          // First call, pause the application
          efl_event_callback_call(loop, EFL_LOOP_EVENT_PAUSE, NULL);
          break;
        case 1:
          // Second call, resume the application
          efl_event_callback_call(loop, EFL_LOOP_EVENT_RESUME, NULL);
          break;
        default:
          // Last call, exit the application
          efl_exit(0);
     }

   called++;
}
```

If you have read the [Events Programming Guide](/develop/guides/c/core/events.md) you will know that ``efl_event_callback_call()`` is manually emitting an event. This is what EFL uses internally to inform your application of these events on systems that support them (i.e. *not* on desktops).

And now create a timer at the end of ``efl_main()`` that will periodically call the above defined ``_lifecycle_simulation()`` method:

```c
   [...]
   efl_add(EFL_LOOP_TIMER_CLASS, ev->object,
           efl_event_callback_add(efl_added, EFL_LOOP_TIMER_EVENT_TICK, _lifecycle_simulation, ev->object),
           efl_loop_timer_interval_set(efl_added, 1.0));
```

Read the [Main Loop Programming Guide](/develop/guides/c/core/main-loop.md) to learn about timers. Suffice to say that this call creates one, registers a callback to it, and configures it to trigger every second.

With this, the program is complete, including simulation code.

## Complete Application ##

The whole program, with some additional comments for clarity, should look like this:

```c
#define EFL_EO_API_SUPPORT 1
#define EFL_BETA_API_SUPPORT 1

#include <Eina.h>
#include <Efl_Core.h>

/*
 * This helper method triggers lifecycle events for the purpose of this demo.
 * efl_pause and efl_resume may never be called for your application, depending
 * on your environment, therefore this demo triggers them directly to show how
 * you can respond.
 */

static void
_lifecycle_simulation(void *data, const Efl_Event *ev EINA_UNUSED)
{
   Efl_Loop *loop = data;
   static int called = 0;

   switch (called)
     {
        case 0:
          // First call, pause the application
          efl_event_callback_call(loop, EFL_LOOP_EVENT_PAUSE, NULL);
          break;
        case 1:
          // Second call, resume the application
          efl_event_callback_call(loop, EFL_LOOP_EVENT_RESUME, NULL);
          break;
        default:
          // Last call, exit the application
          efl_exit(0);
     }

   called++;
}

EAPI_MAIN void
efl_pause(void *data EINA_UNUSED, const Efl_Event *ev EINA_UNUSED)
{
   printf("Lifecycle: paused\n");
}

EAPI_MAIN void
efl_resume(void *data EINA_UNUSED, const Efl_Event *ev EINA_UNUSED)
{
   printf("Lifecycle: resumed\n");
}

EAPI_MAIN void
efl_terminate(void *data EINA_UNUSED, const Efl_Event *ev EINA_UNUSED)
{
   printf("Lifecycle: terminated\n");
}

EAPI_MAIN void
efl_main(void *data EINA_UNUSED, const Efl_Event *ev)
{
   printf("Lifecycle: launched\n");

   // The timer function will trigger the chain of simulated events to show
   // how an app could respond to system lifecycle events.
   efl_add(EFL_LOOP_TIMER_CLASS, ev->object,
           efl_event_callback_add(efl_added, EFL_LOOP_TIMER_EVENT_TICK, _lifecycle_simulation, ev->object),
           efl_loop_timer_interval_set(efl_added, 1.0));
}
EFL_MAIN_EX()
```

You can find it in the EFL repository: [tutorial/c/lifecycle/src/lifecycle_main.c](https://git.enlightenment.org/tools/examples.git/tree/tutorial/c/lifecycle/src/lifecycle_main.c).

If you execute it, you should see how your application receives events for Launching, Pause, Resume and Termination, spaced every second.

## Summary ##

At the end of this tutorial you have learned:

* To use ``EFL_MAIN_EX()`` instead of ``EFL_MAIN()`` if you are interested in application management events.

* The methods to implement in that case are ``efl_pause()``, ``efl_resume()`` and ``efl_terminate()``.

## Further Reading ##

[Hello World](/develop/tutorials/c/hello-world)
:    Tutorial explaining how to create an EFL application.

[Events Programming Guide](/develop/guides/c/core/events.md)
:    Guide explaining how EFL events work.

[Main Loop Programming Guide](/develop/guides/c/core/main-loop.md)
:    Guide explaining how EFL timers work.

[tutorial/c/lifecycle/src/lifecycle_main.c](https://git.enlightenment.org/tools/examples.git/tree/tutorial/c/lifecycle/src/lifecycle_main.c)
:    Application management from the EFL git examples repository.