Ejemplo n.º 1
0
bool slideshow::run()
{
	if ( !init_graphics() )
	{
		return false;
	}

	SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);

	// Have the first image ready in the cache before starting the timer
	m_image_cache->lookup(0);
	
	m_image_index = 0;
	m_prev_image_index = -1;

	start_timer();

	while(1)
	{
		if ( m_prev_image_index != m_image_index )
		{
			show_image();
			m_prev_image_index = m_image_index;
		}

		// After the image is shown and before we wait for a key, 
		// get the prev and next images ready.  We most likely only
		// need to load one of these, as the other will already be
		// in the cache.
		if ( m_image_index > 0 )
			m_image_cache->lookup( m_image_index - 1 );
		if ( m_image_index < m_file_list->count() - 1 )
			m_image_cache->lookup( m_image_index + 1 );

		if ( m_options->show_ready_indicator )
		{
			SDL_Rect rc;
			rc.x = m_sdl->w - 5;
			rc.y = 0;
			rc.w = 5;
			rc.h = 5;
			SDL_FillRect(m_sdl, &rc, SDL_MapRGB(m_sdl->format, 0, 255, 0));
			SDL_UpdateRects(m_sdl, 1, &rc);
		}

		user_command cmd = wait_for_command();

		switch(cmd)
		{
		case c_quit:
			stop_timer();
			return true;
		case c_first_slide:
			stop_timer();
			m_image_index = 0;
			break;
		case c_last_slide:
			stop_timer();
			m_image_index = m_file_list->count() - 1;
			break;
		case c_prev_slide:
			stop_timer();
			if ( m_image_index > 0 )
				m_image_index--;
			break;
		case c_next_slide:
			stop_timer();
			// fall through
		case c_timer_advance:
			if ( m_image_index < m_file_list->count() - 1 )
				m_image_index++;
			break;
		case c_redraw:
			break;
		}

		// stop the auto-advance when the last picture is about to be shown
		if ( m_image_index == m_file_list->count() - 1 )
			stop_timer();
	}

	return true;
}
Ejemplo n.º 2
0
static int sh_create_sub()
{
  pid_t res;
  volatile int   retval = 0;

  SH_MUTEX_LOCK(mutex_sub);

#if !defined(O_NONBLOCK)
#if defined(O_NDELAY)
#define O_NONBLOCK  O_NDELAY
#else
#define O_NONBLOCK  0
#endif
#endif

  if (sh_child_pid == -1)
    {
      sigset_t signal_set_new;
      sigset_t signal_set_old;

      sigfillset ( &signal_set_new );
      sigemptyset( &signal_set_old );

      /* Create pipes. */
      res = pipe (parent2child);
      if (res == 0)
	res = pipe (child2parent);
      
      if (res != 0)
	goto out;

      SH_SETSIGMASK(SIG_BLOCK, &signal_set_new, &signal_set_old);

      res = fork();
      
      if (res == 0)
	{
	  /* Child process. */
#ifdef _SC_OPEN_MAX
	  int fdlimit = sysconf (_SC_OPEN_MAX);
#else
#ifdef OPEN_MAX
	  int fdlimit = OPEN_MAX;
#else
	  int fdlimit = _POSIX_OPEN_MAX;
#endif
#endif
	  int sflags, i, fd = 0;
	  struct sigaction act;

	  /* zero private information 
	   */
	  memset(skey, 0, sizeof(sh_key_t)); 

	  close (parent2child[1]);
	  close (child2parent[0]);
	  
	  sflags = fcntl(parent2child[0], F_GETFL, 0);
	  fcntl(parent2child[0], F_SETFL, sflags | O_NONBLOCK);
	  sflags = fcntl(child2parent[1], F_GETFL, 0);
	  fcntl(child2parent[1], F_SETFL, sflags | O_NONBLOCK);

	  /* close inherited file descriptors 
	   */
	  if (fdlimit < 0) 
	    fdlimit = 20;  /* POSIX lower limit */
	  while (fd < fdlimit)
	    {
	      if (fd != parent2child[0] && fd != child2parent[1])
		close(fd);
	      ++fd;
	    }

	  /*
	  for (i = 0; i < 3; ++i)
	    {
	      if ( fcntl(i, F_GETFL, 0) == (-1))
		(void) open(_("/dev/null"), O_RDWR, 0);
	    }
	  */

	  /* reset signal handling 
	   */
	  act.sa_handler = SIG_DFL;
	  act.sa_flags   = 0;
	  sigemptyset( &act.sa_mask );
	  
	  for (i = 1; i < NSIG; ++i)
	    sigaction(i, &act, NULL);
	  SH_SETSIGMASK(SIG_UNBLOCK, &signal_set_new, NULL);

	  wait_for_command();
	  
	  _exit(0);
	}
      else if (res > 0)
	{
	  /* Parent process. */
	  int sflags;

	  SH_SETSIGMASK(SIG_SETMASK, &signal_set_old, NULL);

	  close (parent2child[0]);
	  close (child2parent[1]);
	  
	  sflags = fcntl(parent2child[1], F_GETFL, 0);
	  fcntl(parent2child[1], F_SETFL, sflags | O_NONBLOCK);
	  sflags = fcntl(child2parent[0], F_GETFL, 0);
	  fcntl(child2parent[0], F_SETFL, sflags | O_NONBLOCK);

	  sh_child_pid = res;
	}
      else
	{
	  /* Failure. */

	  SH_SETSIGMASK(SIG_SETMASK, &signal_set_old, NULL);

	  close (parent2child[0]);
	  close (parent2child[1]);

	  close (child2parent[0]);
	  close (child2parent[1]);
	  
	  retval = -1;
	}
    }

 out:
  ; /* 'label at end of compound statement' */
  SH_MUTEX_UNLOCK(mutex_sub);
  return retval;
}