Compare commits

...

2 Commits

Author SHA1 Message Date
Krzosa Karol
85ca1a6a9e RegisterCommand 2025-12-14 17:32:48 +01:00
Krzosa Karol
5a12ab8d8c Linux add backtrace, fixing scaling / DPI problems 2025-12-14 17:05:55 +01:00
14 changed files with 111 additions and 119 deletions

0
build.bat Normal file → Executable file
View File

81
build.sh Normal file → Executable file
View File

@@ -3,79 +3,10 @@
mkdir build
cd build
if [ "$1" = "release" ]; then
profile_flags="-DDEBUG_BUILD=0 -O2"
else
profile_flags="-DDEBUG_BUILD=1"
fi
FLAGS="-nostdlib++ -fno-exceptions -fdiagnostics-absolute-paths -g -Wno-writable-strings -I../src -DDEBUG_BUILD=1"
# clang -o metaprogram $FLAGS ../src/metaprogram/metaprogram.cpp
# ./metaprogram
if [ ! -f "lbaselib.o" ]; then
clang -g -I../src/external/lua/src -I../src/external/glad \
../src/external/lua/src/lbaselib.c \
../src/external/lua/src/lctype.c \
../src/external/lua/src/ldo.c \
../src/external/lua/src/lgc.c \
../src/external/lua/src/liolib.c \
../src/external/lua/src/lmem.c \
../src/external/lua/src/lopcodes.c \
../src/external/lua/src/lstate.c \
../src/external/lua/src/ltable.c \
../src/external/lua/src/lundump.c \
../src/external/lua/src/lzio.c \
../src/external/lua/src/lapi.c \
../src/external/lua/src/lcode.c \
../src/external/lua/src/ldblib.c \
../src/external/lua/src/ldump.c \
../src/external/lua/src/llex.c \
../src/external/lua/src/loadlib.c \
../src/external/lua/src/loslib.c \
../src/external/lua/src/lstring.c \
../src/external/lua/src/ltablib.c \
../src/external/lua/src/lutf8lib.c \
../src/external/lua/src/lauxlib.c \
../src/external/lua/src/lcorolib.c \
../src/external/lua/src/ldebug.c \
../src/external/lua/src/lfunc.c \
../src/external/lua/src/linit.c \
../src/external/lua/src/lmathlib.c \
../src/external/lua/src/lobject.c \
../src/external/lua/src/lparser.c \
../src/external/lua/src/lstrlib.c \
../src/external/lua/src/ltm.c \
../src/external/lua/src/lvm.c \
../src/external/glad/glad.c \
-c
fi
if [ ! -f "metaprogram.exe" ]; then
clang ../src/metaprogram/metaprogram.cpp ../src/basic/unix.cpp -o metaprogram.exe \
-nostdlib++ -fno-exceptions -fdiagnostics-absolute-paths -g \
-Wno-writable-strings \
-I../src
fi
./metaprogram.exe
clang ../src/text_editor/text_editor.cpp ../src/basic/unix.cpp -o te_linux.exe \
-nostdlib++ -fno-exceptions -fdiagnostics-absolute-paths -g \
$profile_flags \
-Wno-writable-strings \
-I../src/external/SDL/include \
-I../src/external/lua/src \
-I../src/external/glad \
-I../src/ \
-lm \
../src/external/SDL/build/libSDL3.a \
lbaselib.o lctype.o ldo.o lgc.o liolib.o lmem.o \
lopcodes.o lstate.o ltable.o lundump.o lzio.o lapi.o lcode.o ldblib.o ldump.o \
llex.o loadlib.o loslib.o lstring.o ltablib.o lutf8lib.o lauxlib.o lcorolib.o ldebug.o \
lfunc.o linit.o lmathlib.o lobject.o lparser.o lstrlib.o ltm.o lvm.o \
glad.o \
if [ "$1" = "release" ]; then
cp te_linux.exe ../data/te
echo written ../data/te
else
cp te_linux.exe ../data/te_debug
echo written ../data/te_debug
fi
I="-I../src/external/SDL/include -I../src/external/lua/src -I../src/external/glad"
clang -o te $FLAGS ../src/text_editor/text_editor.cpp $I -lSDL3 -lm -lbacktrace
cp te ../data/te

0
build_web.bat Normal file → Executable file
View File

0
build_web.sh Normal file → Executable file
View File

View File

@@ -26,11 +26,6 @@ Splits:
- move titlebar, search to splits?
Linux
- Add backtrace
Commands TODO:
- Search
- Ctrl + F

View File

