appcore-widget : initial commit
[platform/core/appfw/appcore-widget.git] / include / widget_app.h
1 /*
2  * Copyright (c) 2015 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  * @brief Destroy type of widget instance
38  * @since_tizen 2.3.1
39  */
40 typedef enum widget_app_destroy_type {
41         WIDGET_APP_DESTROY_TYPE_DEFAULT = 0x00,   /**< Deleted */
42         WIDGET_APP_DESTROY_TYPE_UPGRADE = 0x01,   /**< Deleted for upgrading */
43         WIDGET_APP_DESTROY_TYPE_UNINSTALL = 0x02, /**< Deleted by uninstalling */
44         WIDGET_APP_DESTROY_TYPE_TERMINATE = 0x03, /**< Deleted for reboot device */
45         WIDGET_APP_DESTROY_TYPE_FAULT = 0x04,     /**< Deleted by system-fault */
46         WIDGET_APP_DESTROY_TYPE_TEMPORARY = 0x05, /**< Temporarly deleted, will be created again */
47         WIDGET_APP_DESTROY_TYPE_UNKNOWN = 0x06    /**< Undefined reason */
48 } widget_app_destroy_type_e; /**< Delete type */
49
50 /**
51  * @brief The handle for widget class.
52  * @since_tizen 2.3.1
53  */
54 typedef struct _widget_class* widget_class_h;
55
56 /**
57  * @brief The handle for widget context
58  * @since_tizen 2.3.1
59  */
60 typedef struct _widget_context* widget_context_h;
61
62 /**
63  * @brief Called when the widget instance starts.
64  * @since_tizen 2.3.1
65  *
66  * @details The callback function is called after widget instance is created.
67  *          In this callback, you can initialize resources for this instance.
68  *
69  * @param[in] context The context of widget instance.
70  * @param[in] content The data set for the previous status
71  * @param[in] w The pixel value for widget width
72  * @param[in] h The pixel value for widget height
73  *
74  * @return #WIDGET_ERROR_NONE on success,
75  *         otherwise an error code (see WIDGET_ERROR_XXX) on failure
76  */
77 typedef int (*widget_instance_create_cb)(widget_context_h context, bundle *content, int w, int h);
78
79 /**
80  * @brief Called before the widget instance is destroyed.
81  * @since_tizen 2.3.1
82  *
83  * @details The callback function is called before widget instance is destroyed.
84  *          In this callback, you can finalize resources for this instance.
85  *          If reason is not #WIDGET_APP_DESTROY_TYPE_DEFAULT, It should store the current status by using incoming bundle.
86  *
87  * @param[in] context The context of widget instance.
88  * @param[in] reason The reason for destruction
89  * @param[in,out] content The data set to save
90  * @remark Note that the parameter 'content' is used to save the status of the widget instance.
91  *         As a input parameter, content contains the saved status of the widget instance.
92  *         You can fill the content parameter with the current status in this callback,
93  *         then the framework will save the content by receiving it as a output parameter.
94  *         Consequently, you should not use widget_app_context_set_content_info() api in this callback.
95  *         The content will be overwritten after this callback returns with the 'content' parameter.
96  *
97  * @return #WIDGET_ERROR_NONE on success,
98  *         otherwise an error code (see WIDGET_ERROR_XXX) on failure
99  */
100 typedef int (*widget_instance_destroy_cb)(widget_context_h context, widget_app_destroy_type_e reason, bundle *content);
101
102 /**
103  * @brief Called when the widget is invisible.
104  * @since_tizen 2.3.1
105  *
106  * @details The callback function is called when the widget is invisible.
107  *          The paused instance may be destroyed by framework
108  *
109  * @param[in] context The context of widget instance.
110  * @return #WIDGET_ERROR_NONE on success,
111  *         otherwise an error code (see WIDGET_ERROR_XXX) on failure
112  */
113 typedef int (*widget_instance_pause_cb)(widget_context_h context);
114
115 /**
116  * @brief Called when the widget is visible.
117  * @since_tizen 2.3.1
118  *
119  * @details The callback function is called when the widget is visible.
120  *
121  * @param[in] context The context of widget instance.
122  * @return #WIDGET_ERROR_NONE on success,
123  *         otherwise an error code (see WIDGET_ERROR_XXX) on failure
124  */
125 typedef int (*widget_instance_resume_cb)(widget_context_h context);
126
127 /**
128  * @brief Called before the widget size is changed.
129  * @since_tizen 2.3.1
130  *
131  * @details The callback function is called before the widget size is changed.
132  *
133  * @param[in] context The context of widget instance.
134  * @param[in] w The pixel value for widget width
135  * @param[in] h The pixel value for widget height
136  * @return #WIDGET_ERROR_NONE on success,
137  *         otherwise an error code (see WIDGET_ERROR_XXX) on failure
138  */
139 typedef int (*widget_instance_resize_cb)(widget_context_h context, int w, int h);
140
141 /**
142  * @brief Called when the event for updating widget is received.
143  * @since_tizen 2.3.1
144  *
145  * @details The callback function is called when the event for updating widget is received.
146  *
147  * @param[in] context The context of widget instance.
148  * @param[in] content The data set for updating this widget. It will be provided by requester.
149  *                    Requester can use widget_service_trigger_update().
150  * @param[in] force Although the widget is paused, if it is TRUE, the widget can be updated.
151  * @return #WIDGET_ERROR_NONE on success,
152  *         otherwise an error code (see WIDGET_ERROR_XXX) on failure
153  * @see widget_service_trigger_update
154  */
155 typedef int (*widget_instance_update_cb)(widget_context_h context, bundle *content, int force);
156
157 /**
158  * @brief The structure for lifecycle of a widget instance
159  * @since_tizen 2.3.1
160  */
161 typedef struct
162 {
163         widget_instance_create_cb create; /**< The callback function is called after widget instance is created. */
164         widget_instance_destroy_cb destroy; /**< The callback function is called before widget instance is destroyed. */
165         widget_instance_pause_cb pause; /**< The callback function is called when the widget is invisible. */
166         widget_instance_resume_cb resume; /**< The callback function is called when the widget is visible. */
167         widget_instance_resize_cb resize; /**< The callback function is called before the widget size is changed. */
168         widget_instance_update_cb update; /**< The callback function is called when the event for updating widget is received. */
169 } widget_instance_lifecycle_callback_s;
170
171 /**
172  * @brief Called when the application starts.
173  * @since_tizen 2.3.1
174  *
175  * @details The callback function is called before the main loop of the application starts.
176  *          In this callback, you can initialize resources which can be shared among widget instances.
177  *          This function should return the handle for widget class so that it will be used for making instances of widget.
178  *
179  * @param[in] user_data The user data passed from the callback registration function
180  * @return The object of widget class
181  * @see widget_app_main()
182  * @code
183  *
184  * static widget_class_h __widget_app_created(void *user_data)
185  * {
186  *     widget_instance_lifecycle_callback_s callback = { .... };
187  *
188  *     return widget_app_class_create(callback);
189  * }
190  *
191  * @endcode
192  */
193 typedef widget_class_h (*widget_app_create_cb)(void *user_data);
194
195 /**
196  * @brief Called when the application's main loop exits.
197  * @details This callback function is called once after the main loop of the application exits.
198  *          You should release the application's resources in this function.
199  * @since_tizen 2.3.1
200  *
201  * @param[in] user_data The user data passed from the callback registration function
202  * @see widget_app_main()
203  */
204 typedef void (*widget_app_terminate_cb)(void *user_data);
205
206 /**
207  * @brief The structure for lifecycle of a widget application
208  * @since_tizen 2.3.1
209  */
210 typedef struct
211 {
212         widget_app_create_cb create; /**< The callback function is called before the main loop of the application starts. */
213         widget_app_terminate_cb terminate; /**< This callback function is called once after the main loop of the application exits. */
214 } widget_app_lifecycle_callback_s;
215
216 /**
217  * @brief Called for each widget context
218  * @since_tizen 2.3.1
219  *
220  * @details This function will be called in the function of widget_app_foreach_context repeatedly.
221  *
222  * @param[in] context The context for widget instance
223  * @param[in] data The data for caller
224  * @return true to continue with the next iteration of the loop,
225  *         otherwise false to break out of the loop.
226  * @see widget_app_foreach_context
227  */
228 typedef bool (*widget_context_cb)(widget_context_h context, void *data);
229
230 /**
231  * @brief Runs the main loop of the application until widget_app_exit() is called.
232  * @since_tizen 2.3.1
233  *
234  * @param[in] argc The argument count
235  * @param[in] argv The argument vector
236  * @param[in] callback The set of callback functions to handle application events
237  * @param[in] user_data The user data to be passed to the callback functions
238  *
239  * @return @c 0 on success,
240  *         otherwise a negative error value.
241  * @retval #WIDGET_ERROR_NONE Successful
242  * @retval #WIDGET_ERROR_INVALID_PARAMETER Invalid parameter
243  * @retval #WIDGET_ERROR_NOT_SUPPORTED Not supported
244  * @retval #WIDGET_ERROR_FAULT Unrecoverable error
245  * @see widget_app_exit
246  */
247 int widget_app_main(int argc, char **argv, widget_app_lifecycle_callback_s *callback, void *user_data);
248
249 /**
250  * @brief Exits the main loop of the application.
251  * @details The main loop of the application stops and widget_app_terminate_cb() is invoked.
252  * @since_tizen 2.3.1
253  *
254  * @return @c 0 on success,
255  *         otherwise a negative error value.
256  * @retval #WIDGET_ERROR_NOT_SUPPORTED Not supported
257  * @retval #WIDGET_ERROR_FAULT Unrecoverable error
258  * @see widget_app_main
259  * @see widget_app_terminate_cb
260  */
261 int widget_app_exit(void);
262
263 /**
264  * @brief Finishes context for the widget instance
265  * @since_tizen 2.3.1
266  *
267  * @param[in] context The context for widget instance
268  *
269  * @return @c 0 on success, otherwise a negative error value.
270  * @retval #WIDGET_ERROR_NONE Successfull
271  * @retval #WIDGET_ERROR_INVALID_PARAMETER Invalid parameter
272  * @retval #WIDGET_ERROR_NOT_SUPPORTED Not supported
273  * @retval #WIDGET_ERROR_FAULT Unrecoverable error
274  */
275 int widget_app_terminate_context(widget_context_h context);
276
277 /**
278  * @brief Retrieves all widget contexts in this application
279  * @since_tizen 2.3.1
280  *
281  * @param[in] callback The iteration callback function
282  * @param[in] data The data for the callback function
283  *
284  * @return 0 on success, otherwise a negative error value
285  * @retval #WIDGET_ERROR_NONE Successfull
286  * @retval #WIDGET_ERROR_INVALID_PARAMETER Invalid parameter
287  * @retval #WIDGET_ERROR_CANCELED The iteration is canceled
288  * @retval #WIDGET_ERROR_NOT_SUPPORTED Not supported
289  * @retval #WIDGET_ERROR_FAULT Unrecoverable error
290  * @see widget_app_foreach_context
291  */
292 int widget_app_foreach_context(widget_context_cb callback, void *data);
293
294 /**
295  * @brief Adds the system event handler
296  * @since_tizen 2.3.1
297  *
298  * @param[out] event_handler The event handler
299  * @param[in] event_type The system event type. APP_EVENT_DEVICE_ORIENTATION_CHANGED is not supported
300  * @param[in] callback The callback function
301  * @param[in] user_data The user data to be passed to the callback function
302  *
303  * @return 0 on success, otherwise a negative error value
304  * @retval #WIDGET_ERROR_NONE Successfull
305  * @retval #WIDGET_ERROR_INVALID_PARAMETER Invalid parameter
306  * @retval #WIDGET_ERROR_OUT_OF_MEMORY Out of memory
307  * @retval #WIDGET_ERROR_NOT_SUPPORTED Not supported
308  * @retval #WIDGET_ERROR_FAULT Unrecoverable error
309  * @see app_event_type_e
310  * @see app_event_cb
311  * @see watch_app_remove_event_handler
312  */
313 int widget_app_add_event_handler(app_event_handler_h *event_handler, app_event_type_e event_type,
314                                  app_event_cb callback, void *user_data);
315
316 /**
317  * @brief Removes registered event handler
318  * @since_tizen 2.3.1
319  *
320  * @param[in] event_handler The event handler
321  *
322  * @return 0 on success, otherwise a negative error value
323  * @retval #WIDGET_ERROR_NONE Successfull
324  * @retval #WIDGET_ERROR_INVALID_PARAMETER Invalid parameter
325  * @retval #WIDGET_ERROR_NOT_SUPPORTED Not supported
326  * @retval #WIDGET_ERROR_FAULT Unrecoverable error
327  * @see watch_app_add_event_handler
328  */
329 int widget_app_remove_event_handler(app_event_handler_h event_handler);
330
331 /**
332  * @brief Gets a widget instance id
333  * @since_tizen 2.3.1
334  *
335  * @param[in] context The context for widget instance
336  *
337  * @return Widget ID on success, otherwise NULL
338  *         You can get the returned value using get_last_result()
339  * @retval #WIDGET_ERROR_NOT_SUPPORTED Not supported
340  * @retval #WIDGET_ERROR_FAULT Unrecoverable error
341  * @remark You must not free returned Widget ID
342  * @see get_last_result
343  */
344 const char* widget_app_get_id(widget_context_h context);
345
346 /**
347  * @brief Makes a class for widget instances.
348  * @since_tizen 2.3.1
349  *
350  * @param[in] callback The set of lifecycle callbacks
351  * @return The handle of class on success, otherwise NULL
352  *         You can get the returned value using get_last_result()
353  * @retval #WIDGET_ERROR_NOT_SUPPORTED Not supported
354  * @see get_last_result
355  */
356 widget_class_h widget_app_class_create(widget_instance_lifecycle_callback_s callback);
357
358 /**
359  * @brief Sets a tag in the context
360  * @since_tizen 2.3.1
361  *
362  * @param[in] context The context for widget instance
363  * @param[in] tag The value to save
364  *
365  * @return 0 on success, otherwise a negative error value
366  * @retval #WIDGET_ERROR_NOT_SUPPORTED Not supported
367  * @retval #WIDGET_ERROR_INVALID_PARAMETER Invalid parameter
368  * @retval #WIDGET_ERROR_FAULT Unrecoverable error
369  */
370 int widget_app_context_set_tag(widget_context_h context, void *tag);
371
372 /**
373  * @brief Gets the tag in the context
374  * @since_tizen 2.3.1
375  *
376  * @param[in] context The context for widget instance
377  * @param[out] tag The value to get
378  *
379  * @return 0 on success, otherwise a negative error value
380  * @retval #WIDGET_ERROR_NOT_SUPPORTED Not supported
381  * @retval #WIDGET_ERROR_INVALID_PARAMETER Invalid parameter
382  * @retval #WIDGET_ERROR_FAULT Unrecoverable error
383  */
384 int widget_app_context_get_tag(widget_context_h context, void **tag);
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_INVALID_PARAMETER Invalid argument
394  * @retval #WIDGET_ERROR_NOT_SUPPORTED Not supported
395  * @retval #WIDGET_ERROR_OUT_OF_MEMORY Out of memory
396  * @retval #WIDGET_ERROR_FAULT Unrecoverable error
397  * @retval #WIDGET_ERROR_NONE Successfully sent
398  */
399 int widget_app_context_set_content_info(widget_context_h context, bundle *content_info);
400
401 /**
402  * @brief Sends the title to the widget
403  * @since_tizen 2.3.1
404  * @param[in] context The context for widget instance
405  * @param[in] title When an accessibility mode is turned on, this string will be read.
406  * @return #WIDGET_ERROR_NONE on success,
407  *          otherwise an error code (see WIDGET_ERROR_XXX) on failure
408  * @retval #WIDGET_ERROR_INVALID_PARAMETER Invalid argument
409  * @retval #WIDGET_ERROR_NOT_SUPPORTED Not supported
410  * @retval #WIDGET_ERROR_OUT_OF_MEMORY Out of memory
411  * @retval #WIDGET_ERROR_FAULT Unrecoverable error
412  * @retval #WIDGET_ERROR_NONE Successfully sent
413  */
414 int widget_app_context_set_title(widget_context_h context, const char *title);
415
416 /**
417  * @}
418  */
419
420 #ifdef __cplusplus
421 }
422 #endif
423
424 #endif /* __TIZEN_APPFW_WIDGET_APP_H__ */