📄 ui.c
字号:
/*************************************************************************** file : ui.cpp begin : Sat Mar 25 2000 copyright : (C) 2000 by Henrik Witt-Hansen email : bean@mrbean.dk ***************************************************************************//*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************//*base configuration*/#ifdef HAVE_CONFIG_H#include <config.h>#endif/*standard includes*/#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <stdarg.h>/*local headers*/#include "sitback.h"/*special includes*/#ifdef HAVE_NCURSES #include <ncurses.h>#endif#include <signal.h>#include <pthread.h>/*local data*/int ncurses_initialized=0; /*set to 1 when a interface is available*/int x=70,y; /*width and height of the screen*/char ncurses_progress_text[1025]; /*text to show in progress window*/#ifdef HAVE_NCURSES WINDOW *wmessage=NULL,*wmessage_top=NULL; WINDOW *wprogress=NULL,*wprogress_top=NULL;#endif/*color pairs*/#define COLOR_MAINWINDOW 1#define COLOR_SUBWINDOW 2#define COLOR_INFO 3#define COLOR_WARNING 4/******************************************************************** int UI__Init(int argc,char **argv) Initialize interface, background etc.. Relate to: All operations arguments: none return: 0 on success -1 on error********************************************************************/int UI__Init(int argc,char **argv){ #ifdef HAVE_NCURSES char msg[64]; int count; /* do not do anything if we are a daemon */ if(conf__mode==DAEMON) return 0; /*initialize the screen*/ if(initscr()==NULL) return -1; ncurses_initialized=1; /*get info about the screen*/ getmaxyx(stdscr,y,x); /*initalize colorpaires that we want to use. But only if colors is available*/ if(has_colors()) { /*set screen in color mode*/ start_color(); /*active color scheme*/ init_pair(COLOR_MAINWINDOW,COLOR_BLACK,COLOR_WHITE); /*sub windows, active color scheme*/ init_pair(COLOR_SUBWINDOW,COLOR_WHITE,COLOR_BLUE); /*info messages in subwindow*/ init_pair(COLOR_INFO,COLOR_CYAN,COLOR_BLUE); /*warnings in subwindow*/ init_pair(COLOR_WARNING,COLOR_RED,COLOR_WHITE); } /*prepare the background*/ if(has_colors()) { bkgd(COLOR_PAIR(COLOR_MAINWINDOW)); #ifdef HAVE_COLOR_SET color_set(COLOR_PAIR(COLOR_MAINWINDOW),NULL); #endif attrset(COLOR_PAIR(COLOR_MAINWINDOW)); } move(1,2); sprintf(msg,"Sitback %s",VERSION); for(count=strlen(msg);count>1;count--) { memmove(&msg[count],&msg[count-1],strlen(&msg[count-1])+1); msg[count-1]=' '; } printw(msg); move(y-1,x-25); printw("bean@mrbean.dk 1999-2004"); /*that's it. Refresh the screen and return*/ refresh(); #endif return 0;}/******************************************************************** int UI__Shutdown() Shutdown the user interface Relate to: All operations arguments: none return: none********************************************************************/void UI__Shutdown(){ #ifndef DEBUG struct stat statbuf; #endif #ifdef HAVE_NCURSES /*nothing to do if advanced ui is not initialized*/ if(!ncurses_initialized) return; /* do not do anything if we are a daemon */ if(conf__mode==DAEMON) return; /*kill windows*/ if(wmessage!=NULL) delwin(wmessage); if(wmessage_top!=NULL) delwin(wmessage); if(wprogress!=NULL) delwin(wprogress); if(wprogress_top!=NULL) delwin(wprogress_top); refresh(); /*break out*/ endwin(); #ifndef DEBUG if(!stat("/bin/clear",&statbuf)) system("/bin/clear"); else if(!stat("/sbin/clear",&statbuf)) system("/sbin/clear"); else if(!stat("/usr/bin/clear",&statbuf)) system("/usr/bin/clear"); else if(!stat("/usr/sbin/clear",&statbuf)) system("/usr/sbin/clear"); else if(!stat("/usr/local/bin/clear",&statbuf)) system("/usr/local/bin/clear"); else if(!stat("/usr/local/sbin/clear",&statbuf)) system("/usr/local/sbin/clear"); #endif /*no longer initialized*/ ncurses_initialized=0; #endif}/******************************************************************** int UI__ShowBackup() Prepare subwindows for backup operation Relate to: Backup arguments: none return: 0 on success -1 on error********************************************************************/int UI__ShowBackup(){ #ifdef HAVE_NCURSES /*nothing to do if advanced ui is not initialized*/ if(!ncurses_initialized) return 0; /* do not do anything if we are a daemon */ if(conf__mode==DAEMON) return 0; /*create the message window*/ wmessage_top=subwin(stdscr,y-11,x-16,4,8); wmessage= subwin(stdscr,y-13,x-20,5,10); scrollok(wmessage,TRUE); wborder(wmessage_top,ACS_VLINE,ACS_VLINE,ACS_HLINE,ACS_HLINE,ACS_ULCORNER,ACS_URCORNER,ACS_LLCORNER,ACS_LRCORNER); if(has_colors()) { /*set colors*/ wbkgd(wmessage_top,COLOR_PAIR(COLOR_SUBWINDOW)); #ifdef HAVE_WCOLOR_SET wcolor_set(wmessage_top,COLOR_PAIR(COLOR_SUBWINDOW),NULL); #endif wattrset(wmessage_top,COLOR_PAIR(COLOR_SUBWINDOW)); wbkgd(wmessage,COLOR_PAIR(COLOR_SUBWINDOW)); #ifdef HAVE_WCOLOR_SET wcolor_set(wmessage,COLOR_PAIR(COLOR_SUBWINDOW),NULL); #endif wattrset(wmessage,COLOR_PAIR(COLOR_SUBWINDOW)); } wrefresh(wmessage_top); wrefresh(wmessage); /*create the window that shows what files are currently being processed*/ wprogress_top=subwin(stdscr,3,x-16,y-5,8); wprogress= subwin(stdscr,1,x-20,y-4,10); scrollok(wprogress,TRUE); wborder(wprogress_top,ACS_VLINE,ACS_VLINE,ACS_HLINE,ACS_HLINE,ACS_ULCORNER,ACS_URCORNER,ACS_LLCORNER,ACS_LRCORNER); if(has_colors()) { /*set colors*/ wbkgd(wprogress_top,COLOR_PAIR(COLOR_SUBWINDOW)); #ifdef HAVE_WCOLOR_SET wcolor_set(wprogress_top,COLOR_PAIR(COLOR_SUBWINDOW),NULL); #endif wattrset(wprogress_top,COLOR_PAIR(COLOR_SUBWINDOW)); wbkgd(wprogress,COLOR_PAIR(COLOR_SUBWINDOW)); #ifdef HAVE_WCOLOR_SET wcolor_set(wprogress,COLOR_PAIR(COLOR_SUBWINDOW),NULL); #endif wattrset(wprogress,COLOR_PAIR(COLOR_SUBWINDOW)); } mvwaddstr(wprogress,0,0,""); wrefresh(wprogress_top); wrefresh(wprogress); /*no problems*/ #endif return 0;}/******************************************************************** void UI__Message(char *msg, ... ) Print message in the message window Relate to: All operations arguments: msg; the message string to write ...; parameters to suit the formatting of 'msg' return: none********************************************************************/void UI__Message(char *msg, ... ){ char buffer[4096]; va_list ap; /* do not do anything if we are a daemon */ if(conf__mode==DAEMON) return; /* prepare the buffer */ va_start(ap,msg); vsnprintf(buffer,4095,msg,ap); va_end(ap); /*Use raw text if the interface is not initialized*/ if(!ncurses_initialized) printf(buffer); else { /*print the message*/ #ifdef HAVE_NCURSES waddstr(wmessage,buffer); wrefresh(wmessage); #endif }}/******************************************************************** void UI__Warning(char *msg) Print message in the message window Relate to: All operations arguments: msg; string to print (incl. formatting codes) ...; arguments for the formatstring return: word; Not used, but present since this functions serves as entry point for a new thread********************************************************************/void UI__Warning(char *msg, ... ){ va_list ap; #ifdef HAVE_NCURSES char *p; int size=0; #endif /* do not do anything if we are a daemon */ if(conf__mode==DAEMON) return; /*Use raw text if the interface is not initialized*/ va_start(ap,msg); if(!ncurses_initialized) vprintf(msg,ap); else { /*print the message*/ #ifdef HAVE_NCURSES do { size+=1024; if((p=(char*) malloc(size))==NULL) return; /* weird case.. should never happen */ } while(vsnprintf(p,size,msg,ap)<0); wattron(wmessage,COLOR_PAIR(COLOR_WARNING)); waddstr(wmessage,p); free(p); wrefresh(wmessage); wattron(wmessage,COLOR_PAIR(COLOR_SUBWINDOW)); #endif }}/******************************************************************** void *UI__TrackStdout(void *word) Track verbose output from tar. Write it to 'wprogress' Relate to: Backup and Restore arguments: none (word is ignored) return: none********************************************************************/void *UI__TrackStdout(void *word){ FILE *file; size_t target_size[2]={0,0}; long target_offset[2]={0,0}; struct stat statbuf; char target_name[2][15]={"sitback.tmpout","sitback.tmperr"}; int target=0; char buffer[1024]; char extrabuf[2]; int has_data=1; /* Do not run the tracker if the advanced interface is not up */ if(!ncurses_initialized) return NULL; /* keep running until killed */ for(;;) { /* Any changes for the target ?? */ if(!stat(target_name[target],&statbuf)) { if(statbuf.st_size>target_size[target]) { /* Open the file and read a complete line */ if((file=fopen(target_name[target],"r"))!=NULL) { /* We must have a complete line */ fseek(file,target_offset[target],SEEK_SET); fgets(buffer,1024,file); if(buffer[strlen(buffer)-1]!='\n') { /* Try to get the rest of the line */ for(;;) { fgets(extrabuf,2,file); if(feof(file)) { /* Well, no full line available */ fclose(file); goto WAIT_BEFORE_CHECK; } memmove(buffer,&buffer[1],strlen(buffer)); strcat(buffer,extrabuf); if(buffer[strlen(buffer)-1]=='\n') { break; } } } /* Update the offset and size and close the file */ target_size[target]+=ftell(file)-target_offset[target]; target_offset[target]=ftell(file); fclose(file); /* Buffer is ready, print it */ if(strlen(buffer)>60) { memmove(buffer,&buffer[strlen(buffer)-60],61); memcpy(buffer,"[..]",4); } #ifdef HAVE_NCURSES werase(wprogress); mvwaddstr(wprogress,0,0,buffer); wrefresh(wprogress); #endif has_data=1; } } else if(statbuf.st_size<target_size[target]) { /* Reset counters */ target_size[target]=0; target_offset[target]=0; #ifdef HAVE_NCURSES werase(wprogress); wrefresh(wprogress); #endif } } /* Wait a little time before checking again */ WAIT_BEFORE_CHECK: pthread_testcancel(); if(has_data) { usleep(50); has_data=0; } else usleep(500); if(target==0) target=1; else target=0; }}/******************************************************************** void UI__SetProgressText() Set the text to show in the progress window.. Relate to: Backup and restore arguments: none return: none********************************************************************/void UI__SetProgressText(char *text){ /*nothing to do if advanced ui is not initialized or the progress window is not visible*/ #ifdef HAVE_NCURSES if(!ncurses_initialized || wprogress==NULL) return; /* do not do anything if we are a daemon */ if(conf__mode==DAEMON) return; /*save the new string*/ if(strlen(text)<1024) strcpy(ncurses_progress_text,text); werase(wprogress); mvwaddstr(wprogress,0,0,ncurses_progress_text); wrefresh(wprogress); #endif}/******************************************************************** void UI__OkDlg(char *msg) Show a box with a ok button that must be pressed before we return. Relate to: all operations arguments: none return: none********************************************************************/void UI__OkDlg(char *msg){ /* do not do anything if we are a daemon */ if(conf__mode==DAEMON) return; /*fallback if interface is not initialized*/ if(!ncurses_initialized) { printf("%s\nPress ENTER to continue\n",msg); if(conf__unattended==0) getchar(); return; } /*write a better version of this*/ #ifdef HAVE_NCURSES wattron(wmessage,COLOR_PAIR(COLOR_INFO)); UI__Message(msg); UI__Message("\nPress any key to continue\n"); noecho(); if(conf__unattended==0) getch(); echo(); wattron(wmessage,COLOR_PAIR(COLOR_SUBWINDOW)); #endif /* ............................ */}/******************************************************************** void UI__Beep() Make a beep Relate to: all operations arguments: none return: none********************************************************************/void UI__Beep(){ /* do not do anything if we are a daemon */ if(conf__mode==DAEMON) return; #ifdef HAVE_NCURSES if(ncurses_initialized) beep(); #endif}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -