Modified flushing memory logic
[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_Wl2.h>
26 #include <glib-object.h>
27 #include <glib.h>
28 #include <gio/gio.h>
29
30 #include "appcore_base.h"
31 #include "appcore_multiwindow_base.h"
32 #include "appcore_multiwindow_base_private.h"
33
34 typedef struct _win_context {
35         appcore_multiwindow_base_instance_h inst;
36         int win_id;
37 } win_context;
38
39 static GList *__win_contexts;
40
41 static gint __comp(gconstpointer a, gconstpointer b)
42 {
43         const win_context *cxt = a;
44
45         if (!a || !b)
46                 return -1;
47
48         if (cxt->inst == b)
49                 return 0;
50
51         return -1;
52 }
53
54 static gint __comp_wid(gconstpointer a, gconstpointer b)
55 {
56         int wid = GPOINTER_TO_INT(b);
57         const win_context *cxt = a;
58
59         if (!a || !b)
60                 return -1;
61
62         if (cxt->win_id == wid)
63                 return 0;
64
65         return -1;
66 }
67
68 static win_context *__find_win_context(appcore_multiwindow_base_instance_h h)
69 {
70         GList *node = g_list_find_custom(__win_contexts, h, __comp);
71
72         if (!node)
73                 return NULL;
74
75         return node->data;
76 }
77
78 static win_context *__find_win_context_by_wid(int wid)
79 {
80         GList *node = g_list_find_custom(__win_contexts, GINT_TO_POINTER(wid), __comp_wid);
81
82         if (!node)
83                 return NULL;
84
85         return node->data;
86 }
87
88 EXPORT_API bool appcore_multiwindow_base_window_is_resumed(void)
89 {
90         win_context *ctx;
91         GList *iter;
92
93         iter = __win_contexts;
94         while (iter) {
95                 ctx = (win_context *)iter->data;
96                 if (appcore_multiwindow_base_instance_is_resumed(ctx->inst))
97                         return true;
98                 iter = g_list_next(iter);
99         }
100
101         return false;
102 }
103
104 EXPORT_API void appcore_multiwindow_base_window_on_show(int type, void *event)
105 {
106 }
107
108 EXPORT_API void appcore_multiwindow_base_window_on_hide(int type, void *event)
109 {
110         Ecore_Wl2_Event_Window_Hide *ev = event;
111         win_context *cxt = __find_win_context_by_wid(ev->win);
112
113         if (!cxt)
114                 return;
115
116         __win_contexts = g_list_remove(__win_contexts, cxt);
117         free(cxt);
118 }
119
120 EXPORT_API void appcore_multiwindow_base_window_on_lower(int type, void *event)
121 {
122 }
123
124 EXPORT_API void appcore_multiwindow_base_window_on_visibility(int type, void *event)
125 {
126         Ecore_Wl2_Event_Window_Visibility_Change *ev = event;
127         win_context *cxt = __find_win_context_by_wid(ev->win);
128
129         if (!cxt)
130                 return;
131
132         if (ev->fully_obscured)
133                 appcore_multiwindow_base_instance_pause(cxt->inst);
134         else
135                 appcore_multiwindow_base_instance_resume(cxt->inst);
136 }
137
138 EXPORT_API void appcore_multiwindow_base_window_on_pre_visibility(int type, void *event)
139 {
140         Ecore_Wl2_Event_Window_Pre_Visibility_Change *ev = event;
141         win_context *cxt = __find_win_context_by_wid(ev->win);
142
143         if (!cxt)
144                 return;
145
146         if (ev->type == ECORE_WL2_WINDOW_VISIBILITY_TYPE_PRE_UNOBSCURED)
147                 appcore_multiwindow_base_instance_resume(cxt->inst);
148 }
149
150 EXPORT_API void appcore_multiwindow_base_window_bind(appcore_multiwindow_base_instance_h h, Ecore_Wl2_Window *wl_win)
151 {
152         win_context *cxt;
153         int id;
154
155         if (!wl_win) {
156                 _ERR("Invalid parameter");
157                 return;
158         }
159
160         cxt = __find_win_context(h);
161         if (cxt) {
162                 _ERR("This instance is already binded");
163                 return;
164         }
165
166         id = ecore_wl2_window_id_get(wl_win);
167
168         cxt = malloc(sizeof(win_context));
169         if (cxt == NULL) {
170                 _ERR("Out of memory");
171                 return;
172         }
173
174         cxt->win_id = id;
175         cxt->inst = h;
176         __win_contexts = g_list_append(__win_contexts, cxt);
177 }
178
179 EXPORT_API void appcore_multiwindow_base_window_unbind(appcore_multiwindow_base_instance_h h)
180 {
181         win_context *cxt;
182
183         cxt = __find_win_context(h);
184         if (!cxt)
185                 return;
186
187         __win_contexts = g_list_remove(__win_contexts, cxt);
188         free(cxt);
189 }
190