2 Комити d7fae551d8 ... 3b2607df02

Аутор SHA1 Порука Датум
  Johannes Zellner 3b2607df02 Prefer a wired link over WiFi at boot пре 1 недеља
  Johannes Zellner 63a76a46bc Turn the top-bar power text into status icons пре 1 недеља
12 измењених фајлова са 322 додато и 128 уклоњено
  1. 22 16
      AGENTS.md
  2. 10 2
      asix.c
  3. 1 1
      docs/cpu.html
  4. 6 3
      docs/drivers.html
  5. 6 3
      docs/graphics.html
  6. 15 9
      docs/network.html
  7. 10 1
      e1000e.c
  8. 17 9
      kernel.c
  9. 3 3
      netdev.h
  10. 39 6
      r8168.c
  11. 9 12
      ucmd_wifi.c
  12. 184 63
      ucmd_wm.c

+ 22 - 16
AGENTS.md

@@ -327,13 +327,14 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   The right-aligned date/time text is itself a click target: `draw_bar()`
   records its rectangle, the pointer highlights it, and a click spawns
   `/ramfs/bin/settime` (the `Clock` launcher still opens the analog clock).
-  A network status icon is the rightmost bar item (the date/time, temperature
-  and AC/battery cluster sits to its left): a cable for a wired netdev,
-  signal bars for an associated WiFi link, a USB mark for RNDIS tethering,
-  and a red X otherwise.  The WM polls `SYS_NET_INFO` (and
-  `SYS_WIFI_STATUS` only while WiFi is active) at 1 Hz; clicking the icon
-  spawns one `/ramfs/bin/netmenu` window and anchors it under the icon once
-  its window exists (`u_wm_set_geom`).
+  The rightmost bar item is the AC/battery status icon: a plug while the
+  adapter is online and a level-filled battery on battery.  Hovering it paints
+  a percentage tooltip below the bar, above every window.  To its left is the
+  network status icon: a cable for a wired netdev, signal bars for an
+  associated WiFi link, a USB mark for RNDIS tethering, and a red X otherwise.
+  The WM polls `SYS_NET_INFO` (and `SYS_WIFI_STATUS` only while WiFi is active)
+  at 1 Hz; clicking the network icon spawns one `/ramfs/bin/netmenu` window and
+  anchors it under the icon once its window exists (`u_wm_set_geom`).
   The last launcher, `Chat`, spawns `/ramfs/bin/chat`; `WM_BTN_END_X`
   (`wm_abi.h`) is the launchers' right edge, and the bar notice and the
   right-aligned status never draw left of it.
@@ -594,8 +595,11 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   BSSIDs that share an SSID/security collapse to the strongest entry (the
   active BSSID wins while connected).  `wifi.c` (the Intel
   7265D/3165 netdev, see the WiFi bullet) is
-  last in the table; `struct netdev` has an optional `link_up()` hook, and
-  the boot DHCP is skipped while it returns 0.  `net.c` — ARP + ICMP echo, IPv4/UDP
+  last in the table; `struct netdev` has an optional `link_up()` hook
+  (e1000e `STATUS.LU`, r8168 `PHYstatus`, ASIX probe-time `BMSR`, WiFi
+  association state), the boot DHCP is skipped while it returns 0, and a
+  physically linked wired NIC keeps `wifi autoconnect` from overriding it.
+  `net.c` — ARP + ICMP echo, IPv4/UDP
   send + parse helpers,
   shared ARP resolution (`net_arp_resolve`) and routing (`net_next_hop`), frame
   decode, `net_checksum()`/`net_checksum_tcp()`.  ARP entries expire after five
@@ -679,9 +683,10 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   A successful menu/CLI connect also writes the SSID and passphrase to
   `/data/WIFI.CFG` (8.3, plaintext, BSSID zeroed so a mesh/repeater AP works);
   after the boot DHCP attempt `kernel_main` spawns
-  `/ramfs/bin/wifi autoconnect` when that file exists.  Autoconnect skips a
-  wired link that got a DHCP lease and leaves an already-linked WiFi card
-  alone; `/data` is the persistent FAT volume from the installer.
+  `/ramfs/bin/wifi autoconnect` when that file exists.  Autoconnect leaves a
+  wired link that is physically up alone and only tries WiFi when no wired
+  link is present; an already-linked WiFi card is also left alone.  `/data`
+  is the persistent FAT volume from the installer.
   `wifi forget` removes the saved record.  A FAT read may write a final
   partial sector past the requested length, so the credential reader keeps
   512 bytes of slack on the stack.
@@ -822,10 +827,11 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   125 °C, 230 = 100, other Bonnell = 90, otherwise 100).  The 330's 125 °C
   is Linux coretemp's empirical value; Intel only specifies TJ = 90 °C and
   Tcase-max = 85.2 °C, so `sensors` prints the raw delta and the 90/100/125 °C
