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