Skip to content

Commit

Permalink
refactor: distribute 'stuff'
Browse files Browse the repository at this point in the history
  • Loading branch information
atennert committed Jan 10, 2021
1 parent 0efea27 commit 6746e42
Show file tree
Hide file tree
Showing 20 changed files with 146 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package de.atennert.lcarsde.statusbar

import de.atennert.lcarsde.statusbar.configuration.WidgetConfiguration
import de.atennert.lcarsde.statusbar.configuration.readConfiguration
import de.atennert.lcarsde.statusbar.extensions.gSignalConnect
import de.atennert.lcarsde.statusbar.extensions.setStyling
import de.atennert.lcarsde.statusbar.widgets.EmptyWidget
import de.atennert.lcarsde.statusbar.widgets.StatusFillerWidget
import de.atennert.lcarsde.statusbar.widgets.StatusWidget
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package de.atennert.lcarsde.statusbar.configuration

import de.atennert.lcarsde.statusbar.toKString
import de.atennert.lcarsde.statusbar.toUByteArray
import de.atennert.lcarsde.statusbar.extensions.toKString
import de.atennert.lcarsde.statusbar.extensions.toUByteArray
import kotlinx.cinterop.*
import kotlinx.cinterop.toKString
import platform.posix.F_OK
import platform.posix.access
import platform.posix.getenv
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package de.atennert.lcarsde.statusbar

const val CELL_SIZE = 40
const val GAP_SIZE = 8

const val LCARSDE_STATUS_BAR = "LCARSDE_STATUS_BAR"

const val STYLE_PATH = "/usr/share/lcarsde/status-bar/style.css"
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package de.atennert.lcarsde.statusbar

import kotlinx.cinterop.*
import statusbar.*

