Просмотр исходного кода

wm: add Alt+Tab window switcher

Johannes Zellner 2 часов назад
Родитель
Сommit
265a35fced
7 измененных файлов с 481 добавлено и 3 удалено
  1. 12 0
      AGENTS.md
  2. 20 0
      docs/graphics.html
  3. 13 0
      include/wm_abi.h
  4. 25 0
      kernel/io.c
  5. 5 0
      kernel/io.h
  6. 27 3
      kernel/wm.c
  7. 379 0
      user/apps/wm/ucmd_wm.c

+ 12 - 0
AGENTS.md

@@ -605,6 +605,18 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   moved `WM_RESIZE_MIN_STEP` (8 px), and the release forces the final
   moved `WM_RESIZE_MIN_STEP` (8 px), and the release forces the final
   geometry; the kernel carries the previous front canvas over so the content
   geometry; the kernel carries the previous front canvas over so the content
   does not blink black while the client reflows (see the canvas API bullet).
   does not blink black while the client reflows (see the canvas API bullet).
+  **Alt+Tab** switches windows through the same MRU focus order.  `io.c`
+  tracks either Alt key and turns Tab while Alt is held into
+  `IO_KEY_ALT_TAB`/`IO_KEY_ALT_SHIFT_TAB` (with typematic repeat);
+  `wm_pump_keyboard()` consumes the chord so the focused client never sees a
+  Tab and queues `WM_CHORD_NEXT`/`WM_CHORD_PREV` in `struct wm_status`'s
+  eight-entry chord ring, because the WM itself is not a window with a key
+  queue.  The WM keeps the MRU list from the `focused` flags it already
+  refreshes (`sw_track_focus()`), draws a centred icon strip with the
+  selected app's name while `alt_down` is set, and focuses the selection
+  with `SYS_WM_FOCUS` when Alt is released (a quick tap switches without a
+  visible overlay).  Popovers are filtered out of the list; Shift+Alt+Tab
+  walks backwards.
   The desktop background is `wm_prefs.bg_rgb` (default `#202030`), unless a
   The desktop background is `wm_prefs.bg_rgb` (default `#202030`), unless a
   wallpaper image is installed.  The image viewer and the Settings app's
   wallpaper image is installed.  The image viewer and the Settings app's
   *Appearance* page pre-render an image to a screen-aspect RGB565
   *Appearance* page pre-render an image to a screen-aspect RGB565

+ 20 - 0
docs/graphics.html

@@ -334,6 +334,26 @@
 			<li><strong>Focus and z-order</strong> are explicit; closing a window kills its session and frees its canvas.</li>
 			<li><strong>Focus and z-order</strong> are explicit; closing a window kills its session and frees its canvas.</li>
 		</ul>
 		</ul>
 
 
+		<h3>Alt+Tab switcher</h3>
+		<p>
+			Either Alt key is tracked in <span class="file">kernel/io.c</span>,
+			which turns Tab while Alt is held into
+			<code>IO_KEY_ALT_TAB</code> (and Shift+Alt+Tab into
+			<code>IO_KEY_ALT_SHIFT_TAB</code>) with the normal typematic
+			repeat. <code>wm_pump_keyboard()</code> consumes the chord so the
+			focused client never receives a Tab and queues
+			<code>WM_CHORD_NEXT</code>/<code>WM_CHORD_PREV</code> in the
+			eight-entry chord ring of <code>struct wm_status</code>, since
+			the WM has no window key queue of its own. The WM derives the
+			most-recently-focused order from the window <code>focused</code>
+			flags it refreshes every frame, draws a centred icon strip with
+			the selected app's name (popovers are excluded), and calls
+			<code>SYS_WM_FOCUS</code> for the selection when
+			<code>alt_down</code> drops. A quick Alt+Tab tap therefore
+			switches without a visible overlay; holding Alt keeps the strip
+			up while repeated Tabs walk the list.
+		</p>
+
 		<h3>Compositing cost</h3>
 		<h3>Compositing cost</h3>
 		<p>
 		<p>
 			The WM keeps a front-to-back order: the focused window first, then
 			The WM keeps a front-to-back order: the focused window first, then

+ 13 - 0
include/wm_abi.h

@@ -98,6 +98,11 @@ struct wm_mouse_info {
 	unsigned int buttons;      /* bit0 left, bit1 right, bit2 middle */
 	unsigned int buttons;      /* bit0 left, bit1 right, bit2 middle */
 };
 };
 
 