-  scales as well as the top bar's `CPU 43C` before the clock (with
-  `AC on`/`AC off` and `BAT 87%` to its left on ThinkPads and the HP
-  Chromebook) and `cpuinfo`'s value plus EIST state.  Platforms without DTS
-  (QEMU, Pentium) hide it.
+  scales as well as the top bar's bare `43C` before the clock and, on
+  ThinkPads and the HP Chromebook, the AC/battery icon at the far right (a
+  plug while on AC, a level-filled battery on battery, with a percentage
+  tooltip below it on hover) and `cpuinfo`'s value plus EIST state.  Platforms
+  without DTS (QEMU, Pentium) hide it.
 - **Board sensors**: `it87.c/h` — read-only probe of the IT87xx Super I/O
   (the AT3IONT-I exposes three temperatures and three fan tachometers), used
   by `SYS_SENSORS` and the ring-3 `sensors` command to cross-check the DTS

+ 10 - 2
asix.c

@@ -334,12 +334,20 @@ static void net_print_info(void)
 	put_mac();
 	io_putstr("\n");
 	st = dev.phy ? mdio_read(MII_BMSR) : -1;
-	if (st >= 0 && (st & BMSR_LSTATUS))
+	dev.link = st >= 0 && (st & BMSR_LSTATUS);
+	if (dev.link)
 		io_putstr("  link up, 100 Mbit capable\n");
 	else
 		io_putstr("  link down\n");
 }
 
