Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ dependencies {
implementation("net.java.dev.jna:jna:5.15.0")
implementation("net.java.dev.jna:jna-platform:5.15.0")
implementation("com.hynnet:jacob:1.18")
implementation("com.github.kwhat:jnativehook:2.2.2")
}

tasks {
Expand Down
65 changes: 56 additions & 9 deletions src/main/kotlin/eu/tricht/gamesense/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@ import eu.tricht.gamesense.events.EventProducer
import java.util.*
import java.util.prefs.Preferences
import kotlin.system.exitProcess
import com.github.kwhat.jnativehook.GlobalScreen
import com.github.kwhat.jnativehook.NativeHookException
import com.github.kwhat.jnativehook.keyboard.NativeKeyEvent
import com.github.kwhat.jnativehook.keyboard.NativeKeyListener

val mapper = jacksonObjectMapper()
const val GAME_NAME = "GAMESENSE_ESSENTIALS"
const val CLOCK_EVENT = "CLOCK"
const val VOLUME_EVENT = "VOLUME"
const val SONG_EVENT = "SONG"
const val CALCULATOR_EVENT = "CALCULATOR"
var timer = Timer()
var client: ApiClient? = null
var preferences: Preferences = Preferences.userNodeForPackage(Main::class.java)
Expand All @@ -27,6 +32,8 @@ fun main() {

class Main {
companion object {
var eventProducer: EventProducer? = null

fun registerHandlers(client: ApiClient) {
registerClockHandler(client)
val volumeHandler = EventRegistration(
Expand Down Expand Up @@ -60,6 +67,7 @@ class Main {
exitProcess(1)
}
registerSongHandler(client)
registerCalculatorHandler(client)
}

fun registerClockHandler(client: ApiClient) {
Expand Down Expand Up @@ -94,10 +102,10 @@ class Main {
MultiLine(
listOf(
HandlerData(
contextFrameKey = "artist"
contextFrameKey = "text2"
),
HandlerData(
contextFrameKey = "song"
contextFrameKey = "text1"
)
),
if (preferences.get("songIcon", "true")!!.toBoolean()) 23 else 0
Expand All @@ -107,11 +115,11 @@ class Main {
),
listOf(
DataField(
"song",
"text1",
"Song"
),
DataField(
"artist",
"text2",
"Artist"
)
)
Expand All @@ -131,10 +139,10 @@ class Main {
MultiLine(
listOf(
HandlerData(
contextFrameKey = "song"
contextFrameKey = "text1"
),
HandlerData(
contextFrameKey = "artist"
contextFrameKey = "text2"
)
),
if (preferences.get("songIcon", "true")!!.toBoolean()) 23 else 0
Expand All @@ -144,11 +152,11 @@ class Main {
),
listOf(
DataField(
"song",
"text1",
"Song"
),
DataField(
"artist",
"text2",
"Artist"
)
)
Expand All @@ -161,8 +169,47 @@ class Main {
}
}

fun registerCalculatorHandler(client: ApiClient) {
val calculatorHandler = EventRegistration(
GAME_NAME,
CALCULATOR_EVENT,
listOf(
Handler(
listOf(
MultiLine(
listOf(
HandlerData(
contextFrameKey = "text1"
),
HandlerData(
contextFrameKey = "text2"
)
)
)
)
)
),
listOf(
DataField(
"text1",
"Input"
),
DataField(
"text2",
"Answer"
)
)
)
val response = client.addEvent(calculatorHandler).execute()
if (!response.isSuccessful) {
println("Failed to add calculator handler, error: " + response.errorBody()?.string())
exitProcess(1)
}
}

fun startTimer() {
timer.schedule(EventProducer(), 0, Tick.tickRateInMs())
eventProducer = EventProducer()
timer.schedule(eventProducer, 0, Tick.tickRateInMs())
}
}
}
6 changes: 6 additions & 0 deletions src/main/kotlin/eu/tricht/gamesense/SystemTray.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ class SystemTray {
createSettingMenuItem("Enable song icon", "songIcon"),
createSettingMenuItem("Flip song title and artist", "songInfoFlip", false),
createSettingMenuItem("Display song scrolling separator", "songSeparator", false),
createSettingMenuItem("Enable calculator (Need numpad)", "calculator"),
createSettingMenuItem("Use algebraic calculator", "calculatorIsAlgebraic", false),
createSettingMenuItem("Flip calculator numpad", "calculatorNumpadFlip", false),
tickRate,
exit
).forEach(menu::add)
Expand Down Expand Up @@ -66,6 +69,9 @@ class SystemTray {
timer = Timer()
Main.startTimer()
}
if (setting == "calculatorIsAlgebraic") {
Main.eventProducer!!.calculator.reset()
}
}
settingMenuItem.state = preferences.get(setting, default.toString()).toBoolean()
return settingMenuItem
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package eu.tricht.gamesense.com.steelseries.model

data class Frame(
val song: String,
val artist: String
val text1: String,
val text2: String
)
25 changes: 25 additions & 0 deletions src/main/kotlin/eu/tricht/gamesense/events/EventProducer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import eu.tricht.gamesense.com.steelseries.model.Data
import eu.tricht.gamesense.com.steelseries.model.Event
import eu.tricht.gamesense.com.steelseries.model.Frame
import eu.tricht.gamesense.model.SongInformation
import eu.tricht.gamesense.model.Calculator
import java.net.ConnectException
import java.text.DateFormat
import java.util.*
Expand All @@ -20,6 +21,8 @@ class EventProducer : TimerTask() {
private var displayClockPeriodically = 0
private var currentSong: SongInformation? = null
private var masterVolumeTimeout = 0
public var calculator = Calculator()
private var toggleInsertionPoint = false

override fun run() {
try {
Expand Down Expand Up @@ -49,6 +52,10 @@ class EventProducer : TimerTask() {
waitTicks = Tick.msToTicks(2000)
return
}
if (preferences.get("calculator", "true").toBoolean() && (if (!preferences.get("calculatorNumpadFlip", "false").toBoolean()) calculator.numpadOn else !calculator.numpadOn)) {
sendCalculatorEvent()
return
}
val potentialSong = dataFetcher.getCurrentSong()
if (preferences.get("songInfo", "true").toBoolean() && potentialSong != null && potentialSong != "") {
if (currentSong == null || potentialSong != currentSong!!.fullSongName) {
Expand Down Expand Up @@ -118,4 +125,22 @@ class EventProducer : TimerTask() {
)
).execute()
}

private fun sendCalculatorEvent() {
toggleInsertionPoint = !toggleInsertionPoint
waitTicks = Tick.msToTicks(200)
client.sendEvent(
Event(
GAME_NAME,
CALCULATOR_EVENT,
Data(
"${if (toggleInsertionPoint) {calculator.inputDisplay.replace("|", "")} else {calculator.inputDisplay.replace("|", "|")}}:${if (toggleInsertionPoint) {calculator.answerDisplay.replace("|", "")} else {calculator.answerDisplay.replace("|", "|")}}",
Frame(
if (toggleInsertionPoint) {calculator.inputDisplay.replace("|", "")} else {calculator.inputDisplay.replace("|", "|")},
if (toggleInsertionPoint) {calculator.answerDisplay.replace("|", "")} else {calculator.answerDisplay.replace("|", "|")}
)
)
)
).execute()
}
}
Loading