Esempio n. 1
0
void sigchld_handler ( int n ) {

  pnd_log ( pndn_rem, "---[ SIGCHLD received ]---\n" );

  int status;
  wait ( &status );

  pnd_log ( pndn_rem, "     SIGCHLD done ]---\n" );

  return;
}
Esempio n. 2
0
void setup_notifications ( void ) {
  searchpath = notifypath;

  // if this is first time through, we can just set it up; for subsequent times
  // through, we need to close existing fd and re-open it, since we're too lame
  // to store the list of watches and 'rm' them
#if 1
  if ( nh ) {
    pnd_notify_shutdown ( nh );
    nh = 0;
  }
#endif

  // set up a new set of notifies
  if ( ! nh ) {
    nh = pnd_notify_init();
  }

  if ( ! nh ) {
    pnd_log ( pndn_rem, "INOTIFY failed to init.\n" );
    exit ( -1 );
  }

#if 0
  pnd_log ( pndn_rem, "INOTIFY is up.\n" );
#endif

  SEARCHPATH_PRE
  {

    pnd_log ( pndn_rem, "Watching path '%s' and its descendents.\n", buffer );
    pnd_notify_watch_path ( nh, buffer, PND_NOTIFY_RECURSE );

    //pnd_notify_watch_path ( nh, buffer, 0 /* no recurse */ );

  }
  SEARCHPATH_POST

#if 0
  sleep ( 1 ); // wait for events to trigger?

  // clear out any notifies we just created
  while ( pnd_notify_rediscover_p ( nh ) ) {
    usleep ( 100 ); // spin
  } // while
#endif

  return;
}
Esempio n. 3
0
void dispatch_event ( int code, int val ) {
  unsigned int i;

  // LID val decodes as:
  // 1 - closing
  // 0 - opening

  pnd_log ( pndn_rem, "Dispatching Event..\n" );

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

    if ( ( g_evmap [ i ].key_p == 0 ) &&
	 ( ((generic_event_t*) (g_evmap [ i ].reqs)) -> code == code ) &&
	 ( g_evmap [ i ].script ) )
    {

      // just hand the code to the script (ie: 0 or 1 to script)
      if ( time ( NULL ) - g_evmap [ i ].last_trigger_time >= g_minimum_separation ) {
	int x;
	char value [ 100 ];

	sprintf ( value, "%d", val );

	g_evmap [ i ].last_trigger_time = time ( NULL );

	pnd_log ( pndn_rem, "Will attempt to invoke: %s %s\n", g_evmap [ i ].script, value );

	if ( ( x = fork() ) < 0 ) {
	  pnd_log ( pndn_error, "ERROR: Couldn't fork()\n" );
	  exit ( -3 );
	}

	if ( x == 0 ) {
	  execl ( g_evmap [ i ].script, g_evmap [ i ].script, value, (char*)NULL );
	  pnd_log ( pndn_error, "ERROR: Couldn't exec(%s)\n", g_evmap [ i ].script );
	  exit ( -4 );
	}

      } else {
	pnd_log ( pndn_rem, "Skipping invokation.. falls within minimum_separation threshold\n" );
      }

      return;
    } // found matching event for keycode

  } // while

  return;
}
Esempio n. 4
0
void sighup_handler ( int n ) {

  pnd_log ( pndn_rem, "---[ SIGHUP received ]---\n" );

  // reparse config files
  consume_configuration();

  // re set up the notifier watches
  setup_notifications();

  return;
}
Esempio n. 5
0
void sigint_handler ( int n ) {

  pnd_log ( pndn_rem, "---[ SIGINT received ]---\n" );

  if ( dbh ) {
    pnd_dbusnotify_shutdown ( dbh );
  }

  if ( nh ) {
    pnd_notify_shutdown ( nh );
  }

  return;
}
Esempio n. 6
0
unsigned char cache_preview ( pnd_disco_t *app, unsigned int maxwidth, unsigned int maxheight ) {
  SDL_Surface *s;
  mm_cache_t *c;

  // does this sucker even have a preview?
  if ( ! app -> preview_pic1 ) {
    return ( 1 ); // nothing here, so thats fine
  }

  // check if already cached
  if ( ( c = cache_query_preview ( app -> unique_id ) ) ) {
    return ( 1 ); // already got it
  }

  // not cached, load it up
  //

  // show hourglass
  ui_show_hourglass ( 1 /* updaterect*/ );

  // see if we can mount the pnd/dir
  // does preview file exist?
  //   if so, load it up, size it, cache it
  //   if not, warning and bail
  // unmount it

  // can we mount? or can we find it in preview cache? or an override?
  char fullpath [ PATH_MAX ] = "";
  char filepath [ PATH_MAX ] = "";

  // first, check for preview override
  {
    char ovrfile [ PATH_MAX ];
    char *fooby;
    sprintf ( ovrfile, "%s/%s", app -> object_path, app -> object_filename );
    fooby = strcasestr ( ovrfile, PND_PACKAGE_FILEEXT );
    if ( fooby ) {
      sprintf ( fooby, "_pvw#%u.png", app -> subapp_number );
      struct stat statbuf;
      if ( stat ( ovrfile, &statbuf ) == 0 ) {
	strncpy ( filepath, ovrfile, PATH_MAX );
      } // stat
    } // ovr?
  }

  // if not yet found, try to find in cache
  if ( filepath [ 0 ] == '\0' && g_pvwcache ) {
    static char *cache_findpath = NULL;
    if ( ! cache_findpath ) {
      cache_findpath = pnd_conf_get_as_char ( g_conf, "previewpic.cache_findpath" );
    }
    char buffer [ FILENAME_MAX ];
    sprintf ( buffer, "%s.png", app -> unique_id );
    char *f = pnd_locate_filename ( cache_findpath, buffer );
    if ( f ) {
      strncpy ( filepath, f, PATH_MAX );
    }
  }

  // unique-id to use for the cache mount
  char *uid = app -> unique_id;

  if ( app -> appdata_dirname ) {
    uid = app -> appdata_dirname;
  }

  // if we don't have a file path sorted out yet, means we need to mount and figure it
  if ( ! filepath [ 0 ] ) {
    sprintf ( fullpath, "%s/%s", app -> object_path, app -> object_filename );

    if ( ! pnd_pnd_mount ( pnd_run_script, fullpath, uid ) ) {
      pnd_log ( pndn_debug, "Couldn't mount '%s' for preview\n", fullpath );
      return ( 0 ); // couldn't mount?!
    }

    sprintf ( filepath, "%s/%s/%s", PND_MOUNT_PATH, uid, app -> preview_pic1 );
  }

  // load whatever path we've got
  s = IMG_Load ( filepath );

  if ( ! s ) {
    // unmount it, if mounted
    if ( fullpath [ 0 ] ) {
      pnd_pnd_unmount ( pnd_run_script, fullpath, uid );
    }
    pnd_log ( pndn_debug, "Couldn't open image '%s' for preview\n", filepath );
    return ( 0 );
  }

  // try to copy file to the cache, if we're doing that, and if mounted
  if ( g_pvwcache && fullpath [ 0 ] ) {
    char cacheoutpath [ PATH_MAX ] = "";

    // figure out where we want to write the file to
    if ( cache_find_writable ( app -> object_path, cacheoutpath, PATH_MAX ) ) {
      static char *cache_path = NULL;
      char buffer [ PATH_MAX ];
      if ( ! cache_path ) {
	cache_path = pnd_conf_get_as_char ( g_conf, "previewpic.cache_path" );
      }
      // make the dir
      snprintf ( buffer, PATH_MAX, "%s/%s", cacheoutpath, cache_path );
      struct stat statbuf;
      if ( stat ( buffer, &statbuf ) != 0 ) {
	snprintf ( buffer, PATH_MAX, "/bin/mkdir -p %s/%s", cacheoutpath, cache_path );
	system ( buffer );
      }
      // set up target filename to copy
      snprintf ( buffer, PATH_MAX, "%s/%s/%s.png", cacheoutpath, cache_path, app -> unique_id );
      pnd_log ( pndn_debug, "Found free space to cache preview to here: %s", buffer );
      if ( ! pnd_filecopy ( filepath, buffer ) ) {
	pnd_log ( pndn_error, "ERROR: Copying preview from %s to %s", filepath, buffer );
      }
    } else {
      pnd_log ( pndn_warning, "WARN: Couldn't find a device to cache preview to.\n" );
    }

  } // preview file cache

  // unmount it, if mounted
  if ( fullpath [ 0 ] ) {
    pnd_pnd_unmount ( pnd_run_script, fullpath, app -> unique_id );
  }

  //pnd_log ( pndn_debug, "Image size is %u x %u (max %u x %u)\n", s -> w, s -> h, maxwidth, maxheight );

  // scale
  if ( s -> w < maxwidth ) {
    // scale up?
    if ( pnd_conf_get_as_int_d ( g_conf, "previewpic.scale_up_bool", 1 ) ) {
      SDL_Surface *scaled;
      double scale = (double)maxwidth / (double)s -> w;
      //pnd_log ( pndn_debug, "  Upscaling; scale factor %f\n", scale );
      scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
      SDL_FreeSurface ( s );
      s = scaled;
    }
  } else if ( s -> w > maxwidth ) {
    SDL_Surface *scaled;
    double scale = (double)maxwidth / (double)s -> w;
    //pnd_log ( pndn_debug, "  Downscaling; scale factor %f\n", scale );
    scaled = rotozoomSurface ( s, 0 /* angle*/, scale /* scale */, 1 /* smooth==1*/ );
    SDL_FreeSurface ( s );
    s = scaled;
  }

  // add to cache
  c = (mm_cache_t*) malloc ( sizeof(mm_cache_t) );
  bzero ( c, sizeof(mm_cache_t) );

  if ( ! g_preview_cache ) {
    g_preview_cache = c;
  } else {
    c -> next = g_preview_cache;
    g_preview_cache = c;
  }

  strncpy ( c -> uniqueid, app -> unique_id, 1000 );
  c -> i = s;

  return ( 1 );
}
Esempio n. 7
0
int main ( int argc, char *argv[] ) {
  // behaviour
  unsigned char scanonlaunch = 1;
  unsigned int interval_secs = 5;
  int logall = -1; // -1 means normal logging rules; >=0 means log all!
  // misc
  int i;

  /* iterate across args
   */
  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 ( isdigit ( argv [ i ][ 0 ] ) ) {
      interval_secs = atoi ( argv [ i ] );
    } else if ( argv [ i ][ 0 ] == '-' && argv [ i ][ 1 ] == 'n' ) {
      scanonlaunch = 0;
    } 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 {
      printf ( "%s [-d] [-l] [##]\n", argv [ 0 ] );
      printf ( "-d\tDaemon mode; detach from terminal, chdir to /tmp, suppress output. Optional.\n" );
      printf ( "-n\tDo not scan on launch; default is to run a scan for apps when %s is invoked.\n", argv [ 0 ] );
      printf ( "-l#\tLog-it; -l is 0-and-up (or all), and -l2 means 2-and-up (not all); l[0-3] for now. Log goes to /tmp/pndnotifyd.log\n" );
      printf ( "##\tA numeric value is interpreted as number of seconds between checking for filesystem changes. Default %u.\n",
	       interval_secs );
      printf ( "Signal: HUP the process to force reload of configuration and reset the notifier watch paths\n" );
      exit ( 0 );
    }

  } // for

  /* enable logging?
   */
  pnd_log_set_pretext ( "pndnotifyd" );
  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/pndnotifyd.log", "w" );

    if ( f ) {
      pnd_log_set_filter ( logall );
      pnd_log_to_stream ( f );
      pnd_log ( pndn_rem, "logall mode - logging to /tmp/pndnotifyd.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() );

  pnd_log ( pndn_rem, "Interval between checks is %u seconds\n", interval_secs );

  // 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" );

  // basic daemon set up
  if ( g_daemon_mode ) {

    // set a CWD somewhere else
#if 0
    chdir ( "/tmp" );
#endif

    // 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

  // 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" );
  while ( 1 ) {
    if ( pnd_check_login ( g_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", g_username );

  /* parse configs
   */

  consume_configuration();

  /* startup
   */

  pnd_log ( pndn_rem, "PXML overrides searchpath is '%s'\n", overridespath );
  pnd_log ( pndn_rem, "Notify searchpath is '%s'\n", notifypath );

  pnd_log ( pndn_rem, "Desktop apps ---------------------------------\n" );
  pnd_log ( pndn_rem, "Apps searchpath is '%s'\n", desktop_appspath );
  pnd_log ( pndn_rem, ".desktop files emit to '%s'\n", desktop_dotdesktoppath );
  pnd_log ( pndn_rem, ".desktop icon files emit to '%s'\n", desktop_iconpath );

  pnd_log ( pndn_rem, "Menu apps ---------------------------------\n" );
  pnd_log ( pndn_rem, "Apps searchpath is '%s'\n", menu_appspath );
  pnd_log ( pndn_rem, ".desktop files emit to '%s'\n", menu_dotdesktoppath );
  pnd_log ( pndn_rem, ".desktop icon files emit to '%s'\n", menu_iconpath );
  pnd_log ( pndn_rem, ".desktop info files emit to '%s'\n", info_dotdesktoppath ? info_dotdesktoppath : "n/a" );

  /* set up signal handler
   */
  sigset_t ss;
  sigemptyset ( &ss );

  struct sigaction siggy;
  siggy.sa_handler = sighup_handler;
  siggy.sa_mask = ss; /* implicitly blocks the origin signal */
  siggy.sa_flags = 0; /* don't need anything */

  sigaction ( SIGHUP, &siggy, NULL );

  siggy.sa_handler = sigint_handler;
  sigaction ( SIGINT, &siggy, NULL );

  /* set up notifies
   */

  // if we're gong to scan on launch, it'll set things right up and then set up notifications.
  // if no scan on launch, we need to set the notif's up now.
  //if ( ! scanonlaunch ) {
  setup_notifications();
  //}

  dbh = pnd_dbusnotify_init();

  /* daemon main loop
   */
  unsigned char watch_inotify, watch_dbus;
  while ( 1 ) {

    watch_dbus = 0;
    watch_inotify = 0;

    if ( dbh ) {
      watch_dbus = pnd_dbusnotify_rediscover_p ( dbh );
    }

    if ( ! watch_dbus && nh ) {
      watch_inotify = pnd_notify_rediscover_p ( nh );
    }

    // need to rediscover?
    if ( scanonlaunch || watch_inotify || watch_dbus ) {

      // by this point, the watched directories have notified us that something of relevent
      // has occurred; we should be clever, but we're not, so just re-brute force the
      // discovery and spit out .desktop files..
      pnd_log ( pndn_rem, "------------------------------------------------------\n" );

      pnd_log ( pndn_rem, "System changes detected in dbus or watched paths .. performing re-discover!\n" );

      // if this was a forced scan, lets not do that next iteration
      if ( scanonlaunch ) {
	pnd_log ( pndn_rem, "scan-on-first-launch detected an event\n" );
	scanonlaunch = 0;
      }

      if ( watch_inotify ) {
	pnd_log ( pndn_rem, "inotify detected an event\n" );
      }

      if ( watch_dbus ) {
	pnd_log ( pndn_rem, "dbusnotify detected an event\n" );
	pnd_notify_shutdown ( nh );
	nh = 0;
      }

      if ( time ( NULL ) - createtime <= 2 ) {
	pnd_log ( pndn_rem, "Rediscovery request comes to soon after previous discovery; skipping.\n" );
	sleep ( interval_secs );
	continue;
      }

      pnd_log ( pndn_rem, "------------------------------------------------------\n" );

      createtime = time ( NULL ); // all 'new' .destops are created at or after this time; prev are old.

      /* run the discovery
       */

      // do some 'locking'
      pnd_log ( pndn_rem, "creating lockfile %s", PNDLOCKNAME );
      if ( ! pnd_lock ( PNDLOCKNAME ) ) {
	// problem .. well, too bad, we need to do this .. proceed!
      }

      // scan..
      pnd_log ( pndn_rem, "  Scanning desktop paths----------------------------\n" );
      if ( ! perform_discoveries ( desktop_appspath, overridespath, desktop_dotdesktoppath, desktop_iconpath ) ) {
	pnd_log ( pndn_rem, "    No applications found in desktop search path\n" );
      }

      if ( menu_appspath && menu_dotdesktoppath && menu_iconpath ) {
	pnd_log ( pndn_rem, "  Scanning menu paths----------------------------\n" );
	if ( ! perform_discoveries ( menu_appspath, overridespath, menu_dotdesktoppath, menu_iconpath ) ) {
	  pnd_log ( pndn_rem, "    No applications found in menu search path\n" );
	}
      }

      // unlock
      pnd_log ( pndn_rem, "clearing lockfile %s", PNDLOCKNAME );
      pnd_unlock ( PNDLOCKNAME );

      // if we've got a hup script located, lets invoke it
      if ( pndhup ) {
	pnd_log ( pndn_rem, "Invoking hup script '%s'.\n", pndhup );
	pnd_exec_no_wait_1 ( pndhup, NULL );
      }

      // since its entirely likely new directories have been found (ie: SD with a directory structure was inserted)
      // we should re-apply watches to catch all these new directories; ie: user might use on-device browser to
      // drop in new applications, or use the shell to juggle them around, or any number of activities.
      if ( watch_dbus ) {
	setup_notifications();
      }

    } // need to rediscover?

    // lets not eat up all the CPU
    // should use an alarm or select() or something -- I mean really, why aren't I putting interval_secs into
    // the select() call above in pnd_notify_whatsitcalled()? -- but lets not break this right before release shall we
    // NOTE: Oh right, I remember now -- inotify will spam when a card is inserted, and it will not be instantaneoous..
    // the events will dribble in over a second. So this sleep is a lame trick that generally works. I really should
    // do select(), and then when it returns just spin for a couple seconds slurping up events until no more and a thresh-hold
    // time is hit, but this will do for now. I suck.
    sleep ( interval_secs );

  } // while

  /* shutdown
   */
  pnd_notify_shutdown ( nh );
  pnd_dbusnotify_shutdown ( dbh );

  return ( 0 );
}
Esempio n. 8
0
// This very recently was inline code; just slight refactor to functionize it so that it can be
// reused in a couple of places. Simple code with simple design quickly became too large for
// its simple design; should revisit a lot of these little things..
void process_discoveries ( pnd_box_handle applist, char *emitdesktoppath, char *emiticonpath ) {
  pnd_disco_t *d = pnd_box_get_head ( applist );

  while ( d ) {

    pnd_log ( pndn_rem, "Found app: %s\n", pnd_box_get_key ( d ) );

    // check if icon already exists (from a previous extraction say); if so, we needn't
    // do it again
    char existingpath [ FILENAME_MAX ];
    sprintf ( existingpath, "%s/%s.png", emiticonpath, d -> unique_id /*, d -> subapp_number */ );

    struct stat dirs;
    if ( stat ( existingpath, &dirs ) == 0 ) {
      // icon seems to exist, so just crib the location into the .desktop

      pnd_log ( pndn_rem, "  Found icon already existed, so reusing it! %s\n", existingpath );

      if ( d -> icon ) {
	free ( d -> icon );
      }
      d -> icon = strdup ( existingpath );

    } else {
      // icon seems unreadable or does not exist; lets try to create it..

      pnd_log ( pndn_debug, "  Icon not already present, so trying to write it! %s\n", existingpath );

      // handle same-path icon override for davec :)
      char ovrfile [ PATH_MAX ];
      char *fixpxml;
      sprintf ( ovrfile, "%s/%s", d -> object_path, d -> object_filename );
      fixpxml = strcasestr ( ovrfile, PND_PACKAGE_FILEEXT );
      if ( fixpxml ) {
	strcpy ( fixpxml, ".png" );
	fixpxml = NULL;
	struct stat statbuf;
	if ( stat ( ovrfile, &statbuf ) == 0 ) {
	  d -> icon = strdup ( ovrfile );
	  fixpxml = ovrfile; // !NULL will be the trigger to skip emittinf desktop from .pnd
	} // stat
      } // ovr?

      // attempt to create icon files; if successful, alter the disco struct to contain new
      // path, otherwise leave it alone (since it could be a generic icon reference..)
      if ( fixpxml == NULL ) {
	// don't have an same-path override icon, so go fetch something from pnd file

	if ( pnd_emit_icon ( emiticonpath, d ) ) {
	  // success; fix up icon path to new one..
	  if ( d -> icon ) {
	    free ( d -> icon );
	  }
	  d -> icon = strdup ( existingpath );
	} else {
	  pnd_log ( pndn_debug, "  WARN: Couldn't write out icon %s\n", existingpath );
	}

      } // got ovr icon already?

    } // icon already exists?

    // create the .desktop file
    if ( pnd_emit_dotdesktop ( emitdesktoppath, pndrun, d ) ) {
      // add a watch onto the newly created .desktop?
#if 0
      char buffer [ FILENAME_MAX ];
      sprintf ( buffer, "%s/%s", emitdesktoppath, d -> unique_id );
      pnd_notify_watch_path ( nh, buffer, PND_NOTIFY_RECURSE );
#endif
    } else {
      pnd_log ( pndn_rem, "ERROR: Error creating .desktop file for app: %s\n", pnd_box_get_key ( d ) );
    }

    // info .desktop
    if ( g_info_p && info_dotdesktoppath ) {
      if ( ! pnd_emit_dotinfo ( info_dotdesktoppath, pndrun, d ) ) {
	pnd_log ( pndn_rem, "ERROR: Error creating info .desktop file for app: %s\n", pnd_box_get_key ( d ) );
      }
    }

    // does this object request any mkdir's?
    if ( d -> mkdir_sp ) {

      // it would appear it does! but we have to carefully validate these suckers
      pnd_log ( pndn_rem, "  App %s requests mkdir: %s\n", d -> object_path, d -> mkdir_sp );

      // for each mkdir requested path, do it...
      char *searchpath = d -> mkdir_sp;

      SEARCHCHUNK_PRE
      {
	/* "buffer" now holds each chunk of the searchpath, expanded */

	// WARN: This whole concept could be flawed; what if they represent '..' in some other obscure way (unicode?)
	// and we end up allowing mkdir's all over the place? The risk really is limited -- once the pnd is here,
	// if the user _runs it_, it can go nuts, so creating a few dirs isn't all that dangerous...
	//   HMRF :/
	// Perhaps I should have a config setting for pndnotifyd to suppress this whole mkdir behaviour?

	// if not containing ".." we allow it
	if ( strstr ( buffer, ".." )  == NULL ) {

	  // determine mountpoint for the file
	  // - we could deduce this from the path (somewhat risky if we assume leading /media/mmcblk1p1 type notation .. could
	  //   be other distributions entirely
	  // - better to scan through mount-list and figure it out.. *sucks*
	  char mountpoint [ PATH_MAX ];
	  if ( pnd_determine_mountpoint ( d -> object_path, mountpoint, PATH_MAX - strlen ( buffer ) - 1 ) == 1 ) {

	    strcat ( mountpoint, "/" );
	    strcat ( mountpoint, buffer );

	    struct stat t;
	    if ( stat ( mountpoint, &t ) == 0 ) {
	      pnd_log ( pndn_rem, "    Skipping existing mkdir: %s\n", mountpoint );
	    } else {
	      pnd_log ( pndn_rem, "    Attempting create of non-existant path: %s\n", mountpoint );
	      mkdir ( mountpoint, 0777 );
	    }

	  } // if figured out the mountpoint

	} // if valid path

      }
      SEARCHCHUNK_POST

    } // mkdir request

    // next!
    d = pnd_box_get_next ( d );

  } // while applist
Esempio n. 9
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;
}
Esempio n. 10
0
void sigalrm_handler ( int n ) {

  pnd_log ( pndn_debug, "---[ SIGALRM ]---\n" );

  static time_t last_charge_check, last_charge_worka;
  int batlevel = pnd_device_get_battery_gauge_perc();
  int uamps = 0;
  time_t now;

  pnd_device_get_charge_current ( &uamps );

  if ( batlevel < 0 ) {
#if 0
    // couldn't read the battery level, so just assume low and make blinks?
    batlevel = 4; // low, but not cause a shutdown
#else
    // couldn't read the battery level, so just assume ok!
    batlevel = 50;
#endif
  }

  // first -- are we critical yet? if so, shut down!
  if ( batlevel <= b_shutdown && b_shutdown_script) {

    if ( uamps > 100 ) {
        // critical battery, but charging, so relax.
        b_warned = 0;
    } else {
      if (b_warned == 0) {
          // Avoid warning again till re-powered
          b_warned = 1;
          int x;
          pnd_log ( pndn_error, "Battery Current: %d\n", uamps );
          pnd_log ( pndn_error, "CRITICAL BATTERY LEVEL -- shutdown the system down! Invoke: %s\n",
	      	b_shutdown_script );

          if ( ( x = fork() ) < 0 ) {
	        pnd_log ( pndn_error, "ERROR: Couldn't fork()\n" );
    	    exit ( -3 );
          }

         if ( x == 0 ) {
           char value [ 100 ];
           sprintf ( value, "%d", b_shutdelay );
	   execl ( b_shutdown_script, b_shutdown_script, value, (char*)NULL );
	   pnd_log ( pndn_error, "ERROR: Couldn't exec(%s)\n", b_shutdown_script );
	   exit ( -4 );
         }
      }
    } // charging

  }

  // charge monitoring
  now = time(NULL);
  if ( bc_enable && bc_charge_device != NULL && (unsigned int)(now - last_charge_check) > 60 ) {

    int charge_enabled = pnd_device_get_charger_enable ( bc_charge_device );
    if ( charge_enabled < 0 )
      pnd_log ( pndn_error, "ERROR: Couldn't read charger enable control\n" );
    else {

      if ( charge_enabled && batlevel >= bc_stopcap && 0 < uamps && uamps < bc_stopcur ) {
        pnd_log ( pndn_debug, "Charge stop conditions reached, disabling charging\n" );
        pnd_device_set_charger_enable ( bc_charge_device, 0 );
      }
      else if ( !charge_enabled && batlevel <= bc_startcap ) {
        pnd_log ( pndn_debug, "Charge start conditions reached, enabling charging\n" );
        pnd_device_set_charger_enable ( bc_charge_device, 1 );
      }

      // for some unknown reason it just stops charging randomly (happens once per week or so),
      // and does not restart, resulting in a flat battery if machine is unattended.
      // What seems to help here is writing to chip registers, we can do it here indirectly
      // by writing to enable. Doing it occasionally should do no harm even with missing charger.
      if ( batlevel <= bc_startcap && (unsigned int)(now - last_charge_worka) > 20*60 ) {
        pnd_log ( pndn_debug, "Charge workaround trigger\n" );
        pnd_device_set_charger_enable ( bc_charge_device, 1 );
        last_charge_worka = now;
      }
    }
    last_charge_check = now;
  }

  // is battery warning already active?
  if ( b_active ) {
    // warning is on!

    // is user charging up? if so, stop blinking.
    // perhaps we shoudl check if charger is connected, and not blink at all in that case..
    if ( uamps > 0 ) {
      //Re-arm warning
      b_warned = 0;
      pnd_log ( pndn_debug, "Battery is high again, flipping to non-blinker mode\n" );
      b_active = 0;
      set_next_alarm ( b_frequency, 0 );
      pnd_device_set_led_charger_brightness ( 250 );
      return;
    }

    if ( b_active == 1 ) {
      // turn LED on
      pnd_log ( pndn_debug, "Blink on\n" );
      pnd_device_set_led_charger_brightness ( 200 );
      // set timer to short duration
      b_active = 2;
      set_next_alarm ( 0, b_blinkdur );
    } else if ( b_active == 2 ) {
      // turn LED off
      pnd_log ( pndn_debug, "Blink off\n" );
      pnd_device_set_led_charger_brightness ( 10 );
      // back to longer duration
      b_active = 1;
      set_next_alarm ( b_blinkfreq, 0 );
    }

    return;
  }

  // warning is off..
  if ( batlevel <= b_threshold && uamps < 0 ) {
    // battery seems low, go to active mode
    pnd_log ( pndn_debug, "Battery is low, flipping to blinker mode\n" );
    b_active = 1;
    set_next_alarm ( b_blinkfreq, 0 );
  } // battery level

  return;
}
Esempio n. 11
0
// this should really register the keystate and time, and then another func to monitor
// time-passage and check the registered list and act on the event..
void dispatch_key ( int keycode, int val ) {
  unsigned int i;

  // val decodes as:
  // 1 - down (pressed)
  // 2 - down again (hold)
  // 0 - up (released)

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

    if ( ( g_evmap [ i ].key_p ) &&
	 ( ((keycode_t*) (g_evmap [ i ].reqs)) -> keycode == keycode ) &&
	 ( g_evmap [ i ].script ) )
    {
      unsigned char invoke_it = 0;

      // is this a keydown or a keyup?
      if ( val == 1 ) {
	// keydown
	g_evmap [ i ].keydown_time = time ( NULL );

      } else if ( val == 2 && g_evmap [ i ].keydown_time ) {
	// key is being held; we should check if max-hold is set

	if ( g_evmap [ i ].maxhold &&
	     time ( NULL ) - g_evmap [ i ].keydown_time >= g_evmap [ i ].maxhold )
	{
	  invoke_it = 1;
	}

      } else if ( val == 0 && g_evmap [ i ].keydown_time ) {
	// keyup (while key is down)

	if ( time ( NULL ) - g_evmap [ i ].last_trigger_time >= g_minimum_separation ) {
	  invoke_it = 1;
	} else {
	  pnd_log ( pndn_rem, "Skipping invokation.. falls within minimum_separation threshold\n" );
	}

      } // key up or down?

      if ( invoke_it ) {

	char holdtime [ 128 ];
	sprintf ( holdtime, "%d", (int)( time(NULL) - g_evmap [ i ].keydown_time ) );
	pnd_log ( pndn_rem, "Will attempt to invoke: %s %s\n", g_evmap [ i ].script, holdtime );

	// state
	g_evmap [ i ].keydown_time = 0; // clear the keydown-ness
	g_evmap [ i ].last_trigger_time = time ( NULL );

	// invocation
	int x;

	if ( ( x = fork() ) < 0 ) {
	  pnd_log ( pndn_error, "ERROR: Couldn't fork()\n" );
	  exit ( -3 );
	}

	if ( x == 0 ) {
	  execl ( g_evmap [ i ].script, g_evmap [ i ].script, holdtime, (char*)NULL );
	  pnd_log ( pndn_error, "ERROR: Couldn't exec(%s)\n", g_evmap [ i ].script );
	  exit ( -4 );
	}

      } // invoke the script!

      return;
    } // found matching event for keycode

  } // while

  return;
}
Esempio n. 12
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
Esempio n. 13
0
unsigned char pnd_emit_icon ( char *targetpath, pnd_disco_t *p ) {
  //#define BITLEN (8*1024)
#define BITLEN (64*1024)
  char buffer [ FILENAME_MAX ]; // target filename
  char from [ FILENAME_MAX ];   // source filename
  unsigned char bits [ BITLEN ];
  unsigned int bitlen;
  FILE *pnd, *target;

  // prelim .. if a pnd file, and no offset found, discovery code didn't locate icon.. so bail.
  if ( ( p -> object_type == pnd_object_type_pnd ) &&
       ( ! p -> pnd_icon_pos ) )
  {
    return ( 0 ); // discover code didn't find it, so FAIL
  }

  // determine filename for target
  sprintf ( buffer, "%s/%s.png", targetpath, p -> unique_id /*, p -> subapp_number*/ ); // target

  /* first.. open the source file, by type of application:
   * are we looking through a pnd file or a dir?
   */
  if ( p -> object_type == pnd_object_type_directory ) {
    sprintf ( from, "%s/%s", p -> object_path, p -> icon );
  } else if ( p -> object_type == pnd_object_type_pnd ) {
    sprintf ( from, "%s/%s", p -> object_path, p -> object_filename );
  }

  pnd = fopen ( from, "rb" );

  if ( ! pnd ) {
    pnd_log ( PND_LOG_DEFAULT, "    Emit icon, couldn't open source\n" );
    return ( 0 );
  }

  unsigned int len;

  target = fopen ( buffer, "wb" );

  if ( ! target ) {
    fclose ( pnd );
    pnd_log ( PND_LOG_DEFAULT, "    Emit icon, couldn't open target\n" );
    return ( 0 );
  }

  fseek ( pnd, 0, SEEK_END );
  len = ftell ( pnd );
  //fseek ( pnd, 0, SEEK_SET );

  fseek ( pnd, p -> pnd_icon_pos, SEEK_SET );

  len -= p -> pnd_icon_pos;

  pnd_log ( PND_LOG_DEFAULT, "    Emit icon, length: %u\n", len );

  while ( len ) {

    if ( len > (BITLEN) ) {
      bitlen = (BITLEN);
    } else {
      bitlen = len;
    }

    if ( fread ( bits, bitlen, 1, pnd ) != 1 ) {
      fclose ( pnd );
      fclose ( target );
      unlink ( buffer );
      pnd_log ( PND_LOG_DEFAULT, "    Emit icon, bad read\n" );
      return ( 0 );
    }

#if 0
    {
      unsigned int i = 0;
      char bigbuffer [ 200 * 1024 ] = "\0";
      char b [ 10 ];
      pnd_log ( PND_LOG_DEFAULT, "    Read hexdump\n" );
      while ( i < bitlen ) {
	sprintf ( b, "%x,", bits [ i ] );
	strcat ( bigbuffer, b );
	i++;
      }
      pnd_log ( PND_LOG_DEFAULT, bigbuffer );
    }
#endif

    if ( fwrite ( bits, bitlen, 1, target ) != 1 ) {
      fclose ( pnd );
      fclose ( target );
      unlink ( buffer );
      pnd_log ( PND_LOG_DEFAULT, "    Emit icon, bad write\n" );
      return ( 0 );
    }

    len -= bitlen;
    //pnd_log ( PND_LOG_DEFAULT, "    Emit icon, next block, length: %u\n", len );
  } // while

  fclose ( pnd );
  fclose ( target );

  //pnd_log ( PND_LOG_DEFAULT, "    Emit icon, done.\n" );

  return ( 1 );
}
Esempio n. 14
0
unsigned char pnd_emit_dotinfo ( char *targetpath, char *pndrun, pnd_disco_t *p ) {
  char filename [ FILENAME_MAX ];
  char buffer [ 1024 ];
  FILE *f;
  char *viewer, *searchpath;
  pnd_conf_handle desktoph;

  // specification
  // http://standards.freedesktop.org/desktop-entry-spec

  // validation
  //

  // viewer
  searchpath = pnd_conf_query_searchpath();

  desktoph = pnd_conf_fetch_by_id ( pnd_conf_desktop, searchpath );

  if ( ! desktoph ) {
    return ( 0 );
  }

  viewer = pnd_conf_get_as_char ( desktoph, "info.viewer" );

  if ( ! viewer ) {
    return ( 0 ); // no way to view the file
  }

  // etc
  if ( ! p -> unique_id ) {
    pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing unique-id\n", targetpath );
    return ( 0 );
  }

  if ( ! p -> info_filename ) {
    pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing info_filename\n", targetpath );
    return ( 0 );
  }

  if ( ! p -> info_name ) {
    pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing info_name\n", targetpath );
    return ( 0 );
  }

  if ( ! targetpath ) {
    pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing target path\n", targetpath );
    return ( 0 );
  }

  if ( ! pndrun ) {
    pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing pnd_run.sh\n", targetpath );
    return ( 0 );
  }

  // set up

  sprintf ( filename, "%s/%s#%uinfo.desktop", targetpath, p -> unique_id, p -> subapp_number );

  // emit

  f = fopen ( filename, "w" );

  if ( ! f ) {
    return ( 0 );
  }

  fprintf ( f, "%s\n", PND_DOTDESKTOP_HEADER );

  if ( p -> info_name ) {
    snprintf ( buffer, 1020, "Name=%s\n", p -> info_name );
    fprintf ( f, "%s", buffer );
  }

  fprintf ( f, "Type=Application\n" );
  fprintf ( f, "Version=1.0\n" );

#if 0
  if ( p -> icon ) {
    snprintf ( buffer, 1020, "Icon=%s\n", p -> icon );
    fprintf ( f, "%s", buffer );
  }
#endif

  if ( p -> unique_id ) {
    fprintf ( f, "X-Pandora-UID=%s\n", p -> unique_id );
  }

  if ( p -> title_en && p -> title_en [ 0 ] ) {
    snprintf ( buffer, 1020, "Comment=Automatic menu info entry for %s\n", p -> title_en );
    fprintf ( f, "%s", buffer );
  }

#if 0 // we let pnd_run.sh command line handle this instead of in .desktop
  if ( p -> startdir ) {
    snprintf ( buffer, 1020, "Path=%s\n", p -> startdir );
    fprintf ( f, "%s", buffer );
  } else {
    fprintf ( f, "Path=%s\n", PND_DEFAULT_WORKDIR );
  }
#endif

  // exec line
  char args [ 1001 ];
  char *pargs = args;
  char *viewerargs = pnd_conf_get_as_char ( desktoph, "info.viewer_args" );
  if ( viewerargs && viewerargs [ 0 ] ) {
    snprintf ( pargs, 1001, "%s %s",
	       pnd_conf_get_as_char ( desktoph, "info.viewer_args" ), p -> info_filename );
  } else {
    pargs = NULL;
    // WARNING: This might not be quite right; if no viewer-args, shouldn't we still append the info-filename? likewise,
    //          even if we do have view-args, shouldn't we check if filename is present?
  }

  char pndfile [ 1024 ];
  if ( p -> object_type == pnd_object_type_directory ) {
    // for PXML-app-dir, pnd_run.sh doesn't want the PXML.xml.. it just wants the dir-name
    strncpy ( pndfile, p -> object_path, 1000 );
  } else if ( p -> object_type == pnd_object_type_pnd ) {
    // pnd_run.sh wants the full path and filename for the .pnd file
    snprintf ( pndfile, 1020, "%s/%s", p -> object_path, p -> object_filename );
  }

  pnd_apps_exec_info_t info;
  info.viewer = viewer;
  info.args = pargs;

  if ( ! pnd_apps_exec_disco ( pndrun, p, PND_EXEC_OPTION_NORUN | PND_EXEC_OPTION_INFO, &info ) ) {
    return ( 0 );
  }

  fprintf ( f, "Exec=%s\n", pnd_apps_exec_runline() );

  if ( pnd_conf_get_as_char ( desktoph, "info.category" ) ) {
    fprintf ( f, "Categories=%s\n", pnd_conf_get_as_char ( desktoph, "info.category" ) );
  } else {
    fprintf ( f, "Categories=Documentation\n" );
  }

  fprintf ( f, "%s\n", PND_DOTDESKTOP_SOURCE ); // should we need to know 'who' created the file during trimming

  fclose ( f );

  return ( 1 );
}
Esempio n. 15
0
unsigned char pnd_emit_dotdesktop ( char *targetpath, char *pndrun, pnd_disco_t *p ) {
  char filename [ FILENAME_MAX ];
  char buffer [ 4096 ];
  FILE *f;

  // specification
  // http://standards.freedesktop.org/desktop-entry-spec

  // validation

  if ( ! p -> unique_id ) {
    pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing unique-id\n", targetpath );
    return ( 0 );
  }

  if ( ! p -> exec ) {
    pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing exec\n", targetpath );
    return ( 0 );
  }

  if ( ! targetpath ) {
    pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing target path\n", targetpath );
    return ( 0 );
  }

  if ( ! pndrun ) {
    pnd_log ( PND_LOG_DEFAULT, "Can't emit dotdesktop for %s, missing pnd_run.sh\n", targetpath );
    return ( 0 );
  }

  // set up

  sprintf ( filename, "%s/%s#%u.desktop", targetpath, p -> unique_id, p -> subapp_number );

  // emit

  //printf ( "EMIT DOTDESKTOP '%s'\n", filename );

  f = fopen ( filename, "w" );

  if ( ! f ) {
    return ( 0 );
  }

  fprintf ( f, "%s\n", PND_DOTDESKTOP_HEADER );

  if ( p -> title_en ) {
    snprintf ( buffer, 1020, "Name=%s\n", p -> title_en );
    fprintf ( f, "%s", buffer );
  }

  fprintf ( f, "Type=Application\n" );
  fprintf ( f, "Version=1.0\n" );

  if ( p -> icon ) {
    snprintf ( buffer, 1020, "Icon=%s\n", p -> icon );
    fprintf ( f, "%s", buffer );
  }

  if ( p -> unique_id ) {
    fprintf ( f, "X-Pandora-UID=%s\n", p -> unique_id );
  }

#if 1
  if ( p -> desc_en && p -> desc_en [ 0 ] ) {
    snprintf ( buffer, 1020, "Comment=%s\n", p -> desc_en ); // no [en] needed I suppose, yet
    fprintf ( f, "%s", buffer );
  }
#endif

  if ( p -> preview_pic1 ) {
    fprintf ( f, "X-Pandora-Preview-Pic-1=%s\n", p -> preview_pic1 );
  }

  if ( p -> clockspeed ) {
    fprintf ( f, "X-Pandora-Clockspeed=%s\n", p -> clockspeed );
  }

  if ( p -> startdir ) {
    fprintf ( f, "X-Pandora-Startdir=%s\n", p -> startdir );
  }

  if ( p -> main_category ) {
    fprintf ( f, "X-Pandora-MainCategory=%s\n", p -> main_category );
  }
  if ( p -> main_category1 ) {
    fprintf ( f, "X-Pandora-MainCategory1=%s\n", p -> main_category1 );
  }
  if ( p -> main_category2 ) {
    fprintf ( f, "X-Pandora-MainCategory2=%s\n", p -> main_category2 );
  }

  if ( p -> alt_category ) {
    fprintf ( f, "X-Pandora-AltCategory=%s\n", p -> alt_category );
  }
  if ( p -> alt_category1 ) {
    fprintf ( f, "X-Pandora-AltCategory1=%s\n", p -> alt_category1 );
  }
  if ( p -> alt_category2 ) {
    fprintf ( f, "X-Pandora-AltCategory2=%s\n", p -> alt_category2 );
  }
  if ( p -> info_filename ) {
    fprintf ( f, "X-Pandora-Info-Filename=%s\n", p -> info_filename );
  }
  if ( p -> info_name ) {
    fprintf ( f, "X-Pandora-Info-Name=%s\n", p -> info_name );
  }

#if 0 // we let pnd_run.sh command line handle this instead of in .desktop
  if ( p -> startdir ) {
    snprintf ( buffer, 1020, "Path=%s\n", p -> startdir );
    fprintf ( f, "%s", buffer );
  } else {
    fprintf ( f, "Path=%s\n", PND_DEFAULT_WORKDIR );
  }
#endif

  // association requests
  if ( p -> associationitem1_filetype ) {
    fprintf ( f, "MimeType=%s\n", p -> associationitem1_filetype );
  }

  if ( p -> exec ) {
    char *nohup;

    if ( p -> option_no_x11 ) {
      nohup = "/usr/bin/nohup ";
    } else {
      nohup = "";
    }

    // basics
    if ( p -> object_type == pnd_object_type_directory ) {
      snprintf ( buffer, 1020, "Exec=%s%s -p \"%s\" -e \"%s\" -b \"%s\"",
		 nohup, pndrun, p -> object_path, p -> exec,
		 p -> appdata_dirname ? p -> appdata_dirname : p -> unique_id );
    } else if ( p -> object_type == pnd_object_type_pnd ) {
      snprintf ( buffer, 1020, "Exec=%s%s -p \"%s/%s\" -e \"%s\" -b \"%s\"",
		 nohup, pndrun, p -> object_path, p -> object_filename, p -> exec,
		 p -> appdata_dirname ? p -> appdata_dirname : p -> unique_id );
    }

    // start dir
    if ( p -> startdir ) {
      strncat ( buffer, " -s ", 1020 );
      strncat ( buffer, p -> startdir, 1020 );
    }

    // args (regular args, for pnd_run.sh to handle)
    if ( p -> execargs ) {
      char argbuf [ 1024 ];
      snprintf ( argbuf, 1000, " -a \"%s\"", p -> execargs );
      strncat ( buffer, argbuf, 1020 );
    }

    // clockspeed
    if ( p -> clockspeed && atoi ( p -> clockspeed ) != 0 ) {
      strncat ( buffer, " -c ", 1020 );
      strncat ( buffer, p -> clockspeed, 1020 );
    }

    // exec options
    if ( pnd_pxml_get_x11 ( p -> option_no_x11 ) == pnd_pxml_x11_stop ) {
      strncat ( buffer, " -x ", 1020 );
    }

    // args (dashdash -- args, that the shell can screw with before pnd_run.sh gets them)
    if ( p -> exec_dashdash_args ) {
      char argbuf [ 4096 ];
      snprintf ( argbuf, 4096, " -- %s", p -> exec_dashdash_args );
      strncat ( buffer, argbuf, 4096 );
    }

    // newline
    strncat ( buffer, "\n", 1020 );

    // emit
    fprintf ( f, "%s", buffer );

    // and lets copy in some stuff in case it makes .desktop consumers life easier
    if ( p -> exec ) { fprintf ( f, "X-Pandora-Exec=%s\n", p -> exec ); }
    if ( p -> appdata_dirname ) { fprintf ( f, "X-Pandora-Appdata-Dirname=%s\n", p -> appdata_dirname ); }
    if ( p -> execargs ) { fprintf ( f, "X-Pandora-ExecArgs=%s\n", p -> execargs ); }
    if ( p -> object_flags & PND_DISCO_FLAG_OVR ) { fprintf ( f, "X-Pandora-Object-Flag-OVR=%s\n", "Yes" ); }
    if ( p -> object_type == pnd_object_type_pnd ) {
      fprintf ( f, "X-Pandora-Object-Path=%s\n", p -> object_path );
      fprintf ( f, "X-Pandora-Object-Filename=%s\n", p -> object_filename );
    }

  }

  // categories
  {
    char cats [ 512 ] = "";
    int n;
    pnd_conf_handle c;
    char *confpath;

    // uuuuh, defaults?
    // "Application" used to be in the standard and is commonly supported still
    // Utility and Network should ensure the app is visible 'somewhere' :/
    char *defaults = PND_DOTDESKTOP_DEFAULT_CATEGORY;

    // determine searchpath (for conf, not for apps)
    confpath = pnd_conf_query_searchpath();

    // inhale the conf file
    c = pnd_conf_fetch_by_id ( pnd_conf_categories, confpath );

    // if we can find a default category set, pull it in; otherwise assume
    // the hardcoded one
    if ( pnd_conf_get_as_char ( c, "default" ) ) {
      defaults = pnd_conf_get_as_char ( c, "default" );
    }

    // ditch the confpath
    free ( confpath );

    // attempt mapping
    if ( c ) {

      n = pnd_map_dotdesktop_categories ( c, cats, 511, p );

      if ( n ) {
	fprintf ( f, "Categories=%s\n", cats );
      } else {
	fprintf ( f, "Categories=%s\n", defaults );
      }

    } else {
      fprintf ( f, "Categories=%s\n", defaults );
    }

  }

  fprintf ( f, "%s\n", PND_DOTDESKTOP_SOURCE ); // should we need to know 'who' created the file during trimming

  fclose ( f );

  return ( 1 );
}