gbm-sdl2-example
diff 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 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/drmtest.cc Wed Jul 04 22:16:23 2018 +0300 1.3 @@ -0,0 +1,131 @@ 1.4 +/* 1.5 + * Copyright © 2007 Intel Corporation 1.6 + * 1.7 + * Permission is hereby granted, free of charge, to any person obtaining a 1.8 + * copy of this software and associated documentation files (the "Software"), 1.9 + * to deal in the Software without restriction, including without limitation 1.10 + * the rights to use, copy, modify, merge, publish, distribute, sublicense, 1.11 + * and/or sell copies of the Software, and to permit persons to whom the 1.12 + * Software is furnished to do so, subject to the following conditions: 1.13 + * 1.14 + * The above copyright notice and this permission notice (including the next 1.15 + * paragraph) shall be included in all copies or substantial portions of the 1.16 + * Software. 1.17 + * 1.18 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1.19 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1.20 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 1.21 + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 1.22 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 1.23 + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 1.24 + * IN THE SOFTWARE. 1.25 + * 1.26 + * Authors: 1.27 + * Eric Anholt <eric@anholt.net> 1.28 + * 1.29 + */ 1.30 + 1.31 +#include <string.h> 1.32 + 1.33 +#include <fcntl.h> 1.34 +#include <fnmatch.h> 1.35 +#include <sys/stat.h> 1.36 +#include <sys/ioctl.h> 1.37 +#include "drmtest.h" 1.38 + 1.39 +#define LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE 1.40 +#include <libudev.h> 1.41 + 1.42 +static int is_master(int fd) 1.43 +{ 1.44 + drm_client_t client; 1.45 + int ret; 1.46 + 1.47 + /* Check that we're the only opener and authed. */ 1.48 + client.idx = 0; 1.49 + ret = ioctl(fd, DRM_IOCTL_GET_CLIENT, &client); 1.50 + assert (ret == 0); 1.51 + if (!client.auth) 1.52 + return 0; 1.53 + client.idx = 1; 1.54 + ret = ioctl(fd, DRM_IOCTL_GET_CLIENT, &client); 1.55 + if (ret != -1 || errno != EINVAL) 1.56 + return 0; 1.57 + 1.58 + return 1; 1.59 +} 1.60 + 1.61 +/** Open the first DRM device matching the criteria */ 1.62 +int drm_open_matching(const char *pci_glob, int flags) 1.63 +{ 1.64 + struct udev *udev; 1.65 + struct udev_enumerate *e; 1.66 + struct udev_device *device, *parent; 1.67 + struct udev_list_entry *entry; 1.68 + const char *pci_id, *path; 1.69 + int fd; 1.70 + 1.71 + udev = udev_new(); 1.72 + if (udev == NULL) { 1.73 + fprintf(stderr, "failed to initialize udev context\n"); 1.74 + abort(); 1.75 + } 1.76 + 1.77 + fd = -1; 1.78 + e = udev_enumerate_new(udev); 1.79 + udev_enumerate_add_match_subsystem(e, "drm"); 1.80 + udev_enumerate_scan_devices(e); 1.81 + udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) { 1.82 + path = udev_list_entry_get_name(entry); 1.83 + device = udev_device_new_from_syspath(udev, path); 1.84 + parent = udev_device_get_parent(device); 1.85 + /* Filter out KMS output devices. */ 1.86 + if (strcmp(udev_device_get_subsystem(parent), "pci") != 0) 1.87 + continue; 1.88 + pci_id = udev_device_get_property_value(parent, "PCI_ID"); 1.89 + if (fnmatch(pci_glob, pci_id, 0) != 0) 1.90 + continue; 1.91 + fd = open(udev_device_get_devnode(device), O_RDWR); 1.92 + if (fd < 0) 1.93 + continue; 1.94 + if ((flags & DRM_TEST_MASTER) && !is_master(fd)) { 1.95 + close(fd); 1.96 + fd = -1; 1.97 + continue; 1.98 + } 1.99 + 1.100 + break; 1.101 + } 1.102 + udev_enumerate_unref(e); 1.103 + udev_unref(udev); 1.104 + 1.105 + return fd; 1.106 +} 1.107 + 1.108 +int drm_open_any(void) 1.109 +{ 1.110 + int fd = drm_open_matching("*:*", 0); 1.111 + 1.112 + if (fd < 0) { 1.113 + fprintf(stderr, "failed to open any drm device\n"); 1.114 + abort(); 1.115 + } 1.116 + 1.117 + return fd; 1.118 +} 1.119 + 1.120 +/** 1.121 + * Open the first DRM device we can find where we end up being the master. 1.122 + */ 1.123 +int drm_open_any_master(void) 1.124 +{ 1.125 + int fd = drm_open_matching("*:*", DRM_TEST_MASTER); 1.126 + 1.127 + if (fd < 0) { 1.128 + fprintf(stderr, "failed to open any drm device\n"); 1.129 + abort(); 1.130 + } 1.131 + 1.132 + return fd; 1.133 + 1.134 +}