+/* Probe-time link state (chip_init waits for auto-negotiation): the boot
+ * network policy prefers this link over the saved WiFi network. */
+static int net_link_up(void)
+{
+	return dev.present && dev.chip_ready && dev.link;
+}
+
 static void net_reset(void)
 {
 	rx_pos = 0;
@@ -369,7 +377,7 @@ const struct netdev asix_netdev = {
 	asix_default_mask,
 	0,
 	0,
-	0,
+	net_link_up,
 };
 
 /* Read and parse the configuration descriptor.  The AX88772 has one

+ 1 - 1
docs/cpu.html

@@ -349,7 +349,7 @@
 			<li><strong>Nehalem/Westmere P-states.</strong> <code>power_init</code> enables SpeedStep, raises the package C-state limit to C6 when unlocked, reads min/max ratios from <code>PLATFORM_INFO</code> and starts at the maximum non-turbo ratio. The target ratio goes in bits 15:8 of <code>IA32_PERF_CTL</code> with the low byte zero.</li>
 			<li><strong>Airmont/Braswell P-states.</strong> Family 6 model <code>0x4C</code> reads the Atom ratio/VID MSRs (<code>0x66A..0x66D</code>), <code>MSR_FSB_FREQ</code> and the FSB table. The N3060 reports pstates 6..20, BCLK 80 MHz (1600 MHz nominal), turbo pstate 31 (2480 MHz) and VID 0x2D..0x3C. <code>power_init</code> enters at max non-turbo and the governor drops to pstate 6 (480 MHz); <code>IA32_PERF_CTL</code> carries the pstate in 15:8 and the Linux-interpolated VID in 7:0. Turbo is detected but not requested. The probe deliberately avoids package/power-SKU MSRs; an unsupported <code>rdmsr</code> before the IDT triple-faults.</li>
 			<li><strong>Governor.</strong> A 100 ms window picks min/max/linear target pstates; short busy streaks jump to max immediately. Each CPU writes its own <code>IA32_PERF_CTL</code>; <code>cpufreq [auto|min|max|off|&lt;pstate&gt;]</code> overrides it.</li>
-			<li><strong>Telemetry.</strong> <code>power</code> prints effective MHz from APERF/MPERF, the current/requested pstate and, on Airmont, the FSB, nominal MHz, turbo pstate/VID, VID range and live <code>PERF_CTL</code>; <code>sensors</code> prints DTS, board sensors and both EC types; <code>powerinfo</code> prints the AC adapter and battery state, and the WM top bar shows AC and battery percentage on ThinkPads and Chromebooks.</li>
+			<li><strong>Telemetry.</strong> <code>power</code> prints effective MHz from APERF/MPERF, the current/requested pstate and, on Airmont, the FSB, nominal MHz, turbo pstate/VID, VID range and live <code>PERF_CTL</code>; <code>sensors</code> prints DTS, board sensors and both EC types; <code>powerinfo</code> prints the AC adapter and battery state, and the WM top bar shows the temperature plus a far-right AC plug or level-filled battery icon (percentage tooltip below it on hover) on ThinkPads and Chromebooks.</li>
 			<li><strong>Thermal.</strong> Each CPU samples its DTS once a second from its scheduler tick; the hottest core is exposed to ring 3. TjMax comes from <code>IA32_TEMPERATURE_TARGET</code> on Nehalem/Westmere, the coretemp 90 °C table on Airmont, and the Bonnell table on older Atoms.</li>
 			<li><strong>Limits.</strong> Package C-states/S0ix remain firmware-managed; Airmont reports no C3/C6 residency and does not request the turbo pstate. The explicit <code>idle c6</code> override is the Nehalem <code>0x20</code> hint; use <code>idle auto</code> for the Airmont C6N (<code>0x58</code>) path.</li>
 		</ul>

+ 6 - 3
docs/drivers.html

@@ -509,9 +509,12 @@
 			adapter, battery model/manufacturer/serial, capacities, voltage,
 			rate and cycle count through <code>SYS_POWER_INFO</code>;
 			<code>power</code> prints ratios, effective MHz and C-state hints.
-			The WM top bar shows the AC adapter state and the first present
-			battery with a valid percentage next to the CPU temperature on
-			both ThinkPads and Chromebooks; machines without an EC show neither.
+			The WM top bar shows the CPU temperature and, on both ThinkPads
+			and Chromebooks, a status icon at the far right for the first
+			present battery: a plug while the AC adapter is online, a
+			level-filled battery on battery, with a percentage tooltip below
+			the icon on hover.  The tooltip is painted after the windows, so
+			it always stays on top.  Machines without an EC show neither.
 		</p>
 	</section>
 

+ 6 - 3
docs/graphics.html

@@ -139,11 +139,14 @@
 		<p>
 			The WM is a process that owns the desktop and a fixed array of eight
 			windows. It draws a system bar with the banner, launcher buttons,
-			clock, the CPU temperature and, on ThinkPads, the AC adapter state
-			and battery percentage, plus draggable
+			clock, the temperature, and, on ThinkPads and Chromebooks, an
+			AC/battery status icon at the far right (a plug on AC, a
+			level-filled battery on battery, with a percentage tooltip below
+			the bar on hover, painted above the windows), plus draggable
 			terminal and canvas windows with a title bar and close button.
 			Click-to-focus, drag-by-title-bar and close are routed through the
-			same pointer path. At the far right, a network status icon shows a
+			same pointer path. To the left of the rightmost AC/battery status
+			icon, a network status icon shows a
 			cable for wired Ethernet, signal bars for a connected WiFi link, a
 			USB mark for RNDIS tethering, or a red X when no link is up;
 			clicking it opens the anchored <code>bin/netmenu</code> window.

+ 15 - 9
docs/network.html

@@ -172,9 +172,11 @@
 			<span class="file">wifi.c</span> is the built-in WiFi of the
 			Chromebook (Intel 7265D/3165, see the
 			<a href="drivers.html#wifi">driver page</a>). It is last in the
-			registry, so it only becomes active when nothing wired probes, and
-			it reports <code>link_up() = 0</code> until it is connected: the boot
-			DHCP checks that optional hook and skips the 4 s attempt. The
+			registry, so the boot probe only makes it active when nothing
+			wired is present (a later <code>wifi connect</code> can still select
+			it over a wired NIC whose cable is unplugged), and it reports
+			<code>link_up() = 0</code> until it is connected: the boot DHCP
+			checks that optional hook and skips the 4 s attempt. The
 			<code>wifi</code> command scans, associates, runs the WPA2-PSK
 			handshake in ring 3 and then calls <code>SYS_WIFI_AUTHORIZE</code>,
 			which opens the data port and selects the <code>wifi</code> netdev.
@@ -193,9 +195,11 @@
 			the SSID and passphrase to <code>/data/WIFI.CFG</code> on the
 			persistent <code>NEBOSDATA</code> partition. After the boot DHCP
 			attempt <code>kernel_main</code> spawns
-			<code>wifi autoconnect</code> when that file exists; it skips a
-			wired link that already has a DHCP lease and leaves an existing
-			WiFi link alone. <code>wifi forget</code> removes the saved record.
+			<code>wifi autoconnect</code> when that file exists. It samples the
+			active NIC's <code>link_up()</code> state: a wired cable that is
+			physically up keeps the Ethernet path, while no wired link lets
+			WiFi associate and run DHCP. An existing WiFi link is left alone.
+			<code>wifi forget</code> removes the saved record.
 			The file is a plaintext 8.3 FAT file with the BSSID zeroed so the
 			strongest AP advertising that SSID is used.
 		</p>
@@ -235,9 +239,11 @@
 		</ul>
 		<p>
 			The static default address is <code>192.168.178.240</code>. At boot,
-			with a NIC present, a DHCP client runs with a 4-second budget
-			(requesting the current address); if no server answers, the static
-			address stays. The idle WM loop answers ARP/ICMP for the board's IP
+			when the active NIC reports a physical link, a DHCP client runs with
+			a 4-second budget (requesting the current address); if no server
+			answers, the static address stays, and a cable that is not plugged
+			in skips the wait so the saved WiFi network can be tried. The idle
+			WM loop answers ARP/ICMP for the board's IP
 			even when no command is running, so the board is reachable and
 			MAC-resolvable.
 		</p>

+ 10 - 1
e1000e.c

@@ -1743,6 +1743,15 @@ static void e1000e_get_mac(unsigned char out[6])
 		out[i] = mac[i];
 }
 
+/* Physical link state: kernel_main and `wifi autoconnect` prefer a wired
+ * link over the saved WiFi network. */
+static int e1000e_link_up(void)
+{
+	if (!present || !mmio)
+		return 0;
+	return (r32(REG_STATUS) & STATUS_LU) ? 1 : 0;
+}
+
 /* Give a descriptor back to the hardware.  The buffer address is rewritten
  * every time: the extended descriptor write-back overwrites the first 8
  * bytes with RSS/checksum data, so restoring it makes the ring survive even
@@ -2293,5 +2302,5 @@ const struct netdev e1000e_netdev = {
 	e1000e_dump_regs, 0, e1000e_reset, e1000e_set_speed, e1000e_set_mac,
 	e1000e_default_ip, e1000e_default_mask, e1000e_dump_rx,
 	e1000e_stop,
-	0,
+	e1000e_link_up,
 };

+ 17 - 9
kernel.c

@@ -568,17 +568,25 @@ void kernel_main(void)
 	boot_trace("terminal off");
 #endif
 
-	/* With a NIC present, try DHCP; on failure the static default stays.
-	 * Deliberately after the WM: a missing cable or DHCP server must not
-	 * delay the first frame. */
-	if (netdev_active() && !net_kept &&
-	    (!netdev_active()->link_up || netdev_active()->link_up()))
-		dhcp_run(4000);
-	boot_trace(net_kept ? "dhcp skipped" : "dhcp");
+	/* With a linked NIC present, try DHCP; on failure the static default
+	 * stays.  A wired NIC without a cable is skipped so the WiFi fallback
+	 * below does not have to wait out the 4 s timeout first.  Deliberately
+	 * after the WM: a missing cable or DHCP server must not delay the first
+	 * frame. */
+	{
+		const struct netdev *nd = netdev_active();
+		int linked = nd && netdev_link(nd);
+
+		if (linked && !net_kept)
+			dhcp_run(4000);
+		boot_trace(net_kept ? "dhcp skipped" :
+			   (linked ? "dhcp" : "dhcp no link"));
+	}
 
 	/* If the writable /data volume has saved WiFi credentials, let the
-	 * normal supplicant reconnect in the background.  It skips a working
-	 * wired link, and a missing /data or wifi helper is harmless. */
+	 * normal supplicant reconnect in the background.  It leaves a wired
+	 * link that is physically up alone, and a missing /data or wifi
+	 * helper is harmless. */
 	{
 		unsigned int sz = 0;
 		int is_dir = 0;

+ 3 - 3
netdev.h

@@ -29,9 +29,9 @@ struct netdev {
 	void (*dump_brief)(void);
 	/* Stop DMA before a kexec jump; may be NULL. */
 	void (*stop)(void);
-	/* Link state for drivers whose link can be down for long (WiFi before
-	 * association): 0 = no link, so the boot DHCP is skipped.  NULL means
-	 * "always up" (the wired drivers). */
+	/* Physical link state: 0 = no link, so the boot DHCP is skipped and
+	 * the saved WiFi network is tried instead.  Drivers without a reliable
+	 * link bit (ne2000, RNDIS) leave this NULL and count as always up. */
 	int (*link_up)(void);
 };
 

+ 39 - 6
r8168.c

@@ -545,6 +545,20 @@ static void set_rx_mode(void)
 	w32(REG_RxConfig, cfg);
 }
 
+/* Bounded poll of PHYstatus.LINK.  Only used to recover a link that was
+ * established before the probe reset; a missing cable must not delay boot. */
+static int wait_link(unsigned int timeout_ms)
+{
+	unsigned int t0 = get_ticks_ms();
+
+	while (!(r8(REG_PHYstatus) & PHYST_LINK)) {
+		if (timed_out(t0, timeout_ms))
+			return 0;
+		__asm__ volatile ("pause");
+	}
+	return 1;
+}
+
 static void phy_autoneg(void)
 {
 	int id1 = mdio_read(MII_PHYSID1);
@@ -564,8 +578,8 @@ static void phy_autoneg(void)
 /* ---- netdev interface ---- */
 
 /* Rings + configuration registers + PHY + RX/TX enable.  Used by both the
- * first probe and `netreset`. */
-static void hw_start(void)
+ * first probe and `netreset`; the latter forces a fresh auto-negotiation. */
+static void hw_start(int restart_phy)
 {
 	w8(REG_Cfg9346, Cfg9346_Unlock);
 	setup_rings();
@@ -580,7 +594,11 @@ static void hw_start(void)
 	w8(REG_Cfg9346, 0x00);
 	dbg("regs ok");
 
-	phy_autoneg();
+	/* Keep a link the firmware already established: the boot network
+	 * policy samples PHYstatus to choose between wired and WiFi, and
+	 * restarting auto-negotiation would make a plugged cable look down. */
+	if (restart_phy || !(r8(REG_PHYstatus) & PHYST_LINK))
+		phy_autoneg();
 	dbg("phy up");
 
 	w8(REG_ChipCmd, ChipCmd_RxEnb | ChipCmd_TxEnb);
@@ -595,6 +613,7 @@ int r8168_init(void)
 {
 	char buf[40];
 	unsigned int i;
+	int had_link;
 
 	if (inited)
 		return present ? 0 : -1;
@@ -632,6 +651,9 @@ int r8168_init(void)
 	}
 
 	xid = (r32(REG_TxConfig) >> 20) & 0xfcf;
+	/* Sample before the reset: a cable that was already linked keeps
+	 * the boot DHCP on the wired path instead of falling back to WiFi. */
+	had_link = (r8(REG_PHYstatus) & PHYST_LINK) != 0;
 
 	if (hw_reset() != 0) {
 		dbg("reset timeout");
@@ -654,7 +676,9 @@ int r8168_init(void)
 	}
 	dbg("ring memory ok");
 
-	hw_start();
+	hw_start(0);
+	if (had_link && !(r8(REG_PHYstatus) & PHYST_LINK))
+		wait_link(3000);
 
 	/* "<chip> XID <xxx> <mac>" */
 	i = 0;
@@ -688,7 +712,7 @@ void r8168_reset(void)
 		return;
 	}
 	read_mac();
-	hw_start();
+	hw_start(1);
 	dbg("netreset done");
 }
 
@@ -907,6 +931,15 @@ void r8168_print_info(void)
 	io_putchar('\n');
 }
 
