1. Piotr Husiatyński
  2. vala-fan

Commits

Piotr Husiatyński  committed e2b5fa5

first fully working version

  • Participants
  • Parent commits bb7d0d8
  • Branches default
  • Tags 0.1

Comments (0)

Files changed (5)

File src/controller.vala

View file
         private string file_speed_get = "/mnt/apple/fan1_output";
         private string file_temp = "/sys/devices/platform/coretemp.0/temp1_input";
         private string file_manual = "/mnt/apple/fan1_manual";
-        private int max_speed = 6200;
-        private int min_speed = 2000;
-        private bool auto_mode = false;
+        private int max_speed = 6100;
+        private int min_speed = 2500;
+        private int best_temp = 45000;
+        // auto mode sleep in seconds
+        private int auto_mode_delta = 10;
+        private bool auto_mode = true;
 
         public int min { 
             get { return this.min_speed; }
         }
+
         public int max { 
             get { return this.max_speed; }
         }
+
         public int speed {
             set {
                 if ((value < this.max_speed || value < this.speed) && value > this.min_speed) {
             }
             set {
                 this.auto_mode = value;
+                if (this.auto == true)
+                    Timeout.add_seconds(this.auto_mode_delta, this.update_speed);
             }
         }
 
-        public void change_mode() {
-            this.auto_mode = ! this.auto_mode;
+        public bool update_speed() {
+            this.speed -= this.best_temp - this.temp;
+            return this.auto;
         }
     }
 

File src/fan.vala

View file
 public static int main(string[] args)
 {
     Gtk.init(ref args);
-    int a = 5;
-    int step = 500;
-    int best_temp = 4000;
-    int delta = 10;
-    var fan = new Fan.FanStatus();
-    while(true) {
-        break;
-    }
+    var  fan = new Fan.FanStatus();
     Gtk.main();
     return 0;
 }

File src/status.vala

View file
     public class FanStatus : GLib.Object {
         private FanController fan;
         private Gtk.StatusIcon tray_icon;
+        private Scale menu;
         private int change_value = 500;
 
         public FanStatus() {
             this.tray_icon = new Gtk.StatusIcon.from_file("icon.png");
             this.tray_icon.popup_menu += this.popup_menu_show;
             this.tray_icon.activate += this.popup_scale_menu;
-            this.update_tooltip_info();
+            this.tray_icon.scroll_event += (e) => { 
+                    switch (e.scroll.direction) {
+                        case Gdk.ScrollDirection.UP:
+                            this.fan.speed += this.change_value / 2;
+                            break;
+                        case Gdk.ScrollDirection.DOWN:
+                            this.fan.speed -= this.change_value / 2;
+                            break;
+                    }
+                };
+            this.tray_icon.set_tooltip_text(this.get_cooler_info());
             this.tray_icon.set_visible(true);
         }
 
         private string get_cooler_info() {
-            return "Cooler speed: %d".printf(fan.speed);
+            return "Cooler speed: %d  Proc temp: %d°C".printf(fan.speed, this.fan.temp / 1000);
         }
 
         private void popup_menu_show(uint button, uint activate_time) {
             // fan auto/manual
             if (!this.fan.auto) {
                 var item_mode = new Gtk.MenuItem.with_label("Set cooler speed to *auto*");
-                item_mode.activate += (source) => { this.fan.change_mode(); };
+                item_mode.activate += (source) => { this.fan.auto = ! this.fan.auto; };
                 tray_menu.append(item_mode);
             }
             // exit
         }
 
         private void popup_scale_menu() {
-            int new_speed;
-            var menu = new Scale(this.fan, this.change_value);
+            if (this.menu != null)
+                return;
+            this.menu = new Scale(this.fan, this.change_value);
+            menu.scale.button_release_event += (e) => { this.set_speed((int) menu.scale.get_value()); };
             menu.popup();
         }
+
+        public bool set_speed(int speed) {
+            this.fan.speed = speed;
+            this.menu.destroy();
+            this.menu = null;
+            return true;
+        }
     }
 
     class Scale : GLib.Object {
         private Gtk.Window window;
-        private Gtk.VScale scale;
+        public Gtk.VScale scale;
         private FanController controller;
 
         public Scale(FanController fan, int step=0) {
             this.controller = fan;
             this.window = new Gtk.Window(Gtk.WindowType.POPUP);
             this.scale = new Gtk.VScale.with_range(fan.min, fan.max, step);
-            this.scale.button_release_event += this.value_choosen;
+            this.scale.set_value(fan.speed);
             this.window.resize(35, 200);
             this.window.set_position(Gtk.WindowPosition.MOUSE);
             this.window.add(scale);
+            this.window.set_modal(true);
+        }
+        
+        public void popup() {
+            this.window.show_all();
         }
 
-        public bool value_choosen(Gdk.EventButton event) {
-            error("x\n");
-            this.controller.speed = (int) this.scale.get_value();
-            this.window.delete_event(null);
-            return true;
-        }
-
-        public void set_value(int v) {
-            this.scale.set_value(v);
-        }
-
-        public void popup() {
-            this.window.show_all();
+        public void destroy() {
+            this.window.destroy();
         }
     }
 

File src/wscript_build

View file
 # -*- coding: utf-8
 
 obj = bld.new_task_gen(
-	features = 'cc cprogram',
-	packages = 'gtk+-2.0',
-	target = 'vala-fan',
-	uselib = 'GTK GLIB',
+	features='cc cprogram',
+	packages='gtk+-2.0 glib-2.0',
+	target='vala-fan',
+	uselib='GTK GLIB THREAD',
 	)
 
 obj.find_sources_in_dirs('.')

File wscript

View file
 srcdir = '.'
 blddir = 'build'
 
+
 def set_options(opt):
 	opt.tool_options('compiler_cc')
 
 def configure(conf):
-	conf.check_tool('compiler_cc cc vala')
-	conf.check_cfg(package='glib-2.0', uselib_store='GLIB', atleast_version='2.10.0', mandatory=1, args='--cflags --libs')
-	conf.check_cfg(package='gtk+-2.0', uselib_store='GTK', atleast_version='2.10.0', mandatory=1, args='--cflags --libs')
+    conf.check_tool('compiler_cc cc vala')
+    conf.check_cfg(package='glib-2.0', uselib_store='GLIB', atleast_version='2.10.0', mandatory=1, args='--cflags --libs --threads')
+    conf.check_cfg(package='gtk+-2.0', uselib_store='GTK', atleast_version='2.10.0', mandatory=1, args='--cflags --libs')
 
 def build(bld):
 	bld.add_subdirs('src')