blob: 87d73527c9ee1bd1e95b3ead7819d7a4204593c1
1 | #include "CTvInput.h" |
2 | #include <stdio.h> |
3 | #include <string.h> |
4 | #include <errno.h> |
5 | #include <fcntl.h> |
6 | #include <CTvLog.h> |
7 | #include <utils/Timers.h> |
8 | #define LOG_TAG "FBC" |
9 | CTvInput::CTvInput() |
10 | { |
11 | mKeyEventFd_IR = open(KEY_EVENT_0, O_RDWR); |
12 | mKeyEventFd = open(KEY_EVENT_1, O_RDWR); |
13 | if (mKeyEventFd < 0 || mKeyEventFd_IR < 0) { |
14 | LOGD( "could not open /dev/input/event1\n "); |
15 | return ; |
16 | } |
17 | mRepeatKeyCode = -1; |
18 | mRepeatKeydisTime = -1; |
19 | mWhenTimeRepeatKeyStartToSend = -1; |
20 | mTimeoutCount = 0; |
21 | } |
22 | |
23 | CTvInput::~CTvInput() |
24 | { |
25 | close(mKeyEventFd); |
26 | close(mKeyEventFd_IR); |
27 | } |
28 | |
29 | void CTvInput::sendkeyEvent(const int &type, const int &code, const int &value) |
30 | { |
31 | int ret; |
32 | struct input_event event; |
33 | |
34 | memset(&event, 0, sizeof(event)); |
35 | event.type = type; |
36 | event.code = code ; |
37 | event.value = value; |
38 | ret = write(mKeyEventFd, &event, sizeof(event)); |
39 | if (ret < sizeof(event)) { |
40 | LOGD("sendkeyEvent :write event failed, %s\n", strerror(errno)); |
41 | return ; |
42 | } |
43 | } |
44 | |
45 | void CTvInput::sendIRkeyEvent(const int &type, const int &code, const int &value) |
46 | { |
47 | int ret; |
48 | struct input_event event; |
49 | |
50 | memset(&event, 0, sizeof(event)); |
51 | event.type = type; |
52 | event.code = code ; |
53 | event.value = value; |
54 | ret = write(mKeyEventFd_IR, &event, sizeof(event)); |
55 | if (ret < sizeof(event)) { |
56 | LOGD("sendIRkeyEvent :write event failed, %s\n", strerror(errno)); |
57 | return ; |
58 | } |
59 | } |
60 | |
61 | void CTvInput::sendkeyCode(const int &code) |
62 | { |
63 | sendkeyEvent(1 , code, 1); //down |
64 | sendkeyEvent(0, 0, 0); //clear |
65 | sendkeyEvent(1 , code, 0); //up |
66 | sendkeyEvent(0, 0, 0); //clear |
67 | } |
68 | |
69 | void CTvInput::sendkeyCode_Up(const int &code) |
70 | { |
71 | sendkeyEvent(1 , code, 0); //up |
72 | sendkeyEvent(0, 0, 0); //clear |
73 | } |
74 | |
75 | void CTvInput::sendkeyCode_Down(const int &code) |
76 | { |
77 | sendkeyEvent(1 , code, 1); //down |
78 | sendkeyEvent(0, 0, 0); //clear |
79 | } |
80 | void CTvInput::sendIRkeyCode(const int &code) |
81 | { |
82 | sendIRkeyEvent(1 , code, 1); //down |
83 | sendIRkeyEvent(0, 0, 0); //clear |
84 | sendIRkeyEvent(1 , code, 0); //up |
85 | sendIRkeyEvent(0, 0, 0); //clear |
86 | } |
87 | |
88 | void CTvInput::sendIRkeyCode_Up(const int &code) |
89 | { |
90 | sendIRkeyEvent(1 , code, 0); //up |
91 | sendIRkeyEvent(0, 0, 0); //clear |
92 | } |
93 | |
94 | void CTvInput::sendIRkeyCode_Down(const int &code) |
95 | { |
96 | sendIRkeyEvent(1 , code, 1); //down |
97 | sendIRkeyEvent(0, 0, 0); //clear |
98 | } |
99 | |
100 | nsecs_t CTvInput::getNowMs() |
101 | { |
102 | return systemTime(SYSTEM_TIME_MONOTONIC) / 1000000; |
103 | } |
104 | void CTvInput::sendKeyRepeatStart(const int &code, int disTime, int repeatDisTime) |
105 | { |
106 | CMutex::Autolock _l(mLock); |
107 | mTimeoutCount = 0; |
108 | mRepeatKeyCode = code; |
109 | mRepeatKeydisTime = disTime; |
110 | mWhenTimeRepeatKeyStartToSend = disTime + getNowMs(); |
111 | mRepeatKeyRepeateDisTime = repeatDisTime; |
112 | LOGD("sendKeyRepeatStart when = %lld", mWhenTimeRepeatKeyStartToSend); |
113 | mSendKeyCondition.signal(); |
114 | } |
115 | |
116 | void CTvInput::sendKeyRepeatStop() |
117 | { |
118 | CMutex::Autolock _l(mLock); |
119 | mRepeatKeyCode = -1; |
120 | mRepeatKeydisTime = -1; |
121 | } |
122 | |
123 | bool CTvInput::threadLoop() |
124 | { |
125 | int sleeptime = 100;//ms |
126 | |
127 | while (!exitPending()) { //requietexit() or requietexitWait() not call |
128 | LOGD("threadLoop0 when = %lld", mWhenTimeRepeatKeyStartToSend); |
129 | while (mRepeatKeyCode < 0 || mRepeatKeydisTime < 0) { //msg queue is empty |
130 | mLock.lock(); |
131 | mSendKeyCondition.wait(mLock);//first unlock,when return,lock again,so need,call unlock |
132 | mLock.unlock(); |
133 | } |
134 | LOGD("threadLoop1 when = %lld", mWhenTimeRepeatKeyStartToSend); |
135 | int disToSend = -1; |
136 | do { |
137 | disToSend = mWhenTimeRepeatKeyStartToSend - getNowMs(); |
138 | LOGD("dis when = %lld", disToSend); |
139 | if (disToSend <= 0) break; |
140 | |
141 | mLock.lock(); |
142 | mSendKeyCondition.waitRelative(mLock, disToSend);//first unlock,when return,lock again,so need,call unlock |
143 | mLock.unlock(); |
144 | } while (disToSend > 0); // |
145 | LOGD("threadLoop2 when = %lld ", mWhenTimeRepeatKeyStartToSend); |
146 | if (mTimeoutCount < 30) { |
147 | LOGD("mTimeoutCount = %d \n", mTimeoutCount); |
148 | |
149 | mTimeoutCount++; |
150 | if (mRepeatKeyCode == 955) { |
151 | sendkeyCode_Down(mRepeatKeyCode); |
152 | } else { |
153 | sendkeyCode(mRepeatKeyCode); |
154 | } |
155 | } else { |
156 | LOGD("mTimeoutCount = %d,so stop sending long press msg.\n", mTimeoutCount); |
157 | } |
158 | usleep(mRepeatKeyRepeateDisTime * 1000); |
159 | } |
160 | //exit |
161 | //return true, run again, return false,not run. |
162 | return false; |
163 | } |