/*
 * GNOME lm_sensors panel applet
 * (C) 1999 The Free Software Foundation
 *
 * Author: Fabrice Bellet <Fabrice.Bellet@imag.fr>
 *         adapted from wmlm (by ben jarvis<bjarvis@bresnanlink.net>)
 *
 *
 */

#include <config.h>
#include <lm_sensors.h>

#include "wmlm_master.xpm"
#include "wmlm_mask.xbm"

#include "wmlm_master2.xpm"
#include "wmlm_mask2.xbm"

static void
properties_apply_cb (GtkWidget *b, gint page_num, gpointer data)
{
   LmSensorsApplet *sa = data;
   gboolean need_new_pixmaps = sa->display_temp_curves != sa->p_display_temp_curves;
   gboolean need_reset_history = sa->temp1_number != sa->p_temp1_number ||
   	sa->temp2_number != sa->p_temp2_number;
   gboolean need_reload_config = !sa->p_use_adaptive_limits && sa->use_adaptive_limits;

   sa->temp_unit = sa->p_temp_unit;
   sa->fan_number = sa->p_fan_number;
   sa->temp1_number = sa->p_temp1_number;
   sa->temp2_number = sa->p_temp2_number;
   sa->temp1_type = sa->p_temp1_type;
   sa->temp2_type = sa->p_temp2_type;
   sa->update_timeout = sa->p_update_timeout;
   sa->temp_curve_ll = sa->p_temp_curve_ll;
   sa->temp_curve_ul = sa->p_temp_curve_ul;
   sa->display_temp_curves = sa->p_display_temp_curves;

   sa->reset_adaptive_limits = (sa->p_use_adaptive_limits && !sa->use_adaptive_limits);
   /*
    * factory limits need to be reloaded when switching
    * to non-adaptive limits
    */ 
   if (need_reload_config) sensors_config (sa);
   /*
    * temperature history resetted when changing temp1 or temp2 input number
    */
   if (need_reset_history) {
   	sa->temp_history_nb=0;
   	sa->temp_history_idx=0;
   }
   sa->use_adaptive_limits = sa->p_use_adaptive_limits;

#ifdef DEBUG
   g_print ("properties_apply_cb:\n");
   g_print ("  temp unit           = %s\n",sa->temp_unit==CELCIUS?"C":(sa->temp_unit==FAHRENHEIT?"F":"K"));
   g_print ("  fan number          = %d\n",sa->fan_number);
   g_print ("  temp1 number        = %d\n",sa->temp1_number);
   g_print ("  temp2 number        = %d\n",sa->temp2_number);
   g_print ("  temp1 type          = %s\n",sa->temp1_type==MB_TEMP?"MB":"CPU");
   g_print ("  temp2 type          = %s\n",sa->temp2_type==MB_TEMP?"MB":"CPU");
   g_print ("  update timeout      = %d\n",sa->update_timeout/1000);
   g_print ("  temp curve llimit   = %2.1f\n",sa->temp_curve_ll);
   g_print ("  temp curve ulimit   = %2.1f\n",sa->temp_curve_ul);
   g_print ("  display temp curves = %s\n",sa->display_temp_curves?"yes":"no");
   g_print ("  use adaptive limits = %s\n",sa->use_adaptive_limits?"yes":"no");
   g_print ("\n\n");
#endif

   if (need_new_pixmaps)
	create_pixmap (sa);
   /*
    * install a new timeout
    */
   gtk_timeout_remove (sa->timeout_handler_id);
   sa->timeout_handler_id=gtk_timeout_add(sa->update_timeout,(GtkFunction)sensors_update,sa);

   applet_widget_sync_config (APPLET_WIDGET (sa->applet));
   update_pixmap (sa);
}

static gint
properties_destroy_cb (GtkWidget *widget, gpointer data)
{
  LmSensorsApplet *sa = data;
  sa->pbox = NULL;
  return FALSE;
}

static gint
temp_unit_selected_cb (GtkWidget *widget,gpointer data)
{
   LmSensorsApplet *sa = data;
   gdouble temp;
   gint old_temp_unit;

   old_temp_unit = sa->p_temp_unit;
   sa->p_temp_unit = GPOINTER_TO_INT (gtk_object_get_user_data (GTK_OBJECT (widget)));

   /*
    * update the spin button values, that express temperature 
    * values in "sa->temp_unit" unit.
    * sa->changing_unit is a dirty hack to avoid the "value_changed" 
    * callback to interfere with our modifications
    */
   sa->changing_unit = TRUE;
   temp = gtk_spin_button_get_value_as_float (GTK_SPIN_BUTTON (sa->temp_curve_ll_spin));
   temp = invert_temp_convert (temp, old_temp_unit);
   sa->temp_curve_ll_adj->value = temp_convert (temp, sa->p_temp_unit);
   sa->temp_curve_ll_adj->lower = temp_convert (-273.0, sa->p_temp_unit);
   sa->temp_curve_ll_adj->upper = temp_convert (100.0, sa->p_temp_unit);
   gtk_adjustment_changed (sa->temp_curve_ll_adj);
   gtk_adjustment_value_changed (sa->temp_curve_ll_adj);

   temp = gtk_spin_button_get_value_as_float (GTK_SPIN_BUTTON (sa->temp_curve_ul_spin));
   temp = invert_temp_convert (temp, old_temp_unit);
   sa->temp_curve_ul_adj->value = temp_convert (temp, sa->p_temp_unit);
   sa->temp_curve_ul_adj->lower = temp_convert (-273.0, sa->p_temp_unit);
   sa->temp_curve_ul_adj->upper = temp_convert (100.0, sa->p_temp_unit);
   gtk_adjustment_changed (sa->temp_curve_ul_adj);
   gtk_adjustment_value_changed (sa->temp_curve_ul_adj);
   sa->changing_unit = FALSE;

   gnome_property_box_changed(GNOME_PROPERTY_BOX(sa->pbox));
   return FALSE;
}

static gint
fan_selected_cb (GtkWidget *widget, gpointer data)
{
   LmSensorsApplet *sa = data;

   sa->p_fan_number = GPOINTER_TO_INT (gtk_object_get_user_data (GTK_OBJECT (widget)));
   gnome_property_box_changed(GNOME_PROPERTY_BOX(sa->pbox));
   return FALSE;
}

static gint
temp1_selected_cb (GtkWidget *widget, gpointer data)
{
   LmSensorsApplet *sa = data;

   sa->p_temp1_number = GPOINTER_TO_INT (gtk_object_get_user_data (GTK_OBJECT (widget)));
   gnome_property_box_changed(GNOME_PROPERTY_BOX(sa->pbox));
   return FALSE;
}

static gint
temp2_selected_cb (GtkWidget *widget, gpointer data)
{
   LmSensorsApplet *sa = data;

   sa->p_temp2_number = GPOINTER_TO_INT (gtk_object_get_user_data (GTK_OBJECT (widget)));
   gnome_property_box_changed(GNOME_PROPERTY_BOX(sa->pbox));
   return FALSE;
}

static gint
temp1_type_selected_cb (GtkWidget *widget, gpointer data)
{
   LmSensorsApplet *sa = data;

   sa->p_temp1_type = GPOINTER_TO_INT (gtk_object_get_user_data (GTK_OBJECT (widget)));
   gnome_property_box_changed(GNOME_PROPERTY_BOX(sa->pbox));
   return FALSE;
}

static gint
temp2_type_selected_cb (GtkWidget *widget, gpointer data)
{
   LmSensorsApplet *sa = data;

   sa->p_temp2_type = GPOINTER_TO_INT (gtk_object_get_user_data (GTK_OBJECT (widget)));
   gnome_property_box_changed(GNOME_PROPERTY_BOX(sa->pbox));
   return FALSE;
}

static gint
update_timeout_changed_cb (GtkWidget *widget, gpointer data)
{
   LmSensorsApplet *sa = data;

   sa->p_update_timeout = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (sa->timeout_spin))*1000;
   gnome_property_box_changed(GNOME_PROPERTY_BOX(sa->pbox));
   return FALSE;
}

static gint
display_temp_curves_selected_cb (GtkWidget *widget, gpointer data)
{
   LmSensorsApplet *sa = data;

   sa->p_display_temp_curves = GTK_TOGGLE_BUTTON (widget)->active;
   gnome_property_box_changed(GNOME_PROPERTY_BOX(sa->pbox));
   return FALSE;
}

static gint
use_adaptive_limits_selected_cb (GtkWidget *widget, gpointer data)
{
   LmSensorsApplet *sa = data;

   sa->p_use_adaptive_limits = GTK_TOGGLE_BUTTON (widget)->active;
   gnome_property_box_changed(GNOME_PROPERTY_BOX(sa->pbox));
   return FALSE;
}

