int main(int argc, char **argv)//^_^,main在这里
{ struct sigaction act, oact; //信号的数据结构
g_signal_pending=0; memset(&info, 0, sizeof(info) );//将一段内存空间填入某值
#ifdef TCP_PORT_MONITOR tcp_port_monitor_collection_args.min_port_monitors = MIN_PORT_MONITORS_DEFAULT; tcp_port_monitor_args.min_port_monitor_connections = MIN_PORT_MONITOR_CONNECTIONS_DEFAULT; #endif
/* handle command line parameters that don't change configs */ #ifdef X11 char *s, *temp; unsigned int x; //读取环境变量LC_ALL ,LC_CTYPE ,LANG
if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && *s) || ((s = getenv("LANG")) && *s)) { temp = (char *)malloc((strlen(s) + 1) * sizeof(char)); if (temp == NULL) { ERR("malloc failed"); } for (x = 0; x < strlen(s); x++) { temp[x] = tolower(s[x]); } if (strstr(temp, "utf-8") || strstr(temp, "utf8")) { utf8_mode = 1; } free(temp); } if (!setlocale(LC_CTYPE, "")) {//设定locale(LC_CTYPE)
ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL."); } #endif /* X11 */ while (1) {//这个循环,根据后面参数的不同采取不同的显示
int c = getopt(argc, argv, getopt_string);//分析命令行参数
if (c == -1) break;
switch (c) { case 'v': case 'V': print_version(); case 'c': /* if current_config is set to a strdup of CONFIG_FILE, free it (even * though free() does the NULL check itself;), then load optarg value */ if (current_config) free(current_config); current_config = strdup(optarg); break;
case 'h': printf ("Usage: %s [OPTION]...\n" "Conky is a system monitor that renders text on desktop or to own transparent\n" "window. Command line options will override configurations defined in config\n" "file.\n" " -V version\n" " -c FILE config file to load instead of " CONFIG_FILE "\n" " -d daemonize, fork to background\n" " -h help\n" #ifdef X11 " -a ALIGNMENT text alignment on screen, {top,bottom}_{left,right}\n" " -f FONT font to use\n" #ifdef OWN_WINDOW " -o create own window to draw\n" #endif #ifdef HAVE_XDBE " -b double buffer (prevents flickering)\n" #endif " -w WIN_ID window id to draw\n" " -x X x position\n" " -y Y y position\n" #endif /* X11 */ " -t TEXT text to render, remember single quotes, like -t '$uptime'\n" " -u SECS update interval\n" " -i NUM number of times to update Conky\n", argv[0]); return 0; #ifdef X11 case 'w': window.window = strtol(optarg, 0, 0); break; #endif /* X11 */
case '?': exit(EXIT_FAILURE); } } #ifdef X11 /* initalize X BEFORE we load config. (we need to so that 'screen' is set) */ init_X11();//在加载配置之前,初始化x
#endif /* X11 */
/* load current_config or CONFIG_FILE */
#ifdef CONFIG_FILE if (current_config == NULL) { /* load default config file */ char buf[256];
variable_substitute(CONFIG_FILE, buf, 256);//读取默认配置文件里的变量
if (buf[0] != '\0') current_config = strdup(buf);//复制字符串
} #endif
if (current_config != NULL && fopen((const char *)current_config, (const char *)"r")) load_config_file(current_config); else { set_default_configurations(); }//如果有用户的配置文件,就读取并设置
#ifdef MAIL_FILE if (current_mail_spool == NULL) { char buf[256]; variable_substitute(MAIL_FILE, buf, 256);
if (buf[0] != '\0') current_mail_spool = strdup(buf); } #endif
/* handle other command line arguments */
#if defined(__FreeBSD__) || defined (__OpenBSD__) || defined(__NetBSD__) optind = optreset = 1; #else optind = 0; #endif
#if defined(__FreeBSD__) if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY, "kvm_open")) == NULL) CRIT_ERR( "cannot read kvm"); #endif while (1) {//读取配置
int c = getopt(argc, argv, getopt_string);//分析命令行参数了
if (c == -1) break;
switch (c) { case 'd': fork_to_background = 1;//后台运行
break;
#ifdef X11 case 'f': set_first_font(optarg);//设置字体了
break; case 'a': text_alignment = string_to_alignment(optarg); break;
#ifdef OWN_WINDOW case 'o': own_window = 1; break; #endif #ifdef HAVE_XDBE case 'b': use_xdbe = 1; break; #endif #endif /* X11 */ case 't': if (text != original_text) free(text); text = strdup(optarg); convert_escapes(text);//转义字符
break;
case 'u': update_interval = strtod(optarg, 0);//将字符串转换为浮点数
break;
case 'i': total_run_times = strtod(optarg, 0); break; #ifdef X11 case 'x': gap_x = atoi(optarg);//将字符串转换为整型数
break;
case 'y': gap_y = atoi(optarg); break; #endif /* X11 */
case '?': exit(EXIT_FAILURE); } }
#ifdef X11 /* load font */ load_fonts();//加载字体
#endif /* X11 */
/* generate text and get initial size */ extract_variable_text(text);//计算文本,获取初始的大小
if (text != original_text) { free(text); } text = NULL; /* fork *///fork一个后台进程,也就是使conky作为后台运行
if (fork_to_background) { int pid = fork(); switch (pid) { case -1: ERR("can't fork() to background: %s", strerror(errno)); break;
case 0: /* child process */ sleep(1); fprintf(stderr,"\n");fflush(stderr); break;
default: /* parent process */ fprintf(stderr,"Conky: forked to background, pid is %d\n",pid); fflush(stderr); return 0; } }
update_uname();//更新系统版本信息
generate_text();//计算文本区
#ifdef X11 selected_font = 0; update_text_area(); /* to get initial size of the window */ //获取窗口的大小
init_window (own_window, text_width + border_margin * 2 + 1, text_height + border_margin * 2 + 1, set_transparent, background_colour, info.uname_s.nodename, argv, argc);//这个就设置了每一个info的显示
selected_font = 0; update_text_area(); /* to position text/window on screen */ #endif /* X11 */ //定位text窗口
#ifdef X11 #ifdef OWN_WINDOW if (own_window && !fixed_pos) { XMoveWindow(display, window.window, window.x, window.y); } if (own_window) { set_transparent_background(window.window); } #endif
create_gc();//这个做什么用?--是Graphic Context (GC)图形上下文
//在这里有提及
set_font();//设置字体
draw_stuff();//^_^,这里就开始显示了
#endif /* X11 */
/* Set signal handlers */ act.sa_handler = signal_handler;//建立信号处理部分
sigemptyset(&act.sa_mask);//初始化信号集
act.sa_flags = 0; #ifdef SA_RESTART act.sa_flags |= SA_RESTART; #endif
if ( sigaction(SIGINT,&act,&oact) < 0 || sigaction(SIGUSR1,&act,&oact) < 0 || sigaction(SIGTERM,&act,&oact) < 0 ) { ERR("error setting signal handler: %s", strerror(errno) ); } #ifdef AUDACIOUS if ( (create_audacious_thread() !=0) ) { CRIT_ERR("unable to create audacious thread!"); } #endif
main_loop();//主要的循环
#ifdef AUDACIOUS if ( info.audacious.thread && (destroy_audacious_thread()!=0) ) ERR("error destroying audacious thread"); #endif
#if defined(__FreeBSD__) kvm_close(kd); #endif return 0; }
|