diff --git a/build.bat b/build.bat old mode 100644 new mode 100755 diff --git a/build.sh b/build.sh old mode 100644 new mode 100755 index 43b1653..4d54e42 --- a/build.sh +++ b/build.sh @@ -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 \ No newline at end of file diff --git a/build_web.bat b/build_web.bat old mode 100644 new mode 100755 diff --git a/build_web.sh b/build_web.sh old mode 100644 new mode 100755 diff --git a/src/backup/todo.txt b/src/backup/todo.txt index 4ac6494..0e80367 100644 --- a/src/backup/todo.txt +++ b/src/backup/todo.txt @@ -28,7 +28,7 @@ Splits: Linux - Add backtrace - +- Use DPI well, use as much resolution but scale with DPI Commands TODO: diff --git a/src/basic/basic_alloc.cpp b/src/basic/basic_alloc.cpp index c4d52e8..3a178e6 100644 --- a/src/basic/basic_alloc.cpp +++ b/src/basic/basic_alloc.cpp @@ -42,6 +42,8 @@ API bool VDecommit(void *p, size_t size) { } #elif OS_LINUX || OS_MAC +#include + API void *VReserve(size_t size) { void *result = mmap(0, size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, (off_t)0); diff --git a/src/basic/basic_os.cpp b/src/basic/basic_os.cpp index 7111bb6..ad145af 100644 --- a/src/basic/basic_os.cpp +++ b/src/basic/basic_os.cpp @@ -14,11 +14,58 @@ #include #include #include +#include +#include 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; } diff --git a/src/text_editor/buffer.cpp b/src/text_editor/buffer.cpp index 0edd068..e047bb4 100644 --- a/src/text_editor/buffer.cpp +++ b/src/text_editor/buffer.cpp @@ -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; } diff --git a/src/text_editor/commands_bindings.cpp b/src/text_editor/commands_bindings.cpp index f151817..29d5af3 100644 --- a/src/text_editor/commands_bindings.cpp +++ b/src/text_editor/commands_bindings.cpp @@ -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,12 +132,14 @@ 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; } } - Int p = ScreenSpaceToBufferPos(selected.window, selected.view, selected.buffer, mouse); + Int p = ScreenSpaceToBufferPos(selected.window, selected.view, selected.buffer, mouse); Caret &caret = selected.view->carets[0]; caret = SetFrontWithAnchor(caret, DocumentAnchor, p); } diff --git a/src/text_editor/event.cpp b/src/text_editor/event.cpp index 1e17c61..6c0f4a8 100644 --- a/src/text_editor/event.cpp +++ b/src/text_editor/event.cpp @@ -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,10 +346,10 @@ 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.xwheel = b.x; - event.ywheel = b.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; case SDL_EVENT_MOUSE_MOTION: { diff --git a/src/text_editor/text_editor.cpp b/src/text_editor/text_editor.cpp index c32c49d..5a3e1ea 100644 --- a/src/text_editor/text_editor.cpp +++ b/src/text_editor/text_editor.cpp @@ -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; } @@ -312,7 +315,7 @@ int main(int argc, char **argv) int yhalf = 30; Uint32 window_flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY; - SDLWindow = SDL_CreateWindow("Text editor", whalf, hhalf, window_flags); + SDLWindow = SDL_CreateWindow("Text editor", whalf, hhalf, window_flags); if (SDLWindow == NULL) { ReportErrorf("Couldn't create window! %s", SDL_GetError()); return 1; diff --git a/src/text_editor/title_bar.cpp b/src/text_editor/title_bar.cpp index 8588e1b..6601004 100644 --- a/src/text_editor/title_bar.cpp +++ b/src/text_editor/title_bar.cpp @@ -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);