+/* Physical link state: kernel_main and `wifi autoconnect` prefer a wired
+ * link over the saved WiFi network. */
+static int r8168_link_up(void)
+{
+	if (!present || !mmio)
+		return 0;
+	return (r8(REG_PHYstatus) & PHYST_LINK) ? 1 : 0;
+}
+
 void r8168_dump_regs(void)
 {
 	static const struct {
@@ -986,5 +1019,5 @@ const struct netdev r8168_netdev = {
 	r8168_init, r8168_get_mac, r8168_rx, r8168_tx, r8168_print_info,
 	r8168_dump_regs, r8168_set_loopback, r8168_reset,
 	0, 0, 0, 0, 0, 0,
-	0,
+	r8168_link_up,
 };

+ 9 - 12
ucmd_wifi.c

@@ -625,7 +625,7 @@ static int cmd_connect_file(const char *args)
 }
 
 /* Boot-time helper: reconnect to the network saved in /data/WIFI.CFG.  A
- * wired link that already has an address wins; an existing WiFi link is left
+ * wired link that is physically up wins; an existing WiFi link is left
  * alone. */
 static int cmd_autoconnect(void)
 {
@@ -638,6 +638,14 @@ static int cmd_autoconnect(void)
 
 	if (load_wifi_cred(&cfg) != 0)
 		return 0;
+	/* A wired link that is physically up wins: do not even probe the WiFi
+	 * card in that case.  Only fall back to WiFi when no cable is
+	 * connected. */
+	if (u_net_info(&ni) == 1 && ni.present && ni.link &&
+	    !name_eq(ni.name, "wifi")) {
+		wipe_cfg(&cfg);
+		return 0;
+	}
 	if (u_wifi(SYS_WIFI_STATUS, (int)&ws, 0) != 0 || !ws.present) {
 		wipe_cfg(&cfg);
 		return 0;
@@ -646,17 +654,6 @@ static int cmd_autoconnect(void)
 		wipe_cfg(&cfg);
 		return 0;
 	}
-	if (u_net_info(&ni) == 1 && ni.present && !name_eq(ni.name, "wifi")) {
-		unsigned char srv[4];
-		unsigned int lease = 0;
-
-		/* Only a real wired DHCP lease keeps the boot-time wired link;
-		 * the static fallback address does not mean the cable is up. */
-		if (u_syscall2(SYS_DHCP_INFO, (int)srv, (int)&lease) == 1) {
-			wipe_cfg(&cfg);
-			return 0;
-		}
-	}
 	for (i = 0; i < cfg.ssid_len; i++)
 		ssid[i] = (char)cfg.ssid[i];
 	ssid[cfg.ssid_len] = '\0';

+ 184 - 63
ucmd_wm.c

@@ -51,6 +51,20 @@ static int bar_temp_c = -1;
 static int bar_batt_pct = -1;
 static int bar_batt_charging;
 static int bar_ac_online = -1;
+/* Battery/AC status icon: a plug on AC, a level-filled battery otherwise.
+ * The rightmost bar item; hovering it shows a percentage tooltip below it. */
+#define BAR_PWR_W 24
+#define BAR_PWR_Y 6
+#define BAR_TIP_H 20
+static int bar_pwr_x = -1;
+static int bar_pwr_hot;
+static int bar_pwr_tip_x = -1;
+static int bar_pwr_tip_y;
+static unsigned int bar_pwr_tip_w, bar_pwr_tip_h;
+static int bar_pwr_tip_want;       /* tooltip computed for this frame */
+static int bar_pwr_tip_on;         /* tooltip pixels currently on screen */
+static int bar_pwr_tip_on_x, bar_pwr_tip_on_y;
+static unsigned int bar_pwr_tip_on_w, bar_pwr_tip_on_h;
 static char bar_msg[24];
 static unsigned int bar_msg_until;
 static int bar_msg_dirty;
@@ -277,15 +291,11 @@ static unsigned int temp_color(int c)
 	return 0x55FF55u;
 }
 
-/* "CPU 43C" (no libc printf here). */
+/* "43C" (no libc printf here). */
 static unsigned int temp_text(char *b, int c)
 {
 	unsigned int n = 0, v = (unsigned int)c;
 
-	b[n++] = 'C';
-	b[n++] = 'P';
-	b[n++] = 'U';
-	b[n++] = ' ';
 	if (v >= 100)
 		b[n++] = (char)('0' + (v / 100) % 10);
 	if (v >= 10)
@@ -296,15 +306,11 @@ static unsigned int temp_text(char *b, int c)
 	return n;
 }
 
-/* "BAT 87%" (no libc printf here). */
-static unsigned int batt_text(char *b, int pct)
+/* "87%" (no libc printf here). */
+static unsigned int pct_text(char *b, int pct)
 {
 	unsigned int n = 0, v = (unsigned int)pct;
 
-	b[n++] = 'B';
-	b[n++] = 'A';
-	b[n++] = 'T';
-	b[n++] = ' ';
 	if (v >= 100)
 		b[n++] = (char)('0' + (v / 100) % 10);
 	if (v >= 10)
@@ -326,29 +332,75 @@ static unsigned int batt_color(int pct, int charging, int ac)
 	return 0x55D2FFu;
 }
 
-/* "AC on" / "AC off" (no libc printf here).  Only drawn on machines whose
- * ThinkPad EC reported the adapter state. */
-static unsigned int ac_text(char *b, int online)
+/* Battery pictogram: outline, terminal and a fill proportional to the
+ * percentage.  A critical charge turns the outline red as well. */
+static void bar_icon_battery(int x, int y, int pct, unsigned int c)
 {
-	unsigned int n = 0;
+	unsigned int frame = pct <= 10 ? c : 0xD2D2D2u;
+	unsigned int fill = (unsigned int)((14 * pct + 99) / 100);
+
+	usurf_fill(&screen, x + 1, y + 2, 18, 2, frame);
+	usurf_fill(&screen, x + 1, y + 14, 18, 2, frame);
+	usurf_fill(&screen, x + 1, y + 2, 2, 14, frame);
+	usurf_fill(&screen, x + 17, y + 2, 2, 14, frame);
+	usurf_fill(&screen, x + 19, y + 6, 3, 6, frame);
+	if (fill > 14)
+		fill = 14;
+	if (fill)
+		usurf_fill(&screen, x + 3, y + 4, fill, 8, c);
+}
 
-	b[n++] = 'A';
-	b[n++] = 'C';
-	b[n++] = ' ';
-	b[n++] = 'o';
-	if (online) {
-		b[n++] = 'n';
-	} else {
-		b[n++] = 'f';
-		b[n++] = 'f';
-	}
-	b[n] = '\0';
-	return n;
+/* AC plug pictogram: two prongs and the body, kept compact in height. */
+static void bar_icon_plug(int x, int y, unsigned int c)
+{
+	usurf_fill(&screen, x + 7, y + 2, 3, 5, c);
+	usurf_fill(&screen, x + 14, y + 2, 3, 5, c);
+	usurf_fill(&screen, x + 5, y + 7, 14, 7, c);
+	usurf_fill(&screen, x + 7, y + 9, 10, 3, COL_BG);
+}
+
+/* Percentage tooltip: a small box below the status icon.  It is drawn after
+ * the windows (see bar_tooltip_overlay), so it always sits on top. */
+static void bar_draw_tooltip(void)
+{
+	char lb[8];
+	unsigned int lw;
+	int tx;
+
+	if (!bar_pwr_tip_want || bar_pwr_tip_x < 0)
+		return;
+	usurf_fill(&screen, bar_pwr_tip_x, bar_pwr_tip_y,
+		   bar_pwr_tip_w, bar_pwr_tip_h, 0x161622u);
+	usurf_fill(&screen, bar_pwr_tip_x, bar_pwr_tip_y,
+		   bar_pwr_tip_w, 1, 0x646464u);
+	usurf_fill(&screen, bar_pwr_tip_x,
+		   bar_pwr_tip_y + (int)bar_pwr_tip_h - 1,
+		   bar_pwr_tip_w, 1, 0x646464u);
+	usurf_fill(&screen, bar_pwr_tip_x, bar_pwr_tip_y,
+		   1, bar_pwr_tip_h, 0x646464u);
+	usurf_fill(&screen, bar_pwr_tip_x + (int)bar_pwr_tip_w - 1,
+		   bar_pwr_tip_y, 1, bar_pwr_tip_h, 0x646464u);
+	(void)pct_text(lb, bar_batt_pct);
+	lw = usurf_text_width(&screen, lb);
+	tx = bar_pwr_tip_x + ((int)bar_pwr_tip_w - (int)lw) / 2;
+	usurf_text(&screen, tx, bar_pwr_tip_y + 3, lb,
+		   batt_color(bar_batt_pct, bar_batt_charging, bar_ac_online),
+		   0, 0, fbw, fbh);
 }
 
-static unsigned int ac_color(int online)
+/* Publish the geometry computed by draw_bar() and redraw the tooltip. */
+static void bar_tooltip_commit(void)
 {
-	return online ? 0x55FF55u : 0xFFD250u;
+	if (bar_pwr_tip_want) {
+		bar_draw_tooltip();
+		bar_pwr_tip_on = 1;
+		bar_pwr_tip_on_x = bar_pwr_tip_x;
+		bar_pwr_tip_on_y = bar_pwr_tip_y;
+		bar_pwr_tip_on_w = bar_pwr_tip_w;
+		bar_pwr_tip_on_h = bar_pwr_tip_h;
+	} else {
+		bar_pwr_tip_on = 0;
+	}
 }
 
 static void draw_bar(void)
@@ -399,6 +451,9 @@ static void draw_bar(void)
 		bar_clock_hot = 0;
 		bar_net_x = -1;
 		bar_net_w = 0;
+		bar_pwr_x = -1;
+		bar_pwr_tip_x = -1;
+		bar_pwr_tip_want = 0;
 		n = 0;
 		while (msg[n])
 			dt[n] = msg[n], n++;
@@ -418,8 +473,51 @@ static void draw_bar(void)
 		bar_clock_w = 0;
 		bar_net_x = -1;
 		bar_net_w = 0;
-		/* The network status is the rightmost item, so it survives
-		 * narrow screens and a full temperature/battery cluster. */
+		bar_pwr_x = -1;
+		bar_pwr_tip_x = -1;
+		bar_pwr_tip_want = 0;
+		/* The AC/battery status icon is the rightmost item. */
+		if (bar_batt_pct >= 0 || bar_ac_online > 0) {
+			int px = right - BAR_PWR_W;
+
+			if (px >= min_x) {
+				if (bar_pwr_hot)
+					usurf_fill(&screen, px - 3, 2,
+						   BAR_PWR_W + 6,
+						   WM_BAR_H - 7, 0x3A3A55u);
+				if (bar_ac_online > 0)
+					bar_icon_plug(px, BAR_PWR_Y, 0x55FF55u);
+				else
+					bar_icon_battery(px, BAR_PWR_Y,
+							 bar_batt_pct,
+							 batt_color(bar_batt_pct,
+								    bar_batt_charging,
+								    bar_ac_online));
+				bar_pwr_x = px;
+				right = px - 8;
+				if (bar_pwr_hot && bar_batt_pct >= 0) {
+					char lb[8];
+					unsigned int lw, bw;
+					int bx;
+
+					(void)pct_text(lb, bar_batt_pct);
+					lw = usurf_text_width(&screen, lb);
+					bw = lw + 10;
+					bx = px + BAR_PWR_W - (int)bw;
+					if (bx < 4)
+						bx = 4;
+					if (bx + (int)bw > (int)fbw - 4)
+						bx = (int)fbw - 4 - (int)bw;
+					bar_pwr_tip_x = bx;
+					bar_pwr_tip_y = WM_BAR_H + 2;
+					bar_pwr_tip_w = bw;
+					bar_pwr_tip_h = BAR_TIP_H;
+					bar_pwr_tip_want = 1;
+				}
+			}
+		}
+		/* The network status sits to the left of the power icon, so it
+		 * survives narrow screens and a full temperature cluster. */
 		{
 			int nx = right - BAR_NET_W;
 
@@ -427,7 +525,7 @@ static void draw_bar(void)
 				if (bar_net_hot)
 					usurf_fill(&screen, nx - 3, 2,
 						   BAR_NET_W + 6,
-						   WM_BAR_H - 4, 0x3A3A55u);
+						   WM_BAR_H - 7, 0x3A3A55u);
 				bar_draw_net_icon(nx, BAR_NET_Y);
 				bar_net_x = nx;
 				bar_net_w = BAR_NET_W;
@@ -446,7 +544,7 @@ static void draw_bar(void)
 			if (bar_clock_hot)
 				usurf_fill(&screen, tx - 5, 2,
 					   (unsigned int)tw + 10,
-					   WM_BAR_H - 4, 0x3A3A55u);
+					   WM_BAR_H - 7, 0x3A3A55u);
 			usurf_text(&screen, tx, 5, dt, COL_BANNER, 0, 0,
 				   fbw, WM_BAR_H);
 			clock_chars = (unsigned int)tw;
