Ejemplo n.º 1
0
unsigned char conf_run_menu ( confitem_t *toplevel ) {
  confitem_t *page = toplevel;
  unsigned int sel = 0;
  unsigned int first_visible = 0;
  unsigned char max_visible = 12;
  SDL_Event event;
  confitem_t *lastpage = NULL;

  while ( 1 ) {

    if ( ! page ) {
      page = pages;
      sel = 0;
      first_visible = 0;
    }

    if ( lastpage != page ) {
      conf_prepare_page ( page );
      lastpage = page;
    }

    conf_display_page ( page, sel, first_visible, max_visible );

    // check for input
    while ( SDL_WaitEvent ( &event ) ) {

      switch ( event.type ) {

      //case SDL_KEYUP:
      case SDL_KEYDOWN:

	if ( event.key.keysym.sym == SDLK_UP ) {

	  do {

	    if ( sel ) {
	      sel--;

	      if ( sel < first_visible ) {
		first_visible--;
	      }

	    }

	  } while ( page [ sel ].type == ct_nil );

	} else if ( event.key.keysym.sym == SDLK_DOWN ) {

	  do {

	    if ( page [ sel + 1 ].text ) {
	      sel++;

	      // ensure visibility
	      if ( sel >= first_visible + max_visible ) {
		first_visible++;
	      }

	    }

	  } while ( page [ sel ].type == ct_nil );

	} else if ( event.key.keysym.sym == SDLK_PAGEUP ) {
	  page = NULL;

	} else if ( event.key.keysym.sym == SDLK_LEFT || event.key.keysym.sym == SDLK_RIGHT ) {

	  unsigned char left = 0;
	  if ( event.key.keysym.sym == SDLK_LEFT ) {
	    left = 1;
	  }

	  switch ( page [ sel ].type ) {

	  case ct_cpu_speed:
	    {
	      int v = pnd_conf_get_as_int ( g_conf, page [ sel ].key );
	      if ( v == PND_CONF_BADNUM ) {
		v = 500;
	      }

	      if ( left ) {
		if ( v > 30 ) {
		  v -= 10;
		}
	      } else {
		v += 10;
	      }

	      char buffer [ 20 ];
	      sprintf ( buffer, "%u", v );
	      pnd_conf_set_char ( g_conf, page [ sel ].key, buffer );

	    }
	    break;

	  case ct_visible_tab_list:
	    {
	      if ( g_categorycount ) {
		char *v = pnd_conf_get_as_char ( g_conf, page [ sel ].key );
		if ( v ) {
		  unsigned char n = 0;
		  for ( n = 0; n < g_categorycount; n++ ) {
		    if ( strcmp ( v, g_categories [ n ] -> catname ) == 0 ) {
		      break;
		    }
		  }
		  if ( n < g_categorycount ) {
		    if ( left && n ) {
		      pnd_conf_set_char ( g_conf, page [ sel ].key, g_categories [ n - 1 ] -> catname );
		    } else if ( ! left && n + 1 < g_categorycount ) {
		      pnd_conf_set_char ( g_conf, page [ sel ].key, g_categories [ n + 1 ] -> catname );
		    }
		  } else {
		    pnd_conf_set_char ( g_conf, page [ sel ].key, g_categories [ 0 ] -> catname );
		  }
		} else {
		  pnd_conf_set_char ( g_conf, page [ sel ].key, g_categories [ 0 ] -> catname );
		}
	      } // if category count
	    }
	    break;

	  case ct_boolean:
	    {
	      int v = pnd_conf_get_as_int ( g_conf, page [ sel ].key );
	      if ( v == PND_CONF_BADNUM ) {
		v = 0;
	      }
	      if ( v ) {
		v = 0;
	      } else {
		v = 1;
	      }

	      char buffer [ 20 ];
	      sprintf ( buffer, "%u", v );

	      pnd_conf_set_char ( g_conf, page [ sel ].key, buffer );
	    }
	    break;

	  case ct_filename:
	    break;

	  case ct_nil:
	  case ct_go_manage_categories:
	  case ct_go_manage_skins:
	  case ct_switch_page:
	  case ct_reset:
	  case ct_exit:
	    break;

	  } // switch

	} else if ( event.key.keysym.sym == SDLK_ESCAPE ) {
	  emit_and_quit ( MM_QUIT );

	} else if ( event.key.keysym.sym == SDLK_RETURN || event.key.keysym.sym == SDLK_END ) { // return, or "B"

	  switch ( page [ sel ].type ) {
	  case ct_exit:
	    {
	      return ( 1 /* always cause restart for now */ );
	    }
	    break;
	  case ct_reset:
	    {
	      conf_reset_to_default ( g_conf );
	      page = NULL;
	      sel = 0;
	    }
	    break;
	  case ct_switch_page:
	    page = page [ sel ].newhead;
	    sel = 0; // should use a stack..
	    break;
	  case ct_go_manage_categories:
	    ui_manage_categories();
	    break;
	  case ct_go_manage_skins:
	    if ( ui_pick_skin() ) {
	      emit_and_quit ( MM_RESTART );
	    }
	    break;
	  case ct_filename:
	    break;
	  case ct_nil:
	  case ct_visible_tab_list:
	  case ct_cpu_speed:
	  case ct_boolean:
	    break;
	  } // switch

	} else {
	  // nada
	}

	break; // case sdl_key_up

      } // switch what SDL event

      break; // get out of sdl-wait-event loop
    } // while events

  } // while forever

  return ( 1 /* always cause restart for now */ );
}
Ejemplo n.º 2
0
void consume_configuration ( void ) {

  // attempt to fetch a sensible default searchpath for configs
  configpath = pnd_conf_query_searchpath();

  // attempt to fetch the apps config to pick up a searchpath
  pnd_conf_handle apph;

  apph = pnd_conf_fetch_by_id ( pnd_conf_apps, configpath );

  if ( apph ) {

    overridespath = pnd_conf_get_as_char ( apph, PND_PXML_OVERRIDE_KEY );

    if ( ! overridespath ) {
      overridespath = PND_PXML_OVERRIDE_SEARCHPATH;
    }

    notifypath = pnd_conf_get_as_char ( apph, PND_APPS_NOTIFY_KEY );

    if ( ! notifypath ) {
      notifypath = PND_APPS_NOTIFYPATH;
    }

  } else {
    // couldn't find a useful app search path so use the default
    overridespath = PND_PXML_OVERRIDE_SEARCHPATH;
    notifypath = PND_APPS_NOTIFYPATH;
  }

  // attempt to figure out where to drop dotfiles .. now that we're going
  // multi-target we see the limit of my rudimentary conf-file parser; should
  // just parse to an array of targets, rather that hardcoding two, but
  // on the other hand, don't likely see the need for more than two? (famous
  // last words.)
  pnd_conf_handle desktoph;

  desktoph = pnd_conf_fetch_by_id ( pnd_conf_desktop, configpath );

  // for 'desktop' main applications
  if ( desktoph ) {
    desktop_dotdesktoppath = pnd_conf_get_as_char ( desktoph, PND_DESKTOP_DOTDESKTOP_PATH_KEY );
    desktop_iconpath = pnd_conf_get_as_char ( desktoph, PND_DESKTOP_ICONS_PATH_KEY );
    desktop_appspath = pnd_conf_get_as_char ( desktoph, PND_DESKTOP_SEARCH_KEY );
    info_dotdesktoppath = pnd_conf_get_as_char ( desktoph, "info.dotdesktoppath" );
  }

  if ( ! desktop_dotdesktoppath ) {
    desktop_dotdesktoppath = PND_DESKTOP_DOTDESKTOP_PATH_DEFAULT;
  }

  if ( ! desktop_iconpath ) {
    desktop_iconpath = PND_DESKTOP_ICONS_PATH_DEFAULT;
  }

  if ( ! desktop_appspath ) {
    desktop_appspath = PND_DESKTOP_SEARCH_PATH_DEFAULT;
  }

  // for 'menu' applications
  if ( desktoph ) {
    menu_dotdesktoppath = pnd_conf_get_as_char ( desktoph, PND_MENU_DOTDESKTOP_PATH_KEY );
    menu_iconpath = pnd_conf_get_as_char ( desktoph, PND_MENU_ICONS_PATH_KEY );
    menu_appspath = pnd_conf_get_as_char ( desktoph, PND_MENU_SEARCH_KEY );
  }

  // info
  if ( desktoph ) {
    g_info_p = pnd_conf_get_as_int_d ( desktoph, "info.emit_info", 0 );
  }

  /* try to locate a runscript and optional hupscript
   */

  if ( apph ) {
    run_searchpath = pnd_conf_get_as_char ( apph, PND_PNDRUN_SEARCHPATH_KEY );
    run_script = pnd_conf_get_as_char ( apph, PND_PNDRUN_KEY );
    pndrun = NULL;

    if ( ! run_searchpath ) {
      run_searchpath = PND_APPS_SEARCHPATH;
      run_script = PND_PNDRUN_FILENAME;
    }

    if ( pnd_conf_get_as_int ( apph, PNDNOTIFYD_LOGLEVEL ) != PND_CONF_BADNUM ) {
      if ( pnd_log_do_buried_logging() == 0 ) {
	pnd_log_set_filter ( pnd_conf_get_as_int ( apph, PNDNOTIFYD_LOGLEVEL ) );
	pnd_log ( pndn_rem, "config file causes loglevel to change to %u", pnd_log_get_filter() );
      } else {
	pnd_log ( pndn_rem, "-l command line suppresses log level change in config file\n" );
      }
    }

  } else {
    run_searchpath = NULL;
    run_script = NULL;
    pndrun = PND_PNDRUN_DEFAULT;
  }

  if ( ! pndrun ) {
    pndrun = pnd_locate_filename ( run_searchpath, run_script );

    if ( pndrun ) {
      pndrun = strdup ( pndrun ); // so we don't just use the built in buffer; next locate will overwrite it
    } else {
      pndrun = PND_PNDRUN_DEFAULT;
    }

  }

  if ( desktoph && run_searchpath ) {
    char *t;

    if ( ( t = pnd_conf_get_as_char ( desktoph, PND_PNDHUP_KEY ) ) ) {
      pndhup = pnd_locate_filename ( run_searchpath, t );

      if ( pndhup ) {
	pndhup = strdup ( pndhup ); // so we don't just use the built in buffer; next locate will overwrite it
      }

#if 0 // don't enable this; if no key in config, we don't want to bother hupping
    } else {
      pndhup = pnd_locate_filename ( run_searchpath, PND_PNDHUP_FILENAME );
#endif
    }

  }

  // debug
  if ( run_searchpath ) pnd_log ( pndn_rem, "Locating pnd run in %s\n", run_searchpath );
  if ( run_script ) pnd_log ( pndn_rem, "Locating pnd runscript as %s\n", run_script );
  if ( pndrun ) pnd_log ( pndn_rem, "pndrun is %s\n", pndrun );
  if ( pndhup ) {
    pnd_log ( pndn_rem, "pndhup is %s\n", pndhup );
  } else {
    pnd_log ( pndn_rem, "No pndhup found (which is fine.)\n" );
  }

  /* cheap hack, maybe it'll help when pndnotifyd is coming up before the rest of the system and before
   * the user is formally logged in
   */
  pnd_log ( pndn_rem, "Setting a default $HOME to non-root user\n" );

  // first, try to see if known-username maps to a homedir; if so, just use that!
  // otherwise, pick first non-root homedir and assume .. or we start blaring .desktops
  // out to all users like idiots
  unsigned char got_user_homedir = 0;

  if ( g_username [ 0 ] ) {
    struct stat homedir;
    char path [ PATH_MAX ];

    sprintf ( path, "/home/%s", g_username );

    // does this made up path exist?
    if ( stat ( path, &homedir ) == 0 ) {

      // and its a dir?
      if ( S_ISDIR(homedir.st_mode) ) {
	pnd_log ( pndn_rem, "  User [%s] matches path [%s], going with '%s'\n", g_username, path, path );
	setenv ( "HOME", path, 1 /* overwrite */ );
	got_user_homedir = 1;
      } // and its a dir?

    } // guessing a homedirname..

  } // got a username?

  // if guessing a path was no good, just try finding one
  if ( got_user_homedir == 0 ) {
    DIR *dir;

    if ( ( dir = opendir ( "/home" ) ) ) {
      struct dirent *dirent;

      while ( ( dirent = readdir ( dir ) ) ) {
	pnd_log ( pndn_rem, "  Scanning user homedir '%s'\n", dirent -> d_name );

	// file is a .desktop?
	if ( dirent -> d_name [ 0 ] == '.' ) {
	  continue;
	} else if ( strcmp ( dirent -> d_name, "root" ) == 0 ) {
	  continue;
	}

	// a non-root user is found
	char buffer [ 200 ];
	sprintf ( buffer, "/home/%s", dirent -> d_name );
	pnd_log ( pndn_rem, "  Going with '%s'\n", buffer );
	setenv ( "HOME", buffer, 1 /* overwrite */ );
	break;

      } // while iterating through dir listing

      closedir ( dir );
    } // opendir?

  } // scope

  /* handle globbing or variable substitution
   */
  desktop_dotdesktoppath = pnd_expand_tilde ( strdup ( desktop_dotdesktoppath ) );
  desktop_iconpath = pnd_expand_tilde ( strdup ( desktop_iconpath ) );
  mkdir ( desktop_dotdesktoppath, 0777 );
  mkdir ( desktop_iconpath, 0777 );

  if ( info_dotdesktoppath ) {
    info_dotdesktoppath = pnd_expand_tilde ( strdup ( info_dotdesktoppath ) );
    mkdir ( info_dotdesktoppath, 0777 );
  }

  if ( menu_dotdesktoppath ) {
    menu_dotdesktoppath = pnd_expand_tilde ( strdup ( menu_dotdesktoppath ) );
    mkdir ( menu_dotdesktoppath, 0777 );
  }
  if ( menu_iconpath ) {
    menu_iconpath = pnd_expand_tilde ( strdup ( menu_iconpath ) );
    mkdir ( menu_iconpath, 0777 );
  }

  // done
  return;
}
Ejemplo n.º 3
0
void conf_display_page ( confitem_t *page, unsigned int selitem, unsigned int first_visible, unsigned int max_visible ) {
  extern TTF_Font *g_big_font;
  extern TTF_Font *g_tab_font;

#define MAXRECTS 200
  SDL_Rect rects [ MAXRECTS ];
  SDL_Rect *dest = rects;
  bzero ( dest, sizeof(SDL_Rect)*MAXRECTS );

  unsigned short int tx, ty;

  // background
  if ( g_imagecache [ IMG_BACKGROUND_800480 ].i ) {
    dest -> x = 0;
    dest -> y = 0;
    dest -> w = sdl_realscreen -> w;
    dest -> h = sdl_realscreen -> h;
    SDL_BlitSurface ( g_imagecache [ IMG_BACKGROUND_800480 ].i, NULL /* whole image */, sdl_realscreen, dest /* 0,0 */ );
    dest++;
  }

  // title
  //
  // <items>
  // description
  // default
  //
  // controls help

  // title
  dest += conf_render_text ( g_big_font, "Minimenu Configuration", dest, 10, 10, CONF_UNSELECTED );
  dest += conf_render_line ( dest, 45 );

  // scrollable hints
  {

    // up
    if ( first_visible > 0 ) {
      dest -> x = 10;
      dest -> y = 65;
      SDL_BlitSurface ( g_imagecache [ IMG_ARROW_UP ].i, NULL /* whole image */, sdl_realscreen, dest );
      dest++;
    } // scroll arrow up

    // down
    if ( first_visible + max_visible < conf_determine_pagelength ( page ) ) {
      dest -> x = 10;
      dest -> y = 345;
      SDL_BlitSurface ( g_imagecache [ IMG_ARROW_DOWN ].i, NULL /* whole image */, sdl_realscreen, dest );
      dest++;
    } // scroll arrow up

  } // scrollbar

  // items
  tx = 50; ty = 70;
  unsigned char counter = first_visible;
  while ( page [ counter ].text ) {

    // item
    conf_render_text ( g_tab_font, page [ counter ].text, dest, tx, ty, counter == selitem ? CONF_SELECTED : CONF_UNSELECTED );

    // value
    switch ( page [ counter ].type ) {
    case ct_switch_page:
      break;
    case ct_reset:
      break;
    case ct_visible_tab_list:
      {
	char *v = pnd_conf_get_as_char ( g_conf, page [ counter ].key );
	if ( v ) {
	  conf_render_text ( g_tab_font, v, dest, tx + 400, ty, counter == selitem ? CONF_SELECTED : CONF_UNSELECTED );
	} else {
	  conf_render_text ( g_tab_font, "Not specified", dest, tx + 400, ty, counter == selitem ? CONF_SELECTED : CONF_UNSELECTED );
	}
      }
      break;
    case ct_cpu_speed:
      {
	int v = pnd_conf_get_as_int ( g_conf, page [ counter ].key );
	conf_render_text ( g_tab_font, conf_format_int ( v, page [ counter ].type ), dest, tx + 400, ty, counter == selitem ? CONF_SELECTED : CONF_UNSELECTED );
      }
      break;
    case ct_boolean:
      {
	int v = pnd_conf_get_as_int ( g_conf, page [ counter ].key );
	conf_render_text ( g_tab_font, conf_format_int ( v, page [ counter ].type ), dest, tx + 400, ty, counter == selitem ? CONF_SELECTED : CONF_UNSELECTED );
      }
      break;
    case ct_filename:
      conf_render_text ( g_tab_font, page [ counter ].def, dest, tx + 400, ty, counter == selitem ? CONF_SELECTED : CONF_UNSELECTED );
      break;
    case ct_exit:
      break;
    case ct_go_manage_categories:
    case ct_go_manage_skins:
    case ct_nil:
      break;
    } // switch

    // far enough?
    if ( counter - first_visible >= max_visible - 1 ) {
      break;
    }

    // next line
    ty += 25;
    counter++;
  } // while

  // description and default
  if ( page [ selitem ].desc ) {
    dest += conf_render_text ( g_tab_font, page [ selitem ].desc, dest, 380, 400, CONF_UNSELECTED );
  }
  if ( page [ selitem ].def ) {
    char buffer [ 100 ] = "Default: ";

    switch ( page [ selitem ].type ) {
    case ct_boolean:
      sprintf ( buffer + strlen ( buffer ), "%s", conf_format_int ( atoi ( page [ selitem ].def ), ct_boolean ) );
      break;
    case ct_cpu_speed:
      sprintf ( buffer + strlen ( buffer ), "%s", conf_format_int ( atoi ( page [ selitem ].def ), ct_cpu_speed ) );
      break;
    case ct_filename:
      sprintf ( buffer + strlen ( buffer ), "%s", page [ selitem ].def );
      break;
    case ct_nil:
    case ct_switch_page:
    case ct_reset:
    case ct_exit:
    case ct_visible_tab_list:
    case ct_go_manage_categories:
    case ct_go_manage_skins:
      break;
    } // switch

    dest += conf_render_text ( g_tab_font, buffer, dest, 380, 420, CONF_UNSELECTED );
  } else {
    dest += conf_render_text ( g_tab_font, "No default value", dest, 380, 420, CONF_UNSELECTED );
  }

  // cursor's conf item count number - not for top level, just the sublevels
  if ( page != pages ) {
    char buffer [ 40 ];
    sprintf ( buffer, "Config item %d of %d", selitem + 1, conf_determine_pagelength ( page ) );
    /*dest += */conf_render_text ( g_tab_font, buffer, dest, 380, 440, CONF_UNSELECTED );
  }

  // help
  dest += conf_render_line ( dest, 380 );
  dest += conf_render_text ( g_tab_font, "D-pad Up/down; Y return to index", dest, 10, 400, CONF_UNSELECTED );
  dest += conf_render_text ( g_tab_font, "Left and right to alter selected item", dest, 10, 420, CONF_UNSELECTED );
  dest += conf_render_text ( g_tab_font, "B or Enter to activate an option", dest, 10, 440, CONF_UNSELECTED );

  // update all the rects and send it all to sdl
  // - at this point, we could probably just do 1 rect, of the
  //   whole screen, and be faster :/
  SDL_UpdateRects ( sdl_realscreen, dest - rects, rects );

  return;
}
Ejemplo n.º 4
0
int main ( int argc, char *argv[] ) {
  int i;
  int logall = -1; // -1 means normal logging rules; >=0 means log all!

  for ( i = 1; i < argc; i++ ) {

    if ( argv [ i ][ 0 ] == '-' && argv [ i ][ 1 ] == 'd' ) {
      //printf ( "Going daemon mode. Silent running.\n" );
      g_daemon_mode = 1;
    } else if ( argv [ i ][ 0 ] == '-' && argv [ i ][ 1 ] == 'l' ) {

      if ( isdigit ( argv [ i ][ 2 ] ) ) {
	unsigned char x = atoi ( argv [ i ] + 2 );
	if ( x >= 0 &&
	     x < pndn_none )
	{
	  logall = x;
	}
      } else {
	logall = 0;
      }
    } else {
      usage ( argv );
      exit ( 0 );
    }

  } // for

  /* enable logging?
   */
  pnd_log_set_pretext ( "pndevmapperd" );
  pnd_log_set_flush ( 1 );

  if ( logall == -1 ) {
    // standard logging; non-daemon versus daemon

    if ( g_daemon_mode ) {
      // nada
    } else {
      pnd_log_set_filter ( pndn_rem );
      pnd_log_to_stdout();
    }

  } else {
    FILE *f;

    f = fopen ( "/tmp/pndevmapperd.log", "w" );

    if ( f ) {
      pnd_log_set_filter ( logall );
      pnd_log_to_stream ( f );
      pnd_log ( pndn_rem, "logall mode - logging to /tmp/pndevmapperd.log\n" );
    }

    if ( logall == pndn_debug ) {
      pnd_log_set_buried_logging ( 1 ); // log the shit out of it
      pnd_log ( pndn_rem, "logall mode 0 - turned on buried logging\n" );
    }

  } // logall

  pnd_log ( pndn_rem, "%s built %s %s", argv [ 0 ], __DATE__, __TIME__ );

  pnd_log ( pndn_rem, "log level starting as %u", pnd_log_get_filter() );

  // basic daemon set up
  if ( g_daemon_mode ) {

    // set a CWD somewhere else
    chdir ( "/tmp" );

    // detach from terminal
    if ( ( i = fork() ) < 0 ) {
      pnd_log ( pndn_error, "ERROR: Couldn't fork()\n" );
      exit ( i );
    }
    if ( i ) {
      exit ( 0 ); // exit parent
    }
    setsid();

    // umask
    umask ( 022 ); // emitted files can be rwxr-xr-x

  } // set up daemon

  /* hmm, seems to not like working right after boot.. do we depend on another daemon or
   * on giving kernel time to init something, or ... wtf?
   * -- lets give the system some time to wake up
   */
  { // delay

    // this one works for pndnotifyd, which actually needs INOTIFYH..
    //

    // check if inotify is awake yet; if not, try waiting for awhile to see if it does
    pnd_log ( pndn_rem, "Starting INOTIFY test; should be instant, but may take awhile...\n" );

    if ( ! pnd_notify_wait_until_ready ( 120 /* seconds */ ) ) {
      pnd_log ( pndn_error, "ERROR: INOTIFY refuses to be useful and quite awhile has passed. Bailing out.\n" );
      return ( -1 );
    }

    pnd_log ( pndn_rem, "INOTIFY seems to be useful, whew.\n" );

    // pndnotifyd also waits for user to log in .. pretty excessive, especially since
    // what if user wants to close the lid while at the log in screen? for now play the
    // odds as thats pretty unliekly usage scenariom but is clearly not acceptible :/
    //

    // wait for a user to be logged in - we should probably get hupped when a user logs in, so we can handle
    // log-out and back in again, with SDs popping in and out between..
    pnd_log ( pndn_rem, "Checking to see if a user is logged in\n" );
    char tmp_username [ 128 ];
    while ( 1 ) {
      if ( pnd_check_login ( tmp_username, 127 ) ) {
	break;
      }
      pnd_log ( pndn_debug, "  No one logged in yet .. spinning.\n" );
      sleep ( 2 );
    } // spin
    pnd_log ( pndn_rem, "Looks like user '%s' is in, continue.\n", tmp_username );

  } // delay

  /* inhale config or die trying
   */
  char *configpath;

  // attempt to fetch a sensible default searchpath for configs
  configpath = pnd_conf_query_searchpath();

  // attempt to fetch the apps config. since it finds us the runscript
  pnd_conf_handle evmaph;

  evmaph = pnd_conf_fetch_by_id ( pnd_conf_evmap, configpath );

  if ( ! evmaph ) {
    // couldn't locate conf, just bail
    pnd_log ( pndn_error, "ERROR: Couldn't locate conf file\n" );
    exit ( -1 );
  }

  /* iterate across conf, stocking the event map
   */
  void *n = pnd_box_get_head ( evmaph );

  while ( n ) {
    char *k = pnd_box_get_key ( n );
    //printf ( "key %s\n", k );

    if ( strncmp ( k, "keys.", 5 ) == 0 ) {
      k += 5;

      // keys should really push push generic-events onto the table, since they;'re just a special case of them
      // to make things easier to read

      // figure out which keycode we're talking about
      keycode_t *p = keycodes;
      while ( p -> keycode != -1 ) {
	if ( strcasecmp ( p -> keyname, k ) == 0 ) {
	  break;
	}
	p++;
      }

      if ( p -> keycode != -1 ) {
	g_evmap [ g_evmap_max ].key_p = 1;    // its a key, not an event
	g_evmap [ g_evmap_max ].reqs = p;     // note the keycode

	// note the script to activate in response
	if ( strchr ( n, ' ' ) ) {
	  char *foo = strdup ( n );
	  char *t = strchr ( foo, ' ' );
	  *t = '\0';
	  g_evmap [ g_evmap_max ].script = foo;
	  g_evmap [ g_evmap_max ].maxhold = atoi ( t + 1 );
	} else {
	  g_evmap [ g_evmap_max ].script = n;
	  g_evmap [ g_evmap_max ].maxhold = 0;
	}

	pnd_log ( pndn_rem, "Registered key %s [%d] to script %s with maxhold %d\n",
		  p -> keyname, p -> keycode, (char*) n, g_evmap [ g_evmap_max ].maxhold );

	g_evmap_max++;
      } else {
	pnd_log ( pndn_warning, "WARNING! Key '%s' is not handled by pndevmapperd yet! Skipping.", k );
      }

    } else if ( strncmp ( k, "events.", 7 ) == 0 ) {
      k += 7;

      // yes, key events could really be defined in this generic sense, and really we could just let people
      // put the code and so on right in the conf, but trying to keep it easy on people; maybe should
      // add a 'generic' section to conf file and just let folks redefine random events that way
      // Really, it'd be nice if the /dev/input/events could spit out useful text, and just use scripts
      // to respond without a daemon per se; for that matter, pnd-ls and pnd-map pnd-dotdesktopemitter
      // should just exist as scripts rather than daemons, but whose counting?

      // figure out which keycode we're talking about
      generic_event_t *p = generics;
      while ( p -> code != -1 ) {
	if ( strcasecmp ( p -> name, k ) == 0 ) {
	  break;
	}
	p++;
      }

      if ( p -> code != -1 ) {
	g_evmap [ g_evmap_max ].key_p = 0;    // its an event, not a key
	g_evmap [ g_evmap_max ].reqs = p;     // note the keycode
	g_evmap [ g_evmap_max ].script = n;   // note the script to activate in response
	pnd_log ( pndn_rem, "Registered generic event %s [%d] to script %s\n", p -> name, p -> code, (char*) n );
	g_evmap_max++;
      } else {
	pnd_log ( pndn_warning, "WARNING! Generic event '%s' is not handled by pndevmapperd yet! Skipping.", k );
      }

    } else if ( strncmp ( k, "pndevmapperd.", 7 ) == 0 ) {
      // not consumed here, skip silently

    } else if ( strncmp ( k, "battery.", 8 ) == 0 ) {
      // not consumed here, skip silently

    } else if ( strncmp ( k, "battery_charge.", 15 ) == 0 ) {
      // not consumed here, skip silently

    } else {
      // uhhh
      pnd_log ( pndn_warning, "Unknown config key '%s'; skipping.\n", k );
    }

    n = pnd_box_get_next ( n );
  } // while

  if ( pnd_conf_get_as_int ( evmaph, "pndevmapperd.loglevel" ) != PND_CONF_BADNUM ) {
    pnd_log_set_filter ( pnd_conf_get_as_int ( evmaph, "pndevmapperd.loglevel" ) );
    pnd_log ( pndn_rem, "config file causes loglevel to change to %u", pnd_log_get_filter() );
  }

  if ( pnd_conf_get_as_int ( evmaph, "pndevmapperd.minimum_separation" ) != PND_CONF_BADNUM ) {
    g_minimum_separation = pnd_conf_get_as_int ( evmaph, "pndevmapperd.minimum_separation" );
    pnd_log ( pndn_rem, "config file causes minimum_separation to change to %u", g_minimum_separation );
  }

  // battery conf
  if ( pnd_conf_get_as_int ( evmaph, "battery.threshold" ) != PND_CONF_BADNUM ) {
    b_threshold = pnd_conf_get_as_int ( evmaph, "battery.threshold" );
    pnd_log ( pndn_rem, "Battery threshold set to %u", b_threshold );
  }
  if ( pnd_conf_get_as_int ( evmaph, "battery.check_interval" ) != PND_CONF_BADNUM ) {
    b_frequency = pnd_conf_get_as_int ( evmaph, "battery.check_interval" );
    pnd_log ( pndn_rem, "Battery check interval set to %u", b_frequency );
  }
  if ( pnd_conf_get_as_int ( evmaph, "battery.blink_interval" ) != PND_CONF_BADNUM ) {
    b_blinkfreq = pnd_conf_get_as_int ( evmaph, "battery.blink_interval" );
    pnd_log ( pndn_rem, "Battery blink interval set to %u", b_blinkfreq );
  }
  if ( pnd_conf_get_as_int ( evmaph, "battery.blink_duration" ) != PND_CONF_BADNUM ) {
    b_blinkdur = pnd_conf_get_as_int ( evmaph, "battery.blink_duration" );
    pnd_log ( pndn_rem, "Battery blink duration set to %u", b_blinkdur );
  }
  b_active = 0;
  if ( pnd_conf_get_as_int ( evmaph, "battery.shutdown_threshold" ) != PND_CONF_BADNUM ) {
    b_shutdown = pnd_conf_get_as_int ( evmaph, "battery.shutdown_threshold" );
    pnd_log ( pndn_rem, "Battery shutdown threshold set to %u", b_shutdown );
  }
  if ( pnd_conf_get_as_int ( evmaph, "battery.shutdown_delay" ) != PND_CONF_BADNUM ) {
    b_shutdelay = pnd_conf_get_as_int ( evmaph, "battery.shutdown_delay" );
    pnd_log ( pndn_rem, "Battery shutdown delay set to %u", b_shutdelay );
  }
  if ( pnd_conf_get_as_char ( evmaph, "battery.shutdown_script" ) != NULL ) {
    b_shutdown_script = strdup ( pnd_conf_get_as_char ( evmaph, "battery.shutdown_script" ) );
    pnd_log ( pndn_rem, "Battery shutdown script set to %s", b_shutdown_script );
  }
  if ( pnd_conf_get_as_int ( evmaph, "battery_charge.enable" ) != PND_CONF_BADNUM ) {
    bc_enable = pnd_conf_get_as_int ( evmaph, "battery_charge.enable" );
    pnd_log ( pndn_rem, "Battery charge enable set to %u", bc_enable );
  }
  if ( pnd_conf_get_as_int ( evmaph, "battery_charge.stop_capacity" ) != PND_CONF_BADNUM ) {
    bc_stopcap = pnd_conf_get_as_int ( evmaph, "battery_charge.stop_capacity" );
    pnd_log ( pndn_rem, "Battery charge stop capacity set to %u", bc_stopcap );
  }
  if ( pnd_conf_get_as_int ( evmaph, "battery_charge.stop_current" ) != PND_CONF_BADNUM ) {
    bc_stopcur = pnd_conf_get_as_int ( evmaph, "battery_charge.stop_current" );
    pnd_log ( pndn_rem, "Battery charge stop current set to %u", bc_stopcur );
  }
  if ( pnd_conf_get_as_int ( evmaph, "battery_charge.start_capacity" ) != PND_CONF_BADNUM ) {
    bc_startcap = pnd_conf_get_as_int ( evmaph, "battery_charge.start_capacity" );
    pnd_log ( pndn_rem, "Battery charge start capacity set to %u", bc_startcap );
  }
  if ( pnd_conf_get_as_char ( evmaph, "battery_charge.device" ) != NULL ) {
    bc_charge_device = strdup ( pnd_conf_get_as_char ( evmaph, "battery_charge.device" ) );
    pnd_log ( pndn_rem, "Battery charge device set to %s", bc_charge_device );
  }

  /* do we have anything to do?
   */
  if ( ! g_evmap_max ) {
    // uuuh, nothing to do?
    pnd_log ( pndn_warning, "WARNING! No events configured to watch, so just spinning wheels...\n" );
    exit ( -1 );
  } // spin

  /* set up sigchld -- don't want zombies all over; well, we do, but not process zombies
   */
  sigset_t ss;
  sigemptyset ( &ss );

  struct sigaction siggy;
  siggy.sa_handler = sigchld_handler;
  siggy.sa_mask = ss; /* implicitly blocks the origin signal */
  siggy.sa_flags = SA_RESTART; /* don't need anything */
  sigaction ( SIGCHLD, &siggy, NULL );

  /* set up the battery level warning timers
   */
  siggy.sa_handler = sigalrm_handler;
  siggy.sa_mask = ss; /* implicitly blocks the origin signal */
  siggy.sa_flags = SA_RESTART; /* don't need anything */
  sigaction ( SIGALRM, &siggy, NULL );

  if ( set_next_alarm ( b_frequency, 0 ) ) { // check every 'frequency' seconds
    pnd_log ( pndn_rem, "Checking for low battery every %u seconds\n", b_frequency );
  } else {
    pnd_log ( pndn_error, "ERROR: Couldn't set up timer for every %u seconds\n", b_frequency );
  }

  /* actually try to do something useful
   */

  // stolen in part from notaz :)

  // try to locate the appropriate devices
  int id;
  int fds [ 8 ] = { -1, -1, -1, -1, -1, -1, -1, -1 }; // 0 = keypad, 1 = gpio keys
  int imaxfd = 0;

  for ( id = 0; ; id++ ) {
    char fname[64];
    char name[256] = { 0, };
    int fd;

    snprintf ( fname, sizeof ( fname ), "/dev/input/event%i", id );
    fd = open ( fname, O_RDONLY );

    if ( fd == -1 ) {
      break;
    }

    if ( ioctl (fd, EVIOCGNAME(sizeof(name)), name ) < 0 ) {
      name [ 0 ] = '\0';
    }

    pnd_log ( pndn_rem, "%s maps to %s\n", fname, name );

    if ( strcmp ( name, PND_EVDEV_KEYPAD/*"omap_twl4030keypad"*/ ) == 0 ) {
      fds [ 0 ] = fd;
    } else if ( strcmp ( name, "gpio-keys" ) == 0) {
      fds [ 1 ] = fd;
    } else if ( strcmp ( name, "AT Translated Set 2 keyboard" ) == 0) { // for vmware, my dev environment
      fds [ 0 ] = fd;
    } else if ( strcmp ( name, PND_EVDEV_POWER/*"triton2-pwrbutton"*/ ) == 0) {
      fds [ 2 ] = fd;
    } else if ( strcmp ( name, PND_EVDEV_TS/*"ADS784x Touchscreen"*/ ) == 0) {
      fds [ 3 ] = fd;
    } else if ( strcmp ( name, PND_EVDEV_NUB1/*"vsense66"*/ ) == 0) {
      fds [ 4 ] = fd;
    } else if ( strcmp ( name, PND_EVDEV_NUB1/*"vsense67"*/ ) == 0) {
      fds [ 5 ] = fd;
    } else {
      pnd_log ( pndn_rem, "Ignoring unknown device '%s'\n", name );
      //fds [ 6 ] = fd;
      close ( fd );
      fd = -1;
      continue;
    }

    if (imaxfd < fd) imaxfd = fd;

  } // for

  if ( fds [ 0 ] == -1 ) {
    pnd_log ( pndn_warning, "WARNING! Couldn't find keypad device\n" );
  }

  if ( fds [ 1 ] == -1 ) {
    pnd_log ( pndn_warning, "WARNING! couldn't find button device\n" );
  }

  if ( fds [ 0 ] == -1 && fds [ 1 ] == -1 ) {
    pnd_log ( pndn_error, "ERROR! Couldn't find either device!\n" );
    //exit ( -2 );
  }

  /* loop forever, watching for events
   */

  while ( 1 ) {
    struct input_event ev[64];

    unsigned int max_fd = 3; /* imaxfd */
    int fd = -1, rd, ret;
    fd_set fdset;

    // set up fd list
    FD_ZERO ( &fdset );

    imaxfd = 0;
    for (i = 0; i < max_fd /*imaxfd*/; i++) {
      if ( fds [ i ] != -1 ) {
	FD_SET( fds [ i ], &fdset );

	if ( fds [ i ] > imaxfd ) {
	  imaxfd = fds [ i ];
	}

      }
    }

    // figure out if we can block forever, or not
    unsigned char do_block = 1;
    struct timeval tv;
    tv.tv_usec = 0;
    tv.tv_sec = 1;

    for ( i = i; i < g_evmap_max; i++ ) {
      if ( g_evmap [ i ].keydown_time && g_evmap [ i ].maxhold ) {
	do_block = 0;
	break;
      }
    }

    // wait for fd's or timeout
    ret = select ( imaxfd + 1, &fdset, NULL, NULL, do_block ? NULL /* no timeout */ : &tv );

    if ( ret == -1 ) {
      pnd_log ( pndn_error, "ERROR! select(2) failed with: %s\n", strerror ( errno ) );
      continue; // retry!

    } else if ( ret == 0 ) { // select returned with timeout (no fd)

      // timeout occurred; should only happen when 1 or more keys are being held down and
      // they're "maxhold" keys, so we have to see if their timer has passed
      unsigned int now = time ( NULL );

      for ( i = i; i < g_evmap_max; i++ ) {

	if ( g_evmap [ i ].keydown_time &&
	     g_evmap [ i ].maxhold &&
	     now - g_evmap [ i ].keydown_time >= g_evmap [ i ].maxhold )
	{
	  keycode_t *k = (keycode_t*) g_evmap [ i ].reqs;
	  dispatch_key ( k -> keycode, 0 /* key up */ );
	}

      } // for

    } else { // an fd was fiddled with

      for ( i = 0; i < max_fd; i++ ) {
	if ( fds [ i ] != -1 && FD_ISSET ( fds [ i ], &fdset ) ) {
	  fd = fds [ i ];
	} // fd is set?
      } // for

      /* buttons or keypad */
      rd = read ( fd, ev, sizeof(struct input_event) * 64 );
      if ( rd < (int) sizeof(struct input_event) ) {
	pnd_log ( pndn_error, "ERROR! read(2) input_event failed with: %s\n", strerror ( errno ) );
	break;
      }

      for (i = 0; i < rd / sizeof(struct input_event); i++ ) {

	if ( ev[i].type == EV_SYN ) {
	  continue;
	} else if ( ev[i].type == EV_KEY ) {

	  // do we even know about this key at all?
	  keycode_t *p = keycodes;
	  while ( p -> keycode != -1 ) {
	    if ( p -> keycode == ev [ i ].code ) {
	      break;
	    }
	    p++;
	  }

	  // if we do, hand it off to dispatcher to look up if we actually do something with it
	  if ( p -> keycode != -1 ) {
	    if ( logall >= 0 ) {
	      pnd_log ( pndn_debug, "Key Event: key %s [%d] value %d\n", p -> keyname, p -> keycode, ev [ i ].value );
	    }
	    dispatch_key ( p -> keycode, ev [ i ].value );
	  } else {
	    if ( logall >= 0 ) {
	      pnd_log ( pndn_warning, "Unknown Key Event: keycode %d value %d\n",  ev [ i ].code, ev [ i ].value );
	    }
	  }

	} else if ( ev[i].type == EV_SW ) {

	  // do we even know about this event at all?
	  generic_event_t *p = generics;
	  while ( p -> code != -1 ) {
	    if ( p -> code == ev [ i ].code ) {
	      break;
	    }
	    p++;
	  }

	  // if we do, hand it off to dispatcher to look up if we actually do something with it
	  if ( p -> code != -1 ) {
	    if ( logall >= 0 ) {
	      pnd_log ( pndn_debug, "Generic Event: event %s [%d] value %d\n", p -> name, p -> code, ev [ i ].value );
	    }
	    dispatch_event ( p -> code, ev [ i ].value );
	  } else {
	    if ( logall >= 0 ) {
	      pnd_log ( pndn_warning, "Unknown Generic Event: code %d value %d\n",  ev [ i ].code, ev [ i ].value );
	    }
	  }

	} else {
	  pnd_log ( pndn_debug, "DEBUG: Unexpected event type %i received\n", ev[i].type );
	  continue;
	} // type?

      } // for

    } // an fd was touched

  } // while

  for (i = 0; i < 2; i++) {
    if ( i != 2 && fds [ i ] != -1 ) {
      close (fds [ i ] );
    }
  }

  return ( 0 );
} // main