static gint
temp_curve_ll_changed_cb (GtkWidget *widget, gpointer data)
{
   LmSensorsApplet *sa = data;
   gdouble new_temp;

   new_temp = gtk_spin_button_get_value_as_float (GTK_SPIN_BUTTON (sa->temp_curve_ll_spin));
   sa->p_temp_curve_ll = invert_temp_convert (new_temp, sa->p_temp_unit);

   if (!sa->changing_unit &&
   	sa->p_temp_curve_ul < sa->p_temp_curve_ll + 0.5)
   	gtk_spin_button_set_value (
   		GTK_SPIN_BUTTON (sa->temp_curve_ll_spin),
   		temp_convert (sa->p_temp_curve_ul - 0.5, sa->p_temp_unit));
   gnome_property_box_changed(GNOME_PROPERTY_BOX(sa->pbox));
   return FALSE;
}

static gint
temp_curve_ul_changed_cb (GtkWidget *widget, gpointer data)
{
   LmSensorsApplet *sa = data;
   gdouble new_temp;

   new_temp = gtk_spin_button_get_value_as_float (GTK_SPIN_BUTTON (sa->temp_curve_ul_spin));
   sa->p_temp_curve_ul = invert_temp_convert (new_temp, sa->p_temp_unit);

   if (!sa->changing_unit &&
   	sa->p_temp_curve_ul < sa->p_temp_curve_ll + 0.5)
   	gtk_spin_button_set_value (
   		GTK_SPIN_BUTTON (sa->temp_curve_ul_spin), 
   		temp_convert (sa->p_temp_curve_ll + 0.5, sa->p_temp_unit));
   gnome_property_box_changed(GNOME_PROPERTY_BOX(sa->pbox));
   return FALSE;
}

