Browse Source

wrap cstring to nim string conversion in a proc

master
Wesley Kerfoot 4 years ago
parent
commit
dd2528f7f5
  1. 35
      src/nimwin.nim

35
src/nimwin.nim

@ -13,6 +13,12 @@ proc handleBadWindow(display : PDisplay, ev : PXErrorEvent) : cint {.cdecl.} =
proc handleIOError(display : PDisplay) : cint {.cdecl.} = proc handleIOError(display : PDisplay) : cint {.cdecl.} =
0 0
proc cstringToNim(cst : cstring) : Option[string] =
var nst = newString(cst.len)
if nst.len > 0:
copyMem(addr(nst[0]), cst, cst.len)
return some(nst)
none(string)
template HandleKey(key : TKeySym, body : untyped) : untyped = template HandleKey(key : TKeySym, body : untyped) : untyped =
block: block:
@ -79,8 +85,11 @@ proc getPropertyValue(display : PDisplay, window : TWindow, property : TAtom) :
var propValue : ptr cuchar var propValue : ptr cuchar
var currentAtomName = display.XGetAtomName(property) var currentAtomName = display.XGetAtomName(property)
var atomName = newString(currentAtomName.len) var atomName = cstringToNim(currentAtomName)
copyMem(addr(atomName[0]), currentAtomName, currentAtomName.len)
if atomName.isNone:
quit(fmt"Could not allocate atomName for some reason")
discard currentAtomName.XFree discard currentAtomName.XFree
discard display.XGetWindowProperty(window, discard display.XGetWindowProperty(window,
@ -95,19 +104,22 @@ proc getPropertyValue(display : PDisplay, window : TWindow, property : TAtom) :
bytesAfterReturn.addr, bytesAfterReturn.addr,
propValue.addr) propValue.addr)
if actualTypeFormat == 0:
# Invalid type
return none(WinProp)
let typeName = display.XGetAtomName(actualType) let typeName = display.XGetAtomName(actualType)
if typeName == "STRING": if typeName == "STRING":
var propStrValue = newString(propValue.len) var propStrValue = cstringToNim(propValue)
if propStrValue.len > 0: if propStrValue.isSome:
copyMem(addr(propStrValue[0]), propValue, propValue.len) result = some(WinProp(name: atomName.get, kind: pkString, strProp: propStrValue.get))
result = some(WinProp(name: atomName, kind: pkString, strProp: propStrValue))
else: else:
result = none(WinProp) result = none(WinProp)
elif typeName == "CARDINAL": elif typeName == "CARDINAL":
result = some( result = some(
WinProp( WinProp(
name: atomName, name: atomName.get,
kind: pkCardinal, kind: pkCardinal,
cardinalProp: unpackPropValue(actualTypeFormat.int, nItemsReturn.int, propValue) cardinalProp: unpackPropValue(actualTypeFormat.int, nItemsReturn.int, propValue)
) )
@ -118,16 +130,15 @@ proc getPropertyValue(display : PDisplay, window : TWindow, property : TAtom) :
for atom in unpackPropValue(actualTypeFormat.int, nItemsReturn.int, propValue): for atom in unpackPropValue(actualTypeFormat.int, nItemsReturn.int, propValue):
let atomPropNameCS = display.XGetAtomName(atom.culong) let atomPropNameCS = display.XGetAtomName(atom.culong)
var atomPropName = newString(atomPropNameCS.len) var atomPropName = cstringToNim(atomPropNameCS)
if atomPropName.len > 0: if atomPropName.isSome:
copyMem(addr(atomPropName[0]), atomPropNameCS, atomPropNameCS.len) atomPropNames &= atomPropName.get
atomPropNames &= atomPropName
discard atomPropNameCS.XFree discard atomPropNameCS.XFree
result = some( result = some(
WinProp( WinProp(
name: atomName, name: atomName.get,
kind: pkAtom, kind: pkAtom,
atomProps: atomPropNames atomProps: atomPropNames
) )

Loading…
Cancel
Save