From 75920242a0efd749bf88fe1eb9a51946bb2a2365 Mon Sep 17 00:00:00 2001 From: raven Date: Thu, 9 Apr 2026 18:50:16 -0500 Subject: context menu to jump to different mappings --- src/main.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 57 insertions(+), 6 deletions(-) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index a806692..476ab06 100644 --- a/src/main.c +++ b/src/main.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -11,6 +12,8 @@ #include "curve.h" #include "procfs.h" #include "memory.h" +#include "font.h" +#include "menu.h" SDL_Window *window; SDL_Renderer *renderer; @@ -30,6 +33,8 @@ static page_list pages = {.fd = -1, .first = NULL}; static procfs_map *mappings = NULL; static int n_mappings = 0; +static menu *current_menu = NULL; + static double get_scale() { if (scale >= 1) { return SDL_round(scale); @@ -57,10 +62,8 @@ static void go_to(uintptr_t addr) { int ipos_x, ipos_y; to_pos(addr, &ipos_x, &ipos_y); pos_x = ipos_x; pos_y = ipos_y; - float mx, my; - SDL_GetMouseState(&mx, &my); - pos_x -= mx / get_scale(); - pos_y -= my / get_scale(); + pos_x -= window_width / 2 / get_scale(); + pos_y -= window_height / 2 / get_scale(); } static void detach() { @@ -181,6 +184,30 @@ static void handle_child() { } } +static void replace_menu(menu *new) { + if (current_menu) close_menu(current_menu); + current_menu = new; +} + +static void map_menu_selected(void *data) { + go_to((uintptr_t) data); +} + +static void open_map_menu() { + menu_entry *entries = calloc(n_mappings, sizeof(menu_entry)); + for (int i = 0; i < n_mappings; i++) { + entries[i].action = map_menu_selected; + entries[i].data = (void *) mappings[i].base; + snprintf( + entries[i].name, sizeof(entries[i].name), "%p %s", + mappings[i].base, mappings[i].name + ); + } + float mx, my; + SDL_GetMouseState(&mx, &my); + replace_menu(create_menu(entries, n_mappings, mx, my)); +} + static bool held(SDL_Keycode k) { const bool *state = SDL_GetKeyboardState(NULL); return state[SDL_GetScancodeFromKey(k, NULL)]; @@ -201,6 +228,13 @@ static void handle_key(SDL_Keycode key) { } static bool handle_event(SDL_Event e) { + if (current_menu) { + if (!menu_handle_event(current_menu, e)) { + close_menu(current_menu); + current_menu = NULL; + } + return true; + } switch (e.type) { case SDL_EVENT_KEY_DOWN: handle_key(e.key.key); @@ -210,7 +244,18 @@ static bool handle_event(SDL_Event e) { if (state & SDL_BUTTON_LMASK) { pos_x -= (double) e.motion.xrel / get_scale(); pos_y -= (double) e.motion.yrel / get_scale(); - } break; + } + break; + case SDL_EVENT_MOUSE_BUTTON_DOWN: + uint8_t button = e.button.button; + if (button == SDL_BUTTON_MIDDLE) { + open_map_menu(); + } else if (button == SDL_BUTTON_RIGHT && held(SDLK_LSHIFT)) { + open_map_menu(); + } else if (button == SDL_BUTTON_RIGHT && held(SDLK_RSHIFT)) { + open_map_menu(); + } + break; case SDL_EVENT_MOUSE_WHEEL: add_scale(e.wheel.y * get_scale() / 10); break; @@ -228,6 +273,9 @@ static void update() { n_mappings = procfs_maps(pid, &mappings); if (is_child) handle_child(); } + + if (current_menu) return; + if (held(SDLK_W)) { pos_y -= 5 / get_scale(); } @@ -332,17 +380,18 @@ static SDL_Texture *missing_texture() { static void main_loop() { int last_time = 0, current_time = 0; while (true) { - update(); SDL_Event e; while (SDL_PollEvent(&e)) { if (!handle_event(e)) { return; } } + update(); SDL_GetWindowSize(window, &window_width, &window_height); SDL_RenderClear(renderer); show_pages(); SDL_RenderPresent(renderer); + if (current_menu) menu_render(current_menu); last_time = current_time; current_time = SDL_GetTicks(); @@ -358,6 +407,8 @@ static void usage(const char *name) { } int main(int argc, char *argv[]) { + must(TTF_Init()); + load_font(); must(SDL_Init(SDL_INIT_VIDEO)); must(SDL_CreateWindowAndRenderer("core", window_width, window_height, SDL_WINDOW_RESIZABLE, &window, &renderer -- cgit v1.2.3