~ubuntu-branches/ubuntu/quantal/gnome-applets/quantal

« back to all changes in this revision

Viewing changes to battstat/battstat_applet.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2011-05-30 11:39:03 UTC
  • mfrom: (1.10.9 upstream) (1.4.3 experimental)
  • Revision ID: james.westby@ubuntu.com-20110530113903-1gru2a9vbvjt5wyj
Tags: 2.91.4~20110321-1ubuntu1
* Resynchronize on Debian, thanks Jeremy Bicha for some of the work
* Remaining diff
* debian/control.in:
  - Depend on introspection packages
  - Drop obsolete build-depends on libxklavier-dev
  - Don't build depends on system-tools-backends-dev, libcpufreq-dev
    libgstreamer0.10-dev, libgstreamer-plugins-base0.10-dev (no mixer built), 
    libupower-glib-dev (not using 01_battstat_upower.patch)
  - Remove gstreamer0.10-alsa | gstreamer0.10-audiosink depends and
    the mixer description from gnome-applets (mixer not built)
  - gnome-applets suggests gnome-netstatus-applet, deskbar-applet and
    cpufrequtils rather than recommends.
  - Remove keyboard layout switcher from package description
* debian/rules:
  - Don't enable the mixer applet, disable the battery status applet option
  - Compress packages with bzip2
  - use dh_pysupport
* debian/gnome-applets.manpages:
  - Stop shipping manpages, those are not distributed upstream either and the
    binaries are not in the standard path anyway so can't be run by users.
