4a2068efafc1437bded3885db033c09d346ca01d
[platform/core/appfw/appcore-widget.git] / include / widget_app.h
1 /*
2  * Copyright (c) 2015 - 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18 #ifndef __TIZEN_APPFW_WIDGET_APP_H__
19 #define __TIZEN_APPFW_WIDGET_APP_H__
20
21 #include <tizen.h>
22 #include <app_control.h>
23 #include <app_common.h>
24 #include <bundle.h>
25 #include <widget_errno.h>
26
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30
31 /**
32  * @addtogroup CAPI_WIDGET_APP_MODULE
33  * @{
34  */
35
36
37 /**
38  * @brief Enumeration for destroy type of widget instance.
39  * @since_tizen 2.3.1
40  */
41 typedef enum widget_app_destroy_type {
42         WIDGET_APP_DESTROY_TYPE_PERMANENT = 0x00, /**< User deleted this widget from the viewer */
43         WIDGET_APP_DESTROY_TYPE_TEMPORARY = 0x01, /**< Widget is deleted because of other reasons (e.g. widget process is terminated temporarily by the system) */
44 } widget_app_destroy_type_e;
45
46
47 /**
48  * @brief The widget class handle.
49  * @since_tizen 2.3.1
50  */
51 typedef struct _widget_class *widget_class_h;
52
53
54 /**
55  * @brief The widget context handle.
56  * @since_tizen 2.3.1
57  */
58 typedef struct _widget_context *widget_context_h;
59
60
61 /**
62  * @brief Called when the widget instance starts.
63  * @details The callback function is called after widget instance is created.
64  *          In this callback, you can initialize resources for this instance.
65  * @since_tizen 2.3.1
66  * @param[in] context The context of widget instance
67  * @param[in] content The data set for the previous status
68  * @param[in] w The pixel value for widget width
69  * @param[in] h The pixel value for widget height
70  * @param[in] user_data The user data passed from widget_app_class_create function
71  *
72  * @return #WIDGET_ERROR_NONE on success,
73  *         otherwise an error code (see WIDGET_ERROR_XXX) on failure
74  */
75 typedef int (*widget_instance_create_cb)(widget_context_h context, bundle *content, int w, int h, void *user_data);
76
77
78 /**
79  * @brief Called before the widget instance is destroyed.
80  * @details The callback function is called before widget instance is destroyed.
81  *          In this callback, you can finalize resources for this instance.
82  *          If reason is not #WIDGET_APP_DESTROY_TYPE_TEMPORARY, it should store the current status by using incoming bundle.
83  * @since_tizen 2.3.1
84  * @remark Note that the parameter 'content' is used to save the status of the widget instance.
85  *         As a input parameter, content contains the saved status of the widget instance.
86  *         You can fill the content parameter with the current status in this callback,
87  *         then the framework will save the content by receiving it as a output parameter.
88  *         Consequently, you should not use widget_app_context_set_content_info() api in this callback.
89  *         The content will be overwritten after this callback returns with the 'content' parameter.
90  * @param[in] context The context of widget instance
91  * @param[in] reason The reason for destruction
92  * @param[in,out] content The data set to save
93  * @param[in] user_data The user data passed from widget_app_class_create function
94  * @return #WIDGET_ERROR_NONE on success,
95  *         otherwise an error code (see WIDGET_ERROR_XXX) on failure
96  */
97 typedef int (*widget_instance_destroy_cb)(widget_context_h context, widget_app_destroy_type_e reason, bundle *content, void *user_data);
98
99
100 /**
101  * @brief Called when the widget is invisible.
102  * @details The callback function is called when the widget is invisible.
103  *          The paused instance may be destroyed by framework.
104  * @since_tizen 2.3.1
105  * @param[in] context The context of widget instance
106  * @param[in] user_data The user data passed from widget_app_class_create function
107  * @return #WIDGET_ERROR_NONE on success,
108  *         otherwise an error code (see WIDGET_ERROR_XXX) on failure
109  */
110 typedef int (*widget_instance_pause_cb)(widget_context_h context, void *user_data);
111
112
113 /**
114  * @brief Called when the widget is visible.
115  * @details The callback function is called when the widget is visible.
116  * @since_tizen 2.3.1
117  * @param[in] context The context of widget instance
118  * @param[in] user_data The user data passed from widget_app_class_create function
119  * @return #WIDGET_ERROR_NONE on success,
120  *         otherwise an error code (see WIDGET_ERROR_XXX) on failure
121  */
122 typedef int (*widget_instance_resume_cb)(widget_context_h context, void *user_data);
123
124
125 /**
126  * @brief Called before the widget size is changed.
127  * @details The callback function is called before the widget size is changed.
128  * @since_tizen 2.3.1
129  * @param[in] context The context of widget instance
130  * @param[in] w The pixel value for widget width
131  * @param[in] h The pixel value for widget height
132  * @param[in] user_data The user data passed from widget_app_class_create function
133  * @return #WIDGET_ERROR_NONE on success,
134  *         otherwise an error code (see WIDGET_ERROR_XXX) on failure
135  */
136 typedef int (*widget_instance_resize_cb)(widget_context_h context, int w, int h, void *user_data);
137
138
139 /**
140  * @brief Called when the event for updating widget is received.
141  * @details The callback function is called when the event for updating widget is received.
142  * @since_tizen 2.3.1
143  * @param[in] context The context of widget instance
144  * @param[in] content The data set for updating this widget. It will be provided by requester.
145  *                    Requester can use widget_service_trigger_update()
146  * @param[in] force Although the widget is paused, if it is TRUE, the widget can be updated
147  * @param[in] user_data The user data passed from widget_app_class_create function
148  * @return #WIDGET_ERROR_NONE on success,
149  *         otherwise an error code (see WIDGET_ERROR_XXX) on failure
150  * @see widget_service_trigger_update()
151  */
152 typedef int (*widget_instance_update_cb)(widget_context_h context, bundle *content, int force, void *user_data);
153
154
155 /**
156  * @brief The structure type containing the set of callback functions for lifecycle of a widget instance.
157  * @since_tizen 2.3.1
158  */
159 typedef struct {
160         widget_instance_create_cb create; /**< The callback function is called after widget instance is created. */
161         widget_instance_destroy_cb destroy; /**< The callback function is called before widget instance is destroyed. */
162         widget_instance_pause_cb pause; /**< The callback function is called when the widget is invisible. */
163         widget_instance_resume_cb resume; /**< The callback function is called when the widget is visible. */
164         widget_instance_resize_cb resize; /**< The callback function is called before the widget size is changed. */
165         widget_instance_update_cb update; /**< The callback function is called when the event for updating widget is received. */
166 } widget_instance_lifecycle_callback_s;
167
168
169 /**
170  * @brief Called when the application starts.
171  * @details The callback function is called before the main loop of the application starts.
172  *          In this callback, you can initialize resources which can be shared among widget instances.
173  *          This function should return the handle for widget class so that it will be used for making instances of widget.
174  * @since_tizen 2.3.1
175  * @param[in] user_data The user data passed from the callback registration function
176  * @return The object of widget class
177  * @see widget_app_main()
178  * @code
179  *
180  * static widget_class_h __widget_app_created(void *user_data)
181  * {
182  *     widget_instance_lifecycle_callback_s callback = { .... };
183  *
184  *     return widget_app_class_create(callback);
185  * }
186  *
187  * @endcode
188  */
189 typedef widget_class_h (*widget_app_create_cb)(void *user_data);
190
191
192 /**
193  * @brief Called when the application's main loop exits.
194  * @details This callback function is called once after the main loop of the application exits.
195  *          You should release the application's resources in this function.
196  * @since_tizen 2.3.1
197  * @param[in] user_data The user data passed from the callback registration function
198  * @see widget_app_main()
199  */
200 typedef void (*widget_app_terminate_cb)(void *user_data);
201
202
203 /**
204  * @brief The structure for lifecycle of a widget application.
205  * @since_tizen 2.3.1
206  */
207 typedef struct {
208         widget_app_create_cb create; /**< The callback function is called before the main loop of the application starts. */
209         widget_app_terminate_cb terminate; /**< This callback function is called once after the main loop of the application exits. */
210 } widget_app_lifecycle_callback_s;
211
212
213 /**
214  * @brief Called for each widget context.
215  * @details This function will be called in the function of widget_app_foreach_context repeatedly.
216  * @since_tizen 2.3.1
217  * @param[in] context The context for widget instance
218  * @param[in] data The data for caller
219  * @return @c true to continue with the next iteration of the loop,
220  *         otherwise @c false to break out of the loop
221  * @see widget_app_foreach_context()
222  */
223 typedef bool (*widget_context_cb)(widget_context_h context, void *data);
224
225
226 /**
227  * @brief Runs the main loop of the application until widget_app_exit() is called.
228  * @since_tizen 2.3.1
229  * @param[in] argc The argument count
230  * @param[in] argv The argument vector
231  * @param[in] callback The set of callback functions to handle application events
232  * @param[in] user_data The user data to be passed to the callback functions
233  * @return #WIDGET_ERROR_NONE on success,
234  *         otherwise an error code (see WIDGET_ERROR_XXX) on failure
235  * @retval #WIDGET_ERROR_NONE Successful
236  * @retval #WIDGET_ERROR_INVALID_PARAMETER Invalid parameter
237  * @retval #WIDGET_ERROR_NOT_SUPPORTED Not supported
238  * @retval #WIDGET_ERROR_FAULT Unrecoverable error
239  * @see widget_app_exit()
240  */
241 int widget_app_main(int argc, char **argv, widget_app_lifecycle_callback_s *callback, void *user_data);
242
243
244 /**
245  * @brief Exits the main loop of the application.
246  * @details The main loop of the application stops and widget_app_terminate_cb() is invoked.
247  * @since_tizen 2.3.1
248  * @return #WIDGET_ERROR_NONE on success,
249  *         otherwise an error code (see WIDGET_ERROR_XXX) on failure
250  * @retval #WIDGET_ERROR_NONE Successful
251  * @retval #WIDGET_ERROR_NOT_SUPPORTED Not supported
252  * @retval #WIDGET_ERROR_FAULT Unrecoverable error
253  * @see widget_app_main()
254  * @see widget_app_terminate_cb()
255  */
256 int widget_app_exit(void);
257
258
259 /**
260  * @brief Finishes context for the widget instance.
261  * @since_tizen 2.3.1
262  * @param[in] context The context for widget instance
263  * @return #WIDGET_ERROR_NONE on success,
264  *         otherwise an error code (see WIDGET_ERROR_XXX) on failure
265  * @retval #WIDGET_ERROR_NONE Successful
266  * @retval #WIDGET_ERROR_INVALID_PARAMETER Invalid parameter
267  * @retval #WIDGET_ERROR_NOT_SUPPORTED Not supported
268  * @retval #WIDGET_ERROR_FAULT Unrecoverable error
269  */
270 int widget_app_terminate_context(widget_context_h context);
271
272
273 /**
274  * @brief Retrieves all widget contexts in this application.
275  * @since_tizen 2.3.1
276  * @param[in] callback The iteration callback function
277  * @param[in] data The data for the callback function
278  * @return #WIDGET_ERROR_NONE on success,
279  *         otherwise an error code (see WIDGET_ERROR_XXX) on failure
280  * @retval #WIDGET_ERROR_NONE Successful
281  * @retval #WIDGET_ERROR_INVALID_PARAMETER Invalid parameter
282  * @retval #WIDGET_ERROR_CANCELED The iteration is canceled
283  * @retval #WIDGET_ERROR_NOT_SUPPORTED Not supported
284  * @retval #WIDGET_ERROR_FAULT Unrecoverable error
285  * @see widget_app_foreach_context()
286  */
287 int widget_app_foreach_context(widget_context_cb callback, void *data);
288
289
290 /**
291  * @brief Adds the system event handler.
292  * @since_tizen 2.3.1
293  * @param[out] event_handler The event handler
294  * @param[in] event_type The system event type. APP_EVENT_DEVICE_ORIENTATION_CHANGED is not supported
295  * @param[in] callback The callback function
296  * @param[in] user_data The user data to be passed to the callback function
297  * @return #WIDGET_ERROR_NONE on success,
298  *         otherwise an error code (see WIDGET_ERROR_XXX) on failure
299  * @retval #WIDGET_ERROR_NONE Successful
300  * @retval #WIDGET_ERROR_INVALID_PARAMETER Invalid parameter
301  * @retval #WIDGET_ERROR_OUT_OF_MEMORY Out of memory
302  * @retval #WIDGET_ERROR_NOT_SUPPORTED Not supported
303  * @retval #WIDGET_ERROR_FAULT Unrecoverable error
304  * @see app_event_type_e
305  * @see app_event_cb()
306  * @see watch_app_remove_event_handler()
307  */
308 int widget_app_add_event_handler(app_event_handler_h *event_handler, app_event_type_e event_type,
309                 app_event_cb callback, void *user_data);
310
311 /**
312  * @brief Removes registered event handler.
313  * @since_tizen 2.3.1
314  * @param[in] event_handler The event handler
315  * @return #WIDGET_ERROR_NONE on success,
316  *         otherwise an error code (see WIDGET_ERROR_XXX) on failure
317  * @retval #WIDGET_ERROR_NONE Successful
318  * @retval #WIDGET_ERROR_INVALID_PARAMETER Invalid parameter
319  * @retval #WIDGET_ERROR_NOT_SUPPORTED Not supported
320  * @retval #WIDGET_ERROR_FAULT Unrecoverable error
321  * @see watch_app_add_event_handler()
322  */
323 int widget_app_remove_event_handler(app_event_handler_h event_handler);
324
325
326 /**
327  * @brief Gets a widget instance id.
328  * @since_tizen 2.3.1
329  * @remarks The specific error code can be obtained using the get_last_result() method. Error codes are described in Exception section.
330  * @remark You must not free returned Widget ID
331  * @param[in] context The context for widget instance
332  * @return Widget ID on success,
333  *         otherwise NULL
334  * @exception #WIDGET_ERROR_NOT_SUPPORTED Not supported
335  * @exception #WIDGET_ERROR_FAULT Unrecoverable error
336  * @see get_last_result()
337  */
338 const char *widget_app_get_id(widget_context_h context);
339
340
341 /**
342  * @brief Makes a class for widget instances.
343  * @since_tizen 2.3.1
344  * @remarks The specific error code can be obtained using the get_last_result() method. Error codes are described in Exception section.
345  * @param[in] callback The set of lifecycle callbacks
346  * @param[in] user_data The user data to be passed to the callback functions
347  * @return The new widget class object,
348  *         NULL on error
349  * @exception #WIDGET_ERROR_NONE Successfully added
350  * @exception #WIDGET_ERROR_INVALID_PARAMETER Not supported
351  * @exception #WIDGET_ERROR_NOT_SUPPORTED Not supported
352  * @exception #WIDGET_ERROR_OUT_OF_MEMORY Out of memory
353  * @see get_last_result()
354  */
355 widget_class_h widget_app_class_create(widget_instance_lifecycle_callback_s callback, void *user_data);
356
357
358 /**
359  * @brief Sets a tag in the context.
360  * @since_tizen 2.3.1
361  * @param[in] context The context for widget instance
362  * @param[in] tag The value to save
363  * @return #WIDGET_ERROR_NONE on success,
364  *         otherwise an error code (see WIDGET_ERROR_XXX) on failure
365  * @retval #WIDGET_ERROR_NOT_SUPPORTED Not supported
366  * @retval #WIDGET_ERROR_INVALID_PARAMETER Invalid parameter
367  * @retval #WIDGET_ERROR_FAULT Unrecoverable error
368  */
369 int widget_app_context_set_tag(widget_context_h context, void *tag);
370
371
372 /**
373  * @brief Gets the tag in the context.
374  * @since_tizen 2.3.1
375  * @param[in] context The context for widget instance
376  * @param[out] tag The value to get
377  * @return #WIDGET_ERROR_NONE on success,
378  *         otherwise an error code (see WIDGET_ERROR_XXX) on failure
379  * @retval #WIDGET_ERROR_NOT_SUPPORTED Not supported
380  * @retval #WIDGET_ERROR_INVALID_PARAMETER Invalid parameter
381  * @retval #WIDGET_ERROR_FAULT Unrecoverable error
382  */
383 int widget_app_context_get_tag(widget_context_h context, void **tag);
384
385
386 /**
387  * @brief Sets the content info to the widget.
388  * @since_tizen 2.3.1
389  * @param[in] context The context for widget instance
390  * @param[in] content_info The data set to save
391  * @return #WIDGET_ERROR_NONE on success,
392  *         otherwise an error code (see WIDGET_ERROR_XXX) on failure
393  * @retval #WIDGET_ERROR_NONE Successfully sent
394  * @retval #WIDGET_ERROR_INVALID_PARAMETER Invalid argument
395  * @retval #WIDGET_ERROR_NOT_SUPPORTED Not supported
396  * @retval #WIDGET_ERROR_OUT_OF_MEMORY Out of memory
397  * @retval #WIDGET_ERROR_FAULT Unrecoverable error
398  */
399 int widget_app_context_set_content_info(widget_context_h context, bundle *content_info);
400
401
402 /**
403  * @brief Sends the title to the widget.
404  * @since_tizen 2.3.1
405  * @param[in] context The context for widget instance
406  * @param[in] title When an accessibility mode is turned on, this string will be read
407  * @return #WIDGET_ERROR_NONE on success,
408  *         otherwise an error code (see WIDGET_ERROR_XXX) on failure
409  * @retval #WIDGET_ERROR_NONE Successfully sent
410  * @retval #WIDGET_ERROR_INVALID_PARAMETER Invalid argument
411  * @retval #WIDGET_ERROR_NOT_SUPPORTED Not supported
412  * @retval #WIDGET_ERROR_OUT_OF_MEMORY Out of memory
413  * @retval #WIDGET_ERROR_FAULT Unrecoverable error
414  */
415 int widget_app_context_set_title(widget_context_h context, const char *title);
416
417
418 /**
419  * @brief Adds an additional widget class for multi-class of widget instantiation.
420  * @since_tizen 3.0
421  * @remarks The specific error code can be obtained using the get_last_result() method. Error codes are described in Exception section.
422  * @param[in] widget_class The result of widget_app_class_create()
423  * @param[in] class_id The class id of provider
424  * @param[in] callback The set of lifecycle callbacks
425  * @param[in] user_data The user data to be passed to the callback functions
426  * @return The new widget class object,
427  *         NULL on error
428  * @exception #WIDGET_ERROR_NONE Successfully added
429  * @exception #WIDGET_ERROR_INVALID_PARAMETER Not supported
430  * @exception #WIDGET_ERROR_NOT_SUPPORTED Not supported
431  * @exception #WIDGET_ERROR_OUT_OF_MEMORY Out of memory
432  * @see get_last_result()
433  */
434 widget_class_h widget_app_class_add(widget_class_h widget_class, const char *class_id,
435                 widget_instance_lifecycle_callback_s callback, void *user_data);
436
437 /**
438  * @}
439  */
440
441 #ifdef __cplusplus
442 }
443 #endif
444
445 #endif /* __TIZEN_APPFW_WIDGET_APP_H__ */