X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=bam_tview_curses.c;h=4fdd1fb66be93292dc9c952729215e0f31711b8b;hb=HEAD;hp=571633c817b1e7a9fd899817c7972269898f5547;hpb=3880f184334b2b5afe27de5c5a0f344cdcff1ef9;p=samtools.git diff --git a/bam_tview_curses.c b/bam_tview_curses.c index 571633c..4fdd1fb 100644 --- a/bam_tview_curses.c +++ b/bam_tview_curses.c @@ -1,9 +1,38 @@ -#include "bam_tview_curses.h" +#undef _HAVE_CURSES +#if _CURSES_LIB == 0 +#elif _CURSES_LIB == 1 +#include +#ifndef NCURSES_VERSION +#warning "_CURSES_LIB=1 but NCURSES_VERSION not defined; tview is NOT compiled" +#else +#define _HAVE_CURSES +#endif +#elif _CURSES_LIB == 2 +#include +#define _HAVE_CURSES +#else +#warning "_CURSES_LIB is not 0, 1 or 2; tview is NOT compiled" +#endif +#include "bam_tview.h" -static void curses_tv_destroy(tview_t* base) +#ifdef _HAVE_CURSES + + + +typedef struct CursesTview { + tview_t view; + WINDOW *wgoto, *whelp; + } curses_tview_t; + + + + +#define FROM_TV(ptr) ((curses_tview_t*)ptr) + +static void curses_destroy(tview_t* base) { curses_tview_t* tv=(curses_tview_t*)base; @@ -16,42 +45,56 @@ static void curses_tv_destroy(tview_t* base) free(tv); } -curses_tview_t* curses_tv_init(const char *fn, const char *fn_fa, const char *samples) +/* + void (*my_mvprintw)(struct AbstractTview* ,int,int,const char*,...); + void (*my_)(struct AbstractTview*,int,int,int); + void (*my_attron)(struct AbstractTview*,int); + void (*my_attroff)(struct AbstractTview*,int); + void (*my_clear)(struct AbstractTview*); + int (*my_colorpair)(struct AbstractTview*,int); +*/ + +static void curses_mvprintw(struct AbstractTview* tv,int y ,int x,const char* fmt,...) { - curses_tview_t *tv = (curses_tview_t*)calloc(1, sizeof(curses_tview_t)); - tview_t* base=(tview_t*)tv; - if(tv==0) - { - fprintf(stderr,"Calloc failed\n"); - return 0; - } - - base_tv_init(base,fn,fn_fa,samples); - /* initialize callbacks */ - base->destroy=curses_tv_destroy; - - initscr(); - keypad(stdscr, TRUE); - clear(); - noecho(); - cbreak(); - - getmaxyx(stdscr, base->mrow, base->mcol); - tv->wgoto = newwin(3, TV_MAX_GOTO + 10, 10, 5); - tv->whelp = newwin(29, 40, 5, 5); - - start_color(); - init_pair(1, COLOR_BLUE, COLOR_BLACK); - init_pair(2, COLOR_GREEN, COLOR_BLACK); - init_pair(3, COLOR_YELLOW, COLOR_BLACK); - init_pair(4, COLOR_WHITE, COLOR_BLACK); - init_pair(5, COLOR_GREEN, COLOR_BLACK); - init_pair(6, COLOR_CYAN, COLOR_BLACK); - init_pair(7, COLOR_YELLOW, COLOR_BLACK); - init_pair(8, COLOR_RED, COLOR_BLACK); - init_pair(9, COLOR_BLUE, COLOR_BLACK); - return tv; + unsigned int size=tv->mcol+2; + char* str=malloc(size); + if(str==0) exit(EXIT_FAILURE); + va_list argptr; + va_start(argptr, fmt); + vsnprintf(str,size, fmt, argptr); + va_end(argptr); + mvprintw(y,x,str); + free(str); + } + +static void curses_mvaddch(struct AbstractTview* tv,int y,int x,int ch) + { + mvaddch(y,x,ch); } + +static void curses_attron(struct AbstractTview* tv,int flag) + { + attron(flag); + } +static void curses_attroff(struct AbstractTview* tv,int flag) + { + attroff(flag); + } +static void curses_clear(struct AbstractTview* tv) + { + clear(); + } + +static int curses_colorpair(struct AbstractTview* tv,int flag) + { + return COLOR_PAIR(flag); + } + +static int curses_drawaln(struct AbstractTview* tv, int tid, int pos) + { + return base_draw_aln(tv, tid, pos); + } + static void tv_win_goto(curses_tview_t *tv, int *tid, int *pos) @@ -130,10 +173,15 @@ static void tv_win_help(curses_tview_t *tv) { wgetch(win); } -void tv_loop(curses_tview_t *CTV) -{ +static int curses_underline(tview_t* tv) + { + return A_UNDERLINE; + } + +static int curses_loop(tview_t* tv) + { int tid, pos; - tview_t* tv=(tview_t*)CTV; + curses_tview_t *CTV=(curses_tview_t *)tv; tid = tv->curr_tid; pos = tv->left_pos; while (1) { int c = getch(); @@ -176,59 +224,74 @@ void tv_loop(curses_tview_t *CTV) } if (pos < 0) pos = 0; if (tv->row_shift < 0) tv->row_shift = 0; - tv_draw_aln(tv, tid, pos); + tv->my_drawaln(tv, tid, pos); } end_loop: - return; + return 0; } -void error(const char *format, ...) -{ - if ( !format ) - { - fprintf(stderr, "\n"); - fprintf(stderr, "Usage: bamtk tview [options] [ref.fasta]\n"); - fprintf(stderr, "Options:\n"); - fprintf(stderr, " -p chr:pos go directly to this position\n"); - fprintf(stderr, " -s STR display only reads from this sample or grou\n"); - fprintf(stderr, "\n\n"); - } - else - { - va_list ap; - va_start(ap, format); - vfprintf(stderr, format, ap); - va_end(ap); - } - exit(-1); -} -int bam_tview_main(int argc, char *argv[]) -{ - curses_tview_t *CTV; - tview_t* tv=NULL; - char *samples=NULL, *position=NULL; - int c; - while ((c = getopt(argc, argv, "s:p:")) >= 0) { - switch (c) { - case 's': samples=optarg; break; - case 'p': position=optarg; break; - default: error(NULL); - } - } - if (argc==optind) error(NULL); - CTV = curses_tv_init(argv[optind], (optind+1>=argc)? 0 : argv[optind+1], samples); - tv=(tview_t*)CTV; - if ( position ) - { - int _tid = -1, _beg, _end; - bam_parse_region(tv->header, position, &_tid, &_beg, &_end); - if (_tid >= 0) { tv->curr_tid = _tid; tv->left_pos = _beg; } - } - tv_draw_aln(tv, tv->curr_tid, tv->left_pos); - tv_loop(CTV); - tv->destroy(tv); - free(tv); - return 0; -} + +tview_t* curses_tv_init(const char *fn, const char *fn_fa, const char *samples) + { + curses_tview_t *tv = (curses_tview_t*)calloc(1, sizeof(curses_tview_t)); + tview_t* base=(tview_t*)tv; + if(tv==0) + { + fprintf(stderr,"Calloc failed\n"); + return 0; + } + + base_tv_init(base,fn,fn_fa,samples); + /* initialize callbacks */ +#define SET_CALLBACK(fun) base->my_##fun=curses_##fun; + SET_CALLBACK(destroy); + SET_CALLBACK(mvprintw); + SET_CALLBACK(mvaddch); + SET_CALLBACK(attron); + SET_CALLBACK(attroff); + SET_CALLBACK(clear); + SET_CALLBACK(colorpair); + SET_CALLBACK(drawaln); + SET_CALLBACK(loop); + SET_CALLBACK(underline); +#undef SET_CALLBACK + + initscr(); + keypad(stdscr, TRUE); + clear(); + noecho(); + cbreak(); + + getmaxyx(stdscr, base->mrow, base->mcol); + tv->wgoto = newwin(3, TV_MAX_GOTO + 10, 10, 5); + tv->whelp = newwin(29, 40, 5, 5); + + start_color(); + init_pair(1, COLOR_BLUE, COLOR_BLACK); + init_pair(2, COLOR_GREEN, COLOR_BLACK); + init_pair(3, COLOR_YELLOW, COLOR_BLACK); + init_pair(4, COLOR_WHITE, COLOR_BLACK); + init_pair(5, COLOR_GREEN, COLOR_BLACK); + init_pair(6, COLOR_CYAN, COLOR_BLACK); + init_pair(7, COLOR_YELLOW, COLOR_BLACK); + init_pair(8, COLOR_RED, COLOR_BLACK); + init_pair(9, COLOR_BLUE, COLOR_BLACK); + return base; + } + + +#else // #ifdef _HAVE_CURSES +#include +#warning "No curses library is available; tview with curses is disabled." + +extern tview_t* text_tv_init(const char *fn, const char *fn_fa, const char *samples); + +tview_t* curses_tv_init(const char *fn, const char *fn_fa, const char *samples) + { + return text_tv_init(fn,fn_fa,samples); + } +#endif // #ifdef _HAVE_CURSES + +