Home | History | Annotate | Line # | Download | only in util
      1 /*
      2  * Copyright 2008 Tungsten Graphics
      3  *   Jakob Bornecrantz <jakob (at) tungstengraphics.com>
      4  * Copyright 2008 Intel Corporation
      5  *   Jesse Barnes <jesse.barnes (at) intel.com>
      6  *
      7  * Permission is hereby granted, free of charge, to any person obtaining a
      8  * copy of this software and associated documentation files (the "Software"),
      9  * to deal in the Software without restriction, including without limitation
     10  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     11  * and/or sell copies of the Software, and to permit persons to whom the
     12  * Software is furnished to do so, subject to the following conditions:
     13  *
     14  * The above copyright notice and this permission notice shall be included in
     15  * all copies or substantial portions of the Software.
     16  *
     17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     22  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
     23  * IN THE SOFTWARE.
     24  */
     25 
     26 /*
     27  * This fairly simple test program dumps output in a similar format to the
     28  * "xrandr" tool everyone knows & loves.  It's necessarily slightly different
     29  * since the kernel separates outputs into encoder and connector structures,
     30  * each with their own unique ID.  The program also allows test testing of the
     31  * memory management and mode setting APIs by allowing the user to specify a
     32  * connector and mode to use for mode setting.  If all works as expected, a
     33  * blue background should be painted on the monitor attached to the specified
     34  * connector after the selected mode is set.
     35  *
     36  * TODO: use cairo to write the mode info on the selected output once
     37  *       the mode has been programmed, along with possible test patterns.
     38  */
     39 
     40 #include <errno.h>
     41 #include <stdint.h>
     42 #include <stdio.h>
     43 #include <stdlib.h>
     44 #include <string.h>
     45 #include <fcntl.h>
     46 #include <unistd.h>
     47 
     48 #include "xf86drm.h"
     49 #include "xf86drmMode.h"
     50 
     51 #include "common.h"
     52 
     53 struct type_name {
     54 	unsigned int type;
     55 	const char *name;
     56 };
     57 
     58 static const char *util_lookup_type_name(unsigned int type,
     59 					 const struct type_name *table,
     60 					 unsigned int count)
     61 {
     62 	unsigned int i;
     63 
     64 	for (i = 0; i < count; i++)
     65 		if (table[i].type == type)
     66 			return table[i].name;
     67 
     68 	return NULL;
     69 }
     70 
     71 static const struct type_name encoder_type_names[] = {
     72 	{ DRM_MODE_ENCODER_NONE, "none" },
     73 	{ DRM_MODE_ENCODER_DAC, "DAC" },
     74 	{ DRM_MODE_ENCODER_TMDS, "TMDS" },
     75 	{ DRM_MODE_ENCODER_LVDS, "LVDS" },
     76 	{ DRM_MODE_ENCODER_TVDAC, "TVDAC" },
     77 	{ DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
     78 	{ DRM_MODE_ENCODER_DSI, "DSI" },
     79 	{ DRM_MODE_ENCODER_DPMST, "DPMST" },
     80 	{ DRM_MODE_ENCODER_DPI, "DPI" },
     81 };
     82 
     83 const char *util_lookup_encoder_type_name(unsigned int type)
     84 {
     85 	return util_lookup_type_name(type, encoder_type_names,
     86 				     ARRAY_SIZE(encoder_type_names));
     87 }
     88 
     89 static const struct type_name connector_status_names[] = {
     90 	{ DRM_MODE_CONNECTED, "connected" },
     91 	{ DRM_MODE_DISCONNECTED, "disconnected" },
     92 	{ DRM_MODE_UNKNOWNCONNECTION, "unknown" },
     93 };
     94 
     95 const char *util_lookup_connector_status_name(unsigned int status)
     96 {
     97 	return util_lookup_type_name(status, connector_status_names,
     98 				     ARRAY_SIZE(connector_status_names));
     99 }
    100 
    101 int util_open(const char *device, const char *module)
    102 {
    103 	int fd = -1;
    104 	drmVersionPtr version;
    105 
    106 	if (module || device) {
    107 		fd = drmOpen(module, device);
    108 		if (fd < 0) {
    109 			fprintf(stderr, "failed to open device '%s' with busid '%s': %s\n",
    110 				module, device, strerror(errno));
    111 			return -errno;
    112 		}
    113 	} else {
    114 		unsigned int i;
    115 		drmDevicePtr devices[64];
    116 		int num_devices = drmGetDevices2(0, devices, ARRAY_SIZE(devices));
    117 		if (num_devices < 0) {
    118 			fprintf(stderr, "drmGetDevices2() failed with %s\n", strerror(num_devices));
    119 			return num_devices;
    120 		}
    121 
    122 		for (i = 0; i < num_devices; i++) {
    123 			drmDevicePtr device = devices[i];
    124 			// Select only primary nodes
    125 			if ((device->available_nodes & 1 << DRM_NODE_PRIMARY) == 0)
    126 				continue;
    127 
    128 			printf("trying to open device '%s'... ", device->nodes[DRM_NODE_PRIMARY]);
    129 			fd = open(device->nodes[DRM_NODE_PRIMARY], O_RDWR | O_CLOEXEC);
    130 
    131 			if (fd < 0) {
    132 				printf("failed\n");
    133 			} else if (!drmIsKMS(fd)) {
    134 				printf("is not a KMS device\n");
    135 				close(fd);
    136 				fd = -1;
    137 			} else {
    138 				printf("done\n");
    139 				break;
    140 			}
    141 		}
    142 
    143 		if (fd < 0) {
    144 			fprintf(stderr, "no device found\n");
    145 			return -ENODEV;
    146 		}
    147 	}
    148 
    149 	version = drmGetVersion(fd);
    150 	printf("opened device `%s` on driver `%s` (version %d.%d.%d at %s)\n",
    151 	       version->desc,
    152 	       version->name,
    153 	       version->version_major,
    154 	       version->version_minor,
    155 	       version->version_patchlevel,
    156 	       version->date);
    157 	drmFreeVersion(version);
    158 
    159 	return fd;
    160 }
    161