package com.github.hypfvieh.dbus.examples.signal;

import java.io.IOException;

import org.freedesktop.dbus.connections.impl.DBusConnection;
import org.freedesktop.dbus.connections.impl.DBusConnectionBuilder;
import org.freedesktop.dbus.exceptions.DBusException;
import org.freedesktop.dbus.handlers.AbstractPropertiesChangedHandler;
import org.freedesktop.dbus.interfaces.Properties.PropertiesChanged;

/**
 * Sample on how to register a callback for a signal.
 * <p>
 * In this case a global {@link PropertiesChanged} handler is installed.
 * It will be queried for every properties changed event on the bus.
 *
 * @author hypfvieh
 */
public class PropertiesChangedSignal {

    public static void main(String[] _args) throws DBusException, InterruptedException, IOException {
        // open connection to SYSTEM Bus
        try (DBusConnection connection = DBusConnectionBuilder.forSystemBus().build()) {
            // add our signal handler
            connection.addSigHandler(PropertiesChanged.class, new PropChangedHandler());

            // just do some sleep so you can see the events on stdout (you would probably do something else here)
            System.out.println("sleeping");
            Thread.sleep(60000L);
            System.out.println("shutting down");
        }
    }

    /**
     * This handler will be called for every property change signal.
     */
    public static class PropChangedHandler extends AbstractPropertiesChangedHandler {

        @Override
        public void handle(PropertiesChanged _s) {
            System.out.println("changed: " + _s.getPropertiesChanged());
        }

    }
}

@@ -408,17 +408,38 @@
 %0A * 
-Sample on
+This example code demonstrates
  how
@@ -483,29 +483,9 @@
 %0A * 
-%3Cp%3E%0A * In this case a
+A
  glo
@@ -538,31 +538,26 @@
 lled
-.%0A * It will be
+, which is
  queried
  for
@@ -552,16 +552,19 @@
  queried
+%0A *
  for eve
@@ -725,47 +725,8 @@
 ows 
-DBusException, InterruptedException, IO
 Exce
@@ -744,17 +744,17 @@
      // 
-o
+O
 pen conn
@@ -767,18 +767,23 @@
  to 
-SYSTEM B
+the system b
 us
+.
 %0A   
@@ -887,15 +887,13 @@
  // 
-add our
+Add a
  sig
@@ -903,16 +903,17 @@
  handler
+.
 %0A       
@@ -916,16 +916,34 @@
         
+ final var token =
  connect
@@ -1032,106 +1032,71 @@
  // 
-just do some sleep so you can see the events on stdout (you would probably do something else here)
+Pause to see events written to stdout (your code would differ).
 %0A   
@@ -1218,16 +1218,86 @@
  down%22);
+%0A%0A            // Remove the signal handler.%0A            token.close();
 %0A       
@@ -1477,17 +1477,16 @@
 ndler %7B%0A
-%0A
         
@@ -1625,21 +1625,20 @@
 ;%0A        %7D%0A
-%0A
     %7D%0A%7D%0A

Add signal handler deregistration example