Add initial version
[src/drm-lease-manager.git] / examples / dlm-client-test / dlm-client-test.c
1 /* Copyright 2020-2021 IGEL Co., Ltd.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include "dlmclient.h"
17
18 #include <errno.h>
19 #include <stdint.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include <xf86drmMode.h>
25
26 static void usage(const char *name)
27 {
28         fprintf(stderr,
29                 "%s <lease name>\n"
30                 "\tlease name: Name of lease to check\n",
31                 name);
32 }
33
34 static void dump_lease_resources(int lease_fd)
35 {
36         drmModeObjectListPtr ol = drmModeGetLease(lease_fd);
37         if (!ol) {
38                 fprintf(
39                     stderr,
40                     "drmModeGetLease failed. Received fd is not a DRM lease\n");
41                 return;
42         }
43         free(ol);
44
45         drmModeRes *res = drmModeGetResources(lease_fd);
46         if (!res) {
47                 fprintf(stderr, "drmModeGetResources failed\n");
48                 return;
49         }
50
51         for (int i = 0; i < res->count_crtcs; i++)
52                 printf("crtc-id: %u\n", res->crtcs[i]);
53
54         for (int i = 0; i < res->count_connectors; i++)
55                 printf("connector-id: %u\n", res->connectors[i]);
56
57         drmModeFreeResources(res);
58
59         drmModePlaneRes *plane_res = drmModeGetPlaneResources(lease_fd);
60         if (!plane_res) {
61                 fprintf(stderr, "drmModeGetPlaneResources failed\n");
62                 return;
63         }
64
65         for (uint32_t i = 0; i < plane_res->count_planes; i++)
66                 printf("plane-id: %u\n", plane_res->planes[i]);
67
68         drmModeFreePlaneResources(plane_res);
69 }
70
71 int main(int argc, char **argv)
72 {
73         if (argc < 2) {
74                 usage(argv[0]);
75                 return EXIT_FAILURE;
76         }
77
78         struct dlm_lease *lease = dlm_get_lease(argv[1]);
79         if (!lease) {
80                 fprintf(stderr, "dlm_get_lease: %s\n", strerror(errno));
81                 return EXIT_FAILURE;
82         }
83
84         int lease_fd = dlm_lease_fd(lease);
85         if (lease_fd < 0) {
86                 fprintf(stderr, "dlm_lease_fd: %s\n", strerror(errno));
87                 dlm_release_lease(lease);
88                 return EXIT_FAILURE;
89         }
90
91         dump_lease_resources(lease_fd);
92         dlm_release_lease(lease);
93         return EXIT_SUCCESS;
94 }