static void
properties_cb (AppletWidget *applet, gpointer data)
{
   LmSensorsApplet *sa = data;

   GtkWidget *label;
   GtkWidget *opts;
   GtkWidget *frame;
   GtkWidget *frame2;
   GtkWidget *option;
   GtkWidget *option_menu;
   GtkWidget *menu_item;
   GtkWidget *hbox;
   GtkAdjustment *adj;

   if (sa->pbox) {
   	gdk_window_raise (sa->pbox->window);
   	return;
   }

   sa->p_temp_unit = sa->temp_unit;
   sa->p_fan_number = sa->fan_number;
   sa->p_temp1_number = sa->temp1_number;
   sa->p_temp2_number = sa->temp2_number;
   sa->p_temp1_type = sa->temp1_type;
   sa->p_temp2_type = sa->temp2_type;
   sa->p_update_timeout = sa->update_timeout;
   sa->p_display_temp_curves = sa->display_temp_curves;
   sa->p_temp_curve_ll = sa->temp_curve_ll;
   sa->p_temp_curve_ul = sa->temp_curve_ul;
   sa->p_use_adaptive_limits = sa->use_adaptive_limits;

#ifdef DEBUG
   g_print ("properties_cb:\n");
   g_print ("  temp unit           = %s\n",sa->temp_unit==CELCIUS?"C":(sa->temp_unit==FAHRENHEIT?"F":"K"));
   g_print ("  fan number          = %d\n",sa->fan_number);
   g_print ("  temp1 number        = %d\n",sa->temp1_number);
   g_print ("  temp2 number        = %d\n",sa->temp2_number);
   g_print ("  temp1 type          = %s\n",sa->temp1_type==MB_TEMP?"MB":"CPU");
   g_print ("  temp2 type          = %s\n",sa->temp2_type==MB_TEMP?"MB":"CPU");
   g_print ("  update timeout      = %d\n",sa->update_timeout/1000);
   g_print ("  temp curve llimit   = %2.1f\n",sa->temp_curve_ll);
   g_print ("  temp curve ulimit   = %2.1f\n",sa->temp_curve_ul);
   g_print ("  display temp curves = %s\n",sa->display_temp_curves?"yes":"no");
   g_print ("  use adaptive limits = %s\n",sa->use_adaptive_limits?"yes":"no");
   g_print ("\n\n");
#endif

   sa->pbox=gnome_property_box_new();
   gtk_window_set_title (
        GTK_WINDOW(&GNOME_PROPERTY_BOX(sa->pbox)->dialog.window),
        _("LM Sensors setting"));
   frame = gtk_vbox_new(5, TRUE);
   opts = gtk_vbox_new(5, TRUE);
   /*
    * menu widget for the temp unit
    */
   frame2 = gtk_frame_new ( _("Temp unit") );
   gtk_box_pack_start (GTK_BOX (opts), frame2, FALSE, FALSE, 0);
   gtk_container_set_border_width (GTK_CONTAINER (frame2), 8);

   hbox = gtk_hbox_new (FALSE, GNOME_PAD_SMALL);
   gtk_container_add (GTK_CONTAINER (frame2), hbox);

   option = gtk_option_menu_new ();
   gtk_box_pack_start (GTK_BOX (hbox), option, TRUE, TRUE, 0);
   GTK_WIDGET_SET_FLAGS (option, GTK_CAN_DEFAULT);

   option_menu = gtk_menu_new ();
   menu_item = gtk_menu_item_new_with_label ( _("Celcius"));
   gtk_object_set_user_data (GTK_OBJECT (menu_item),GINT_TO_POINTER(CELCIUS));
   gtk_menu_append (GTK_MENU (option_menu), menu_item);
   gtk_signal_connect (GTK_OBJECT (menu_item), "activate",
   	GTK_SIGNAL_FUNC (temp_unit_selected_cb),(gpointer)sa);
   menu_item = gtk_menu_item_new_with_label ( _("Fahrenheit"));
   gtk_object_set_user_data (GTK_OBJECT (menu_item),GINT_TO_POINTER(FAHRENHEIT));
   gtk_menu_append (GTK_MENU (option_menu), menu_item);
   gtk_signal_connect (GTK_OBJECT (menu_item), "activate",
   	GTK_SIGNAL_FUNC (temp_unit_selected_cb),(gpointer)sa);
   menu_item = gtk_menu_item_new_with_label ( _("Kelvin"));
   gtk_object_set_user_data (GTK_OBJECT (menu_item),GINT_TO_POINTER(KELVIN));
   gtk_menu_append (GTK_MENU (option_menu), menu_item);
   gtk_signal_connect (GTK_OBJECT (menu_item), "activate",
   	GTK_SIGNAL_FUNC (temp_unit_selected_cb),(gpointer)sa);
   gtk_menu_set_active(GTK_MENU (option_menu),0);
   gtk_option_menu_set_menu (GTK_OPTION_MENU (option),option_menu);

   /*
    * menu widget for fan number
    */
   if (sa->chip.fan_number>0) {
   	frame2 = gtk_frame_new (_("Fan selector"));
   	gtk_box_pack_start (GTK_BOX (opts), frame2, TRUE, TRUE, 0);
   	gtk_container_set_border_width (GTK_CONTAINER (frame2), 8);

   	hbox = gtk_hbox_new (FALSE, GNOME_PAD_SMALL);
   	gtk_container_add (GTK_CONTAINER (frame2), hbox);
	
   	option = gtk_option_menu_new ();
   	gtk_box_pack_start (GTK_BOX (hbox), option, TRUE, TRUE, 0);
   	GTK_WIDGET_SET_FLAGS (option, GTK_CAN_DEFAULT);
	
   	option_menu = gtk_menu_new ();
   	menu_item = gtk_menu_item_new_with_label ( _("fan1"));
   	gtk_object_set_user_data (GTK_OBJECT (menu_item),GINT_TO_POINTER(0));
   	gtk_menu_append (GTK_MENU (option_menu), menu_item);
   	gtk_signal_connect (GTK_OBJECT (menu_item), "activate",
   		GTK_SIGNAL_FUNC (fan_selected_cb),(gpointer)sa);
   	if (sa->chip.fan_number > 1) {
   		menu_item = gtk_menu_item_new_with_label ( _("fan2"));
   		gtk_object_set_user_data (GTK_OBJECT (menu_item),GINT_TO_POINTER(1));
   		gtk_menu_append (GTK_MENU (option_menu), menu_item);
   		gtk_signal_connect (GTK_OBJECT (menu_item), "activate",
   			GTK_SIGNAL_FUNC (fan_selected_cb),(gpointer)sa);
   	}
   	if (sa->chip.fan_number > 2) {
   		menu_item= gtk_menu_item_new_with_label ( _("fan3"));
   		gtk_object_set_user_data (GTK_OBJECT (menu_item),GINT_TO_POINTER(2));
   		gtk_menu_append (GTK_MENU (option_menu), menu_item);
   		gtk_signal_connect (GTK_OBJECT (menu_item), "activate",
   			GTK_SIGNAL_FUNC (fan_selected_cb),(gpointer)sa);
   	}
   	gtk_menu_set_active(GTK_MENU (option_menu),sa->p_fan_number);
   	gtk_option_menu_set_menu (GTK_OPTION_MENU (option),option_menu);
   }
   

   /*
    * menu widget for temp1 number
    */
   if (sa->chip.fan_number>0) {
   	frame2 = gtk_frame_new (_("Temperature1 selector"));
   	gtk_box_pack_start (GTK_BOX (opts), frame2, FALSE, FALSE, 0);
   	gtk_container_set_border_width (GTK_CONTAINER (frame2), 8);
	
   	hbox = gtk_hbox_new (FALSE, GNOME_PAD_SMALL);
   	gtk_container_add (GTK_CONTAINER (frame2), hbox);

   	option = gtk_option_menu_new ();
   	gtk_box_pack_start (GTK_BOX (hbox), option, TRUE, TRUE, 0);
   	GTK_WIDGET_SET_FLAGS (option, GTK_CAN_DEFAULT);
	
   	option_menu = gtk_menu_new ();
   	menu_item = gtk_menu_item_new_with_label ( _("temp1"));
   	gtk_object_set_user_data (GTK_OBJECT (menu_item),GINT_TO_POINTER(0));
   	gtk_menu_append (GTK_MENU (option_menu), menu_item);
   	gtk_signal_connect (GTK_OBJECT (menu_item), "activate",
   		GTK_SIGNAL_FUNC (temp1_selected_cb),(gpointer)sa);
   	if (sa->chip.fan_number > 1) {
   		menu_item = gtk_menu_item_new_with_label ( _("temp2"));
   		gtk_object_set_user_data (GTK_OBJECT (menu_item),GINT_TO_POINTER(1));
   		gtk_menu_append (GTK_MENU (option_menu), menu_item);
   		gtk_signal_connect (GTK_OBJECT (menu_item), "activate",
   			GTK_SIGNAL_FUNC (temp1_selected_cb),(gpointer)sa);
   	}
   	if (sa->chip.fan_number > 2) {
   		menu_item= gtk_menu_item_new_with_label ( _("temp3"));
   		gtk_object_set_user_data (GTK_OBJECT (menu_item),GINT_TO_POINTER(2));
   		gtk_menu_append (GTK_MENU (option_menu), menu_item);
   		gtk_signal_connect (GTK_OBJECT (menu_item), "activate",
   			GTK_SIGNAL_FUNC (temp1_selected_cb),(gpointer)sa);
   	}
   	gtk_menu_set_active(GTK_MENU (option_menu),sa->p_temp1_number);
   	gtk_option_menu_set_menu (GTK_OPTION_MENU (option),option_menu);

   	/***/

   	option = gtk_option_menu_new ();
   	gtk_box_pack_start (GTK_BOX (hbox), option, TRUE, TRUE, 0);
   	GTK_WIDGET_SET_FLAGS (option, GTK_CAN_DEFAULT);

   	option_menu = gtk_menu_new ();
   	menu_item = gtk_menu_item_new_with_label ( _("MotherBoard Temp"));
   	gtk_object_set_user_data (GTK_OBJECT (menu_item),GINT_TO_POINTER(MB_TEMP));
   	gtk_menu_append (GTK_MENU (option_menu), menu_item);
   	gtk_signal_connect (GTK_OBJECT (menu_item), "activate",
   		GTK_SIGNAL_FUNC (temp1_type_selected_cb),(gpointer)sa);
   	menu_item = gtk_menu_item_new_with_label ( _("CPU Temp"));
   	gtk_object_set_user_data (GTK_OBJECT (menu_item),GINT_TO_POINTER(CPU_TEMP));
   	gtk_menu_append (GTK_MENU (option_menu), menu_item);
   	gtk_signal_connect (GTK_OBJECT (menu_item), "activate",
   		GTK_SIGNAL_FUNC (temp1_type_selected_cb),(gpointer)sa);
   	gtk_menu_set_active(GTK_MENU (option_menu),(sa->p_temp1_type==MB_TEMP?0:1));
   	gtk_option_menu_set_menu (GTK_OPTION_MENU (option),option_menu);
   }
   

   /*
    * menu widget for temp2 number
    */
   if (sa->chip.fan_number>0) {
   	frame2 = gtk_frame_new (_("Temperature2 selector"));
   	gtk_box_pack_start (GTK_BOX (opts), frame2, FALSE, FALSE, 0);
   	gtk_container_set_border_width (GTK_CONTAINER (frame2), 8);

   	hbox = gtk_hbox_new(FALSE, GNOME_PAD_SMALL);
   	gtk_container_add (GTK_CONTAINER (frame2), hbox);

   	option = gtk_option_menu_new ();
   	gtk_box_pack_start (GTK_BOX (hbox), option, TRUE, TRUE, 0);
   	GTK_WIDGET_SET_FLAGS (option, GTK_CAN_DEFAULT);
	
   	option_menu = gtk_menu_new ();
   	menu_item = gtk_menu_item_new_with_label ( _("temp1"));
   	gtk_object_set_user_data (GTK_OBJECT (menu_item),GINT_TO_POINTER(0));
   	gtk_menu_append (GTK_MENU (option_menu), menu_item);
   	gtk_signal_connect (GTK_OBJECT (menu_item), "activate",
   		GTK_SIGNAL_FUNC (temp2_selected_cb),(gpointer)sa);
   	if (sa->chip.fan_number > 1) {
   		menu_item = gtk_menu_item_new_with_label ( _("temp2"));
   		gtk_object_set_user_data (GTK_OBJECT (menu_item),GINT_TO_POINTER(1));
   		gtk_menu_append (GTK_MENU (option_menu), menu_item);
   		gtk_signal_connect (GTK_OBJECT (menu_item), "activate",
   			GTK_SIGNAL_FUNC (temp2_selected_cb),(gpointer)sa);
   	}
   	if (sa->chip.fan_number > 2) {
   		menu_item= gtk_menu_item_new_with_label ( _("temp3"));
   		gtk_object_set_user_data (GTK_OBJECT (menu_item),GINT_TO_POINTER(2));
   		gtk_menu_append (GTK_MENU (option_menu), menu_item);
   		gtk_signal_connect (GTK_OBJECT (menu_item), "activate",
   			GTK_SIGNAL_FUNC (temp2_selected_cb),(gpointer)sa);
   	}
   	gtk_menu_set_active(GTK_MENU (option_menu),sa->p_temp2_number);
   	gtk_option_menu_set_menu (GTK_OPTION_MENU (option),option_menu);

   	/***/

   	option = gtk_option_menu_new ();
   	gtk_box_pack_start (GTK_BOX (hbox), option, TRUE, TRUE, 0);
   	GTK_WIDGET_SET_FLAGS (option, GTK_CAN_DEFAULT);

   	option_menu = gtk_menu_new ();
   	menu_item = gtk_menu_item_new_with_label ( _("MotherBoard Temp"));
   	gtk_object_set_user_data (GTK_OBJECT (menu_item),GINT_TO_POINTER(MB_TEMP));
   	gtk_menu_append (GTK_MENU (option_menu), menu_item);
   	gtk_signal_connect (GTK_OBJECT (menu_item), "activate",
   		GTK_SIGNAL_FUNC (temp2_type_selected_cb),(gpointer)sa);
   	menu_item = gtk_menu_item_new_with_label ( _("CPU Temp"));
   	gtk_object_set_user_data (GTK_OBJECT (menu_item),GINT_TO_POINTER(CPU_TEMP));
   	gtk_menu_append (GTK_MENU (option_menu), menu_item);
   	gtk_signal_connect (GTK_OBJECT (menu_item), "activate",
   		GTK_SIGNAL_FUNC (temp2_type_selected_cb),(gpointer)sa);
   	gtk_menu_set_active(GTK_MENU (option_menu),(sa->p_temp2_type==MB_TEMP?0:1));
   	gtk_option_menu_set_menu (GTK_OPTION_MENU (option),option_menu);
   }
   

   /*
    * spin button widget to update timeout
    */
   frame2 = gtk_frame_new (_("Update delay (seconds)"));
   gtk_box_pack_start (GTK_BOX (opts), frame2, TRUE, TRUE, 0);
   gtk_container_set_border_width (GTK_CONTAINER (frame2), 8);

   adj = (GtkAdjustment *) gtk_adjustment_new (((gdouble)sa->p_update_timeout)/1000.,
   	1.0, 999.0, 1.0, 10.0, 0.0);
   sa->timeout_spin = gtk_spin_button_new (adj,1.0,0);
   gtk_spin_button_set_wrap (GTK_SPIN_BUTTON (sa->timeout_spin),TRUE);
   gtk_spin_button_set_shadow_type (GTK_SPIN_BUTTON (sa->timeout_spin), GTK_SHADOW_IN);
   gtk_container_add (GTK_CONTAINER (frame2), sa->timeout_spin);
   gtk_signal_connect (GTK_OBJECT (adj),"value_changed",
   	GTK_SIGNAL_FUNC (update_timeout_changed_cb),sa);

   /*
    * Display temperature curves
    */
   label = gtk_check_button_new_with_label (_("Display temperature curves"));
   gtk_box_pack_start (GTK_BOX(opts), label, FALSE, FALSE, 0);
   gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(label),sa->p_display_temp_curves);
   gtk_signal_connect (GTK_OBJECT(label), "clicked",
   	GTK_SIGNAL_FUNC (display_temp_curves_selected_cb), sa);
   /*
    * Spin buttons widgets to update temp limits on graphic
    */
   frame2 = gtk_frame_new (_("Temperature limits on display"));
   gtk_box_pack_start (GTK_BOX (opts), frame2, TRUE, TRUE, 0);
   gtk_container_set_border_width (GTK_CONTAINER (frame2), 8);

   hbox = gtk_hbox_new(FALSE, GNOME_PAD_SMALL);
   gtk_container_add (GTK_CONTAINER (frame2), hbox);

   label = gtk_label_new (_("lower limit:"));
   gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
   sa->temp_curve_ll_adj = (GtkAdjustment *) gtk_adjustment_new (
   	temp_convert (sa->p_temp_curve_ll, sa->p_temp_unit),
   	temp_convert (-273.0, sa->p_temp_unit),
   	temp_convert (100.0, sa->p_temp_unit), 
   	0.5, 5.0, 0.0);
   sa->temp_curve_ll_spin = gtk_spin_button_new (sa->temp_curve_ll_adj, 1.0, 1);
   gtk_spin_button_set_wrap (GTK_SPIN_BUTTON (sa->temp_curve_ll_spin), GTK_SHADOW_IN);
   gtk_box_pack_start (GTK_BOX (hbox), sa->temp_curve_ll_spin, TRUE, TRUE, 0);
   gtk_signal_connect (GTK_OBJECT (sa->temp_curve_ll_adj), "value_changed",
   	GTK_SIGNAL_FUNC (temp_curve_ll_changed_cb), sa);

   label = gtk_label_new (_("upper limit:"));
   gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
   sa->temp_curve_ul_adj = (GtkAdjustment *) gtk_adjustment_new (
   	temp_convert (sa->p_temp_curve_ul, sa->p_temp_unit),
   	temp_convert (-273.0, sa->p_temp_unit),
   	temp_convert (100.0, sa->p_temp_unit), 
   	0.5, 5.0, 0.0);
   sa->temp_curve_ul_spin = gtk_spin_button_new (sa->temp_curve_ul_adj, 1.0, 1);
   gtk_spin_button_set_wrap (GTK_SPIN_BUTTON (sa->temp_curve_ul_spin), GTK_SHADOW_IN);
   gtk_box_pack_start (GTK_BOX (hbox), sa->temp_curve_ul_spin, TRUE, TRUE, 0);
   gtk_signal_connect (GTK_OBJECT (sa->temp_curve_ul_adj), "value_changed",
   	GTK_SIGNAL_FUNC (temp_curve_ul_changed_cb), sa);
   /*
    * Use adaptive limits
    */
   label = gtk_check_button_new_with_label (_("Use adaptives min/max"));
   gtk_box_pack_start(GTK_BOX(opts), label, FALSE, FALSE, 0);
   gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(label),sa->p_use_adaptive_limits);
   gtk_signal_connect (GTK_OBJECT(label), "clicked",
   	GTK_SIGNAL_FUNC (use_adaptive_limits_selected_cb), sa);

   /*
    *
    */
   gtk_widget_show(opts);
   gtk_box_pack_start(GTK_BOX(frame), opts, TRUE, TRUE, 5);
   gtk_widget_show(frame);

   label = gtk_label_new(_("General"));
   gnome_property_box_append_page(GNOME_PROPERTY_BOX(sa->pbox),frame,label);

   gtk_signal_connect (GTK_OBJECT (sa->pbox),
   	"apply", GTK_SIGNAL_FUNC (properties_apply_cb), sa);
   gtk_signal_connect (GTK_OBJECT (sa->pbox),
   	"destroy", GTK_SIGNAL_FUNC (properties_destroy_cb), sa);

   gtk_widget_show_all(sa->pbox);
}

