日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

c语言长按键后开关机程序,C程序实现监听长按物理power键3秒关机功能

發布時間:2024/9/30 编程问答 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c语言长按键后开关机程序,C程序实现监听长按物理power键3秒关机功能 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

要實現的功能:在android Framework層被裁剪掉的情況下,實現監聽長按的物理power按鍵,實現長按3秒后關機功能;

思路:使用event epoll,非阻塞式IO操作,同時避免無差別輪詢,做到避免忙輪和無差別輪詢提高效率;

要用到的epoll函數:epoll_create(EPOLL_SIZE_HINT);

epoll_wait(mEpollFd,?mPendingEventItems,?8,?-1);

epoll_ctl(mEpollFd,?EPOLL_CTL_ADD,?fd,?&eventItem)

epoll_ctl(mEpollFd,?EPOLL_CTL_DEL,?fd,?&eventItem);

下面是實現的過程:int?main(int?argc,?char?*argv[])

{

ALOGI("robot?test?robot?power");

struct?itimerval?old_value;

int?tap_power_time?=?0;

int?tap_power_type?=?0;

int?tap_power_code?=?0;

int?tap_power_value?=?0;

struct?itimerval?tick;

int?mEpollFd;

const?int?EPOLL_SIZE_HINT?=?8;

const?char*?DEV_INPUT?=?"/dev/input/event1";

struct?epoll_event?mPendingEventItems[8];

unsigned?char?buf[1024]?=?{0};

struct?inotify_event?*event?=?NULL;

struct?RawEvent*?readBuffer?=?(struct?RawEvent*)?malloc?(sizeof(struct?RawEvent));

mEpollFd?=?epoll_create(EPOLL_SIZE_HINT);

int?fd?=?open(DEV_INPUT,?O_RDWR?|?O_CLOEXEC);

/*

*?struct?epoll_event?{

*uint32_t?events;?????//?epoll?events?(bit?mask)

*epoll_data_t?data;???//?User?data

*?};

*/

struct?epoll_event?eventItem;

memset(&eventItem,?0,?sizeof(eventItem));

/**

*?EPOLLIN:?has?data?to?read

*?EPOLLOUT:?has?data?to?write

*/

eventItem.events?=?EPOLLIN;

/**

*?EPOLL_CTL_ADD:?add?fd?to?watching?list

*?EPOLL_CTL_DEL:?remove?fd?from?watching?list

*/

if?(epoll_ctl(mEpollFd,?EPOLL_CTL_ADD,?fd,?&eventItem)?==?-1)

{

exit(0);

}

for?(;;)

{

/**

*?parameter?-1?:?wait?until?event?occur?;

*????????????0?:?return?immediately?when?deal?with?a?non-block?event

*/

epoll_wait(mEpollFd,?mPendingEventItems,?8,?-1);

int32_t?readSize?=?read(fd,?readBuffer,?sizeof(struct?input_event));

struct?input_event?*event?=(struct?input_event*)?readBuffer;

tap_power_time?=?(int)?event->time.tv_sec;

tap_power_type?=?event->type;

tap_power_code?=?event->code;

tap_power_value?=?event->value;

if(is_power_down_key(tap_power_type,?tap_power_code,?tap_power_value))

{

ALOGI("robot?power?down?key");

signal(SIGALRM,handle_exit);

memset(&tick,0,sizeof(tick));

tick.it_value.tv_sec=?3;

tick.it_value.tv_usec=?0;

setitimer(ITIMER_REAL,&tick,NULL);

}

else?if(is_power_up_key(tap_power_type,?tap_power_code,?tap_power_value))

{

tick.it_value.tv_sec=?0;

tick.it_value.tv_usec=?0;

setitimer(ITIMER_REAL,&tick,NULL);

}

}

epoll_ctl(mEpollFd,?EPOLL_CTL_DEL,?fd,?&eventItem);

return?0;

}struct?RawEvent?{

int32_t?when;

int32_t?deviceId;

int32_t?type;

int32_t?code;

int32_t?value;

};

void?handle_exit(int?sig)

{

ALOGI("robot?3?second?time?out?,now?shutdown!");

system("reboot?-p");

}

int?is_power_down_key(int?type,?int?code,?int?value)

{

int?is_down?=?0;

is_down?=?(type?==?1)?&&?(code?==?116)?&&?(value?==?1);

return?is_down;

}

int?is_power_up_key(int?type,?int?code,?int?value)

{

int?is_up?=?0;

is_up?=?(type?==?1)?&&?(code?==?116)?&&?(value?==?0);

return?is_up;

}

總結

以上是生活随笔為你收集整理的c语言长按键后开关机程序,C程序实现监听长按物理power键3秒关机功能的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。