change one comment in pnd_pxml.h to be more right
[pandora-libraries.git] / minimenu / mmui.c
index aa7cf76..d0d3f78 100644 (file)
@@ -10,6 +10,7 @@
 #include "SDL_ttf.h"
 #include "SDL_gfxPrimitives.h"
 #include "SDL_rotozoom.h"
+#include "SDL_thread.h"
 
 #include "pnd_conf.h"
 #include "pnd_logger.h"
  */
 SDL_Surface *sdl_realscreen = NULL;
 unsigned int sdl_ticks = 0;
+SDL_Thread *g_preview_thread = NULL;
+
+enum { sdl_user_ticker = 0,
+       sdl_user_finishedpreview = 1,
+       sdl_user_finishedicon = 2,
+};
 
 /* app state
  */
@@ -177,6 +184,9 @@ mm_imgcache_t g_imagecache [ IMG_TRUEMAX ] = {
   { IMG_SELECTED_ALPHAMASK,   "graphics.IMG_SELECTED_ALPHAMASK" },
   { IMG_TAB_SEL,              "graphics.IMG_TAB_SEL" },
   { IMG_TAB_UNSEL,            "graphics.IMG_TAB_UNSEL" },
+  { IMG_TAB_LINE,             "graphics.IMG_TAB_LINE" },
+  { IMG_TAB_LINEL,            "graphics.IMG_TAB_LINEL" },
+  { IMG_TAB_LINER,            "graphics.IMG_TAB_LINER" },
   { IMG_ICON_MISSING,         "graphics.IMG_ICON_MISSING" },
   { IMG_SELECTED_HILITE,      "graphics.IMG_SELECTED_HILITE" },
   { IMG_PREVIEW_MISSING,      "graphics.IMG_PREVIEW_MISSING" },
@@ -271,6 +281,7 @@ void ui_render ( unsigned int render_mask ) {
   unsigned int icon_offset_x = pnd_conf_get_as_int ( g_conf, "grid.icon_offset_x" );
   unsigned int icon_offset_y = pnd_conf_get_as_int ( g_conf, "grid.icon_offset_y" );
   unsigned int icon_max_width = pnd_conf_get_as_int ( g_conf, "grid.icon_max_width" );
+  unsigned int icon_max_height = pnd_conf_get_as_int ( g_conf, "grid.icon_max_height" );
 
   unsigned int text_width = pnd_conf_get_as_int ( g_conf, "grid.text_width" );
   unsigned int text_clip_x = pnd_conf_get_as_int ( g_conf, "grid.text_clip_x" );
@@ -343,16 +354,17 @@ void ui_render ( unsigned int render_mask ) {
   if ( g_imagecache [ IMG_TAB_SEL ].i && g_imagecache [ IMG_TAB_UNSEL ].i ) {
     unsigned int tab_width = pnd_conf_get_as_int ( g_conf, "tabs.tab_width" );
     unsigned int tab_height = pnd_conf_get_as_int ( g_conf, "tabs.tab_height" );
+    unsigned int tab_selheight = pnd_conf_get_as_int ( g_conf, "tabs.tab_selheight" );
     unsigned int tab_offset_x = pnd_conf_get_as_int ( g_conf, "tabs.tab_offset_x" );
     unsigned int tab_offset_y = pnd_conf_get_as_int ( g_conf, "tabs.tab_offset_y" );
     unsigned int text_offset_x = pnd_conf_get_as_int ( g_conf, "tabs.text_offset_x" );
     unsigned int text_offset_y = pnd_conf_get_as_int ( g_conf, "tabs.text_offset_y" );
     unsigned int text_width = pnd_conf_get_as_int ( g_conf, "tabs.text_width" );
+    unsigned int maxtab = ( screen_width / tab_width ) < g_categorycount ? ( screen_width / tab_width ) + ui_catshift : g_categorycount + ui_catshift;
 
+    // draw tabs with categories
     for ( col = ui_catshift;
-         col < ( 
-                  ( screen_width / tab_width ) < g_categorycount ? ( screen_width / tab_width ) + ui_catshift : g_categorycount + ui_catshift
-               );
+         col < maxtab;
          col++ )
     {
 
@@ -367,7 +379,11 @@ void ui_render ( unsigned int render_mask ) {
       src.x = 0;
       src.y = 0;
       src.w = tab_width;
-      src.h = tab_height;
+      if ( col == ui_category ) {
+       src.h = tab_selheight;
+      } else {
+       src.h = tab_height;
+      }
       dest -> x = tab_offset_x + ( (col-ui_catshift) * tab_width );
       dest -> y = tab_offset_y;
       //pnd_log ( pndn_debug, "tab %u at %ux%u\n", col, dest.x, dest.y );
@@ -378,6 +394,23 @@ void ui_render ( unsigned int render_mask ) {
 
       dest++;
 
+      // draw tab line
+      if ( col == ui_category ) {
+       // no line for selected tab
+      } else {
+       if ( col - ui_catshift == 0 ) {
+         s = g_imagecache [ IMG_TAB_LINEL ].i;
+       } else if ( col - ui_catshift == maxtab - 1 ) {
+         s = g_imagecache [ IMG_TAB_LINER ].i;
+       } else {
+         s = g_imagecache [ IMG_TAB_LINE ].i;
+       }
+       dest -> x = tab_offset_x + ( (col-ui_catshift) * tab_width );
+       dest -> y = tab_offset_y + tab_height;
+       SDL_BlitSurface ( s, NULL /* whole image */, sdl_realscreen, dest );
+       dest++;
+      }
+
       // draw text
       SDL_Surface *rtext;
       SDL_Color tmpfontcolor = { font_rgba_r, font_rgba_g, font_rgba_b, font_rgba_a };
@@ -394,6 +427,25 @@ void ui_render ( unsigned int render_mask ) {
 
     } // for
 
+    // draw tab lines under where tabs would be if we had categories
+    maxtab = ( screen_width / tab_width );
+    for ( /* foo */; col < maxtab; col++ ) {
+      SDL_Surface *s;
+
+      if ( col - ui_catshift == 0 ) {
+       s = g_imagecache [ IMG_TAB_LINEL ].i;
+      } else if ( col - ui_catshift == maxtab - 1 ) {
+       s = g_imagecache [ IMG_TAB_LINER ].i;
+      } else {
+       s = g_imagecache [ IMG_TAB_LINE ].i;
+      }
+      dest -> x = tab_offset_x + ( (col-ui_catshift) * tab_width );
+      dest -> y = tab_offset_y + tab_height;
+      SDL_BlitSurface ( s, NULL /* whole image */, sdl_realscreen, dest );
+      dest++;
+
+    } // for
+
   } // tabs
 
   // scroll bars and arrows
@@ -504,7 +556,7 @@ void ui_render ( unsigned int render_mask ) {
            src.w = 60;
            src.h = 60;
            dest -> x = grid_offset_x + ( col * cell_width ) + icon_offset_x + (( icon_max_width - iconsurface -> w ) / 2);
-           dest -> y = grid_offset_y + ( displayrow * cell_height ) + icon_offset_y;
+           dest -> y = grid_offset_y + ( displayrow * cell_height ) + icon_offset_y + (( icon_max_height - iconsurface -> h ) / 2);
 
            SDL_BlitSurface ( iconsurface, &src, sdl_realscreen, dest );
 
@@ -628,6 +680,21 @@ void ui_render ( unsigned int render_mask ) {
       desty += src.h;
     }
 
+    // show sub-app# on right side of cpu clock?
+    //if ( ui_selected -> ref -> subapp_number )
+    {
+      sprintf ( buffer, "(app#%u)", ui_selected -> ref -> subapp_number );
+
+      SDL_Surface *rtext;
+      SDL_Color tmpfontcolor = { font_rgba_r, font_rgba_g, font_rgba_b, font_rgba_a };
+      rtext = TTF_RenderText_Blended ( g_grid_font, buffer, tmpfontcolor );
+      dest -> x = cell_offset_x + cell_width - rtext -> w;
+      dest -> y = desty - src.h;
+      SDL_BlitSurface ( rtext, NULL /* full src */, sdl_realscreen, dest );
+      SDL_FreeSurface ( rtext );
+      dest++;
+    }
+
     // info hint
     if ( ui_selected -> ref -> clockspeed && ui_selected -> ref -> info_filename ) {
 
@@ -673,9 +740,15 @@ void ui_render ( unsigned int render_mask ) {
 
   // battery
   if ( 1 ) {
-    unsigned char batterylevel = pnd_device_get_battery_gauge_perc();
+    static int last_battlevel = 0;
+    static unsigned char batterylevel = 0;
     char buffer [ 100 ];
 
+    if ( time ( NULL ) - last_battlevel > 60 ) {
+      batterylevel = pnd_device_get_battery_gauge_perc();
+      last_battlevel = time ( NULL );
+    }
+
     sprintf ( buffer, "Battery: %u%%", batterylevel );
 
     SDL_Surface *rtext;
@@ -739,22 +812,57 @@ void ui_process_input ( unsigned char block_p ) {
     case SDL_USEREVENT:
       // update something
 
-      if ( pnd_conf_get_as_int_d ( g_conf, "minimenu.load_previews_later", 0 ) ) {
+      if ( event.user.code == sdl_user_ticker ) {
+
+       // timer went off, time to load something
+       if ( pnd_conf_get_as_int_d ( g_conf, "minimenu.load_previews_later", 0 ) ) {
 
-       pnd_log ( pndn_debug, "Deferred preview pic load ----------\n" );
+         pnd_log ( pndn_debug, "Deferred preview pic load ----------\n" );
 
-       // load the preview pics now!
-       pnd_disco_t *iter = ui_selected -> ref;
+         // load the preview pics now!
+         pnd_disco_t *iter = ui_selected -> ref;
 
-       if ( iter -> preview_pic1 &&
-            ! cache_preview ( iter, pnd_conf_get_as_int_d ( g_conf, "previewpic.cell_width", 200 ), pnd_conf_get_as_int_d ( g_conf, "previewpic.cell_height", 180 ) ) )
-       {
-         pnd_log ( pndn_debug, "  Couldn't load preview pic: '%s' -> '%s'\n", IFNULL(iter->title_en,"No Name"), iter -> preview_pic1 );
+         if ( iter -> preview_pic1 ) {
+
+           if ( pnd_conf_get_as_int_d ( g_conf, "minimenu.threaded_preview", 0 ) ) {
+
+             g_preview_thread = SDL_CreateThread ( (void*)ui_threaded_defered_preview, iter );
+
+             if ( ! g_preview_thread ) {
+               pnd_log ( pndn_error, "ERROR: Couldn't create preview thread\n" );
+             }
+
+           } else {
+
+             if ( ! cache_preview ( iter, pnd_conf_get_as_int_d ( g_conf, "previewpic.cell_width", 200 ),
+                                    pnd_conf_get_as_int_d ( g_conf, "previewpic.cell_height", 180 ) )
+                )
+             {
+               pnd_log ( pndn_debug, "  Couldn't load preview pic: '%s' -> '%s'\n",
+                         IFNULL(iter->title_en,"No Name"), iter -> preview_pic1 );
+             }
+
+           } // threaded?
+
+         } // got a preview at all?
+
+         pnd_log ( pndn_debug, "Deferred preview pic load finish ---\n" );
+
+         ui_event++;
        }
 
-       pnd_log ( pndn_debug, "Deferred preview pic load finish ---\n" );
+      } else if ( event.user.code == sdl_user_finishedpreview ) {
+
+       // if we just finished the one we happen to be looking at, better redraw now; otherwise, if
+       // we finished another, no big woop
+       if ( ui_selected && event.user.data1 == ui_selected -> ref ) {
+         ui_event++;
+       }
 
+      } else if ( event.user.code == sdl_user_finishedicon ) {
+       // redraw, so we can show the newly loaded icon
        ui_event++;
+
       }
 
       break;
@@ -896,12 +1004,13 @@ void ui_process_input ( unsigned char block_p ) {
          "Shutdown Pandora",
          "Rescan for Applications",
          "Run xfce4 from Minimenu",
+         "Run a terminal/console",
          "Exit and run xfce4",
          "Exit and run pmenu",
          "Quit (<- beware)",
          "About Minimenu"
        };
-       int sel = ui_modal_single_menu ( opts, 8, "Minimenu", "Enter to select; other to return." );
+       int sel = ui_modal_single_menu ( opts, 9, "Minimenu", "Enter to select; other to return." );
 
        char buffer [ 100 ];
        if ( sel == 0 ) {
@@ -925,22 +1034,32 @@ void ui_process_input ( unsigned char block_p ) {
          sprintf ( buffer, "%s %s\n", MM_RUN, "/usr/bin/startxfce4" );
          emit_and_quit ( buffer );
        } else if ( sel == 4 ) {
+         // run terminal
+         char *argv[5];
+         argv [ 0 ] = pnd_conf_get_as_char ( g_conf, "utility.terminal" );
+         argv [ 1 ] = NULL;
+
+         if ( argv [ 0 ] ) {
+           ui_forkexec ( argv );
+         }
+
+       } else if ( sel == 5 ) {
          // set env to xfce
          sprintf ( buffer, "echo startxfce4 > /tmp/gui.load" );
          system ( buffer );
          //sprintf ( buffer, "sudo poweroff" );
          //system ( buffer );
          exit ( 0 );
-       } else if ( sel == 5 ) {
+       } else if ( sel == 6 ) {
          // set env to pmenu
          sprintf ( buffer, "echo pmenu > /tmp/gui.load" );
          system ( buffer );
          //sprintf ( buffer, "sudo poweroff" );
          //system ( buffer );
          exit ( 0 );
-       } else if ( sel == 6 ) {
-         emit_and_quit ( MM_QUIT );
        } else if ( sel == 7 ) {
+         emit_and_quit ( MM_QUIT );
+       } else if ( sel == 8 ) {
          // about
        }
 
@@ -1068,6 +1187,10 @@ void ui_push_right ( unsigned char forcecoil ) {
 void ui_push_up ( void ) {
   unsigned char col_max = pnd_conf_get_as_int ( g_conf, MMENU_DISP_COLMAX );
 
+  if ( ! ui_selected ) {
+    return;
+  }
+
   // what row we in?
   unsigned int row = ui_determine_row ( ui_selected );
 
@@ -1344,6 +1467,7 @@ void ui_cachescreen ( unsigned char clearscreen, char *filename ) {
 
   SDL_Rect rects [ 4 ];
   SDL_Rect *dest = rects;
+  SDL_Rect src;
   bzero ( dest, sizeof(SDL_Rect)* 4 );
 
   unsigned int font_rgba_r = pnd_conf_get_as_int_d ( g_conf, "display.font_rgba_r", 200 );
@@ -1367,19 +1491,30 @@ void ui_cachescreen ( unsigned char clearscreen, char *filename ) {
       dest++;
     }
 
-  }
+  } else {
+
+    // render background
+    if ( g_imagecache [ IMG_BACKGROUND_800480 ].i ) {
+      src.x = 0;
+      src.y = 0;
+      src.w = sdl_realscreen -> w;
+      src.h = 100;
+      dest -> x = 0;
+      dest -> y = 0;
+      dest -> w = sdl_realscreen -> w;
+      dest -> h = sdl_realscreen -> h;
+      SDL_BlitSurface ( g_imagecache [ IMG_BACKGROUND_800480 ].i, &src, sdl_realscreen, dest );
+      dest++;
+    }
+
+  } // clear it
 
   // render text
   SDL_Surface *rtext;
   SDL_Color tmpfontcolor = { font_rgba_r, font_rgba_g, font_rgba_b, font_rgba_a };
   rtext = TTF_RenderText_Blended ( g_big_font, "Caching applications artwork...", tmpfontcolor );
-  if ( clearscreen ) {
-    dest -> x = 20;
-    dest -> y = 20;
-  } else {
-    dest -> x = 20;
-    dest -> y = 40;
-  }
+  dest -> x = 20;
+  dest -> y = 20;
   SDL_BlitSurface ( rtext, NULL /* full src */, sdl_realscreen, dest );
   SDL_FreeSurface ( rtext );
   dest++;
@@ -1460,7 +1595,9 @@ unsigned int ui_callback_f ( unsigned int t ) {
   }
 
   SDL_Event e;
+  bzero ( &e, sizeof(SDL_Event) );
   e.type = SDL_USEREVENT;
+  e.user.code = sdl_user_ticker;
   SDL_PushEvent ( &e );
 
   return ( 0 );
@@ -1788,3 +1925,98 @@ void ui_touch_act ( unsigned int x, unsigned int y ) {
 
   return;
 }
+
+unsigned char ui_forkexec ( char *argv[] ) {
+  char *fooby = argv[0];
+  int x;
+
+  if ( ( x = fork() ) < 0 ) {
+    pnd_log ( pndn_error, "ERROR: Couldn't fork() for '%s'\n", fooby );
+    return ( 0 );
+  }
+
+  if ( x == 0 ) { // child
+    execv ( fooby, argv );
+    pnd_log ( pndn_error, "ERROR: Couldn't exec(%s)\n", fooby );
+    return ( 0 );
+  }
+
+  // parent, success
+  return ( 1 );
+}
+
+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 ),
+                        pnd_conf_get_as_int_d ( g_conf, "previewpic.cell_height", 180 ) )
+     )
+  {
+    pnd_log ( pndn_debug, "THREAD: Couldn't load preview pic: '%s' -> '%s'\n",
+             IFNULL(p->title_en,"No Name"), p -> preview_pic1 );
+  }
+
+  // trigger that we completed
+  SDL_Event e;
+  bzero ( &e, sizeof(SDL_Event) );
+  e.type = SDL_USEREVENT;
+  e.user.code = sdl_user_finishedpreview;
+  e.user.data1 = p;
+  SDL_PushEvent ( &e );
+
+  return ( 0 );
+}
+
+SDL_Thread *g_icon_thread = NULL;
+void ui_post_scan ( void ) {
+
+  // if deferred icon load, kick off the thread now
+  if ( pnd_conf_get_as_int_d ( g_conf, "minimenu.load_icons_later", 0 ) == 1 ) {
+
+    g_icon_thread = SDL_CreateThread ( (void*)ui_threaded_defered_icon, NULL );
+
+    if ( ! g_icon_thread ) {
+      pnd_log ( pndn_error, "ERROR: Couldn't create icon thread\n" );
+    }
+
+  } // deferred icon load
+
+  return;
+}
+
+unsigned char ui_threaded_defered_icon ( void *p ) {
+  extern pnd_box_handle g_active_apps;
+  pnd_box_handle h = g_active_apps;
+
+  unsigned char maxwidth, maxheight;
+  maxwidth = pnd_conf_get_as_int_d ( g_conf, MMENU_DISP_ICON_MAX_WIDTH, 50 );
+  maxheight = pnd_conf_get_as_int_d ( g_conf, MMENU_DISP_ICON_MAX_HEIGHT, 50 );
+
+  pnd_disco_t *iter = pnd_box_get_head ( h );
+
+  while ( iter ) {
+
+    // cache it
+    if ( iter -> pnd_icon_pos &&
+        ! cache_icon ( iter, maxwidth, maxheight ) )
+    {
+      pnd_log ( pndn_warning, "  Couldn't load icon: '%s'\n", IFNULL(iter->title_en,"No Name") );
+    } else {
+
+      // trigger that we completed
+      SDL_Event e;
+      bzero ( &e, sizeof(SDL_Event) );
+      e.type = SDL_USEREVENT;
+      e.user.code = sdl_user_finishedicon;
+      SDL_PushEvent ( &e );
+
+      //pnd_log ( pndn_warning, "  Finished deferred load icon: '%s'\n", IFNULL(iter->title_en,"No Name") );
+      usleep ( pnd_conf_get_as_int_d ( g_conf, "minimenu.defer_icon_us", 50000 ) );
+
+    }
+
+    // next
+    iter = pnd_box_get_next ( iter );
+  } // while
+
+  return ( 0 );
+}