Move header into proper file
[platform/core/api/gesture.git] / manager / gestured_invocation_manager.cpp
1 #include <stdlib.h>
2 #include <map>
3
4 #include <dlog.h>
5
6 #include "gestured_invocation_manager.h"
7
8 #ifdef LOG_TAG
9 #undef LOG_TAG
10 #endif
11 #define LOG_TAG "GESTURE_INVOCATION"
12
13 static const int MAX_INVOCATION_ID = 1000000;
14
15 static std::map<int, GDBusMethodInvocation*> g_invocation_map;
16 static int g_last_id = -1;
17
18 int gestureInvocationManagerInit()
19 {
20         LOGD("Initialize invocation manager");
21         g_last_id = -1;
22         g_invocation_map.clear();
23
24         return 0;
25 }
26
27 int gestureInvocationManagerAdd(GDBusMethodInvocation* invocation)
28 {
29         if (NULL == invocation) {
30                 LOGE("Invalid parameter");
31                 return -1;
32         }
33
34         LOGD("Add new invocation handle");
35         bool findId = false;
36         for (int i = g_last_id + 1; i != g_last_id; i = (i + 1) % MAX_INVOCATION_ID) {
37                 if (g_invocation_map.find(i) == g_invocation_map.end()) {
38                         g_last_id = i;
39                         findId = true;
40                         break;
41                 }
42         }
43
44         if (!findId) {
45                 LOGE("Invocation map is full");
46                 return -1;
47         }
48
49         g_invocation_map.insert({g_last_id, invocation});
50
51         return g_last_id;
52 }
53
54 GDBusMethodInvocation* gestureInvocationManagerGet(int invocation_id)
55 {
56         LOGD("Find invocation handle. ID(%d)", invocation_id);
57         auto iter = g_invocation_map.find(invocation_id);
58         if (iter == g_invocation_map.end()) {
59                 LOGE("There is no handle");
60                 return nullptr;
61         }
62
63         return iter->second;
64 }
65
66 bool gestureInvocationManagerRemove(int invocation_id)
67 {
68         LOGD("Remove invocation handle. ID(%d)", invocation_id);
69         g_invocation_map.erase(invocation_id);
70         return true;
71 }