static void
about_cb(AppletWidget *applet,gpointer data)
{
   GtkWidget *about;

   const gchar *authors[] = {
	"Fabrice Bellet <Fabrice.Bellet@imag.fr>",
	"Ben Jarvis <bjarvis@bresnanlink.net>",NULL};

   about=gnome_about_new (_("LM Sensors"),
   	LM_VERSION,
   	_("(C) 1999 The Free Software Foundation"),
   	authors,
   	_("a GNOME applet to display motherboard info "
   	"(temperature, fan speed, voltage). lm_sensors "
   	"is required."),
   	NULL);
   gtk_widget_show (about);
   return;
}

static gint
darea_expose_cb (GtkWidget *widget, GdkEventExpose *event, gpointer data)
{
   LmSensorsApplet *sa = data;
#ifdef DEBUG
   g_print ("darea_export event\n");
#endif
   update_pixmap (sa);
   return FALSE;
}

inline void
copy_xpm_area (gint x, gint y,gint dx, gint dy, gint sx, gint sy, LmSensorsApplet *sa)
{
   gdk_draw_pixmap (sa->darea->window,sa->darea->style->fg_gc[GTK_WIDGET_STATE(sa->darea)],
   	sa->lm_master,x,y,dx,dy,sx,sy);
}


static void
draw_temp_labels (LmSensorsApplet *sa)
{
   /*
    * reset widget area :
    *   line 1 : CPU in Celcius degrees
    *   line 2 : MB  in Celcius degrees
    */
   copy_xpm_area (0, 0, 0, 0, 64, 64, sa);
   /*
    * Update temp units
    */
   if (sa->temp_unit==FAHRENHEIT) {
   	copy_xpm_area (60, 64, 52,  6, 5, 7, sa);
   	copy_xpm_area (60, 64, 52, 20, 5, 7, sa);
   } else if (sa->temp_unit==KELVIN) {
   	copy_xpm_area (70, 64, 52,  6, 5, 7, sa);
   	copy_xpm_area (70, 64, 52, 20, 5, 7, sa);
   }
   /*
    * Update temp labels
    */
   if (sa->temp1_type == MB_TEMP)
   	copy_xpm_area (6, 20, 6, 6, 17, 7, sa);
   if (sa->temp2_type == CPU_TEMP)
   	copy_xpm_area (6, 6, 6, 20, 17, 7, sa);
}

gdouble
temp_convert (gdouble temp, gint temp_unit)
{
   if (temp_unit == FAHRENHEIT)
   	return temp*1.8 + 32;
   else if (temp_unit == KELVIN)
   	return temp + 273;
   else /* CELCIUS */
   	return temp;
}

gdouble
invert_temp_convert (gdouble temp, gint temp_unit)
{
   if (temp_unit == FAHRENHEIT)
   	return (temp - 32)/1.8;
   else if (temp_unit == KELVIN)
   	return temp - 273;
   else
   	return temp;
}

static void
draw_temp (gdouble temp, gint x, gint y, LmSensorsApplet *sa)
{
   gint c;
   gint digit;

   c = (gint)temp_convert (temp, sa->temp_unit);

   /* 
    * clear area first
    */
   copy_xpm_area (70, 54, x+12, y, 6, 9, sa);
   copy_xpm_area (70, 54, x+6, y, 6, 9, sa);
   copy_xpm_area (70, 54, x, y, 6, 9, sa);

   if (c>0) {
	digit=c%10;
	copy_xpm_area (digit*6, 64, x+12, y, 6, 9, sa);
	if (c>9) {
	   digit=((c%100)-digit)/10;
	   copy_xpm_area (digit*6, 64, x+6, y, 6, 9, sa);
	   if (c>9) {
	      digit=(c-(c%100))/100;
	      copy_xpm_area (digit*6, 64, x, y, 6, 9, sa);
	   }
	}
   }
}

