summaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c62
1 files changed, 57 insertions, 5 deletions
diff --git a/src/main.c b/src/main.c
index 4a80d21..e65459b 100644
--- a/src/main.c
+++ b/src/main.c
@@ -14,6 +14,7 @@
#include "memory.h"
#include "font.h"
#include "menu.h"
+#include "tooltip.h"
SDL_Window *window;
SDL_Renderer *renderer;
@@ -34,6 +35,7 @@ static procfs_map *mappings = NULL;
static int n_mappings = 0;
static menu *current_menu = NULL;
+static tooltip current_tooltip = {0};
static double get_scale() {
if (scale >= 1) {
@@ -307,6 +309,45 @@ static void update() {
} else {
mouse_held = false;
}
+
+ double x = mx, y = my;
+ absolute_pos(&x, &y);
+ uintptr_t addr = to_addr(x, y);
+ uintptr_t page_addr = addr - addr % PAGE_SIZE;
+ static uint8_t page_data[PAGE_SIZE];
+ if (read_mem(pages.fd, page_addr, page_data, PAGE_SIZE) == 0) {
+ uint8_t hover_byte = page_data[addr % PAGE_SIZE];
+ static char tooltip_buf[1024];
+ int n = snprintf(tooltip_buf, sizeof(tooltip_buf),
+ "%p\n0x%02x", addr, hover_byte
+ );
+ char *p = tooltip_buf + n;
+ if (hover_byte < 128 && hover_byte != 0) {
+ p = stpcpy(p, "\n\"");
+ int min, max;
+ for (min = addr % PAGE_SIZE; min > 0; min--) {
+ uint8_t next = page_data[min - 1];
+ if (next >= 128 || next == 0) break;
+ }
+ for (max = addr % PAGE_SIZE; max < PAGE_SIZE; max++) {
+ uint8_t cur = page_data[max];
+ if (cur >= 128 || cur == 0) break;
+ }
+ while (max - min > 64) {
+ if (max - addr % PAGE_SIZE > addr % PAGE_SIZE - min) {
+ max--;
+ } else {
+ min++;
+ }
+ }
+ memcpy(p, page_data + min, max - min);
+ p += max - min;
+ p = stpcpy(p, "\"");
+ }
+ set_tooltip_text(&current_tooltip, tooltip_buf);
+ } else {
+ set_tooltip_text(&current_tooltip, NULL);
+ }
}
static SDL_Texture *missing_texture();
@@ -377,6 +418,21 @@ static SDL_Texture *missing_texture() {
return tex;
}
+static void show() {
+ SDL_GetWindowSize(window, &window_width, &window_height);
+ SDL_RenderClear(renderer);
+ show_pages();
+ SDL_RenderPresent(renderer);
+ if (current_menu) {
+ menu_render(current_menu);
+ hide_tooltip(&current_tooltip);
+ } else {
+ float mx, my;
+ SDL_GetMouseState(&mx, &my);
+ show_tooltip(&current_tooltip, mx, my);
+ }
+}
+
static void main_loop() {
int last_time = 0, current_time = 0;
while (true) {
@@ -387,11 +443,7 @@ static void main_loop() {
}
}
update();
- SDL_GetWindowSize(window, &window_width, &window_height);
- SDL_RenderClear(renderer);
- show_pages();
- SDL_RenderPresent(renderer);
- if (current_menu) menu_render(current_menu);
+ show();
last_time = current_time;
current_time = SDL_GetTicks();