Skip to content

Commit

Permalink
Add TIOCGWINSZ ioctl (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
H-ANSEN authored Jul 16, 2024
1 parent e009312 commit 30f9610
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 2 deletions.
3 changes: 2 additions & 1 deletion discover/discover.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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"; ];
Expand Down
1 change: 1 addition & 0 deletions dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"Roman Vorobets"
"Stéphane Glondu"
"Sylvain Le Gall"
"Teague Hansen"
"ygrek"
"Zhenya Lykhovyd"
)
Expand Down
1 change: 1 addition & 0 deletions extunix.opam
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ authors: [
"Roman Vorobets"
"Stéphane Glondu"
"Sylvain Le Gall"
"Teague Hansen"
"ygrek"
"Zhenya Lykhovyd"
]
Expand Down
6 changes: 6 additions & 0 deletions src/extUnix.pp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 *)
Expand Down
22 changes: 21 additions & 1 deletion src/tty_ioctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -47,4 +68,3 @@ TTY_IOCTL_INT(TIOCMBIC)
TTY_IOCTL_INT(TIOCMBIS)

#endif

0 comments on commit 30f9610

Please sign in to comment.