static void
draw_voltage (gdouble in, gdouble in_ll, gdouble in_ul, gint x, gint sy, LmSensorsApplet *sa)
{
   gint c = ((int)(((1-(in-in_ll)/(in_ul-in_ll)))*sy));
   if (c>sy || c<0)
	copy_xpm_area (71, 26, x, 33, 3, sy, sa);
   else {
	/*
	 * Clear area first
	 */
       	copy_xpm_area (68, 26, x, 33, 3, sy, sa);
	copy_xpm_area (67, 64, x, 33+c, 3, 1, sa);
   }
}

static void
draw_fan_meter (gdouble fan, gdouble fan_ll, gdouble fan_ul, gint x, gint sy, LmSensorsApplet *sa)
{
   gint c = (int)(((fan - fan_ll)/(fan_ul-fan_ll))*sy);
   /*
    * clear area first
    */
   copy_xpm_area (71,0,x,33,7,sy,sa);
   copy_xpm_area (64,sy-c,x,33+(sy-c),7,c,sa);
}

static void
draw_temp_curves (gint x0, gint y0, gint width, gint height,
	gdouble t_ll, gdouble t_ul, LmSensorsApplet *sa)
{
   gint i,j;
   gint i0;
   gdouble y;
   /*
    * Clear area first
    */
   copy_xpm_area (x0, y0, x0, y0, width, height, sa);
   /*
    * Draw a grid
    */
   for (i=0; i<width; i+=10)
   	for (j=0; j<height; j++)
   		copy_xpm_area (72, 55, x0+i, y0+j, 1, 1, sa);
   for (y=5.*(int)(t_ll/5.); y<t_ul; y+=5.) {
   	j = ((t_ul-y)/(t_ul-t_ll))*height;
   	if (j>=0 && j<height)
   		for (i=0; i<width; i++)
   			copy_xpm_area (72, 55, x0+i, y0+j, 1, 1, sa);
   }
   /*
    * shorten history if not enough measures
    */
   if (width > sa->temp_history_nb) {
   	x0 += (width - sa->temp_history_nb);
   	width = sa->temp_history_nb;
   }

   i0=(sa->temp_history_idx+TEMP_HISTORY_SIZE-width)%TEMP_HISTORY_SIZE;
   for (i=0;i<width;i++) {
   	/* plots first temp in green */
   	j=((t_ul-sa->temp_history[0][i0])/(t_ul-t_ll))*height;
   	if (j>=0 && j<height)
		copy_xpm_area (67, 64, x0+i, y0+j, 1, 1, sa);

   	/* plots second temp in yellow */
	j=((sa->temp_history[1][i0]-t_ul)/(t_ll-t_ul))*height;
	if (j>=0 && j<height)
		copy_xpm_area (67, 65, x0+i, y0+j, 1, 1, sa);

	if ((++i0)==TEMP_HISTORY_SIZE) i0=0;
   }
}

#define TTIP_SIZE 1024

static void
update_adaptive_limits (LmSensorsApplet *sa)
{
   gint i;

   /*
    * update voltage upper/lower limits
    */
   if (sa->use_adaptive_limits) {
   	if (sa->reset_adaptive_limits) {
   		for (i=0; i < 7; i++) {
   			sa->in_ll[i] = sa->in[i] - EPSILON;
   			sa->in_ul[i] = sa->in[i] + EPSILON;
   		}
   		for (i=0; i < sa->chip.temp_number; i++) {
   			sa->temp_ll[i] = sa->temp[i] - EPSILON;
   			sa->temp_ul[i] = sa->temp[i] + EPSILON;
   		}
   		for (i=0; i < sa->chip.fan_number; i++) {
   			sa->fan_ll[i] = sa->fan[i] - EPSILON;
   			sa->fan_ul[i] = sa->fan[i] + EPSILON;
   		}
   		sa->reset_adaptive_limits = FALSE;
   	} else {
   		for (i=0; i < 7; i++) {
   			if (sa->in[i] < sa->in_ll[i]) sa->in_ll[i] = sa->in[i];
   			if (sa->in[i] > sa->in_ul[i]) sa->in_ul[i] = sa->in[i];
   		}
   		for (i=0; i < sa->chip.temp_number; i++) {
   			if (sa->temp[i] < sa->temp_ll[i]) sa->temp_ll[i] = sa->temp[i];
   			if (sa->temp[i] > sa->temp_ul[i]) sa->temp_ul[i] = sa->temp[i];
   		}
   		for (i=0; i < sa->chip.fan_number; i++) {
   			if (sa->fan[i] < sa->fan_ll[i]) sa->fan_ll[i] = sa->fan[i];
   			if (sa->fan[i] > sa->fan_ul[i]) sa->fan_ul[i] = sa->fan[i];
   		}
   	}
   }
   /*
    * update fan meter upper limit
    */
   if (sa->fan[sa->fan_number] > sa->fan_ul[sa->fan_number])
   	sa->fan_ul[sa->fan_number] = sa->fan[sa->fan_number];

}

static void
update_history (LmSensorsApplet *sa)
{
   gdouble t1,t2;
   gint i;

   i=sa->temp_history_idx;
   t1=sa->temp [sa->temp1_number];
   t2=sa->temp [sa->temp2_number];
   sa->temp_history[0][i]=t1;
   sa->temp_history[1][i++]=t2;
   if (i==TEMP_HISTORY_SIZE) i=0;
   sa->temp_history_idx=i;
   if (sa->temp_history_nb<TEMP_HISTORY_SIZE)
   	sa->temp_history_nb++;
}

void
update_pixmap (LmSensorsApplet *sa)
{
   gchar *ttip;
   gint i,j;

   /*
    * Draw temp labels + units
    */
   draw_temp_labels (sa);
   /*
    * Draw temps digits
    */
   draw_temp (sa->temp [sa->temp1_number],30,5,sa);
   draw_temp (sa->temp [sa->temp2_number],30,19,sa);

   if (sa->display_temp_curves)
   	/*
   	 * Draw temps curves
   	 */
   	draw_temp_curves (5,33,TEMP_HISTORY_SIZE,26,sa->temp_curve_ll,sa->temp_curve_ul,sa);
   else
   	/*
   	 * voltages
   	 */
   	for (i=0,j=5;i<7;i++,j+=6)
   		draw_voltage (sa->in[i], sa->in_ll[i], sa->in_ul[i], j, 26, sa);

   /*
    * fan meter
    */
   draw_fan_meter (sa->fan[sa->fan_number],
   	sa->fan_ll[sa->fan_number],
   	sa->fan_ul[sa->fan_number], 
   	52, 26, sa);

   /*
    * tooltip
    */
   ttip = g_malloc0 (TTIP_SIZE);
   g_snprintf (ttip, TTIP_SIZE, 
   	"in0: %2.1f in (%2.1f,%2.1f)\n"
   	"in1: %2.1f in (%2.1f,%2.1f)\n"
   	"in2: %2.1f in (%2.1f,%2.1f)\n"
   	"in3: %2.1f in (%2.1f,%2.1f)\n"
   	"in4: %2.1f in (%2.1f,%2.1f)\n"
   	"in5: %2.1f in (%2.1f,%2.1f)\n"
   	"in6: %2.1f in (%2.1f,%2.1f)\n",
   	sa->in[0], sa->in_ll[0], sa->in_ul[0],
   	sa->in[1], sa->in_ll[1], sa->in_ul[1],
   	sa->in[2], sa->in_ll[2], sa->in_ul[2],
   	sa->in[3], sa->in_ll[3], sa->in_ul[3],
   	sa->in[4], sa->in_ll[4], sa->in_ul[4],
   	sa->in[5], sa->in_ll[5], sa->in_ul[5],
   	sa->in[6], sa->in_ll[6], sa->in_ul[6]);
   applet_widget_set_tooltip (APPLET_WIDGET (sa->applet),ttip);
   g_free (ttip);
}

