From a7af08010131a877f9a8ae12ff18f91e0a45501b Mon Sep 17 00:00:00 2001 From: Witold Filipczyk Date: Wed, 24 May 2023 21:52:45 +0200 Subject: [PATCH] [teminal] Added two output parameters to get_terminal_size. cell_width and cell_height in pixels. It is a bit ugly, and not efficient. --- src/osdep/dos/dos.cpp | 4 +++- src/osdep/dos/dos.h | 2 +- src/osdep/os2/os2.c | 16 ++++++++++++---- src/osdep/osdep.c | 7 ++++++- src/osdep/osdep.h | 2 +- src/osdep/win32/win32.c | 10 +++++++++- src/terminal/event.c | 12 ++++++++++-- src/terminal/event.h | 21 +++++++++++++++------ src/terminal/kbd.c | 7 ++++--- src/terminal/terminal.cpp | 6 ++---- src/terminal/window.c | 4 ++-- 11 files changed, 65 insertions(+), 26 deletions(-) diff --git a/src/osdep/dos/dos.cpp b/src/osdep/dos/dos.cpp index f0b42365..fd07b8e3 100644 --- a/src/osdep/dos/dos.cpp +++ b/src/osdep/dos/dos.cpp @@ -230,10 +230,12 @@ static void sigbreak(int sig) break_pressed = 1; } -void get_terminal_size(int fd, int *x, int *y) +void get_terminal_size(int fd, int *x, int *y, int *cw, int *ch) { *x = ScreenCols(); *y = ScreenRows(); + *cw = 8; + *ch = 16; } void handle_terminal_resize(int fd, void (*fn)(void)) diff --git a/src/osdep/dos/dos.h b/src/osdep/dos/dos.h index 8bb01e14..89b9699f 100644 --- a/src/osdep/dos/dos.h +++ b/src/osdep/dos/dos.h @@ -39,7 +39,7 @@ int os_default_charset(void); void done_draw(void); int get_system_env(void); -void get_terminal_size(int fd, int *x, int *y); +void get_terminal_size(int fd, int *x, int *y, int *cw, int *ch); void *handle_mouse(int cons, void (*fn)(void *, char *, int), void *data); void handle_terminal_resize(int fd, void (*fn)(void)); void init_osdep(void); diff --git a/src/osdep/os2/os2.c b/src/osdep/os2/os2.c index 7e1c6394..9efcc509 100644 --- a/src/osdep/os2/os2.c +++ b/src/osdep/os2/os2.c @@ -66,11 +66,12 @@ winch_thread(void) the thread responsible to handle this. */ static int old_xsize, old_ysize; static int cur_xsize, cur_ysize; + int cw, ch; signal(SIGPIPE, SIG_IGN); - if (get_terminal_size(0, &old_xsize, &old_ysize)) return; + if (get_terminal_size(0, &old_xsize, &old_ysize, &cw, &ch)) return; while (1) { - if (get_terminal_size(0, &cur_xsize, &cur_ysize)) return; + if (get_terminal_size(0, &cur_xsize, &cur_ysize, &cw, &ch)) return; if ((old_xsize != cur_xsize) || (old_ysize != cur_ysize)) { old_xsize = cur_xsize; old_ysize = cur_ysize; @@ -112,7 +113,7 @@ unhandle_terminal_resize(int fd) } void -get_terminal_size(int fd, int *x, int *y) +get_terminal_size(int fd, int *x, int *y, int *cw, *ch) { if (is_xterm()) { #ifdef X2 @@ -128,10 +129,14 @@ get_terminal_size(int fd, int *x, int *y) */ *x = DEFAULT_TERMINAL_WIDTH; *y = DEFAULT_TERMINAL_HEIGHT; + *cw = 8; + *ch = 16; return 0; } *y = win.ws_row; *x = win.ws_col; + *cw = win.ws_xpixel / win.ws_col; + *ch = win.ws_ypixel / win.ws_row; /* DBG("%d %d", *x, *y); */ @@ -154,6 +159,8 @@ get_terminal_size(int fd, int *x, int *y) *y = get_e("LINES"); if (!*y) *y = DEFAULT_TERMINAL_HEIGHT; } + *cw = 8; + *ch = 16; } } @@ -611,9 +618,10 @@ want_draw(void) if (mouse_h != -1) { static int x = -1, y = -1; static int c = -1; + int cw, ch; if (x == -1 || y == -1 || (c != resize_count)) { - get_terminal_size(1, &x, &y); + get_terminal_size(1, &x, &y, &cw, &ch); c = resize_count; } diff --git a/src/osdep/osdep.c b/src/osdep/osdep.c index 102f46c9..c8c59628 100644 --- a/src/osdep/osdep.c +++ b/src/osdep/osdep.c @@ -216,16 +216,21 @@ unhandle_terminal_resize(int fd) } void -get_terminal_size(int fd, int *x, int *y) +get_terminal_size(int fd, int *x, int *y, int *cw, int *ch) { struct winsize ws; if (ioctl(1, TIOCGWINSZ, &ws) != -1) { *x = ws.ws_col; *y = ws.ws_row; + + *cw = ws.ws_xpixel / ws.ws_col; + *ch = ws.ws_ypixel / ws.ws_row; } else { *x = 0; *y = 0; + *cw = 8; + *ch = 16; } if (!*x) { diff --git a/src/osdep/osdep.h b/src/osdep/osdep.h index a466455d..048c52ff 100644 --- a/src/osdep/osdep.h +++ b/src/osdep/osdep.h @@ -24,7 +24,7 @@ int get_system_env(void); int get_e(const char *env); int is_xterm(void); int is_twterm(void); -void get_terminal_size(int, int *, int *); +void get_terminal_size(int, int *, int *, int *, int *); void handle_terminal_resize(int, void (*)(void)); void unhandle_terminal_resize(int); void set_bin(int); diff --git a/src/osdep/win32/win32.c b/src/osdep/win32/win32.c index f4c148de..6b6e4279 100644 --- a/src/osdep/win32/win32.c +++ b/src/osdep/win32/win32.c @@ -81,7 +81,7 @@ unhandle_terminal_resize(int fd) } void -get_terminal_size(int fd, int *x, int *y) +get_terminal_size(int fd, int *x, int *y, int *cw, int *ch) { CONSOLE_SCREEN_BUFFER_INFO csbi; @@ -97,6 +97,14 @@ get_terminal_size(int fd, int *x, int *y) if (!*y) *y = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; } + + if (!*cw) { + *cw = 8; + } + + if (!*ch) { + *ch = 16; + } } diff --git a/src/terminal/event.c b/src/terminal/event.c index 1c0fea63..a2876cf6 100644 --- a/src/terminal/event.c +++ b/src/terminal/event.c @@ -87,6 +87,10 @@ term_send_event(struct terminal *term, struct term_event *ev) } resize_screen(term, width, height); +#ifdef CONFIG_LIBSIXEL + term->cell_width = ev->info.size.cell_width; + term->cell_height = ev->info.size.cell_height; +#endif erase_screen(term); /* Fall through */ } @@ -250,7 +254,9 @@ handle_interlink_event(struct terminal *term, struct interlink_event *ilev) * possible. */ set_init_term_event(&tev, ilev->info.size.width, - ilev->info.size.height); + ilev->info.size.height, + ilev->info.size.cell_width, + ilev->info.size.cell_height); term_send_event(term, &tev); /* Either the initialization of the first session failed or we @@ -272,7 +278,9 @@ handle_interlink_event(struct terminal *term, struct interlink_event *ilev) case EVENT_RESIZE: set_wh_term_event(&tev, ilev->ev, ilev->info.size.width, - ilev->info.size.height); + ilev->info.size.height, + ilev->info.size.cell_width, + ilev->info.size.cell_height); term_send_event(term, &tev); /* If textarea_data is set and the terminal is not blocked, diff --git a/src/terminal/event.h b/src/terminal/event.h index 9b41dd7c..e9100737 100644 --- a/src/terminal/event.h +++ b/src/terminal/event.h @@ -22,6 +22,7 @@ enum term_event_type { struct term_event_size { int width, height; + int cell_width, cell_height; }; @@ -112,28 +113,36 @@ set_abort_term_event(struct term_event *ev) } static inline void -set_wh_term_event(struct term_event *ev, enum term_event_type type, int width, int height) +set_wh_term_event(struct term_event *ev, enum term_event_type type, int width, int height, int cell_width, int cell_height) { memset(ev, 0, sizeof(*ev)); ev->ev = type; ev->info.size.width = width; ev->info.size.height = height; +#ifdef CONFIG_LIBSIXEL + ev->info.size.cell_width = cell_width; + ev->info.size.cell_height = cell_height; +#endif } -#define set_init_term_event(ev, w, h) set_wh_term_event(ev, EVENT_INIT, w, h) -#define set_resize_term_event(ev, w, h) set_wh_term_event(ev, EVENT_RESIZE, w, h) -#define set_redraw_term_event(ev, w, h) set_wh_term_event(ev, EVENT_REDRAW, w, h) +#define set_init_term_event(ev, w, h, cw, ch) set_wh_term_event(ev, EVENT_INIT, w, h, cw, ch) +#define set_resize_term_event(ev, w, h, cw, ch) set_wh_term_event(ev, EVENT_RESIZE, w, h, cw, ch) +#define set_redraw_term_event(ev, w, h, cw, ch) set_wh_term_event(ev, EVENT_REDRAW, w, h, cw, ch) static inline void -set_wh_interlink_event(struct interlink_event *ev, enum term_event_type type, int width, int height) +set_wh_interlink_event(struct interlink_event *ev, enum term_event_type type, int width, int height, int cell_width, int cell_height) { memset(ev, 0, sizeof(*ev)); ev->ev = type; ev->info.size.width = width; ev->info.size.height = height; +#ifdef CONFIG_LIBSIXEL + ev->info.size.cell_width = cell_width; + ev->info.size.cell_height = cell_height; +#endif } -#define set_resize_interlink_event(ev, w, h) set_wh_interlink_event(ev, EVENT_RESIZE, w, h) +#define set_resize_interlink_event(ev, w, h, cw, ch) set_wh_interlink_event(ev, EVENT_RESIZE, w, h, cw, ch) /** This holds the information used when handling the initial diff --git a/src/terminal/kbd.c b/src/terminal/kbd.c index 553516d9..3396345f 100644 --- a/src/terminal/kbd.c +++ b/src/terminal/kbd.c @@ -229,9 +229,10 @@ resize_terminal(void) { struct interlink_event ev; int width, height; + int cell_width, cell_height; - get_terminal_size(ditrm->out.std, &width, &height); - set_resize_interlink_event(&ev, width, height); + get_terminal_size(ditrm->out.std, &width, &height, &cell_width, &cell_height); + set_resize_interlink_event(&ev, width, height, cell_width, cell_height); itrm_queue_event(ditrm, (char *) &ev, sizeof(ev)); } @@ -330,7 +331,7 @@ handle_trm(int std_in, int std_out, int sock_in, int sock_out, int ctl_in, dos_setraw(ctl_in, 1); #endif - get_terminal_size(ctl_in, &size->width, &size->height); + get_terminal_size(ctl_in, &size->width, &size->height, &size->cell_width, &size->cell_height); info.event.ev = EVENT_INIT; info.system_env = get_system_env(); info.length = init_len; diff --git a/src/terminal/terminal.cpp b/src/terminal/terminal.cpp index 17b38875..ea25c275 100644 --- a/src/terminal/terminal.cpp +++ b/src/terminal/terminal.cpp @@ -56,7 +56,7 @@ redraw_terminal(struct terminal *term) { struct term_event ev; - set_redraw_term_event(&ev, term->width, term->height); + set_redraw_term_event(&ev, term->width, term->height, term->cell_width, term->cell_height); term_send_event(term, &ev); } @@ -65,7 +65,7 @@ redraw_terminal_cls(struct terminal *term) { struct term_event ev; - set_resize_term_event(&ev, term->width, term->height); + set_resize_term_event(&ev, term->width, term->height, term->cell_width, term->cell_height); term_send_event(term, &ev); } @@ -120,8 +120,6 @@ init_term(int fdin, int fdout) #ifdef CONFIG_LIBSIXEL init_list(term->images); - term->cell_height = 13; - term->cell_width = 6; #endif term->fdin = fdin; term->fdout = fdout; diff --git a/src/terminal/window.c b/src/terminal/window.c index 903a9ad6..7280ce9c 100644 --- a/src/terminal/window.c +++ b/src/terminal/window.c @@ -45,7 +45,7 @@ redraw_windows(enum windows_to_redraw which, struct window *win) return; } - set_redraw_term_event(&ev, term->width, term->height); + set_redraw_term_event(&ev, term->width, term->height, term->cell_width, term->cell_height); for (; win != end; win = win->prev) { if (!inactive_tab(win)) win->handler(win, &ev); @@ -69,7 +69,7 @@ add_window(struct terminal *term, window_handler_T handler, void *data) win->term = term; win->type = WINDOW_NORMAL; add_at_pos((struct window *) &term->windows, win); - set_init_term_event(&ev, term->width, term->height); + set_init_term_event(&ev, term->width, term->height, term->cell_width, term->cell_height); win->handler(win, &ev); }