Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Xerus committed Apr 25, 2018
1 parent 54cc232 commit 3f35832
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 204 deletions.
5 changes: 3 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import java.util.Date

plugins {
kotlin("jvm") version "1.2.30"
kotlin("jvm") version "1.2.51"
id("com.jfrog.bintray") version "1.8.0"
}

Expand All @@ -16,6 +16,7 @@ allprojects {

repositories {
jcenter()
maven("https://jitpack.io")
}

}
Expand All @@ -26,7 +27,7 @@ val kotlinVersion: String by extra {
}

dependencies {
compile("com.github.hypfvieh", "dbus-java", "2.7.+")
compile("com.github.Xerus2000", "dbus-java", "2.7-SNAPSHOT")
compile("org.jetbrains.kotlin", "kotlin-runtime", kotlinVersion)
}

Expand Down
4 changes: 2 additions & 2 deletions extensions/src/xerus/mpris/AbstractMPRISPlayer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import org.mpris.MediaPlayer2.*
* A val represents a Read-only field as declared by MPRIS, it is perfectly valid to implement it as var */
abstract class AbstractMPRISPlayer : MediaPlayerX, PlayerX, DefaultDBus {

val connection = DBusConnection.getConnection(DBusConnection.SESSION)
val connection: DBusConnection = DBusConnection.getConnection(DBusConnection.SESSION)
val properties = HashMap<String, MutableMap<String, Variant<*>>>()
internal val propertyListeners = HashMap<String, (Any) -> Unit>()

Expand Down Expand Up @@ -109,4 +109,4 @@ interface PlaylistsX : Playlists {

val playlistCount: Int
val activePlaylist: MaybePlaylist
}
}
32 changes: 27 additions & 5 deletions extensions/src/xerus/mpris/DBusPropertyDelegate.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import javafx.beans.value.ObservableValue
import org.freedesktop.dbus.DBusInterfaceName
import org.mpris.MediaPlayer2.PlaylistOrdering
import org.slf4j.LoggerFactory
import kotlin.properties.ObservableProperty
import kotlin.properties.ReadOnlyProperty
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
Expand All @@ -21,7 +20,7 @@ private fun findInterface(clazz: Class<*>, name: String): Class<*>? =
(clazz.interfaces + clazz.superclass).firstOrNull {
if(it == null) return@firstOrNull false
if (it.declaredMethods.any { it.name.contains(name) }) {
logger.debug("Found $it for property $name")
logger.trace("Found $it for property $name")
return it.interfaces.first()
}
findInterface(it, name) != null
Expand All @@ -40,15 +39,38 @@ class DBusProperty<T : Any>(private val initial: T, private val observable: Obse
?: throw RuntimeException("No interface found for Property $name")
val interfaceName = (clazz.annotations.find { it is DBusInterfaceName } as? DBusInterfaceName)?.value
?: clazz.name
logger.debug("Registered Property ${prop.name} for $interfaceName")
thisRef.properties.getOrPut(interfaceName) { HashMap() }.put(name, initial.variant())
logger.trace("Registered Property ${prop.name} for $interfaceName")
thisRef.properties.getOrPut(interfaceName) { HashMap() }[name] = initial.variant()
val property = Property<T>(interfaceName, name)
if (onSet != null)
thisRef.propertyListeners[name] = onSet as ((Any) -> Unit)
observable?.addListener { _, _, new -> property.setValue(thisRef, prop, new) }
return property
}

}
class DBusMapProperty<K, V>(private val initial: Map<K, V> = HashMap<K, V>(), private val observable: ObservableValue<Map<K, V>>? = null, private val onSet: ((Map<K, V>) -> Unit)? = null) {

constructor(observable: ObservableValue<Map<K, V>>, onSet: ((Map<K, V>) -> Unit)? = null) : this(observable.value, observable, onSet)

operator fun provideDelegate(
thisRef: AbstractMPRISPlayer,
prop: KProperty<*>
): ReadWriteProperty<AbstractMPRISPlayer, Map<K, V>> {
val name = prop.name.capitalize()
val clazz = findInterface(thisRef::class.java, name)
?: throw RuntimeException("No interface found for Property $name")
val interfaceName = (clazz.annotations.find { it is DBusInterfaceName } as? DBusInterfaceName)?.value
?: clazz.name
logger.trace("Registered Property ${prop.name} for $interfaceName")
//thisRef.properties.getOrPut(interfaceName) { HashMap() }[name] = initial.variant()
val property = Property<Map<K, V>>(interfaceName, name)
if (onSet != null)
thisRef.propertyListeners[name] = onSet as ((Any) -> Unit)
observable?.addListener { _, _, new -> property.setValue(thisRef, prop, new) }
return property
}

}

