mymp3.h:
- typedef struct node
- {
- char data[100];
- struct node *left;
- struct node *right;
- }NODE;
- enum modules{mod1,mod2};
- enum mp3buttons{pre,start,next,pau,up,down};
- NODE *mycreatedoublelist(void);
- extern void mp3_display(NODE *,int,int *,int *,int);
- extern int module(void);
mp3.c:
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/ioctl.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <sys/select.h>
- #include <sys/time.h>
- #include <errno.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <linux/soundcard.h>
- #include <sys/ipc.h>
- #include <sys/shm.h>
- #include "mymp3.h"
- int main(void)
- {
- int buttons_fd,mix_fd;
- int iLeft = 0;
- int iRight = 0;
- int iLevel;
- int volume = 75;
- iLeft = iRight = volume;
- iLevel = (iRight << 8) + iLeft;
- int mod;
- int ret;
- int sta = 0;
- int isstart = 0;
- int rand;
- int flg = 0;
- int num = 0;
- int stop = 0;
- char play[60];
- NODE *shmptr = NULL;
- int shmid;
- NODE *head = mycreatedoublelist();
- NODE *p = head;
- mod = module();
- mp3_display(head,10,&volume,&flg,mod);
- char buttons[6] = {'0', '0', '0', '0', '0', '0'};
- buttons_fd = open("/dev/my_buttons",0);
- if (buttons_fd < 0)
- {
- perror("open device buttons\n");
- exit(1);
- }
- mix_fd = open("/dev/mixer",O_WRONLY);
- if(mix_fd < 0)
- {
- perror("open device mixer\n");
- exit(1);
- }
- ioctl(mix_fd,MIXER_WRITE(SOUND_MIXER_VOLUME),&iLevel);
- shmid = shmget(IPC_PRIVATE,sizeof(NODE),0777);
- shmptr = (NODE *)shmat(shmid,0,0);
- memcpy(shmptr,p,sizeof(NODE));
- for (;;)
- {
- char current_buttons[6];
- int count_of_changed_key;
- int i;
- int changeflg = 0;
- pid_t pid,playpid;
- if (read(buttons_fd, current_buttons, sizeof current_buttons) != sizeof current_buttons)
- {
- perror("read buttons:");
- exit(1);
- }
- for (i = 0, count_of_changed_key = 0; i < sizeof buttons / sizeof buttons[0]; i++)
- {
- if (buttons[i] != current_buttons[i])
- {
- buttons[i] = current_buttons[i];
- if(buttons[i] == '1')
- {
- ;
- }
- else
- {
- switch(i)
- {
- case pau:
- {
- if(isstart != 0)
- {
- if(stop == 0)
- {
- system("killall -STOP madplay &");
- kill(pid,19);
- stop = 1;
- }
- else
- {
- printf("haved stopped!\n");
- sleep(1);
- }
- mp3_display(shmptr,pau,&volume,&flg,mod);
- }
- else
- {
- printf("Please input start frist!\n");
- sleep(1);
- }
- break;
- }
- case pre:
- {
- if(num != 0)
- {
- kill(pid,9);
- system("killall -STOP madplay &");
- system("killall -9 madplay &");
- num = 0;
- }
- if(mod == 1)
- {
- p = shmptr;
- p = p->left;
- memset(shmptr,0,sizeof(NODE));
- memcpy(shmptr,p,sizeof(NODE));
- }
- else
- {
- rand = random() % 14 + 1;
- while(rand--)
- {
- p = shmptr;
- p = p->left;
- memset(shmptr,0,sizeof(NODE));
- memcpy(shmptr,p,sizeof(NODE));
- }
- }
- changeflg = 1;
- }
- case next:
- {
- if(changeflg != 1)
- {
- if(num != 0)
- {
- kill(pid,9);
- system("killall -STOP madplay &");
- system("killall -9 madplay &");
- num = 0;
- }
- if(mod == 1)
- {
- p = shmptr;
- p = p->right;
- memset(shmptr,0,sizeof(NODE));
- memcpy(shmptr,p,sizeof(NODE));
- }
- else
- {
- rand = random() % 14 + 1;
- while(rand--)
- {
- p = shmptr;
- p = p->right;
- memset(shmptr,0,sizeof(NODE));
- memcpy(shmptr,p,sizeof(NODE));
- }
- }
- }
- }
- case start:
- {
- if(stop == 1)
- {
- system("killall -CONT madplay &");
- kill(pid,18);
- stop = 0;
- mp3_display(shmptr,start,&volume,&flg,mod);
- }
- else
- {
- if(num != 0)
- {
- system("killall -STOP madplay &");
- system("killall -9 madplay &");
- kill(pid,9);
- num = 0;
- }
- pid = fork();
- if(pid < 0)
- {
- perror("fork error\n");
- }
- else if(pid == 0)
- {
- loopv:
- playpid = fork();
- if(playpid < 0)
- {
- perror("fork2 error\n");
- }
- else if(playpid == 0)
- {
- execl("/music/madplay","./madplay",shmptr->data,NULL);
- }
- wait(&sta);
- if(WIFEXITED(sta) != 0)
- {
- p = shmptr;
- p = p->right;
- memset(shmptr,0,sizeof(NODE));
- memcpy(shmptr,p,sizeof(NODE));
- mp3_display(shmptr,start,&volume,&flg,mod);
- goto loopv;
- }
- else
- {
- mp3_display(shmptr,start,&volume,&flg,mod);
- goto loopv;
- }
- }
- else
- {
- changeflg = 0;
- num = 1;
- mp3_display(shmptr,start,&volume,&flg,mod);
- }
- }
- isstart = 1;
- break;
- }
- case up:
- {
- mp3_display(shmptr,up,&volume,&flg,mod);
- iLeft = iRight = volume;
- iLevel = (iRight << 8) + iLeft;
- ioctl(mix_fd,MIXER_WRITE(SOUND_MIXER_VOLUME),&iLevel);
- break;
- }
- case down:
- {
- mp3_display(shmptr,down,&volume,&flg,mod);
- iLeft = iRight = volume;
- iLevel = (iRight << 8) + iLeft;
- ioctl(mix_fd,MIXER_WRITE(SOUND_MIXER_VOLUME),&iLevel);
- break;
- }
- default:
- {
- break;
- }
- }
- count_of_changed_key++;
- }
- }
- }
- if (count_of_changed_key)
- {
- printf("\n");
- }
- }
- shmctl(shmid,IPC_RMID,NULL);
- close(mix_fd);
- close(buttons_fd);
- return 0;
- }