* debian/patches/01_no_more_deprecated.patch:
  - Add patch to migrate null_applet to libpanel-applet-3 (LP: #673109)
    and invest applet to use GObject introspection (LP: #673110)
* debian/patches/01_battstat_upower.patch:
  - Dropped, don't build the battery status applet
* debian/patches/80_correct_minicommander_install.patch:
  - Don't install the minicommander server when not required.
* debian/patches/81_silent_applet_cleaning:
  - Don't display a dialog about the applet configuration cleaning since 
    that's something which should be transparent for the users
* debian/patches/86_deprecate_applets.patch:
  - Define some applets as deprecated
* debian/watch:
  - Watch for unstable versions
* Dropped diff:
* debian/control.in:
  - Pre-depends on dpkg (required for bzip2 packaging), the dpkg version 
    has been available since before the previous lts

Show diffs side-by-side

added added

removed removed

Lines of Context:
73
73
#define DC_POWER_STRING _("System is running on battery power")
74
74
 
75
75
/* The icons for Battery, Critical, AC and Charging */
76
 
static GdkPixmap *statusimage[STATUS_PIXMAP_NUM];
77
 
static GdkBitmap *statusmask[STATUS_PIXMAP_NUM];
 
76
static GdkPixbuf *statusimage[STATUS_PIXMAP_NUM];
78
77
 
79
78
/* Assuming a horizontal battery, the colour is drawn into it one horizontal
80
79
   line at a time as a vertical gradient.  The following arrays decide where
218
217
static void
219
218
initialise_global_pixmaps( void )
220
219
{
221
 
  GdkDrawable *defaults;
222
 
 
223
 
  defaults = gdk_screen_get_root_window( gdk_screen_get_default() );
224
 
 
225
220
  statusimage[STATUS_PIXMAP_BATTERY] =
226
 
    gdk_pixmap_create_from_xpm_d( defaults, &statusmask[STATUS_PIXMAP_BATTERY],
227
 
                                  NULL, battery_small_xpm );
 
221
    gdk_pixbuf_new_from_xpm_data ((const char **) battery_small_xpm);
228
222
 
229
223
  statusimage[STATUS_PIXMAP_METER] =
230
 
    gdk_pixmap_create_from_xpm_d( defaults, &statusmask[STATUS_PIXMAP_METER],
231
 
                                  NULL, battery_small_meter_xpm );
 
224
    gdk_pixbuf_new_from_xpm_data ((const char **) battery_small_meter_xpm);
232
225
 
233
226
  statusimage[STATUS_PIXMAP_AC] =
234
 
    gdk_pixmap_create_from_xpm_d( defaults, &statusmask[STATUS_PIXMAP_AC],
235
 
                                  NULL, ac_small_xpm );
 
227
    gdk_pixbuf_new_from_xpm_data ((const char **) ac_small_xpm);
236
228
   
237
229
  statusimage[STATUS_PIXMAP_CHARGE] =
238
 
    gdk_pixmap_create_from_xpm_d( defaults, &statusmask[STATUS_PIXMAP_CHARGE],
239
 
                                  NULL, charge_small_xpm );
 
230
    gdk_pixbuf_new_from_xpm_data ((const char **) charge_small_xpm);
240
231
   
241
232
  statusimage[STATUS_PIXMAP_WARNING] =
242
 
    gdk_pixmap_create_from_xpm_d( defaults, &statusmask[STATUS_PIXMAP_WARNING],
243
 
                                  NULL, warning_small_xpm );
244
 
}
245
 
 
246
 
/* For non-truecolour displays, each GdkColor has to have a palette entry
247
 
   allocated for it.  This should only be done once for the entire display.
248
 
*/
249
 
static void
250
 
allocate_battery_colours( void )
251
 
{
252
 
  GdkColormap *colourmap;
253
 
  int i;
254
 
 
255
 
  colourmap = gdk_colormap_get_system();
256
 
 
257
 
  /* assumed: all the colour arrays have the same number of elements */
258
 
  for( i = 0; i < G_N_ELEMENTS( orange ); i++ )
259
 
  {
260
 
     gdk_colormap_alloc_color( colourmap, &darkorange[i], FALSE, TRUE );
261
 
     gdk_colormap_alloc_color( colourmap, &darkyellow[i], FALSE, TRUE );
262
 
     gdk_colormap_alloc_color( colourmap, &darkred[i], FALSE, TRUE );
263
 
     gdk_colormap_alloc_color( colourmap, &darkgreen[i], FALSE, TRUE );
264
 
     gdk_colormap_alloc_color( colourmap, &orange[i], FALSE, TRUE );
265
 
     gdk_colormap_alloc_color( colourmap, &yellow[i], FALSE, TRUE );
266
 
     gdk_colormap_alloc_color( colourmap, &red[i], FALSE, TRUE );
267
 
     gdk_colormap_alloc_color( colourmap, &green[i], FALSE, TRUE );
268
 
  }
 
233
    gdk_pixbuf_new_from_xpm_data ((const char **) warning_small_xpm);
269
234
}
270
235
 
271
236
/* Our backends may be either event driven or poll-based.
318
283
  if (!first_time)
319
284
    return NULL;
320
285
 
321
 
  allocate_battery_colours();
322
286
  initialise_global_pixmaps();
323
287
  err = power_management_initialise (no_hal, status_change_callback);
324
288
 
352
316
  gtk_window_set_screen( GTK_WINDOW (dialog),
353
317
                         gtk_widget_get_screen (GTK_WIDGET (applet)) );
354
318
 
355
 
  g_signal_connect_swapped( GTK_OBJECT (dialog), "response",
 
319
  g_signal_connect_swapped( G_OBJECT (dialog), "response",
356
320
                            G_CALLBACK (gtk_widget_destroy),
357
 
                            GTK_OBJECT (dialog) );
 
321
                            G_OBJECT (dialog) );
358
322
 
359
323
  gtk_widget_show_all( dialog );
360
324
}
435
399
                        GTK_ICON_LOOKUP_USE_BUILTIN,
436
400
                        NULL);
437
401
        
438
 
        NotifyNotification *n = notify_notification_new (_("Your battery is now fully recharged"), "", /* "battery" */ NULL, applet);
 
402
        NotifyNotification *n = notify_notification_new (_("Your battery is now fully recharged"), "", /* "battery" */ NULL);
439
403
 
440
404
        /* XXX: it would be nice to pass this as a named icon */
441
405
        notify_notification_set_icon_from_pixbuf (n, icon);
477
441
                GTK_STOCK_OK,
478
442
                GTK_RESPONSE_ACCEPT,
479
443
                NULL);
