425 lines
9.7 KiB
C++
425 lines
9.7 KiB
C++
#include "gl3w.h"
|
|
#include "Window.h"
|
|
#include "Runtime.h"
|
|
|
|
#define INITIAL_WIDTH 800
|
|
#define INITIAL_HEIGHT 800
|
|
#define FONT_SIZE 16
|
|
|
|
struct
|
|
{
|
|
SDL_TimerID timer_id;
|
|
SDL_Keycode keysym;
|
|
bool pressed;
|
|
} Key_Statuses[SDL_NUM_SCANCODES];
|
|
|
|
/**
|
|
* Initialize SDL.
|
|
*
|
|
* @retval true SDL was loaded successfully.
|
|
* @retval false Loading SDL failed.
|
|
*/
|
|
static bool Initialize_SDL()
|
|
{
|
|
static bool initialized = false;
|
|
|
|
if (initialized)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
atexit(SDL_Quit);
|
|
|
|
initialized = true;
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Initialize OpenGL.
|
|
*
|
|
* @retval true OpenGL was loaded successfully.
|
|
* @retval false Loading OpenGL failed.
|
|
*/
|
|
static bool Initialize_OpenGL()
|
|
{
|
|
if (gl3wInit() != 0)
|
|
{
|
|
/* Failed to gl3wInit() */
|
|
return false;
|
|
}
|
|
|
|
if (!gl3wIsSupported(3, 0))
|
|
{
|
|
/* OpenGL 3.0 is not supported */
|
|
return false;
|
|
}
|
|
|
|
glActiveTexture(GL_TEXTURE0);
|
|
glEnable(GL_BLEND);
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Create a Window.
|
|
*
|
|
* @retval true The Window was created successfully.
|
|
* @retval false There was an error while creating the Window.
|
|
*/
|
|
bool Window::create(std::shared_ptr<Buffer> buffer)
|
|
{
|
|
if (!Initialize_SDL())
|
|
{
|
|
return false;
|
|
}
|
|
|
|
m_window = SDL_CreateWindow(
|
|
APPNAME,
|
|
SDL_WINDOWPOS_UNDEFINED,
|
|
SDL_WINDOWPOS_UNDEFINED,
|
|
INITIAL_WIDTH,
|
|
INITIAL_HEIGHT,
|
|
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
|
|
if (m_window == NULL)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
(void)SDL_GL_CreateContext(m_window);
|
|
|
|
if (!Initialize_OpenGL())
|
|
{
|
|
return false;
|
|
}
|
|
|
|
m_exit_requested = false;
|
|
|
|
/* TODO: user configurable font, size */
|
|
std::string font_path = Runtime::find(Runtime::FONT, "DejaVuSansMono");
|
|
if (font_path == "")
|
|
{
|
|
return false;
|
|
}
|
|
|
|
m_font = std::make_shared<Font>();
|
|
if (!m_font->load(font_path.c_str(), FONT_SIZE))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
m_gl = std::make_shared<GL>();
|
|
|
|
m_target_column = 0u; /* TODO: fix */
|
|
|
|
glClearColor (0.0, 0.0, 0.0, 0.0);
|
|
|
|
m_buffer_pane = std::make_shared<BufferPane>(buffer, m_font, m_gl);
|
|
m_buffer_status_pane = std::make_shared<BufferStatusPane>(m_buffer_pane, m_font, m_gl);
|
|
|
|
resize();
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Run the Window event loop.
|
|
*
|
|
* TODO: make this a static method to support multiple GUI windows.
|
|
*/
|
|
void Window::run_event_loop()
|
|
{
|
|
SDL_Event event;
|
|
|
|
while ((!m_exit_requested) && SDL_WaitEvent(&event))
|
|
{
|
|
handle_event(event);
|
|
}
|
|
}
|
|
|
|
Uint32 Key_Repeat(Uint32 interval, void * param)
|
|
{
|
|
if (Key_Statuses[(uintptr_t)param].pressed)
|
|
{
|
|
SDL_Event event;
|
|
|
|
event.user.code = 0;
|
|
event.user.data1 = (void *)(uintptr_t)Key_Statuses[(uintptr_t)param].keysym;
|
|
event.user.data2 = nullptr;
|
|
event.type = SDL_USEREVENT;
|
|
|
|
SDL_PushEvent(&event);
|
|
|
|
return 25u;
|
|
}
|
|
else
|
|
{
|
|
return 0u;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Handle a SDL event.
|
|
*/
|
|
void Window::handle_event(SDL_Event & event)
|
|
{
|
|
switch (event.type)
|
|
{
|
|
case SDL_QUIT:
|
|
m_exit_requested = true;
|
|
break;
|
|
|
|
case SDL_KEYDOWN:
|
|
m_keymod = event.key.keysym.mod;
|
|
if (event.key.repeat == 0)
|
|
{
|
|
Key_Statuses[event.key.keysym.scancode].pressed = true;
|
|
Key_Statuses[event.key.keysym.scancode].keysym = event.key.keysym.sym;
|
|
Key_Statuses[event.key.keysym.scancode].timer_id = SDL_AddTimer(200, Key_Repeat, (void *)event.key.keysym.scancode);
|
|
handle_keysym(event.key.keysym.sym);
|
|
}
|
|
break;
|
|
|
|
case SDL_KEYUP:
|
|
m_keymod = event.key.keysym.mod;
|
|
Key_Statuses[event.key.keysym.scancode].pressed = false;
|
|
if (Key_Statuses[event.key.keysym.scancode].timer_id != 0)
|
|
{
|
|
SDL_RemoveTimer(Key_Statuses[event.key.keysym.scancode].timer_id);
|
|
Key_Statuses[event.key.keysym.scancode].timer_id = 0;
|
|
}
|
|
break;
|
|
|
|
case SDL_WINDOWEVENT:
|
|
switch (event.window.event)
|
|
{
|
|
case SDL_WINDOWEVENT_EXPOSED:
|
|
redraw();
|
|
break;
|
|
case SDL_WINDOWEVENT_RESIZED:
|
|
resize();
|
|
redraw();
|
|
break;
|
|
}
|
|
break;
|
|
|
|
case SDL_USEREVENT:
|
|
handle_keysym((uint32_t)(uintptr_t)event.user.data1);
|
|
break;
|
|
}
|
|
}
|
|
|
|
void Window::handle_keysym(uint32_t keysym)
|
|
{
|
|
handle_keyval(get_keyval(keysym));
|
|
}
|
|
|
|
void Window::handle_keyval(uint32_t keyval)
|
|
{
|
|
#if 0
|
|
if (m_buffer->piece_table->in_insert_mode())
|
|
{
|
|
if (keyval == '\033')
|
|
{
|
|
m_buffer->piece_table->end_insert();
|
|
}
|
|
else if (keyval <= 0xFFu)
|
|
{
|
|
m_buffer->piece_table->insert_code_point(keyval);
|
|
}
|
|
redraw();
|
|
}
|
|
else
|
|
#endif
|
|
{
|
|
switch (keyval)
|
|
{
|
|
case KEYMOD_CTRL + 'q':
|
|
m_exit_requested = true;
|
|
break;
|
|
case '0':
|
|
cursor_move(CURSOR_SOL);
|
|
break;
|
|
case '$':
|
|
cursor_move(CURSOR_EOL);
|
|
break;
|
|
case 'h':
|
|
cursor_move(CURSOR_LEFT);
|
|
break;
|
|
case 'j':
|
|
cursor_move(CURSOR_DOWN);
|
|
break;
|
|
case 'k':
|
|
cursor_move(CURSOR_UP);
|
|
break;
|
|
case 'l':
|
|
cursor_move(CURSOR_RIGHT);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
void Window::cursor_move(int which)
|
|
{
|
|
#if 0
|
|
bool moved = false;
|
|
int current_row_offset = m_cursor->column / m_columns;
|
|
int row_offset = 0;
|
|
|
|
switch (which)
|
|
{
|
|
case CURSOR_LEFT:
|
|
moved = m_cursor->check_go_left(1);
|
|
m_target_column = m_cursor->column;
|
|
break;
|
|
case CURSOR_RIGHT:
|
|
moved = m_cursor->check_go_right(1, false);
|
|
m_target_column = m_cursor->column;
|
|
break;
|
|
case CURSOR_UP:
|
|
moved = m_cursor->check_go_up(1, m_target_column);
|
|
break;
|
|
case CURSOR_DOWN:
|
|
moved = m_cursor->check_go_down(1, m_target_column);
|
|
break;
|
|
case CURSOR_SOL:
|
|
moved = m_cursor->check_go_start_of_line();
|
|
m_target_column = 0u;
|
|
break;
|
|
case CURSOR_EOL:
|
|
moved = m_cursor->check_go_end_of_line(false);
|
|
m_target_column = 0x7FFFFFFFu;
|
|
break;
|
|
}
|
|
if (moved)
|
|
{
|
|
switch (which)
|
|
{
|
|
case CURSOR_LEFT:
|
|
case CURSOR_RIGHT:
|
|
row_offset = m_cursor->column / m_columns - current_row_offset;
|
|
break;
|
|
case CURSOR_UP:
|
|
{
|
|
PieceTable::Cursor c = *m_cursor;
|
|
c.go_end_of_line(false);
|
|
row_offset = m_cursor->column / m_columns - c.column / m_columns - 1 - current_row_offset;
|
|
}
|
|
break;
|
|
case CURSOR_DOWN:
|
|
{
|
|
PieceTable::Cursor c = *m_cursor;
|
|
c.go_up(1, 0u);
|
|
c.go_end_of_line(false);
|
|
row_offset = c.column / m_columns - current_row_offset + 1 + m_cursor->column / m_columns;
|
|
}
|
|
break;
|
|
case CURSOR_SOL:
|
|
row_offset = -current_row_offset;
|
|
break;
|
|
case CURSOR_EOL:
|
|
{
|
|
PieceTable::Cursor c = *m_cursor;
|
|
c.go_end_of_line(false);
|
|
row_offset = c.column / m_columns - current_row_offset;
|
|
}
|
|
break;
|
|
}
|
|
update_cursor_row(m_cursor_row + row_offset);
|
|
redraw();
|
|
}
|
|
#endif
|
|
}
|
|
|
|
void Window::resize()
|
|
{
|
|
SDL_GetWindowSize(m_window, &m_width, &m_height);
|
|
glViewport(0, 0, m_width, m_height);
|
|
m_gl->resize(m_width, m_height);
|
|
int buffer_status_pane_height = m_font->get_line_height() + 1;
|
|
m_buffer_pane->move(0, buffer_status_pane_height);
|
|
m_buffer_pane->resize(m_width, m_height - buffer_status_pane_height);
|
|
m_buffer_status_pane->move(0, 0);
|
|
m_buffer_status_pane->resize(m_width, buffer_status_pane_height);
|
|
}
|
|
|
|
void Window::redraw()
|
|
{
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
m_buffer_pane->draw();
|
|
m_buffer_status_pane->draw();
|
|
|
|
SDL_GL_SwapWindow(m_window);
|
|
}
|
|
|
|
uint32_t Window::get_keyval(SDL_Keycode keysym)
|
|
{
|
|
uint32_t keyval = keysym;
|
|
if ((m_keymod & (KMOD_SHIFT | KMOD_CAPS)) != 0u)
|
|
{
|
|
uint32_t shifted = get_shifted(keyval);
|
|
if (shifted != keyval)
|
|
{
|
|
keyval = shifted;
|
|
}
|
|
else
|
|
{
|
|
keyval |= KEYMOD_SHIFT;
|
|
}
|
|
}
|
|
if ((m_keymod & KMOD_CTRL) != 0u)
|
|
{
|
|
keyval |= KEYMOD_CTRL;
|
|
}
|
|
if ((m_keymod & KMOD_ALT) != 0u)
|
|
{
|
|
keyval |= KEYMOD_ALT;
|
|
}
|
|
if ((m_keymod & KMOD_GUI) != 0u)
|
|
{
|
|
keyval |= KEYMOD_GUI;
|
|
}
|
|
return keyval;
|
|
}
|
|
|
|
uint32_t Window::get_shifted(uint32_t keysym)
|
|
{
|
|
if ((keysym >= 'a') && (keysym <= 'z'))
|
|
{
|
|
return keysym - ('a' - 'A');
|
|
}
|
|
|
|
switch (keysym)
|
|
{
|
|
case SDLK_QUOTE: return '"';
|
|
case SDLK_COMMA: return '<';
|
|
case SDLK_MINUS: return '_';
|
|
case SDLK_PERIOD: return '>';
|
|
case SDLK_SLASH: return '?';
|
|
case SDLK_0: return ')';
|
|
case SDLK_1: return '!';
|
|
case SDLK_2: return '@';
|
|
case SDLK_3: return '#';
|
|
case SDLK_4: return '$';
|
|
case SDLK_5: return '%';
|
|
case SDLK_6: return '^';
|
|
case SDLK_7: return '&';
|
|
case SDLK_8: return '*';
|
|
case SDLK_9: return '(';
|
|
case SDLK_SEMICOLON: return ':';
|
|
case SDLK_EQUALS: return '+';
|
|
case SDLK_LEFTBRACKET: return '{';
|
|
case SDLK_BACKSLASH: return '|';
|
|
case SDLK_RIGHTBRACKET: return '}';
|
|
case SDLK_BACKQUOTE: return '~';
|
|
}
|
|
|
|
return keysym;
|
|
}
|