client: Add new API to handle pending events
[platform/core/uifw/libtdm.git] / client / tdm_client.h
1 /**************************************************************************
2  *
3  * libtdm
4  *
5  * Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved.
6  *
7  * Contact: Eunchul Kim <chulspro.kim@samsung.com>,
8  *          JinYoung Jeon <jy0.jeon@samsung.com>,
9  *          Taeheon Kim <th908.kim@samsung.com>,
10  *          YoungJun Cho <yj44.cho@samsung.com>,
11  *          SooChan Lim <sc1.lim@samsung.com>,
12  *          Boram Park <boram1288.park@samsung.com>
13  *
14  * Permission is hereby granted, free of charge, to any person obtaining a
15  * copy of this software and associated documentation files (the
16  * "Software"), to deal in the Software without restriction, including
17  * without limitation the rights to use, copy, modify, merge, publish,
18  * distribute, sub license, and/or sell copies of the Software, and to
19  * permit persons to whom the Software is furnished to do so, subject to
20  * the following conditions:
21  *
22  * The above copyright notice and this permission notice (including the
23  * next paragraph) shall be included in all copies or substantial portions
24  * of the Software.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
27  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
29  * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
30  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
31  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
32  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33  *
34 **************************************************************************/
35
36 #ifndef _TDM_CLIENT_H_
37 #define _TDM_CLIENT_H_
38
39 #include "tdm_client_types.h"
40
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44
45 /**
46  * @file tdm_client.h
47  * @brief The header file for a client of TDM.
48  * @par Example
49  * @code
50  * #include <tdm_client.h>    //for a client of TDM
51  * @endcode
52  */
53
54 /**
55  * @brief Create a TDM client object.
56  * @param[out] error #TDM_ERROR_NONE if success. Otherwise, error value.
57  * @return A TDM client object if success. Otherwise, NULL.
58  * @see #tdm_client_destroy
59  */
60 tdm_client*
61 tdm_client_create(tdm_error *error);
62
63 /**
64  * @brief Destroy a TDM client object
65  * @param[in] client A TDM client object
66  * @see #tdm_client_create
67  */
68 void
69 tdm_client_destroy(tdm_client *client);
70
71 /**
72  * @brief Get the file descriptor
73  * @param[in] client A TDM client object
74  * @param[out] fd The file descriptor
75  * @return #TDM_ERROR_NONE if success. Otherwise, error value.
76  * @see #tdm_client_handle_events
77  * @par Example
78  * @code
79  * #include <tdm_client.h>    //for a client of TDM
80  *
81  * err = tdm_client_get_fd(client, &fd);
82  * if (err != TDM_ERROR_NONE) {
83  *     //error handling
84  * }
85  *
86  * fds.events = POLLIN;
87  * fds.fd = fd;
88  * fds.revents = 0;
89  *
90  * while(1) {
91  *    ret = poll(&fds, 1, -1);
92  *    if (ret < 0) {
93  *       if (errno == EINTR || errno == EAGAIN)
94  *          continue;
95  *       else {
96  *          //error handling
97  *       }
98  *    }
99  *
100  *    err = tdm_client_handle_events(client);
101  *    if (err != TDM_ERROR_NONE) {
102  *        //error handling
103  *    }
104  * }
105  * @endcode
106  */
107 tdm_error
108 tdm_client_get_fd(tdm_client *client, int *fd);
109
110 /**
111  * @brief Handle the events of the given file descriptor
112  * @param[in] client A TDM client object
113  * @return #TDM_ERROR_NONE if success. Otherwise, error value.
114  * @see #tdm_client_get_fd
115  */
116 tdm_error
117 tdm_client_handle_events(tdm_client *client);
118
119 /**
120  * @brief Handle the events of the given file descriptor with millisecond timeout
121  * @details
122  * -1: infinite. 0: return immediately. Otherwise, waiting for ms_timeout milliseconds.
123  * @param[in] client A TDM client object
124  * @param[in] ms_timeout timeout value.
125  * @return #TDM_ERROR_NONE if success. Otherwise, error value.
126  * @see #tdm_client_get_fd
127  */
128 tdm_error
129 tdm_client_handle_events_timeout(tdm_client *client, int ms_timeout);
130
131 /**
132  * @brief Handle the pending events of the given tdm_client
133  * @param[in] client A TDM client object
134  * @return #TDM_ERROR_NONE if success. Otherwise, error value.
135  * @see #tdm_client_get_fd
136  */
137 tdm_error
138 tdm_client_handle_pending_events(tdm_client *client);
139
140 /**
141  * @brief @b Deprecated. Wait for VBLANK.
142  * @deprecated
143  * @details After interval vblanks, a client vblank handler will be called.
144  * If 'sw_timer' param is 1 in case of DPMS off, TDM will use the SW timer and
145  * call a client vblank handler. Otherwise, this function will return error.
146  * @param[in] client A TDM client object
147  * @param[in] name The name of a TDM output
148  * @param[in] sw_timer 0: not using SW timer, 1: using SW timer
149  * @param[in] interval vblank interval
150  * @param[in] sync 0: asynchronous, 1:synchronous
151  * @param[in] func A client vblank handler
152  * @param[in] user_data The user data
153  * @return #TDM_ERROR_NONE if success. Otherwise, error value.
154  * @see #tdm_client_vblank_handler
155  */
156 tdm_error
157 tdm_client_wait_vblank(tdm_client *client, char *name,
158                                            int sw_timer, int interval, int sync,
159                                            tdm_client_vblank_handler2 func, void *user_data);
160
161 /**
162  * @brief Get the client output object which has the given name.
163  * @details
164  * The client output name can be @b 'primary' or @b 'default' to get the main output.
165  * @param[in] client The TDM client object
166  * @param[in] name The name of the TDM client output object
167  * @param[out] error #TDM_ERROR_NONE if success. Otherwise, error value.
168  * @return A client output object if success. Otherwise, NULL.
169  */
170 tdm_client_output*
171 tdm_client_get_output(tdm_client *client, char *name, tdm_error *error);
172
173 /**
174  * @brief Add the client output change handler
175  * @details The handler will be called when the status of a
176  * client output object is changed. connection, DPMS, etc.
177  * @param[in] output The client output object
178  * @param[in] func The client output change handler
179  * @param[in] user_data The user data
180  * @return #TDM_ERROR_NONE if success. Otherwise, error value.
181  * @par Example
182  * @code
183  * #include <tdm_client.h>    //for a client of TDM
184  *
185  * static void
186  * _client_output_handler(tdm_client_output *output, tdm_output_change_type type,
187  *                        tdm_value value, void *user_data)
188  * {
189  *     char *conn_str[3] = {"disconnected", "connected", "mode_setted"};
190  *     char *dpms_str[4] = {"on", "standy", "suspend", "off"};
191  *
192  *     if (type == TDM_OUTPUT_CHANGE_CONNECTION)
193  *         printf("output %s.\n", conn_str[value.u32]);
194  *     else if (type == TDM_OUTPUT_CHANGE_DPMS)
195  *         printf("dpms %s.\n", dpms_str[value.u32]);
196  * }
197  * ...
198  * tdm_client_output_add_change_handler(output, _client_output_handler, NULL);
199  * ...
200  * tdm_client_output_remove_change_handler(output, _client_output_handler, NULL);
201  *
202  * @endcode
203  */
204 tdm_error
205 tdm_client_output_add_change_handler(tdm_client_output *output,
206                                                                          tdm_client_output_change_handler func,
207                                                                          void *user_data);
208
209 /**
210  * @brief Remove the client output change handler
211  * @param[in] output The client output object
212  * @param[in] func The client output change handler
213  * @param[in] user_data The user data
214  * @return #TDM_ERROR_NONE if success. Otherwise, error value.
215  */
216 void
217 tdm_client_output_remove_change_handler(tdm_client_output *output,
218                                                                                 tdm_client_output_change_handler func,
219                                                                                 void *user_data);
220
221 /**
222  * @brief Get the vertical refresh rate of the given client output
223  * @param[in] output The client output object
224  * @param[out] refresh The refresh rate
225  * @return #TDM_ERROR_NONE if success. Otherwise, error value.
226  */
227 tdm_error
228 tdm_client_output_get_refresh_rate(tdm_client_output *output, unsigned int *refresh);
229
230 /**
231  * @brief Get the width and height of the given client output mode
232  * @param[in] output The client output object
233  * @param[out] width The width of output mode
234  * @param[out] height The height of output mode
235  * @return #TDM_ERROR_NONE if success. Otherwise, error value.
236  */
237 tdm_error
238 tdm_client_output_get_mode(tdm_client_output *output, unsigned int *width, unsigned int *height);
239
240 /**
241  * @brief Get the connection status of the given client output
242  * @param[in] output The client output object
243  * @param[out] status The connection status
244  * @return #TDM_ERROR_NONE if success. Otherwise, error value.
245  */
246 tdm_error
247 tdm_client_output_get_conn_status(tdm_client_output *output, tdm_output_conn_status *status);
248
249 /**
250  * @brief Get the DPMS value of the given client output
251  * @param[in] output The client output object
252  * @param[out] dpms The DPMS value
253  * @return #TDM_ERROR_NONE if success. Otherwise, error value.
254  */
255 tdm_error
256 tdm_client_output_get_dpms(tdm_client_output *output, tdm_output_dpms *dpms);
257
258 /**
259  * @brief Create the client vblank object of the given client output
260  * @details
261  * tdm client vblank basically uses the HW vblank resource. Therefore, if HW vblank
262  * is not available for some reasons, such as output disconnection and dpms off,
263  * #tdm_client_vblank_wait will return error. If you want it to work propery in spite
264  * of these reasons, you can use #tdm_client_vblank_set_enable_fake to get SW fake
265  * vblank events.
266  * @param[in] output The client output object
267  * @param[out] error #TDM_ERROR_NONE if success. Otherwise, error value.
268  * @return A client vblank object if success. Otherwise, NULL.
269  */
270 tdm_client_vblank*
271 tdm_client_output_create_vblank(tdm_client_output *output, tdm_error *error);
272
273 /**
274  * @brief Destroy the client vblank object
275  * @param[in] vblank The client vblank object
276  */
277 void
278 tdm_client_vblank_destroy(tdm_client_vblank *vblank);
279
280 /**
281  * @brief Set the name to the client vblank object
282  * @param[in] vblank The client vblank object
283  * @param[in] name The client vblank name
284  * @return #TDM_ERROR_NONE if success. Otherwise, error value.
285  */
286 tdm_error
287 tdm_client_vblank_set_name(tdm_client_vblank *vblank, const char *name);
288
289 /**
290  * @brief Set the sync value to the client vblank object
291  * @details
292  * If sync == 1, the user client vblank handler of #tdm_client_vblank_wait
293  * will be called before #tdm_client_vblank_wait returns the result. If sync == 0,
294  * the user client vblank handler of #tdm_client_vblank_wait will be called
295  * asynchronously after #tdm_client_vblank_wait returns. Default is @b asynchronous.
296  * @param[in] vblank The client vblank object
297  * @param[in] sync 0: asynchronous, 1:synchronous
298  * @return #TDM_ERROR_NONE if success. Otherwise, error value.
299  */
300 tdm_error
301 tdm_client_vblank_set_sync(tdm_client_vblank *vblank, unsigned int sync);
302
303 /**
304  * @brief Set the fps to the client vblank object
305  * @details Default is the @b vertical @b refresh @b rate of the given client output.
306  * @param[in] vblank The client vblank object
307  * @param[in] fps more than 0
308  * @return #TDM_ERROR_NONE if success. Otherwise, error value.
309  */
310 tdm_error
311 tdm_client_vblank_set_fps(tdm_client_vblank *vblank, unsigned int fps);
312
313 /**
314  * @brief Set the offset(milli-second) to the client vblank object
315  * @details Default is @b 0.
316  * @param[in] vblank The client vblank object
317  * @param[in] offset_ms the offset(milli-second)
318  * @return #TDM_ERROR_NONE if success. Otherwise, error value.
319  */
320 tdm_error
321 tdm_client_vblank_set_offset(tdm_client_vblank *vblank, int offset_ms);
322
323 /**
324  * @brief Enable/Disable the fake vblank to the client vblank object
325  * @details
326  * If enable_fake == 0, #tdm_client_vblank_wait will return error when HW vblank is
327  * not available. Otherwise, #tdm_client_vblank_wait will return TDM_ERROR_NONE
328  * as success. Once #tdm_client_vblank_wait returns TDM_ERROR_NONE, the user client
329  * vblank handler(#tdm_client_vblank_handler) SHOULD be called after the given
330  * interval of #tdm_client_vblank_wait. Default is @b disable.
331  * @param[in] vblank The client vblank object
332  * @param[in] enable_fake 1:enable, 0:disable
333  * @return #TDM_ERROR_NONE if success. Otherwise, error value.
334  */
335 tdm_error
336 tdm_client_vblank_set_enable_fake(tdm_client_vblank *vblank, unsigned int enable_fake);
337
338 /**
339  * @brief Wait for a vblank
340  * @details
341  * This function will return error when HW vblank resource is not available. However,
342  * #tdm_client_vblank_wait will return TDM_ERROR_NONE as success if
343  * #tdm_client_vblank_set_enable_fake sets true. Once #tdm_client_vblank_wait
344  * returns TDM_ERROR_NONE, the user client vblank handler(#tdm_client_vblank_handler)
345  * SHOULD be called after the given interval. \n
346  * The sequence value of tdm_client_vblank_handler is the relative value of fps.
347  * If fps = 10, this sequence value should be increased by 10 during 1 second.
348  * @param[in] vblank The client vblank object
349  * @param[in] interval The vblank interval
350  * @param[in] func The user client vblank handler
351  * @param[in] user_data The user data
352  * @return #TDM_ERROR_NONE if success. Otherwise, error value.
353  * @par Example
354  * @code
355  * #include <tdm_client.h>    //for a client of TDM
356  *
357  * static void
358  * _client_vblank_handler(tdm_client_vblank *vblank, tdm_error error, unsigned int sequence,
359  *                        unsigned int tv_sec, unsigned int tv_usec, void *user_data)
360  * {
361  *     if (error != TDM_ERROR_NONE)
362  *         //error handling
363  * }
364  *
365  * {
366  *     tdm_client_output *output;
367  *     tdm_client_vblank *vblank;
368  *     tdm_error error;
369  *     struct pollfd fds;
370  *     int fd = -1;
371  *
372  *     cliet = tdm_client_create(&error);
373  *     if (error != TDM_ERROR_NONE)
374  *         //error handling
375  *
376  *     output = tdm_client_get_output(client, NULL, &error);
377  *     if (error != TDM_ERROR_NONE)
378  *         //error handling
379  *
380  *     vblank = tdm_client_output_create_vblank(output, &error);
381  *     if (error != TDM_ERROR_NONE)
382  *         //error handling
383  *
384  *     tdm_client_vblank_set_enable_fake(vblank, enable_fake); //default: disable
385  *     tdm_client_vblank_set_sync(vblank, 0); //default: async
386  *     tdm_client_vblank_set_fps(vblank, fps); //default: refresh rate of output
387  *     tdm_client_vblank_set_offset(vblank, offset); //default: 0
388  *
389  *     error = tdm_client_get_fd(data->client, &fd);
390  *     if (error != TDM_ERROR_NONE)
391  *         //error handling
392  *
393  *     fds.events = POLLIN;
394  *     fds.fd = fd;
395  *     fds.revents = 0;
396  *
397  *     while (1) {
398  *         int ret;
399  *
400  *         error = tdm_client_vblank_wait(vblank, interval,
401  *                                        _client_vblank_handler, NULL);
402  *         if (error != TDM_ERROR_NONE)
403  *             //error handling
404  *
405  *         ret = poll(&fds, 1, -1);
406  *         if (ret < 0) {
407  *             if (errno == EINTR || errno == EAGAIN)  // normal case
408  *                 continue;
409  *             else
410  *                 //error handling
411  *         }
412  *
413  *         error = tdm_client_handle_events(client);
414  *         if (error != TDM_ERROR_NONE)
415  *             //error handling
416  *     }
417  *
418  *     tdm_client_vblank_destroy(vblank);
419  *     tdm_client_destroy(client);
420  * }
421  * @endcode
422  */
423 tdm_error
424 tdm_client_vblank_wait(tdm_client_vblank *vblank, unsigned int interval, tdm_client_vblank_handler func, void *user_data);
425
426 /**
427  * @brief Wait for a vblank with the target sequence number
428  * @details
429  * This function will return error when HW vblank resource is not available. However,
430  * #tdm_client_vblank_wait will return TDM_ERROR_NONE as success if
431  * #tdm_client_vblank_set_enable_fake sets true. Once #tdm_client_vblank_wait_seq
432  * returns TDM_ERROR_NONE, the user client vblank handler(#tdm_client_vblank_handler)
433  * SHOULD be called on reaching the target sequence.
434  * @param[in] vblank The client vblank object
435  * @param[in] sequence The target sequence number
436  * @param[in] func The user client vblank handler
437  * @param[in] user_data The user data
438  * @return #TDM_ERROR_NONE if success. Otherwise, error value.
439  */
440 tdm_error
441 tdm_client_vblank_wait_seq(tdm_client_vblank *vblank, unsigned int sequence, tdm_client_vblank_handler func, void *user_data);
442
443 /**
444  * @brief Check if the client vblank object is waiting a vblank event
445  * @param[in] vblank The client vblank object
446  * @return 1 if waiting. 0 if not waiting.
447  */
448 unsigned int
449 tdm_client_vblank_is_waiting(tdm_client_vblank *vblank);
450
451
452 /**
453  * @brief Create the client voutput object which has the given name
454  * @param[in] client The TDM client object
455  * @param[in] name The name of the TDM client virtual output object
456  * @param[out] error #TDM_ERROR_NONE if success. Otherwise, error value.
457  * @return A client voutput object if success. Otherwise, NULL.
458  */
459 tdm_client_voutput *
460 tdm_client_create_voutput(tdm_client *client, const char *name, tdm_error *error);
461
462 /**
463  * @brief Destroy the client voutput object
464  * @param[in] voutput The client voutput object
465  */
466 void
467 tdm_client_voutput_destroy(tdm_client_voutput *voutput);
468
469 /**
470  * @brief Set the available mode info to the client voutput object
471  * @param[in] voutput The client voutput object
472  * @param[in] modes The array of available mode info
473  * @param[in] count The mode count
474  * @return #TDM_ERROR_NONE if success. Otherwise, error value.
475  */
476 tdm_error
477 tdm_client_voutput_set_available_modes(tdm_client_voutput *voutput, const tdm_client_output_mode *modes, int count);
478
479 /**
480  * @brief Set the physical size info to the client voutput object
481  * @param[in] voutput The client voutput object
482  * @param[in] mmWidth The width of voutput
483  * @param[in] mmHeight The height of voutput
484  * @return #TDM_ERROR_NONE if success. Otherwise, error value.
485  */
486 tdm_error
487 tdm_client_voutput_set_physical_size(tdm_client_voutput *voutput, unsigned int mmWidth, unsigned int mmHeight);
488
489 /**
490  * @brief Set the client voutput buffer commit handler
491  * @details The handler will be called when server send buffer.
492  * Client can get the committed buffer in the handler function.
493  * @param[in] voutput The client voutput object
494  * @param[in] func The user client voutput buffer commit handler
495  * @param[in] user_data The user_data
496  * @return #TDM_ERROR_NONE if success. Otherwise, error value.
497  */
498 tdm_error
499 tdm_client_voutput_add_commit_handler(tdm_client_voutput *voutput, tdm_client_voutput_commit_handler func, void *user_data);
500
501 /**
502  * @brief Remove the client voutput buffer commit handler
503  * @param[in] voutput The client voutput object
504  * @param[in] func The user client voutput buffer commit handler
505  * @param[in] user_data The user_data
506  */
507 void
508 tdm_client_voutput_remove_commit_handler(tdm_client_voutput *voutput, tdm_client_voutput_commit_handler func, void *user_data);
509
510 /**
511  * @brief Send the commit done event to server
512  * @details
513  * After using the commited buffer which is get from commit handler
514  * have to send done event to server.
515  * @param[in] voutput The client voutput object
516  * @return #TDM_ERROR_NONE if success. Otherwise, error value.
517  */
518 tdm_error
519 tdm_client_voutput_commit_done(tdm_client_voutput *voutput);
520
521 /**
522  * @brief Get the client output object which was created by the given voutput object
523  * @details
524  * The client output object is made by voutput object.
525  * @param[in] voutput The client voutput object
526  * @param[out] error #TDM_ERROR_NONE if success. Otherwise, error value.
527  * @return A client output object if success. Otherwise, NULL.
528  */
529 tdm_client_output *
530 tdm_client_voutput_get_client_output(tdm_client_voutput *voutput, tdm_error *error);
531
532 /**
533  * @brief Set the mode to the client voutput object
534  * details
535  * The client can set the mode of voutput object by send mode index.
536  * the index is the array number of available modes info.
537  * @param[in] voutput The client voutput object
538  * @param[in] modes The array of available mode info
539  * @param[in] index The index of avaliable modes
540  * @return #TDM_ERROR_NONE if success. Otherwise, error value.
541  */
542 tdm_error
543 tdm_client_voutput_set_mode(tdm_client_voutput *voutput, int index);
544
545 /**
546  * @brief Set the client voutput object connected status
547  * @detail Send availabe_modes, physical size and connect info to the server.
548  * @param[in] voutput The client voutput object
549  * @return #TDM_ERROR_NONE if success. Otherwise, error value.
550  */
551 tdm_error
552 tdm_client_voutput_connect(tdm_client_voutput *voutput);
553
554 /**
555  * @brief Set the client voutput object disconnected status
556  * @param[in] voutput The client voutput object
557  * @return #TDM_ERROR_NONE if success. Otherwise, error value.
558  */
559 tdm_error
560 tdm_client_voutput_disconnect(tdm_client_voutput *voutput);
561
562 #ifdef __cplusplus
563 }
564 #endif
565
566 #endif /* _TDM_CLIENT_H_ */