Add APIs for multi-window app core
[platform/core/appfw/app-core.git] / src / multiwindow_base / appcore_multiwindow_base_window.c
1 /*
2  * Copyright (c) 2017 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 #include <stdio.h>
18 #include <stdarg.h>
19 #include <errno.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <malloc.h>
23 #include <stdbool.h>
24
25 #include <Ecore_Wayland.h>
26 #include <Elementary.h>
27 #include <glib-object.h>
28 #include <glib.h>
29 #include <gio/gio.h>
30
31 #include "appcore_base.h"
32 #include "appcore_multiwindow_base.h"
33 #include "appcore_multiwindow_base_private.h"
34
35 typedef struct _win_context {
36         appcore_multiwindow_base_instance_h inst;
37         int win_id;
38 } win_context;
39
40 static GList *__win_contexts;
41
42 static gint __comp(gconstpointer a, gconstpointer b)
43 {
44         const win_context *cxt = a;
45
46         if (!a || !b)
47                 return -1;
48
49         if (cxt->inst == b)
50                 return 0;
51
52         return -1;
53 }
54
55 static gint __comp_wid(gconstpointer a, gconstpointer b)
56 {
57         int wid = GPOINTER_TO_INT(b);
58         const win_context *cxt = a;
59
60         if (!a || !b)
61                 return -1;
62
63         if (cxt->win_id == wid)
64                 return 0;
65
66         return -1;
67 }
68
69 static win_context *__find_win_context(appcore_multiwindow_base_instance_h h)
70 {
71         GList *node = g_list_find_custom(__win_contexts, h, __comp);
72
73         if (!node)
74                 return NULL;
75
76         return node->data;
77 }
78
79 static win_context *__find_win_context_by_wid(int wid)
80 {
81         GList *node = g_list_find_custom(__win_contexts, GINT_TO_POINTER(wid), __comp_wid);
82
83         if (!node)
84                 return NULL;
85
86         return node->data;
87 }
88
89 EXPORT_API void appcore_multiwindow_base_window_on_show(int type, void *event)
90 {
91 }
92
93 EXPORT_API void appcore_multiwindow_base_window_on_hide(int type, void *event)
94 {
95         Ecore_Wl_Event_Window_Hide *ev = event;
96         win_context *cxt = __find_win_context_by_wid(ev->win);
97
98         if (!cxt)
99                 return;
100
101         __win_contexts = g_list_remove(__win_contexts, cxt);
102         free(cxt);
103 }
104
105 EXPORT_API void appcore_multiwindow_base_window_on_lower(int type, void *event)
106 {
107 }
108
109 EXPORT_API void appcore_multiwindow_base_window_on_visibility(int type, void *event)
110 {
111         Ecore_Wl_Event_Window_Visibility_Change *ev = event;
112         win_context *cxt = __find_win_context_by_wid(ev->win);
113
114         if (!cxt)
115                 return;
116
117         if (ev->fully_obscured)
118                 appcore_multiwindow_base_instance_pause(cxt->inst);
119         else
120                 appcore_multiwindow_base_instance_resume(cxt->inst);
121 }
122
123 EXPORT_API void appcore_multiwindow_base_window_bind(appcore_multiwindow_base_instance_h h, Evas_Object *win)
124 {
125         win_context *cxt;
126         Ecore_Wl_Window *wl_win;
127         int id;
128
129         cxt = __find_win_context(h);
130         if (cxt) {
131                 _ERR("This instance is already binded");
132                 return;
133         }
134
135         wl_win = elm_win_wl_window_get(win);
136         if (!wl_win) {
137                 _ERR("failed to get wayland window");
138                 return;
139         }
140
141         id = ecore_wl_window_id_get(wl_win);
142
143         cxt = malloc(sizeof(win_context));
144         cxt->win_id = id;
145         cxt->inst = h;
146         __win_contexts = g_list_append(__win_contexts, cxt);
147 }