class DBusConstant<out T : Any>(private val value: T) {
Expand All @@ -62,7 +84,7 @@ class DBusConstant<out T : Any>(private val value: T) {
?: throw RuntimeException("No interface found for Property $name")
val interfaceName = (clazz.annotations.find { it is DBusInterfaceName } as? DBusInterfaceName)?.value
?: clazz.name
logger.debug("Registered Constant ${prop.name} for $interfaceName")
logger.trace("Registered Constant ${prop.name} for $interfaceName")
thisRef.properties.getOrPut(interfaceName) { HashMap() }.put(name, value.variant())
return Constant(value)
}
Expand Down
50 changes: 26 additions & 24 deletions extensions/src/xerus/mpris/DefaultDBus.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,35 @@ package xerus.mpris
import org.freedesktop.DBus.Properties
import org.freedesktop.dbus.Variant
import org.freedesktop.dbus.types.DBusMapType
import java.security.InvalidParameterException
import java.util.*

fun Any.variant(): Variant<*> {
if(this is Variant<*>)
return this
if(this is Map<*, *>) {
val params = this::class.java.typeParameters
return Variant(this, DBusMapType(params[0], params[1]))
}
return Variant(this)
if (this is Variant<*>)
return this
if (this is Map<*, *>)
throw InvalidParameterException("Use Map.variant for Maps!")
return Variant(this)
}

interface DefaultDBus: Properties {

override fun isRemote() = false

@Suppress("UNCHECKED_CAST")
override fun <A: Any> Get(interface_name: String, property_name: String) =
GetAll(interface_name)[property_name]?.value as A

override fun <A: Any> Set(interface_name: String, property_name: String, value: A) {
GetAll(interface_name).put(property_name, value.variant())
propertyChanged(interface_name, property_name)
}

/** returns a new [Properties.PropertiesChanged] signal */
fun propertyChanged(interface_name: String, property_name: String) =
Properties.PropertiesChanged(objectPath, interface_name, Collections.singletonMap(property_name, Get<Any>(interface_name, property_name).variant()) as Map<String, Variant<*>>, null)

inline fun <reified K, reified V> Map<K, V>.variant() =
Variant(this, DBusMapType(K::class.java, V::class.java))

interface DefaultDBus : Properties {

override fun isRemote() = false

@Suppress("UNCHECKED_CAST")
override fun <A : Any> Get(interface_name: String, property_name: String) =
GetAll(interface_name)[property_name]?.value as A

override fun <A : Any> Set(interface_name: String, property_name: String, value: A) {
GetAll(interface_name).put(property_name, value.variant())
propertyChanged(interface_name, property_name)
}

/** returns a new [Properties.PropertiesChanged] signal */
fun propertyChanged(interface_name: String, property_name: String) =
Properties.PropertiesChanged(objectPath, interface_name, Collections.singletonMap(property_name, Get<Any>(interface_name, property_name).variant()) as Map<String, Variant<*>>, null)

}
3 changes: 1 addition & 2 deletions extensions/src/xerus/mpris/PropertyMap.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package xerus.mpris

import org.freedesktop.dbus.Variant
import org.freedesktop.dbus.types.DBusMapType
import java.util.HashMap
import java.util.*

class PropertyMap private constructor(initial: PropertyMap.() -> Unit, private val map: HashMap<String, Variant<*>>): Map<String, Variant<*>> by map {

Expand Down
Loading

0 comments on commit 3f35832

Please sign in to comment.