+/* Alt+Tab chord events queued in struct wm_status. */
+#define WM_CHORD_QUEUE 8
+#define WM_CHORD_NEXT 1   /* Alt+Tab: next window in MRU order */
+#define WM_CHORD_PREV 2   /* Shift+Alt+Tab: previous window */
+
 struct wm_status {
 struct wm_status {
 	struct wm_mouse_info mouse;
 	struct wm_mouse_info mouse;
 	unsigned int full_redraw;
 	unsigned int full_redraw;
@@ -120,6 +125,14 @@ struct wm_status {
 	 * (SYS_WM_OPEN_SETTINGS); the sequence makes every request visible. */
 	 * (SYS_WM_OPEN_SETTINGS); the sequence makes every request visible. */
 	unsigned int settings_req_seq;
 	unsigned int settings_req_seq;
 	unsigned int settings_req_section;
 	unsigned int settings_req_section;
+	/* Alt+Tab chords for the WM.  The WM has no window key queue of its
+	 * own, so the kernel consumes the chord and publishes the events
+	 * here.  chord_head is a monotonic write counter: the event at
+	 * position n lives in chord_keys[n % WM_CHORD_QUEUE].  alt_down lets
+	 * the WM commit the selection when Alt is released. */
+	unsigned int alt_down;
+	unsigned int chord_head;
+	unsigned int chord_keys[WM_CHORD_QUEUE];
 };
 };
 
 
 /* Wallpaper file: one header followed by tightly packed RGB565 pixels,
 /* Wallpaper file: one header followed by tightly packed RGB565 pixels,

+ 25 - 0
kernel/io.c

@@ -51,6 +51,7 @@ static const unsigned char scancode_to_ascii[256] __attribute__((section(".rodat
 
 
 static int shift_pressed;
 static int shift_pressed;
 static int ctrl_pressed;
 static int ctrl_pressed;
+static int alt_pressed;
 static int kbd_extended;  /* 1 after 0xE0, next byte is extended scancode */
 static int kbd_extended;  /* 1 after 0xE0, next byte is extended scancode */
 
 
 /* Software typematic repeat.  PS/2 keyboards repeat in hardware; the USB HID
 /* Software typematic repeat.  PS/2 keyboards repeat in hardware; the USB HID
@@ -304,6 +305,7 @@ void io_drain_keyboard(void)
 		(void)inb(KB_DATA);
 		(void)inb(KB_DATA);
 	kbd_head = kbd_tail = 0;
 	kbd_head = kbd_tail = 0;
 	kbd_extended = 0;
 	kbd_extended = 0;
+	alt_pressed = 0;
 	repeat_active = 0;
 	repeat_active = 0;
 }
 }
 
 
@@ -353,12 +355,18 @@ static int kbd_process_byte_inner(unsigned char sc)
 			kbd_repeat_stop(1, sc & 0x7F);
 			kbd_repeat_stop(1, sc & 0x7F);
 			if (sc == 0x9D)   /* right Ctrl release */
 			if (sc == 0x9D)   /* right Ctrl release */
 				ctrl_pressed = 0;
 				ctrl_pressed = 0;
+			if (sc == 0xB8)   /* right Alt release */
+				alt_pressed = 0;
 			return 0;  /* key release */
 			return 0;  /* key release */
 		}
 		}
 		if (sc == 0x1D) {         /* right Ctrl */
 		if (sc == 0x1D) {         /* right Ctrl */
 			ctrl_pressed = 1;
 			ctrl_pressed = 1;
 			return 0;
 			return 0;
 		}
 		}
