1. Changed license year
[apps/home/mobileprint.git] / mobileprint / smsc / lib / smsc_position.c
1 /*
2 *  Mobileprint
3 *
4 * Copyright 2013  Samsung Electronics Co., Ltd
5
6 * Licensed under the Flora License, Version 1.1 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9
10 * http://floralicense.org/license/
11
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19
20 #include <math.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include <Ecore.h>
25
26 #include <preview_coords.h>
27
28 #include "smsc_debug.h"
29
30 #include "smsc_position.h"
31
32
33 int init_smsc_position(struct smsc_position *position)
34 {
35         SMSC_DEBUG("init_smsc_position() started");
36
37         memset(position, 0, sizeof(struct smsc_position));
38
39         /* event of position update */
40         position->position_update_event_type = ecore_event_type_new();
41
42         SMSC_DEBUG("init_smsc_position() finished");
43
44         return 0;
45 }
46
47
48 int set_smsc_position_settings(struct smsc_position *position, int pages_count,
49                 const struct size_px *available_size_px,
50                 const struct size_pts *page_size)
51 {
52         /* calculate page size in pixels */
53         struct size_pts min_area_size;
54         struct size_px min_area_size_px;
55         //int vis_pages = 3;
56
57         position->is_configured = 1;
58         position->pages_count = pages_count;
59         position->available_size = *available_size_px;
60         position->offset = 0;
61
62         min_area_size.x = page_size->x
63                 * (1 + PAGE_SIDE_SHOW_COEFF * 2 + MIN_PAGE_SPACE_COEFF * 2);
64         min_area_size.y = page_size->y;
65
66         if (pts_size2px(&min_area_size, available_size_px,
67                         &min_area_size_px) < 0) {
68                 //PGEN_DEBUG("ERROR: full_size: pts_size2px()\n");
69                 return -1;
70         }
71
72         position->page_size_px.x = min_area_size_px.x
73                 / (1 + PAGE_SIDE_SHOW_COEFF * 2 + MIN_PAGE_SPACE_COEFF * 2);
74         position->page_size_px.y = min_area_size_px.y;
75         SMSC_DEBUG("init_smsc_position(): page_size_px: (%d, %d)",
76                         position->page_size_px.x, position->page_size_px.y);
77
78         return 0;
79 }
80
81
82 int clear_smsc_position_settings(struct smsc_position *position)
83 {
84         position->is_configured = 0;
85         position->pages_count = -1;
86         return 0;
87 }
88
89
90 int get_smsc_position_all_width(const struct smsc_position *position)
91 {
92         if (position->is_configured == 0)
93                 return -1;
94         if (1 == position->pages_count)
95                 return 0;
96         return position->page_size_px.x * (position->pages_count - 1)
97                 * (1 + MIN_PAGE_SPACE_COEFF);
98 }
99
100
101 int get_smsc_position_cur_page(const struct smsc_position *position)
102 {
103         float res;
104
105         SMSC_RETV_IF(NULL == position, -1, "Argument error");
106         SMSC_RETV_IF(!position->is_configured, -1, "Position not configured");
107
108         /*SMSC_DEBUG("position->offset = %d", position->offset);
109         SMSC_DEBUG("position->page_size_px.x = %d", position->page_size_px.x);
110         SMSC_DEBUG("MIN_PAGE_SPACE_COEFF = %f", MIN_PAGE_SPACE_COEFF);
111         SMSC_DEBUG("position->pages_count = %d", position->pages_count);*/
112         res = (position->offset + (position->page_size_px.x / 2)
113                         * (1 + MIN_PAGE_SPACE_COEFF))
114                 / (position->page_size_px.x * (1 + MIN_PAGE_SPACE_COEFF));
115         //SMSC_DEBUG("res = %f", res);
116         if (res >= position->pages_count)
117                 return position->pages_count - 1;
118         else if (res < 0)
119                 return 0;
120
121         return res;
122 }
123
124 int get_smsc_position_max_vispages_count(const struct smsc_position *position)
125 {
126         int all_sizex;
127         int page_sizex;
128         int page_count;
129
130         if (NULL == position)
131                 return -1;
132
133         page_sizex = position->page_size_px.x;
134         all_sizex = position->available_size.x;
135         page_count = ceil(all_sizex * (1 + MIN_PAGE_SPACE_COEFF)
136                         / (page_sizex * (1 + MIN_PAGE_SPACE_COEFF)));
137         //SMSC_DEBUG("page_count = %d", page_count);
138         if (page_count > 0) {
139                 /* provide odd page_count to have central page */
140                 page_count = page_count + 1 - page_count % 2;
141                 //SMSC_DEBUG("corrected page_count = %d", page_count);
142                 return page_count;
143         }
144         return 1;
145 }
146
147 int *get_smsc_position_visible_pages(const struct smsc_position *position)
148 {
149         int *res;
150         int cur_page_i;
151         int cur_page;
152         int max_vispage_count;
153         int vispages_right;
154         int vispages_left;
155         int vispages;
156         int middle_page;
157         int i;
158
159         if (position->is_configured == 0)
160                 return NULL;
161
162         if (1 == position->pages_count) {
163                 res = malloc(sizeof(int) * (1 + 1));
164                 res[0] = 0;
165                 res[1] = -1;
166                 return res;
167         }
168
169         cur_page_i = get_smsc_position_cur_page(position);
170         if (cur_page_i < 0)
171                 return NULL;
172         cur_page = cur_page_i + 1;
173         max_vispage_count = get_smsc_position_max_vispages_count(position);
174         if (max_vispage_count < 1)
175                 return NULL;
176
177         middle_page = 1 + max_vispage_count / 2;
178         vispages_left = cur_page - 1;
179         if (vispages_left > middle_page - 1)
180                 vispages_left = middle_page - 1;
181         vispages_right = position->pages_count - cur_page;
182         if (vispages_right > middle_page - 1)
183                 vispages_right = middle_page - 1;
184         vispages = vispages_right + vispages_left + 1;
185
186         res = malloc(sizeof(int) * (vispages + 1));
187         if (NULL == res)
188                 return NULL;
189         /*SMSC_DEBUG("vispages = %d, vispages_left = %d, vispages_right = %d, "
190                         "middle_page = %d",
191                         vispages, vispages_left, vispages_right, middle_page);*/
192
193         for (i = 0; i < vispages; ++i)
194                 res[i] = cur_page + (i + 1 - (vispages_left + 1)) - 1;
195         res[vispages] = -1;
196
197         return res;
198 }
199
200
201 int get_smsc_position_page(const struct smsc_position *position,
202                 int page, int *is_ok)
203 {
204         int res;
205
206         SMSC_RETV_IF(NULL == position || NULL == is_ok, -1, "Argument error");
207
208         *is_ok = 1;
209         if (position->is_configured == 0) {
210                 *is_ok = 0;
211                 return 0;
212         }
213         if (NULL == position || page < 0 || page >= position->pages_count) {
214                 /*SMSC_DEBUG("ERROR in get_smsc_position_page(), page = %d,"
215                                 " pages_count = %d",
216                                 page, position->pages_count);*/
217                 *is_ok = 0;
218                 return 0;
219         }
220         res = position->available_size.x / 2 - position->offset
221                 + page * position->page_size_px.x
222                 * (1 + MIN_PAGE_SPACE_COEFF)
223                 - position->page_size_px.x / 2;
224         //SMSC_DEBUG("get_smsc_position_page(): page %d: res = %d", page, res);
225         return res;
226 }
227
228
229 int get_smsc_position_cur_page_center_xdiff(
230                 const struct smsc_position *position, int *is_ok)
231 {
232         int page;
233         int res;
234         SMSC_RETV_IF(NULL == is_ok, -1, "Argument error");
235         *is_ok = 0;
236         SMSC_RETV_IF(NULL == position, -1, "Argument error");
237         *is_ok = 1;
238         page = get_smsc_position_cur_page(position);
239         SMSC_RETV_IF(page < 0, -1, "Error in current page calculation");
240         res = page * (position->page_size_px.x * (1 + MIN_PAGE_SPACE_COEFF))
241                         - position->offset;
242         return res;
243 }
244
245
246 int move_smsc_position(struct smsc_position *position, int xdiff)
247 {
248         struct smsc_position_event *event_data;
249         int all_width;
250
251         SMSC_RETV_IF(NULL == position, -1, "Argument error");
252         SMSC_RETV_IF(position->is_configured == 0, -1, "Not configured");
253
254         all_width = get_smsc_position_all_width(position);
255         SMSC_RETV_IF(all_width < 0, -1, "Width calculation error");
256         //SMSC_DEBUG("move_smsc_position() started");
257         if (xdiff + position->offset >= all_width)
258                 position->offset = all_width;
259         else if (xdiff + position->offset <= 0)
260                 position->offset = 0;
261         else
262                 position->offset = position->offset + xdiff;
263
264         /* emit ecore event */
265         event_data = malloc(sizeof(struct smsc_position_event));
266         memset(event_data, 0, sizeof(struct smsc_position_event));
267         ecore_event_add(position->position_update_event_type,
268                         event_data, NULL, NULL);
269
270         return 0;
271 }
272
273
274 int set_smsc_position(struct smsc_position *position, int offset)
275 {
276         struct smsc_position_event *event_data;
277         int all_width;
278
279         SMSC_RETV_IF(NULL == position || offset < 0, -1, "Argument error");
280
281         all_width = get_smsc_position_all_width(position);
282         SMSC_RETV_IF(all_width < 0, -1, "Width calculation error");
283
284         if (offset >= all_width)
285                 position->offset = all_width;
286         else
287                 position->offset = offset;
288
289         /* emit ecore event */
290         event_data = malloc(sizeof(struct smsc_position_event));
291         memset(event_data, 0, sizeof(struct smsc_position_event));
292         ecore_event_add(position->position_update_event_type,
293                         event_data, NULL, NULL);
294
295         return 0;
296 }
297