From 30f96100db77c299bd398f167a1ed116cb492061 Mon Sep 17 00:00:00 2001 From: thanse23 <62850132+H-ANSEN@users.noreply.github.com> Date: Tue, 16 Jul 2024 04:59:44 -0400 Subject: [PATCH] Add TIOCGWINSZ ioctl (#54) --- discover/discover.ml | 3 ++- dune-project | 1 + extunix.opam | 1 + src/extUnix.pp.ml | 6 ++++++ src/tty_ioctl.c | 22 +++++++++++++++++++++- 5 files changed, 31 insertions(+), 2 deletions(-) diff --git a/discover/discover.ml b/discover/discover.ml index a0ea7fe..8e122e6 100644 --- a/discover/discover.ml +++ b/discover/discover.ml @@ -281,8 +281,9 @@ let features = "TTY_IOCTL", L[ fd_int; I "termios.h"; I "sys/ioctl.h"; + T "struct winsize"; S "ioctl"; S "tcsetattr"; S "tcgetattr"; - D "CRTSCTS"; D "TCSANOW"; D "TIOCMGET"; D "TIOCMSET"; D "TIOCMBIC"; D "TIOCMBIS"; + D "CRTSCTS"; D "TCSANOW"; D "TIOCMGET"; D "TIOCMSET"; D "TIOCMBIC"; D "TIOCMBIS"; D "TIOCGWINSZ" ]; "TTYNAME", L[ fd_int; I "unistd.h"; S "ttyname"; ]; "CTERMID", L[ I "stdio.h"; S "ctermid"; V "L_ctermid"; ]; diff --git a/dune-project b/dune-project index 38d06a8..969417b 100644 --- a/dune-project +++ b/dune-project @@ -38,6 +38,7 @@ "Roman Vorobets" "Stéphane Glondu" "Sylvain Le Gall" + "Teague Hansen" "ygrek" "Zhenya Lykhovyd" ) diff --git a/extunix.opam b/extunix.opam index 19a220d..072cc09 100644 --- a/extunix.opam +++ b/extunix.opam @@ -25,6 +25,7 @@ authors: [ "Roman Vorobets" "Stéphane Glondu" "Sylvain Le Gall" + "Teague Hansen" "ygrek" "Zhenya Lykhovyd" ] diff --git a/src/extUnix.pp.ml b/src/extUnix.pp.ml index 6c15d9d..d4209fb 100644 --- a/src/extUnix.pp.ml +++ b/src/extUnix.pp.ml @@ -793,6 +793,12 @@ external tiocmbic : Unix.file_descr -> int -> unit = "caml_extunix_ioctl_TIOCMBI (** Set the indicated modem bits. See TIOCMBIS in tty_ioctl(4). *) external tiocmbis : Unix.file_descr -> int -> unit = "caml_extunix_ioctl_TIOCMBIS" +(** [tiocgwinsz fd] returns a tuple [(cols, rows, xpixel, ypixel)] representing + the size of the character device. [cols] is the number of character columns, + [rows] is the number of character rows, [xpixel] is width of the device in + pixels, and [ypixel] is the height of the device in pixels. *) +external tiocgwinsz : Unix.file_descr -> (int * int * int * int) = "caml_extunix_ioctl_TIOCGWINSZ" + ] end (* module Ioctl *) diff --git a/src/tty_ioctl.c b/src/tty_ioctl.c index 13ab2b2..97de59f 100644 --- a/src/tty_ioctl.c +++ b/src/tty_ioctl.c @@ -33,6 +33,27 @@ CAMLprim value caml_extunix_ioctl_##cmd(value v_fd, value v_arg) \ CAMLreturn(Val_unit); \ } +CAMLprim value caml_extunix_ioctl_TIOCGWINSZ(value v_fd) +{ + CAMLparam1(v_fd); + CAMLlocal1(result); + + struct winsize ws; + + int r = ioctl(Int_val(v_fd), TIOCGWINSZ, &ws); + if (r < 0) { + uerror("ioctl", caml_copy_string("TIOCGWINSZ")); + } + + result = caml_alloc_tuple(4); + Store_field(result, 0, Val_int(ws.ws_col)); + Store_field(result, 1, Val_int(ws.ws_row)); + Store_field(result, 2, Val_int(ws.ws_xpixel)); + Store_field(result, 3, Val_int(ws.ws_ypixel)); + + CAMLreturn(result); +} + CAMLprim value caml_extunix_ioctl_TIOCMGET(value v_fd) { CAMLparam1(v_fd); @@ -47,4 +68,3 @@ TTY_IOCTL_INT(TIOCMBIC) TTY_IOCTL_INT(TIOCMBIS) #endif -