@@ -467,36 +565,6 @@ static void draw_bar(void)
 				right = tx - 8;
 			}
 		}
-		if (bar_batt_pct >= 0) {
-			char bb[10];
-			int bx;
-
-			(void)batt_text(bb, bar_batt_pct);
-			bx = right - (int)usurf_text_width(&screen, bb);
-
-			if (bx >= min_x) {
-				usurf_text(&screen, bx, 5, bb,
-					   batt_color(bar_batt_pct,
-						      bar_batt_charging,
-						      bar_ac_online),
-					   0, 0, fbw, WM_BAR_H);
-				right = bx - 8;
-			}
-		}
-		if (bar_ac_online >= 0) {
-			char ab[8];
-			int ax;
-
-			(void)ac_text(ab, bar_ac_online);
-			ax = right - (int)usurf_text_width(&screen, ab);
-
-			if (ax >= min_x) {
-				usurf_text(&screen, ax, 5, ab,
-					   ac_color(bar_ac_online), 0, 0,
-					   fbw, WM_BAR_H);
-				right = ax - 8;
-			}
-		}
 	}
 }
 
@@ -794,6 +862,16 @@ static void add_rect(struct wm_present_info *p, int x, int y,
 	}
 }
 
+/* Redraw the power tooltip above whatever a render pass just painted. */
+static void bar_tooltip_overlay(struct wm_present_info *p)
+{
+	if (!bar_pwr_tip_on || !bar_pwr_tip_want)
+		return;
+	bar_draw_tooltip();
+	add_rect(p, bar_pwr_tip_on_x, bar_pwr_tip_on_y,
+		 bar_pwr_tip_on_w, bar_pwr_tip_on_h);
+}
+
 /* A focused window is topmost, so the shadow contains exactly its pixels in
  * the old rectangle.  Move that block and repaint only the old part the new
  * rectangle does not cover.  When either rectangle is partly off-screen the
@@ -833,6 +911,7 @@ static void move_window_pixels(struct uwin *u, int ox, int oy, int nx, int ny)
 	for (k = 0; k < np; k++)
 		add_rect(&present, pieces[k].x, pieces[k].y,
 			 pieces[k].w, pieces[k].h);
+	bar_tooltip_overlay(&present);
 	u_wm_present(&present);
 }
 
@@ -856,6 +935,7 @@ static void redraw_geom(struct uwin *u, int ox, int oy,
 		for (i = 0; i < present.count; i++)
 			draw_rect(present.rect[i].x, present.rect[i].y,
 				  present.rect[i].w, present.rect[i].h);
+		bar_tooltip_overlay(&present);
 		u_wm_present(&present);
 	}
 }
@@ -927,6 +1007,21 @@ static int bar_net_hit(unsigned int mx, unsigned int my)
 	       (int)mx < bar_net_x + (int)bar_net_w + 3;
 }
 
+/* The AC/battery icon has no click action, but hovering it reveals the
+ * percentage tooltip below it; the tooltip area keeps the hover alive. */
+static int bar_pwr_hit(unsigned int mx, unsigned int my)
+{
+	if (my >= WM_BAR_H)
+		return bar_pwr_tip_on &&
+		       (int)mx >= bar_pwr_tip_on_x &&
+		       (int)mx < bar_pwr_tip_on_x + (int)bar_pwr_tip_on_w &&
+		       (int)my >= bar_pwr_tip_on_y &&
+		       (int)my < bar_pwr_tip_on_y + (int)bar_pwr_tip_on_h;
+	return bar_pwr_x >= 0 &&
+	       (int)mx >= bar_pwr_x - 3 &&
+	       (int)mx < bar_pwr_x + BAR_PWR_W + 3;
+}
+
 static void do_update(struct wm_status *st)
 {
 	struct wm_present_info present;
@@ -984,6 +1079,14 @@ static void do_update(struct wm_status *st)
 			bar_dirty = 1;
 		}
 	}
