Home | History | Annotate | Download | only in kms
      1 /*
      2  * Copyright  2014 NVIDIA Corporation
      3  *
      4  * Permission is hereby granted, free of charge, to any person obtaining a
      5  * copy of this software and associated documentation files (the "Software"),
      6  * to deal in the Software without restriction, including without limitation
      7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
      8  * and/or sell copies of the Software, and to permit persons to whom the
      9  * Software is furnished to do so, subject to the following conditions:
     10  *
     11  * The above copyright notice and this permission notice (including the next
     12  * paragraph) shall be included in all copies or substantial portions of the
     13  * Software.
     14  *
     15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
     21  * IN THE SOFTWARE.
     22  */
     23 
     24 #include <errno.h>
     25 #include <fcntl.h>
     26 #include <signal.h>
     27 #include <stdio.h>
     28 #include <stdint.h>
     29 #include <string.h>
     30 #include <unistd.h>
     31 #ifdef HAVE_SYS_SELECT_H
     32 #include <sys/select.h>
     33 #endif
     34 
     35 #include <drm_fourcc.h>
     36 
     37 #include "util/pattern.h"
     38 #include "libkms-test.h"
     39 
     40 static void signal_handler(int signum)
     41 {
     42 }
     43 
     44 int main(int argc, char *argv[])
     45 {
     46 	struct kms_framebuffer *fb;
     47 	struct kms_screen *screen;
     48 	struct kms_device *device;
     49 	unsigned int index = 0;
     50 	struct sigaction sa;
     51 	int fd, err;
     52 	void *ptr;
     53 
     54 	if (argc < 2) {
     55 		fprintf(stderr, "usage: %s DEVICE\n", argv[0]);
     56 		return 1;
     57 	}
     58 
     59 	memset(&sa, 0, sizeof(sa));
     60 	sa.sa_handler = signal_handler;
     61 
     62 	err = sigaction(SIGINT, &sa, NULL);
     63 	if (err < 0) {
     64 		fprintf(stderr, "sigaction() failed: %m\n");
     65 		return 1;
     66 	}
     67 
     68 	fd = open(argv[1], O_RDWR);
     69 	if (fd < 0) {
     70 		fprintf(stderr, "open() failed: %m\n");
     71 		return 1;
     72 	}
     73 
     74 	device = kms_device_open(fd);
     75 	if (!device) {
     76 		fprintf(stderr, "kms_device_open() failed: %m\n");
     77 		return 1;
     78 	}
     79 
     80 	if (device->num_screens < 1) {
     81 		fprintf(stderr, "no screens found\n");
     82 		kms_device_close(device);
     83 		close(fd);
     84 		return 1;
     85 	}
     86 
     87 	/* TODO: allow command-line to override */
     88 	screen = device->screens[0];
     89 
     90 	printf("Using screen %s, resolution %ux%u\n", screen->name,
     91 	       screen->width, screen->height);
     92 
     93 	fb = kms_framebuffer_create(device, screen->width, screen->height,
     94 				    DRM_FORMAT_XRGB8888);
     95 	if (!fb) {
     96 		fprintf(stderr, "kms_framebuffer_create() failed\n");
     97 		return 1;
     98 	}
     99 
    100 	err = kms_framebuffer_map(fb, &ptr);
    101 	if (err < 0) {
    102 		fprintf(stderr, "kms_framebuffer_map() failed: %d\n", err);
    103 		return 1;
    104 	}
    105 
    106 	util_fill_pattern(fb->format, UTIL_PATTERN_SMPTE, &ptr, fb->width,
    107 			  fb->height, fb->pitch);
    108 
    109 	kms_framebuffer_unmap(fb);
    110 
    111 	err = kms_screen_set(screen, device->crtcs[index++], fb);
    112 	if (err < 0) {
    113 		fprintf(stderr, "kms_screen_set() failed: %d\n", err);
    114 		return 1;
    115 	}
    116 
    117 	while (true) {
    118 		int nfds = STDIN_FILENO + 1;
    119 		struct timeval timeout;
    120 		fd_set fds;
    121 
    122 		memset(&timeout, 0, sizeof(timeout));
    123 		timeout.tv_sec = 5;
    124 		timeout.tv_usec = 0;
    125 
    126 		FD_ZERO(&fds);
    127 		FD_SET(STDIN_FILENO, &fds);
    128 
    129 		err = select(nfds, &fds, NULL, NULL, &timeout);
    130 		if (err < 0) {
    131 			if (errno == EINTR)
    132 				break;
    133 
    134 			fprintf(stderr, "select() failed: %d\n", errno);
    135 			break;
    136 		}
    137 
    138 		if (err > 0) {
    139 			if (FD_ISSET(STDIN_FILENO, &fds))
    140 				break;
    141 		}
    142 
    143 		/* switch CRTC */
    144 		if (index >= device->num_crtcs)
    145 			index = 0;
    146 
    147 		err = kms_screen_set(screen, device->crtcs[index], fb);
    148 		if (err < 0) {
    149 			fprintf(stderr, "kms_screen_set() failed: %d\n", err);
    150 			break;
    151 		}
    152 
    153 		index++;
    154 	}
    155 
    156 	kms_framebuffer_free(fb);
    157 	kms_device_close(device);
    158 	close(fd);
    159 
    160 	return 0;
    161 }
    162