modes.hh 6.72 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
/*
 * Copyright © 2018 Christian Persch
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#pragma once

#include <cstdint>

namespace vte {
namespace terminal {
namespace modes {

#define VTE_MODES_MASK(shift) (1U << (shift))

#define MODE_ACCESSOR(name) \
        inline void set_##name(bool value) noexcept \
        { \
                set(e##name, value); \
        } \
        \
        inline constexpr bool name() const noexcept \
        { \
                return get(e##name); \
        }

39 40 41 42 43 44
#define MODE_FIXED_ACCESSOR(name,value) \
        inline constexpr bool name() const noexcept \
        { \
                return value; \
        }

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
template <typename T>
static inline void vte_modes_set_bool(T* modes,
                                      unsigned int shift,
                                      bool value)
{
        if (value)
                *modes |= T(1U) << shift;
        else
                *modes &= ~(T(1U) << shift);
}

template <typename T>
static constexpr inline bool vte_modes_get_bool(T modes,
                                                unsigned int shift)
{
        return (modes >> shift) & 1U;
}

template <typename T>
static constexpr inline bool vte_modes_unset_bool(T* modes,
                                                  unsigned int shift)
{
        bool set = vte_modes_get_bool<T>(*modes, shift);
        vte_modes_set_bool<T>(modes, shift, false);
        return set;
}

template <typename T>
class Base
{
public:
        using Self = Base<T>;
        using Storage = T;

        constexpr Base(std::initializer_list<int> modes)
        {
                for (auto i : modes)
                        m_default_modes |= VTE_MODES_MASK(i);
                m_modes = m_default_modes;
        }

        ~Base() = default;
        Base(Self const&) = default;
        Base(Self&&) = default;
        Self& operator= (Self const&) = default;
        Self& operator= (Self&&) = default;

        inline void set(int bit,
                        bool value) noexcept
        {
                vte_modes_set_bool<Storage>(&m_modes, bit, value);
        }

        constexpr inline bool get(int bit) const noexcept
        {
                return vte_modes_get_bool<Storage>(m_modes, bit);
        }

        inline void set_modes(Storage value) noexcept
        {
                m_modes = value;
        }

        constexpr inline Storage get_modes() const noexcept
        {
                return m_modes;
        }

        void reset() noexcept
        {
                set_modes(m_default_modes);
        }

private:
        T m_modes{0};
        T m_default_modes{0};
};

class ECMA : public Base<uint8_t>
{
public:
        enum Modes {
                eUNKNOWN      = -3,
                eALWAYS_SET   = -2,
                eALWAYS_RESET = -1,

#define MODE(name,param) e##name,
#define MODE_FIXED(name,param,value) e##name,
#include "modes-ecma.hh"
#undef MODE
#undef MODE_FIXED
        };

        int mode_from_param(int param) const noexcept
        {
                switch (param) {
#define MODE(name,param) case param: return e##name;
#define MODE_FIXED(name,param,value) case param: return e##value;
#include "modes-ecma.hh"
#undef MODE
#undef MODE_FIXED
                default:
                        return eUNKNOWN;
                }
        }

        char const* mode_to_cstring(int param) const noexcept
        {
                switch (param) {
                case eUNKNOWN: return "UNKNOWN";
                case eALWAYS_SET: return "ALWAYS_SET";
                case eALWAYS_RESET: return "ALWAYS_RESET";
#define MODE(name,param) case e##name: return #name;
#define MODE_FIXED(name,param,value)
#include "modes-ecma.hh"
#undef MODE
#undef MODE_FIXED
                default:
                        return "INVALID";
                }
        }

#define MODE(name,param) MODE_ACCESSOR(name)
168
#define MODE_FIXED(name,param,value) MODE_FIXED_ACCESSOR(name, e##value == eALWAYS_SET)
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
#include "modes-ecma.hh"
#undef MODE
#undef MODE_FIXED

        constexpr ECMA() : Self{eSRM} { }

}; // class ECMA

class Private : public Base<uint32_t>
{
public:
        enum Modes {
                eUNKNOWN      = -3,
                eALWAYS_SET   = -2,
                eALWAYS_RESET = -1,

#define MODE(name,param) e##name,
#define MODE_FIXED(name,param,value) e##name,
#include "modes-private.hh"
#undef MODE
#undef MODE_FIXED
        };

        int mode_from_param(int param) const noexcept
        {
                switch (param) {
#define MODE(name,param) case param: return e##name;
#define MODE_FIXED(name,param,value) case param: return e##value;
#include "modes-private.hh"
#undef MODE
#undef MODE_FIXED
                default:
                        return eUNKNOWN;
                }
        }

        char const* mode_to_cstring(int param) const noexcept
        {
                switch (param) {
                case eUNKNOWN: return "UNKNOWN";
                case eALWAYS_SET: return "ALWAYS_SET";
                case eALWAYS_RESET: return "ALWAYS_RESET";
#define MODE(name,param) case e##name: return #name;
#define MODE_FIXED(name,param,value)
#include "modes-private.hh"
#undef MODE
#undef MODE_FIXED
                default:
                        return "INVALID";
                }
        }

#define MODE(name,param) MODE_ACCESSOR(name)
222
#define MODE_FIXED(name,param,value) MODE_FIXED_ACCESSOR(name, e##value == eALWAYS_SET)
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
#include "modes-private.hh"
#undef MODE
#undef MODE_FIXED

        constexpr Private() : Self{eDEC_AUTOWRAP,
                                   eDEC_TEXT_CURSOR,
                                   eXTERM_ALTBUF_SCROLL,
                                   eXTERM_META_SENDS_ESCAPE} { }

        inline void push_saved(int mode)
        {
                vte_modes_set_bool<Storage>(&m_saved_modes, mode, get(mode));
        }

        constexpr inline bool pop_saved(int mode)
        {
                return vte_modes_unset_bool<Storage>(&m_saved_modes, mode);
        }

        inline void clear_saved()
        {
                m_saved_modes = 0;
        }

private:
        Storage m_saved_modes{0};

}; // class Private

#undef MODE_ACCESSOR
253
#undef MODE_FIXED_ACCESSOR
254 255 256 257

} // namespace modes
} // namespace terminal
} // namespace vte