+	{
+		int hot = bar_pwr_hit(st->mouse.x, st->mouse.y);
+
+		if (hot != bar_pwr_hot) {
+			bar_pwr_hot = hot;
+			bar_dirty = 1;
+		}
+	}
 
 	/* Refresh cached window state and mappings before opening a frame:
 	 * MAP may unmap/remap content. */
@@ -1129,6 +1232,7 @@ static void do_update(struct wm_status *st)
 		{
 			draw_rect(0, 0, fbw, fbh);
 			draw_bar();
+			bar_tooltip_commit();
 			present.count = 0;
 			add_rect(&present, 0, 0, fbw, fbh);
 			present.immediate = 0;
@@ -1169,6 +1273,15 @@ static void do_update(struct wm_status *st)
 		need = 1;
 	}
 
+	/* Repaint the previous tooltip area so hiding or moving it restores
+	 * the window/desktop underneath; the new one is drawn after the
+	 * windows below. */
+	if (bar_pwr_tip_on) {
+		need = 1;
+		add_rect(&present, bar_pwr_tip_on_x, bar_pwr_tip_on_y,
+			 bar_pwr_tip_on_w, bar_pwr_tip_on_h);
+	}
+
 	if (!need) {
 		/* Drag movement is handled below; a stationary mouse can be
 		 * left to the kernel's cursor overlay. */
@@ -1182,6 +1295,13 @@ static void do_update(struct wm_status *st)
 			  present.rect[i].w, present.rect[i].h);
 	if (bar_dirty)
 		draw_bar();
+	if (bar_pwr_tip_want) {
+		bar_tooltip_commit();
+		add_rect(&present, bar_pwr_tip_on_x, bar_pwr_tip_on_y,
+			 bar_pwr_tip_on_w, bar_pwr_tip_on_h);
+	} else {
+		bar_pwr_tip_on = 0;
+	}
 
 	for (i = 0; i < WM_MAX_WINDOWS; i++) {
 		struct uwin *u = &wins[i];
@@ -1433,6 +1553,7 @@ static void handle_mouse(struct wm_status *st)
 								  present.rect[i].y,
 								  present.rect[i].w,
 								  present.rect[i].h);
+							bar_tooltip_overlay(&present);
 							u_wm_present(&present);
 						}
 					}