/* Copyright (c) 2021, 2022, 2023 Dennis Wölfing
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/* kernel/include/dennix/kernel/console.h
 * System console.
 */

#ifndef KERNEL_CONSOLE_H
#define KERNEL_CONSOLE_H

#include <wchar.h>
#include <dennix/kernel/display.h>
#include <dennix/kernel/terminal.h>

#define MAX_PARAMS 16

class Console : public Terminal, public KeyboardListener {
public:
    Console();
    ~Console() = default;
    NOT_COPYABLE(Console);
    NOT_MOVABLE(Console);

    void lock();
    void unlock();
    void updateDisplaySize();
protected:
    bool getTtyPath(char* buffer, size_t size) override;
    void output(const char* buffer, size_t size);
private:
    void handleSequence(const char* sequence);
    void onKeyboardEvent(int key) override;
    void setGraphicsRendition();
    void printCharacter(char c);
    void printCharacterRaw(char c);
public:
    Reference<Display> display;
private:
    bool alternateBuffer;
    Color color;
    Color savedColor;
    Color alternateSavedColor;
    bool fgIsVgaColor;
    CharPos cursorPos;
    CharPos savedPos;
    CharPos alternateSavedPos;
    bool reversedColors;

    bool endOfLine;
    unsigned int params[MAX_PARAMS];
    bool paramSpecified[MAX_PARAMS];
    size_t paramIndex;
    mbstate_t ps;
    bool questionMarkModifier;
    int status;
};

extern Reference<Console> console;

#endif