@@ -42,6 +42,8 @@ API bool VDecommit(void *p, size_t size) {
}
#elif OS_LINUX || OS_MAC
#include <sys/mman.h>
API void *VReserve(size_t size) {
void *result = mmap(0, size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, (off_t)0);

View File

@@ -14,11 +14,58 @@
#include <stdlib.h>
#include <spawn.h>
#include <poll.h>
#include <execinfo.h>
#include <backtrace.h>
API void (*Error)(const char *, ...);
struct backtrace_state *backtrace_state = NULL;
void os_core_backtrace_error_callback(void *data, const char *msg, int errnum) {
Error("libbacktrace error: %s (errnum: %d)\n", msg, errnum);
}
int os_core_backtrace_print_callback(void *data, uintptr_t pc, const char *filename, int lineno, const char *function) {
bool printed = false;
if (filename != NULL) {
char buffer[512];
char *f = realpath(filename, buffer);
printf("%s:%d:1: ", f, lineno);
printed = true;
}
if (function != NULL) {
printf("%s", function);
printed = true;
}
if (printed) {
printf("\n");
}
return 0;
}
void os_unix_crash_handler(int signal, siginfo_t* info, void* context) {
backtrace_full(backtrace_state, 2, os_core_backtrace_print_callback, os_core_backtrace_error_callback, NULL);
exit(1);
}
void os_unix_register_crash_handler(void) {
backtrace_state = backtrace_create_state(NULL, 1, os_core_backtrace_error_callback, NULL);
struct sigaction sa;
sa.sa_sigaction = os_unix_crash_handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART | SA_SIGINFO;
sigaction(SIGSEGV, &sa, NULL);
sigaction(SIGABRT, &sa, NULL);
sigaction(SIGBUS, &sa, NULL);
sigaction(SIGILL, &sa, NULL);
sigaction(SIGFPE, &sa, NULL);
}
API void InitOS(void (*error_proc)(const char *, ...)) {
Error = error_proc;
os_unix_register_crash_handler();
}
API String ReadFile(Allocator al, String path) {
@@ -171,9 +218,9 @@ API void Advance(FileIter *it) {
const char *dir_char_ending = it->is_directory ? "/" : "";
const char *separator = it->path.data[it->path.len - 1] == '/' ? "" : "/";
it->relative_path = Format(it->allocator, "%.*s%s%s%s", FmtString(it->path), separator, file->d_name, dir_char_ending);
it->relative_path = Format(it->allocator, "%S%s%s%s", it->path, separator, file->d_name, dir_char_ending);
it->absolute_path = GetAbsolutePath(it->allocator, it->relative_path);
if (it->is_directory) it->absolute_path = Format(it->allocator, "%.*s/", FmtString(it->absolute_path));
if (it->is_directory) it->absolute_path = Format(it->allocator, "%S/", it->absolute_path);
it->is_valid = true;
return;
}

View File

@@ -1467,13 +1467,11 @@ void InitBuffers() {
Buffer *null_buffer = CreateBuffer(sys_allocator, GetUniqueBufferName(GetWorkingDir(scratch), "console"));
View *null_view = CreateView(null_buffer->id);
Assert(null_buffer->id == NullBufferID && null_view->id == NullViewID);
TraceBuffer = CreateBuffer(sys_allocator, GetUniqueBufferName(WorkDir, "trace"));
TraceView = CreateView(TraceBuffer->id);
GCInfoBuffer = CreateBuffer(sys_allocator, GetUniqueBufferName(WorkDir, "gc"));
EventBuffer = CreateBuffer(sys_allocator, GetUniqueBufferName(WorkDir, "events"));
ScratchBuffer = BufferOpenFile(GetUniqueBufferName(WorkDir, "scratch"));
EventBuffer->no_history = true;
GCInfoBuffer->no_history = true;
}

View File

@@ -705,7 +705,7 @@ void SaveBuffer(Buffer *buffer) {
void Command_Save() {
BSet active = GetBSet(LastActiveLayoutWindowID);
SaveBuffer(active.buffer);
}
} RegisterCommand(Command_Save);
int Lua_Save(lua_State *L) {
Command_Save();
@@ -718,7 +718,7 @@ void Command_SaveAll() {
SaveBuffer(it);
}
}
}
} RegisterCommand(Command_SaveAll);
int Lua_SaveAll(lua_State *L) {
Command_SaveAll();
@@ -992,7 +992,7 @@ void Command_Reopen() {
BSet main = GetBSet(LastActiveLayoutWindowID);
ReopenBuffer(main.buffer);
NextActiveWindowID = main.window->id;
}
} RegisterCommand(Command_Reopen);
int Lua_Reopen(lua_State *L) {
Command_Reopen();
@@ -1016,15 +1016,16 @@ void New(Window *window, String name = "") {
WindowOpenBufferView(window, name);
}
void Command_New(String name = "") {
void Command_New() {
BSet main = GetBSet(LastActiveLayoutWindowID);
New(main.window, name);
}
New(main.window, "");
} RegisterCommand(Command_New);
int Lua_New(lua_State *L) {
String name = lua_tostring(L, 1);
lua_pop(L, 1);
Command_New(name);
BSet main = GetBSet(LastActiveLayoutWindowID);
New(main.window, name);
return 0;
}
@@ -1070,7 +1071,7 @@ void Command_ToggleFullscreen() {
}
IsInFullscreen = !IsInFullscreen;
}
} RegisterCommand(Command_ToggleFullscreen);
int Lua_ToggleFullscreen(lua_State *L) {
Command_ToggleFullscreen();
@@ -1102,7 +1103,7 @@ void Command_ListCode() {
main.view->fuzzy_search = true;
main.view->update_scroll = true;
SelectRange(main.view, GetBufferEndAsRange(main.buffer));
}
} RegisterCommand(Command_ListCode);
int Lua_ListCode(lua_State *L) {
Command_ListCode();
@@ -1252,7 +1253,7 @@ void Command_ShowBufferList() {
}
command_bar.view->update_scroll = true;
SelectRange(command_bar.view, GetBufferEndAsRange(command_bar.buffer));
}
} RegisterCommand(Command_ShowBufferList);
void Command_ShowCommandList() {
BSet command_bar = GetBSet(CommandBarWindowID);
@@ -1265,7 +1266,7 @@ void Command_ShowCommandList() {
command_bar.view->update_scroll = true;
SelectRange(command_bar.view, GetBufferEndAsRange(command_bar.buffer));
}
} RegisterCommand(Command_ShowCommandList);
void Command_ListViews() {
BSet command_bar = GetBSet(CommandBarWindowID);
@@ -1279,7 +1280,7 @@ void Command_ListViews() {
command_bar.view->fuzzy_search = true;
command_bar.view->update_scroll = true;
SelectRange(command_bar.view, GetBufferEndAsRange(command_bar.buffer));
}
} RegisterCommand(Command_ListViews);
int Lua_ListViews(lua_State *L) {
Command_ListViews();
@@ -1316,7 +1317,7 @@ void SetProjectFile(Buffer *buffer) {
void Command_SetProjectFile() {
BSet main = GetBSet(LastActiveLayoutWindowID);
SetProjectFile(main.buffer);
}
} RegisterCommand(Command_SetProjectFile);
void Command_SetWorkDir() {
String dir = lua_tostring(LuaState, -1);
@@ -1331,7 +1332,7 @@ void Command_SetWorkDir() {
void Command_SetProject() {
Command_SetWorkDir();
Command_SetProjectFile();
}
} RegisterCommand(Command_SetProject);
int Lua_SetProjectFile(lua_State *L) {
Command_SetProjectFile();
@@ -1419,12 +1420,12 @@ String16 FetchLoadWord(void) {
void Command_ToggleDebug() {
Window *window = GetWindow(DebugWindowID);
window->visible = !window->visible;
}
} RegisterCommand(Command_ToggleDebug);
void Command_KillProcess() {
BSet main = GetBSet(LastActiveLayoutWindowID);
KillProcess(main.view);
}
} RegisterCommand(Command_KillProcess);
int Lua_KillProcess(lua_State *L) {
Command_KillProcess();
@@ -1434,7 +1435,7 @@ int Lua_KillProcess(lua_State *L) {
void Command_KillWindow() {
BSet main = GetBSet(LastActiveLayoutWindowID);
main.window->kill = true;
}
} RegisterCommand(Command_KillWindow);
int Lua_KillWindow(lua_State *L) {
Command_KillWindow();

View File

@@ -125,8 +125,6 @@ void OnCommand(Event event) {
} else if (IsDocumentSelectionValid()) {
Assert(ScrollbarSelected.id == -1);
BSet selected = GetBSet(DocumentSelected);
Vec2I mouse = MouseVec2I();
// Special case for full-screen where we can have document
// aligned with monitor screen in which case mouse cursor cannot
@@ -134,6 +132,8 @@ void OnCommand(Event event) {
if (mouse.y == 0 && selected.window->document_rect.min.y == 0) {
float x, y;
SDL_GetGlobalMouseState(&x, &y);
x = roundf(DPIScale * x);
y = roundf(DPIScale * y);
if (y == 0) {
mouse.y = -10;
}

View File

@@ -270,11 +270,11 @@ void FillEventWithBasicData(Event *event) {
float xmouse, ymouse;
SDL_GetMouseState(&xmouse, &ymouse);
event->xmouse = (int16_t)xmouse;
event->ymouse = (int16_t)ymouse;
event->xmouse = (int16_t)roundf(DPIScale * xmouse);
event->ymouse = (int16_t)roundf(DPIScale * ymouse);
int xwindow, ywindow;
SDL_GetWindowSize(SDLWindow, &xwindow, &ywindow);
SDL_GetWindowSizeInPixels(SDLWindow, &xwindow, &ywindow);
event->xwindow = xwindow;
event->ywindow = ywindow;
event->text = "";
@@ -305,8 +305,8 @@ Event TranslateSDLEvent(SDL_Event *input_event) {
case SDL_EVENT_MOUSE_BUTTON_DOWN: {
SDL_MouseButtonEvent &b = input_event->button;
event.xmouse = (int16_t)b.x;
event.ymouse = (int16_t)b.y;
event.xmouse = (int16_t)roundf(DPIScale * b.x);
event.ymouse = (int16_t)roundf(DPIScale * b.y);
event.clicks = b.clicks;
if (b.button == SDL_BUTTON_LEFT) {
event.kind = EVENT_MOUSE_LEFT;
@@ -326,8 +326,8 @@ Event TranslateSDLEvent(SDL_Event *input_event) {
case SDL_EVENT_MOUSE_BUTTON_UP: {
SDL_MouseButtonEvent &b = input_event->button;
event.xmouse = (int16_t)b.x;
event.ymouse = (int16_t)b.y;
event.xmouse = (int16_t)roundf(DPIScale * b.x);
event.ymouse = (int16_t)roundf(DPIScale * b.y);
if (b.button == SDL_BUTTON_LEFT) {
event.kind = EVENT_MOUSE_LEFT_UP;
} else if (b.button == SDL_BUTTON_RIGHT) {
@@ -346,8 +346,8 @@ Event TranslateSDLEvent(SDL_Event *input_event) {
case SDL_EVENT_MOUSE_WHEEL: {
event.kind = EVENT_MOUSE_WHEEL;
SDL_MouseWheelEvent &b = input_event->wheel;
event.xmouse = (int16_t)b.mouse_x;
event.ymouse = (int16_t)b.mouse_y;
event.xmouse = (int16_t)roundf(DPIScale * b.x);
event.ymouse = (int16_t)roundf(DPIScale * b.y);
event.xwheel = b.x;
event.ywheel = b.y;
} break;

View File

@@ -185,3 +185,18 @@ void ReloadStyle() {
StyleVCVarsall = GetStyleString("VCVarsall", StyleVCVarsall);
StyleUndoMergeTimeout = GetStyleFloat("UndoMergeTimeout", StyleUndoMergeTimeout);
}
typedef void CommandFunction(void);
Array<CommandFunction *> CommandFunctions;
#define STRINGIFY_(x) x
#define STRINGIFY(x) STRINGIFY_(x)
#define CONCAT_(a, b) a ## b
#define CONCAT(a, b) CONCAT_(a, b)
struct Register_Command {
Register_Command(CommandFunction *f) {
Add(&CommandFunctions, f);
}
};
#define RegisterCommand(NAME) Register_Command CONCAT(COMMAND, __COUNTER__)(NAME)

View File

@@ -7,6 +7,9 @@
#include "external/glad/glad.h"
#include "external/stb_truetype.h"
#include "external/stb_truetype.c"
#if OS_LINUX
#define MCO_USE_UCONTEXT
#endif
#define MINICORO_IMPL
#include "external/minicoro.h"
#define LUA_USE_LONGJMP
@@ -198,7 +201,7 @@ void MainLoop() {
if (it.xwindow == 0 || it.ywindow == 0) {
int xwindow, ywindow;
SDL_GetWindowSize(SDLWindow, &xwindow, &ywindow);
SDL_GetWindowSizeInPixels(SDLWindow, &xwindow, &ywindow);
it.xwindow = xwindow;
it.ywindow = ywindow;
}

View File

@@ -14,7 +14,7 @@ void UpdateDebugBuffer() {
float xmouse, ymouse;
SDL_GetMouseState(&xmouse, &ymouse);
RawAppendf(buffer, "mouse: [%f, %f]\n", xmouse, ymouse);
RawAppendf(buffer, "mouse: [%f, %f]\n", roundf(DPIScale * xmouse), roundf(DPIScale * ymouse));
RawAppendf(buffer, "BufferID id = %d\n", main.buffer->id.id);
RawAppendf(buffer, "String name = %S\n", main.buffer->name);