input_hal branch
[staging/toyota.git] / src / input_drm.cpp
1 /*
2  * @copyright Copyright (c) 2018-2020 TOYOTA MOTOR CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include "input_drm.h"
17
18 #include <fcntl.h>
19 #include <sys/stat.h>
20 #include <sys/types.h>
21 #include <unistd.h>
22
23 #if defined(_USE_DRM)
24 #include <xf86drm.h>
25 #include <xf86drmMode.h>
26 #endif
27
28 #include "input_hal.h"
29 #include "input_hal_debug.h"
30
31 #define DIR_PATH         "/dev/dri/card0"
32 #define DEFAULT_RESOLUTION_HORIZONTAL   1280       /* Horizontal resolution default value */
33 #define DEFAULT_RESOLUTION_VERTICAL     800        /* Vertical resolution default value */
34 /**
35  * Panel resolution acquisition / GetPanelSpecResolutionInput
36  */
37 int GetPanelSpecResolutionInput(int *reso_h, int *reso_v) {
38   if ((NULL == reso_h) || (NULL == reso_v)) {
39     return HAL_INPUT_RET_ERROR;
40   }
41
42 #if defined(_USE_DRM)
43   int  fd;
44   drmModeRes *resources = NULL;
45   drmModeConnector *connector = NULL;
46   drmModeEncoder *encoder = NULL;
47   drmModeCrtc *crtc = NULL;
48
49   fd = open(DIR_PATH, O_RDWR);
50   if (fd < 0) {
51     INPUT_ERROR_LOG("DRI Open Error=%s\n", DIR_PATH);
52     goto err_rtn;
53   }
54
55   resources = drmModeGetResources(fd);
56   if (!resources) {
57     INPUT_ERROR_LOG("No resources\n");
58     goto err_rtn;
59   }
60   if (2 > resources->count_connectors) {
61     INPUT_ERROR_LOG("DRI Connect Num Error connectors=%d\n",
62                       resources->count_connectors);
63     goto err_rtn;
64   }
65
66   connector = drmModeGetConnector(fd, resources->connectors[1]);
67   if (!connector) {
68     INPUT_ERROR_LOG("No Connector\n");
69     goto err_rtn;
70   }
71
72   if ((DRM_MODE_CONNECTED == connector->connection) &&(connector->count_modes > 0)) {
73   } else {
74     INPUT_ERROR_LOG("Not found connected connector\n");
75     goto err_rtn;
76   }
77
78   encoder = drmModeGetEncoder(fd, connector->encoder_id);
79   if (!encoder) {
80     INPUT_ERROR_LOG("drmModeGetEncoder null\n");
81     goto err_rtn;
82   }
83
84   crtc = drmModeGetCrtc(fd, encoder->crtc_id);
85   if (!crtc) {
86     INPUT_ERROR_LOG("drmModeGetCrtc null\n");
87     goto err_rtn;
88   }
89
90   *reso_h  = crtc->mode.hdisplay;
91   *reso_v = crtc->mode.vdisplay;
92
93   drmModeFreeCrtc(crtc);
94   drmModeFreeEncoder(encoder);
95   drmModeFreeConnector(connector);
96   drmModeFreeResources(resources);
97   close(fd);
98
99   INPUT_LOG_TRACE("width=%d height=%d\n", *reso_h, *reso_v);
100   return HAL_INPUT_RET_NORMAL;
101
102 err_rtn:
103
104   if (encoder) {
105     drmModeFreeEncoder(encoder);
106   }
107   if (connector) {
108     drmModeFreeConnector(connector);
109   }
110   if (resources) {
111     drmModeFreeResources(resources);
112   }
113   if (fd >= 0) {
114     close(fd);
115   }
116
117   INPUT_ERROR_LOG("Use Default Resolution\n");
118   *reso_h =  DEFAULT_RESOLUTION_HORIZONTAL;
119   *reso_v  =  DEFAULT_RESOLUTION_VERTICAL;
120
121   return HAL_INPUT_RET_ERROR;
122 #else
123   *reso_h  =  DEFAULT_RESOLUTION_HORIZONTAL;
124   *reso_v  =  DEFAULT_RESOLUTION_VERTICAL;
125
126   return HAL_INPUT_RET_NORMAL;
127 #endif
128 }