how to use the resource Pt_ARG_TTY_INPUT of PtTty!

hi,everyone!

who ever used this Pt_ARG_TTY_INPUT resource of PtTty!
in help. It says that its type is array . C type: char *,unsigned
short.
which dimension does it ? how to use unsigned short. 3x.

some simple example code will be very good ! :laughing:

dxwang wrote:

hi,everyone!

who ever used this Pt_ARG_TTY_INPUT resource of PtTty!
in help. It says that its type is array . C type: char *,unsigned
short.

Right. It’s an array of char. When you set it, you need to specify the
address and the number of elements, just like for any other array resource.

which dimension does it ? how to use unsigned short. 3x.

Unsigned short just means that the array can’t be longer than 65535
bytes. Even though you pass the length to PtSetArg() or PtSetResource()
as a long, you should ensure that the actual value fits into an unsigned
short.

(Unsigned short would normally mean that if you specify a length greater
than 65535, it’ll be truncated to 16 bits; in particular, if it’s a
multiple of 65536, it’ll be interpreted as zero. That’s what the widget
used to do, but at some point this was changed, and any value greater
than 65535 causes the widget to attempt to write 65535 bytes.
Typically, the write will return much less than that anyway, and your
code should use the Pt_ARG_TTY_INPUT_WRITTEN resource and a loop to
ensure that everything is written out. The change was made to make sure
that you don’t loop forever if you’re not careful and your length
happens to be a multiple of 65536.)

some simple example code will be very good ! > :laughing:

This one is pretty simple:

const char command[] = “ls -l /bin\r”;
PtSetResource( tty, Pt_ARG_TTY_INPUT, command, strlen(command) );

And here is how you can use the Pt_ARG_TTY_INPUT_WRITTEN resource to
ensure that a long string doesn’t get truncated:

const char *longstring = …
size_t length = strlen(longstring);
unsigned short *ptr;
PtSetResource( tty, Pt_ARG_TTY_INPUT, longstring, length );
PtGetResource( tty, Pt_ARG_TTY_INPUT_WRITTEN, &ptr, 0 );
while ( ( length -= *ptr ) != 0 ) {
longstring += *ptr;
// Block the widget to make sure
// that keyboard input doesn’t get mixed in
PtSetResource( tty, Pt_ARG_FLAGS, Pt_BLOCKED, Pt_BLOCKED );
delay( 100 ); // Give the shell some time to read
PtBkgdHandlerProcess(); // Process any pending Photon events
PtSetResource( tty, Pt_ARG_FLAGS, 0, Pt_BLOCKED ); // Unblock
PtSetResource( tty, Pt_ARG_TTY_INPUT, longstring, length );
}

Wojtek Lerch wrote:

PtGetResource( tty, Pt_ARG_TTY_INPUT_WRITTEN, &ptr, 0 );
while ( ( length -= *ptr ) != 0 ) {
longstring += *ptr;
// Block the widget to make sure
// that keyboard input doesn’t get mixed in
PtSetResource( tty, Pt_ARG_FLAGS, Pt_BLOCKED, Pt_BLOCKED );
delay( 100 ); // Give the shell some time to read
PtBkgdHandlerProcess(); // Process any pending Photon events
PtSetResource( tty, Pt_ARG_FLAGS, 0, Pt_BLOCKED ); // Unblock
PtSetResource( tty, Pt_ARG_TTY_INPUT, longstring, length );
}

It’s been pointed out to me that this loop is missing another
PtGetResource() call to get Pt_ARG_TTY_INPUT_WRITTEN after setting
Pt_ARG_TTY_INPUT. I happen to know that the code will work without it
because the pointer points to a location within the widget that always
holds the correct value; but even though that’s how a lot of resources
are implemented, it’s not a safe assumption to make in general.

Wojtek Lerch: very thanks