gint
sensors_update (LmSensorsApplet *sa)
{
   if (!sa->darea ||
   	!GTK_WIDGET_REALIZED(sa->darea) ||
   	!GTK_WIDGET_DRAWABLE(sa->darea))
   		return TRUE;

   switch (sa->chip.chiptype) {
	case LM78 :
		sensors_get_feature(*sa->chip.name,SENSORS_LM78_IN0, &sa->in[0]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78_IN1, &sa->in[1]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78_IN2, &sa->in[2]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78_IN3, &sa->in[3]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78_IN4, &sa->in[4]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78_IN5, &sa->in[5]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78_IN6, &sa->in[6]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78_TEMP, &sa->temp[0]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78_FAN1, &sa->fan[1]);
      		sensors_get_feature(*sa->chip.name,SENSORS_LM78_FAN2, &sa->fan[2]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78_FAN3, &sa->fan[3]);
		break;
      case LM78J :
		sensors_get_feature(*sa->chip.name,SENSORS_LM78J_IN0, &sa->in[0]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78J_IN1, &sa->in[1]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78J_IN2, &sa->in[2]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78J_IN3, &sa->in[3]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78J_IN4, &sa->in[4]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78J_IN5, &sa->in[5]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78J_IN6, &sa->in[6]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78J_TEMP, &sa->temp[0]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78J_FAN1, &sa->fan[1]);
      		sensors_get_feature(*sa->chip.name,SENSORS_LM78J_FAN2, &sa->fan[2]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78J_FAN3, &sa->fan[3]);
      		break;
      case LM79 :
		sensors_get_feature(*sa->chip.name,SENSORS_LM79_IN0, &sa->in[0]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM79_IN1, &sa->in[1]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM79_IN2, &sa->in[2]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM79_IN3, &sa->in[3]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM79_IN4, &sa->in[4]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM79_IN5, &sa->in[5]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM79_IN6, &sa->in[6]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM79_TEMP, &sa->temp[0]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM79_FAN1, &sa->fan[0]);
      		sensors_get_feature(*sa->chip.name,SENSORS_LM79_FAN2, &sa->fan[1]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM79_FAN3, &sa->fan[2]);
      		break;
      case LM80 :
		sensors_get_feature(*sa->chip.name,SENSORS_LM80_IN0, &sa->in[0]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM80_IN1, &sa->in[1]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM80_IN2, &sa->in[2]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM80_IN3, &sa->in[3]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM80_IN4, &sa->in[4]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM80_IN5, &sa->in[5]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM80_IN6, &sa->in[6]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM80_TEMP, &sa->temp[0]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM80_FAN1, &sa->fan[0]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM80_FAN2, &sa->fan[1]);
      case W83781D :
		sensors_get_feature(*sa->chip.name,SENSORS_W83781D_IN0, &sa->in[0]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83781D_IN1, &sa->in[1]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83781D_IN2, &sa->in[2]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83781D_IN3, &sa->in[3]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83781D_IN4, &sa->in[4]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83781D_IN5, &sa->in[5]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83781D_IN6, &sa->in[6]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83781D_TEMP1, &sa->temp[0]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83781D_TEMP2, &sa->temp[1]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83781D_TEMP3, &sa->temp[2]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83781D_FAN1, &sa->fan[0]);
      		sensors_get_feature(*sa->chip.name,SENSORS_W83781D_FAN2, &sa->fan[1]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83781D_FAN3, &sa->fan[2]);
      		break;
      case W83782D :
		sensors_get_feature(*sa->chip.name,SENSORS_W83782D_IN0, &sa->in[0]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83782D_IN1, &sa->in[1]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83782D_IN2, &sa->in[2]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83782D_IN3, &sa->in[3]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83782D_IN4, &sa->in[4]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83782D_IN5, &sa->in[5]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83782D_IN6, &sa->in[6]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83782D_TEMP1, &sa->temp[0]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83782D_TEMP2, &sa->temp[1]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83782D_TEMP3, &sa->temp[2]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83782D_FAN1, &sa->fan[0]);
      		sensors_get_feature(*sa->chip.name,SENSORS_W83782D_FAN2, &sa->fan[1]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83782D_FAN3, &sa->fan[2]);
		break;
      case W83783S :
		sensors_get_feature(*sa->chip.name,SENSORS_W83783S_IN0, &sa->in[0]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83783S_IN1, &sa->in[1]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83783S_IN2, &sa->in[2]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83783S_IN3, &sa->in[3]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83783S_IN4, &sa->in[4]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83783S_IN5, &sa->in[5]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83783S_IN6, &sa->in[6]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83783S_TEMP1, &sa->temp[0]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83783S_TEMP2, &sa->temp[1]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83783S_FAN1, &sa->fan[0]);
      		sensors_get_feature(*sa->chip.name,SENSORS_W83783S_FAN2, &sa->fan[1]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83783S_FAN3, &sa->fan[2]);
      		break;
   }
#ifdef DEBUG
   {
   	gint i;

   	g_print("current ranges:\n");
   	for (i=0;i<7;i++)
   		g_print("in%d   %f\n", i, sa->in[i]);
   	for (i=0;i<sa->chip.temp_number;i++)
   		g_print("temp%d %f\n", i, sa->temp[i]);
   	for (i=0;i<sa->chip.fan_number;i++)
   		g_print("fan%d  %f\n", i, sa->fan[i]);
   	g_print ("\n\n");
   }
#endif

   update_adaptive_limits (sa);
   update_history (sa);
   update_pixmap (sa);

   return TRUE;
}

