dynamic.pl revision e39b573c
1#!/usr/bin/perl -w
2# $XTermId: dynamic.pl,v 1.2 2011/07/05 09:32:53 tom Exp $
3# -----------------------------------------------------------------------------
4# this file is part of xterm
5#
6# Copyright 2011 by Thomas E. Dickey
7#
8#                         All Rights Reserved
9#
10# Permission is hereby granted, free of charge, to any person obtaining a
11# copy of this software and associated documentation files (the
12# "Software"), to deal in the Software without restriction, including
13# without limitation the rights to use, copy, modify, merge, publish,
14# distribute, sublicense, and/or sell copies of the Software, and to
15# permit persons to whom the Software is furnished to do so, subject to
16# the following conditions:
17#
18# The above copyright notice and this permission notice shall be included
19# in all copies or substantial portions of the Software.
20#
21# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24# IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
25# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
26# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28#
29# Except as contained in this notice, the name(s) of the above copyright
30# holders shall not be used in advertising or otherwise to promote the
31# sale, use or other dealings in this Software without prior written
32# authorization.
33# -----------------------------------------------------------------------------
34# Test the dynamic-color query option of xterm.
35# The programs xtermcontrol and xtermset provide more options.
36
37use strict;
38
39use Getopt::Std;
40use IO::Handle;
41
42our @color_names = (
43	"VT100 text foreground",
44	"VT100 text background",
45	"text cursor",
46	"mouse foreground",
47	"mouse background",
48	"Tektronix foreground",
49	"Tektronix background",
50	"highlight background",
51	"Tektronix cursor",
52	"highlight foreground"
53);
54
55our ($opt_c, $opt_r);
56&getopts('c:r') || die("Usage: $0 [options]\n
57Options:\n
58  -c XXX  set cursor-color
59  -r      reset colors
60");
61
62sub no_reply($) {
63	open TTY, "+</dev/tty" or die("Cannot open /dev/tty\n");
64	autoflush TTY 1;
65	my $old=`stty -g`;
66	system "stty raw -echo min 0 time 5";
67
68	print TTY @_;
69	close TTY;
70	system "stty $old";
71}
72
73sub get_reply($) {
74	open TTY, "+</dev/tty" or die("Cannot open /dev/tty\n");
75	autoflush TTY 1;
76	my $old=`stty -g`;
77	system "stty raw -echo min 0 time 5";
78
79	print TTY @_;
80	my $reply=<TTY>;
81	close TTY;
82	system "stty $old";
83	if ( defined $reply ) {
84		die("^C received\n") if ( "$reply" eq "\003" );
85	}
86	return $reply;
87}
88
89sub query_color($) {
90	my $code = $_[0];
91	my $param1 = $code + 10;
92	my $reply;
93
94	$reply=get_reply("\x1b]$param1;?\007");
95
96	return unless defined $reply;
97	if ( $reply =~ /\x1b]$param1;.*\007/ ) {
98		my $value = $reply;
99
100		$value =~ s/^\x1b]$param1;//;
101		$value =~ s/\007//;
102
103		printf "%24s = %s\n", $color_names[$code], $value;
104	}
105}
106
107sub query_colors() {
108	my $n;
109
110	for ( $n = 0; $n <= 9; ++$n) {
111		&query_color($n);
112	}
113}
114
115sub reset_colors() {
116	my $n;
117
118	for ( $n = 0; $n <= 9; ++$n) {
119		my $code = 110 + $n;
120		&no_reply("\x1b]$code\007");
121	}
122}
123
124if ( defined ($opt_c) ) {
125	&no_reply("\x1b]12;$opt_c\007");
126}
127if ( defined ($opt_r) ) {
128	&reset_colors();
129}
130
131&query_colors();
132