+		if (sc == 0x38) {         /* right Alt (AltGr) */
+			alt_pressed = 1;
+			return 0;
+		}
 		if (sc == 0x14 || sc == 0x15) {
 		if (sc == 0x14 || sc == 0x15) {
 			/* Standard AT brightness down/up (E0 14 / E0 15);
 			/* Standard AT brightness down/up (E0 14 / E0 15);
 			 * ChromeOS EC action-key boards send these. */
 			 * ChromeOS EC action-key boards send these. */
@@ -392,6 +400,7 @@ static int kbd_process_byte_inner(unsigned char sc)
 	if (sc & 0x80) {
 	if (sc & 0x80) {
 		if (sc == 0xAA || sc == 0xB6) shift_pressed = 0;
 		if (sc == 0xAA || sc == 0xB6) shift_pressed = 0;
 		if (sc == 0x9D) ctrl_pressed = 0;
 		if (sc == 0x9D) ctrl_pressed = 0;
+		if (sc == 0xB8) alt_pressed = 0;
 		kbd_repeat_stop(0, sc & 0x7F);
 		kbd_repeat_stop(0, sc & 0x7F);
 		return 0;
 		return 0;
 	}
 	}
@@ -407,6 +416,17 @@ static int kbd_process_byte_inner(unsigned char sc)
 		shift_pressed = 1;
 		shift_pressed = 1;
 		return 0;
 		return 0;
 	}
 	}
