英文:
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 <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;
}
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 <gtk/gtk.h>
#include <stdio.h>
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 < n_monitors; nth++) {
GdkMonitor *monitor = gdk_display_get_monitor(display, nth);
GdkRectangle geo;
gdk_monitor_get_geometry(monitor, &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_width
和 gdk_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:
gdk_window_fullscreen
|gtk_window_fullscreen
gdk_window_fullscreen_on_monitor
|gtk_window_fullscreen_on_monitor
gdk_window_unfullscreen
|gtk_window_unfullscreen
#include <gtk/gtk.h>
#include <stdio.h>
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 < n_monitors; nth++) {
GdkMonitor *monitor = gdk_display_get_monitor(display, nth);
GdkRectangle geo;
gdk_monitor_get_geometry(monitor, &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;
}
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("%dx%d\n", 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 <stdio.h>
#include <X11/Xlib.h>
int main (void)
{
// Declare variables.
Display *dpy = XOpenDisplay(NULL);
XWindowAttributes wa = {0};
// Get screen attributes.
XGetWindowAttributes(dpy, DefaultRootWindow(dpy), &wa);
// Print values.
printf("Width: %d\tHeight: %d\n", 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论