Re: [PATCH 2/2] vt: add TIOCL_GETCURSORPOS to retrieve the screen cursor position

Nicolas Pitre
2025-05-14T13:25:09Z

On Wed, 14 May 2025, Jiri Slaby wrote:

> On 14. 05. 25, 3:52, Nicolas Pitre wrote:
> ...
> > So let's work around this limitation by adding TIOCL_GETCURSORPOS as a
> > fallback method to get the cursor position when /dev/vcsa reports 255.
> ...
> > --- a/drivers/tty/vt/vt.c
> > +++ b/drivers/tty/vt/vt.c
> ...
> > +/* invoked via ioctl(TIOCLINUX) */
> > +static int get_cursor_pos(struct tty_struct *tty)
> > +{
> > + struct vc_data *vc = tty->driver_data;
> > + unsigned int x, y;
> > +
> > + console_lock();
> > + x = vc->state.x;
> > + y = vc->state.y;
> > + console_unlock();
> > +
> > + /*
> > + * Clamp x to 16 bits, y to 15 bits. A display larger than 65535x32767
> > + * characters won't be a concern for the foreseeable future.
> > + * Bit 31 is reserved to represent negative error codes elsewhere.
> > + */
> > + return min(x, 0xFFFFu) | (min(y, 0x7FFFu) << 16);
>
> Hmm, I would do a proper struct instead. Like winsize.

I did that initially, than I had second thoughts about the extra
overhead implied by such a structure and the separate put_user(). This
coupled with the ambiguity around alignment with argp usage. In that
context is the structure really worth it compared to a simple return?

For the record, TIOCL_SETSEL is defined using:

struct {
char subcode;
short xs, ys, xe, ye;
short sel_mode;
};

but the documentation fails to mention that the structure must be packed
as there must nott be any alignment padding between subcode and the rest
of the structure given the kernel-side implementation.

> > --- a/include/uapi/linux/tiocl.h
> > +++ b/include/uapi/linux/tiocl.h
> > @@ -38,4 +38,8 @@ struct tiocl_selection {
> > #define TIOCL_GETKMSGREDIRECT 17 /* get the vt the kernel
> > messages are restricted to */
> > #define TIOCL_GETBRACKETEDPASTE 18 /* get whether paste may be
> > bracketed */
> >
> > +#define TIOCL_GETCURSORPOS 20 /* Get screen cursor position */
>
> The same question about man-pages :).

Shall do.

Nicolas