#pragma once #include "include/cef_browser.h" #include #include #include class BrowserClient; // Implemented by TabManager. Lets the input loop drive tab operations without // knowing about CEF internals. class TabController { public: virtual ~TabController() {} virtual void tabNew() {} virtual void tabClose() {} virtual void tabNext() {} virtual void tabPrev() {} }; class InputHandler { public: enum InputMode { MODE_STANDARD, // h/j/k/l for arrows, shortcuts without Ctrl MODE_MOUSE, // hjkl for mouse movement, q/f for clicks MODE_INSERT, // All keys pass through to CEF MODE_VISUAL // Text selection mode }; InputHandler(CefRefPtr browser, int term_width, int term_height, int cell_width, int cell_height, int pixel_width, int pixel_height); ~InputHandler(); void start(); void stop(); void releaseBrowser() { browser_ = nullptr; } void setBrowserClient(BrowserClient *client) { browser_client_ = client; } void setTabController(TabController *t) { tab_controller_ = t; } void updateBrowser(CefRefPtr b) { browser_ = b; } void setTiledRenderingEnabled(bool enabled) { tiled_rendering_enabled_ = enabled; } void setZoomLevel(double level) { current_zoom_level_ = level; } double getZoomLevel() const { return current_zoom_level_; } InputMode getCurrentMode() const { return current_mode_; } const char *getModeName() const; bool IsSearchActive() const { return search_input_active_; } std::string GetSearchQuery() const { return search_input_buffer_; } void updateDimensions(int cols, int rows, int cell_w, int cell_h, int pixel_w, int pixel_h); private: void enableMouseTracking(); void disableMouseTracking(); void readLoop(); void parseMouseEvent(const char *seq, int len); void parseKeySequence(const char *seq, int len); void clearCacheAndReload(); void sendKeyEvent(int key_code, char character, bool is_char_event, bool shift_pressed = false); int readUTF8Char(unsigned char first_byte, std::string &utf8_char); void sendUTF8CharEvent(const std::string &utf8_char); void removeLastUTF8Char(std::string &str); CefRefPtr browser_; TabController *tab_controller_ = nullptr; int term_width_; int term_height_; int cell_width_; int cell_height_; int pixel_width_; int pixel_height_; std::thread reader_thread_; std::atomic running_; struct termios old_tio_; BrowserClient *browser_client_; // URL input mode bool url_input_active_ = false; std::string url_input_buffer_; // Search input mode bool search_input_active_ = false; std::string search_input_buffer_; bool search_started_ = false; // true after Enter pressed // Auth dialog mode bool auth_dialog_active_ = false; std::string auth_username_buffer_; std::string auth_password_buffer_; bool auth_password_mode_ = false; // true when entering password // Console input mode bool console_input_active_ = false; std::string console_input_buffer_; int console_scroll_offset_ = 0; // Calculate scroll amount (2/3 of window height) int getScrollAmount() const { // Use terminal rows (not pixels) for more accurate calculation int scroll_rows = (term_height_ * 2) / 3; // 30 units per line gives good 2/3 viewport scrolling return scroll_rows * 30; } // JS prompt input std::string js_prompt_input_; // Coordinate click mode input bool coordinate_mode_active_ = false; std::string coordinate_input_buffer_; // Visual selection mode bool visual_mode_active_ = false; // Always-on coordinate cursor (positioned by two uppercase grid letters) char coord_buf_ = 0; // pending first uppercase letter, 0 if none bool coord_placed_ = false; // a pointer is currently shown int coord_col_ = 0; int coord_row_ = 0; // Mouse drag tracking bool mouse_button_down_ = false; CefBrowserHost::MouseButtonType mouse_button_type_ = MBT_LEFT; bool physical_mouse_dragging_ = false; int drag_start_x_ = 0; int drag_start_y_ = 0; // Modal control state InputMode current_mode_ = MODE_STANDARD; // Tiled rendering state (toggled with 'z' key) bool tiled_rendering_enabled_ = false; // Zoom level tracking double current_zoom_level_ = 0.0; };