| 1 | #!/usr/bin/perl | ||
| 2 | # made by: KorG | ||
| 3 | # vim: cc=119 et sw=4 ts=4 : | ||
| 4 | package X11::korgwm::EWMH; | ||
| 5 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use feature 'signatures'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | 0 | ▶ | use X11::XCB ':all'; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 10 | use X11::XCB::Event::ClientMessage; | ||
| 11 | use X11::XCB::Event::PropertyNotify; | ||
| 12 | |||
| 13 | use X11::korgwm::Common; | ||
| 14 | use X11::korgwm::Window; | ||
| 15 | |||
| 16 | # Unconditionally update a title | ||
| 17 | sub icccm_update_title($evt) { | ||
| 18 | my $win = $windows->{$evt->{window}} or return; | ||
| 19 | $win->update_title(); | ||
| 20 | } | ||
| 21 | |||
| 22 | # React only on urgency | ||
| 23 | sub icccm_update_wm_hints($evt) { | ||
| 24 | my $win = $windows->{$evt->{window}} or return; | ||
| 25 | |||
| 26 | # Short path if nothing changed | ||
| 27 | my $urgency_old = $win->{urgent} // -1; | ||
| 28 | my $urgency_new = $win->urgency_get() // -1; | ||
| 29 | return if $urgency_new == $urgency_old; | ||
| 30 | |||
| 31 | # The state has changed | ||
| 32 | $win->urgency_clear() if $urgency_old > 0; | ||
| 33 | $win->urgency_raise() if $urgency_new > 0; | ||
| 34 | |||
| 35 | $win->{urgent} = $urgency_new > 0; | ||
| 36 | } | ||
| 37 | |||
| 38 | # _WM_STATE Attention handler | ||
| 39 | sub net_wm_attention($action, $win) { | ||
| 40 | if ($action == _NET_WM_STATE_ADD) { | ||
| 41 | $win->urgency_raise(); | ||
| 42 | } elsif ($action == _NET_WM_STATE_REMOVE) { | ||
| 43 | $win->urgency_clear(); | ||
| 44 | } elsif ($action == _NET_WM_STATE_TOGGLE) { | ||
| 45 | $win->{urgent} ? $win->urgency_clear() : $win->urgency_raise(); | ||
| 46 | } else { | ||
| 47 | croak "(attention) Unknown action specified in _NET_WM_STATE EWMH: $action"; | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | # _WM_STATE Fullscreen handler | ||
| 52 | sub net_wm_fullscreen($action, $win) { | ||
| 53 | if ($action == _NET_WM_STATE_ADD) { | ||
| 54 | $win->toggle_maximize(1, allow_invisible => 1); | ||
| 55 | } elsif ($action == _NET_WM_STATE_REMOVE) { | ||
| 56 | $win->toggle_maximize(0, allow_invisible => 1); | ||
| 57 | } elsif ($action == _NET_WM_STATE_TOGGLE) { | ||
| 58 | $win->toggle_maximize(2, allow_invisible => 1); | ||
| 59 | } else { | ||
| 60 | croak "(fullscreen) Unknown action specified in _NET_WM_STATE EWMH: $action"; | ||
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 | # _NET_WM_STATE handlers | ||
| 65 | my $atom_fullscreen; | ||
| 66 | my $atom_netwm_attention; | ||
| 67 | my %wm_state_handlers; | ||
| 68 | sub icccm_update_wm_state($evt) { | ||
| 69 | my $win = $windows->{$evt->{window}} or return; | ||
| 70 | |||
| 71 | # Extract event from the data | ||
| 72 | my ($action, $first, $second, $source_indication) = unpack "LLLL", $evt->{data} // return; | ||
| 73 | $second //= $first //= 0; | ||
| 74 | |||
| 75 | # Execute handler for each known atom | ||
| 76 | $_->($action, $win) for grep defined, map { $wm_state_handlers{$_} } $first, $second; | ||
| 77 | } | ||
| 78 | |||
| 79 | our $icccm_atoms = {}; | ||
| 80 | our $icccm_handlers = { | ||
| 81 | "WM_HINTS" => \&icccm_update_wm_hints, | ||
| 82 | "WM_NAME" => \&icccm_update_title, | ||
| 83 | "_NET_WM_NAME" => \&icccm_update_title, | ||
| 84 | "_NET_WM_STATE" => \&icccm_update_wm_state, | ||
| 85 | "_NET_SUPPORTED" => undef, | ||
| 86 | "_NET_SUPPORTING_WM_CHECK" => undef, | ||
| 87 | "_NET_WM_STATE_DEMANDS_ATTENTION" => undef, | ||
| 88 | "_NET_WM_STATE_FULLSCREEN" => undef, | ||
| 89 | }; | ||
| 90 | |||
| 91 | sub fill_icccm_atoms { | ||
| 92 | $icccm_atoms->{atom($_)} = $_ for keys %{ $icccm_handlers }; | ||
| 93 | } | ||
| 94 | |||
| 95 | # Set EWMH support declaration | ||
| 96 | sub declare_support { | ||
| 97 | fill_icccm_atoms unless keys %{ $icccm_atoms }; | ||
| 98 | my @atoms = keys %{ $icccm_atoms }; | ||
| 99 | |||
| 100 | # Create a child window | ||
| 101 | my $wid = $X->generate_id(); | ||
| 102 | $X->create_window(0, $wid, $X->root->id, 0, 0, 1, 1, 0, WINDOW_CLASS_INPUT_OUTPUT, 0, CW_OVERRIDE_REDIRECT, 1); | ||
| 103 | |||
| 104 | # Define EWMH atoms we support | ||
| 105 | $X->change_property(PROP_MODE_REPLACE, $X->root->id, atom("_NET_SUPPORTED"), | ||
| 106 | atom("ATOM"), 32, 0+@atoms, pack "L" x @atoms => @atoms); | ||
| 107 | |||
| 108 | # Watchdog for siblings | ||
| 109 | for my $id ($wid, $X->root->id) { | ||
| 110 | $X->change_property(PROP_MODE_REPLACE, $id, atom("_NET_SUPPORTING_WM_CHECK"), | ||
| 111 | atom("WINDOW"), 32, 1, pack L => $wid); | ||
| 112 | } | ||
| 113 | } | ||
| 114 | |||
| 115 | sub init { | ||
| 116 | # Populate current atom ids | ||
| 117 | fill_icccm_atoms unless keys %{ $icccm_atoms }; | ||
| 118 | |||
| 119 | $atom_fullscreen = atom("_NET_WM_STATE_FULLSCREEN"); | ||
| 120 | $wm_state_handlers{ $atom_fullscreen } = \&net_wm_fullscreen; | ||
| 121 | |||
| 122 | $atom_netwm_attention = atom("_NET_WM_STATE_DEMANDS_ATTENTION"); | ||
| 123 | $wm_state_handlers{ $atom_netwm_attention } = \&net_wm_attention; | ||
| 124 | |||
| 125 | # Set up event handlers | ||
| 126 | add_event_cb(CLIENT_MESSAGE(), sub ($evt) { | ||
| 127 | my $atomname = $icccm_atoms->{$evt->type} or return; | ||
| 128 | my $handler = $icccm_handlers->{$atomname} or return; | ||
| 129 | $handler->($evt); | ||
| 130 | }); | ||
| 131 | |||
| 132 | add_event_cb(PROPERTY_NOTIFY(), sub ($evt) { | ||
| 133 | my $atomname = $icccm_atoms->{$evt->{atom}} or return; | ||
| 134 | my $handler = $icccm_handlers->{$atomname} or return; | ||
| 135 | $handler->($evt); | ||
| 136 | }); | ||
| 137 | } | ||
| 138 | |||
| 139 | push @X11::korgwm::extensions, \&init; | ||
| 140 | |||
| 141 | 1; | ||