Add chage remote app
[apps/agl-service-windowmanager.git] / src / util.hpp
1 /*
2  * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
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 #ifndef WM_UTIL_HPP
18 #define WM_UTIL_HPP
19
20 #include <functional>
21 #include <thread>
22 #include <string>
23 #include <vector>
24 #include <unordered_map>
25 #include <sys/poll.h>
26 #include <string.h>
27
28 #define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
29
30 #define HMI_ERROR(args,...) _HMI_LOG(LOG_LEVEL_ERROR, __FILENAME__, __FUNCTION__, __LINE__,"wm",args, ##__VA_ARGS__)
31 #define HMI_WARNING(args,...) _HMI_LOG(LOG_LEVEL_WARNING, __FILENAME__, __FUNCTION__,__LINE__, "wm", args,##__VA_ARGS__)
32 #define HMI_NOTICE(args,...) _HMI_LOG(LOG_LEVEL_NOTICE, __FILENAME__, __FUNCTION__,__LINE__, "wm", args,##__VA_ARGS__)
33 #define HMI_INFO(args,...)  _HMI_LOG(LOG_LEVEL_INFO, __FILENAME__, __FUNCTION__,__LINE__, "wm", args,##__VA_ARGS__)
34 #define HMI_DEBUG(args,...) _HMI_LOG(LOG_LEVEL_DEBUG, __FILENAME__, __FUNCTION__,__LINE__, "wm", args,##__VA_ARGS__)
35
36 #define HMI_SEQ_ERROR(seq_num, args,...) _HMI_SEQ_LOG(LOG_LEVEL_ERROR, __FILENAME__, __FUNCTION__, __LINE__, seq_num, args, ##__VA_ARGS__)
37 #define HMI_SEQ_WARNING(seq_num, args,...) _HMI_SEQ_LOG(LOG_LEVEL_WARNING, __FILENAME__, __FUNCTION__, __LINE__, seq_num, args, ##__VA_ARGS__)
38 #define HMI_SEQ_NOTICE(seq_num, args,...) _HMI_SEQ_LOG(LOG_LEVEL_NOTICE, __FILENAME__, __FUNCTION__, __LINE__, seq_num, args, ##__VA_ARGS__)
39 #define HMI_SEQ_INFO(seq_num, args,...) _HMI_SEQ_LOG(LOG_LEVEL_INFO, __FILENAME__, __FUNCTION__, __LINE__, seq_num, args, ##__VA_ARGS__)
40 #define HMI_SEQ_DEBUG(seq_num, args,...) _HMI_SEQ_LOG(LOG_LEVEL_DEBUG, __FILENAME__, __FUNCTION__, __LINE__, seq_num, args, ##__VA_ARGS__)
41
42 #define DUMP(args, ...) _DUMP(LOG_LEVEL_INFO, args, ##__VA_ARGS__)
43
44 enum LOG_LEVEL{
45     LOG_LEVEL_NONE = 0,
46     LOG_LEVEL_ERROR,
47     LOG_LEVEL_WARNING,
48     LOG_LEVEL_NOTICE,
49     LOG_LEVEL_INFO,
50     LOG_LEVEL_DEBUG,
51     LOG_LEVEL_MAX = LOG_LEVEL_DEBUG
52 };
53
54 void _HMI_LOG(enum LOG_LEVEL level, const char* file, const char* func, const int line, const char* prefix, const char* log, ...);
55 void _HMI_SEQ_LOG(enum LOG_LEVEL level, const char* file, const char* func, const int line, unsigned seq_num, const char* log, ...);
56 void _DUMP(enum LOG_LEVEL level, const char *log, ...);
57
58 std::vector<std::string> parseString(std::string str, char delimiter);
59 std::string deleteSpace(std::string str);
60
61 struct rect
62 {
63     int32_t w, h;
64     int32_t x, y;
65 };
66
67 struct size
68 {
69     uint32_t w, h;
70 };
71
72 class rectangle
73 {
74   public:
75     explicit rectangle(long wd, long ht) : _right(wd - 1), _bottom(ht - 1) {};
76
77     void set_left(long l) {
78         _left = l;
79     }
80     long left() const { return _left; };
81
82     void set_right(long r) {
83         _right = r;
84     }
85     long right() const { return _right; };
86
87     void set_top(long t) {
88         _top = t;
89     }
90     long top() const { return _top; };
91
92     void set_bottom(long b) {
93         _bottom = b;
94     }
95     long bottom() const { return _bottom; }
96
97     long width() const {
98         if (is_valid())
99             return 0;
100         else
101             return _right - _left + 1;
102     }
103
104     long height() const {
105         if (is_valid())
106             return 0;
107         else
108             return _bottom - _top + 1;
109     }
110
111     void set_aspect(double ratio);
112     void fit(unsigned long to_width, unsigned long to_height);
113     void center(unsigned long outer_w, unsigned long outer_h);
114
115   private:
116     bool is_valid() const {
117         return (_top > _bottom || _left > _right);
118     }
119
120     long _left = 0;
121     long _top = 0;
122     long _right;
123     long _bottom;
124 };
125
126 typedef struct ChangeAreaReq {
127     std::string appname;
128     std::unordered_map<std::string, struct rect> area_req;
129     bool save;
130     std::unordered_map<std::string, std::string> update_app2area;
131     ChangeAreaReq() = default;
132     ~ChangeAreaReq() = default;
133     ChangeAreaReq(const ChangeAreaReq& val) = default;
134     void dump();
135 } ChangeAreaReq;
136 #endif // !WM_UTIL_HPP