Skip to content

Commit 850e7f2

Browse files
committed
#496 - windows sleep prevention
1 parent 8d4c2a2 commit 850e7f2

File tree

10 files changed

+74
-11
lines changed

10 files changed

+74
-11
lines changed

domain/src/androidMain/kotlin/uk/co/sentinelweb/cuer/app/db/repository/file/PlatformFileOperation.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import kotlinx.datetime.Instant
44
import java.io.File
55

66
actual class PlatformFileOperation {
7+
actual var separator: String = File.separator
78

89
actual fun delete(file: AFile) {
910
val platformFile = File(file.path)

domain/src/commonMain/kotlin/uk/co/sentinelweb/cuer/app/db/repository/file/PlatformFileOperation.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ data class AFileProperties(
1818
)
1919

2020
expect class PlatformFileOperation() {
21+
var separator: String
2122
fun delete(file: AFile)
2223

2324
fun currentDir(): AFile

domain/src/iosMain/kotlin/uk/co/sentinelweb/cuer/app/db/repository/file/PlatformFileOperation.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package uk.co.sentinelweb.cuer.app.db.repository.file
22

33
actual class PlatformFileOperation {
4+
actual var separator: String = "/"
45
actual fun delete(file: AFile) {
56
TODO("Not yet implemented")
67
}

domain/src/jsMain/kotlin/uk/co/sentinelweb/cuer/app/db/repository/file/PlatformFileOperation.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package uk.co.sentinelweb.cuer.app.db.repository.file
22

33
actual class PlatformFileOperation {
4+
actual var separator: String = "/"
5+
46
actual fun delete(file: AFile) {
57
TODO("Not yet implemented")
68
}

domain/src/jvmMain/kotlin/uk/co/sentinelweb/cuer/app/db/repository/file/PlatformFileOperation.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import java.nio.file.Paths
99
import java.nio.file.attribute.BasicFileAttributes
1010

1111
actual class PlatformFileOperation: KoinComponent {
12-
12+
actual var separator: String = File.separator
1313
actual fun delete(file: AFile) {
1414
val jvmFile = File(file.path)
1515
if (jvmFile.exists()) {

hub/src/main/kotlin/uk/co/sentinelweb/cuer/hub/di/Modules.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ import uk.co.sentinelweb.cuer.hub.util.share.scan.TodoLinkScanner
6666
import uk.co.sentinelweb.cuer.hub.util.sleep.SleepPreventer
6767
import uk.co.sentinelweb.cuer.hub.util.sleep.SleepPreventerEmpty
6868
import uk.co.sentinelweb.cuer.hub.util.sleep.SleepPreventerLinuxDBus
69+
import uk.co.sentinelweb.cuer.hub.util.sleep.WindowsSleepPreventer
6970
import uk.co.sentinelweb.cuer.hub.util.system_tray.SystemTrayComposePopup
7071
import uk.co.sentinelweb.cuer.hub.util.system_tray.SystemTrayIcon
7172
import uk.co.sentinelweb.cuer.hub.util.wrapper.EmptyVibrateWrapper
@@ -123,11 +124,11 @@ object Modules {
123124
when (getOS()) {
124125
NodeDomain.DeviceType.MAC -> SleepPreventerMac()
125126
NodeDomain.DeviceType.LINUX -> SleepPreventerLinuxDBus()
127+
NodeDomain.DeviceType.WINDOWS -> WindowsSleepPreventer()
126128
else -> SleepPreventerEmpty()
127129
// NodeDomain.DeviceType.ANDROID -> TODO()
128130
// NodeDomain.DeviceType.IOS -> TODO()
129131
// NodeDomain.DeviceType.WEB -> TODO()
130-
// NodeDomain.DeviceType.WINDOWS -> TODO()
131132
// NodeDomain.DeviceType.OTHER -> TODO()
132133
}
133134
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package uk.co.sentinelweb.cuer.hub.util.sleep
2+
3+
import com.sun.jna.Native
4+
import com.sun.jna.Structure
5+
import com.sun.jna.platform.win32.WinDef.DWORD
6+
import com.sun.jna.win32.StdCallLibrary
7+
8+
class WindowsSleepPreventer : SleepPreventer {
9+
private interface Kernel32Extended : StdCallLibrary {
10+
companion object {
11+
val INSTANCE = Native.load("kernel32", Kernel32Extended::class.java)
12+
}
13+
14+
fun SetThreadExecutionState(esFlags: EXECUTION_STATE): EXECUTION_STATE
15+
}
16+
17+
@Structure.FieldOrder("value")
18+
class EXECUTION_STATE(var value: DWORD) : DWORD(value.toLong()) {
19+
override fun toShort(): Short = value.toShort()
20+
override fun toByte(): Byte = value.toByte()
21+
}
22+
23+
private companion object {
24+
private val ES_CONTINUOUS = EXECUTION_STATE(DWORD(0x80000000L))
25+
private val ES_SYSTEM_REQUIRED = EXECUTION_STATE(DWORD(0x00000001L))
26+
private val ES_DISPLAY_REQUIRED = EXECUTION_STATE(DWORD(0x00000002L))
27+
}
28+
29+
override fun preventSleep() {
30+
try {
31+
val flags = DWORD(
32+
ES_CONTINUOUS.toLong() or
33+
ES_SYSTEM_REQUIRED.toLong() or
34+
ES_DISPLAY_REQUIRED.toLong()
35+
)
36+
37+
// Create an EXECUTION_STATE from the DWORD for the API call
38+
val executionState = EXECUTION_STATE(flags)
39+
Kernel32Extended.INSTANCE.SetThreadExecutionState(executionState)
40+
} catch (e: Exception) {
41+
// Consider logging the error or throwing a custom exception
42+
e.printStackTrace()
43+
}
44+
}
45+
46+
override fun allowSleep() {
47+
try {
48+
Kernel32Extended.INSTANCE.SetThreadExecutionState(ES_CONTINUOUS)
49+
} catch (e: Exception) {
50+
// Consider logging the error or throwing a custom exception
51+
e.printStackTrace()
52+
}
53+
}
54+
}

hub/src/main/resources/db/default-dbinit.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
},
1616
"config": {
1717
"deletable": false
18-
}
18+
},
19+
"type":"USER"
1920
},
2021
{
2122
"id": {

shared/src/commonMain/kotlin/uk/co/sentinelweb/cuer/app/di/SharedAppModule.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ object SharedAppModule {
371371
factory<FullTruncatedPathMapper> {
372372
DefaultFullTruncatedPathMapper(
373373
prefs = get(),
374+
fileOps = get(),
374375
log = get()
375376
)
376377
}
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package uk.co.sentinelweb.cuer.app.util.disk
22

3+
import uk.co.sentinelweb.cuer.app.db.repository.file.PlatformFileOperation
34
import uk.co.sentinelweb.cuer.app.util.prefs.multiplatfom_settings.MultiPlatformPreferencesWrapper
45
import uk.co.sentinelweb.cuer.core.wrapper.LogWrapper
56

67
class DefaultFullTruncatedPathMapper(
78
private val prefs: MultiPlatformPreferencesWrapper,
9+
private val fileOps: PlatformFileOperation,
810
private val log: LogWrapper
911
) : FullTruncatedPathMapper {
1012

@@ -18,15 +20,14 @@ class DefaultFullTruncatedPathMapper(
1820
override fun fullToTruncatedFolderPath(path: String): String? =
1921
prefs.folderRoots
2022
.find { path.startsWith(it) }
21-
?.let { path.replace(it, it.substringAfterLast("/")) }
23+
?.let { path.replace(it, it.substringAfterLast(fileOps.separator, it)) }
2224

2325
override fun truncatedToFullFolderPath(path: String): String? =
2426
prefs.folderRoots
25-
.find { root -> path.startsWith(root.substringAfterLast("/")) }
26-
?.let { root -> path.replace(root.substringAfterLast("/"), root) }
27-
// allow already full path though
28-
?: prefs.folderRoots
29-
.find { root -> path.startsWith(root) }
30-
?.let { path }
31-
27+
.find { path.startsWith(it.substringAfterLast(fileOps.separator)) }
28+
?.let { path.replace(it.substringAfterLast(fileOps.separator), it) }
29+
// allow already full path though
30+
// ?: prefs.folderRoots
31+
// .find { root -> path.startsWith(root) }
32+
// ?.let { path }
3233
}

0 commit comments

Comments
 (0)