480
 
  g_signal_connect_swapped (GTK_OBJECT (dialog), "response",
 
444
  g_signal_connect_swapped (G_OBJECT (dialog), "response",
481
445
                            G_CALLBACK (gtk_widget_destroy),
482
 
                            GTK_OBJECT (dialog));
 
446
                            G_OBJECT (dialog));
483
447
 
484
448
  gtk_container_set_border_width (GTK_CONTAINER (dialog), 6);
485
 
  gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
486
449
  hbox = gtk_hbox_new (FALSE, 6);
487
450
  pixbuf = gtk_icon_theme_load_icon (
488
451
                gtk_icon_theme_get_default (),
636
599
  gtk_dialog_set_default_response( GTK_DIALOG (battery->battery_low_dialog),
637
600
                                   GTK_RESPONSE_ACCEPT );
638
601
 
639
 
  g_signal_connect_swapped( GTK_OBJECT (battery->battery_low_dialog),
 
602
  g_signal_connect_swapped( G_OBJECT (battery->battery_low_dialog),
640
603
                            "response",
641
604
                            G_CALLBACK (battery_low_dialog_destroy),
642
605
                            battery );
643
606
 
644
607
  gtk_container_set_border_width (GTK_CONTAINER (battery->battery_low_dialog),
645
608
                  6);
646
 
  gtk_dialog_set_has_separator (GTK_DIALOG (battery->battery_low_dialog),
647
 
                  FALSE);
648
609
  hbox = gtk_hbox_new (FALSE, 6);
649
610
  gtk_container_set_border_width (GTK_CONTAINER (hbox), 6);
650
611
  pixbuf = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
713
674
  g_free (tiptext);
714
675
}
715
676
 
 
677
static void
 
678
pixbuf_draw_line( GdkPixbuf *pixbuf, GdkColor *colour, int x1, int y1, int x2, int y2)
 
679
{
 
680
  guchar *pixels = gdk_pixbuf_get_pixels (pixbuf);
 
681
  int stride = gdk_pixbuf_get_rowstride (pixbuf);
 
682
  int channels = gdk_pixbuf_get_n_channels (pixbuf);
 
683
  guchar r, g, b, a;
 
684
  int i, n;
 
685
 
 
686
  r = colour->red >> 8;
 
687
  g = colour->green >> 8;
 
688
  b = colour->blue >> 8;
 
689
  a = 0xFF;
 
690
 
 
691
  pixels += stride * y1 + 4 * x1;
 
692
 
 
693
  if (x1 == x2)
 
694
  {
 
695
    /* stride = gdk_pixbuf_get_rowstride (pixbuf); */
 
696
    n = y2 - y1;
 
697
  }
 
698
  else if (y1 == y2)
 
699
  {
 
700
    stride = gdk_pixbuf_get_n_channels (pixbuf);
 
701
    n = x2 - x1;
 
702
  }
 
703
  else
 
704
    g_assert_not_reached ();
 
705
 
 
706
  for (i = 0; i < n; i++)
 
707
  {
 
708
    pixels[0] = r;
 
709
    pixels[1] = g;
 
710
    pixels[2] = b;
 
711
    if (channels == 4)
 
712
      pixels[3] = a;
 
713
 
 
714
    pixels += stride;
 
715
  }
 
716
}
 
717
 
716
718
/* Redraw the battery meter image.
717
719
 */
718
720
static void
719
721
update_battery_image (ProgressData *battstat, int batt_percent, int batt_time)
720
722
{
721
723
  GdkColor *color, *darkcolor;
722
 
  GdkPixmap *pixmap;
723
 
  GdkBitmap *pixmask;
 
724
  GdkPixbuf *pixbuf;
724
725
  guint progress_value;
725
726
  gint i, x;
726
727
  int batt_life;
751
752
    darkcolor = darkgreen;
752
753
  }
753
754
 
754
 
  /* We keep this pixgc allocated so we don't have to alloc/free it every
755
 
     time.  A widget has to be realized before it has a valid ->window so
756
 
     we do that here for battstat->applet just in case it's not already done.
757
 
  */
758
 
  if( battstat->pixgc == NULL )
759
 
  {
760
 
    gtk_widget_realize( battstat->applet );
761
 
    battstat->pixgc = gdk_gc_new( gtk_widget_get_window (battstat->applet) );
762
 
  }
763
 
 
764
755
  /* Depending on if the meter is horizontally oriented start out with the
765
756
     appropriate XPM image (from pixmaps.h)
766
757
  */
767
758
  if (battstat->horizont)
768
 
    pixmap = gdk_pixmap_create_from_xpm_d( gtk_widget_get_window (battstat->applet), &pixmask,
769
 
                                           NULL, battery_gray_xpm );
 
759
    pixbuf = gdk_pixbuf_new_from_xpm_data ((const char **) battery_gray_xpm);
770
760
  else
771
 
    pixmap = gdk_pixmap_create_from_xpm_d( gtk_widget_get_window (battstat->applet), &pixmask,
772
 
                                           NULL, battery_y_gray_xpm );
 
761
    pixbuf = gdk_pixbuf_new_from_xpm_data ((const char **) battery_y_gray_xpm);
773
762
 
774
763
  /* The core code responsible for painting the battery meter.  For each
775
764
     colour in our gradient array, draw a vertical or horizontal line
780
769
            
781
770
    for( i = 0; i < G_N_ELEMENTS( orange ); i++ )
782
771
    {
783
 
      gdk_gc_set_foreground (battstat->pixgc, &color[i]);
784
 
 
785
772
      if (battstat->horizont)
786
 
        gdk_draw_line (pixmap, battstat->pixgc, pixel_offset_top[i], i + 2,
 
773
        pixbuf_draw_line (pixbuf, &color[i], pixel_offset_top[i], i + 2,
787
774
                       pixel_offset_top[i] + progress_value, i + 2);
788
775
      else
789
 
        gdk_draw_line (pixmap, battstat->pixgc, i + 2, pixel_offset_top[i],
 
776
        pixbuf_draw_line (pixbuf, &color[i], i + 2, pixel_offset_top[i],
790
777
                       i + 2, pixel_offset_top[i] + progress_value);
791
778
    }
792
779
  }
796
783
 
797
784
    for( i = 0; i < G_N_ELEMENTS( orange ); i++)
798
785
    {
799
 
      gdk_gc_set_foreground (battstat->pixgc, &color[i]);
800
 
 
801
786
      if (battstat->horizont)
802
 
        gdk_draw_line (pixmap, battstat->pixgc, pixel_offset_bottom[i], i + 2,
803
 
                       pixel_offset_bottom[i] - progress_value, i + 2);
 
787
        pixbuf_draw_line (pixbuf, &color[i], pixel_offset_bottom[i], i + 2,
 
788
                          pixel_offset_bottom[i] - progress_value, i + 2);
804
789
      else
805
 
        gdk_draw_line (pixmap, battstat->pixgc, i + 2,
806
 
                       pixel_offset_bottom[i] - 1, i + 2,
807
 
                       pixel_offset_bottom[i] - progress_value);
 
790
        pixbuf_draw_line (pixbuf, &color[i], i + 2,
 
791
                          pixel_offset_bottom[i] - 1, i + 2,
 
792
                          pixel_offset_bottom[i] - progress_value);
808
793
    }
809
794
 
810
795
    for( i = 0; i < G_N_ELEMENTS( orange ); i++ )
815
800
 
816
801
      if (progress_value < 33)
817
802
      {
818
 
        gdk_gc_set_foreground (battstat->pixgc, &darkcolor[i]);
819
 
                     
820
803
        if (battstat->horizont)
821
 
          gdk_draw_line (pixmap, battstat->pixgc,
822
 
                         pixel_offset_bottom[i] - progress_value - 1,
823
 
                         i + 2, x, i + 2);
 
804
          pixbuf_draw_line (pixbuf, &darkcolor[i],
 
805
                            pixel_offset_bottom[i] - progress_value - 1,
 
806
                            i + 2, x, i + 2);
824
807
        else
825
 
          gdk_draw_line (pixmap, battstat->pixgc, i + 2,
826
 
                         pixel_offset_bottom[i] - progress_value - 1,
827
 
                         i + 2, x);
 
808
          pixbuf_draw_line (pixbuf, &darkcolor[i], i + 2,
 
809
                            pixel_offset_bottom[i] - progress_value - 1,
 
810
                            i + 2, x);
828
811
      }
829
812
    }
830
813
  }
832
815
  /* Store our newly created pixmap into the GtkImage.  This results in
833
816
     the last reference to the old pixmap/mask being dropped.
834
817
  */
835
 
  gtk_image_set_from_pixmap( GTK_IMAGE(battstat->battery),
836
 
                             pixmap, pixmask );
 
818
  gtk_image_set_from_pixbuf( GTK_IMAGE(battstat->battery),
 
819
                             pixbuf);
837
820
 
838
821
  /* The GtkImage does not assume a reference to the pixmap or mask;
839
822
     you still need to unref them if you own references. GtkImage will
840
823
     add its own reference rather than adopting yours.
841
824
  */
842
 
  g_object_unref( G_OBJECT(pixmap) );
843
 
  g_object_unref( G_OBJECT(pixmask) );
 
825
  g_object_unref( G_OBJECT(pixbuf) );
844
826
}
845
827
 
