https://kotlinlang.org logo
#kotlin-native
Title
# kotlin-native
p

phisch

05/30/2021, 2:41 PM
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

msink

05/30/2021, 4:04 PM
last parameter in this function is
void *
, so probably .
pointed.atom.ptr
p

phisch

05/30/2021, 4:19 PM
tried that before, as .ptr should be the reverse of .pointed, but .atom does not have .ptr.
m

msink

05/30/2021, 5:08 PM
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

phisch

05/30/2021, 5:23 PM
@msink how would i copy the value of
windowTypeDockReply.pointed.atom
?
m

msink

05/30/2021, 5:29 PM
Hm. Either just
=
or
.value =
p

phisch

05/30/2021, 5:31 PM
I don't get it.
m

msink

05/30/2021, 5:39 PM
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

phisch

05/30/2021, 5:47 PM
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

msink

05/30/2021, 5:52 PM
my issue is that it doesn't let me use alloc like this
Did you wrap it in
Copy code
memScoped {
}
p

phisch

05/30/2021, 5:57 PM
yes and no, i tried all i know of
m

msink

05/30/2021, 6:03 PM
I don't believe
alloc
does not work inside
memScoped
block.
p

phisch

05/30/2021, 6:05 PM
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

msink

05/30/2021, 6:14 PM
Agreed, just don't know better workaround. Maybe try
alloc<fcb_atom_tVar>
p

phisch

05/30/2021, 6:20 PM
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

JoakimForslund

05/31/2021, 9:15 AM
Thats because of the current memory management in kotlin native. You can also use arena to allocate everything in one place.
m

msink

05/31/2021, 9:35 AM
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

napperley

06/02/2021, 1:13 AM
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.
5 Views