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 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <errno.h>
29 #include <string.h>
30
31 #include "libkms-test.h"
32
kms_screen_probe(struct kms_screen * screen)33 static void kms_screen_probe(struct kms_screen *screen)
34 {
35 struct kms_device *device = screen->device;
36 drmModeConnector *con;
37
38 con = drmModeGetConnector(device->fd, screen->id);
39 if (!con)
40 return;
41
42 screen->type = con->connector_type;
43
44 if (con->connection == DRM_MODE_CONNECTED)
45 screen->connected = true;
46 else
47 screen->connected = false;
48
49 memcpy(&screen->mode, &con->modes[0], sizeof(drmModeModeInfo));
50 screen->width = screen->mode.hdisplay;
51 screen->height = screen->mode.vdisplay;
52
53 drmModeFreeConnector(con);
54 }
55
kms_screen_create(struct kms_device * device,uint32_t id)56 struct kms_screen *kms_screen_create(struct kms_device *device, uint32_t id)
57 {
58 struct kms_screen *screen;
59
60 screen = calloc(1, sizeof(*screen));
61 if (!screen)
62 return NULL;
63
64 screen->device = device;
65 screen->id = id;
66
67 kms_screen_probe(screen);
68
69 return screen;
70 }
71
kms_screen_free(struct kms_screen * screen)72 void kms_screen_free(struct kms_screen *screen)
73 {
74 if (screen)
75 free(screen->name);
76
77 free(screen);
78 }
79
kms_screen_set(struct kms_screen * screen,struct kms_crtc * crtc,struct kms_framebuffer * fb)80 int kms_screen_set(struct kms_screen *screen, struct kms_crtc *crtc,
81 struct kms_framebuffer *fb)
82 {
83 struct kms_device *device = screen->device;
84 int err;
85
86 err = drmModeSetCrtc(device->fd, crtc->id, fb->id, 0, 0, &screen->id,
87 1, &screen->mode);
88 if (err < 0)
89 return -errno;
90
91 return 0;
92 }
93