846
828
/* Update the text label that either shows the percentage of time left.
871
853
  g_free (new_label);
872
854
}
873
855
 
874
 
/* Utility function to create a copy of a GdkPixmap */
875
 
static GdkPixmap *
876
 
copy_gdk_pixmap( GdkPixmap *src, GdkGC *gc )
877
 
{
878
 
  gint height, width;
879
 
  GdkPixmap *dest;
880
 
 
881
 
  gdk_drawable_get_size( GDK_DRAWABLE( src ), &width, &height );
882
 
 
883
 
  dest = gdk_pixmap_new( GDK_DRAWABLE( src ), width, height, -1 );
884
 
 
885
 
  gdk_draw_drawable( GDK_DRAWABLE( dest ), gc, GDK_DRAWABLE( src ),
886
 
                     0, 0, 0, 0, width, height );
887
 
 
888
 
  return dest;
889
 
}
890
 
 
891
856
/* Determine what status icon we ought to be displaying and change the
892
857
   status icon to display it if it is different from what we are currently
893
858
   showing.
933
898
       battstat->last_pixmap_index != STATUS_PIXMAP_METER) )
934
899
  {
935
900
    GdkColor *colour;
936
 
    GdkPixmap *meter;
 
901
    GdkPixbuf *meter;
937
902
    guint progress_value;
938
903
    gint i, x;
939
904
 
940
 
    /* We keep this pixgc allocated so we don't have to alloc/free it every
941
 
       time.  A widget has to be realized before it has a valid ->window so
942
 
       we do that here for battstat->applet just in case it's not already done.
943
 
    */
