日期:2014-05-16  浏览次数:20652 次

Linux c 编程获取屏幕分辨率
请问怎么用Linux c 编程获取屏幕分辨率,不要QT和GTK的。 谢谢了

------解决方案--------------------
open /dev/graphics/fb0 
ioctl FBIOGET_VSCREENINFO.
------解决方案--------------------
我没学过内核级programming,但是如果是在X下的话,以下代码来自XCB官方。
C/C++ code

    #include <stdio.h>
    #include <xcb/xcb.h>

    int 
    main ()
    {
        /* Open the connection to the X server. Use the DISPLAY environment variable */

        int i, screenNum;
        xcb_connection_t *connection = xcb_connect (NULL, &screenNum);


        /* Get the screen whose number is screenNum */

        const xcb_setup_t *setup = xcb_get_setup (connection);
        xcb_screen_iterator_t iter = xcb_setup_roots_iterator (setup);  

        // we want the screen at index screenNum of the iterator
        for (i = 0; i < screenNum; ++i) {
            xcb_screen_next (&iter);
        }

        xcb_screen_t *screen = iter.data;


        /* report */

        printf ("\n");
        printf ("Informations of screen %ld:\n", screen->root);
        printf ("  width.........: %d\n", screen->width_in_pixels);
        printf ("  height........: %d\n", screen->height_in_pixels);
        printf ("  white pixel...: %ld\n", screen->white_pixel);
        printf ("  black pixel...: %ld\n", screen->black_pixel);
        printf ("\n");

        return 0;
    }

------解决方案--------------------
fb0是终端的SVGA FrameBuffer驱动程序。所以获得的是终端的数据。
XCB是X-Window C Binding,是一个用于和X交互的C语言API。你应该安装libxcb的开发库和头文件。比如fedora要安装libxcb-devel这样的包。
这样在gcc编译的时候指定--libs xcb就可以了。具体可以看教程http://xcb.freedesktop.org/tutorial/,注意不需要按照那个安装步骤,因为一般Linux发行版都打包了libxcb的开发文件,用软件管理器安装即可。