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