Trying out some XCB in kotlin with cinterop, and i...
# kotlin-native
p
Trying out some XCB in kotlin with cinterop, and i am struggling to set a window atom.
Copy code
xcb_change_property(
    this.connection,
    XCB_PROP_MODE_REPLACE.toUByte(),
    window,
    windowTypeReply.pointed.atom,
    XCB_ATOM_ATOM,
    32,
    2,
    windowTypeDockReply.pointed.atom // THIS needs to be a CValuesRef
)
The
windowTypeDockReply
is of the type
CPointer<xcb_intern_atom_reply_t>
, which has a property
atom
of the type
xcb_atom_t
which can be accessed through the pointed property. The function
xcb_change_property
in c needs a
xcb_atom_t
as its last parameter. In Kotlin however it requires a
kotlinx.cinterop.CValuesRef<*>?
. So i am wondering how i can make
windowTypeDockReply.pointed.atom
into a
CValuesRef
?
m
last parameter in this function is
void *
, so probably .
pointed.atom.ptr
p
tried that before, as .ptr should be the reverse of .pointed, but .atom does not have .ptr.
m
So question is - how to get pointer to a field of structure, of type int32? I don't know. Other than alloc a temporary variable, copy value, and pass pointer to it
p
@msink how would i copy the value of
windowTypeDockReply.pointed.atom
?
m
Hm. Either just
=
or
.value =
p
I don't get it.
m
I too :) What do you want - pass a value to that function or get result from it? Anyway
Copy code
val atom = alloc<xcb_atom_t> { value = .....atom.value }
xcb_....(...., atom.ptr)
And similar to get result. Cannot check from my phone, sorry.
p
my issue is that it doesn't let me use alloc like this, and i can't set
value
like this either, and the atom doesn`t have
.ptr
. Essentially i have no idea what i am doing, i am actually just copy pasting stuff together now in the hopes that something might work. Like i just started learning to code (frustrated).
Just want to be able to call this function with the proper data. Been googling for explanations and examples for similar issues for hours now, and i can't find a solution. (To this problem which seems trivial)
m
my issue is that it doesn't let me use alloc like this
Did you wrap it in
Copy code
memScoped {
}
p
yes and no, i tried all i know of
m
I don't believe
alloc
does not work inside
memScoped
block.
p
well i don't know what to tell you, it doesn't for me also i just can't imagine that this is the right way to get a pointer to a struct property, this has to be easier
m
Agreed, just don't know better workaround. Maybe try
alloc<fcb_atom_tVar>
p
that actually worked inside the memscoped!
so yeah, in a memscoped, create a temorary variable, allocate memory for
xcb_atom_tVar
(NOT
xcb_atom_t
), set its value to the value of the actual thing, and then use that new variables pointer in the function call. So here for completeness the code i used:
Copy code
val windowTypeReply = xcb_intern_atom_reply(
    this.connection,
    xcb_intern_atom(connection, 0, "_NET_WM_WINDOW_TYPE".length.toUShort(), "_NET_WM_WINDOW_TYPE"),
    null
) ?: error("could not get atom")

val windowTypeDockReply = xcb_intern_atom_reply(
    connection,
    xcb_intern_atom(connection, 0, "_NET_WM_WINDOW_TYPE_DOCK".length.toUShort(), "_NET_WM_WINDOW_TYPE_DOCK"),
    null
) ?: error("could not get atom")

memScoped {
    val temporary = alloc<xcb_atom_tVar> {
        value = windowTypeDockReply.pointed.atom
    }

    xcb_change_property(
        connection,
        XCB_PROP_MODE_REPLACE.toUByte(),
        window,
        windowTypeReply.pointed.atom,
        XCB_ATOM_ATOM,
        32,
        1,
        temporary.ptr
    )
}
makes 0 sense to me why i can't just access the atoms pointer directly, but at least this is a workaround
j
Thats because of the current memory management in kotlin native. You can also use arena to allocate everything in one place.
m
Looks that it's just not implemented, in C
&windowTypeDockReply->atom
is legal (and actually pretty common) operation, K/N cinterop does not support it.
n
Usually Arena is used for allocating stuff in memory which has a program lifetime (lasts until the program is terminated). Another place where Arena is used is in Kotlin objects where the allocated stuff has a Kotlin object lifetime (lasts until the Kotlin object is closed; usually via the object's close function by convention).
Note that if
xcb_atom_tVar
is allocated then
xcb_atom_t
is returned via the
value
property.