[wfdsink] Change the way to wayland surface setting
[platform/core/multimedia/libmm-wfd.git] / src / mm_wfd_sink_attrs.c
1 /*
2  * libmm-wfd
3  *
4  * Copyright (c) 2011 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: JongHyuk Choi <jhchoi.choi@samsung.com>, ByungWook Jang <bw.jang@samsung.com>,
7  * Manoj Kumar K <manojkumar.k@samsung.com>, Hyunil Park <hyunil46.park@samsung.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  */
22
23 #include "mm_wfd_sink_attrs.h"
24 #include "mm_wfd_sink_dlog.h"
25 #include "mm_wfd_sink_priv.h"
26
27 typedef struct {
28         char *name;
29         int value_type;
30         int flags;           /* r, w */
31         void *default_value;
32         int valid_type;
33         int value_min;
34         int value_max;
35         mm_wfd_sink_commit_func_t attr_commit;
36 } MMWfdAttrsSpec;
37
38 static void __mm_wfd_sink_print_attrs(const char *attr_name, const MMAttrsValue * value, const char *prefix)
39 {
40         switch (value->type) {
41         case MM_ATTRS_TYPE_INT:
42                 wfd_sink_debug("[ %s : %s : %d ]", prefix, attr_name, value->value.i_val);
43                 break;
44         case MM_ATTRS_TYPE_DOUBLE:
45                 wfd_sink_debug("[ %s : %s : %f ]", prefix, attr_name, value->value.d_val);
46                 break;
47         case MM_ATTRS_TYPE_STRING:
48                 wfd_sink_debug("[ %s : %s : %s ]", prefix, attr_name, value->value.s_val);
49                 break;
50         case MM_ATTRS_TYPE_DATA:
51                 wfd_sink_debug("[ %s : %s : %p ]", prefix, attr_name, value->value.p_val);
52                 break;
53         default:
54                 break;
55         }
56
57         return;
58 }
59
60 static bool _mmwfd_set_display_info(MMHandleType handle, int attr_idx, const MMAttrsValue *value);
61
62 MMWfdAttrsSpec wfd_attrs[] = {
63         {
64                 (char *)"server_ip",
65                 MM_ATTRS_TYPE_STRING,
66                 MM_ATTRS_FLAG_RW,
67                 (void *)"127.0.0.1",
68                 MM_ATTRS_VALID_TYPE_NONE,
69                 0,
70                 0,
71                 NULL,
72         },
73         {
74                 (char *)"server_port",
75                 MM_ATTRS_TYPE_STRING,
76                 MM_ATTRS_FLAG_RW,
77                 (void *)"8554",
78                 MM_ATTRS_VALID_TYPE_NONE,
79                 0,
80                 0,
81                 NULL,
82         },
83         {
84                 (char *)"max_client_count",
85                 MM_ATTRS_TYPE_INT,
86                 MM_ATTRS_FLAG_RW,
87                 (void *)1,
88                 MM_ATTRS_VALID_TYPE_INT_RANGE,
89                 0,
90                 10,
91                 NULL,
92         },
93         /* Initialized with invalid native type, if a valid value is set then only this atribute will be considered */
94         {
95                 (char *)"native_resolution",
96                 MM_ATTRS_TYPE_INT,
97                 MM_ATTRS_FLAG_RW,
98                 (void *)0,
99                 MM_ATTRS_VALID_TYPE_INT_RANGE,
100                 0,
101                 3,
102                 NULL,
103         },
104         /* Initialized with invalid resolution, if a valid value is set then only this atribute will be considered */
105         {
106                 (char *)"prefered_resolutions",
107                 MM_ATTRS_TYPE_INT,
108                 MM_ATTRS_FLAG_RW,
109                 (void *)2147483647,
110                 MM_ATTRS_VALID_TYPE_INT_RANGE,
111                 0,
112                 2147483647,
113                 NULL,
114         },
115         /* Initialized with invalid uibc option, if a valid value is set then only this atribute will be considered */
116         {
117                 (char *)"set_hdcp",
118                 MM_ATTRS_TYPE_INT,
119                 MM_ATTRS_FLAG_RW,
120                 (void *)2,
121                 MM_ATTRS_VALID_TYPE_INT_RANGE,
122                 0,
123                 2,
124                 NULL,
125         },
126         {
127                 (char *)"display_rotate",
128                 MM_ATTRS_TYPE_INT,
129                 MM_ATTRS_FLAG_RW,
130                 (void *)0,
131                 MM_ATTRS_VALID_TYPE_INT_RANGE,
132                 0,
133                 3,
134                 NULL,
135         },
136         {
137                 (char *)"display_src_crop_x",
138                 MM_ATTRS_TYPE_INT,
139                 MM_ATTRS_FLAG_RW,
140                 (void *) 0,
141                 MM_ATTRS_VALID_TYPE_INT_RANGE,
142                 0,
143                 4096,
144                 NULL,
145         },
146         {
147                 (char *)"display_src_crop_y",
148                 MM_ATTRS_TYPE_INT,
149                 MM_ATTRS_FLAG_RW,
150                 (void *) 0,
151                 MM_ATTRS_VALID_TYPE_INT_RANGE,
152                 0,
153                 4096,
154                 NULL,
155         },
156         {
157                 (char *)"display_src_crop_width",
158                 MM_ATTRS_TYPE_INT,
159                 MM_ATTRS_FLAG_RW,
160                 (void *) 0,
161                 MM_ATTRS_VALID_TYPE_INT_RANGE,
162                 0,
163                 4096,
164                 NULL,
165         },
166         {
167                 (char *)"display_src_crop_height",
168                 MM_ATTRS_TYPE_INT,
169                 MM_ATTRS_FLAG_RW,
170                 (void *) 0,
171                 MM_ATTRS_VALID_TYPE_INT_RANGE,
172                 0,
173                 4096,
174                 NULL,
175         },
176         {
177                 (char *)"display_roi_x",
178                 MM_ATTRS_TYPE_INT,
179                 MM_ATTRS_FLAG_RW,
180                 (void *) 0,
181                 MM_ATTRS_VALID_TYPE_INT_RANGE,
182                 0,
183                 4096,
184                 NULL,
185         },
186         {
187                 (char *)"display_roi_y",
188                 MM_ATTRS_TYPE_INT,
189                 MM_ATTRS_FLAG_RW,
190                 (void *) 0,
191                 MM_ATTRS_VALID_TYPE_INT_RANGE,
192                 0,
193                 4096,
194                 NULL,
195         },
196         {
197                 (char *)"display_roi_width",
198                 MM_ATTRS_TYPE_INT,
199                 MM_ATTRS_FLAG_RW,
200                 (void *) 480,
201                 MM_ATTRS_VALID_TYPE_INT_RANGE,
202                 0,
203                 4096,
204                 NULL,
205         },
206         {
207                 (char *)"display_roi_height",
208                 MM_ATTRS_TYPE_INT,
209                 MM_ATTRS_FLAG_RW,
210                 (void *) 800,
211                 MM_ATTRS_VALID_TYPE_INT_RANGE,
212                 0,
213                 4096,
214                 NULL,
215         },
216         {
217                 (char *)"display_roi_mode",
218                 MM_ATTRS_TYPE_INT,
219                 MM_ATTRS_FLAG_RW,
220                 (void *) MM_DISPLAY_METHOD_CUSTOM_ROI_FULL_SCREEN,
221                 MM_ATTRS_VALID_TYPE_INT_RANGE,
222                 MM_DISPLAY_METHOD_CUSTOM_ROI_FULL_SCREEN,
223                 MM_DISPLAY_METHOD_CUSTOM_ROI_LETER_BOX,
224                 NULL,
225         },
226         {
227                 (char *)"display_rotation",
228                 MM_ATTRS_TYPE_INT,
229                 MM_ATTRS_FLAG_RW,
230                 (void *) MM_DISPLAY_ROTATION_NONE,
231                 MM_ATTRS_VALID_TYPE_INT_RANGE,
232                 MM_DISPLAY_ROTATION_NONE,
233                 MM_DISPLAY_ROTATION_270,
234                 NULL,
235         },
236         {
237                 (char *)"display_visible",
238                 MM_ATTRS_TYPE_INT,
239                 MM_ATTRS_FLAG_RW,
240                 (void *) FALSE,
241                 MM_ATTRS_VALID_TYPE_INT_RANGE,
242                 0,
243                 1,
244                 NULL,
245         },
246         {
247                 (char *)"display_method",
248                 MM_ATTRS_TYPE_INT,
249                 MM_ATTRS_FLAG_RW,
250                 (void *) MM_DISPLAY_METHOD_LETTER_BOX,
251                 MM_ATTRS_VALID_TYPE_INT_RANGE,
252                 MM_DISPLAY_METHOD_LETTER_BOX,
253                 MM_DISPLAY_METHOD_CUSTOM_ROI,
254                 NULL,
255         },
256         {
257                 (char *)"display_overlay",
258                 MM_ATTRS_TYPE_DATA,
259                 MM_ATTRS_FLAG_RW,
260                 (void *) NULL,
261                 MM_ATTRS_VALID_TYPE_NONE,
262                 0,
263                 0,
264                 _mmwfd_set_display_info,
265         },
266         {
267                 (char *)"display_overlay_user_data",
268                 MM_ATTRS_TYPE_DATA,
269                 MM_ATTRS_FLAG_RW,
270                 (void *) NULL,
271                 MM_ATTRS_VALID_TYPE_NONE,
272                 0,
273                 0,
274                 NULL,
275         },
276         {
277                 (char *)"display_zoom",
278                 MM_ATTRS_TYPE_DOUBLE,
279                 MM_ATTRS_FLAG_RW,
280                 (void *) 1,
281                 MM_ATTRS_VALID_TYPE_DOUBLE_RANGE,
282                 1.0,
283                 9.0,
284                 NULL,
285         },
286         {
287                 (char *)"display_surface_type",
288                 MM_ATTRS_TYPE_INT,
289                 MM_ATTRS_FLAG_RW,
290                 (void *) MM_DISPLAY_SURFACE_OVERLAY,
291                 MM_ATTRS_VALID_TYPE_INT_RANGE,
292                 MM_DISPLAY_SURFACE_OVERLAY,
293                 MM_DISPLAY_SURFACE_REMOTE,
294                 NULL,
295         },
296         {
297                 (char *)"display_width",   /* dest width of fimcconvert ouput */
298                 MM_ATTRS_TYPE_INT,
299                 MM_ATTRS_FLAG_RW,
300                 (void *) 0,
301                 MM_ATTRS_VALID_TYPE_INT_RANGE,
302                 0,
303                 4096,
304                 NULL,
305         },
306         {
307                 (char *)"display_height",   /* dest height of fimcconvert ouput */
308                 MM_ATTRS_TYPE_INT,
309                 MM_ATTRS_FLAG_RW,
310                 (void *) 0,
311                 MM_ATTRS_VALID_TYPE_INT_RANGE,
312                 0,
313                 4096,
314                 NULL,
315         },
316         {
317                 (char *)"display_evas_do_scaling",
318                 MM_ATTRS_TYPE_INT,
319                 MM_ATTRS_FLAG_RW,
320                 (void *) TRUE,
321                 MM_ATTRS_VALID_TYPE_INT_RANGE,
322                 FALSE,
323                 TRUE,
324                 NULL,
325         },
326         {
327                 (char *)"display_x",
328                 MM_ATTRS_TYPE_INT,
329                 MM_ATTRS_FLAG_RW,
330                 (void *) 0,
331                 MM_ATTRS_VALID_TYPE_INT_RANGE,
332                 0,
333                 4096,
334                 NULL,
335         },
336         {
337                 (char *)"display_y",
338                 MM_ATTRS_TYPE_INT,
339                 MM_ATTRS_FLAG_RW,
340                 (void *) 0,
341                 MM_ATTRS_VALID_TYPE_INT_RANGE,
342                 0,
343                 4096,
344                 NULL,
345         },
346         {
347                 (char *)"hdcp_version",
348                 MM_ATTRS_TYPE_INT,
349                 MM_ATTRS_FLAG_RW,
350                 (void *) 0,
351                 MM_ATTRS_VALID_TYPE_INT_RANGE,
352                 0,
353                 2,
354                 NULL,
355         },
356         {
357                 (char *)"hdcp_port",
358                 MM_ATTRS_TYPE_INT,
359                 MM_ATTRS_FLAG_RW,
360                 (void *) 0,
361                 MM_ATTRS_VALID_TYPE_INT_RANGE,
362                 0,
363                 2147483647,
364                 NULL,
365         },
366         {
367                 (char *)"hdcp_handle",
368                 MM_ATTRS_TYPE_DATA,
369                 MM_ATTRS_FLAG_RW,
370                 (void *) NULL,
371                 MM_ATTRS_VALID_TYPE_NONE,
372                 0,
373                 0,
374                 NULL,
375         },
376         {
377                 (char *)"window_width",
378                 MM_ATTRS_TYPE_INT,
379                 MM_ATTRS_FLAG_RW,
380                 (void *) 0,
381                 MM_ATTRS_VALID_TYPE_INT_RANGE,
382                 0,
383                 4096,
384                 NULL,
385         },
386         {
387                 (char *)"window_height",
388                 MM_ATTRS_TYPE_INT,
389                 MM_ATTRS_FLAG_RW,
390                 (void *) 0,
391                 MM_ATTRS_VALID_TYPE_INT_RANGE,
392                 0,
393                 4096,
394                 NULL,
395         },
396         {
397                 (char *)"resource_priority",
398                 MM_ATTRS_TYPE_INT,
399                 MM_ATTRS_FLAG_RW,
400                 (void *) 1000,
401                 MM_ATTRS_VALID_TYPE_INT_RANGE,
402                 1000,
403                 1001,
404                 NULL,
405         }
406 };
407
408 bool _mm_wfd_sink_commit_wfd_attrs(int attr_idx, const char *attr_name, const MMAttrsValue * value, void *commit_param)
409 {
410         bool ret = true;
411         return_val_if_fail(commit_param, FALSE);
412         return_val_if_fail(attr_idx >= 0, FALSE);
413         return_val_if_fail(attr_name, FALSE);
414         return_val_if_fail(value, FALSE);
415
416         if (wfd_attrs[attr_idx].attr_commit) {
417                 __mm_wfd_sink_print_attrs(attr_name, value, "Dynamic");
418                 ret = wfd_attrs[attr_idx].attr_commit((MMHandleType) commit_param, attr_idx, value);
419         } else {
420                 __mm_wfd_sink_print_attrs(attr_name, value, "Static");
421         }
422
423         return ret;
424 }
425
426 MMHandleType
427 _mmwfd_construct_attribute(MMHandleType handle)
428 {
429         int idx = 0;
430         MMHandleType attrs = NULL;
431         int num_of_attrs = 0;
432         MMAttrsConstructInfo *base = NULL;
433         int ret = MM_ERROR_NONE;
434
435         wfd_sink_debug_fenter();
436
437         return_val_if_fail(handle, (MMHandleType) NULL);
438
439         num_of_attrs = ARRAY_SIZE(wfd_attrs);
440
441         base = (MMAttrsConstructInfo *)malloc(num_of_attrs * sizeof(MMAttrsConstructInfo));
442
443         if (!base) {
444                 wfd_sink_error("Cannot create mmwfd attribute\n");
445                 goto ERROR;
446         }
447
448         /* initialize values of attributes */
449         for (idx = 0; idx < num_of_attrs; idx++) {
450                 base[idx].name = wfd_attrs[idx].name;
451                 base[idx].value_type = wfd_attrs[idx].value_type;
452                 base[idx].flags = wfd_attrs[idx].flags;
453                 base[idx].default_value = wfd_attrs[idx].default_value;
454         }
455
456         ret = mm_attrs_new(
457                                         base,
458                                         num_of_attrs,
459                                         "mmwfd_attrs",
460                                         _mm_wfd_sink_commit_wfd_attrs,
461                                         (void *)handle, &attrs);
462
463         if (base) {
464                 g_free(base);
465                 base = NULL;
466         }
467
468         if (ret != MM_ERROR_NONE) {
469                 wfd_sink_error("Cannot create mmwfd attribute\n");
470                 goto ERROR;
471         }
472
473         /* set validity type and range */
474         for (idx = 0; idx < num_of_attrs; idx++) {
475                 switch (wfd_attrs[idx].valid_type) {
476                 case MM_ATTRS_VALID_TYPE_INT_RANGE: {
477                                 mm_attrs_set_valid_type(attrs, idx, MM_ATTRS_VALID_TYPE_INT_RANGE);
478                                 mm_attrs_set_valid_range(attrs, idx,
479                                                                         wfd_attrs[idx].value_min,
480                                                                         wfd_attrs[idx].value_max,
481                                                                         (int)wfd_attrs[idx].default_value);
482                         }
483                         break;
484
485                 case MM_ATTRS_VALID_TYPE_INT_ARRAY:
486                 case MM_ATTRS_VALID_TYPE_DOUBLE_ARRAY:
487                 case MM_ATTRS_VALID_TYPE_DOUBLE_RANGE:
488                 default:
489                         break;
490                 }
491         }
492
493         wfd_sink_debug_fleave();
494
495         return attrs;
496
497 ERROR:
498         _mmwfd_deconstruct_attribute(attrs);
499
500         return (MMHandleType)NULL;
501 }
502
503 void
504 _mmwfd_deconstruct_attribute(MMHandleType handle)
505 {
506         wfd_sink_debug_fenter();
507
508         return_if_fail(handle);
509
510         if (handle)
511                 mm_attrs_free(handle);
512
513         wfd_sink_debug_fleave();
514 }
515
516 int
517 _mmwfd_get_attribute(MMHandleType handle,  char **err_attr_name, const char *attribute_name, va_list args_list)
518 {
519         int result = MM_ERROR_NONE;
520         MMHandleType attrs = 0;
521
522         wfd_sink_debug_fenter();
523
524         /* NOTE : Don't need to check err_attr_name because it can be set NULL */
525         /* if it's not want to know it. */
526         return_val_if_fail(attribute_name, MM_ERROR_COMMON_INVALID_ARGUMENT);
527         return_val_if_fail(handle, MM_ERROR_COMMON_INVALID_ARGUMENT);
528
529         attrs = handle;
530
531         return_val_if_fail(attrs, MM_ERROR_COMMON_INVALID_ARGUMENT);
532
533         result = mm_attrs_get_valist(attrs, err_attr_name, attribute_name, args_list);
534
535         if (result != MM_ERROR_NONE)
536                 wfd_sink_error("failed to get %s attribute\n", attribute_name);
537
538         wfd_sink_debug_fleave();
539
540         return result;
541 }
542
543 int
544 _mmwfd_set_attribute(MMHandleType handle,  char **err_attr_name, const char *attribute_name, va_list args_list)
545 {
546         int result = MM_ERROR_NONE;
547         MMHandleType attrs = 0;
548
549         wfd_sink_debug_fenter();
550
551         /* NOTE : Don't need to check err_attr_name because it can be set NULL */
552         /* if it's not want to know it. */
553         return_val_if_fail(attribute_name, MM_ERROR_COMMON_INVALID_ARGUMENT);
554         return_val_if_fail(handle, MM_ERROR_COMMON_INVALID_ARGUMENT);
555
556         attrs = handle;
557
558         return_val_if_fail(attrs, MM_ERROR_COMMON_INVALID_ARGUMENT);
559
560         /* set attributes and commit them */
561         result = mm_attrs_set_valist(attrs, err_attr_name, attribute_name, args_list);
562
563         if (result != MM_ERROR_NONE) {
564                 wfd_sink_error("failed to set %s attribute\n", attribute_name);
565                 return result;
566         }
567
568         /*__mmwfd_apply_attribute(handle, attribute_name); */
569
570         wfd_sink_debug_fleave();
571
572         return result;
573 }
574
575 int
576 _mmwfd_get_attributes_info(MMHandleType handle,  const char *attribute_name, MMWfdAttrsInfo *dst_info)
577 {
578         int result = MM_ERROR_NONE;
579         MMHandleType attrs = 0;
580         MMAttrsInfo src_info = {0, };
581
582         wfd_sink_debug_fenter();
583
584         return_val_if_fail(attribute_name, MM_ERROR_COMMON_INVALID_ARGUMENT);
585         return_val_if_fail(dst_info, MM_ERROR_COMMON_INVALID_ARGUMENT);
586         return_val_if_fail(handle, MM_ERROR_COMMON_INVALID_ARGUMENT);
587
588         attrs = handle;
589
590         return_val_if_fail(attrs, MM_ERROR_COMMON_INVALID_ARGUMENT);
591
592         result = mm_attrs_get_info_by_name(attrs, attribute_name, &src_info);
593
594         if (result != MM_ERROR_NONE) {
595                 wfd_sink_error("failed to get attribute info\n");
596                 return result;
597         }
598
599         memset(dst_info, 0x00, sizeof(MMWfdAttrsInfo));
600
601         dst_info->type = src_info.type;
602         dst_info->flag = src_info.flag;
603         dst_info->validity_type = src_info.validity_type;
604
605         switch (src_info.validity_type) {
606         case MM_ATTRS_VALID_TYPE_INT_ARRAY:
607                 dst_info->int_array.array = src_info.int_array.array;
608                 dst_info->int_array.count = src_info.int_array.count;
609                 dst_info->int_array.d_val = src_info.int_array.dval;
610                 break;
611
612         case MM_ATTRS_VALID_TYPE_INT_RANGE:
613                 dst_info->int_range.min = src_info.int_range.min;
614                 dst_info->int_range.max = src_info.int_range.max;
615                 dst_info->int_range.d_val = src_info.int_range.dval;
616                 break;
617
618         case MM_ATTRS_VALID_TYPE_DOUBLE_ARRAY:
619                 dst_info->double_array.array = src_info.double_array.array;
620                 dst_info->double_array.count = src_info.double_array.count;
621                 dst_info->double_array.d_val = src_info.double_array.dval;
622                 break;
623
624         case MM_ATTRS_VALID_TYPE_DOUBLE_RANGE:
625                 dst_info->double_range.min = src_info.double_range.min;
626                 dst_info->double_range.max = src_info.double_range.max;
627                 dst_info->double_range.d_val = src_info.double_range.dval;
628                 break;
629
630         default:
631                 break;
632         }
633
634         wfd_sink_debug_fleave();
635
636         return result;
637 }
638
639
640 static bool _mmwfd_set_display_info(MMHandleType handle, int attr_idx, const MMAttrsValue *value)
641 {
642         gint surface_type = MM_DISPLAY_SURFACE_OVERLAY;
643         mm_wfd_sink_t *wfd_sink = MMWFDSINK_CAST(handle);
644
645         wfd_sink_debug_fenter();
646
647         /* update display surface */
648         mm_attrs_get_int_by_name(MMWFDSINK_GET_ATTRS(wfd_sink), "display_surface_type", &surface_type);
649         wfd_sink_info("check display surface type attribute: %d", surface_type);
650
651         /* configuring display */
652         switch (surface_type) {
653         case MM_DISPLAY_SURFACE_EVAS:
654                 /* nothing to do */
655                 break;
656         case MM_DISPLAY_SURFACE_OVERLAY:
657                 if (MM_ERROR_NONE != _mm_wfd_sink_set_display_overlay(wfd_sink, value->value.p_val)) {
658                         wfd_sink_error("Setting overlay display is failed.");
659                         return false;
660                 }
661                 break;
662         case MM_DISPLAY_SURFACE_NULL:
663         default:
664                 wfd_sink_error("Not Supported Surface. surface_type: [%d]", surface_type);
665                 return false;
666         }
667
668         wfd_sink_debug_fleave();
669
670         return true;
671 }