Skip to content

Commit

Permalink
🐛 Fixed an issue that caused repeated synchronization of the pasteboa…
Browse files Browse the repository at this point in the history
…rd after restarting (#763)
  • Loading branch information
guiyanakuang authored Apr 19, 2024
1 parent f12feee commit eae62d4
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.clipevery.clip

import com.clipevery.config.ConfigManager
import com.clipevery.dao.clip.ClipDao
import com.clipevery.dao.clip.ClipData
import io.github.oshai.kotlinlogging.KLogger
Expand All @@ -20,6 +21,8 @@ interface ClipboardService : ClipboardMonitor, ClipboardOwner {

val clipDao: ClipDao

val configManager: ConfigManager

val clipConsumer: TransferableConsumer

val clipProducer: TransferableProducer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ data class AppConfig(
val fileCleanTimeIndex: Int = 6,
val isAllowDiscovery: Boolean = true,
val blacklist: String = "[]",
val lastClipboardChangeCount: Int = -1,
) {

constructor(other: AppConfig, appEnv: AppEnv) : this(
Expand All @@ -34,5 +35,6 @@ data class AppConfig(
fileCleanTimeIndex = other.fileCleanTimeIndex,
isAllowDiscovery = other.isAllowDiscovery,
blacklist = other.blacklist,
lastClipboardChangeCount = other.lastClipboardChangeCount,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ object Dependencies {
single<SignalProtocolStore> { DesktopSignalProtocolStore(get(), get(), get(), get()) }

// clip component
single<ClipboardService> { getDesktopClipboardService(get(), get(), get()) }
single<ClipboardService> { getDesktopClipboardService(get(), get(), get(), get()) }
single<TransferableConsumer> {
DesktopTransferableConsumer(
get(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package com.clipevery.clip

import com.clipevery.config.ConfigManager
import com.clipevery.dao.clip.ClipDao
import com.clipevery.os.macos.MacosClipboardService
import com.clipevery.os.windows.WindowsClipboardService
import com.clipevery.platform.currentPlatform

fun getDesktopClipboardService(
clipDao: ClipDao,
configManager: ConfigManager,
clipConsumer: TransferableConsumer,
clipProducer: TransferableProducer,
): ClipboardService {
val currentPlatform = currentPlatform()
return if (currentPlatform.isMacos()) {
MacosClipboardService(clipDao, clipConsumer, clipProducer)
MacosClipboardService(clipDao, configManager, clipConsumer, clipProducer)
} else if (currentPlatform.isWindows()) {
WindowsClipboardService(clipDao, clipConsumer, clipProducer)
WindowsClipboardService(clipDao, configManager, clipConsumer, clipProducer)
} else {
throw Exception("Unsupported platform: ${currentPlatform.name}")
}
Expand Down
1 change: 1 addition & 0 deletions composeApp/src/desktopMain/kotlin/com/clipevery/main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ fun initInject(koinApplication: KoinApplication) {

fun exitClipEveryApplication(exitApplication: () -> Unit) {
val koinApplication = Dependencies.koinApplication
koinApplication.koin.get<ClipboardService>().stop()
koinApplication.koin.get<ClipSearchService>().stop()
koinApplication.koin.get<ClipBonjourService>().unregisterService()
koinApplication.koin.get<ClipServer>().stop()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.clipevery.os.macos
import com.clipevery.clip.ClipboardService
import com.clipevery.clip.TransferableConsumer
import com.clipevery.clip.TransferableProducer
import com.clipevery.config.ConfigManager
import com.clipevery.dao.clip.ClipDao
import com.clipevery.os.macos.api.MacosApi
import com.clipevery.utils.cpuDispatcher
Expand All @@ -23,12 +24,13 @@ import java.awt.datatransfer.Transferable

class MacosClipboardService(
override val clipDao: ClipDao,
override val configManager: ConfigManager,
override val clipConsumer: TransferableConsumer,
override val clipProducer: TransferableProducer,
) : ClipboardService {
override val logger: KLogger = KotlinLogging.logger {}

private var changeCount = 0
private var changeCount = configManager.config.lastClipboardChangeCount

@Volatile
override var owner = false
Expand Down Expand Up @@ -61,6 +63,7 @@ class MacosClipboardService(
MacosApi.INSTANCE.getClipboardChangeCount(changeCount, isRemote, isClipevery)
.let { currentChangeCount ->
if (changeCount != currentChangeCount) {
logger.info { "currentChangeCount $currentChangeCount changeCount $changeCount" }
changeCount = currentChangeCount
if (isClipevery.value != 0) {
logger.debug { "Ignoring clipevery change" }
Expand Down Expand Up @@ -100,5 +103,6 @@ class MacosClipboardService(

override fun stop() {
job?.cancel()
configManager.updateConfig { it.copy(lastClipboardChangeCount = changeCount) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.clipevery.os.windows
import com.clipevery.clip.ClipboardService
import com.clipevery.clip.TransferableConsumer
import com.clipevery.clip.TransferableProducer
import com.clipevery.config.ConfigManager
import com.clipevery.dao.clip.ClipDao
import com.clipevery.os.windows.api.User32
import com.clipevery.platform.currentPlatform
Expand All @@ -29,6 +30,7 @@ import kotlin.math.min

class WindowsClipboardService(
override val clipDao: ClipDao,
override val configManager: ConfigManager,
override val clipConsumer: TransferableConsumer,
override val clipProducer: TransferableProducer,
) : ClipboardService, User32.WNDPROC {
Expand All @@ -37,6 +39,8 @@ class WindowsClipboardService(
@Volatile
private var existNew = false

private var changeCount = configManager.config.lastClipboardChangeCount

@Volatile
override var owner = false

Expand Down Expand Up @@ -138,6 +142,7 @@ class WindowsClipboardService(
override fun stop() {
Kernel32.INSTANCE.SetEvent(event)
job?.cancel()
configManager.updateConfig { it.copy(lastClipboardChangeCount = changeCount) }
}

private fun onChange() {
Expand Down Expand Up @@ -195,7 +200,11 @@ class WindowsClipboardService(
if (existNew) {
existNew = false
try {
onChange()
val clipboardSequenceNumber = User32.INSTANCE.GetClipboardSequenceNumber()
if (changeCount != clipboardSequenceNumber) {
changeCount = clipboardSequenceNumber
onChange()
}
} finally {
User32.INSTANCE.SendMessage(nextViewer, uMsg, uParam, lParam)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ interface User32 : StdCallLibrary {
dwData: LPARAM?,
): Boolean

fun GetClipboardSequenceNumber(): Int

companion object {
val INSTANCE =
Native.load(
Expand Down

0 comments on commit eae62d4

Please sign in to comment.