File: wm-state-exercise.pl

package info (click to toggle)
libx11-protocol-other-perl 31-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,760 kB
  • sloc: perl: 17,593; ansic: 666; sh: 285; lisp: 143; makefile: 42
file content (282 lines) | stat: -rwxr-xr-x 8,291 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/usr/bin/perl -w

# Copyright 2013, 2014 Kevin Ryde

# This file is part of X11-Protocol-Other.
#
# X11-Protocol-Other is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation; either version 3, or (at your option) any
# later version.
#
# X11-Protocol-Other is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with X11-Protocol-Other.  If not, see <http://www.gnu.org/licenses/>.


# killall -9 olvwm olwm tvtwm miwm vtwm aewm
# xwit -iconify -id $WINDOWID; sleep 2; xwit -raise -id $WINDOWID


use strict;
use FindBin;
use File::Spec;
my $script = File::Spec->catfile($FindBin::Bin,$FindBin::Script);
use X11::Protocol;
use X11::AtomConstants;
use X11::Protocol::WM;

if (@ARGV) {
  open STDOUT, '>>', '/tmp/wm-state-exercise.out' or die;
  open STDERR, '>>&', \*STDOUT;
  $| = 1;
  print "\n\n\n-------------------------------------------------------------\n";

  my $wm = $ARGV[0];
  my $pid = fork();
  if ($pid) {
    # parent
    sleep 5;
  } else {
    exec $wm or die $!;
  }

  my $display = $ENV{"DISPLAY"};
  my $window = $ENV{"WINDOWID"};
  my $X = X11::Protocol->new($display);
  my $root = $X->root;
  print "\n$wm $display window=$window\n";
  # system("xprop -root >>/tmp/wm-state-exercise.out");

  if (0) {
    require IPC::Run;
    IPC::Run::run (['perl', '-w', '-I','lib', 't/WM.t']);
  }

  if (1) {
    # WM_STATE

    my $print_wm_state = sub {
      my ($reason) = @_;
      if (my ($state, $icon_window) = X11::Protocol::WM::get_wm_state($X,$window)) {
        $state //= '[undef]';
        print "$reason: $state  (icon window: $icon_window)\n";
        return $state;
      } else {
        print "$reason: WM_STATE not set\n";
        return '';
      }
    };

    $print_wm_state->('initial state');

    print "iconify()\n";
    X11::Protocol::WM::iconify ($X, $window, $root);
    $X->flush;
    sleep 2;
    { my $state = $print_wm_state->('after iconify');
      if ($state ne 'IconicState') {
        print "  oops, expected IconicState\n";
      }
    }

    print "MapWindow()\n";
    $X->MapWindow($window);
    $X->flush;
    sleep 2;
    { my $state = $print_wm_state->('after un-iconify');
      if ($state ne 'NormalState') {
        print "  oops, expected NormalState\n";
      }
    }
  }

  if (1) {
    # _NET_WM_STATE

    my @supported_atoms;
    my %supported_atoms;
    {
      my ($value, $type, $format, $bytes_after)
        = $X->GetProperty ($root,
                           $X->atom('_NET_SUPPORTED'),   # property
                           X11::AtomConstants::ATOM(),  # type
                           0,    # offset
                           999,  # length limit
                           0);   # delete
      if ($format == 32) {
        @supported_atoms = unpack('L*', $value);
        foreach my $atom (@supported_atoms) {
          $supported_atoms{$atom} = 1;
        }
      } else {
        print "no _NET_SUPPORTED\n";
      }
    }
    print "supported count ",scalar(@supported_atoms),"\n";
    print "supported: ",join(', ',map{$X->atom_name($_)} @supported_atoms),"\n";

    # _NET_WM_STATE_HIDDEN
    foreach my $state (qw(
                           _NET_WM_STATE_BELOW
                           _NET_WM_STATE_ABOVE
                           _NET_WM_STATE_MODAL
                           _NET_WM_STATE_STICKY
                           _NET_WM_STATE_MAXIMIZED_VERT
                           _NET_WM_STATE_MAXIMIZED_HORZ
                           _NET_WM_STATE_SHADED
                           _NET_WM_STATE_SKIP_TASKBAR
                           _NET_WM_STATE_SKIP_PAGER
                           _NET_WM_STATE_FULLSCREEN
                           _NET_WM_STATE_DEMANDS_ATTENTION
                        )) {
      print "$state\n";
      my $state_atom = $X->atom($state);
      if (! $supported_atoms{$state_atom}) {
        print "  not supported\n";
        next;
      }

      X11::Protocol::WM::change_net_wm_state ($X, $window, 'add', $state_atom);
      $X->flush;
      sleep 1;
      {
        my @states = X11::Protocol::WM::get_net_wm_state_atoms($X,$window);
        my $found = grep {$_==$state_atom} @states;
        if ($found) {
          print "  add ok\n";
        } else {
          print "  bad, add not set\n";
          print "  have: ",join(' ',map{$X->atom_name($_)}@states),"\n";
        }
      }

      X11::Protocol::WM::change_net_wm_state ($X, $window, 'remove', $state_atom);
      $X->flush;
      sleep 1;
      {
        my @states = X11::Protocol::WM::get_net_wm_state_atoms($X,$window);
        my $found = grep {$_==$state_atom} @states;
        if ($found) {
          print "  bad, remove still set\n";
          print "  have: ",join(' ',map{$X->atom_name($_)}@states),"\n";
        } else {
          print "  remove ok\n";
        }
      }

      X11::Protocol::WM::change_net_wm_state ($X, $window, 'toggle', $state_atom);
      $X->flush;
      sleep 1;
      {
        my @states = X11::Protocol::WM::get_net_wm_state_atoms($X,$window);
        my $found = grep {$_==$state_atom} @states;
        if ($found) {
          print "  toggle-on ok\n";
        } else {
          print "  bad, toggle-on not set\n";
          print "  have: ",join(' ',map{$X->atom_name($_)}@states),"\n";
        }
      }

      X11::Protocol::WM::change_net_wm_state ($X, $window, 'toggle', $state_atom);
      $X->flush;
      sleep 1;
      {
        my @states = X11::Protocol::WM::get_net_wm_state_atoms($X,$window);
        my $found = grep {$_==$state_atom} @states;
        if ($found) {
          print "  bad, toggle-off still set\n";
          print "  have: ",join(' ',map{$X->atom_name($_)}@states),"\n";
        } else {
          print "  toggle-off ok\n";
        }
      }
    }
  }
  # kill $pid;
  print "exit\n";
  exit 0;
}


