Support for stopping the auto-rotate daemon via dbus.
Support for stopping the auto-rotate daemon via dbus.

--- a/auto-rotate.py
+++ b/auto-rotate.py
@@ -31,9 +31,6 @@
 import dbus.service
 import dbus.mainloop.glib
 
-import gobject
-import glib
-
 def main():
     # Initialize possible options
     usage = "usage: %prog [options]"
@@ -57,11 +54,8 @@
         
         autorotate=AutoRotateDaemon.AutoRotateDaemon(system_bus,"/rotate")
         
-        # Start loop
-        glib.timeout_add(2000,autorotate.run)
+        autorotate.start()
         
-        mainloop = gobject.MainLoop()
-        mainloop.run()
 
 main()
 

--- a/librotate/AutoRotateDaemon.py
+++ b/librotate/AutoRotateDaemon.py
@@ -26,6 +26,9 @@
 import sys;
 import math;
 
+import glib
+import gobject
+
 import xrandr
 
 from . import Rotate
@@ -42,6 +45,9 @@
 
     # Codes for rotation
     rotate=Rotate.Rotate();
+
+    # The GLIB mainloop that the daemon is running under
+    mainloop=None;
 
     def __init__(self,bus=0,object_path=''):
         if(bus!=0):
@@ -73,8 +79,18 @@
         self.rotate.setNextRotation();
         self.setDisabled(True); # Disable automatic rotation
 
+    @dbus.service.method("net.krizka.autorotate.kill",
+                         in_signature='', out_signature='')
+    def kill(self):
+        self.mainloop.quit();
+
     def log(self,txt):
 	print "Autorotate: "+txt
+
+    def start(self):
+        glib.timeout_add(2000,self.run)
+        self.mainloop = gobject.MainLoop()
+        self.mainloop.run()
 
     # This code is run every 2 seconds
     def run(self):
@@ -123,3 +139,5 @@
 
         return True;
 
+        
+

--- a/librotate/AutoRotateDaemonMonitor.py
+++ b/librotate/AutoRotateDaemonMonitor.py
@@ -38,4 +38,28 @@
         if(self.daemon_status_callback):
             self.daemon_status_callback(name);
 
+    def isDaemonRunning(self):
+        system_services = self.bus.list_names();
+        return ("net.krizka.autorotate" in system_services);
 
+    def isDaemonDisabled(self):
+        if(not self.isDaemonRunning()):
+            return False;
+        else:
+            remote_state=bus.get_object("net.krizka.autorotate",self.object_path)
+            return remote_state.isDisabled(dbus_interface = "net.krizka.autorotate.isDisabled");
+
+    def setDaemonDisabled(self,disabled):
+        if(not self.isDaemonRunning()):
+            return;
+        else:
+            remote_state=bus.get_object("net.krizka.autorotate",self.object_path);
+            remote_state.setDisabled(disabled,dbus_interface = "net.krizka.autorotate.setDisabled");
+
+    def killDaemon(self):
+        if(not self.isDaemonRunning()):
+            return; # Cannot kill a non existent daemon
+        
+        remote_state=self.bus.get_object("net.krizka.autorotate",self.object_path);
+        remote_state.kill(dbus_interface = "net.krizka.autorotate.kill");
+

--- a/rotate-indicator.py
+++ b/rotate-indicator.py
@@ -29,7 +29,7 @@
         self.controller.setRotation(codes[direction]);
 
     def refreshLabels(self):
-        if(self.controller.isDaemonRunning()):
+        if(self.monitor.isDaemonRunning()):
             self.daemon_menu_item.set_label("Stop Auto-Rotate Daemon")
             self.auto_menu_item.set_sensitive(1);
         else:
@@ -37,8 +37,8 @@
             self.auto_menu_item.set_sensitive(0);
 
     def startStopDaemon(self,widget):
-        if(self.controller.isDaemonRunning()):
-            print "KILL"
+        if(self.monitor.isDaemonRunning()):
+            self.monitor.killDaemon();
         else:
             os.system("auto-rotate.py -d");