+	if (sc == 0x38) {         /* left Alt */
+		alt_pressed = 1;
+		return 0;
+	}
+	if (sc == 0x0F && alt_pressed) {
+		/* Alt+Tab / Shift+Alt+Tab: the WM owns this as a window
+		 * switcher chord; do not deliver a plain Tab to the window. */
+		key = shift_pressed ? IO_KEY_ALT_SHIFT_TAB : IO_KEY_ALT_TAB;
+		kbd_repeat_start(key, 0, sc);
+		return key;
+	}
 	if (sc == 0x1D) {
 	if (sc == 0x1D) {
 		ctrl_pressed = 1;
 		ctrl_pressed = 1;
 		return 0;
 		return 0;
@@ -525,3 +545,8 @@ int io_keyboard_raw(void)
 {
 {
 	return kbd_raw;
 	return kbd_raw;
 }
 }
+
+int io_alt_down(void)
+{
+	return alt_pressed;
+}

+ 5 - 0
kernel/io.h

@@ -36,7 +36,12 @@ void io_drain_keyboard(void);
 #define IO_KEY_CTRL_RIGHT 0x10A
 #define IO_KEY_CTRL_RIGHT 0x10A
 #define IO_KEY_CTRL_DELETE 0x10B
 #define IO_KEY_CTRL_DELETE 0x10B
 #define IO_KEY_CTRL_BACKSPACE 0x10C
 #define IO_KEY_CTRL_BACKSPACE 0x10C
+#define IO_KEY_ALT_TAB       0x10D  /* Alt+Tab (Shift+Alt+Tab is separate) */
+#define IO_KEY_ALT_SHIFT_TAB 0x10E
 int io_poll_key(void);
 int io_poll_key(void);
+/* 1 while either Alt key is held.  The WM uses it to end an Alt+Tab
+   switcher session when Alt is released. */
+int io_alt_down(void);
 /* Same as io_poll_key but also reads port 0x60 when buffer empty (for full-screen programs). */
 /* Same as io_poll_key but also reads port 0x60 when buffer empty (for full-screen programs). */
 int io_poll_key_with_hw(void);
 int io_poll_key_with_hw(void);
 /* 1 when the IRQ1 storm guard masked the keyboard; the WM then polls the
 /* 1 when the IRQ1 storm guard masked the keyboard; the WM then polls the

+ 27 - 3
kernel/wm.c

@@ -63,6 +63,12 @@ static unsigned int wm_settings_req_seq;
 static unsigned int wm_settings_req_section;
 static unsigned int wm_settings_req_section;
 static unsigned int wm_last_seen;
 static unsigned int wm_last_seen;
 
 
+/* Alt+Tab chords are routed to the WM through struct wm_status: the WM is
+ * not a window, so it has no key queue of its own.  This ring decouples the
+ * 2 ms keyboard pump from the WM's 10 ms poll. */
+static unsigned int wm_chord_head;
+static unsigned int wm_chord_keys[WM_CHORD_QUEUE];
+
 static inline unsigned int irq_save_disable(void)
 static inline unsigned int irq_save_disable(void)
 {
 {
 	unsigned int flags;
 	unsigned int flags;
@@ -1159,6 +1165,8 @@ static void wm_route_scroll(unsigned int mx, unsigned int my)
 
 
 int wm_server_poll(struct wm_status *out)
 int wm_server_poll(struct wm_status *out)
 {
 {
+	unsigned int i;
+
 	if (!wm_server_is_wm() || !out)
 	if (!wm_server_is_wm() || !out)
 		return -1;
 		return -1;
 	wm_last_seen = get_ticks_ms();
 	wm_last_seen = get_ticks_ms();
@@ -1185,6 +1193,10 @@ int wm_server_poll(struct wm_status *out)
 	out->prefs = wm_prefs;
 	out->prefs = wm_prefs;
 	out->settings_req_seq = wm_settings_req_seq;
 	out->settings_req_seq = wm_settings_req_seq;
 	out->settings_req_section = wm_settings_req_section;
 	out->settings_req_section = wm_settings_req_section;
+	out->alt_down = io_alt_down() ? 1 : 0;
+	out->chord_head = wm_chord_head;
+	for (i = 0; i < WM_CHORD_QUEUE; i++)
+		out->chord_keys[i] = wm_chord_keys[i];
 	wm_full_redraw = 0;
 	wm_full_redraw = 0;
 	return 0;
 	return 0;
 }
 }
@@ -1389,8 +1401,6 @@ static int wm_point_in_rect(int px, int py,
 	       py >= ry && py < ry + (int)rh;
 	       py >= ry && py < ry + (int)rh;
 }
 }
 
 
-
-
 static void wm_focus_index(unsigned int idx)
 static void wm_focus_index(unsigned int idx)
 {
 {
 	unsigned int i;
 	unsigned int i;
@@ -1616,7 +1626,6 @@ void wm_pump_keyboard(void)
 
 
 	if (io_keyboard_raw())
 	if (io_keyboard_raw())
 		return;   /* an inline command is polling for keys itself */
 		return;   /* an inline command is polling for keys itself */
-
 	if (!fw)
 	if (!fw)
 		return;
 		return;
 
 
@@ -1631,6 +1640,21 @@ void wm_pump_keyboard(void)
 		}
 		}
 		if (!k)
 		if (!k)
 			break;
 			break;
+		/* Alt+Tab and Shift+Alt+Tab are global window-switcher chords:
+		 * they must not reach the focused window (or a popover).  A
+		 * typematic repeat after Alt was released is ignored. */
+		if (k == IO_KEY_ALT_TAB || k == IO_KEY_ALT_SHIFT_TAB) {
+			if (io_alt_down()) {
+				unsigned int slot = wm_chord_head %
+						    WM_CHORD_QUEUE;
+
+				wm_chord_keys[slot] =
+					k == IO_KEY_ALT_TAB ? WM_CHORD_NEXT :
+							      WM_CHORD_PREV;
+				wm_chord_head++;
+			}
+			continue;
+		}
 		if (fw->canvas_window) {
 		if (fw->canvas_window) {
 			unsigned int next = (fw->key_head + 1) % 16;
 			unsigned int next = (fw->key_head + 1) % 16;
 
 

+ 379 - 0
user/apps/wm/ucmd_wm.c

@@ -162,6 +162,31 @@ static unsigned int osd_seen_seq;
 static int osd_seen_valid;
 static int osd_seen_valid;
 static int osd_x, osd_y;
 static int osd_x, osd_y;
 static unsigned int osd_w, osd_h;
 static unsigned int osd_w, osd_h;
+/* Alt+Tab switcher: the kernel consumes the chord (so the focused window
+ * never sees the Tab) and queues WM_CHORD_NEXT/PREV events plus the Alt
+ * held state in wm_status.  The WM keeps the MRU order and the selection,
+ * draws the icon strip, and focuses the selected window when Alt is
+ * released.  sw_visible tracks pixels on screen so a window drag under it
+ * takes the full repaint path. */
+#define SW_ICON_MAX   48
+#define SW_CELL_W     68
+#define SW_PAD        16
+#define SW_LABEL_H    24
+static int sw_active;
+static unsigned int sw_sel;
+static unsigned int sw_count;
+static unsigned int sw_slots[WM_MAX_WINDOWS];
+static int sw_visible;
+static int sw_x, sw_y;
+static unsigned int sw_w, sw_h;
+/* Most-recently-focused order, maintained from the window focus flags (the
+ * WM is the only process that moves focus, but the kernel can pick a new
+ * focused window when one closes).  Popovers are filtered out of the list. */
+static unsigned int sw_mru[WM_MAX_WINDOWS];
+static unsigned int sw_mru_count;
+static int sw_last_focus = -1;
+static unsigned int sw_chord_seen;
+static int sw_chord_seen_valid;
 static char bar_msg[24];
 static char bar_msg[24];
 static unsigned int bar_msg_until;
 static unsigned int bar_msg_until;
 static int bar_msg_dirty;
 static int bar_msg_dirty;
@@ -266,6 +291,12 @@ static void sm_add_present(struct wm_present_info *p);
 static int sm_update_hover(unsigned int mx, unsigned int my);
 static int sm_update_hover(unsigned int mx, unsigned int my);
 static void sm_close(void);
 static void sm_close(void);
 static void sm_launch(int idx);
 static void sm_launch(int idx);
+static void sm_tile(int x, int y, int size, unsigned int rgb, char letter);
+
+/* Alt+Tab switcher overlay (defined next to the app-icon helpers). */
+static void sw_geom(void);
+static void sw_draw(void);
+static void sw_add_present(struct wm_present_info *p);
 
 
 /* Bottom dock (defined with the start-menu/launch helpers further down). */
 /* Bottom dock (defined with the start-menu/launch helpers further down). */
 static void dock_update(unsigned int mx, unsigned int my);
 static void dock_update(unsigned int mx, unsigned int my);
@@ -1544,6 +1575,10 @@ static void wm_overlays(struct wm_present_info *p)
 		sm_draw();
 		sm_draw();
 		sm_add_present(p);
 		sm_add_present(p);
 	}
 	}
+	if (sw_active) {
+		sw_draw();
+		sw_add_present(p);
+	}
 }
 }
 
 
 /* True when a screen rectangle contains pixels from an overlay rather than
 /* True when a screen rectangle contains pixels from an overlay rather than
@@ -1562,6 +1597,10 @@ static int shadow_overlay_covers(int x, int y, int w, int h)
 	    x < osd_x + (int)osd_w && x + w > osd_x &&
 	    x < osd_x + (int)osd_w && x + w > osd_x &&
 	    y < osd_y + (int)osd_h && y + h > osd_y)
 	    y < osd_y + (int)osd_h && y + h > osd_y)
 		return 1;
 		return 1;
+	if (sw_visible &&
+	    x < sw_x + (int)sw_w && x + w > sw_x &&
+	    y < sw_y + (int)sw_h && y + h > sw_y)
+		return 1;
 	return 0;
 	return 0;
 }
 }
 
 
@@ -1870,6 +1909,310 @@ static void sm_tile(int x, int y, int size, unsigned int rgb, char letter)
 		   x, y, (unsigned int)size, (unsigned int)size);
 		   x, y, (unsigned int)size, (unsigned int)size);
 }
 }
 
 
+/* ---- Alt+Tab switcher -------------------------------------------------
+ * The kernel consumes the chord and reports it plus the Alt held state in
+ * wm_status; the WM keeps the MRU order and the selection, draws a
+ * horizontal icon strip with the selected app's name below it, and focuses
+ * the selection when Alt is released.  The overlay itself is display
+ * only. */
+static unsigned int sw_cell;
+static unsigned int sw_icon;
+
+static int sw_slot_usable(unsigned int idx)
+{
+	return idx < WM_MAX_WINDOWS && wins[idx].info.used &&
+	       !wins[idx].info.popover;
+}
+
+static void sw_mru_remove(unsigned int idx)
+{
+	unsigned int i, j;
+
+	for (i = 0; i < sw_mru_count; i++) {
+		if (sw_mru[i] != idx)
+			continue;
+		for (j = i; j + 1 < sw_mru_count; j++)
+			sw_mru[j] = sw_mru[j + 1];
+		sw_mru_count--;
+		return;
+	}
+}
+
+static void sw_mru_touch(unsigned int idx)
+{
+	unsigned int i;
+
+	sw_mru_remove(idx);
+	for (i = sw_mru_count < WM_MAX_WINDOWS ? sw_mru_count :
+	     WM_MAX_WINDOWS - 1; i > 0; i--)
+		sw_mru[i] = sw_mru[i - 1];
+	sw_mru[0] = idx;
+	if (sw_mru_count < WM_MAX_WINDOWS)
+		sw_mru_count++;
+}
+
+/* Track focus transitions from the window info the WM refreshes every
+ * frame.  The focused normal window goes to the front of the MRU; closed
+ * entries are dropped. */
+static void sw_track_focus(void)
+{
+	int f = -1;
+	unsigned int i;
+
+	for (i = 0; i < WM_MAX_WINDOWS; i++)
+		if (wins[i].info.used && wins[i].info.focused &&
+		    !wins[i].info.popover) {
+			f = (int)i;
+			break;
+		}
+	if (f != sw_last_focus) {
+		sw_last_focus = f;
+		if (f >= 0)
+			sw_mru_touch((unsigned int)f);
+	}
+	for (i = 0; i < sw_mru_count; ) {
+		if (sw_slot_usable(sw_mru[i]))
+			i++;
+		else
+			sw_mru_remove(sw_mru[i]);
+	}
+}
+
+/* Build the switcher list in MRU order, then append any normal window the
+ * MRU does not know yet. */
+static void sw_build(void)
+{
+	unsigned int i, j, n = 0;
+
+	for (i = 0; i < sw_mru_count && n < WM_MAX_WINDOWS; i++)
+		if (sw_slot_usable(sw_mru[i]))
+			sw_slots[n++] = sw_mru[i];
+	for (i = 0; i < WM_MAX_WINDOWS && n < WM_MAX_WINDOWS; i++) {
+		int seen = 0;
+
+		if (!sw_slot_usable(i))
+			continue;
+		for (j = 0; j < n; j++)
+			if (sw_slots[j] == i) {
+				seen = 1;
+				break;
+			}
+		if (!seen)
+			sw_slots[n++] = i;
+	}
+	sw_count = n;
+}
+
+/* One chord step.  The first step opens the session with the focused window
+ * as entry 0, so the selection moves to the previously used window. */
+static void sw_step(int dir)
+{
+	if (!sw_active) {
+		sw_build();
+		if (sw_count < 2)
+			return;
+		sw_active = 1;
+		sw_sel = 0;
+		sm_close();
+		full_redraw = 1;
+	} else {
+		sw_build();
+		if (sw_count < 2) {
+			sw_active = 0;
+			sw_count = 0;
+			full_redraw = 1;
+			return;
+		}
+	}
+	if (dir > 0)
+		sw_sel = (sw_sel + 1) % sw_count;
+	else
+		sw_sel = (sw_sel + sw_count - 1) % sw_count;
+}
+
+/* Alt released: focus the selection.  A missing/closed entry is ignored. */
+static void sw_commit(void)
+{
+	unsigned int idx;
+
+	if (!sw_active)
+		return;
+	sw_active = 0;
+	if (sw_sel < sw_count) {
+		idx = sw_slots[sw_sel];
+		if (sw_slot_usable(idx))
+			u_wm_focus(idx);
+	}
+	sw_count = 0;
+	sw_sel = 0;
+	full_redraw = 1;
+}
+
+/* Consume queued chords and end the session when Alt is released.  Returns 1
+ * when an active session needs its panel repainted. */
+static int sw_process(struct wm_status *st)
+{
+	int changed = 0;
+	unsigned int n;
+
+	if (!sw_chord_seen_valid) {
+		sw_chord_seen = st->chord_head;
+		sw_chord_seen_valid = 1;
+	}
+	while (sw_chord_seen != st->chord_head) {
+		unsigned int key =
+			st->chord_keys[sw_chord_seen % WM_CHORD_QUEUE];
+
+		sw_chord_seen++;
+		if (key == WM_CHORD_NEXT)
+			sw_step(1);
+		else if (key == WM_CHORD_PREV)
+			sw_step(-1);
+		changed = 1;
+	}
+	if (!sw_active)
+		return 0;
+	if (!st->alt_down) {
+		sw_commit();
+		return 0;
+	}
+	/* A window may have closed while the session is open. */
+	{
+		unsigned int keep = sw_sel < sw_count ? sw_slots[sw_sel] :
+				    WM_MAX_WINDOWS;
+		unsigned int i, sel = 0;
+
+		n = 0;
+		for (i = 0; i < sw_count; i++)
+			if (sw_slot_usable(sw_slots[i])) {
+				if (sw_slots[i] == keep)
+					sel = n;
+				sw_slots[n++] = sw_slots[i];
+			}
+		if (n != sw_count) {
+			sw_count = n;
+			sw_sel = sel;
+			changed = 1;
+		}
+	}
+	if (sw_count < 2) {
+		sw_active = 0;
+		sw_count = 0;
+		full_redraw = 1;
+		return 0;
+	}
+	return changed;
+}
+
+static const struct sm_app *sw_app_for_title(const char *title)
+{
+	int i;
+
+	for (i = 0; i < SM_NAPP; i++) {
+		const char *t = sm_apps[i].win_title;
+		const char *a = title;
+
+		if (!t)
+			continue;
+		while (*a && *a == *t) {
+			a++;
+			t++;
+		}
+		if (*a == *t)
+			return &sm_apps[i];
+	}
+	return (void *)0;
+}
+
+static void sw_geom(void)
+{
+	unsigned int n = sw_count;
+
+	if (!n)
+		n = 1;
+	sw_cell = SW_CELL_W;
+	if ((int)(n * sw_cell + 2 * SW_PAD) > (int)fbw - 16 &&
+	    (int)fbw > (int)(2 * SW_PAD + 16 + n * 34))
+		sw_cell = (fbw - 16 - 2 * SW_PAD) / n;
+	sw_icon = sw_cell > 20 ? sw_cell - 20 : 16;
+	if (sw_icon > SW_ICON_MAX)
+		sw_icon = SW_ICON_MAX;
+	sw_w = n * sw_cell + 2 * SW_PAD;
+	sw_h = SW_PAD + sw_icon + 10 + SW_LABEL_H + SW_PAD;
+	sw_x = ((int)fbw - (int)sw_w) / 2;
+	if (sw_x < 8)
+		sw_x = 8;
+	sw_y = WM_BAR_H + ((int)fbh - WM_BAR_H - (int)sw_h) / 2;
+	if (sw_y < WM_BAR_H + 8)
+		sw_y = WM_BAR_H + 8;
+	if (sw_y + (int)sw_h > (int)fbh - 8)
+		sw_y = (int)fbh - 8 - (int)sw_h;
+}
+
+static void sw_draw(void)
+{
+	const char *label;
+	unsigned int i;
+	int ty, tx, tw;
+
+	if (!sw_active || !sw_count)
+		return;
+	sw_geom();
+	usurf_fill_round_alpha(&screen, sw_x, sw_y, (int)sw_w, (int)sw_h, 10,
+			       0x141824u, 242);
+
+	for (i = 0; i < sw_count; i++) {
+		const struct wm_window_info *w = &wins[sw_slots[i]].info;
+		const struct sm_app *a = sw_app_for_title(w->title);
+		int cell_x = sw_x + SW_PAD + (int)i * (int)sw_cell;
+		int ix = cell_x + ((int)sw_cell - (int)sw_icon) / 2;
+		int iy = sw_y + SW_PAD;
+
+		if (i == sw_sel)
+			usurf_fill_round_alpha(&screen, cell_x + 3, iy - 5,
+					       (int)sw_cell - 6,
+					       (int)sw_icon + 10, 8,
+					       0x3C69B4u, 230);
+		if (a) {
+			if (!usurf_app_icon(&screen, ix, iy, (int)sw_icon,
+					    (enum app_icon)a->icon))
+				sm_tile(ix, iy, (int)sw_icon, a->tile,
+					a->letter);
+		} else {
+			char letter = '?';
+
+			if (w->title[0]) {
+				letter = w->title[0];
+				if (letter >= 'a' && letter <= 'z')
+					letter = (char)(letter - 'a' + 'A');
+			}
+			sm_tile(ix, iy, (int)sw_icon, 0x4A4F62u, letter);
+		}
+	}
+
+	{
+		const struct wm_window_info *w =
+			&wins[sw_slots[sw_sel]].info;
+		const struct sm_app *a = sw_app_for_title(w->title);
+
+		label = a ? a->label : w->title;
+	}
+	ty = sw_y + SW_PAD + (int)sw_icon + 10 +
+	     (SW_LABEL_H - 16) / 2;
+	tw = (int)usurf_text_width(&screen, label);
+	tx = sw_x + ((int)sw_w - tw) / 2;
+	usurf_text(&screen, tx, ty, label, SM_TEXT,
+		   sw_x, sw_y, sw_w, sw_h);
+}
+
+static void sw_add_present(struct wm_present_info *p)
+{
+	if (!sw_active || !sw_count)
+		return;
+	sw_geom();
+	add_rect(p, sw_x, sw_y, sw_w, sw_h);
+}
+
 static void sm_draw_app(int i, const struct sm_layout *l)
 static void sm_draw_app(int i, const struct sm_layout *l)
 {
 {
 	const struct sm_app *a = &sm_apps[i];
 	const struct sm_app *a = &sm_apps[i];
@@ -2071,6 +2414,7 @@ static void do_update(struct wm_status *st)
 	int osd_hide = 0, osd_repaint = 0;
 	int osd_hide = 0, osd_repaint = 0;
 	int sm_dirty;
 	int sm_dirty;
 	int dock_redraw = 0, dock_overlap = 0;
 	int dock_redraw = 0, dock_overlap = 0;
+	int sw_repaint = 0;
 	unsigned int dj;
 	unsigned int dj;
 
 
 	/* The kernel bumps backlight_seq on every brightness key/command.
 	/* The kernel bumps backlight_seq on every brightness key/command.
@@ -2219,6 +2563,10 @@ static void do_update(struct wm_status *st)
 			}
 			}
 		}
 		}
 	}
 	}
+	/* Alt+Tab runs after the info refresh: focus transitions are visible
+	 * in wins[] and a window that closed mid-session is filtered out. */
+	sw_track_focus();
+	sw_repaint = sw_process(st);
 	/* Close-icon hover: only the topmost window under a free pointer
 	/* Close-icon hover: only the topmost window under a free pointer
 	 * reacts; an open menu, the dock and the top bar own the pointer when
 	 * reacts; an open menu, the dock and the top bar own the pointer when
 	 * they are under it.  A change repaints just the two button rects. */
 	 * they are under it.  A change repaints just the two button rects. */
@@ -2348,6 +2696,8 @@ static void do_update(struct wm_status *st)
 				osd_draw();
 				osd_draw();
 			osd_visible = osd_show;
 			osd_visible = osd_show;
 			sm_draw();
 			sm_draw();
+			sw_draw();
+			sw_visible = sw_active;
 			present.count = 0;
 			present.count = 0;
 			add_rect(&present, 0, 0, fbw, fbh);
 			add_rect(&present, 0, 0, fbw, fbh);
 			present.immediate = 0;
 			present.immediate = 0;
@@ -2376,6 +2726,11 @@ static void do_update(struct wm_status *st)
 		add_rect(&present, osd_x, osd_y, osd_w, osd_h);
 		add_rect(&present, osd_x, osd_y, osd_w, osd_h);
 		need = 1;
 		need = 1;
 	}
 	}
+	if (sw_repaint && sw_active) {
+		sw_geom();
+		add_rect(&present, sw_x, sw_y, sw_w, sw_h);
+		need = 1;
+	}
 	if (osd_show)
 	if (osd_show)
 		need = 1;
 		need = 1;
 	if (bar_dirty) {
 	if (bar_dirty) {
@@ -2419,6 +2774,25 @@ static void do_update(struct wm_status *st)
 			break;
 			break;
 		}
 		}
 	}
 	}
