本文介绍了ws_xpixel 和 ws_ypixel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我用来打印当前终端的像素分辨率的代码.

#include #include #include int main (int argc, char *argv[]) {struct winsize ww;ioctl(STDOUT_FILENO, TIOCGWINSZ, &ww);printf ("x 像素 %d\n", ww.ws_xpixel);printf ("y 像素 %d\n", ww.ws_ypixel);返回0;}

我使用 this 作为 winsize 参考.但代码只打印零.如果我使用 ws_colws_row 它工作正常.

请帮忙,谢谢!

解决方案

如果你看一下 glibc 的源代码 你会看到ws_colws_row 实际上并没有使用.

/* TIOCGWINSZ 和 TIOCSWINSZ 请求的 ARG 类型.*/结构体{无符号短整数 ws_row;/* 行,以字符为单位.*/无符号短整数 ws_col;/* 列,以字符为单位.*//* 这些实际上并没有被使用.*/无符号短整数 ws_xpixel;/* 水平像素.*/无符号短整数 ws_ypixel;/* 垂直像素.*/};

PS:如果您不确定我为什么指向 glibc,请阅读此答案.>

Here is the code I am using to print the resolution in pixels of the current terminal.

#include <sys/ioctl.h>
#include <stdio.h>
#include <unistd.h>

int main (int argc, char *argv[]) {

    struct winsize ww;
    ioctl(STDOUT_FILENO, TIOCGWINSZ, &ww);
    printf ("x-pixels %d\n", ww.ws_xpixel);
    printf ("y-pixels %d\n", ww.ws_ypixel);
    return 0;
}

I used this as winsize reference.But the code prints only zeros. If I use ws_col or ws_row it works fine.

Please help, thanks !

解决方案

If you look at the source code of glibc you will see that ws_col and ws_row are not actually used.

/* Type of ARG for TIOCGWINSZ and TIOCSWINSZ requests.  */
struct winsize
{
  unsigned short int ws_row;    /* Rows, in characters.  */
  unsigned short int ws_col;    /* Columns, in characters.  */

  /* These are not actually used.  */
  unsigned short int ws_xpixel; /* Horizontal pixels.  */
  unsigned short int ws_ypixel; /* Vertical pixels.  */
};

P.S.: Read also this answer, if you are not sure why I am pointing to glibc.

这篇关于ws_xpixel 和 ws_ypixel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-09 14:35