void
sensors_config(LmSensorsApplet *sa)
{
   gint chip_nr=0;
   gint i;
   gint res;
   FILE *cf;

   cf=fopen ("/etc/sensors.conf","r");
   res=sensors_init(cf);
   if (res!=0) {
      cf=fopen ("/usr/etc/sensors.conf","r");
      res=sensors_init(cf);
      if (res!=0) {
         cf=fopen("/usr/local/etc/sensors.conf","r");
         res=sensors_init(cf);
         if (res!=0) {
            g_error("Unable to find file sensors.conf\n");
            gtk_exit(-1);
         }
      }
   }
   fclose(cf);
   sa->chip.chiptype = -1;
   sa->chip.name = sensors_get_detected_chips(&chip_nr);
   while (sa->chip.chiptype == -1 && sa->chip.name != NULL) {
	if (!strcmp(sa->chip.name->prefix,SENSORS_LM78_PREFIX))
		sa->chip.chiptype = LM78;
	else if (!strcmp(sa->chip.name->prefix,SENSORS_LM78J_PREFIX))
		sa->chip.chiptype = LM78J;
	else if (!strcmp(sa->chip.name->prefix,SENSORS_LM79_PREFIX))
		sa->chip.chiptype = LM79;
	else if (!strcmp(sa->chip.name->prefix,SENSORS_LM80_PREFIX))
		sa->chip.chiptype = LM80;
	else if (!strcmp(sa->chip.name->prefix,SENSORS_W83781D_PREFIX))
		sa->chip.chiptype = W83781D;
	else if (!strcmp(sa->chip.name->prefix,SENSORS_W83782D_PREFIX))
		sa->chip.chiptype = W83782D;
	else if (!strcmp(sa->chip.name->prefix,SENSORS_W83783S_PREFIX))
		sa->chip.chiptype = W83783S;
	if (sa->chip.chiptype == -1)
		sa->chip.name = sensors_get_detected_chips(&chip_nr);
   }
   if (sa->chip.chiptype==-1) {
	g_error(_("No supported chips found.\n"));
	gtk_exit(-1);
   }
#ifdef DEBUG
   g_print("Sensor  : %s on %s\n",
   	sa->chip.name->prefix,
   	sensors_get_adapter_name(sa->chip.name->bus));
#endif

   switch (sa->chip.chiptype) {
	case LM78 :
		sensors_get_feature(*sa->chip.name,SENSORS_LM78_IN0_MIN, &sa->in_ll[0]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78_IN0_MAX, &sa->in_ul[0]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78_IN1_MIN, &sa->in_ll[1]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78_IN1_MAX, &sa->in_ul[1]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78_IN2_MIN, &sa->in_ll[2]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78_IN2_MAX, &sa->in_ul[2]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78_IN3_MIN, &sa->in_ll[3]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78_IN3_MAX, &sa->in_ul[3]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78_IN4_MIN, &sa->in_ll[4]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78_IN4_MAX, &sa->in_ul[4]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78_IN5_MIN, &sa->in_ll[5]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78_IN5_MAX, &sa->in_ul[6]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78_IN6_MIN, &sa->in_ll[6]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78_IN6_MAX, &sa->in_ul[6]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78_TEMP_HYST, &sa->temp_ll[0]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78_TEMP_OVER, &sa->temp_ul[0]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78_FAN1_MIN, &sa->fan_ll[0]);
      		sensors_get_feature(*sa->chip.name,SENSORS_LM78_FAN2_MIN, &sa->fan_ll[1]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78_FAN3_MIN, &sa->fan_ll[2]);

		sa->chip.temp_number=1;
		sa->chip.fan_number=3;
		break;
      case LM78J :
		sensors_get_feature(*sa->chip.name,SENSORS_LM78J_IN0_MIN, &sa->in_ll[0]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78J_IN0_MAX, &sa->in_ul[0]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78J_IN1_MIN, &sa->in_ll[1]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78J_IN1_MAX, &sa->in_ul[1]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78J_IN2_MIN, &sa->in_ll[2]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78J_IN2_MAX, &sa->in_ul[2]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78J_IN3_MIN, &sa->in_ll[3]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78J_IN3_MAX, &sa->in_ul[3]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78J_IN4_MIN, &sa->in_ll[4]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78J_IN4_MAX, &sa->in_ul[4]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78J_IN5_MIN, &sa->in_ll[5]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78J_IN5_MAX, &sa->in_ul[5]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78J_IN6_MIN, &sa->in_ll[6]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78J_IN6_MAX, &sa->in_ul[6]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78J_TEMP_HYST, &sa->temp_ll[0]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78J_TEMP_OVER, &sa->temp_ul[0]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78J_FAN1_MIN, &sa->fan_ll[0]);
      		sensors_get_feature(*sa->chip.name,SENSORS_LM78J_FAN2_MIN, &sa->fan_ll[1]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM78J_FAN3_MIN, &sa->fan_ll[2]);

		sa->chip.temp_number=1;
		sa->chip.fan_number=3;
      		break;
      case LM79 :
		sensors_get_feature(*sa->chip.name,SENSORS_LM79_IN0_MIN, &sa->in_ll[0]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM79_IN0_MAX, &sa->in_ul[0]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM79_IN1_MIN, &sa->in_ll[1]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM79_IN1_MAX, &sa->in_ul[1]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM79_IN2_MIN, &sa->in_ll[2]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM79_IN2_MAX, &sa->in_ul[2]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM79_IN3_MIN, &sa->in_ll[3]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM79_IN3_MAX, &sa->in_ul[3]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM79_IN4_MIN, &sa->in_ll[4]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM79_IN4_MAX, &sa->in_ul[4]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM79_IN5_MIN, &sa->in_ll[5]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM79_IN5_MAX, &sa->in_ul[5]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM79_IN6_MIN, &sa->in_ll[6]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM79_IN6_MAX, &sa->in_ul[6]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM79_TEMP_HYST, &sa->temp_ll[0]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM79_TEMP_OVER, &sa->temp_ul[0]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM79_FAN1_MIN, &sa->fan_ll[0]);
      		sensors_get_feature(*sa->chip.name,SENSORS_LM79_FAN2_MIN, &sa->fan_ll[1]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM79_FAN3_MIN, &sa->fan_ll[2]);

		sa->chip.temp_number=1;
		sa->chip.fan_number=3;
		break;
      case LM80 :
		sensors_get_feature(*sa->chip.name,SENSORS_LM80_IN0_MIN, &sa->in_ll[0]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM80_IN0_MAX, &sa->in_ul[0]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM80_IN1_MIN, &sa->in_ll[1]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM80_IN1_MAX, &sa->in_ul[1]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM80_IN2_MIN, &sa->in_ll[2]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM80_IN2_MAX, &sa->in_ul[2]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM80_IN3_MIN, &sa->in_ll[3]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM80_IN3_MAX, &sa->in_ul[3]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM80_IN4_MIN, &sa->in_ll[4]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM80_IN4_MAX, &sa->in_ul[4]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM80_IN5_MIN, &sa->in_ll[5]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM80_IN5_MAX, &sa->in_ul[5]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM80_IN6_MIN, &sa->in_ll[6]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM80_IN6_MAX, &sa->in_ul[6]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM80_TEMP_HOT_HYST, &sa->temp_ll[0]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM80_TEMP_HOT_MAX, &sa->temp_ul[0]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM80_FAN1_MIN, &sa->fan_ll[0]);
		sensors_get_feature(*sa->chip.name,SENSORS_LM80_FAN2_MIN, &sa->fan_ll[1]);

		sa->chip.temp_number=1;
		sa->chip.fan_number=2;
      		break;
      case W83781D :
		sensors_get_feature(*sa->chip.name,SENSORS_W83781D_IN0_MIN, &sa->in_ll[0]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83781D_IN0_MAX, &sa->in_ul[0]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83781D_IN1_MIN, &sa->in_ll[1]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83781D_IN1_MAX, &sa->in_ul[1]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83781D_IN2_MIN, &sa->in_ll[2]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83781D_IN2_MAX, &sa->in_ul[2]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83781D_IN3_MIN, &sa->in_ll[3]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83781D_IN3_MAX, &sa->in_ul[3]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83781D_IN4_MIN, &sa->in_ll[4]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83781D_IN4_MAX, &sa->in_ul[4]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83781D_IN5_MIN, &sa->in_ll[5]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83781D_IN5_MAX, &sa->in_ul[5]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83781D_IN6_MIN, &sa->in_ll[6]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83781D_IN6_MAX, &sa->in_ul[6]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83781D_TEMP1_HYST, &sa->temp_ll[0]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83781D_TEMP1_OVER, &sa->temp_ul[0]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83781D_TEMP2_HYST, &sa->temp_ll[1]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83781D_TEMP2_OVER, &sa->temp_ul[1]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83781D_TEMP3_HYST, &sa->temp_ll[2]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83781D_TEMP3_OVER, &sa->temp_ul[2]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83781D_FAN1_MIN, &sa->fan_ll[0]);
      		sensors_get_feature(*sa->chip.name,SENSORS_W83781D_FAN2_MIN, &sa->fan_ll[1]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83781D_FAN3_MIN, &sa->fan_ll[2]);

		sa->chip.temp_number=3;
		sa->chip.fan_number=3;
      		break;
      case W83782D :
		sensors_get_feature(*sa->chip.name,SENSORS_W83782D_IN0_MIN, &sa->in_ll[0]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83782D_IN0_MAX, &sa->in_ul[0]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83782D_IN1_MIN, &sa->in_ll[1]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83782D_IN1_MAX, &sa->in_ul[1]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83782D_IN2_MIN, &sa->in_ll[2]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83782D_IN2_MAX, &sa->in_ul[2]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83782D_IN3_MIN, &sa->in_ll[3]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83782D_IN3_MAX, &sa->in_ul[3]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83782D_IN4_MIN, &sa->in_ll[4]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83782D_IN4_MAX, &sa->in_ul[4]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83782D_IN5_MIN, &sa->in_ll[5]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83782D_IN5_MAX, &sa->in_ul[5]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83782D_IN6_MIN, &sa->in_ll[6]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83782D_IN6_MAX, &sa->in_ul[6]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83782D_TEMP1_HYST, &sa->temp_ll[0]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83782D_TEMP1_OVER, &sa->temp_ul[0]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83782D_TEMP2_HYST, &sa->temp_ll[1]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83782D_TEMP2_OVER, &sa->temp_ul[1]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83782D_TEMP3_HYST, &sa->temp_ll[2]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83782D_TEMP3_OVER, &sa->temp_ul[2]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83782D_FAN1_MIN, &sa->fan_ll[0]);
      		sensors_get_feature(*sa->chip.name,SENSORS_W83782D_FAN2_MIN, &sa->fan_ll[1]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83782D_FAN3_MIN, &sa->fan_ll[2]);

		sa->chip.temp_number=3;
		sa->chip.fan_number=3;
      		break;
      case W83783S :
		sensors_get_feature(*sa->chip.name,SENSORS_W83783S_IN0_MIN, &sa->in_ll[0]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83783S_IN0_MAX, &sa->in_ul[0]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83783S_IN1_MIN, &sa->in_ll[1]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83783S_IN1_MAX, &sa->in_ul[1]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83783S_IN2_MIN, &sa->in_ll[2]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83783S_IN2_MAX, &sa->in_ul[2]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83783S_IN3_MIN, &sa->in_ll[3]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83783S_IN3_MAX, &sa->in_ul[3]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83783S_IN4_MIN, &sa->in_ll[4]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83783S_IN4_MAX, &sa->in_ul[4]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83783S_IN5_MIN, &sa->in_ll[5]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83783S_IN5_MAX, &sa->in_ul[5]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83783S_IN6_MIN, &sa->in_ll[6]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83783S_IN6_MAX, &sa->in_ul[6]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83783S_TEMP1_HYST, &sa->temp_ll[0]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83783S_TEMP1_OVER, &sa->temp_ul[0]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83783S_TEMP2_HYST, &sa->temp_ll[1]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83783S_TEMP2_OVER, &sa->temp_ul[1]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83783S_FAN1_MIN, &sa->fan_ll[0]);
      		sensors_get_feature(*sa->chip.name,SENSORS_W83783S_FAN2_MIN, &sa->fan_ll[1]);
		sensors_get_feature(*sa->chip.name,SENSORS_W83783S_FAN3_MIN, &sa->fan_ll[2]);

		sa->chip.temp_number=2;
		sa->chip.fan_number=3;
      		break;
   }
   for (i=0;i<3;i++)
   	sa->fan_ul[i]=100;

#ifdef DEBUG
   g_print("min/max ranges:\n");
   for (i=0;i<7;i++)
   	g_print("in%d   %f %f\n", i, sa->in_ll[i], sa->in_ul[i]);
   for (i=0;i<sa->chip.temp_number;i++)
   	g_print("temp%d %f %f\n", i, sa->temp_ll[i], sa->temp_ul[i]);
   for (i=0;i<sa->chip.fan_number;i++)
   	g_print("fan%d %f\n", i, sa->fan_ll[i]);
   g_print ("\n\n");
#endif
}