+	/* A window damage rectangle under the switcher panel would overwrite
+	 * its pixels; repaint the panel after the window draw. */
+	if (sw_active && !sw_repaint) {
+		sw_geom();
+		for (dj = 0; dj < present.count; dj++) {
+			int rx = present.rect[dj].x;
+			int ry = present.rect[dj].y;
+
+			if (rx < sw_x + (int)sw_w &&
+			    rx + (int)present.rect[dj].w > sw_x &&
+			    ry < sw_y + (int)sw_h &&
+			    ry + (int)present.rect[dj].h > sw_y) {
+				sw_repaint = 1;
+				add_rect(&present, sw_x, sw_y, sw_w, sw_h);
+				need = 1;
+				break;
+			}
+		}
+	}
 	if (dock_dirty || dock_overlap) {
 	if (dock_dirty || dock_overlap) {
 		dock_damage(&present);
 		dock_damage(&present);
 		dock_redraw = 1;
 		dock_redraw = 1;
@@ -2451,6 +2825,11 @@ static void do_update(struct wm_status *st)
 	}
 	}
 	osd_visible = osd_show;
 	osd_visible = osd_show;
 	sm_draw();
 	sm_draw();
+	if (sw_active)
+		sw_draw();
+	sw_visible = sw_active;
+	if (sw_active)
+		sw_add_present(&present);
 
 
 	for (i = 0; i < WM_MAX_WINDOWS; i++) {
 	for (i = 0; i < WM_MAX_WINDOWS; i++) {
 		struct uwin *u = &wins[i];
 		struct uwin *u = &wins[i];