获取Linux监视器分辨率使用C

huangapple go评论93阅读模式
英文:

get Linux Monitor resolution with C

问题

I can provide a translation of the code and the related text you provided:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

unsigned short *get_screen_size(void)
{
    static unsigned short size[2];
    char *array[8];
    char screen_size[64];
    char* token = NULL;

    FILE *cmd = popen("xdpyinfo | awk '/dimensions/ {print $2}'", "r");

    if (!cmd)
        return 0;

    while (fgets(screen_size, sizeof(screen_size), cmd) != NULL);
    pclose(cmd);

    token = strtok(screen_size, "x\n");

    if (!token)
        return 0;

    for (unsigned short i = 0; token != NULL; ++i) {
        array[i] = token;
        token = strtok(NULL, "x\n");
    }
    size[0] = atoi(array[0]);
    size[1] = atoi(array[1]);
    size[2] = -1;

    return size;
}

int main(void)
{
    unsigned short *size = get_screen_size();

    printf("Screen resolution = %dx%d\n", size[0], size[1]);

    return 0;
}

当我运行它时:

sh: line 1: xdpyinfo: command not found
Segmentation fault (core dumped)

Please note that the code attempts to get the screen resolution using the xdpyinfo command, and the error you encountered suggests that the xdpyinfo command is not found on your system. You may need to install it or check if there are any issues with your environment configuration.

英文:

how can i get monitor resolution in Fedora Linux with C,
I need to know the screen size in my GTK3 application but I can't find a solution
Because I want to make a full screen software

And for this I need to know the screen size in pixels

How do desktops like gnome built with gtk find the screen size ?

I used this Code but it gave me an error :

#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;stdlib.h&gt;

