Juggled timers around; now can sit at 'idle' and eating only 3-4% of cpu. Should...
[pandora-libraries.git] / minimenu / mmui.c
index a4d0246..f04a31b 100644 (file)
@@ -28,6 +28,8 @@
 #include "../lib/pnd_pathiter.h"
 #include "pnd_utility.h"
 #include "pnd_pndfiles.h"
+#include "pnd_notify.h"
+#include "pnd_dbusnotify.h"
 
 #include "mmenu.h"
 #include "mmcat.h"
@@ -52,10 +54,12 @@ unsigned int render_mask = CHANGED_EVERYTHING;
 SDL_Surface *sdl_realscreen = NULL;
 unsigned int sdl_ticks = 0;
 SDL_Thread *g_preview_thread = NULL;
+SDL_Thread *g_timer_thread = NULL;
 
 enum { sdl_user_ticker = 0,
        sdl_user_finishedpreview = 1,
        sdl_user_finishedicon = 2,
+       sdl_user_checksd = 3,
 };
 
 /* app state
@@ -1029,14 +1033,14 @@ void ui_render ( void ) {
 
 } // ui_render
 
-void ui_process_input ( unsigned char block_p ) {
+void ui_process_input ( pnd_dbusnotify_handle dbh, pnd_notify_handle nh ) {
   SDL_Event event;
 
   unsigned char ui_event = 0; // if we get a ui event, flip to 1 and break
   //static ui_sdl_button_e ui_mask = uisb_none; // current buttons down
 
-  while ( ! ui_event &&
-         block_p ? SDL_WaitEvent ( &event ) : SDL_PollEvent ( &event ) )
+  while ( ( ! ui_event ) &&
+         /*block_p ?*/ SDL_WaitEvent ( &event ) /*: SDL_PollEvent ( &event )*/ )
   {
 
     switch ( event.type ) {
@@ -1105,7 +1109,29 @@ void ui_process_input ( unsigned char block_p ) {
        // redraw, so we can show the newly loaded icon
        ui_event++;
 
-      }
+      } else if ( event.user.code == sdl_user_checksd ) {
+       // check if any inotify-type events occured, forcing us to rescan/re-disco the SDs
+
+       unsigned char watch_dbus = 0;
+       unsigned char watch_inotify = 0;
+
+       if ( dbh ) {
+         watch_dbus = pnd_dbusnotify_rediscover_p ( dbh );
+       }
+
+       if ( nh ) {
+         watch_inotify = pnd_notify_rediscover_p ( nh );
+       }
+
+       if ( watch_dbus || watch_inotify ) {
+         pnd_log ( pndn_debug, "dbusnotify detected SD event\n" );
+         applications_free();
+         applications_scan();
+
+         ui_event++;
+       }
+
+      } // SDL user event
 
       render_mask |= CHANGED_EVERYTHING;
 
@@ -1243,12 +1269,25 @@ void ui_process_input ( unsigned char block_p ) {
       } else if ( event.key.keysym.sym == SDLK_RCTRL || event.key.keysym.sym == SDLK_PERIOD ) { // right trigger or period
        ui_push_rtrigger();
        ui_event++;
+
       } else if ( event.key.keysym.sym == SDLK_PAGEUP ) { // Y
        // info
        if ( ui_selected ) {
          ui_show_info ( pnd_run_script, ui_selected -> ref );
          ui_event++;
        }
+      } else if ( event.key.keysym.sym == SDLK_PAGEDOWN ) { // X
+       ui_push_backup();
+
+       // forget the selection, nolonger applies
+       ui_selected = NULL;
+       ui_set_selected ( ui_selected );
+       // rescan the dir
+       if ( g_categories [ ui_category ] -> fspath ) {
+         category_fs_restock ( g_categories [ ui_category ] );
+       }
+       // redraw the grid
+       render_mask |= CHANGED_EVERYTHING;
 
       } else if ( event.key.keysym.sym == SDLK_LALT ) { // start button
        ui_push_exec();
@@ -1614,6 +1653,52 @@ void ui_push_down ( void ) {
   return;
 }
 
+// 'backup' is currently 'X', for going back up in a folder/subcat without having to hit exec on the '..' entry
+void ui_push_backup ( void ) {
+
+  // a subcat-as-dir, or a dir browser?
+  if ( g_categories [ ui_category] -> fspath ) {
+    // dir browser, just climb our way back up
+
+    // go up
+    char *c;
+
+    // lop off last word; if the thing ends with /, lop that one, then the next word.
+    while ( ( c = strrchr ( g_categories [ ui_category] -> fspath, '/' ) ) ) {
+      *c = '\0'; // lop off the last hunk
+      if ( *(c+1) != '\0' ) {
+       break;
+      }
+    } // while
+
+    // nothing left?
+    if ( g_categories [ ui_category] -> fspath [ 0 ] == '\0' ) {
+      free ( g_categories [ ui_category] -> fspath );
+      g_categories [ ui_category] -> fspath = strdup ( "/" );
+    }
+
+  } else {
+    // a pnd subcat .. are we in one, or at the 'top'?
+    char *pcatname = g_categories [ ui_category ] -> parent_catname;
+
+    if ( ! pcatname ) {
+      return; // we're at the 'top' already
+    }
+
+    // set to first cat!
+    ui_category = 0;
+    // republish cats .. shoudl just be the one
+    category_publish ( CFNORMAL, NULL );
+
+    if ( pcatname ) {
+      ui_category = category_index ( pcatname );
+    }
+
+  } // dir or subcat?
+
+  return;
+}
+
 void ui_push_exec ( void ) {
 
   if ( ! ui_selected ) {
@@ -1633,26 +1718,15 @@ void ui_push_exec ( void ) {
       if ( ! g_categories [ ui_category] -> fspath ) {
        // pnd subcat as dir
 
-       static char *ui_category_stack = NULL;
-
        // are we already in a subcat? if so, go back to parent; there is no grandparenting or deeper
        if ( g_categories [ ui_category ] -> parent_catname ) {
          // go back up
-
-         // set to first cat!
-         ui_category = 0;
-         // republish cats .. shoudl just be the one
-         category_publish ( CFNORMAL, NULL );
-
-         if ( ui_category_stack ) {
-           ui_category = category_index ( ui_category_stack );
-         }
+         ui_push_backup();
 
        } else {
          // delve into subcat
 
          // set to first cat!
-         ui_category_stack = g_categories [ ui_category ] -> catname;
          ui_category = 0;
          // republish cats .. shoudl just be the one
          category_publish ( CFBYNAME, ui_selected -> ref -> object_path );
@@ -1669,22 +1743,7 @@ void ui_push_exec ( void ) {
 
        // delve up/down the dir tree
        if ( strcmp ( ui_selected -> ref -> title_en, ".." ) == 0 ) {
-         // go up
-         char *c;
-
-         // lop off last word; if the thing ends with /, lop that one, then the next word.
-         while ( ( c = strrchr ( g_categories [ ui_category] -> fspath, '/' ) ) ) {
-           *c = '\0'; // lop off the last hunk
-           if ( *(c+1) != '\0' ) {
-             break;
-           }
-         } // while
-
-         // nothing left?
-         if ( g_categories [ ui_category] -> fspath [ 0 ] == '\0' ) {
-           free ( g_categories [ ui_category] -> fspath );
-           g_categories [ ui_category] -> fspath = strdup ( "/" );
-         }
+         ui_push_backup();
 
        } else {
          // go down
@@ -2511,6 +2570,44 @@ unsigned char ui_forkexec ( char *argv[] ) {
   return ( 1 );
 }
 
+unsigned char ui_threaded_timer_create ( void ) {
+
+  g_timer_thread = SDL_CreateThread ( (void*)ui_threaded_timer, NULL );
+
+  if ( ! g_timer_thread ) {
+    pnd_log ( pndn_error, "ERROR: Couldn't create timer thread\n" );
+    return ( 0 );
+  }
+
+  return ( 1 );
+}
+
+int ui_threaded_timer ( pnd_disco_t *p ) {
+
+  // this timer's job is to ..
+  // - do nothing for quite awhile
+  // - on wake, post event to SDL event queue, so that main thread will check if SD insert/eject occurred
+  // - goto 10
+
+  unsigned int delay_s = 2; // seconds
+
+  while ( 1 ) {
+
+    // pause...
+    sleep ( delay_s );
+
+    // .. trigger SD check
+    SDL_Event e;
+    bzero ( &e, sizeof(SDL_Event) );
+    e.type = SDL_USEREVENT;
+    e.user.code = sdl_user_checksd;
+    SDL_PushEvent ( &e );
+
+  } // while
+
+  return ( 0 );
+}
+
 unsigned char ui_threaded_defered_preview ( pnd_disco_t *p ) {
 
   if ( ! cache_preview ( p, pnd_conf_get_as_int_d ( g_conf, "previewpic.cell_width", 200 ),