1 /*
2  * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef INTERFACES_INNERKITS_COMMON_GRAPHIC_COMMON_H
17 #define INTERFACES_INNERKITS_COMMON_GRAPHIC_COMMON_H
18 
19 #ifdef __cplusplus
20 #include <cstdint>
21 #include <cstring>
22 #include <map>
23 #include <string>
24 #include <ostream>
25 #include <unistd.h>
26 
27 namespace OHOS {
28 #endif
29 
30 #include "graphic_common_c.h"
31 
32 #ifdef __cplusplus
33 
LowErrorStrSpecial(GSError err)34 inline std::string LowErrorStrSpecial(GSError err)
35 {
36     if (err == LOWERROR_INVALID) {
37         // int to string (in 1000)
38         char num[] = {
39             static_cast<char>(err / 0x64 % 0xa), static_cast<char>(err / 0xa % 0xa), static_cast<char>(err % 0xa), 0
40         };
41         return std::string("with low error <") + num + ">";
42     } else if (err == LOWERROR_FAILURE) {
43         return "with low error <failure>";
44     }
45     return "";
46 }
47 
48 #ifdef _WIN32
49 #define strerror_r(err, buf, len) strerror_s((buf), (len), (err))
50 #endif
51 
LowErrorStr(GSError lowerr)52 inline std::string LowErrorStr(GSError lowerr)
53 {
54     std::string lowError = LowErrorStrSpecial(lowerr);
55     if (lowError == "" && lowerr != 0) {
56         char buf[256] = {0}; // 256 mean buffer max length
57         strerror_r(lowerr, buf, sizeof buf);
58         lowError = std::string("with low error <") + buf + ">";
59     }
60     return lowError;
61 }
62 
GSErrorStr(GSError err)63 inline std::string GSErrorStr(GSError err)
64 {
65     GSError diff = static_cast<GSError>(err % LOWERROR_MAX);
66     std::string lowError = LowErrorStr(diff);
67     switch (static_cast<GSError>(err - diff)) {
68         case GSERROR_OK: return "<200 ok>" + lowError;
69         case GSERROR_INVALID_ARGUMENTS: return "<400 invalid arguments>" + lowError;
70         case GSERROR_NO_PERMISSION: return "<403 no permission>" + lowError;
71         case GSERROR_CONNOT_CONNECT_SAMGR: return "<404 connot connect to samgr>" + lowError;
72         case GSERROR_CONNOT_CONNECT_SERVER: return "<404 connot connect to server>" + lowError;
73         case GSERROR_CONNOT_CONNECT_WESTON: return "<404 connot connect to weston>" + lowError;
74         case GSERROR_NO_BUFFER: return "<406 no buffer>" + lowError;
75         case GSERROR_NO_ENTRY: return "<406 no entry>" + lowError;
76         case GSERROR_OUT_OF_RANGE: return "<406 out of range>" + lowError;
77         case GSERROR_NO_SCREEN: return "<406 no screen>" + lowError;
78         case GSERROR_INVALID_OPERATING: return "<412 invalid operating>" + lowError;
79         case GSERROR_NO_CONSUMER: return "<412 no consumer>" + lowError;
80         case GSERROR_NOT_INIT: return "<412 not init>" + lowError;
81         case GSERROR_TYPE_ERROR: return "<412 type error>" + lowError;
82         case GSERROR_API_FAILED: return "<500 api call failed>" + lowError;
83         case GSERROR_INTERNAL: return "<500 internal error>" + lowError;
84         case GSERROR_NO_MEM: return "<500 no memory>" + lowError;
85         case GSERROR_PROXY_NOT_INCLUDE: return "<500 proxy not include>" + lowError;
86         case GSERROR_SERVER_ERROR: return "<500 server occur error>" + lowError;
87         case GSERROR_ANIMATION_RUNNING: return "<500 animation is running>" + lowError;
88         case GSERROR_NOT_IMPLEMENT: return "<501 not implement>" + lowError;
89         case GSERROR_NOT_SUPPORT: return "<501 not support>" + lowError;
90         case GSERROR_BINDER: return "<504 binder error>" + lowError;
91         default: return "<GSError error index out of range>";
92     }
93     return "<GSError error index out of range>";
94 }
95 
SurfaceErrorStr(GSError err)96 inline std::string SurfaceErrorStr(GSError err)
97 {
98     return GSErrorStr(err);
99 }
100 
operator <<(std::ostream &os, const GSError &err)101 inline std::ostream &operator <<(std::ostream &os, const GSError &err)
102 {
103     os << GSErrorStr(err);
104     return os;
105 }
106 
operator ==(GSError a, GSError b)107 inline bool operator ==(GSError a, GSError b)
108 {
109     return static_cast<int32_t>(a) / LOWERROR_MAX == static_cast<int32_t>(b) / LOWERROR_MAX;
110 }
111 
operator !=(GSError a, GSError b)112 inline bool operator !=(GSError a, GSError b)
113 {
114     return static_cast<int32_t>(a) / LOWERROR_MAX != static_cast<int32_t>(b) / LOWERROR_MAX;
115 }
116 
117 using WMError = GSError;
118 using SurfaceError = GSError;
119 using VsyncError = GSError;
120 } // namespace OHOS
121 #endif // __cplusplus
122 
123 #endif // INTERFACES_INNERKITS_COMMON_GRAPHIC_COMMON_H
124