unlink '/tmp/wm-state-exercise.out';

foreach my $wm (
                # /usr/share/doc/python-plwm/examples/README.examplewm
                '/usr/share/doc/python-plwm/examples/petliwm.py',
                # 'startfluxbox',
                # 'i3',
                # 'subtle',     # no iconic
                # 'aewm',
                # 'matchbox-window-manager',
                # 'tinywm',
                # 'notion',     # no iconic
                # 'ratpoison',
                # 'tritium',    # no NET_WM
                # 'fvwm2',
                # 'evilwm',     # no iconic
                # 'jwm',
                # 'metacity',
                # 'wmii',
                #     'icewm',
                #     'mwm',
                #     'olwm',
                #     'olvwm',
                #     'pekwm',
                # 
                #     'spectrwm',
                #     'windowlab',
                # 
                #     'awesome',
                #     'openbox',
                #     'tvtwm',
                #     '9wm',
                #     'w9wm',
                #     'xfwm4',
                # 
                #     'ctwm',
                #     'flwm',
                #     'herbstluftwm',
                #     'larswm',
                #     'miwm',
                #     'oroborus',
                # 'sapphire',
                # 'dwm',
                # 'wm2',
                # 'xmonad',
                # 'twm',
                # 'vtwm',
                # 'amiwm',  # no NET_WM
                # '/so/swm/sWM-1.3.6/bin/sWM',
               ) {
  # my $command = "xvfb-run -a xterm -e 'echo $wm >>/tmp/xx'";
  my $command = "xvfb-run -a xterm -e 'perl $script $wm'";
  print "$command\n";
  system ($command);
}



# MapWindow to NormalState
# jwm -
# matchbox-window-manager - dodginess
# amiwm - MapRequest looks at WMHints but doesn't notice IconicState
# flwm - MapRequest handler only does initial map
# tritium - /usr/share/pyshared/plwm/wmanager.py deiconify()


# no iconify
# dwm, evilwm, i3, sapphire, subtle, wmii, xmonad

# bad shutdowns:
# aewm
# olvwm
# olwm
# miwm
# vtwm  100% cpu on SIGTERM
# tvtwm [my packaged]