First attempt at implementing screen lock
authorEnrico Zini <enrico@enricozini.org>
Tue, 11 Aug 2009 21:31:55 +0000 (22:31 +0100)
committerEnrico Zini <enrico@enricozini.org>
Tue, 11 Aug 2009 21:31:55 +0000 (22:31 +0100)
src/app_powerbutton.vala

index 62f29e6aa7a625f6a61aeb2bd3e18038620c07a4..175e67fb6e5ac320c93ee5a09038c3f436560847 100644 (file)
@@ -136,12 +136,53 @@ public class CommandButton : Gtk.Button
        }
 }
 
-private void on_power_button(Posix.timeval* time, bool pressed)
+private bool screen_locked = false;
+
+private void set_screen_lock(bool locked)
 {
-               if (!pressed)
+               int fd = Posix.open("/dev/input/event1", Posix.O_RDWR | Posix.O_NONBLOCK);
+               if (fd < 0)
                {
-                       zavai.app.push_applet("menu.power");
+                       zavai.log.error("Cannot open /dev/input/event1");
+                       return;
                }
+
+               int EVIOCGRAB = 0x40044590;
+               if (Posix.ioctl(fd, EVIOCGRAB, locked ? 1 : 0) != 0)
+               {
+                       zavai.log.error("Cannot EVIOCGRAB /dev/input/event1");
+                       Posix.close(fd);
+                       return;
+               }
+               screen_locked = locked;
+               Posix.close(fd);
+}
+
+public class ScreenLockButton : Gtk.Button
+{
+       public ScreenLockButton(string name)
+       {
+               label = name;
+               clicked += on_clicked;
+               set_size_request(0, zavai.config.min_button_height);
+       }
+
+       public void on_clicked()
+       {
+               zavai.log.info("Locking screen");
+               set_screen_lock(true);
+       }
+}
+
+private void on_power_button(Posix.timeval* time, bool pressed)
+{
+       if (!pressed)
+       {
+               if (screen_locked)
+                       set_screen_lock(false);
+               else
+                       zavai.app.push_applet("menu.power");
+       }
 }
 
 /*
@@ -157,6 +198,7 @@ public void init()
 
     // Menus
     var menu_power = new zavai.Menu("Power menu");
+       menu_power.add_widget(new ScreenLockButton("Lock screen"));
        menu_power.add_widget(new CommandButton("Suspend", "apm -s"));
        menu_power.add_widget(new CommandButton("Shutdown", "shutdown -h now"));
        menu_power.add_widget(new CommandButton("Reboot", "shutdown -r now"));