osx - Using termios in Swift -


now we've reached swift 2.0, i've decided convert my, yet unfinished, os x app swift. making progress i've run issues using termios , use clarification , advice.

the termios struct treated struct in swift, no surprise there, surprising array of control characters in struct tuple. expecting array. might imagine took me while figure out. working in playground if do:

var settings:termios = termios() print(settings) 

then correct details printed struct.

in obj-c set control characters use, say,

cfmakeraw(&settings); settings.c_cc[vmin] = 1; 

where vmin #define equal 16 in termios.h. in swift have do

cfmakeraw(&settings) settings.c_cc.16 = 1 

which works, bit more opaque. prefer use along lines of

settings.c_cc.vim = 1 

instead, can't seem find documentation describing swift "version" of termios. know if tuple has pre-assigned names it's elements, or if not, there way assign names after fact? should create own tuple named elements , assign settings.c_cc?

interestingly, despite fact pre-processor directives not supposed work in swift, if do

print(vmin) print(vtime) 

then correct values printed , no compiler errors produced. i'd interested in clarification or comments on that. bug?

the remaining issues have further configuration of termios.

the definition of cfsetspeed given as

func cfsetspeed(_: unsafemutablepointer<termios>, _: speed_t) -> int32 

and speed_t typedef'ed unsigned long. in obj-c we'd do

cfsetspeed(&settings, b38400); 

but since b38400 #define in termios.h can no longer that. has apple set replacement global constants things in swift, , if so, can tell me documented. alternative seems to plug in raw values , lose readability, or create own versions of constants defined in termios.h. i'm happy go route if there isn't better choice.

let's start second problem, easier solve. b38400 is available in swift, has wrong type. have convert explicitly:

var settings = termios() cfsetspeed(&settings, speed_t(b38400)) 

your first problem has no "nice" solution know of. fixed sized arrays imported swift tuples, , – far know – cannot address tuple element variable.

you pointer-juggling , use fact address of tuple can treated address array of elements, in convert string array of int8:

var settings = termios() withunsafemutablepointer(&settings.c_cc) { (tupleptr) -> void in     let ccptr = unsafemutablepointer<cc_t>(tupleptr)     ccptr[int(vmin)] = 1 } 

but not tuple array conversion not documented, in case might want keep your

settings.c_cc.16 = 1 

with integer literal 16.


Comments

Popular posts from this blog

php - Zend Framework / Skeleton-Application / Composer install issue -

c# - Better 64-bit byte array hash -

python - PyCharm Type error Message -