void
create_pixmap (LmSensorsApplet *sa)
{
   GtkStyle *style;
   gint w,h;
   gchar *mask;
   gchar **xpm;

   /*
    * Select the pixmap according to value of display_temp_curves
    */
   if (sa->display_temp_curves) {
   	w=wmlm_mask2_width;
   	h=wmlm_mask2_height;
   	mask=(gchar *)wmlm_mask2_bits;
   	xpm=(gchar **)wmlm_master2_xpm;
   } else {
   	w=wmlm_mask_width;
   	h=wmlm_mask_height;
   	mask=(gchar *)wmlm_mask_bits;
   	xpm=(gchar **)wmlm_master_xpm;
   }
   /*
    * Free the existing bitmaps/pixmap first
    */
   if (sa->lm_mask)
   	gdk_bitmap_unref (sa->lm_mask);
   if (sa->lm_master)
   	gdk_pixmap_unref (sa->lm_master);
   /*
    * Make a shaped window
    */
   sa->lm_mask = gdk_bitmap_create_from_data(sa->darea->window,mask,w,h);
   gdk_window_shape_combine_mask(sa->darea->window,sa->lm_mask,0,0);

   style=gtk_widget_get_style(sa->applet);
   sa->lm_master = gdk_pixmap_create_from_xpm_d(sa->darea->window, &sa->lm_mask,
   	&style->bg[GTK_STATE_NORMAL], xpm);
   draw_temp_labels (sa);
}

static void
properties_load (gchar *path, LmSensorsApplet *sa)
{
   gnome_config_push_prefix (path);
   sa->temp_unit=gnome_config_get_int_with_default ("lm_sensors/temp_unit=0",NULL);
   sa->fan_number=gnome_config_get_int_with_default ("lm_sensors/fan_number=1",NULL);
   sa->temp1_number=gnome_config_get_int_with_default ("lm_sensors/temp1_number=0",NULL);
   sa->temp2_number=gnome_config_get_int_with_default ("lm_sensors/temp2_number=1",NULL);
   sa->temp1_type=gnome_config_get_int_with_default ("lm_sensors/temp1_type=0",NULL);
   sa->temp2_type=gnome_config_get_int_with_default ("lm_sensors/temp2_type=1",NULL);
   sa->update_timeout=gnome_config_get_int_with_default ("lm_sensors/update_timeout=5000",NULL);
   sa->temp_curve_ll=gnome_config_get_float_with_default ("lm_sensors/temp_curve_ll=30.0",NULL);
   sa->temp_curve_ul=gnome_config_get_float_with_default ("lm_sensors/temp_curve_ul=55.0",NULL);
   sa->display_temp_curves=gnome_config_get_bool_with_default ("lm_sensors/display_temp_curves=true",NULL);
   sa->use_adaptive_limits=gnome_config_get_bool_with_default ("lm_sensors/use_adaptive_limits=false",NULL);
   gnome_config_pop_prefix ();
}

static void
properties_save (gchar *path,LmSensorsApplet *sa)
{
#ifdef DEBUG
   g_print ("properties_save:\n");
#endif
   gnome_config_push_prefix (path);
   gnome_config_set_int ("lm_sensors/temp_unit",sa->temp_unit);
   gnome_config_set_int ("lm_sensors/fan_number",sa->fan_number);
   gnome_config_set_int ("lm_sensors/temp1_number",sa->temp1_number);
   gnome_config_set_int ("lm_sensors/temp2_number",sa->temp2_number);
   gnome_config_set_int ("lm_sensors/temp1_type",sa->temp1_type);
   gnome_config_set_int ("lm_sensors/temp2_type",sa->temp2_type);
   gnome_config_set_int ("lm_sensors/update_timeout",sa->update_timeout);
   gnome_config_set_float ("lm_sensors/temp_curve_ll",sa->temp_curve_ll);
   gnome_config_set_float ("lm_sensors/temp_curve_ul",sa->temp_curve_ul);
   gnome_config_set_bool ("lm_sensors/display_temp_curves",sa->display_temp_curves);
   gnome_config_set_bool ("lm_sensors/use_adaptive_limits",sa->use_adaptive_limits);
   gnome_config_sync ();
   gnome_config_drop_all ();
   gnome_config_pop_prefix ();
}

static gint
save_session_cb (GtkWidget *widget,
	gchar *privcfgpath,
	gchar *globcfgpath,
	gpointer data)
{
   LmSensorsApplet *sa = data;
#ifdef DEBUG
   g_print ("save_session_cb\n");
#endif
   properties_save (privcfgpath,sa);
   return FALSE;
}

static void
init_applet (LmSensorsApplet *sa)
{
   sa->fan_number = 1;
   sa->temp1_number = 0;
   sa->temp2_number = 1;
   sa->temp1_type = MB_TEMP;
   sa->temp2_type = CPU_TEMP;
   sa->update_timeout = 5000;
   sa->temp_curve_ll = 30.0;
   sa->temp_curve_ul = 55.0;
   sa->display_temp_curves = TRUE;
   sa->use_adaptive_limits = FALSE;
   sa->reset_adaptive_limits = TRUE;
   sa->temp_history_idx = 0;
   sa->temp_history_nb = 0;
   sa->changing_unit = FALSE;
}

static void
delete_cb (GtkWidget *widget,GdkEvent *event, gpointer data)
{
#ifdef DEBUG
   g_print ("delete_cb\n");
#endif
   gtk_main_quit();
}

int
main(int argc, char **argv)
{
   LmSensorsApplet *sa;

   /* Internationalization stuff */
   bindtextdomain (PACKAGE, GNOMELOCALEDIR);
   textdomain (PACKAGE);

   sa = g_new0(LmSensorsApplet,1);
   init_applet (sa);

   applet_widget_init("lm_sensors_applet", LM_VERSION, argc, argv,
	NULL, 0, NULL);
   sa->applet = applet_widget_new("lm_sensors_applet");
   if (!sa->applet)
	g_error("Can't create applet!\n");

   gtk_signal_connect (GTK_OBJECT (sa->applet),
   	"save_session",GTK_SIGNAL_FUNC(save_session_cb),sa);
   gtk_signal_connect (GTK_OBJECT (sa->applet),
   	"delete_event",GTK_SIGNAL_FUNC(delete_cb),NULL);
   gtk_signal_connect (GTK_OBJECT (sa->applet),
   	"expose_event",GTK_SIGNAL_FUNC(darea_expose_cb),sa);

   applet_widget_register_stock_callback(APPLET_WIDGET(sa->applet),
   	"properties",GNOME_STOCK_MENU_PROP,
   	_("Properties..."),GTK_SIGNAL_FUNC(properties_cb),sa);
   applet_widget_register_stock_callback(APPLET_WIDGET(sa->applet),
   	"about",GNOME_STOCK_MENU_ABOUT,
   	_("About..."),GTK_SIGNAL_FUNC(about_cb),sa);

   properties_load (APPLET_WIDGET (sa->applet)->privcfgpath,sa);

   gtk_widget_push_visual (gdk_rgb_get_visual ());
   gtk_widget_push_colormap (gdk_rgb_get_cmap ());
   sa->darea = gtk_drawing_area_new();
   gtk_widget_pop_visual();
   gtk_widget_pop_colormap();
   gtk_drawing_area_size(GTK_DRAWING_AREA(sa->darea), 64,64);
   gtk_widget_show (sa->darea); 

   applet_widget_add( APPLET_WIDGET(sa->applet), sa->darea);
   gtk_widget_show(sa->applet);

   sensors_config (sa);
   create_pixmap (sa);

   sa->timeout_handler_id=gtk_timeout_add(sa->update_timeout,(GtkFunction)sensors_update,sa);
   nice (NICE_VALUE);
   sensors_update (sa);
   applet_widget_gtk_main();

   return 0;
}
