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