unsigned short *get_screen_size(void)
{
    static unsigned short size[2];
    char *array[8];
    char screen_size[64];
    char* token = NULL;

    FILE *cmd = popen(&quot;xdpyinfo | awk &#39;/dimensions/ {print $2}&#39;&quot;, &quot;r&quot;);

    if (!cmd)
        return 0;

    while (fgets(screen_size, sizeof(screen_size), cmd) != NULL);
    pclose(cmd);

    token = strtok(screen_size, &quot;x\n&quot;);

    if (!token)
        return 0;

    for (unsigned short i = 0; token != NULL; ++i) {
        array[i] = token;
        token = strtok(NULL, &quot;x\n&quot;);
    }
    size[0] = atoi(array[0]);
    size[1] = atoi(array[1]);
    size[2] = -1;

    return size;
}


int main(void)
{
    unsigned short *size = get_screen_size();

    printf(&quot;Screen resolution = %dx%d\n&quot;, size[0], size[1]);

    return 0;
}

when i run it :

sh: line 1: xdpyinfo: command not found
Segmentation fault (core dumped)

答案1

得分: 1

以下是您要求翻译的代码部分:

 [GTK3][1] ([GDK3][2] 3.22+), [`gdk_monitor_get_geometry`][3] 可以用于填充一个 [`GdkRectangle`][4] 结构体,其中包含了 *显示器* 的宽度和高度。

另外,还有 [`gdk_monitor_get_workarea`][5],它的功能类似,但会考虑到停靠栏、面板以及其他桌面组件。

参见:

* [`gdk_window_fullscreen`][6] | [`gtk_window_fullscreen`][7]
* [`gdk_window_fullscreen_on_monitor`][8] | [`gtk_window_fullscreen_on_monitor`][9]
* [`gdk_window_unfullscreen`][10] | [`gtk_window_unfullscreen`][11]

```C
#include &lt;gtk/gtk.h&gt;
#include &lt;stdio.h&gt;

static void
activate(GtkApplication* app, gpointer user_data)
{
    GdkDisplay *display = gdk_display_get_default();
    int n_monitors = gdk_display_get_n_monitors(display);

    for (int nth = 0; nth &lt; n_monitors; nth++) {
        GdkMonitor *monitor = gdk_display_get_monitor(display, nth);

        GdkRectangle geo;
        gdk_monitor_get_geometry(monitor, &amp;geo);

        printf("(#%d) %-5s %-10s | Scale: %2d | %5dx%-5d | Position: %6dx%-6d%s\n",
                nth,
                gdk_monitor_get_manufacturer(monitor),
                gdk_monitor_get_model(monitor),
                gdk_monitor_get_scale_factor(monitor),
                geo.width,
                geo.height,
                geo.x,
                geo.y,
                gdk_monitor_is_primary(monitor) ? " (Primary)" : "");
    }
}

int
main(int argc, char **argv)
{
    GtkApplication *app = gtk_application_new("com.example.monitorsize", G_APPLICATION_DEFAULT_FLAGS);
    g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);

    int status = g_application_run(G_APPLICATION(app), argc, argv);
    g_object_unref(app);

    return status;
}

示例输出:

(#0) AOC   DVI-I-1    | Scale:  1 |  1920x1080  | Position:   1440x0      (Primary)
(#1) DEL   DP-1       | Scale:  1 |  1280x1024  | Position:   3360x0     
(#2) SAM   DVI-D-0    | Scale:  1 |  1440x900   | Position:      0x0

(现已废弃的)函数 gdk_screen_widthgdk_screen_height 获取默认 屏幕 的尺寸,但在存在多个显示器时,此信息并不那么有趣。

printf("%dx%d\n", gdk_screen_width(), gdk_screen_height());
4640x1080
英文:

In GTK3 (GDK3 3.22+), gdk_monitor_get_geometry can be used to populate a GdkRectangle structure with the width and height of a monitor.

Alternatively, there is gdk_monitor_get_workarea, which functions much the same, but takes into consideration docks, panels, and other desktop components.

See also:

#include &lt;gtk/gtk.h&gt;
#include &lt;stdio.h&gt;

static void
activate(GtkApplication* app, gpointer user_data)
{
	GdkDisplay *display = gdk_display_get_default();
	int n_monitors = gdk_display_get_n_monitors(display);

	for (int nth = 0; nth &lt; n_monitors; nth++) {
		GdkMonitor *monitor = gdk_display_get_monitor(display, nth);

		GdkRectangle geo;
		gdk_monitor_get_geometry(monitor, &amp;geo);

		printf(&quot;(#%d) %-5s %-10s | Scale: %2d | %5dx%-5d | Position: %6dx%-6d%s\n&quot;,
				nth,
				gdk_monitor_get_manufacturer(monitor),
				gdk_monitor_get_model(monitor),
				gdk_monitor_get_scale_factor(monitor),
				geo.width,
				geo.height,
				geo.x,
				geo.y,
				gdk_monitor_is_primary(monitor) ? &quot; (Primary)&quot; : &quot;&quot;);
	}
}

int
main(int argc, char **argv)
{
	GtkApplication *app = gtk_application_new(&quot;com.example.monitorsize&quot;, G_APPLICATION_DEFAULT_FLAGS);
	g_signal_connect(app, &quot;activate&quot;, G_CALLBACK(activate), NULL);

	int status = g_application_run(G_APPLICATION(app), argc, argv);
	g_object_unref(app);

	return status;
}

Example output:

(#0) AOC   DVI-I-1    | Scale:  1 |  1920x1080  | Position:   1440x0      (Primary)
(#1) DEL   DP-1       | Scale:  1 |  1280x1024  | Position:   3360x0     
(#2) SAM   DVI-D-0    | Scale:  1 |  1440x900   | Position:      0x0

The (now deprecated) functions gdk_screen_width and gdk_screen_height retrieve the dimensions of the default screen, but this information is rather uninteresting when there are multiple monitors.

printf(&quot;%dx%d\n&quot;, gdk_screen_width(), gdk_screen_height());
4640x1080

答案2

得分: 0

问题在于找不到(未安装)xdpyinfo命令。

在Fedora上包含xdpyinfo命令的软件包是xorg-x11-utils软件包。

要安装上述软件包,您可以使用以下命令:

yum install xorg-x11-utils
英文:

The problem here is that the xdpyinfo command was not found ( not installed ).

The package that contains the xdpyinfo command on fedora is the xorg-x11-utils package.

To install the above package you can use the following command:

yum install xorg-x11-utils

答案3

得分: 0

你可以在使用Linux时使用X11库。

// 声明包含
#include <stdio.h>
#include <X11/Xlib.h>

int main(void)
{
    // 声明变量
    Display *dpy = XOpenDisplay(NULL);
    XWindowAttributes wa = {0};

    // 获取屏幕属性
    XGetWindowAttributes(dpy, DefaultRootWindow(dpy), &wa);

    // 打印数值
    printf("宽度:%d\t高度:%d\n", wa.width, wa.height);

    // 退出程序
    return (0);
}

请注意,这仅适用于仅有一个屏幕的系统。

编辑:你可能需要安装libX11-devel包。

英文:

You can use the X11 library if you're using linux.

// Declare includes.
#include &lt;stdio.h&gt;
#include &lt;X11/Xlib.h&gt;

int main (void)
{
// Declare variables.
    Display *dpy = XOpenDisplay(NULL);
    XWindowAttributes wa = {0};

// Get screen attributes.
    XGetWindowAttributes(dpy, DefaultRootWindow(dpy), &amp;wa);

// Print values.
    printf(&quot;Width: %d\tHeight: %d\n&quot;, wa.width, wa.height);

// Exit program.
    return(0);
}

Keep in mind this is for systems with one screen only.

EDIT: You may need the package :libX11-devel

huangapple
  • 本文由 发表于 2023年6月26日 16:04:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76554710.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定