1 /*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <stdlib.h>
33 #include <unistd.h>
34
35 #include "hdf_log.h"
36 #include "input_manager.h"
37 #include "los_compiler.h"
38 #include "test_demo.h"
39
40 #define DEV_INDEX 1
41
42 #define INPUT_CHECK_NULL_POINTER(pointer, ret) \
43 do { \
44 if ((pointer) == NULL) { \
45 HDF_LOGE("%s: null pointer", __func__); \
46 } \
47 } while (0)
48
49 IInputInterface *g_inputInterface;
50 InputEventCb g_callback;
51
52 // Mouse Para
53 #define MOUSEWIDTH 12
54 #define MOUSEHIGH 18
55 #define SCREENHIGH 480
56 #define SCREENWIDTH 800
57
58 enum {
59 MOUSE_EVENT_TYPE_CLICK_LEFT = 1,
60 MOUSE_EVENT_TYPE_CLICK_RIGHT = 2,
61 MOUSE_EVENT_TYPE_MOVE = 3,
62 };
63
64 static int mousePosX = 400;
65 static int mousePosY = 240;
66 static int clickEventFlag = 0;
67 extern DisplayTest g_displayTest;
68
ShowGreenScreen(void)69 void ShowGreenScreen(void)
70 {
71 int x = 0;
72 int y = 0;
73
74 uint16_t *pBuf = (uint16_t *)g_displayTest.buffer.data.virAddr;
75
76 for (y = 0; y < SCREENHIGH; y++) {
77 for (x = 0; x < SCREENWIDTH; x++) {
78 *(((int *)pBuf) + SCREENWIDTH * y + x) = 0xFF00;
79 }
80 }
81 }
82
ShowBlueButton(void)83 void ShowBlueButton(void)
84 {
85 int x = 0;
86 int y = 0;
87
88 uint16_t *pBuf = (uint16_t *)g_displayTest.buffer.data.virAddr;
89
90 for (y = 100; y < 140; y++) {
91 for (x = 200; x < 280; x++) {
92 *(((int *)pBuf) + SCREENWIDTH * y + x) = 0xFF0000;
93 }
94 }
95 }
96
ShowClickEvent(void)97 void ShowClickEvent(void)
98 {
99 int x = 0;
100 int y = 0;
101
102 uint16_t *pBuf = (uint16_t *)g_displayTest.buffer.data.virAddr;
103
104 for (y = 200; y < 280; y++) {
105 for (x = 400; x < 480; x++) {
106 *(((int *)pBuf) + SCREENWIDTH * y + x) = 0xFF;
107 }
108 }
109 }
110
ShowMouse(int mouseX,int mouseY)111 void ShowMouse(int mouseX, int mouseY)
112 {
113 int x = 0;
114 int y = 0;
115 int i = 0;
116 int ret = 0;
117
118 uint16_t *pBuf = (uint16_t *)g_displayTest.buffer.data.virAddr;
119
120 ShowGreenScreen();
121
122 ShowBlueButton();
123
124 if (clickEventFlag == 1) {
125 ShowClickEvent();
126 }
127
128 for (y = mouseY; y < mouseY + MOUSEHIGH; y++) {
129 i++;
130 if (i < 12) {
131 for (x = mouseX; x < mouseX + i; x++) {
132 *(((int *)pBuf) + SCREENWIDTH * y + x) = 0xFFFFFF;
133 }
134 } else if (i == 12) {
135 for (x = mouseX; x < mouseX + 6; x++) {
136 *(((int *)pBuf) + SCREENWIDTH * y + x) = 0xFFFFFF;
137 }
138 } else if (i == 13) {
139 for (x = mouseX; x < mouseX + 4; x++) {
140 *(((int *)pBuf) + SCREENWIDTH * y + x) = 0xFFFFFF;
141 }
142 for (x = mouseX + 5; x < mouseX + 7; x++) {
143 *(((int *)pBuf) + SCREENWIDTH * y + x) = 0xFFFFFF;
144 }
145 } else if (i == 14) {
146 for (x = mouseX; x < mouseX + 2; x++) {
147 *(((int *)pBuf) + SCREENWIDTH * y + x) = 0xFFFFFF;
148 }
149 for (x = mouseX + 5; x < mouseX + 7; x++) {
150 *(((int *)pBuf) + SCREENWIDTH * y + x) = 0xFFFFFF;
151 }
152 } else if (i == 15) {
153 for (x = mouseX; x < mouseX + 1; x++) {
154 *(((int *)pBuf) + SCREENWIDTH * y + x) = 0xFFFFFF;
155 }
156 for (x = mouseX + 6; x < mouseX + 8; x++) {
157 *(((int *)pBuf) + SCREENWIDTH * y + x) = 0xFFFFFF;
158 }
159 } else if (i == 16) {
160 for (x = mouseX + 6; x < mouseX + 8; x++) {
161 *(((int *)pBuf) + SCREENWIDTH * y + x) = 0xFFFFFF;
162 }
163 } else if (i == 17 || i == 18) {
164 for (x = mouseX + 7; x < mouseX + 9; x++) {
165 *(((int *)pBuf) + SCREENWIDTH * y + x) = 0xFFFFFF;
166 }
167 }
168 }
169
170 if (g_displayTest.layerFuncs->Flush != NULL) {
171 ret = g_displayTest.layerFuncs->Flush(g_displayTest.devId, g_displayTest.layerId, &g_displayTest.buffer);
172 if (ret != DISPLAY_SUCCESS) {
173 HDF_LOGE("flush layer failed");
174 return DISPLAY_FAILURE;
175 }
176 }
177 }
178
GetMousePos(uint16_t eventType,uint16_t code,int value)179 static void GetMousePos(uint16_t eventType, uint16_t code, int value)
180 {
181 if (eventType == MOUSE_EVENT_TYPE_MOVE && code == 0) {
182 value = value / (0x7fff / SCREENWIDTH);
183
184 mousePosX = value;
185
186 if (mousePosX < 0) {
187 mousePosX = 0;
188 } else if (mousePosX >= (SCREENWIDTH - MOUSEWIDTH)) {
189 mousePosX = SCREENWIDTH - MOUSEWIDTH;
190 }
191 } else if (eventType == MOUSE_EVENT_TYPE_MOVE && code == 1) {
192 value = value / (0x7fff / SCREENHIGH);
193
194 mousePosY = value;
195
196 if (mousePosY < 0) {
197 mousePosY = 0;
198 } else if (mousePosY >= (SCREENHIGH - MOUSEHIGH)) {
199 mousePosY = SCREENHIGH - MOUSEHIGH;
200 }
201 }
202 // left click
203 if (eventType == MOUSE_EVENT_TYPE_CLICK_LEFT && code == 272 && value == 0) {
204 if ((200 < mousePosX && mousePosX < 280) && (100 < mousePosY && mousePosY < 140)) {
205 clickEventFlag = !clickEventFlag;
206 }
207 }
208
209 if (eventType == MOUSE_EVENT_TYPE_CLICK_LEFT || eventType == MOUSE_EVENT_TYPE_MOVE) {
210 ShowMouse(mousePosX, mousePosY);
211 }
212 }
213
ReportEventPkgCallback(const EventPackage ** pkgs,uint32_t count)214 static void ReportEventPkgCallback(const EventPackage **pkgs, uint32_t count)
215 {
216 if (pkgs == NULL) {
217 return;
218 }
219 for (uint32_t i = 0; i < count; i++) {
220 GetMousePos(pkgs[i]->type, pkgs[i]->code, pkgs[i]->value);
221 }
222 }
223
InputServiceSample(void)224 int InputServiceSample(void)
225 {
226 uint32_t devType = INDEV_TYPE_MOUSE;
227
228 /* 获取Input驱动能力接口 */
229 int ret = GetInputInterface(&g_inputInterface);
230 if (ret != INPUT_SUCCESS) {
231 HDF_LOGE("%s: get input interfaces failed, ret = %d", __func__, ret);
232 return ret;
233 }
234
235 INPUT_CHECK_NULL_POINTER(g_inputInterface, INPUT_NULL_PTR);
236 INPUT_CHECK_NULL_POINTER(g_inputInterface->iInputManager, INPUT_NULL_PTR);
237 /* 打开特定的input设备 */
238 ret = g_inputInterface->iInputManager->OpenInputDevice(DEV_INDEX);
239 if (ret) {
240 HDF_LOGE("%s: open input device failed, ret = %d", __func__, ret);
241 return ret;
242 }
243
244 INPUT_CHECK_NULL_POINTER(g_inputInterface->iInputController, INPUT_NULL_PTR);
245 /* 获取对应input设备的类型 */
246 ret = g_inputInterface->iInputController->GetDeviceType(DEV_INDEX, &devType);
247
248 if (ret) {
249 HDF_LOGE("%s: get device type failed, ret: %d", __FUNCTION__, ret);
250 return ret;
251 }
252 HDF_LOGI("%s: device1's type is %u\n", __FUNCTION__, devType);
253
254 /* 给特定的input设备注册数据上报回调函数 */
255 g_callback.EventPkgCallback = ReportEventPkgCallback;
256 ret = g_inputInterface->iInputReporter->RegisterReportCallback(DEV_INDEX, &g_callback);
257 return 0;
258 }