Esempio n. 1
0
int main(int argc, char **argv)
{
	int err = 0;
	int id2 = 0, c;
	double yk = 0.0; /* controller output */
	int target_tz_index;

	if (geteuid() != 0) {
		printf("TMON needs to be run as root\n");
		exit(EXIT_FAILURE);
	}

	while ((c = getopt_long(argc, argv, "c:dlht:vgz:", opts, &id2)) != -1) {
		switch (c) {
		case 'c':
			no_control = 0;
			strncpy(ctrl_cdev, optarg, CDEV_NAME_SIZE);
			break;
		case 'd':
			start_daemon_mode();
			printf("Run TMON in daemon mode\n");
			break;
		case 't':
			ticktime = strtod(optarg, NULL);
			if (ticktime < 1)
				ticktime = 1;
			break;
		case 'l':
			printf("Logging data to /var/tmp/tmon.log\n");
			logging = 1;
			break;
		case 'h':
			usage();
			break;
		case 'v':
			version();
			break;
		case 'g':
			debug_on = 1;
			break;
		case 'z':
			target_thermal_zone = strtod(optarg, NULL);
			break;
		default:
			break;
		}
	}
	if (pthread_mutex_init(&input_lock, NULL) != 0) {
		fprintf(stderr, "\n mutex init failed, exit\n");
		return 1;
	}
	start_syslog();
	if (signal(SIGINT, tmon_sig_handler) == SIG_ERR)
		syslog(LOG_DEBUG, "Cannot handle SIGINT\n");
	if (signal(SIGTERM, tmon_sig_handler) == SIG_ERR)
		syslog(LOG_DEBUG, "Cannot handle SIGINT\n");

	if (probe_thermal_sysfs()) {
		pthread_mutex_destroy(&input_lock);
		closelog();
		return -1;
	}
	initialize_curses();
	setup_windows();
	signal(SIGWINCH, resize_handler);
	show_title_bar();
	show_sensors_w();
	show_cooling_device();
	update_thermal_data();
	show_data_w();
	prepare_logging();
	init_thermal_controller();

	nodelay(stdscr, TRUE);
	err = pthread_create(&event_tid, NULL, &handle_tui_events, NULL);
	if (err != 0) {
		printf("\ncan't create thread :[%s]", strerror(err));
		tmon_cleanup();
		exit(EXIT_FAILURE);
	}

	/* validate range of user selected target zone, default to the first
	 * instance if out of range
	 */
	target_tz_index = zone_instance_to_index(target_thermal_zone);
	if (target_tz_index < 0) {
		target_thermal_zone = ptdata.tzi[0].instance;
		syslog(LOG_ERR, "target zone is not found, default to %d\n",
			target_thermal_zone);
	}
	while (1) {
		sleep(ticktime);
		show_title_bar();
		show_sensors_w();
		update_thermal_data();
		if (!dialogue_on) {
			show_data_w();
			show_cooling_device();
		}
		cur_thermal_record++;
		time_elapsed += ticktime;
		controller_handler(trec[0].temp[target_tz_index] / 1000,
				&yk);
		trec[0].pid_out_pct = yk;
		if (!dialogue_on)
			show_control_w();
		if (tmon_exit)
			break;
	}
	tmon_cleanup();
	return 0;
}
   virtual void execute(t_uint32 p_index,service_ptr_t<service_base> p_callback)
   {
      switch ( p_index )
      {
      case CMD_SAVE_1:
      case CMD_SAVE_2:
      case CMD_SAVE_3:
      case CMD_SAVE_4:
      case CMD_SAVE_5:
         {
            if ( cfg_allow_store )
            {
               int n = p_index - CMD_SAVE_1;
               console::printf( "Storing to slot %d", n + 1 );

               RECT rc;
               GetWindowRect( core_api::get_main_window(), &rc );
               /*
               console::printf( "bottom = %d, left = %d, top = %d, right = %d",
                  rc.bottom, rc.left, rc.top, rc.right );
               */

               *slots[n] = rc;

               /*
               console::printf( "bottom = %d, left = %d, top = %d, right = %d",
                  slots[n]->get_value().bottom, 
                  slots[n]->get_value().left, 
                  slots[n]->get_value().top, 
                  slots[n]->get_value().right );
                  */
            }
         }
         break;

      case CMD_REST_1:
      case CMD_REST_2:
      case CMD_REST_3:
      case CMD_REST_4:
      case CMD_REST_5:
         {
            int n = p_index - CMD_REST_1;

            if ( is_slot_valid( n ) )
            {
               int cx = slots[n]->get_value().right - slots[n]->get_value().left;
               int cy = slots[n]->get_value().bottom - slots[n]->get_value().top;

               console::printf( "Restoring slot %d", n + 1 );

               SetWindowPos( core_api::get_main_window(), NULL,
                  slots[n]->get_value().left, 
                  slots[n]->get_value().top, 
                  cx,
                  cy,
                  SWP_NOZORDER );
            }
            else
            {
               console::printf( "No valid data in slot %d", n + 1 );
            }
         }
         break;

      case CMD_NOTITLE:
         if ( !has_title_bar() )
         {
            show_title_bar();
            cfg_hide = 0;
         }
         else
         {
            hide_title_bar();
            cfg_hide = 1;
         }
         break;
      case CMD_SCRIPT1:
         run_script( g_script1, tabsize( g_script1 ) );
         break;

      case CMD_SCRIPT2:
         run_script( g_script2, tabsize( g_script2 ) );
         break;
      }
   }