*************************************************
Function name: main
Parameter : void
Description : 主函数
Return : int
Argument : void
Autor & date : ada 09.12.07
**************************************************/
int main(void)
{
int buttons_fd;
int key_value;
struct song *head;
/*打开设备文件*/
buttons_fd = open("/dev/buttons", 0);
if (buttons_fd < 0)
{
perror("open device buttons");
exit(1);
}
/*创建播放列表*/
head = creat_song_list();
printf("===================================OPTION=======================================\n\n\n\n");
printf(" K1:START/PAUSE K2:STOP K3:NEXT K4:PRIOR\n\n\n\n");
printf("================================================================================\n");
/*共享内存:用于存放子进程ID,播放列表位置*/
//5表示5页,1页地址空间表示4096字节(4KByte),5页也就是20KByte
if((shmid = shmget(IPC_PRIVATE,5,PERM))== -1)
exit(1);
//void *shmat(int shmid, const void *shmaddr, int shm***)
//添加共享内存映射到自己进程地址空间,用于操作使用
p_addr = shmat(shmid,0,0);
//void *memset(void *s, int c, size_t n);
memset(p_addr,'\0',1024);
while(1)
{
fd_set rds;
int ret;
FD_ZERO(&rds);
FD_SET(buttons_fd, &rds);
/*监听获取键值*/
ret = select(buttons_fd + 1, &rds, NULL, NULL, NULL);
if (ret < 0)
{
perror("select");
exit(1);
}
if (ret == 0)
printf("Timeout.\n");
else if (FD_ISSET(buttons_fd, &rds))
{
int ret = read(buttons_fd, &key_value, sizeof key_value);
if (ret != sizeof key_value)
{
if (errno != EAGAIN)
perror("read buttons\n");
continue;
}
else
{
printf("buttons_value: %d\n", key_value+1);
/*首次播放,必须是按键1*/
if(first_key)
{
switch(key_value)
{
case 0:
startplay(&pid,head);
first_key=0;
break;
case 1:
case 2:
case 3:
printf("=======================PRESS K1 TO START PLAY===================\n");
break;
default:
printf("=======================PRESS K1 TO START PLAY===================\n");
break;
} //end switch
}//end if(first_key)
/*若不是首次播放,则根据不同键值处理*/
else if(!first_key)
{
switch(key_value)
{
case 0:
//printf("play_flag:%d\n",play_flag);
if(play_flag)
my_pause(gradchild);
else
conti_play(gradchild);
break;
case 1:
my_stop(gradchild);
break;
case 2:
next(gradchild);
break;
case 3:
prev(gradchild);
break;
} //end switch
}//end if(!first_key)
}
}
}
close(buttons_fd);
return 0;
}
文章评论(0条评论)
登录后参与讨论