summaryrefslogtreecommitdiffstats
path: root/input_hal/src/input_drm.cpp
blob: 20adf79997668ec063679118d0c40c5edbe6a895 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * @copyright Copyright (c) 2018-2020 TOYOTA MOTOR CORPORATION.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "input_drm.h"

#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#if defined(_USE_DRM)
#include <xf86drm.h>
#include <xf86drmMode.h>
#endif

#include "input_hal.h"
#include "input_hal_debug.h"

#define DIR_PATH         "/dev/dri/card0"
#define DEFAULT_RESOLUTION_HORIZONTAL   1280       /* Horizontal resolution default value */
#define DEFAULT_RESOLUTION_VERTICAL     800        /* Vertical resolution default value */
/**
 * Panel resolution acquisition / GetPanelSpecResolutionInput
 */
int GetPanelSpecResolutionInput(int *reso_h, int *reso_v) {
  if ((NULL == reso_h) || (NULL == reso_v)) {
    return HAL_INPUT_RET_ERROR;
  }

#if defined(_USE_DRM)
  int  fd;
  drmModeRes *resources = NULL;
  drmModeConnector *connector = NULL;
  drmModeEncoder *encoder = NULL;
  drmModeCrtc *crtc = NULL;

  fd = open(DIR_PATH, O_RDWR);
  if (fd < 0) {
    INPUT_ERROR_LOG("DRI Open Error=%s\n", DIR_PATH);
    goto err_rtn;
  }

  resources = drmModeGetResources(fd);
  if (!resources) {
    INPUT_ERROR_LOG("No resources\n");
    goto err_rtn;
  }
  if (2 > resources->count_connectors) {
    INPUT_ERROR_LOG("DRI Connect Num Error connectors=%d\n",
                      resources->count_connectors);
    goto err_rtn;
  }

  connector = drmModeGetConnector(fd, resources->connectors[1]);
  if (!connector) {
    INPUT_ERROR_LOG("No Connector\n");
    goto err_rtn;
  }

  if ((DRM_MODE_CONNECTED == connector->connection) &&(connector->count_modes > 0)) {
  } else {
    INPUT_ERROR_LOG("Not found connected connector\n");
    goto err_rtn;
  }

  encoder = drmModeGetEncoder(fd, connector->encoder_id);
  if (!encoder) {
    INPUT_ERROR_LOG("drmModeGetEncoder null\n");
    goto err_rtn;
  }

  crtc = drmModeGetCrtc(fd, encoder->crtc_id);
  if (!crtc) {
    INPUT_ERROR_LOG("drmModeGetCrtc null\n");
    goto err_rtn;
  }

  *reso_h  = crtc->mode.hdisplay;
  *reso_v = crtc->mode.vdisplay;

  drmModeFreeCrtc(crtc);
  drmModeFreeEncoder(encoder);
  drmModeFreeConnector(connector);
  drmModeFreeResources(resources);
  close(fd);

  INPUT_LOG_TRACE("width=%d height=%d\n", *reso_h, *reso_v);
  return HAL_INPUT_RET_NORMAL;

err_rtn:

  if (encoder) {
    drmModeFreeEncoder(encoder);
  }
  if (connector) {
    drmModeFreeConnector(connector);
  }
  if (resources) {
    drmModeFreeResources(resources);
  }
  if (fd >= 0) {
    close(fd);
  }

  INPUT_ERROR_LOG("Use Default Resolution\n");
  *reso_h =  DEFAULT_RESOLUTION_HORIZONTAL;
  *reso_v  =  DEFAULT_RESOLUTION_VERTICAL;

  return HAL_INPUT_RET_ERROR;
#else
  *reso_h  =  DEFAULT_RESOLUTION_HORIZONTAL;
  *reso_v  =  DEFAULT_RESOLUTION_VERTICAL;

  return HAL_INPUT_RET_NORMAL;
#endif
}