upload tizen1.0 source
[platform/core/uifw/tts.git] / client / tts_client.c
1 /*
2 *  Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved 
3 *  Licensed under the Apache License, Version 2.0 (the "License");
4 *  you may not use this file except in compliance with the License.
5 *  You may obtain a copy of the License at
6 *  http://www.apache.org/licenses/LICENSE-2.0
7 *  Unless required by applicable law or agreed to in writing, software
8 *  distributed under the License is distributed on an "AS IS" BASIS,
9 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 *  See the License for the specific language governing permissions and
11 *  limitations under the License.
12 */
13
14
15 #include "tts_client.h"
16
17 /* Max number of handle */
18 static const int g_max_handle = 999;
19 /* allocated handle */
20 static int g_allocated_handle = 0;
21 /* client list */
22 static GList *g_client_list = NULL;
23
24 /* private functions */
25 static int __client_generate_uid(int pid)
26 {
27         g_allocated_handle++;
28
29         if (g_allocated_handle > g_max_handle) {
30                 g_allocated_handle = 1;
31         }
32
33         /* generate uid, handle number should be smaller than 1000 */
34         return pid * 1000 + g_allocated_handle;
35 }
36
37 int tts_client_new(tts_h* tts)
38 {
39         tts_client_s* client = NULL;
40         client = (tts_client_s*)g_malloc0 (sizeof(tts_client_s));
41
42         tts_h temp = (tts_h)g_malloc0(sizeof(tts_h));
43         temp->handle = __client_generate_uid(getpid()); 
44
45         /* initialize client data */
46         client->tts = temp;
47         client->pid = getpid(); 
48         client->uid = temp->handle;
49         client->current_utt_id = 0;
50
51         client->interrupted_cb = NULL;
52         client->interrupted_user_data = NULL;
53         
54         client->utt_started_cb = NULL;
55         client->utt_started_user_data = NULL;
56         client->utt_completeted_cb = NULL;
57         client->utt_completed_user_data = NULL;
58
59         client->error_cb = NULL;
60         client->error_user_data = NULL;
61
62         client->current_state = TTS_STATE_READY; 
63
64         client->cb_ref_count = 0;
65
66         g_client_list = g_list_append(g_client_list, client);
67
68         *tts = temp;
69
70         SLOG(LOG_DEBUG, TAG_TTSC, "[Success] Create client object : uid(%d)", client->uid); 
71
72         return 0;
73 }
74
75 int tts_client_destroy(tts_h tts)
76 {
77         if (tts == NULL) {
78                 SLOG(LOG_ERROR, TAG_TTSC, "Input parameter is NULL");
79                 return 0;
80         }       
81
82         GList *iter = NULL;
83         tts_client_s *data = NULL;
84
85         /* if list have item */
86         if (g_list_length(g_client_list) > 0) {
87                 /* Get a first item */
88                 iter = g_list_first(g_client_list);
89
90                 while (NULL != iter) {
91                         data = iter->data;
92                         if (tts->handle == data->tts->handle) {
93                                 g_client_list = g_list_remove_link(g_client_list, iter);
94
95                                 while (0 != data->cb_ref_count)
96                                 {
97                                         /* wait for release callback function */
98                                 }
99                                 free(data);
100                                 free(tts);
101
102                                 return 0;
103                         }
104
105                         /* Next item */
106                         iter = g_list_next(iter);
107                 }
108         }
109         SLOG(LOG_ERROR, TAG_TTSC, "Fail to destroy client : handle is not valid");
110
111         return -1;
112 }
113
114 tts_client_s* tts_client_get(tts_h tts)
115 {
116         if (tts == NULL) {
117                 SLOG(LOG_ERROR, TAG_TTSC, "Input parameter is NULL");
118                 return NULL;
119         }
120
121         GList *iter = NULL;
122         tts_client_s *data = NULL;
123
124         if (g_list_length(g_client_list) > 0) {
125                 /* Get a first item */
126                 iter = g_list_first(g_client_list);
127
128                 while (NULL != iter) {
129                         data = iter->data;
130
131                         if (tts->handle == data->tts->handle) 
132                                 return data;
133
134                         /* Next item */
135                         iter = g_list_next(iter);
136                 }
137         }
138
139         SLOG(LOG_ERROR, TAG_TTSC, "handle is not valid");
140
141         return NULL;
142 }
143
144 tts_client_s* tts_client_get_by_uid(const int uid)
145 {
146         if (uid < 0) {
147                 SLOG(LOG_ERROR, TAG_TTSC, "out of range : handle");
148                 return NULL;
149         }
150
151         GList *iter = NULL;
152         tts_client_s *data = NULL;
153
154         if (g_list_length(g_client_list) > 0) {
155                 /* Get a first item */
156                 iter = g_list_first(g_client_list);
157
158                 while (NULL != iter) {
159                         data = iter->data;
160                         if (uid == data->uid) {
161                                 return data;
162                         }
163
164                         /* Next item */
165                         iter = g_list_next(iter);
166                 }
167         }
168
169         SLOG(LOG_WARN, TAG_TTSC, "uid is not valid");
170
171         return NULL;
172 }
173
174 int tts_client_get_size()
175 {
176         return g_list_length(g_client_list);
177 }
178
179 int tts_client_use_callback(tts_client_s* client)
180 {
181         client->cb_ref_count++;
182         return 0;
183 }
184
185 int tts_client_not_use_callback(tts_client_s* client)
186 {
187         client->cb_ref_count--;
188         return 0;
189 }