Created
November 20, 2023 21:43
-
-
Save krpors/698672bb0b54ab40a8f7dac9ca5e7120 to your computer and use it in GitHub Desktop.
Nim c ffi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is just an example to get you started. A typical binary package | |
# uses this file as the main entry point of the application. | |
import std/posix | |
type | |
Winsize* = object | |
ws_row*: cushort | |
ws_col*: cushort | |
ws_xpixel*: cushort | |
ws_ypixel*: cushort | |
type | |
Termios* = object | |
c_iflag*: cuint | |
c_oflag*: cuint | |
c_cflag*: cuint | |
c_line*: cchar | |
c_cc*: array[19, cint] | |
proc tcgetattr*(fd: cint, tio: Termios): cint {.importc, header: "<unistd.h>".} | |
proc tcsetattr*(fd: cint, optional_actions: cint, tio: Termios): cint {.importc, header: "<unistd.h>".} | |
# let TCSAFLUSH: cint {.importc, header: "<unistd.h>".} | |
let TCSAFLUSH: cint = 2 | |
# var L_ctermid{.importc, header: "<stdio.h>".}: cint | |
#[ | |
157 ┆ // Signal handler to react on screen resizing. | |
158 ┆ struct sigaction act; | |
159 ┆ memset(&act, 0, sizeof(struct sigaction)); | |
160 ┆ act.sa_handler = handle_term_resize; | |
161 ┆ if (sigaction(SIGWINCH, &act, NULL) == -1) { | |
162 ┆ ┆ perror("A sigaction() call failed"); | |
163 ┆ ┆ abort(); | |
164 ┆ } | |
165 ┆ resizeflag = 0; | |
]# | |
proc asd(x: cint) {.noconv.} = | |
echo("Resizing!!") | |
return | |
when isMainModule: | |
const SIGWINCH = 28 | |
var s: Sigaction | |
s.sa_handler = asd | |
var siga = sigaction(SIGWINCH, s, nil) | |
if siga != 0: | |
echo("Could not sigact") | |
var ws: Winsize | |
var res = ioctl(STDOUT_FILENO, 0x5413, ws.addr) | |
echo(ws) | |
var tt: Termios | |
var x = tcgetattr(STDIN_FILENO, tt) | |
echo(tt) | |
var r = tcsetattr(STDIN_FILENO, TCSAFLUSH, tt) | |
echo(r, r) | |
discard sleep(10) | |
var zzx = "a\x00b" | |
echo(zzx) | |
echo(zzx.len) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment