1
0
Fork 0
mirror of https://github.com/vim/vim synced 2025-03-26 11:45:22 +01:00

Added pixel size in getwininfo()

This commit is contained in:
mikoto2000 2024-10-31 21:43:56 +09:00
parent 5ccac75d96
commit 511470fb1a
4 changed files with 114 additions and 2 deletions

View file

@ -5071,6 +5071,8 @@ getwininfo([{winid}]) *getwininfo()*
botline last complete displayed buffer line
bufnr number of buffer in the window
height window height (excluding winbar)
hpixel window height of pixel-level
{only with UNIX build}
loclist 1 if showing a location list
{only with the +quickfix feature}
quickfix 1 if quickfix or location list window
@ -5082,6 +5084,8 @@ getwininfo([{winid}]) *getwininfo()*
variables a reference to the dictionary with
window-local variables
width window width
wpixel window width of pixel-level
{only with UNIX build}
winbar 1 if the window has a toolbar, 0
otherwise
wincol leftmost screen column of the window;

View file

@ -409,6 +409,18 @@ get_win_info(win_T *wp, short tpnr, short winnr)
if (dict == NULL)
return NULL;
# if defined(UNIX) || defined(VMS)
struct cellsize cs;
#ifdef FEAT_GUI
if (!gui.in_use)
{
#endif
calc_cell_size(&cs);
#ifdef FEAT_GUI
}
#endif
#endif
// make sure w_botline is valid
validate_botline_win(wp);
@ -416,6 +428,16 @@ get_win_info(win_T *wp, short tpnr, short winnr)
dict_add_number(dict, "winnr", winnr);
dict_add_number(dict, "winid", wp->w_id);
dict_add_number(dict, "height", wp->w_height);
# if defined(UNIX) || defined(VMS)
#ifdef FEAT_GUI
if (!gui.in_use)
{
#endif
dict_add_number(dict, "hpixel", cs.cs_ypixel * wp->w_height);
#ifdef FEAT_GUI
}
#endif
#endif
dict_add_number(dict, "winrow", wp->w_winrow + 1);
dict_add_number(dict, "topline", wp->w_topline);
dict_add_number(dict, "botline", wp->w_botline - 1);
@ -423,6 +445,16 @@ get_win_info(win_T *wp, short tpnr, short winnr)
dict_add_number(dict, "winbar", wp->w_winbar_height);
#endif
dict_add_number(dict, "width", wp->w_width);
# if defined(UNIX) || defined(VMS)
#ifdef FEAT_GUI
if (!gui.in_use)
{
#endif
dict_add_number(dict, "wpixel", cs.cs_xpixel * wp->w_width);
#ifdef FEAT_GUI
}
#endif
#endif
dict_add_number(dict, "wincol", wp->w_wincol + 1);
dict_add_number(dict, "textoff", win_col_off(wp));
dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);

View file

@ -4348,6 +4348,69 @@ mch_get_shellsize(void)
return OK;
}
/*
* Try to get the current terminal cell size.
* If faile get cell size, fallback 5x10 pixel.
*/
void
calc_cell_size(struct cellsize *cs_out) {
#if defined(FEAT_GUI)
if (!gui.in_use)
{
#endif
// get parent process pid.
pid_t ppid = getppid();
// get parent process's tty path.
char tty_path[256];
snprintf(tty_path, sizeof(tty_path), "/proc/%d/fd/0", ppid);
char actual_tty[256];
ssize_t len = readlink(tty_path, actual_tty, sizeof(actual_tty) - 1);
if (len == -1) {
cs_out->cs_xpixel = 5;
cs_out->cs_ypixel = 10;
return;
}
actual_tty[len] = '\0';
// open parent process's tty.
int tty_fd = open(tty_path, O_RDWR);
if (tty_fd == -1) {
cs_out->cs_xpixel = 5;
cs_out->cs_ypixel = 10;
return;
}
// get parent tty size.
struct winsize ws;
if (ioctl(tty_fd, TIOCGWINSZ, &ws) == -1) {
cs_out->cs_xpixel = 5;
cs_out->cs_ypixel = 10;
close(tty_fd);
return;
}
close(tty_fd);
// calculate parent tty's pixel per cell.
int x_cell_size = ws.ws_xpixel / ws.ws_col;
int y_cell_size = ws.ws_xpixel / ws.ws_row;
// calculate current tty's pixel
cs_out->cs_xpixel = x_cell_size;
cs_out->cs_ypixel = y_cell_size;
#if defined(FEAT_GUI)
}
else
{
cs_out->cs_xpixel = 5;
cs_out->cs_ypixel = 10;
}
#endif
}
#if defined(FEAT_TERMINAL) || defined(PROTO)
/*
* Report the windows size "rows" and "cols" to tty "fd".
@ -4367,8 +4430,13 @@ mch_report_winsize(int fd, int rows, int cols)
ws.ws_col = cols;
ws.ws_row = rows;
ws.ws_xpixel = cols * 5;
ws.ws_ypixel = rows * 10;
// calcurate and set tty pixel size
struct cellsize cs;
calc_cell_size(&cs);
ws.ws_xpixel = cols * cs.cs_xpixel;
ws.ws_ypixel = rows * cs.cs_ypixel;
retval = ioctl(tty_fd, TIOCSWINSZ, &ws);
ch_log(NULL, "ioctl(TIOCSWINSZ) %s", retval == 0 ? "success" : "failed");
# elif defined(TIOCSSIZE)

View file

@ -488,3 +488,11 @@ int mch_rename(const char *src, const char *dest);
// We have three kinds of ACL support.
#define HAVE_ACL (HAVE_POSIX_ACL || HAVE_SOLARIS_ACL || HAVE_AIX_ACL)
struct cellsize {
unsigned int cs_xpixel;
unsigned int cs_ypixel;
};
void calc_cell_size(struct cellsize *cs_out);