fun executeCommand(command: String) {
val commandParts = command.split(' ')

val byteArgs = commandParts.map { it.encodeToByteArray().pin() }
val convertedArgs = nativeHeap.allocArrayOfPointersTo(byteArgs.map { it.addressOf(0).pointed })
when (fork()) {
-1 -> return
0 -> {
if (setsid() == -1) {
perror("setsid failed")
exit(1)
}

if (execvp(commandParts[0], convertedArgs) == -1) {
perror("execvp failed")
exit(1)
}

exit(0)
}
}
byteArgs.map { it.unpin() }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package de.atennert.lcarsde.statusbar.extensions

import kotlinx.cinterop.CFunction
import kotlinx.cinterop.CPointer
import kotlinx.cinterop.reinterpret
import statusbar.*

fun <F : CFunction<*>> gSignalConnect(obj: CPointer<*>, actionName: String, action: CPointer<F>,
data: gpointer? = null, connect_flags: GConnectFlags = 0u) {
g_signal_connect_data(obj.reinterpret(), actionName, action.reinterpret(), data,
destroy_data = null, connect_flags = connect_flags)
}

fun CPointer<GtkWidget>.setStyling(cssProvider: CPointer<GtkCssProvider>, vararg classes: String) {
val styleContext = gtk_widget_get_style_context(this)
for (cls in classes) {
gtk_style_context_add_class(styleContext, cls)
}
gtk_style_context_add_provider(styleContext, cssProvider.reinterpret(), GTK_STYLE_PROVIDER_PRIORITY_USER)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package de.atennert.lcarsde.statusbar.extensions

import kotlinx.cinterop.*
import kotlinx.cinterop.toKString

/** Convert string to byte array as used in X properties */
fun String.toUByteArray(): UByteArray {
return this.encodeToByteArray().asUByteArray()
}

/** convert this ubyte array pointer to a string */
fun CPointer<UByteVar>.toKString(): String {
val byteString = mutableListOf<Byte>()
var i = 0

while (true) {
val value = this[i]
if (value.convert<Int>() == 0) {
break
}

byteString.add(value.convert())
i++
}
return byteString.toByteArray().toKString()
}

/** convert this ubyte array pointer to a string */
fun CPointer<UByteVar>?.toKString(): String = this?.toKString() ?: ""

/** print a float with a certain amount of places */
fun Float.print(places: Int): String {
val flString = this.toString().split('.', limit = 2)
return "${flString[0]}.${flString[1].take(places)}"
}
20 changes: 20 additions & 0 deletions src/nativeMain/kotlin/de/atennert/lcarsde/statusbar/readFile.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package de.atennert.lcarsde.statusbar

import kotlinx.cinterop.addressOf
import kotlinx.cinterop.toKString
import kotlinx.cinterop.usePinned

fun readFile(path: String): String? {
platform.posix.fopen(path, "r")?. let { fp ->
var s = ""
val buf = ByteArray(1000)
buf.usePinned {
while (platform.posix.fgets(it.addressOf(0), 1000, fp) != null) {
s += it.get().toKString()
}
}
platform.posix.fclose(fp)
return s.trim()
}
return null
}
95 changes: 0 additions & 95 deletions src/nativeMain/kotlin/de/atennert/lcarsde/statusbar/stuff.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package de.atennert.lcarsde.statusbar.widgets

import de.atennert.lcarsde.statusbar.*
import de.atennert.lcarsde.statusbar.CELL_SIZE
import de.atennert.lcarsde.statusbar.GAP_SIZE
import de.atennert.lcarsde.statusbar.configuration.WidgetConfiguration
import de.atennert.lcarsde.statusbar.executeCommand
import de.atennert.lcarsde.statusbar.extensions.gSignalConnect
import kotlinx.cinterop.*
import statusbar.*

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package de.atennert.lcarsde.statusbar.widgets

import de.atennert.lcarsde.statusbar.configuration.WidgetConfiguration
import de.atennert.lcarsde.statusbar.gSignalConnect
import de.atennert.lcarsde.statusbar.extensions.gSignalConnect
import de.atennert.lcarsde.statusbar.readFile
import kotlinx.cinterop.*
import statusbar.*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package de.atennert.lcarsde.statusbar.widgets

import de.atennert.lcarsde.statusbar.configuration.WidgetConfiguration
import de.atennert.lcarsde.statusbar.executeCommand
import de.atennert.lcarsde.statusbar.gSignalConnect
import de.atennert.lcarsde.statusbar.setStyling
import de.atennert.lcarsde.statusbar.extensions.gSignalConnect
import de.atennert.lcarsde.statusbar.extensions.setStyling
import kotlinx.cinterop.*
import statusbar.*

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import kotlinx.cinterop.CPointer
import statusbar.GtkCssProvider
import statusbar.cairo_t

class CpuUsageWidget(widgetConfiguration: WidgetConfiguration, cssProvider: CPointer<GtkCssProvider>)
: RadarGraphWidget(widgetConfiguration, cssProvider, 500, 100) {
class CpuUsageWidget(widgetConfiguration: WidgetConfiguration, cssProvider: CPointer<GtkCssProvider>) :
RadarGraphWidget(widgetConfiguration, cssProvider, 500, 100) {

private val attentionFreq = 60
private val warningFreq = 80
Expand Down Expand Up @@ -37,14 +37,14 @@ class CpuUsageWidget(widgetConfiguration: WidgetConfiguration, cssProvider: CPoi
private fun getCpuUtilization(): List<Double> {
val statData = readFile("/proc/stat")?.lines() ?: return emptyList()
val cpuData = statData
.filter { it.startsWith("cpu") }
.drop(1)
.map {
it.trim()
.split(' ')
.drop(1)
.map(String::toFloat)
}
.filter { it.startsWith("cpu") }
.drop(1)
.map {
it.trim()
.split(' ')
.drop(1)
.map(String::toFloat)
}
val idlesTotals = cpuData.map { Pair(it[3] + it[4], it.sum()) }

val deltas = if (lastIdlesTotals.isEmpty()) {
Expand All @@ -54,7 +54,8 @@ class CpuUsageWidget(widgetConfiguration: WidgetConfiguration, cssProvider: CPoi
}
lastIdlesTotals = idlesTotals

return deltas.map { (idle, total) -> try {
return deltas.map { (idle, total) ->
try {
100 * (1.0 - idle / total)
} catch (e: ArithmeticException) {
100 * (1.0 - idle / 0.001)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package de.atennert.lcarsde.statusbar.widgets

import de.atennert.lcarsde.statusbar.configuration.WidgetConfiguration
import de.atennert.lcarsde.statusbar.setStyling
import de.atennert.lcarsde.statusbar.extensions.setStyling
import kotlinx.cinterop.CPointer
import statusbar.GtkCssProvider
import statusbar.gtk_label_new
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package de.atennert.lcarsde.statusbar.widgets

import de.atennert.lcarsde.statusbar.configuration.WidgetConfiguration
import de.atennert.lcarsde.statusbar.gSignalConnect
import de.atennert.lcarsde.statusbar.extensions.gSignalConnect
import de.atennert.lcarsde.statusbar.readFile
import kotlinx.cinterop.*
import statusbar.*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package de.atennert.lcarsde.statusbar.widgets

import de.atennert.lcarsde.statusbar.configuration.WidgetConfiguration
import de.atennert.lcarsde.statusbar.gSignalConnect
import de.atennert.lcarsde.statusbar.extensions.gSignalConnect
import kotlinx.cinterop.*
import statusbar.*
import kotlin.math.PI
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package de.atennert.lcarsde.statusbar.widgets

import de.atennert.lcarsde.statusbar.configuration.WidgetConfiguration
import de.atennert.lcarsde.statusbar.print
import de.atennert.lcarsde.statusbar.extensions.print
import kotlinx.cinterop.CPointer
import statusbar.*

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package de.atennert.lcarsde.statusbar.widgets

import de.atennert.lcarsde.statusbar.configuration.WidgetConfiguration
import de.atennert.lcarsde.statusbar.setStyling
import de.atennert.lcarsde.statusbar.extensions.setStyling
import kotlinx.cinterop.CPointer
import kotlinx.cinterop.reinterpret
import statusbar.*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package de.atennert.lcarsde.statusbar.widgets

import de.atennert.lcarsde.statusbar.configuration.WidgetConfiguration
import de.atennert.lcarsde.statusbar.gSignalConnect
import de.atennert.lcarsde.statusbar.extensions.gSignalConnect
import kotlinx.cinterop.*
import statusbar.*

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package de.atennert.lcarsde.statusbar.widgets

import de.atennert.lcarsde.statusbar.configuration.WidgetConfiguration
import de.atennert.lcarsde.statusbar.gSignalConnect
import de.atennert.lcarsde.statusbar.extensions.gSignalConnect
import de.atennert.lcarsde.statusbar.readFile
import kotlinx.cinterop.*
import statusbar.*
import kotlin.math.PI
import kotlin.math.max

class WifiWidget(widgetConfiguration: WidgetConfiguration, cssProvider: CPointer<GtkCssProvider>) :
StatusWidget(widgetConfiguration, cssProvider, 1000) {
Expand Down
7 changes: 5 additions & 2 deletions src/nativeMain/kotlin/main.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import de.atennert.lcarsde.statusbar.StatusBar
import de.atennert.lcarsde.statusbar.gSignalConnect
import de.atennert.lcarsde.statusbar.extensions.gSignalConnect
import kotlinx.cinterop.cValue
import kotlinx.cinterop.cValuesOf
import kotlinx.cinterop.staticCFunction
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import statusbar.*
import statusbar.gtk_events_pending
import statusbar.gtk_init
import statusbar.gtk_main_iteration
import statusbar.gtk_widget_show_all

var stop = false

Expand Down

0 comments on commit 6746e42

Please sign in to comment.