Update demos for removal of XID structs and xcb_poll_for_reply error param.
[free-sw/xcb/demo] / xcbdpyinfo.c
1 #include <xcb/xcb.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 xcb_connection_t *c;
7
8 void print_setup();
9 void print_formats();
10 void list_extensions(void (*)(int, char *));
11 void print_extension(int, char *);
12 void query_extension(int, char *);
13 void list_screens();
14 void print_screen(xcb_screen_t *s);
15
16 int main(int argc, char **argv)
17 {
18     void (*ext_printer)(int, char *) = print_extension;
19     int screen;
20
21     c = xcb_connect(0, &screen);
22     if(!c)
23     {
24         fputs("Connect failed.\n", stderr);
25         exit(1);
26     }
27
28     for(--argc; argc; --argc)
29         if(!strcmp(argv[argc], "-queryExtensions"))
30             ext_printer = query_extension;
31
32     /* "name of display:    %s" "\n" */
33     print_setup(c);
34     /* "\n" "focus:  window 0x%x, revert to %s" (e.g. PointerRoot) */
35     list_extensions(ext_printer);
36     printf("\n" "default screen number:    %d", screen);
37     list_screens();
38     fputs("\n", stdout);
39
40     xcb_disconnect(c);
41
42     exit(0);
43 }
44
45 void print_setup()
46 {
47     printf("version number:    %d.%d", xcb_get_setup(c)->protocol_major_version, xcb_get_setup(c)->protocol_minor_version);
48     fputs("\n" "vendor string:    ", stdout);
49     fwrite(xcb_setup_vendor(xcb_get_setup(c)), 1, xcb_setup_vendor_length(xcb_get_setup(c)), stdout);
50     printf("\n" "vendor release number:    %d", (int) xcb_get_setup(c)->release_number);
51     /* "\n" "XFree86 version: %d.%d.%d.%d" */
52     printf("\n" "maximum request size:  %d bytes", xcb_get_setup(c)->maximum_request_length * 4);
53     printf("\n" "motion buffer size:  %d", (int)xcb_get_setup(c)->motion_buffer_size);
54     printf("\n" "bitmap unit, bit order, padding:    %d, %s, %d", xcb_get_setup(c)->bitmap_format_scanline_unit, (xcb_get_setup(c)->bitmap_format_bit_order == XCB_IMAGE_ORDER_LSB_FIRST) ? "LSBFirst" : "MSBFirst", xcb_get_setup(c)->bitmap_format_scanline_pad);
55     printf("\n" "image byte order:    %s", (xcb_get_setup(c)->image_byte_order == XCB_IMAGE_ORDER_LSB_FIRST) ? "LSBFirst" : "MSBFirst");
56
57     print_formats();
58
59     printf("\n" "keycode range:    minimum %d, maximum %d", xcb_get_setup(c)->min_keycode, xcb_get_setup(c)->max_keycode);
60 }
61
62 void print_formats()
63 {
64     int i = xcb_setup_pixmap_formats_length(xcb_get_setup(c));
65     xcb_format_t *p = xcb_setup_pixmap_formats(xcb_get_setup(c));
66     printf("\n" "number of supported pixmap formats:    %d", i);
67     fputs("\n" "supported pixmap formats:", stdout);
68     for(--i; i >= 0; --i, ++p)
69         printf("\n" "    depth %d, bits_per_pixel %d, scanline_pad %d", p->depth, p->bits_per_pixel, p->scanline_pad);
70 }
71
72 void list_extensions(void (*ext_printer)(int, char *))
73 {
74     xcb_list_extensions_reply_t *r;
75     xcb_str_iterator_t i;
76
77     r = xcb_list_extensions_reply(c, xcb_list_extensions(c), 0);
78     if(!r)
79     {
80         fputs("ListExtensions failed.\n", stderr);
81         return;
82     }
83
84     i = xcb_list_extensions_names_iterator(r);
85     printf("\n" "number of extensions:    %d", i.rem);
86     for(; i.rem; xcb_str_next(&i))
87     {
88         fputs("\n" "    ", stdout);
89         ext_printer(xcb_str_name_length(i.data), xcb_str_name(i.data));
90     }
91     free(r);
92 }
93
94 void print_extension(int len, char *name)
95 {
96     fwrite(name, 1, len, stdout);
97 }
98
99 void query_extension(int len, char *name)
100 {
101     xcb_query_extension_reply_t *r;
102     int comma = 0;
103
104     r = xcb_query_extension_reply(c, xcb_query_extension(c, len, name), 0);
105     if(!r)
106     {
107         fputs("QueryExtension failed.\n", stderr);
108         return;
109     }
110
111     print_extension(len, name);
112     fputs("  (", stdout);
113     if(r->major_opcode)
114     {
115         printf("opcode: %d", r->major_opcode);
116         comma = 1;
117     }
118     if(r->first_event)
119     {
120         if(comma)
121             fputs(", ", stdout);
122         printf("base event: %d", r->first_event);
123         comma = 1;
124     }
125     if(r->first_error)
126     {
127         if(comma)
128             fputs(", ", stdout);
129         printf("base error: %d", r->first_error);
130     }
131     fputs(")", stdout);
132 }
133
134 void list_screens()
135 {
136     xcb_screen_iterator_t i;
137     int cur;
138
139     i = xcb_setup_roots_iterator(xcb_get_setup(c));
140     printf("\n" "number of screens:    %d" "\n", i.rem);
141     for(cur = 1; i.rem; xcb_screen_next(&i), ++cur)
142     {
143         printf("\n" "screen #%d:", cur);
144         print_screen(i.data);
145     }
146 }
147
148 void print_screen(xcb_screen_t *s)
149 {
150     printf("\n" "  dimensions:    %dx%d pixels (%dx%d millimeters)", s->width_in_pixels, s->height_in_pixels, s->width_in_millimeters, s->height_in_millimeters);
151 }