summaryrefslogtreecommitdiff
path: root/pages/develop/tutorials/c/lifecycle.md.txt
blob: 15b4f66748c322c0a127d6c501d2b2c8305f03ee (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
---
~~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 often have additional lifecycle constrains which are explored in this tutorial.

## Prerequisites ##

You should have completed the [Hello World](/develop/tutorials/c/hello-world) tutorial in order to understand the basics of EFL application creation before proceeding with this tutorial.

## Application Management Events ##

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

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

Begin by creating a ``lifecycle_main.c`` file:

```c
#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 is based on the Hello World tutorial but rewritten to use ``EFL_MAIN_EX()``. If you try to compile it, however, 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 you can more easily 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. You should free as many resources as you can in ``efl_pause()`` so they are available to other applications. You may then reload them in ``efl_resume()``.

In ``efl_terminate()`` you will usually commit the application state to permanent storage, such as a file on disk, 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. The code provided so far, however, 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, for the sake of this tutorial.

Begin by defining this method in your existing 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 the ``efl_event_callback_call()`` function is manually emitting an event. This used internally by EFL to inform your application of these events on systems that support them - i.e. *not* on desktops.

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));
```

This call creates a timer, registers a callback to it and configures it to trigger every second. Read the [Main Loop Programming Guide](/develop/guides/c/core/main-loop.md) for more information on timers.

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

## Complete Application ##

The whole program, with some additional comments for clarity, is reproduced below:

```c
#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()
```

A copy of this program is available in the EFL git repository: [tutorial/c/lifecycle/src/lifecycle_main.c](https://git.enlightenment.org/tools/examples.git/tree/tutorial/c/lifecycle/src/lifecycle_main.c).

Execute the program and you will see how it receives events for Launch, 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: ``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.