gbm-sdl2-example

annotate src/drmtest.cc @ 0:665119a0580e

example program that uses gbm buffers
author Eleni Maria Stea <eleni@mutantstargoat.com>
date Wed, 04 Jul 2018 22:16:23 +0300
parents
children
rev   line source
eleni@0 1 /*
eleni@0 2 * Copyright © 2007 Intel Corporation
eleni@0 3 *
eleni@0 4 * Permission is hereby granted, free of charge, to any person obtaining a
eleni@0 5 * copy of this software and associated documentation files (the "Software"),
eleni@0 6 * to deal in the Software without restriction, including without limitation
eleni@0 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
eleni@0 8 * and/or sell copies of the Software, and to permit persons to whom the
eleni@0 9 * Software is furnished to do so, subject to the following conditions:
eleni@0 10 *
eleni@0 11 * The above copyright notice and this permission notice (including the next
eleni@0 12 * paragraph) shall be included in all copies or substantial portions of the
eleni@0 13 * Software.
eleni@0 14 *
eleni@0 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
eleni@0 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
eleni@0 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
eleni@0 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
eleni@0 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
eleni@0 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
eleni@0 21 * IN THE SOFTWARE.
eleni@0 22 *
eleni@0 23 * Authors:
eleni@0 24 * Eric Anholt <eric@anholt.net>
eleni@0 25 *
eleni@0 26 */
eleni@0 27
eleni@0 28 #include <string.h>
eleni@0 29
eleni@0 30 #include <fcntl.h>
eleni@0 31 #include <fnmatch.h>
eleni@0 32 #include <sys/stat.h>
eleni@0 33 #include <sys/ioctl.h>
eleni@0 34 #include "drmtest.h"
eleni@0 35
eleni@0 36 #define LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE
eleni@0 37 #include <libudev.h>
eleni@0 38
eleni@0 39 static int is_master(int fd)
eleni@0 40 {
eleni@0 41 drm_client_t client;
eleni@0 42 int ret;
eleni@0 43
eleni@0 44 /* Check that we're the only opener and authed. */
eleni@0 45 client.idx = 0;
eleni@0 46 ret = ioctl(fd, DRM_IOCTL_GET_CLIENT, &client);
eleni@0 47 assert (ret == 0);
eleni@0 48 if (!client.auth)
eleni@0 49 return 0;
eleni@0 50 client.idx = 1;
eleni@0 51 ret = ioctl(fd, DRM_IOCTL_GET_CLIENT, &client);
eleni@0 52 if (ret != -1 || errno != EINVAL)
eleni@0 53 return 0;
eleni@0 54
eleni@0 55 return 1;
eleni@0 56 }
eleni@0 57
eleni@0 58 /** Open the first DRM device matching the criteria */
eleni@0 59 int drm_open_matching(const char *pci_glob, int flags)
eleni@0 60 {
eleni@0 61 struct udev *udev;
eleni@0 62 struct udev_enumerate *e;
eleni@0 63 struct udev_device *device, *parent;
eleni@0 64 struct udev_list_entry *entry;
eleni@0 65 const char *pci_id, *path;
eleni@0 66 int fd;
eleni@0 67
eleni@0 68 udev = udev_new();
eleni@0 69 if (udev == NULL) {
eleni@0 70 fprintf(stderr, "failed to initialize udev context\n");
eleni@0 71 abort();
eleni@0 72 }
eleni@0 73
eleni@0 74 fd = -1;
eleni@0 75 e = udev_enumerate_new(udev);
eleni@0 76 udev_enumerate_add_match_subsystem(e, "drm");
eleni@0 77 udev_enumerate_scan_devices(e);
eleni@0 78 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
eleni@0 79 path = udev_list_entry_get_name(entry);
eleni@0 80 device = udev_device_new_from_syspath(udev, path);
eleni@0 81 parent = udev_device_get_parent(device);
eleni@0 82 /* Filter out KMS output devices. */
eleni@0 83 if (strcmp(udev_device_get_subsystem(parent), "pci") != 0)
eleni@0 84 continue;
eleni@0 85 pci_id = udev_device_get_property_value(parent, "PCI_ID");
eleni@0 86 if (fnmatch(pci_glob, pci_id, 0) != 0)
eleni@0 87 continue;
eleni@0 88 fd = open(udev_device_get_devnode(device), O_RDWR);
eleni@0 89 if (fd < 0)
eleni@0 90 continue;
eleni@0 91 if ((flags & DRM_TEST_MASTER) && !is_master(fd)) {
eleni@0 92 close(fd);
eleni@0 93 fd = -1;
eleni@0 94 continue;
eleni@0 95 }
eleni@0 96
eleni@0 97 break;
eleni@0 98 }
eleni@0 99 udev_enumerate_unref(e);
eleni@0 100 udev_unref(udev);
eleni@0 101
eleni@0 102 return fd;
eleni@0 103 }
eleni@0 104
eleni@0 105 int drm_open_any(void)
eleni@0 106 {
eleni@0 107 int fd = drm_open_matching("*:*", 0);
eleni@0 108
eleni@0 109 if (fd < 0) {
eleni@0 110 fprintf(stderr, "failed to open any drm device\n");
eleni@0 111 abort();
eleni@0 112 }
eleni@0 113
eleni@0 114 return fd;
eleni@0 115 }
eleni@0 116
eleni@0 117 /**
eleni@0 118 * Open the first DRM device we can find where we end up being the master.
eleni@0 119 */
eleni@0 120 int drm_open_any_master(void)
eleni@0 121 {
eleni@0 122 int fd = drm_open_matching("*:*", DRM_TEST_MASTER);
eleni@0 123
eleni@0 124 if (fd < 0) {
eleni@0 125 fprintf(stderr, "failed to open any drm device\n");
eleni@0 126 abort();
eleni@0 127 }
eleni@0 128
eleni@0 129 return fd;
eleni@0 130
eleni@0 131 }