android11 input子系统入门之InputChannel使用说明

InputChannel提供了一个通道,一方写入InputMessage ,一方读取。
以下演示了在同一个进程中,InputChannel的使用。

#include <array>

#include <utils/threads.h>
#include <unistd.h>
#include <time.h>
#include <errno.h>
#include <binder/Binder.h>
#include <binder/Parcel.h>
#include <input/InputTransport.h>
#include <utils/StopWatch.h>
#include <utils/StrongPointer.h>
#include <utils/Timers.h>
#include <binder/ProcessState.h>
#include <binder/IPCThreadState.h>
typedef uint8_t u8;
typedef int32_t u32;

using namespace std;
using namespace android;

int main(int, const char**) {
	sp<InputChannel> serverChannel, clientChannel;
	status_t result = InputChannel::openInputChannelPair("channel name",
			serverChannel, clientChannel);
	cout << serverChannel->getName() << endl;
	cout << clientChannel->getName() << endl;

	// Server->Client communication
	InputMessage serverMsg;
	memset(&serverMsg, 0, sizeof(InputMessage));
	serverMsg.header.type = InputMessage::Type::KEY;
	serverMsg.body.key.action = AKEY_EVENT_ACTION_DOWN;
	serverMsg.body.key.keyCode = 123;
	serverChannel->sendMessage(&serverMsg);

	InputMessage clientMsg;
	clientChannel->receiveMessage(&clientMsg);
	if (serverMsg.header.type == clientMsg.header.type) {
		cout << "OK" << endl;
	} else {
		cout << "no," << endl;
	}
	if (serverMsg.body.key.action == clientMsg.body.key.action) {
		cout << "OK" << endl;
	} else {
		cout << "no," << endl;
	}
	cout << "clientMsg.body.key.keyCode: " <<clientMsg.body.key.keyCode << endl;
	printf("end\n");
	return 0;
}

后面会继续研究InputChannel跨进程的通信。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值