dynamic.pl revision 913cc679
1e0a2b6dfSmrg#!/usr/bin/env perl
2913cc679Smrg# $XTermId: dynamic.pl,v 1.4 2017/01/22 18:34:06 tom Exp $
3e39b573cSmrg# -----------------------------------------------------------------------------
4e39b573cSmrg# this file is part of xterm
5e39b573cSmrg#
6913cc679Smrg# Copyright 2011-2014,2017 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 );
57913cc679Smrg&getopts('c:r') || die(
58913cc679Smrg    "Usage: $0 [options]\n
59e39b573cSmrgOptions:\n
60e39b573cSmrg  -c XXX  set cursor-color
61e39b573cSmrg  -r      reset colors
62913cc679Smrg"
63913cc679Smrg);
64e39b573cSmrg
65e39b573cSmrgsub no_reply($) {
66913cc679Smrg    open TTY, "+</dev/tty" or die("Cannot open /dev/tty\n");
67913cc679Smrg    autoflush TTY 1;
68913cc679Smrg    my $old = `stty -g`;
69913cc679Smrg    system "stty raw -echo min 0 time 5";
70913cc679Smrg
71913cc679Smrg    print TTY @_;
72913cc679Smrg    close TTY;
73913cc679Smrg    system "stty $old";
74e39b573cSmrg}
75e39b573cSmrg
76e39b573cSmrgsub get_reply($) {
77913cc679Smrg    open TTY, "+</dev/tty" or die("Cannot open /dev/tty\n");
78913cc679Smrg    autoflush TTY 1;
79913cc679Smrg    my $old = `stty -g`;
80913cc679Smrg    system "stty raw -echo min 0 time 5";
81913cc679Smrg
82913cc679Smrg    print TTY @_;
83913cc679Smrg    my $reply = <TTY>;
84913cc679Smrg    close TTY;
85913cc679Smrg    system "stty $old";
86913cc679Smrg    if ( defined $reply ) {
87913cc679Smrg        die("^C received\n") if ( "$reply" eq "\003" );
88913cc679Smrg    }
89913cc679Smrg    return $reply;
90e39b573cSmrg}
91e39b573cSmrg
92e39b573cSmrgsub query_color($) {
93913cc679Smrg    my $code   = $_[0];
94913cc679Smrg    my $param1 = $code + 10;
95913cc679Smrg    my $reply;
96e39b573cSmrg
97913cc679Smrg    $reply = get_reply("\x1b]$param1;?\007");
98e39b573cSmrg
99913cc679Smrg    return unless defined $reply;
100913cc679Smrg    if ( $reply =~ /\x1b]$param1;.*\007/ ) {
101913cc679Smrg        my $value = $reply;
102e39b573cSmrg
103913cc679Smrg        $value =~ s/^\x1b]$param1;//;
104913cc679Smrg        $value =~ s/\007//;
105e39b573cSmrg
106913cc679Smrg        printf "%24s = %s\n", $color_names[$code], $value;
107913cc679Smrg    }
108e39b573cSmrg}
109e39b573cSmrg
110e39b573cSmrgsub query_colors() {
111913cc679Smrg    my $n;
112e39b573cSmrg
113913cc679Smrg    for ( $n = 0 ; $n <= 9 ; ++$n ) {
114913cc679Smrg        &query_color($n);
115913cc679Smrg    }
116e39b573cSmrg}
117e39b573cSmrg
118e39b573cSmrgsub reset_colors() {
119913cc679Smrg    my $n;
120e39b573cSmrg
121913cc679Smrg    for ( $n = 0 ; $n <= 9 ; ++$n ) {
122913cc679Smrg        my $code = 110 + $n;
123913cc679Smrg        &no_reply("\x1b]$code\007");
124913cc679Smrg    }
125e39b573cSmrg}
126e39b573cSmrg
127913cc679Smrgif ( defined($opt_c) ) {
128913cc679Smrg    &no_reply("\x1b]12;$opt_c\007");
129e39b573cSmrg}
130913cc679Smrgif ( defined($opt_r) ) {
131913cc679Smrg    &reset_colors();
132e39b573cSmrg}
133e39b573cSmrg
134e39b573cSmrg&query_colors();
135