dynamic.pl revision e0a2b6df
1e0a2b6dfSmrg#!/usr/bin/env perl
2e0a2b6dfSmrg# $XTermId: dynamic.pl,v 1.3 2014/02/26 20:17:07 tom Exp $
3e39b573cSmrg# -----------------------------------------------------------------------------
4e39b573cSmrg# this file is part of xterm
5e39b573cSmrg#
6e0a2b6dfSmrg# Copyright 2011,2014 by Thomas E. Dickey
7e39b573cSmrg#
8e39b573cSmrg#                         All Rights Reserved
9e39b573cSmrg#
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:
17e39b573cSmrg#
18e39b573cSmrg# The above copyright notice and this permission notice shall be included
19e39b573cSmrg# in all copies or substantial portions of the Software.
20e39b573cSmrg#
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.
28e39b573cSmrg#
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 = (
44e39b573cSmrg	"VT100 text foreground",
45e39b573cSmrg	"VT100 text background",
46e39b573cSmrg	"text cursor",
47e39b573cSmrg	"mouse foreground",
48e39b573cSmrg	"mouse background",
49e39b573cSmrg	"Tektronix foreground",
50e39b573cSmrg	"Tektronix background",
51e39b573cSmrg	"highlight background",
52e39b573cSmrg	"Tektronix cursor",
53e39b573cSmrg	"highlight foreground"
54e39b573cSmrg);
55e39b573cSmrg
56e39b573cSmrgour ($opt_c, $opt_r);
57e39b573cSmrg&getopts('c:r') || die("Usage: $0 [options]\n
58e39b573cSmrgOptions:\n
59e39b573cSmrg  -c XXX  set cursor-color
60e39b573cSmrg  -r      reset colors
61e39b573cSmrg");
62e39b573cSmrg
63e39b573cSmrgsub no_reply($) {
64e39b573cSmrg	open TTY, "+</dev/tty" or die("Cannot open /dev/tty\n");
65e39b573cSmrg	autoflush TTY 1;
66e39b573cSmrg	my $old=`stty -g`;
67e39b573cSmrg	system "stty raw -echo min 0 time 5";
68e39b573cSmrg
69e39b573cSmrg	print TTY @_;
70e39b573cSmrg	close TTY;
71e39b573cSmrg	system "stty $old";
72e39b573cSmrg}
73e39b573cSmrg
74e39b573cSmrgsub get_reply($) {
75e39b573cSmrg	open TTY, "+</dev/tty" or die("Cannot open /dev/tty\n");
76e39b573cSmrg	autoflush TTY 1;
77e39b573cSmrg	my $old=`stty -g`;
78e39b573cSmrg	system "stty raw -echo min 0 time 5";
79e39b573cSmrg
80e39b573cSmrg	print TTY @_;
81e39b573cSmrg	my $reply=<TTY>;
82e39b573cSmrg	close TTY;
83e39b573cSmrg	system "stty $old";
84e39b573cSmrg	if ( defined $reply ) {
85e39b573cSmrg		die("^C received\n") if ( "$reply" eq "\003" );
86e39b573cSmrg	}
87e39b573cSmrg	return $reply;
88e39b573cSmrg}
89e39b573cSmrg
90e39b573cSmrgsub query_color($) {
91e39b573cSmrg	my $code = $_[0];
92e39b573cSmrg	my $param1 = $code + 10;
93e39b573cSmrg	my $reply;
94e39b573cSmrg
95e39b573cSmrg	$reply=get_reply("\x1b]$param1;?\007");
96e39b573cSmrg
97e39b573cSmrg	return unless defined $reply;
98e39b573cSmrg	if ( $reply =~ /\x1b]$param1;.*\007/ ) {
99e39b573cSmrg		my $value = $reply;
100e39b573cSmrg
101e39b573cSmrg		$value =~ s/^\x1b]$param1;//;
102e39b573cSmrg		$value =~ s/\007//;
103e39b573cSmrg
104e39b573cSmrg		printf "%24s = %s\n", $color_names[$code], $value;
105e39b573cSmrg	}
106e39b573cSmrg}
107e39b573cSmrg
108e39b573cSmrgsub query_colors() {
109e39b573cSmrg	my $n;
110e39b573cSmrg
111e39b573cSmrg	for ( $n = 0; $n <= 9; ++$n) {
112e39b573cSmrg		&query_color($n);
113e39b573cSmrg	}
114e39b573cSmrg}
115e39b573cSmrg
116e39b573cSmrgsub reset_colors() {
117e39b573cSmrg	my $n;
118e39b573cSmrg
119e39b573cSmrg	for ( $n = 0; $n <= 9; ++$n) {
120e39b573cSmrg		my $code = 110 + $n;
121e39b573cSmrg		&no_reply("\x1b]$code\007");
122e39b573cSmrg	}
123e39b573cSmrg}
124e39b573cSmrg
125e39b573cSmrgif ( defined ($opt_c) ) {
126e39b573cSmrg	&no_reply("\x1b]12;$opt_c\007");
127e39b573cSmrg}
128e39b573cSmrgif ( defined ($opt_r) ) {
129e39b573cSmrg	&reset_colors();
130e39b573cSmrg}
131e39b573cSmrg
132e39b573cSmrg&query_colors();
133