Skip to content

Commit

Permalink
refactor: switch to dynamic generation of XFT colors
Browse files Browse the repository at this point in the history
  • Loading branch information
atennert committed Apr 11, 2021
1 parent d92bd1f commit a62fee5
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 49 deletions.
12 changes: 0 additions & 12 deletions src/nativeMain/kotlin/de/atennert/lcarswm/drawing/ColorFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,6 @@ class ColorFactory(private val drawApi: DrawApi, screen: Screen) {
drawApi.createGC(window, mask.convert(), gcValues.ptr)!!
}

fun getXftColor(colorIndex: Int): XftColor {
val color = nativeHeap.alloc<XftColor>()
val colorCode = colors[colorIndex]
color.color.red = colorCode.first.convert()
color.color.green = colorCode.second.convert()
color.color.blue = colorCode.third.convert()
color.color.alpha = 0xffff.convert()
color.pixel = colorPixels[colorIndex]
return color
}

fun createColorGC(drawable: Drawable, color: Color): GC? {

val pixel = knownColors[color]
Expand Down Expand Up @@ -136,7 +125,6 @@ class ColorFactory(private val drawApi: DrawApi, screen: Screen) {
xftColor.color.alpha = 0xffff.convert()
xftColor.pixel = pixel
knownXftColors[color] = xftColor

xftColor
}
}
Expand Down
30 changes: 9 additions & 21 deletions src/nativeMain/kotlin/de/atennert/lcarswm/drawing/FrameDrawer.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package de.atennert.lcarswm.drawing

