From: hackbard Date: Sun, 16 May 2004 08:28:00 +0000 (+0000) Subject: completely removed ncurses, added minimal audio functions X-Git-Url: https://hackdaworld.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=51804c822aba850ad3d1589b4dc3dc2709d980f5;p=my-code%2Fivac.git completely removed ncurses, added minimal audio functions --- diff --git a/src/Makefile b/src/Makefile index f3b4e06..22c8045 100644 --- a/src/Makefile +++ b/src/Makefile @@ -3,9 +3,9 @@ INCLUDEDIR = /usr/include CFLAGS = -DDISPLAY -DDEBUG -O3 -Wall -LIBS = -lncurses +LIBS = -OBJS = network.o event.o input.o display.o +OBJS = network.o event.o input.o display.o audio.o ivac: $(OBJS) $(CC) -o $@ $(OBJS) ivac.c $(LIBS) diff --git a/src/audio.c b/src/audio.c index bd47644..9c8ca4a 100644 --- a/src/audio.c +++ b/src/audio.c @@ -10,14 +10,114 @@ int audio_init(t_audio *audio) { puts("[audio] initializing audio ..."); - if((audio->fd=open(audio->device,O_RDONLY))==-1) { + if((audio->dsp_fd=open(audio->dsp_dev,O_RDWR))==-1) { perror("[audio] open call"); return A_ERROR; } - if(ioctl(audio->fd,SNDCTL_DSP_GETCAPS,&(audio->cap))==-1) { + if(ioctl(audio->dsp_fd,SNDCTL_DSP_GETCAPS,&(audio->dsp_cap))==-1) { perror("[audio] ioctl call"); return A_ERROR; } - + if(!(audio->dsp_cap&DSP_CAP_DUPLEX)) { + puts("[audio] no duplex support"); + return A_ERROR; + } + + return A_SUCCESS; +} + +int audio_setup(t_audio *audio) { + + int tmp; + + puts("[audio] setting up sound device & allocating record/playback buffer"); + + tmp=audio->fmt; + if(ioctl(audio->dsp_fd,SNDCTL_DSP_SETFMT,&tmp)==-1) { + perror("[audio] ioctl call (SNDCTL_DSP_SETFMT)"); + return A_ERROR; + } + if(tmp!=audio->fmt) { + puts("[audio] FMT not supported"); + return A_ERROR; + } + + tmp=audio->speed; + if(ioctl(audio->dsp_fd,SNDCTL_DSP_SPEED,&tmp)==-1) { + perror("[audio] ioctl call (SNDCTL_DSP_SPEED)"); + return A_ERROR; + } + if(tmp!=audio->speed) { + puts("[audio] SPEED not supported"); + return A_ERROR; + } + + if(ioctl(audio->dsp_fd,SNDCTL_DSP_GETBLKSIZE,&(audio->blksize))==-1) { + perror("[audio] ioctl call (SNDCTL_DSP_GETBLKSIZE)"); + return A_ERROR; + } + + if((audio->play_data=(unsigned char *)malloc(audio->blksize))==NULL) { + perror("[audio] malloc call"); + return A_ERROR; + } + if((audio->rec_data=(unsigned char *)malloc(audio->blksize))==NULL) { + perror("[audio] malloc call"); + return A_ERROR; + } + + return A_SUCCESS; +} + +int audio_shutdown(t_audio *audio) { + + puts("[audio] shutdown"); + + free(audio->play_data); + free(audio->rec_data); + + if(close(audio->dsp_fd)==-1) { + perror("[audio] close call"); + return A_ERROR; + } + + return A_SUCCESS; +} + +int audio_play(t_audio *audio,int len) { + + int count,left; + + count=0; + left=len; + + while(left) { + if((count=write(audio->dsp_fd,audio->play_data+len-left,left))==-1) { + perror("[audio] write call"); + return A_ERROR; + } + left-=count; + } + + return A_SUCCESS; +} + +int audio_record(t_audio *audio,int len) { + + int count,left; + + count=0; + left=len; + + while(left) { + if((count=read(audio->dsp_fd,audio->rec_data+len-left,left))==-1) { + perror("[audio] read call"); + return A_ERROR; + } + left-=count; + } + + return A_SUCCESS; +} diff --git a/src/audio.h b/src/audio.h index b0c01db..33e90cb 100644 --- a/src/audio.h +++ b/src/audio.h @@ -4,17 +4,50 @@ #define AUDIO_H /* includes */ +#include +#include +#include +#include +#include +#include +#include +#include #include /* defines */ #define MAX_CHAR_DEVICE 32 #define SOUND_DEVICE "/dev/dsp" +#define A_SUCCESS 1 +#define A_ERROR -1 + +#define BIT_8 AFMT_U8 +#define BIT_16 AFMT_S16_LE +#define MONO 1 +#define STEREO 2 + /* audio specific variables */ typedef struct s_audio { - int fd; - char device[MAX_CHAR_DEVICE]; - int cap; + int dsp_fd; + int mixer_fd; + char dsp_dev[MAX_CHAR_DEVICE]; + char mixer_dev[MAX_CHAR_DEVICE]; + int dsp_cap; + int fmt; + int channels; + int speed; + int blksize; + int mixer_cap; + unsigned char volume; + unsigned char *play_data; + unsigned char *rec_data; } t_audio; +/* function prototypes */ +int audio_init(t_audio *audio); +int audio_setup(t_audio *audio); +int audio_shutdown(t_audio *audio); +int audio_play(t_audio *audio,int len); +int audio_record(t_audio *audio,int len); + #endif diff --git a/src/display.c b/src/display.c index 9390fbc..914ab63 100644 --- a/src/display.c +++ b/src/display.c @@ -10,27 +10,18 @@ int display_init(t_display *display) { puts("[display] initializing display ..."); - initscr(); - raw(); - noecho(); - keypad(stdscr,TRUE); - getmaxyx(stdscr,display->max_y,display->max_x); + /* init stuff next .. */ return D_SUCCESS; } int display_refresh(t_display *display) { - getmaxyx(stdscr,display->max_y,display->max_x); - return D_SUCCESS; } int display_shutdown(t_display *display) { - noraw(); - echo(); - puts("[display] shutdown"); return D_SUCCESS; diff --git a/src/display.h b/src/display.h index 7ec779e..113e892 100644 --- a/src/display.h +++ b/src/display.h @@ -2,7 +2,6 @@ /* includes */ #include -#include /* defines */ #define D_SUCCESS 1 diff --git a/src/ivac.c b/src/ivac.c index e3e2060..98fc8c2 100644 --- a/src/ivac.c +++ b/src/ivac.c @@ -21,9 +21,7 @@ * */ -// #define USE_NCURSES -/* dont care about ncurses .. go for gtk(2)! */ -#define USE_GTK +// #define USE_GTK #include "ivac.h" @@ -36,6 +34,7 @@ int usage(void) { puts("-h \t\t show this help"); puts("-n \t specify your name"); puts("-p \t specify port to listen for incoming connections"); + puts("-d \t specify audio device"); puts(""); return SUCCESS; @@ -51,6 +50,7 @@ int main(int argc,char **argv) { /* default values */ strcpy(ivac.username,"ivac"); ivac.net.l_port=IVAC_LISTEN_PORT; + strcpy(ivac.audio.dsp_dev,SOUND_DEVICE); /* parse argv and change default values */ for(i=1;inet)); input_shutdown(&(ivac->input)); event_stop(&(ivac->event)); -#ifdef USE_NCURSES + audio_shutdown(&(ivac->audio)); display_shutdown(&(ivac->display)); -#endif return SUCCESS; } @@ -448,37 +455,15 @@ int ivac_parse_command(t_input *input,void *ptr) { int ivac_display_head(t_display *display) { -#ifdef USE_NCURSES - int x,y; - - move(0,0); - for(x=0;xmax_x;x++) addch('#'); - mvaddstr(1,0,"##"); - mvaddstr(1,(display->max_x-4)/2-4,"- ivac -"); - mvaddstr(1,(display->max_x-2),"##"); - move(2,0); - for(x=0;xmax_x;x++) addch('#'); - refresh(); -#else puts("#########################################################"); puts("##### ivac - - Copyright (C) 2004 Frank Zirkelbach #####"); puts("#########################################################"); -#endif return SUCCESS; } int ivac_display_box(t_display *display) { -#ifdef USE_NCURSES - int x,y; - - for(y=IVAC_PROMPT_LEN;ymax_y-IVAC_PROMPT_LEN;y++) { - mvaddch(y,0,'#'); - mvaddch(y,display->max_x-1,'#'); - } -#endif - return SUCCESS; } @@ -491,93 +476,33 @@ int ivac_display_box_content(t_ivac *ivac) { if(ivac->challenger[channel].name[0]==0) strcpy(ivac->challenger[channel].name,""); -#ifdef USE_NCURSES -#else - for(channel=0;channelnet.connection[channel].status&C_INFO_A) - printf("channel %02d: ip:%s port:%d status: %02x - name: %s\n",channel, - ivac->net.connection[channel].ip, - ivac->net.connection[channel].port, - ivac->net.connection[channel].status, - ivac->challenger[channel].name); - } -#endif - return SUCCESS; } int ivac_display_console(t_display *display) { -#ifdef USE_NCURSES - int x,y; - - move(display->max_y-IVAC_CONSOLE_LEN-IVAC_PROMPT_LEN-1,0); - for(x=0;xmax_x;x++) addch('#'); -#endif - return SUCCESS; } int ivac_display_console_content(t_ivac *ivac) { -#ifdef USE_NCURSES - int x,y; - int len; - - for(y=0;yconsole[y]); - move(ivac->display.max_y-IVAC_CONSOLE_LEN-IVAC_PROMPT_LEN+y,2); - for(x=0;xconsole[y][x]>' ')||(ivac->console[y][x]<='~')) - ?ivac->console[y][x]:' '); - for(x=len;xconsole[i]); -#endif return SUCCESS; } int ivac_display_prompt(t_display *display) { -#ifdef USE_NCURSES - int x,y; - - move(display->max_y-3,0); - for(x=0;xmax_x;x++) addch('#'); - mvaddstr(display->max_y-2,0,"## command: "); - mvaddstr(display->max_y-2,display->max_x-2,"##"); - move(display->max_y-1,0); - for(x=0;xmax_x;x++) addch('#'); - refresh(); -#endif - return SUCCESS; } int ivac_display_prompt_content(t_ivac *ivac) { -#ifdef USE_NCURSES - int x,y; - - /* delete old command */ - if(ivac->input.c_count==0) { - move(ivac->display.max_y-2,12); - for(x=12;xdisplay.max_x-1;x++) addch(' '); - } - - for(x=0;xinput.c_count;x++) - mvaddch(ivac->display.max_y-2,x+12,ivac->input.content[x]); - refresh(); -#else printf("%c",ivac->input.content[ivac->input.c_count-1]); fflush(NULL); -#endif return SUCCESS; } diff --git a/src/ivac.h b/src/ivac.h index af64d04..7acb2e6 100644 --- a/src/ivac.h +++ b/src/ivac.h @@ -14,6 +14,7 @@ #include "event.h" #include "input.h" #include "display.h" +#include "audio.h" /* defines */ #define CHAR_USERNAME 32 @@ -62,6 +63,7 @@ typedef struct s_ivac { t_display display; t_challenger challenger[MAX_CONNECTIONS]; char console[IVAC_CONSOLE_LEN][IVAC_CONSOLE_STRING_LEN]; + t_audio audio; } t_ivac; /* function prototypes */