944
 
    if( battstat->pixgc == NULL )
945
 
    {
946
 
      gtk_widget_realize( battstat->applet );
947
 
      battstat->pixgc = gdk_gc_new( gtk_widget_get_window (battstat->applet) );
948
 
    }
949
 
 
950
905
    /* Pull in a clean version of the icons so that we don't paint over
951
 
       top of the same icon over and over.  We neglect to free/update the
952
 
       statusmask here since it will always stay the same.
 
906
       top of the same icon over and over.
953
907
     */
954
 
    meter = copy_gdk_pixmap( statusimage[STATUS_PIXMAP_METER],
955
 
                             battstat->pixgc );
 
908
    meter = gdk_pixbuf_copy ( statusimage[STATUS_PIXMAP_METER]);
956
909
 
957
910
    if (batt_life <= battstat->red_val)
958
911
    {
975
928
    
976
929
    for( i = 0; i < 10; i++ )
977
930
    {
978
 
      gdk_gc_set_foreground( battstat->pixgc, &colour[(i * 13 / 10)] );
979
 
 
980
931
      if( i >= 2 && i <= 7 )
981
932
        x = 17;
982
933
      else
983
934
        x = 16;
984
935
 
985
 
      gdk_draw_line( meter, battstat->pixgc,
986
 
                     i + 1, x - progress_value,
987
 
                     i + 1, x );
 
936
      pixbuf_draw_line( meter, &colour[(i * 13 / 10)],
 
937
                        i + 1, x - progress_value,
 
938
                        i + 1, x );
988
939
    }
989
940
 
990
941
    /* force a redraw immediately */
991
 
    gtk_image_set_from_pixmap( GTK_IMAGE (battstat->status),
992
 
                               meter, statusmask[STATUS_PIXMAP_METER] );
 
942
    gtk_image_set_from_pixbuf( GTK_IMAGE (battstat->status),
 
943
                               meter);
993
944
 
994
945
    /* free our private pixmap copy */
995
946
    g_object_unref( G_OBJECT( meter ) );
997
948
  }
