1e0a2b6dfSmrg#!/usr/bin/env perl
204b94745Smrg# $XTermId: dynamic.pl,v 1.5 2018/08/10 15:01:35 tom Exp $
3e39b573cSmrg# -----------------------------------------------------------------------------
4e39b573cSmrg# this file is part of xterm
5e39b573cSmrg#
6f2e35a3aSmrg# Copyright 2011-2017,2018 by Thomas E. Dickey
7913cc679Smrg#
8e39b573cSmrg#                         All Rights Reserved
9913cc679Smrg#
10e39b573cSmrg# Permission is hereby granted, free of charge, to any person obtaining a
11e39b573cSmrg# copy of this software and associated documentation files (the
12e39b573cSmrg# "Software"), to deal in the Software without restriction, including
13e39b573cSmrg# without limitation the rights to use, copy, modify, merge, publish,
14e39b573cSmrg# distribute, sublicense, and/or sell copies of the Software, and to
15e39b573cSmrg# permit persons to whom the Software is furnished to do so, subject to
16e39b573cSmrg# the following conditions:
17913cc679Smrg#
18e39b573cSmrg# The above copyright notice and this permission notice shall be included
19e39b573cSmrg# in all copies or substantial portions of the Software.
20913cc679Smrg#
21e39b573cSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22e39b573cSmrg# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23e39b573cSmrg# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24e39b573cSmrg# IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
25e39b573cSmrg# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
26e39b573cSmrg# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27e39b573cSmrg# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28913cc679Smrg#
29e39b573cSmrg# Except as contained in this notice, the name(s) of the above copyright
30e39b573cSmrg# holders shall not be used in advertising or otherwise to promote the
31e39b573cSmrg# sale, use or other dealings in this Software without prior written
32e39b573cSmrg# authorization.
33e39b573cSmrg# -----------------------------------------------------------------------------
34e39b573cSmrg# Test the dynamic-color query option of xterm.
35e39b573cSmrg# The programs xtermcontrol and xtermset provide more options.
36e39b573cSmrg
37e39b573cSmrguse strict;
38e0a2b6dfSmrguse warnings;
39e39b573cSmrg
40e39b573cSmrguse Getopt::Std;
41e39b573cSmrguse IO::Handle;
42e39b573cSmrg
43e39b573cSmrgour @color_names = (
44913cc679Smrg    "VT100 text foreground",
45913cc679Smrg    "VT100 text background",
46913cc679Smrg    "text cursor",
47913cc679Smrg    "mouse foreground",
48913cc679Smrg    "mouse background",
49913cc679Smrg    "Tektronix foreground",
50913cc679Smrg    "Tektronix background",
51913cc679Smrg    "highlight background",
52913cc679Smrg    "Tektronix cursor",
53913cc679Smrg    "highlight foreground"
54e39b573cSmrg);
55e39b573cSmrg
56913cc679Smrgour ( $opt_c, $opt_r );
57f2e35a3aSmrg
58f2e35a3aSmrg$Getopt::Std::STANDARD_HELP_VERSION = 1;
59913cc679Smrg&getopts('c:r') || die(
60913cc679Smrg    "Usage: $0 [options]\n
61e39b573cSmrgOptions:\n
62e39b573cSmrg  -c XXX  set cursor-color
63e39b573cSmrg  -r      reset colors
64913cc679Smrg"
65913cc679Smrg);
66e39b573cSmrg
67e39b573cSmrgsub no_reply($) {
68913cc679Smrg    open TTY, "+</dev/tty" or die("Cannot open /dev/tty\n");
69913cc679Smrg    autoflush TTY 1;
70913cc679Smrg    my $old = `stty -g`;
71913cc679Smrg    system "stty raw -echo min 0 time 5";
72913cc679Smrg
73913cc679Smrg    print TTY @_;
74913cc679Smrg    close TTY;
75913cc679Smrg    system "stty $old";
76e39b573cSmrg}
77e39b573cSmrg
78e39b573cSmrgsub get_reply($) {
79913cc679Smrg    open TTY, "+</dev/tty" or die("Cannot open /dev/tty\n");
80913cc679Smrg    autoflush TTY 1;
81913cc679Smrg    my $old = `stty -g`;
82913cc679Smrg    system "stty raw -echo min 0 time 5";
83913cc679Smrg
84913cc679Smrg    print TTY @_;
85913cc679Smrg    my $reply = <TTY>;
86913cc679Smrg    close TTY;
87913cc679Smrg    system "stty $old";
88913cc679Smrg    if ( defined $reply ) {
89913cc679Smrg        die("^C received\n") if ( "$reply" eq "\003" );
90913cc679Smrg    }
91913cc679Smrg    return $reply;
92e39b573cSmrg}
93e39b573cSmrg
94e39b573cSmrgsub query_color($) {
95913cc679Smrg    my $code   = $_[0];
96913cc679Smrg    my $param1 = $code + 10;
97913cc679Smrg    my $reply;
98e39b573cSmrg
99913cc679Smrg    $reply = get_reply("\x1b]$param1;?\007");
100e39b573cSmrg
101913cc679Smrg    return unless defined $reply;
102913cc679Smrg    if ( $reply =~ /\x1b]$param1;.*\007/ ) {
103913cc679Smrg        my $value = $reply;
104e39b573cSmrg
105913cc679Smrg        $value =~ s/^\x1b]$param1;//;
106913cc679Smrg        $value =~ s/\007//;
107e39b573cSmrg
108913cc679Smrg        printf "%24s = %s\n", $color_names[$code], $value;
109913cc679Smrg    }
110e39b573cSmrg}
111e39b573cSmrg
112e39b573cSmrgsub query_colors() {
113913cc679Smrg    my $n;
114e39b573cSmrg
115913cc679Smrg    for ( $n = 0 ; $n <= 9 ; ++$n ) {
116913cc679Smrg        &query_color($n);
117913cc679Smrg    }
118e39b573cSmrg}
119e39b573cSmrg
120e39b573cSmrgsub reset_colors() {
121913cc679Smrg    my $n;
122e39b573cSmrg
123913cc679Smrg    for ( $n = 0 ; $n <= 9 ; ++$n ) {
124913cc679Smrg        my $code = 110 + $n;
125913cc679Smrg        &no_reply("\x1b]$code\007");
126913cc679Smrg    }
127e39b573cSmrg}
128e39b573cSmrg
129913cc679Smrgif ( defined($opt_c) ) {
130913cc679Smrg    &no_reply("\x1b]12;$opt_c\007");
131e39b573cSmrg}
132913cc679Smrgif ( defined($opt_r) ) {
133913cc679Smrg    &reset_colors();
134e39b573cSmrg}
135e39b573cSmrg
136e39b573cSmrg&query_colors();
137