Fix build error on gcc-13 Build
[platform/core/uifw/voice-control.git] / client / vc_widget_client.c
1 /*
2 * Copyright (c) 2011-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 #include "vc_main.h"
19 #include "vc_widget_client.h"
20 #include "voice_control_command.h"
21 #include "voice_control_common.h"
22
23 /* Max number of handle */
24 static const unsigned int MAX_NUMBER_OF_HANDLE = 999;
25 /* allocated handle */
26 static unsigned int g_allocated_handle = 0;
27 /* widget list */
28 static GSList *g_widget_list = NULL;
29
30 static pthread_mutex_t g_widget_list_mutex = PTHREAD_MUTEX_INITIALIZER;
31
32 vc_widget_s* widget_get(vc_h vc)
33 {
34         pthread_mutex_lock(&g_widget_list_mutex);
35
36         if (vc == NULL) {
37                 SLOG(LOG_WARN, TAG_VCW, "[WARNING] Input parameter is NULL");
38                 pthread_mutex_unlock(&g_widget_list_mutex);
39                 return NULL;
40         }
41
42         vc_widget_s *data = NULL;
43
44         int count = g_slist_length(g_widget_list);
45         int i;
46
47         for (i = 0; i < count; i++) {
48                 data = g_slist_nth_data(g_widget_list, i);
49
50                 if (NULL != data && NULL != data->vc) {
51                         if (vc->handle == data->vc->handle) {
52                                 pthread_mutex_unlock(&g_widget_list_mutex);
53                                 return data;
54                         }
55                 }
56         }
57
58         SLOG(LOG_DEBUG, TAG_VCW, "[DEBUG] Fail to get widget by vc");
59
60         pthread_mutex_unlock(&g_widget_list_mutex);
61         return NULL;
62 }
63
64 static unsigned int __client_generate_uid(unsigned int pid)
65 {
66         g_allocated_handle++;
67
68         if (g_allocated_handle > MAX_NUMBER_OF_HANDLE) {
69                 g_allocated_handle = 1;
70         }
71
72         /* generate uid, handle number should be smaller than 1000 */
73         return pid * 1000u + g_allocated_handle;
74 }
75
76 int vc_widget_client_create(vc_h* vc)
77 {
78         vc_widget_s *widget = NULL;
79
80         widget = (vc_widget_s*)calloc(1, sizeof(vc_widget_s));
81         if (NULL == widget) {
82                 SLOG(LOG_ERROR, TAG_VCW, "[ERROR] Fail to allocate memory");
83                 return VC_ERROR_OUT_OF_MEMORY;
84         }
85
86         vc_h temp = (vc_h)calloc(1, sizeof(struct vc_s));
87         if (NULL == temp) {
88                 SLOG(LOG_ERROR, TAG_VCW, "[ERROR] Fail to allocate memory");
89                 free(widget);
90                 return VC_ERROR_OUT_OF_MEMORY;
91         }
92
93         temp->handle = __client_generate_uid((unsigned int)getpid());
94
95         /* initialize widget data */
96         widget->vc = temp;
97         widget->pid = getpid();
98         widget->uid = temp->handle;
99         widget->xid = -1;
100
101         widget->result_cb = NULL;
102         widget->result_user_data = NULL;
103         widget->asr_result_cb = NULL;
104         widget->asr_result_user_data = NULL;
105         widget->asr_result_enabled = false;
106         widget->service_state_changed_cb = NULL;
107         widget->service_state_changed_user_data = NULL;
108         widget->state_changed_cb = NULL;
109         widget->state_changed_user_data = NULL;
110         widget->show_tooltip_cb = NULL;
111         widget->show_tooltip_user_data = NULL;
112         widget->error_cb = NULL;
113         widget->error_user_data = NULL;
114
115         widget->previous_state = VC_STATE_INITIALIZED;
116         widget->current_state = VC_STATE_INITIALIZED;
117
118         widget->conn_timer = NULL;
119
120         widget->cb_ref_count = 0;
121
122         pthread_mutex_lock(&g_widget_list_mutex);
123         g_widget_list = g_slist_append(g_widget_list, widget);
124         pthread_mutex_unlock(&g_widget_list_mutex);
125
126         *vc = temp;
127
128         return VC_ERROR_NONE;
129 }
130
131 int vc_widget_client_destroy(vc_h vc)
132 {
133         if (vc == NULL) {
134                 SLOG(LOG_ERROR, TAG_VCW, "Input parameter is NULL");
135                 return VC_ERROR_NONE;
136         }
137
138         vc_widget_s *data = NULL;
139
140         pthread_mutex_lock(&g_widget_list_mutex);
141         int count = g_slist_length(g_widget_list);
142         int i;
143
144         for (i = 0; i < count; i++) {
145                 data = g_slist_nth_data(g_widget_list, i);
146
147                 if (NULL != data) {
148                         if (vc->handle == data->vc->handle) {
149                                 g_widget_list =  g_slist_remove(g_widget_list, data);
150
151                                 while (0 != data->cb_ref_count) {
152                                         /* wait for release callback function */
153                                 }
154                                 free(data);
155                                 free(vc);
156
157                                 data = NULL;
158                                 vc = NULL;
159
160                                 pthread_mutex_unlock(&g_widget_list_mutex);
161                                 return VC_ERROR_NONE;
162                         }
163                 }
164         }
165
166         SLOG(LOG_ERROR, TAG_VCW, "[ERROR] widget Not found");
167
168         pthread_mutex_unlock(&g_widget_list_mutex);
169         return VC_ERROR_INVALID_PARAMETER;
170 }
171
172 GSList* vc_widget_client_get_client_list()
173 {
174         pthread_mutex_lock(&g_widget_list_mutex);
175         GSList *ret = g_widget_list;
176         pthread_mutex_unlock(&g_widget_list_mutex);
177         return ret;
178 }
179
180 bool vc_widget_client_is_valid(vc_h vc)
181 {
182         vc_widget_s* widget = widget_get(vc);
183
184         /* check handle */
185         if (NULL == widget) {
186                 SLOG(LOG_DEBUG, TAG_VCW, "[DEBUG] vc is not valid");
187                 return false;
188         }
189
190         return true;
191 }
192
193 bool vc_widget_client_is_valid_by_uid(unsigned int uid)
194 {
195         vc_widget_s *data = NULL;
196
197         pthread_mutex_lock(&g_widget_list_mutex);
198         int count = g_slist_length(g_widget_list);
199         int i;
200
201         for (i = 0; i < count; i++) {
202                 data = g_slist_nth_data(g_widget_list, i);
203
204                 if (NULL != data) {
205                         if (uid == data->vc->handle) {
206                                 pthread_mutex_unlock(&g_widget_list_mutex);
207                                 return true;
208                         }
209                 }
210         }
211
212         SLOG(LOG_DEBUG, TAG_VCW, "[DEBUG] Fail to get widget by vc");
213
214         pthread_mutex_unlock(&g_widget_list_mutex);
215         return false;
216 }
217
218 int vc_widget_client_get_handle(unsigned int uid, vc_h* vc)
219 {
220         vc_widget_s *data = NULL;
221
222         pthread_mutex_lock(&g_widget_list_mutex);
223         int count = g_slist_length(g_widget_list);
224         int i;
225
226         for (i = 0; i < count; i++) {
227                 data = g_slist_nth_data(g_widget_list, i);
228
229                 if (NULL != data) {
230                         if (uid == data->vc->handle) {
231                                 *vc = data->vc;
232                                 pthread_mutex_unlock(&g_widget_list_mutex);
233                                 return VC_ERROR_NONE;
234                         }
235                 }
236         }
237
238         pthread_mutex_unlock(&g_widget_list_mutex);
239         return VC_ERROR_INVALID_PARAMETER;
240 }
241
242 /* set/get callback function */
243 int vc_widget_client_set_result_cb(vc_h vc, vc_result_cb callback, void* user_data)
244 {
245         vc_widget_s* widget = widget_get(vc);
246
247         /* check handle */
248         if (NULL == widget)
249                 return VC_ERROR_INVALID_PARAMETER;
250
251         widget->result_cb = callback;
252         widget->result_user_data = user_data;
253
254         return VC_ERROR_NONE;
255 }
256
257 int vc_widget_client_get_result_cb(vc_h vc, vc_result_cb* callback, void** user_data)
258 {
259         vc_widget_s* widget = widget_get(vc);
260
261         /* check handle */
262         if (NULL == widget)
263                 return VC_ERROR_INVALID_PARAMETER;
264
265         *callback = widget->result_cb;
266         *user_data = widget->result_user_data;
267
268         return VC_ERROR_NONE;
269 }
270
271 int vc_widget_client_set_asr_result_enabled(vc_h vc, bool enabled)
272 {
273         vc_widget_s* widget = widget_get(vc);
274
275         /* check handle */
276         if (NULL == widget)
277                 return VC_ERROR_INVALID_PARAMETER;
278
279         widget->asr_result_enabled = enabled;
280
281         return VC_ERROR_NONE;
282 }
283
284 int vc_widget_client_get_asr_result_enabled(vc_h vc, bool* enabled)
285 {
286         vc_widget_s* widget = widget_get(vc);
287
288         /* check handle */
289         if (NULL == widget)
290                 return VC_ERROR_INVALID_PARAMETER;
291
292         *enabled = widget->asr_result_enabled;
293
294         return VC_ERROR_NONE;
295 }
296
297 int vc_widget_client_set_asr_result_cb(vc_h vc, vc_asr_result_cb callback, void* user_data)
298 {
299         vc_widget_s* widget = widget_get(vc);
300
301         /* check handle */
302         if (NULL == widget)
303                 return VC_ERROR_INVALID_PARAMETER;
304
305         widget->asr_result_cb = callback;
306         widget->asr_result_user_data = user_data;
307
308         return VC_ERROR_NONE;
309 }
310
311 int vc_widget_client_get_asr_result_cb(vc_h vc, vc_asr_result_cb* callback, void** user_data)
312 {
313         vc_widget_s* widget = widget_get(vc);
314
315         /* check handle */
316         if (NULL == widget)
317                 return VC_ERROR_INVALID_PARAMETER;
318
319         *callback = widget->asr_result_cb;
320         *user_data = widget->asr_result_user_data;
321
322         return VC_ERROR_NONE;
323 }
324
325 int vc_widget_client_set_service_state_changed_cb(vc_h vc, vc_service_state_changed_cb callback, void* user_data)
326 {
327         vc_widget_s* widget = widget_get(vc);
328
329         /* check handle */
330         if (NULL == widget)
331                 return VC_ERROR_INVALID_PARAMETER;
332
333         widget->service_state_changed_cb = callback;
334         widget->service_state_changed_user_data = user_data;
335
336         return VC_ERROR_NONE;
337 }
338
339 int vc_widget_client_get_service_state_changed_cb(vc_h vc, vc_service_state_changed_cb* callback, void** user_data)
340 {
341         vc_widget_s* widget = widget_get(vc);
342
343         /* check handle */
344         if (NULL == widget)
345                 return VC_ERROR_INVALID_PARAMETER;
346
347         *callback = widget->service_state_changed_cb;
348         *user_data = widget->service_state_changed_user_data;
349
350         return VC_ERROR_NONE;
351 }
352
353 int vc_widget_client_set_state_changed_cb(vc_h vc, vc_state_changed_cb callback, void* user_data)
354 {
355         vc_widget_s* widget = widget_get(vc);
356
357         /* check handle */
358         if (NULL == widget)
359                 return VC_ERROR_INVALID_PARAMETER;
360
361         widget->state_changed_cb = callback;
362         widget->state_changed_user_data = user_data;
363
364         return VC_ERROR_NONE;
365 }
366
367 int vc_widget_client_get_state_changed_cb(vc_h vc, vc_state_changed_cb* callback, void** user_data)
368 {
369         vc_widget_s* widget = widget_get(vc);
370
371         /* check handle */
372         if (NULL == widget)
373                 return VC_ERROR_INVALID_PARAMETER;
374
375         *callback = widget->state_changed_cb;
376         *user_data = widget->state_changed_user_data;
377
378         return VC_ERROR_NONE;
379 }
380
381 int vc_widget_client_set_show_tooltip_cb(vc_h vc, vc_widget_show_tooltip_cb callback, void* user_data)
382 {
383         vc_widget_s* widget = widget_get(vc);
384
385         /* check handle */
386         if (NULL == widget)
387                 return VC_ERROR_INVALID_PARAMETER;
388
389         widget->show_tooltip_cb = callback;
390         widget->show_tooltip_user_data = user_data;
391
392         return VC_ERROR_NONE;
393 }
394
395 int vc_widget_client_get_show_tooltip_cb(vc_h vc, vc_widget_show_tooltip_cb* callback, void** user_data)
396 {
397         vc_widget_s* widget = widget_get(vc);
398
399         /* check handle */
400         if (NULL == widget)
401                 return VC_ERROR_INVALID_PARAMETER;
402
403         *callback = widget->show_tooltip_cb;
404         *user_data = widget->show_tooltip_user_data;
405
406         return VC_ERROR_NONE;
407 }
408
409 int vc_widget_client_set_current_lang_changed_cb(vc_h vc, vc_current_language_changed_cb callback, void* user_data)
410 {
411         vc_widget_s* widget = widget_get(vc);
412
413         /* check handle */
414         if (NULL == widget)
415                 return VC_ERROR_INVALID_PARAMETER;
416
417         widget->current_lang_changed_cb = callback;
418         widget->current_lang_changed_user_data = user_data;
419
420         return VC_ERROR_NONE;
421 }
422
423 int vc_widget_client_get_current_lang_changed_cb(vc_h vc, vc_current_language_changed_cb* callback, void** user_data)
424 {
425         vc_widget_s* widget = widget_get(vc);
426
427         /* check handle */
428         if (NULL == widget)
429                 return VC_ERROR_INVALID_PARAMETER;
430
431         *callback = widget->current_lang_changed_cb;
432         *user_data = widget->current_lang_changed_user_data;
433
434         return VC_ERROR_NONE;
435 }
436
437 int vc_widget_client_set_error_cb(vc_h vc, vc_error_cb callback, void* user_data)
438 {
439         vc_widget_s* widget = widget_get(vc);
440
441         /* check handle */
442         if (NULL == widget)
443                 return VC_ERROR_INVALID_PARAMETER;
444
445         widget->error_cb = callback;
446         widget->error_user_data = user_data;
447
448         return VC_ERROR_NONE;
449 }
450
451 int vc_widget_client_get_error_cb(vc_h vc, vc_error_cb* callback, void** user_data)
452 {
453         vc_widget_s* widget = widget_get(vc);
454
455         /* check handle */
456         if (NULL == widget)
457                 return VC_ERROR_INVALID_PARAMETER;
458
459         *callback = widget->error_cb;
460         *user_data = widget->error_user_data;
461
462         return VC_ERROR_NONE;
463 }
464
465 int vc_widget_client_set_send_command_list_cb(vc_h vc, vc_widget_send_current_command_list_cb callback, void* user_data)
466 {
467         vc_widget_s* widget = widget_get(vc);
468
469         /* check handle */
470         if (NULL == widget)
471                 return VC_ERROR_INVALID_PARAMETER;
472
473         widget->send_command_list_cb = callback;
474         widget->send_command_list_user_data = user_data;
475
476         return VC_ERROR_NONE;
477 }
478
479 int vc_widget_client_get_send_command_list_cb(vc_h vc, vc_widget_send_current_command_list_cb* callback, void** user_data)
480 {
481         vc_widget_s* widget = widget_get(vc);
482
483         /* check handle */
484         if (NULL == widget)
485                 return VC_ERROR_INVALID_PARAMETER;
486
487         *callback = widget->send_command_list_cb;
488         *user_data = widget->send_command_list_user_data;
489
490         return VC_ERROR_NONE;
491 }
492
493
494 /* set/get option */
495 int vc_widget_client_set_service_state(vc_h vc, vc_service_state_e state)
496 {
497         vc_widget_s* widget = widget_get(vc);
498
499         /* check handle */
500         if (NULL == widget)
501                 return VC_ERROR_INVALID_PARAMETER;
502
503         widget->service_state = state;
504
505         return VC_ERROR_NONE;
506 }
507
508 int vc_widget_client_get_service_state(vc_h vc, vc_service_state_e* state)
509 {
510         vc_widget_s* widget = widget_get(vc);
511
512         /* check handle */
513         if (NULL == widget)
514                 return VC_ERROR_INVALID_PARAMETER;
515
516         *state = widget->service_state;
517
518         return VC_ERROR_NONE;
519 }
520
521
522 int vc_widget_client_set_state(vc_h vc, vc_state_e state)
523 {
524         vc_widget_s* widget = widget_get(vc);
525
526         /* check handle */
527         if (NULL == widget)
528                 return VC_ERROR_INVALID_PARAMETER;
529
530         widget->previous_state = widget->current_state;
531         widget->current_state = state;
532
533         return VC_ERROR_NONE;
534 }
535
536 int vc_widget_client_get_state(vc_h vc, vc_state_e* state)
537 {
538         vc_widget_s* widget = widget_get(vc);
539
540         /* check handle */
541         if (NULL == widget)
542                 return VC_ERROR_INVALID_PARAMETER;
543
544         *state = widget->current_state;
545
546         return VC_ERROR_NONE;
547 }
548
549 int vc_widget_client_get_state_by_uid(unsigned int uid, vc_state_e* state)
550 {
551         vc_widget_s *data = NULL;
552
553         pthread_mutex_lock(&g_widget_list_mutex);
554         int count = g_slist_length(g_widget_list);
555         int i;
556
557         for (i = 0; i < count; i++) {
558                 data = g_slist_nth_data(g_widget_list, i);
559
560                 if (NULL != data) {
561                         if (uid == data->vc->handle) {
562                                 *state = data->current_state;
563                                 pthread_mutex_unlock(&g_widget_list_mutex);
564                                 return VC_ERROR_NONE;
565                         }
566                 }
567         }
568
569         pthread_mutex_unlock(&g_widget_list_mutex);
570         return VC_ERROR_INVALID_PARAMETER;
571 }
572
573 int vc_widget_client_get_previous_state(vc_h vc, vc_state_e* state, vc_state_e* previous_state)
574 {
575         vc_widget_s* widget = widget_get(vc);
576
577         /* check handle */
578         if (NULL == widget)
579                 return VC_ERROR_INVALID_PARAMETER;
580
581         *previous_state = widget->previous_state;
582         *state = widget->current_state;
583
584         return VC_ERROR_NONE;
585 }
586
587 int vc_widget_client_set_xid(vc_h vc, int xid)
588 {
589         vc_widget_s* widget = widget_get(vc);
590
591         /* check handle */
592         if (NULL == widget)
593                 return VC_ERROR_INVALID_PARAMETER;
594
595         widget->xid = xid;
596
597         return VC_ERROR_NONE;
598 }
599
600 int vc_widget_cilent_get_xid(vc_h vc, int* xid)
601 {
602         vc_widget_s* widget = widget_get(vc);
603
604         /* check handle */
605         if (NULL == widget)
606                 return VC_ERROR_INVALID_PARAMETER;
607
608         *xid = widget->xid;
609
610         return VC_ERROR_NONE;
611 }
612
613 int vc_widget_client_set_error(vc_h vc, int reason)
614 {
615         vc_widget_s* widget = widget_get(vc);
616
617         /* check handle */
618         if (NULL == widget)
619                 return VC_ERROR_INVALID_PARAMETER;
620
621         widget->reason = reason;
622
623         return VC_ERROR_NONE;
624 }
625
626 int vc_widget_client_get_error(vc_h vc, int* reason)
627 {
628         vc_widget_s* widget = widget_get(vc);
629
630         /* check handle */
631         if (NULL == widget)
632                 return VC_ERROR_INVALID_PARAMETER;
633
634         *reason = widget->reason;
635
636         return VC_ERROR_NONE;
637 }
638
639 int vc_widget_client_set_show_tooltip(vc_h vc, bool show)
640 {
641         vc_widget_s* widget = widget_get(vc);
642
643         /* check handle */
644         if (NULL == widget)
645                 return VC_ERROR_INVALID_PARAMETER;
646
647         widget->show_tooltip = show;
648
649         return VC_ERROR_NONE;
650 }
651
652 int vc_widget_client_get_show_tooltip(vc_h vc, bool* show)
653 {
654         vc_widget_s* widget = widget_get(vc);
655
656         /* check handle */
657         if (NULL == widget)
658                 return VC_ERROR_INVALID_PARAMETER;
659
660         *show = widget->show_tooltip;
661
662         return VC_ERROR_NONE;
663 }
664
665 int vc_widget_client_get_count()
666 {
667         pthread_mutex_lock(&g_widget_list_mutex);
668         int ret = g_slist_length(g_widget_list);
669         pthread_mutex_unlock(&g_widget_list_mutex);
670         return ret;
671 }
672
673 int vc_widget_client_use_callback(vc_h vc)
674 {
675         vc_widget_s* widget = widget_get(vc);
676
677         /* check handle */
678         if (NULL == widget)
679                 return VC_ERROR_INVALID_PARAMETER;
680
681         widget->cb_ref_count++;
682         return VC_ERROR_NONE;
683 }
684
685 int vc_widget_client_not_use_callback(vc_h vc)
686 {
687         vc_widget_s* widget = widget_get(vc);
688
689         /* check handle */
690         if (NULL == widget)
691                 return VC_ERROR_INVALID_PARAMETER;
692
693         widget->cb_ref_count--;
694         return VC_ERROR_NONE;
695 }
696