import de.atennert.lcarswm.*
import de.atennert.lcarswm.lifecycle.closeWith
import de.atennert.lcarswm.monitor.Monitor
import de.atennert.lcarswm.system.api.DrawApi
import de.atennert.lcarswm.system.api.FontApi
Expand All @@ -25,18 +24,15 @@ class FrameDrawer(
private val screen: Screen
) : IFrameDrawer {

private val activeTextColor = colorFactory.getXftColor(1)
private val inactiveTextColor = colorFactory.getXftColor(4)
private val primaryBarColor = colorFactory.getXftColor(2)
private val secondaryBarColor = colorFactory.getXftColor(6)
private val backgroundColor = colorFactory.getXftColor(0)
private val activeTextColor = colorFactory.createXftColor(COLOR_ACTIVE_TITLE)
private val inactiveTextColor = colorFactory.createXftColor(COLOR_INACTIVE_TITLE)
private val maxBarColor = colorFactory.createXftColor(COLOR_MAX_BAR_DOWN)
private val normalBarColor = colorFactory.createXftColor(COLOR_NORMAL_BAR_DOWN)
private val normalCornerDownColor = colorFactory.createXftColor(COLOR_NORMAL_CORNER_4)
private val backgroundColor = colorFactory.createXftColor(COLOR_BACKGROUND)
override val colorMap: Colormap
get() = colorFactory.colorMapId

init {
closeWith(FrameDrawer::close)
}

override fun drawFrame(window: FramedWindow, monitor: Monitor) {
val windowMeasurements = monitor.getWindowMeasurements()
val textW = if (monitor.getScreenMode() == ScreenMode.NORMAL) {
Expand Down Expand Up @@ -74,11 +70,11 @@ class FrameDrawer(
if (monitor.getScreenMode() == ScreenMode.NORMAL) {
val primBarWidth = LOWER_CORNER_WIDTH - NORMAL_WINDOW_LEFT_OFFSET
val secBarWidth = textX - primBarWidth - 2 * BAR_GAP_SIZE + FIRST_LETTER_OFFSET
drawApi.xftDrawRect(xftDraw, primaryBarColor.ptr, 0, TITLE_BAR_OFFSET, primBarWidth.convert(), BAR_HEIGHT.convert())
drawApi.xftDrawRect(xftDraw, secondaryBarColor.ptr, primBarWidth + BAR_GAP_SIZE, TITLE_BAR_OFFSET, secBarWidth.convert(), BAR_HEIGHT.convert())
drawApi.xftDrawRect(xftDraw, normalCornerDownColor.ptr, 0, TITLE_BAR_OFFSET, primBarWidth.convert(), BAR_HEIGHT.convert())
drawApi.xftDrawRect(xftDraw, normalBarColor.ptr, primBarWidth + BAR_GAP_SIZE, TITLE_BAR_OFFSET, secBarWidth.convert(), BAR_HEIGHT.convert())
} else {
val barWidth = textX - BAR_GAP_SIZE + FIRST_LETTER_OFFSET
drawApi.xftDrawRect(xftDraw, primaryBarColor.ptr, 0, TITLE_BAR_OFFSET, barWidth.convert(), BAR_HEIGHT.convert())
drawApi.xftDrawRect(xftDraw, maxBarColor.ptr, 0, TITLE_BAR_OFFSET, barWidth.convert(), BAR_HEIGHT.convert())
}

drawApi.setWindowBackgroundPixmap(window.titleBar, pixmap)
Expand All @@ -87,14 +83,6 @@ class FrameDrawer(
nativeHeap.free(rect.rawPtr)
}

private fun close() {
nativeHeap.free(activeTextColor.rawPtr)
nativeHeap.free(inactiveTextColor.rawPtr)
nativeHeap.free(primaryBarColor.rawPtr)
nativeHeap.free(secondaryBarColor.rawPtr)
nativeHeap.free(backgroundColor.rawPtr)
}

companion object {
private const val FIRST_LETTER_OFFSET = 2
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class RootWindowDrawer(
private val logoImage: CPointer<XImage>?
private val logoText: String

private val logoColor = colorFactory.getXftColor(1)
private val logoColor = colorFactory.createXftColor(COLOR_LOGO)

private val colorMap: Colormap
get() = colorFactory.colorMapId
Expand Down Expand Up @@ -63,7 +63,6 @@ class RootWindowDrawer(

private fun close() {
graphicsContexts.forEach { drawApi.freeGC(it) }
nativeHeap.free(logoColor.rawPtr)
}

private fun drawLogoTextFront(pixmap: Pixmap, x: Int, y: Int, barWidth: Int) {
Expand Down
34 changes: 34 additions & 0 deletions src/nativeMain/kotlin/de/atennert/lcarswm/globalConstants.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.atennert.lcarswm

import de.atennert.lcarswm.drawing.Color
import de.atennert.lcarswm.keys.Modifiers
import xlib.*

Expand Down Expand Up @@ -50,3 +51,36 @@ const val LOWER_CORNER_WIDTH = OUTER_CORNER_RADIUS_BIG + 272

const val WINDOW_TITLE_FONT_SIZE = BAR_HEIGHT
const val WINDOW_TITLE_OFFSET = 30

// Base colors
val BLACK = Color(0, 0, 0)
val YELLOW = Color(0xFFFF, 0x9999, 0)
val ORCHID = Color(0xCCCC, 0x9999, 0xCCCC)
val DAMPENED_PURPLE = Color(0x9999, 0x9999, 0xCCCC)
val DARK_RED = Color(0xCCCC, 0x6666, 0x6666)
val SAND = Color(0xFFFF, 0xCCCC, 0x9999)
val BRIGHT_PURPLE = Color(0x9999, 0x9999, 0xFFFF)
val ORANGE = Color(0xFFFF, 0x9999, 0x6666)
val DARK_PINK = Color(0xCCCC, 0x6666, 0x9999)

val COLOR_LOGO = YELLOW
val COLOR_ACTIVE_TITLE = YELLOW
val COLOR_INACTIVE_TITLE = DARK_RED
val COLOR_LINE_ENDS = BRIGHT_PURPLE
val COLOR_BACKGROUND = BLACK

val COLOR_NORMAL_BAR_UP = BRIGHT_PURPLE
val COLOR_NORMAL_BAR_DOWN = BRIGHT_PURPLE
val COLOR_NORMAL_SIDEBAR_UP = DAMPENED_PURPLE
val COLOR_NORMAL_SIDEBAR_DOWN = DAMPENED_PURPLE
val COLOR_NORMAL_BAR_MIDDLE_1 = DAMPENED_PURPLE
val COLOR_NORMAL_BAR_MIDDLE_2 = DARK_RED
val COLOR_NORMAL_BAR_MIDDLE_3 = BRIGHT_PURPLE
val COLOR_NORMAL_BAR_MIDDLE_4 = ORCHID
val COLOR_NORMAL_CORNER_1 = ORCHID
val COLOR_NORMAL_CORNER_2 = ORCHID
val COLOR_NORMAL_CORNER_3 = ORCHID
val COLOR_NORMAL_CORNER_4 = ORCHID

val COLOR_MAX_BAR_UP = ORCHID
val COLOR_MAX_BAR_DOWN = ORCHID
2 changes: 2 additions & 0 deletions src/nativeTest/kotlin/ShutdownTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ class ShutdownTest {
checkClosingOfAppMenuMessageQueues(functionCalls)
checkFreeingOfGraphicsContexts(functionCalls)
checkFreeingOfColors(functionCalls)
checkFreeingOfColors(functionCalls)
checkFreeingOfColorMap(functionCalls)
checkWindowPropertyRemoval(functionCalls, testFacade.atomMap, "_NET_SUPPORTED")
checkSelectInputSetting(functionCalls, NoEventMask)
Expand Down Expand Up @@ -403,6 +404,7 @@ class ShutdownTest {
checkClosingOfAppMenuMessageQueues(functionCalls)
checkFreeingOfGraphicsContexts(functionCalls)
checkFreeingOfColors(functionCalls)
checkFreeingOfColors(functionCalls)
checkFreeingOfColorMap(functionCalls)
checkWindowPropertyRemoval(functionCalls, testFacade.atomMap, "_NET_SUPPORTED")
checkSelectInputSetting(functionCalls, NoEventMask)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.atennert.lcarswm.drawing

import de.atennert.lcarswm.lifecycle.closeClosables
import de.atennert.lcarswm.system.DrawApiDummy
import kotlinx.cinterop.*
import xlib.*
Expand All @@ -10,6 +11,7 @@ class ColorFactoryTest {
@AfterTest
fun teardown() {
drawApi.clear()
closeClosables()
}

@Test
Expand All @@ -21,8 +23,6 @@ class ColorFactoryTest {

assertNotNull(gc)
assertEquals(1.toULong(), drawApi.pixel)

nativeHeap.free(gc)
}

@Test
Expand All @@ -37,9 +37,6 @@ class ColorFactoryTest {
assertNotNull(gc1)
assertNotNull(gc2)
assertEquals(2.toULong(), drawApi.pixel)

nativeHeap.free(gc1)
nativeHeap.free(gc2)
}

@Test
Expand All @@ -53,8 +50,6 @@ class ColorFactoryTest {
assertNotNull(gc1)
assertEquals(gc1, gc2)
assertEquals(1.toULong(), drawApi.pixel)

nativeHeap.free(gc1)
}

@Test
Expand All @@ -69,9 +64,6 @@ class ColorFactoryTest {
assertNotNull(gc2)
assertNotSame(gc1, gc2)
assertEquals(1.toULong(), drawApi.pixel)

nativeHeap.free(gc1)
nativeHeap.free(gc2)
}

@Test
Expand All @@ -85,8 +77,6 @@ class ColorFactoryTest {
assertEquals(color.green, xftColor.color.green.toInt())
assertEquals(color.blue, xftColor.color.blue.toInt())
assertEquals(drawApi.pixel, xftColor.pixel)

nativeHeap.free(xftColor)
}

@Test
Expand All @@ -98,8 +88,6 @@ class ColorFactoryTest {
val xftColor2 = colorFactory.createXftColor(color)

assertEquals(xftColor1.rawPtr, xftColor2.rawPtr)

nativeHeap.free(xftColor1)
}

private val drawApi = object : DrawApiDummy() {
Expand All @@ -123,6 +111,15 @@ class ColorFactoryTest {
override fun createGC(drawable: Drawable, mask: ULong, gcValues: CValuesRef<XGCValues>?): GC? {
return nativeHeap.alloc<GCVar>().ptr.reinterpret()
}

override fun freeColormap(colorMap: Colormap): Int = 1

override fun freeGC(graphicsContext: GC): Int {
nativeHeap.free(graphicsContext)
return 1
}

override fun freeColors(colorMap: Colormap, pixels: CValuesRef<ULongVar>, pixelCount: Int): Int = 1
}

@ThreadLocal
Expand Down

1 comment on commit a62fee5

@atennert
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prepares for #8

Please sign in to comment.