Integrate top-level .gitignore into .gitignore for each subdirectory
[free-sw/xcb/demo] / xcbxvinfo.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <ctype.h>
5 #include <X11/XCB/xcb.h>
6 #include <X11/XCB/xv.h>
7
8 static void PrintUsage()
9 {
10     fprintf(stderr, "Usage:  xvinfo [-display host:dpy]\n");
11     exit(0);
12 }
13
14 XCBSCREEN *ScreenOfDisplay (XCBConnection *c, int screen)
15 {
16     XCBSCREENIter iter = XCBSetupRootsIter (XCBGetSetup (c));
17     for (; iter.rem; --screen, XCBSCREENNext (&iter))
18         if (screen == 0)
19             return iter.data;
20     return NULL;
21 }
22
23 static int nstrcmp(char *b, int n, char *s) {
24     while (n > 0) {
25         if (*s == '\0')
26             return 1;
27         if (*b - *s != 0)
28             return *b - *s;
29         b++, s++, --n;
30     }
31     return -(*s != '\0');
32 }
33
34 /* 
35  * Copies a string s of size n and returns it with a NULL appended.
36  * String returned is allocated with malloc and should be freed later.
37  */
38 static char *ExtractString(char *s, int n) {
39     char *str;
40     str = (char *)malloc(sizeof(char) * (n+1));
41     strncpy(str, s, n); 
42     str[n] = '\0';
43     return str;
44 }
45
46 int main(int argc, char *argv[])
47 {
48     XCBConnection *c;
49     int scrn;
50     char *display_name = NULL;
51     char *name = NULL;
52     XCBWINDOW root_window = {0};
53     XCBSCREEN *screen;
54     XCBXvQueryExtensionRep *query_ext;
55     XCBXvQueryAdaptorsRep *adaptors_rep;
56     XCBXvAdaptorInfoIter adaptors_iter;
57     XCBXvAdaptorInfo *ainfo;
58     XCBXvFormat *format;
59     XCBXvQueryPortAttributesRep *attr_rep;
60     XCBXvAttributeInfoIter attr_iter;
61     XCBXvAttributeInfo *attribute;
62
63     int nscreens, nattr, i, j, k;
64
65     if ((argc != 1) && (argc != 3))
66         PrintUsage();
67
68     if (argc != 1) {
69         if (strcmp(argv[1], "-display"))
70             PrintUsage();
71         display_name = argv[2];
72     }
73
74     if (!display_name) display_name = getenv("DISPLAY");
75     if (!(c = XCBConnect(display_name, &scrn)))
76     {
77         fprintf(stderr, "xcbxvinfo: Unable to open display %s\n", display_name);
78         exit(1);
79     }
80
81     if (!(query_ext = XCBXvQueryExtensionReply(c, XCBXvQueryExtension(c), NULL)))
82     {
83         fprintf(stderr, "xvinfo: No X-Video extension on %s\n", display_name);
84         exit(0);
85     }
86     else
87     {
88         fprintf(stdout, "X-Video Extension version %i.%i\n", query_ext->major, query_ext->minor);
89     }
90
91     free(query_ext);
92
93     nscreens = XCBSetupRootsLength(XCBGetSetup(c));
94
95     for (i = 0; i < nscreens; i++)
96     {
97         fprintf(stdout, "screen #%i\n", i);
98
99         screen = ScreenOfDisplay(c, scrn);
100         if (screen) root_window = screen->root;
101
102         adaptors_rep = XCBXvQueryAdaptorsReply(c, XCBXvQueryAdaptors(c, root_window), NULL);
103         if (!adaptors_rep->num_adaptors) {
104             fprintf(stdout, " no adaptors present.\n");
105             free(adaptors_rep);
106             continue;
107         }
108
109         adaptors_iter = XCBXvQueryAdaptorsInfoIter(adaptors_rep);
110
111         for (j = 0; j < adaptors_rep->num_adaptors; j++)
112         {
113             ainfo = adaptors_iter.data;
114             name = ExtractString(XCBXvAdaptorInfoName(ainfo), XCBXvAdaptorInfoNameLength(ainfo));
115             fprintf(stdout, "  Adaptor #%i: \"%s\"\n", j, name);
116             fprintf(stdout, "    number of ports: %i\n", ainfo->num_ports);
117             fprintf(stdout, "    port base: %i\n", ainfo->base_id.xid);
118             fprintf(stdout, "    operations supported: ");
119             free(name);
120
121             switch(ainfo->type & (XCBXvTypeInputMask | XCBXvTypeOutputMask)) {
122                 case XCBXvTypeInputMask:
123                     if (ainfo->type & XCBXvTypeVideoMask)
124                         fprintf(stdout, "PutVideo ");
125                     if (ainfo->type & XCBXvTypeStillMask)
126                         fprintf(stdout, "PutStill ");
127                     if (ainfo->type & XCBXvTypeImageMask)
128                         fprintf(stdout, "PutImage ");
129                     break;
130                 case XCBXvTypeOutputMask:
131                     if (ainfo->type & XCBXvTypeVideoMask)
132                         fprintf(stdout, "GetVideo ");
133                     if (ainfo->type & XCBXvTypeStillMask)
134                         fprintf(stdout, "GetStill ");
135                     break;
136                 default:
137                     fprintf(stdout, "none ");
138                     break;
139             }
140             fprintf(stdout, "\n");
141
142             format = XCBXvAdaptorInfoFormats(ainfo);
143
144             fprintf(stdout, "    supported visuals:\n");
145             for (k=0; k < ainfo->num_formats; k++, format++)
146                 fprintf(stdout, "      depth %i, visualID 0x%2x\n",
147                         format->depth, format->visual.id);
148
149             attr_rep = XCBXvQueryPortAttributesReply(c,
150                     XCBXvQueryPortAttributes(c, ainfo->base_id), NULL);
151             nattr = attr_rep->num_attributes;
152             attr_iter = XCBXvQueryPortAttributesAttributesIter(attr_rep);
153
154             if (nattr) {            
155                 fprintf(stdout, "    number of attributes: %i\n", nattr);
156
157                 for (k = 0; k < nattr; k++) {
158                     attribute = attr_iter.data;
159                     fprintf(stdout, "      \"%s\" (range %i to %i)\n",
160                             XCBXvAttributeInfoName(attribute),
161                             attribute->min,
162                             attribute->max);
163
164                     if (attribute->flags & XCBXvAttributeFlagSettable)
165                         fprintf(stdout, "              client settable attribute\n");
166
167                     if (attribute->flags & XCBXvAttributeFlagGettable) {
168                         XCBATOM the_atom;
169                         XCBInternAtomRep *atom_rep;
170
171                         fprintf(stdout, "              client gettable attribute");
172
173                         atom_rep = XCBInternAtomReply(c,
174                                 XCBInternAtom(c,
175                                     1, 
176                                     /*XCBXvAttributeInfoNameLength(attribute),*/
177                                     strlen(XCBXvAttributeInfoName(attribute)),
178                                     XCBXvAttributeInfoName(attribute)),
179                                 NULL);
180                         the_atom = atom_rep->atom;
181
182                         if (the_atom.xid != 0) {
183                             XCBXvGetPortAttributeRep *pattr_rep =
184                                 XCBXvGetPortAttributeReply(c,
185                                         XCBXvGetPortAttribute(c, ainfo->base_id, the_atom),
186                                         NULL);
187                             if (pattr_rep) fprintf(stdout, " (current value is %i)", pattr_rep->value);
188                             free(pattr_rep);
189                         }
190                         fprintf(stdout, "\n");
191                         free(atom_rep);
192                     }
193                     XCBXvAttributeInfoNext(&attr_iter);
194                 }
195             }
196             else {
197                 fprintf(stdout, "    no port attributes defined\n");
198             }
199
200             XCBXvQueryEncodingsRep *qencodings_rep;
201             qencodings_rep = XCBXvQueryEncodingsReply(c, XCBXvQueryEncodings(c, ainfo->base_id), NULL);
202             int nencode = qencodings_rep->num_encodings;
203             XCBXvEncodingInfoIter encoding_iter = XCBXvQueryEncodingsInfoIter(qencodings_rep);
204             XCBXvEncodingInfo *encoding;
205
206             int ImageEncodings = 0;
207             if (nencode) {
208                 int n;
209                 for (n = 0; n < nencode; n++) {
210                     encoding = encoding_iter.data;
211                     name = ExtractString(XCBXvEncodingInfoName(encoding), XCBXvEncodingInfoNameLength(encoding));
212                     if (!nstrcmp(name, strlen(name), "XV_IMAGE"))
213                         ImageEncodings++;
214                     XCBXvEncodingInfoNext(&encoding_iter);
215                     free(name);
216                 }
217
218                 if(nencode - ImageEncodings) {
219                     fprintf(stdout, "    number of encodings: %i\n", nencode - ImageEncodings);
220
221                     /* Reset the iter. */
222                     encoding_iter = XCBXvQueryEncodingsInfoIter(qencodings_rep);
223                     for(n = 0; n < nencode; n++) {
224                         encoding = encoding_iter.data;
225                         name = ExtractString(XCBXvEncodingInfoName(encoding), XCBXvEncodingInfoNameLength(encoding));
226                         if(nstrcmp(name, strlen(name), "XV_IMAGE")) {
227                             fprintf(stdout,
228                                     "      encoding ID #%i: \"%*s\"\n",
229                                     encoding->encoding.xid,
230                                     strlen(name),
231                                     name);
232                             fprintf(stdout, "        size: %i x %i\n",
233                                     encoding->width,
234                                     encoding->height);
235                             fprintf(stdout, "        rate: %f\n",
236                                     (float)encoding->rate.numerator/
237                                     (float)encoding->rate.denominator);
238                             free(name);
239                         }
240                         XCBXvEncodingInfoNext(&encoding_iter);
241                     }
242                 }
243
244                 if(ImageEncodings && (ainfo->type & XCBXvTypeImageMask)) {
245                     char imageName[5] = {0, 0, 0, 0, 0};
246                     encoding_iter = XCBXvQueryEncodingsInfoIter(qencodings_rep);
247                     for(n = 0; n < nencode; n++) {
248                         encoding = encoding_iter.data;
249                         name = ExtractString(XCBXvEncodingInfoName(encoding), XCBXvEncodingInfoNameLength(encoding));
250                         if(!nstrcmp(name, strlen(name), "XV_IMAGE")) {
251                             fprintf(stdout, 
252                                     "    maximum XvImage size: %i x %i\n",      
253                                     encoding->width, encoding->height);
254                             break;
255                         }
256                         free(name);
257                     }
258                     XCBXvListImageFormatsRep *formats_rep;
259                     formats_rep = XCBXvListImageFormatsReply(c,
260                             XCBXvListImageFormats(c, ainfo->base_id),
261                             NULL);
262
263                     int numImages = formats_rep->num_formats;
264                     XCBXvImageFormatInfo *format;
265                     XCBXvImageFormatInfoIter formats_iter = XCBXvListImageFormatsFormatIter(formats_rep);
266                     fprintf(stdout, "    Number of image formats: %i\n",
267                             numImages);
268
269                     for(n = 0; n < numImages; n++) {
270                         format = formats_iter.data;
271                         memcpy(imageName, &(format->id), 4);
272                         fprintf(stdout, "      id: 0x%x", format->id);
273                         if(isprint(imageName[0]) && isprint(imageName[1]) &&
274                                 isprint(imageName[2]) && isprint(imageName[3])) 
275                         {
276                             fprintf(stdout, " (%s)\n", imageName);
277                         } else {
278                             fprintf(stdout, "\n");
279                         }
280                         fprintf(stdout, "        guid: ");
281                         fprintf(stdout, "%02x", (unsigned char) 
282                                 format->guid[0]);
283                         fprintf(stdout, "%02x", (unsigned char) 
284                                 format->guid[1]);
285                         fprintf(stdout, "%02x", (unsigned char) 
286                                 format->guid[2]);
287                         fprintf(stdout, "%02x-", (unsigned char) 
288                                 format->guid[3]);
289                         fprintf(stdout, "%02x", (unsigned char) 
290                                 format->guid[4]);
291                         fprintf(stdout, "%02x-", (unsigned char) 
292                                 format->guid[5]);
293                         fprintf(stdout, "%02x", (unsigned char) 
294                                 format->guid[6]);
295                         fprintf(stdout, "%02x-", (unsigned char) 
296                                 format->guid[7]);
297                         fprintf(stdout, "%02x", (unsigned char) 
298                                 format->guid[8]);
299                         fprintf(stdout, "%02x-", (unsigned char) 
300                                 format->guid[9]);
301                         fprintf(stdout, "%02x", (unsigned char) 
302                                 format->guid[10]);
303                         fprintf(stdout, "%02x", (unsigned char) 
304                                 format->guid[11]);
305                         fprintf(stdout, "%02x", (unsigned char) 
306                                 format->guid[12]);
307                         fprintf(stdout, "%02x", (unsigned char) 
308                                 format->guid[13]);
309                         fprintf(stdout, "%02x", (unsigned char) 
310                                 format->guid[14]);
311                         fprintf(stdout, "%02x\n", (unsigned char) 
312                                 format->guid[15]);
313
314                         fprintf(stdout, "        bits per pixel: %i\n",
315                                 format->bpp);
316                         fprintf(stdout, "        number of planes: %i\n",
317                                 format->num_planes);
318                         fprintf(stdout, "        type: %s (%s)\n", 
319                                 (format->type == XCBXvImageFormatInfoTypeRGB) ? "RGB" : "YUV",
320                                 (format->format == XCBXvImageFormatInfoFormatPacked) ? "packed" : "planar");
321
322                         if(format->type == XCBXvImageFormatInfoTypeRGB) {
323                             fprintf(stdout, "        depth: %i\n", 
324                                     format->depth);
325
326                             fprintf(stdout, "        red, green, blue masks: " 
327                                     "0x%x, 0x%x, 0x%x\n", 
328                                     format->red_mask,
329                                     format->green_mask,
330                                     format->blue_mask);
331                         } else {
332
333                         }
334                         XCBXvImageFormatInfoNext(&formats_iter);
335                     }
336                     free(formats_rep);
337                 }
338             }
339             free(qencodings_rep);
340             XCBXvAdaptorInfoNext(&adaptors_iter);
341         }
342         free(adaptors_rep);
343     }
344
345     return 0;
346 }