998
949
  else if( pixmap_index != battstat->last_pixmap_index )
999
950
  {
1000
 
    gtk_image_set_from_pixmap (GTK_IMAGE (battstat->status),
1001
 
                               statusimage[pixmap_index],
1002
 
                               statusmask[pixmap_index]);
 
951
    gtk_image_set_from_pixbuf (GTK_IMAGE (battstat->status),
 
952
                               statusimage[pixmap_index]);
1003
953
    battstat->last_pixmap_index = pixmap_index;
1004
954
  }
1005
955
}
1163
1113
  if (battstat->timeout_id)
1164
1114
    g_source_remove (battstat->timeout_id);
1165
1115
 
1166
 
  if( battstat->pixgc )
1167
 
    g_object_unref( G_OBJECT(battstat->pixgc) );
1168
 
 
1169
1116
  g_object_unref( G_OBJECT(battstat->status) );
1170
1117
  g_object_unref( G_OBJECT(battstat->percent) );
1171
1118
  g_object_unref( G_OBJECT(battstat->battery) );
1573
1520
  g_object_ref( battstat->status );
1574
1521
  g_object_ref( battstat->percent );
1575
1522
  g_object_ref( battstat->battery );
1576
 
  g_object_ref_sink( GTK_OBJECT( battstat->status ) );
1577
 
  g_object_ref_sink( GTK_OBJECT( battstat->percent ) );
1578
 
  g_object_ref_sink( GTK_OBJECT( battstat->battery ) );
 
1523
  g_object_ref_sink( G_OBJECT( battstat->status ) );
 
1524
  g_object_ref_sink( G_OBJECT( battstat->percent ) );
 
1525
  g_object_ref_sink( G_OBJECT( battstat->battery ) );
1579
1526
 
1580
1527
  /* Let reconfigure_layout know that the table is currently empty. */
1581
1528
  battstat->layout.status = LAYOUT_NONE;
1640
1587
  battstat->horizont = TRUE;
1641
1588
  battstat->battery_low_dialog = NULL;
1642
1589
  battstat->battery_low_label = NULL;
1643
 
  battstat->pixgc = NULL;
1644
1590
  battstat->timeout = -1;
1645
1591
  battstat->timeout_id = 0;
1646
1592
 
1702
1648
 
1703
1649
PANEL_APPLET_OUT_PROCESS_FACTORY ("BattstatAppletFactory",
1704
1650
                                  PANEL_TYPE_APPLET,
1705
 
                                  "battstat",
1706
1651
                                  battstat_applet_factory,
1707
1652
                                  NULL)
1708
1653