Back to index

libdrm  2.4.37
modeprint.c
Go to the documentation of this file.
00001 /*
00002  * \file modedemo.c
00003  * Test program to dump DRM kernel mode setting related information.
00004  * Queries the kernel for all available information and dumps it to stdout.
00005  *
00006  * \author Jakob Bornecrantz <wallbraker@gmail.com>
00007  */
00008 
00009 /*
00010  * Copyright (c) 2007-2008 Tungsten Graphics, Inc., Cedar Park, Texas.
00011  * Copyright (c) 2007-2008 Jakob Bornecrantz <wallbraker@gmail.com>
00012  *
00013  * Permission is hereby granted, free of charge, to any person obtaining a
00014  * copy of this software and associated documentation files (the "Software"),
00015  * to deal in the Software without restriction, including without limitation
00016  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00017  * and/or sell copies of the Software, and to permit persons to whom the
00018  * Software is furnished to do so, subject to the following conditions:
00019  *
00020  * The above copyright notice and this permission notice shall be included in
00021  * all copies or substantial portions of the Software.
00022  *
00023  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00024  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00025  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00026  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00027  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00028  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
00029  * IN THE SOFTWARE.
00030  *
00031  */
00032 
00033 #include <assert.h>
00034 #include <stdio.h>
00035 #include <stdlib.h>
00036 #include <stdint.h>
00037 #include <unistd.h>
00038 #include <string.h>
00039 #include <inttypes.h>
00040 
00041 #include "xf86drm.h"
00042 #include "xf86drmMode.h"
00043 
00044 int connectors;
00045 int full_props;
00046 int edid;
00047 int modes;
00048 int full_modes;
00049 int encoders;
00050 int crtcs;
00051 int fbs;
00052 char *module_name;
00053 
00054 const char* getConnectionText(drmModeConnection conn)
00055 {
00056        switch (conn) {
00057        case DRM_MODE_CONNECTED:
00058               return "connected";
00059        case DRM_MODE_DISCONNECTED:
00060               return "disconnected";
00061        default:
00062               return "unknown";
00063        }
00064 
00065 }
00066 
00067 int printMode(struct drm_mode_modeinfo *mode)
00068 {
00069        if (full_modes) {
00070               printf("Mode: %s\n", mode->name);
00071               printf("\tclock       : %i\n", mode->clock);
00072               printf("\thdisplay    : %i\n", mode->hdisplay);
00073               printf("\thsync_start : %i\n", mode->hsync_start);
00074               printf("\thsync_end   : %i\n", mode->hsync_end);
00075               printf("\thtotal      : %i\n", mode->htotal);
00076               printf("\thskew       : %i\n", mode->hskew);
00077               printf("\tvdisplay    : %i\n", mode->vdisplay);
00078               printf("\tvsync_start : %i\n", mode->vsync_start);
00079               printf("\tvsync_end   : %i\n", mode->vsync_end);
00080               printf("\tvtotal      : %i\n", mode->vtotal);
00081               printf("\tvscan       : %i\n", mode->vscan);
00082               printf("\tvrefresh    : %i\n", mode->vrefresh);
00083               printf("\tflags       : %i\n", mode->flags);
00084        } else {
00085               printf("Mode: \"%s\" %ix%i %i\n", mode->name,
00086                             mode->hdisplay, mode->vdisplay, mode->vrefresh);
00087        }
00088        return 0;
00089 }
00090 
00091 int printProperty(int fd, drmModeResPtr res, drmModePropertyPtr props, uint64_t value)
00092 {
00093        const char *name = NULL;
00094        int j;
00095 
00096        printf("Property: %s\n", props->name);
00097        printf("\tid           : %i\n", props->prop_id);
00098        printf("\tflags        : %i\n", props->flags);
00099        printf("\tcount_values : %d\n", props->count_values);
00100 
00101 
00102        if (props->count_values) {
00103               printf("\tvalues       :");
00104               for (j = 0; j < props->count_values; j++)
00105                      printf(" %" PRIu64, props->values[j]);
00106               printf("\n");
00107        }
00108 
00109 
00110        printf("\tcount_enums  : %d\n", props->count_enums);
00111 
00112        if (props->flags & DRM_MODE_PROP_BLOB) {
00113               drmModePropertyBlobPtr blob;
00114 
00115               blob = drmModeGetPropertyBlob(fd, value);
00116               if (blob) {
00117                      printf("blob is %d length, %08X\n", blob->length, *(uint32_t *)blob->data);
00118                      drmModeFreePropertyBlob(blob);
00119               } else {
00120                      printf("error getting blob %" PRIu64 "\n", value);
00121               }
00122 
00123        } else {
00124               if (!strncmp(props->name, "DPMS", 4))
00125                      ;
00126 
00127               for (j = 0; j < props->count_enums; j++) {
00128                      printf("\t\t%lld = %s\n", props->enums[j].value, props->enums[j].name);
00129                      if (props->enums[j].value == value)
00130                             name = props->enums[j].name;
00131               }
00132 
00133               if (props->count_enums && name) {
00134                      printf("\tcon_value    : %s\n", name);
00135               } else {
00136                      printf("\tcon_value    : %" PRIu64 "\n", value);
00137               }
00138        }
00139 
00140        return 0;
00141 }
00142 
00143 int printConnector(int fd, drmModeResPtr res, drmModeConnectorPtr connector, uint32_t id)
00144 {
00145        int i = 0;
00146        struct drm_mode_modeinfo *mode = NULL;
00147        drmModePropertyPtr props;
00148 
00149        printf("Connector: %d-%d\n", connector->connector_type, connector->connector_type_id);
00150        printf("\tid             : %i\n", id);
00151        printf("\tencoder id     : %i\n", connector->encoder_id);
00152        printf("\tconn           : %s\n", getConnectionText(connector->connection));
00153        printf("\tsize           : %ix%i (mm)\n", connector->mmWidth, connector->mmHeight);
00154        printf("\tcount_modes    : %i\n", connector->count_modes);
00155        printf("\tcount_props    : %i\n", connector->count_props);
00156        if (connector->count_props) {
00157               printf("\tprops          :");
00158               for (i = 0; i < connector->count_props; i++)
00159                      printf(" %i", connector->props[i]);
00160               printf("\n");
00161        }
00162 
00163        printf("\tcount_encoders : %i\n", connector->count_encoders);
00164        if (connector->count_encoders) {
00165               printf("\tencoders       :");
00166               for (i = 0; i < connector->count_encoders; i++)
00167                      printf(" %i", connector->encoders[i]);
00168               printf("\n");
00169        }
00170 
00171        if (modes) {
00172               for (i = 0; i < connector->count_modes; i++) {
00173                      mode = (struct drm_mode_modeinfo *)&connector->modes[i];
00174                      printMode(mode);
00175               }
00176        }
00177 
00178        if (full_props) {
00179               for (i = 0; i < connector->count_props; i++) {
00180                      props = drmModeGetProperty(fd, connector->props[i]);
00181                      if (props) {
00182                             printProperty(fd, res, props, connector->prop_values[i]);
00183                             drmModeFreeProperty(props);
00184                      }
00185               }
00186        }
00187 
00188        return 0;
00189 }
00190 
00191 int printEncoder(int fd, drmModeResPtr res, drmModeEncoderPtr encoder, uint32_t id)
00192 {
00193        printf("Encoder\n");
00194        printf("\tid     :%i\n", id);
00195        printf("\tcrtc_id   :%d\n", encoder->crtc_id);
00196        printf("\ttype   :%d\n", encoder->encoder_type);
00197        printf("\tpossible_crtcs  :0x%x\n", encoder->possible_crtcs);
00198        printf("\tpossible_clones :0x%x\n", encoder->possible_clones);
00199        return 0;
00200 }
00201 
00202 int printCrtc(int fd, drmModeResPtr res, drmModeCrtcPtr crtc, uint32_t id)
00203 {
00204        printf("Crtc\n");
00205        printf("\tid             : %i\n", id);
00206        printf("\tx              : %i\n", crtc->x);
00207        printf("\ty              : %i\n", crtc->y);
00208        printf("\twidth          : %i\n", crtc->width);
00209        printf("\theight         : %i\n", crtc->height);
00210        printf("\tmode           : %p\n", &crtc->mode);
00211        printf("\tgamma size     : %d\n", crtc->gamma_size);
00212 
00213        return 0;
00214 }
00215 
00216 int printFrameBuffer(int fd, drmModeResPtr res, drmModeFBPtr fb)
00217 {
00218        printf("Framebuffer\n");
00219        printf("\thandle    : %i\n", fb->handle);
00220        printf("\twidth     : %i\n", fb->width);
00221        printf("\theight    : %i\n", fb->height);
00222        printf("\tpitch     : %i\n", fb->pitch);;
00223        printf("\tbpp       : %i\n", fb->bpp);
00224        printf("\tdepth     : %i\n", fb->depth);
00225        printf("\tbuffer_id : %i\n", fb->handle);
00226 
00227        return 0;
00228 }
00229 
00230 int printRes(int fd, drmModeResPtr res)
00231 {
00232        int i;
00233        drmModeFBPtr fb;
00234        drmModeCrtcPtr crtc;
00235        drmModeEncoderPtr encoder;
00236        drmModeConnectorPtr connector;
00237 
00238        printf("Resources\n\n");
00239 
00240        printf("count_connectors : %i\n", res->count_connectors);
00241        printf("count_encoders   : %i\n", res->count_encoders);
00242        printf("count_crtcs      : %i\n", res->count_crtcs);
00243        printf("count_fbs        : %i\n", res->count_fbs);
00244 
00245        printf("\n");
00246 
00247        if (connectors) {
00248               for (i = 0; i < res->count_connectors; i++) {
00249                      connector = drmModeGetConnector(fd, res->connectors[i]);
00250 
00251                      if (!connector)
00252                             printf("Could not get connector %i\n", res->connectors[i]);
00253                      else {
00254                             printConnector(fd, res, connector, res->connectors[i]);
00255                             drmModeFreeConnector(connector);
00256                      }
00257               }
00258               printf("\n");
00259        }
00260 
00261 
00262        if (encoders) {
00263               for (i = 0; i < res->count_encoders; i++) {
00264                      encoder = drmModeGetEncoder(fd, res->encoders[i]);
00265 
00266                      if (!encoder)
00267                             printf("Could not get encoder %i\n", res->encoders[i]);
00268                      else {
00269                             printEncoder(fd, res, encoder, res->encoders[i]);
00270                             drmModeFreeEncoder(encoder);
00271                      }
00272               }
00273               printf("\n");
00274        }
00275 
00276        if (crtcs) {
00277               for (i = 0; i < res->count_crtcs; i++) {
00278                      crtc = drmModeGetCrtc(fd, res->crtcs[i]);
00279 
00280                      if (!crtc)
00281                             printf("Could not get crtc %i\n", res->crtcs[i]);
00282                      else {
00283                             printCrtc(fd, res, crtc, res->crtcs[i]);
00284                             drmModeFreeCrtc(crtc);
00285                      }
00286               }
00287               printf("\n");
00288        }
00289 
00290        if (fbs) {
00291               for (i = 0; i < res->count_fbs; i++) {
00292                      fb = drmModeGetFB(fd, res->fbs[i]);
00293 
00294                      if (!fb)
00295                             printf("Could not get fb %i\n", res->fbs[i]);
00296                      else {
00297                             printFrameBuffer(fd, res, fb);
00298                             drmModeFreeFB(fb);
00299                      }
00300               }
00301        }
00302 
00303        return 0;
00304 }
00305 
00306 void args(int argc, char **argv)
00307 {
00308        int i;
00309 
00310        fbs = 0;
00311        edid = 0;
00312        crtcs = 0;
00313        modes = 0;
00314        encoders = 0;
00315        full_modes = 0;
00316        full_props = 0;
00317        connectors = 0;
00318 
00319        module_name = argv[1];
00320 
00321        for (i = 2; i < argc; i++) {
00322               if (strcmp(argv[i], "-fb") == 0) {
00323                      fbs = 1;
00324               } else if (strcmp(argv[i], "-crtcs") == 0) {
00325                      crtcs = 1;
00326               } else if (strcmp(argv[i], "-cons") == 0) {
00327                      connectors = 1;
00328                      modes = 1;
00329               } else if (strcmp(argv[i], "-modes") == 0) {
00330                      connectors = 1;
00331                      modes = 1;
00332               } else if (strcmp(argv[i], "-full") == 0) {
00333                      connectors = 1;
00334                      modes = 1;
00335                      full_modes = 1;
00336               } else if (strcmp(argv[i], "-props") == 0) {
00337                      connectors = 1;
00338                      full_props = 1;
00339               } else if (strcmp(argv[i], "-edids") == 0) {
00340                      connectors = 1;
00341                      edid = 1;
00342               } else if (strcmp(argv[i], "-encoders") == 0) {
00343                      encoders = 1;
00344               } else if (strcmp(argv[i], "-v") == 0) {
00345                      fbs = 1;
00346                      edid = 1;
00347                      crtcs = 1;
00348                      modes = 1;
00349                      encoders = 1;
00350                      full_modes = 1;
00351                      full_props = 1;
00352                      connectors = 1;
00353               }
00354        }
00355 
00356        if (argc == 2) {
00357               fbs = 1;
00358               edid = 1;
00359               crtcs = 1;
00360               modes = 1;
00361               encoders = 1;
00362               full_modes = 0;
00363               full_props = 0;
00364               connectors = 1;
00365        }
00366 }
00367 
00368 int main(int argc, char **argv)
00369 {
00370        int fd;
00371        drmModeResPtr res;
00372 
00373        if (argc == 1) {
00374               printf("Please add modulename as first argument\n");
00375               return 1;
00376        }
00377 
00378        args(argc, argv);
00379 
00380        printf("Starting test\n");
00381 
00382        fd = drmOpen(module_name, NULL);
00383 
00384        if (fd < 0) {
00385               printf("Failed to open the card fd (%d)\n",fd);
00386               return 1;
00387        }
00388 
00389        res = drmModeGetResources(fd);
00390        if (res == 0) {
00391               printf("Failed to get resources from card\n");
00392               drmClose(fd);
00393               return 1;
00394        }
00395 
00396        printRes(fd, res);
00397 
00398        drmModeFreeResources(res);
00399 
00400        printf("Ok\n");
00401 
00402        return 0;
00403 }