ucmd_wm.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276
  1. /* Ring-3 window manager: maps the shadow buffer and each window's content,
  2. * draws the desktop/chrome and composites the terminal cell grids or gfx
  3. * canvases. All glyphs and colours are userland; the kernel only routes
  4. * input, stores window state and copies damage to VRAM. */
  5. #include "ucmd_lib.h"
  6. #include "uwm_lib.h"
  7. #include "version.h"
  8. #define COL_BG 0x202030u
  9. /* The active title bar uses the toolkit highlight (ngui_theme_dark.selection);
  10. * the inactive one stays neutral. Windows have no visible frame. */
  11. #define COL_TITLE_ON 0x365CA6u
  12. #define COL_TITLE_OFF 0x646464u
  13. #define COL_CLOSE 0xC64040u
  14. #define COL_WHITE 0xFFFFFFu
  15. #define COL_BANNER 0x55FF55u
  16. struct uwin {
  17. struct wm_window_info info;
  18. struct wm_map_info map;
  19. int mapped;
  20. unsigned int ack_seq;
  21. /* WIN_NORMAL, or maximized/snapped to a screen half; restore_* is the
  22. * geometry to return to when the window leaves that state. */
  23. int state;
  24. int restore_x, restore_y;
  25. unsigned int restore_w, restore_h;
  26. };
  27. /* Double-click detection on the title bar. */
  28. #define WM_DBLCLICK_MS 500
  29. #define WM_DBLCLICK_SLOP 8
  30. /* A maximized window restores only after the pointer actually starts a drag,
  31. * so a plain click on its title bar leaves it maximized. */
  32. #define WM_DRAG_START 3
  33. /* Window placement states. Maximize fills the work area below the bar; the
  34. * snap states fill its left or right half at full height. */
  35. #define WIN_NORMAL 0
  36. #define WIN_MAXIMIZED 1
  37. #define WIN_SNAP_LEFT 2
  38. #define WIN_SNAP_RIGHT 3
  39. /* Releasing a title-bar drag with the pointer this close to the left/right
  40. * screen edge snaps the window to that half; at the top edge it maximizes. */
  41. #define WM_SNAP_EDGE 2
  42. /* Edge resistance: a dragged window's edge stops at a screen edge or at the
  43. * facing edge of another window until the pointer overshoots it by this
  44. * many pixels, which makes exact side-by-side placement easy. */
  45. #define WM_RESIST 24
  46. static struct usurf screen;
  47. static struct uwin wins[WM_MAX_WINDOWS];
  48. static unsigned int fbw, fbh;
  49. static int drag_id = -1;
  50. static int drag_dx, drag_dy;
  51. static int drag_mx, drag_my; /* press position, for restore-on-drag */
  52. static int drag_moved; /* the pointer passed WM_DRAG_START */
  53. static int resize_id = -1;
  54. static int resize_edges; /* WM_EDGE_* bits captured at the press */
  55. static int resize_ox, resize_oy;
  56. static unsigned int resize_mx, resize_my, resize_ow, resize_oh;
  57. static unsigned int last_title_ms;
  58. static int last_title_id = -1, last_title_x, last_title_y;
  59. static unsigned int prev_buttons;
  60. static int full_redraw = 1;
  61. static int boot_term_spawned;
  62. static int boot_scan_spawned;
  63. static int boot_scan_pid;
  64. static int boot_scan_placed;
  65. static unsigned int boot_scan_deadline;
  66. static unsigned int bar_kexec_busy, bar_kexec_progress, bar_kexec_total;
  67. static int bar_temp_c = -1;
  68. static int bar_batt_pct = -1;
  69. static int bar_batt_charging;
  70. static int bar_ac_online = -1;
  71. /* Battery/AC status icon: the Adwaita AC adapter on AC, the battery outline
  72. * with our own level fill otherwise. The rightmost bar item; hovering it
  73. * shows a percentage tooltip below it. */
  74. #define BAR_PWR_W 24
  75. #define BAR_PWR_Y 7
  76. #define BAR_TIP_H 20
  77. static int bar_pwr_x = -1;
  78. static int bar_pwr_hot;
  79. static int bar_pwr_tip_x = -1;
  80. static int bar_pwr_tip_y;
  81. static unsigned int bar_pwr_tip_w, bar_pwr_tip_h;
  82. static int bar_pwr_tip_want; /* tooltip computed for this frame */
  83. static int bar_pwr_tip_on; /* tooltip pixels currently on screen */
  84. static int bar_pwr_tip_on_x, bar_pwr_tip_on_y;
  85. static unsigned int bar_pwr_tip_on_w, bar_pwr_tip_on_h;
  86. /* Brightness OSD: a centered bubble shown after the kernel's backlight
  87. * seq changes, then hidden after OSD_MS. */
  88. #define OSD_MS 3000
  89. #define OSD_W 210
  90. #define OSD_H 60
  91. static int osd_show; /* should be visible this frame */
  92. static int osd_visible; /* pixels are on screen */
  93. static int osd_level;
  94. static unsigned int osd_until;
  95. static unsigned int osd_seen_seq;
  96. static int osd_seen_valid;
  97. static int osd_x, osd_y;
  98. static unsigned int osd_w, osd_h;
  99. static char bar_msg[24];
  100. static unsigned int bar_msg_until;
  101. static int bar_msg_dirty;
  102. /* Rectangle of the right-aligned date/time text, so a click there can open
  103. * the Date & Time app. w == 0 means "not drawn" (e.g. kexec banner). */
  104. static int bar_clock_x = -1;
  105. static unsigned int bar_clock_w;
  106. static int bar_clock_hot;
  107. static unsigned int stat_frames, stat_ms, stat_px, stat_max, stat_last;
  108. /* Network status icon (right side of the bar). */
  109. #define BAR_NET_W 24
  110. #define BAR_NET_Y 7
  111. #define BAR_NET_NONE 0
  112. #define BAR_NET_WIRED 1
  113. #define BAR_NET_WIFI 2
  114. #define BAR_NET_USB 3
  115. static int bar_net_x = -1;
  116. static unsigned int bar_net_w;
  117. static int bar_net_hot;
  118. static int bar_net_kind;
  119. static int bar_net_bars;
  120. static unsigned int bar_net_color = 0xFF5050u;
  121. static int control_pid;
  122. static void bar_notice(const char *msg)
  123. {
  124. unsigned int i;
  125. for (i = 0; msg[i] && i + 1 < sizeof(bar_msg); i++)
  126. bar_msg[i] = msg[i];
  127. bar_msg[i] = '\0';
  128. bar_msg_until = u_ticks_ms() + 2000;
  129. bar_msg_dirty = 1;
  130. }
  131. static int free_window_slot(void)
  132. {
  133. unsigned int i;
  134. for (i = 0; i < WM_MAX_WINDOWS; i++)
  135. if (!wins[i].info.used)
  136. return 1;
  137. return 0;
  138. }
  139. /* Find a window by owner pid without waiting for the cached sync state:
  140. * boot ordering needs it before the terminal is spawned. */
  141. static int window_index_pid(int pid, struct wm_window_info *out)
  142. {
  143. unsigned int i;
  144. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  145. struct wm_window_info info;
  146. if (u_wm_info(i, &info) == 1 && info.used &&
  147. (int)info.pid == pid) {
  148. if (out)
  149. *out = info;
  150. return (int)i;
  151. }
  152. }
  153. return -1;
  154. }
  155. static unsigned int rgb16(unsigned int r, unsigned int g, unsigned int b)
  156. {
  157. return (r << 16) | (g << 8) | b;
  158. }
  159. /* Rainbow gradient: hue 0..1535 (6*256) maps to R→Y→G→C→B→M→R, the same
  160. * integer interpolation the kernel bar uses. */
  161. static unsigned int rainbow_rgb(unsigned int hue)
  162. {
  163. unsigned int seg = hue / 256, f = hue % 256;
  164. unsigned int r1, g1, b1, r2, g2, b2;
  165. switch (seg) {
  166. case 0: r1=255; g1=0; b1=0; r2=255; g2=255; b2=0; break;
  167. case 1: r1=255; g1=255; b1=0; r2=0; g2=255; b2=0; break;
  168. case 2: r1=0; g1=255; b1=0; r2=0; g2=255; b2=255; break;
  169. case 3: r1=0; g1=255; b1=255; r2=0; g2=0; b2=255; break;
  170. case 4: r1=0; g1=0; b1=255; r2=255; g2=0; b2=255; break;
  171. default:r1=255; g1=0; b1=255; r2=255; g2=0; b2=0; break;
  172. }
  173. return rgb16((r1 * (256 - f) + r2 * f) / 256,
  174. (g1 * (256 - f) + g2 * f) / 256,
  175. (b1 * (256 - f) + b2 * f) / 256);
  176. }
  177. static void fill_clip(int x, int y, unsigned int w, unsigned int h,
  178. unsigned int rgb, int cx, int cy,
  179. unsigned int cw, unsigned int ch)
  180. {
  181. int x0 = x, y0 = y, x1 = x + (int)w, y1 = y + (int)h;
  182. if (x0 < cx) x0 = cx;
  183. if (y0 < cy) y0 = cy;
  184. if (x1 > cx + (int)cw) x1 = cx + (int)cw;
  185. if (y1 > cy + (int)ch) y1 = cy + (int)ch;
  186. if (x1 > x0 && y1 > y0)
  187. usurf_fill(&screen, x0, y0, (unsigned int)(x1 - x0),
  188. (unsigned int)(y1 - y0), rgb);
  189. }
  190. static void bar_button(unsigned int x, unsigned int y, unsigned int w,
  191. unsigned int h, const char *label)
  192. {
  193. int tw = (int)usurf_text_width(&screen, label);
  194. usurf_fill(&screen, x, y, w, h, 0x963CD2u);
  195. usurf_text(&screen, (int)x + ((int)w - tw) / 2, (int)(y + 3),
  196. label, COL_WHITE, 0, 0, fbw, WM_BAR_H);
  197. }
  198. static int name_is(const char *a, const char *b)
  199. {
  200. while (*a && *b) {
  201. if (*a != *b)
  202. return 0;
  203. a++;
  204. b++;
  205. }
  206. return *a == *b;
  207. }
  208. static int net_rssi_bars(int rssi)
  209. {
  210. if (rssi >= -55)
  211. return 4;
  212. if (rssi >= -65)
  213. return 3;
  214. if (rssi >= -75)
  215. return 2;
  216. return 1;
  217. }
  218. /* Small top-bar pictograms: Adwaita symbolic masks from the pack, centered
  219. * in their 24 px hit slots. */
  220. static void bar_draw_net_icon(int x, int y)
  221. {
  222. enum uwm_icon ic;
  223. switch (bar_net_kind) {
  224. case BAR_NET_WIRED:
  225. ic = UWM_ICON_NET_WIRED;
  226. break;
  227. case BAR_NET_WIFI:
  228. ic = bar_net_bars > 0 ? UWM_ICON_NET_WIFI_0 + bar_net_bars :
  229. UWM_ICON_NET_WIFI_OFF;
  230. break;
  231. case BAR_NET_USB:
  232. ic = UWM_ICON_NET_USB;
  233. break;
  234. default:
  235. ic = UWM_ICON_NET_WIRED_OFF;
  236. break;
  237. }
  238. (void)usurf_icon(&screen, x + 4, y, ic, bar_net_color);
  239. }
  240. /* 1 Hz poll: cached state -> the bar's network icon. The WiFi status call is
  241. * only made when WiFi is the active netdev, so a machine without a card does
  242. * not run a PCI scan on every tick. */
  243. static int poll_net_status(void)
  244. {
  245. struct sys_netinfo ni;
  246. int kind = BAR_NET_NONE, bars = 0, have_ip = 0;
  247. unsigned int color = 0xFF5050u;
  248. int changed;
  249. int i;
  250. if (u_net_info(&ni) == 1 && ni.present) {
  251. for (i = 0; i < 4; i++)
  252. if (ni.ip[i])
  253. have_ip = 1;
  254. if (name_is(ni.name, "wifi")) {
  255. struct sys_wifi_status ws;
  256. if (u_wifi(SYS_WIFI_STATUS, (int)&ws, 0) == 0 &&
  257. ws.present) {
  258. kind = BAR_NET_WIFI;
  259. if (ws.state >= SYS_WIFI_ST_ASSOC) {
  260. bars = net_rssi_bars(ws.rssi);
  261. color = (ws.state >= SYS_WIFI_ST_LINK &&
  262. have_ip) ?
  263. 0x55FF55u : 0xFFD250u;
  264. } else {
  265. /* Card present, not associated. */
  266. bars = 0;
  267. color = 0xFF5050u;
  268. }
  269. }
  270. } else if (name_is(ni.name, "rndis")) {
  271. kind = BAR_NET_USB;
  272. color = have_ip ? 0x55FF55u : 0xFFD250u;
  273. } else {
  274. kind = BAR_NET_WIRED;
  275. color = have_ip ? 0x55FF55u : 0xFFD250u;
  276. }
  277. }
  278. changed = kind != bar_net_kind || bars != bar_net_bars ||
  279. color != bar_net_color;
  280. bar_net_kind = kind;
  281. bar_net_bars = bars;
  282. bar_net_color = color;
  283. return changed;
  284. }
  285. static unsigned int temp_color(int c)
  286. {
  287. if (c >= 80)
  288. return 0xFF5050u;
  289. if (c >= 60)
  290. return 0xFFD250u;
  291. return 0x55FF55u;
  292. }
  293. /* "43C" (no libc printf here). */
  294. static unsigned int temp_text(char *b, int c)
  295. {
  296. unsigned int n = 0, v = (unsigned int)c;
  297. if (v >= 100)
  298. b[n++] = (char)('0' + (v / 100) % 10);
  299. if (v >= 10)
  300. b[n++] = (char)('0' + (v / 10) % 10);
  301. b[n++] = (char)('0' + v % 10);
  302. b[n++] = 'C';
  303. b[n] = '\0';
  304. return n;
  305. }
  306. /* "87%" (no libc printf here). */
  307. static unsigned int pct_text(char *b, int pct)
  308. {
  309. unsigned int n = 0, v = (unsigned int)pct;
  310. if (v >= 100)
  311. b[n++] = (char)('0' + (v / 100) % 10);
  312. if (v >= 10)
  313. b[n++] = (char)('0' + (v / 10) % 10);
  314. b[n++] = (char)('0' + v % 10);
  315. b[n++] = '%';
  316. b[n] = '\0';
  317. return n;
  318. }
  319. static unsigned int batt_color(int pct)
  320. {
  321. if (pct <= 10)
  322. return 0xFF5050u;
  323. return 0x55FF55u;
  324. }
  325. /* Battery: the Adwaita outline with our own level fill over the lower
  326. * interior. The symbol's body is striped, so the interior is cleared to a
  327. * dim shade of the state colour first; the level then reads as a bright
  328. * block inside the outline. A critical charge turns the outline red too.
  329. * Geometry of battery-symbolic at 16 px: body x2..13, cavity x4..11 and
  330. * y4..13. */
  331. static unsigned int batt_dim(unsigned int c)
  332. {
  333. return (c >> 2) & 0x3F3F3F;
  334. }
  335. static void bar_icon_battery(int x, int y, int pct, unsigned int c)
  336. {
  337. unsigned int fill = (unsigned int)((10 * pct + 99) / 100);
  338. int ix = x + 4;
  339. if (fill > 10)
  340. fill = 10;
  341. if (!usurf_icon(&screen, ix, y, UWM_ICON_BATTERY, c))
  342. return;
  343. usurf_fill(&screen, ix + 4, y + 4, 8, 10, batt_dim(c));
  344. if (fill)
  345. usurf_fill(&screen, ix + 4, y + 14 - (int)fill, 8, fill, c);
  346. }
  347. /* AC adapter: the Adwaita plug symbol. */
  348. static void bar_icon_ac(int x, int y, unsigned int c)
  349. {
  350. (void)usurf_icon(&screen, x + 4, y, UWM_ICON_AC, c);
  351. }
  352. /* Percentage tooltip: a small box below the status icon. It is drawn after
  353. * the windows (see bar_tooltip_overlay), so it always sits on top. */
  354. static void bar_draw_tooltip(void)
  355. {
  356. char lb[8];
  357. unsigned int lw;
  358. int tx;
  359. if (!bar_pwr_tip_want || bar_pwr_tip_x < 0)
  360. return;
  361. usurf_fill(&screen, bar_pwr_tip_x, bar_pwr_tip_y,
  362. bar_pwr_tip_w, bar_pwr_tip_h, 0x161622u);
  363. usurf_fill(&screen, bar_pwr_tip_x, bar_pwr_tip_y,
  364. bar_pwr_tip_w, 1, 0x646464u);
  365. usurf_fill(&screen, bar_pwr_tip_x,
  366. bar_pwr_tip_y + (int)bar_pwr_tip_h - 1,
  367. bar_pwr_tip_w, 1, 0x646464u);
  368. usurf_fill(&screen, bar_pwr_tip_x, bar_pwr_tip_y,
  369. 1, bar_pwr_tip_h, 0x646464u);
  370. usurf_fill(&screen, bar_pwr_tip_x + (int)bar_pwr_tip_w - 1,
  371. bar_pwr_tip_y, 1, bar_pwr_tip_h, 0x646464u);
  372. (void)pct_text(lb, bar_batt_pct);
  373. lw = usurf_text_width(&screen, lb);
  374. tx = bar_pwr_tip_x + ((int)bar_pwr_tip_w - (int)lw) / 2;
  375. usurf_text(&screen, tx, bar_pwr_tip_y + 3, lb,
  376. batt_color(bar_batt_pct), 0, 0, fbw, fbh);
  377. }
  378. /* Publish the geometry computed by draw_bar() and redraw the tooltip. */
  379. static void bar_tooltip_commit(void)
  380. {
  381. if (bar_pwr_tip_want) {
  382. bar_draw_tooltip();
  383. bar_pwr_tip_on = 1;
  384. bar_pwr_tip_on_x = bar_pwr_tip_x;
  385. bar_pwr_tip_on_y = bar_pwr_tip_y;
  386. bar_pwr_tip_on_w = bar_pwr_tip_w;
  387. bar_pwr_tip_on_h = bar_pwr_tip_h;
  388. } else {
  389. bar_pwr_tip_on = 0;
  390. }
  391. }
  392. static void draw_bar(void)
  393. {
  394. char dt[48];
  395. unsigned int n, i, x, y;
  396. usurf_fill(&screen, 0, 0, fbw, WM_BAR_H, COL_BG);
  397. usurf_text(&screen, 4, 5, OS_NAME " v" OS_VERSION, COL_BANNER,
  398. 0, 0, fbw, WM_BAR_H);
  399. bar_button(WM_BTN_X, WM_BTN_Y, WM_BTN_W, WM_BTN_H, "New");
  400. bar_button(WM_BTN_CLOCK_X, WM_BTN_CLOCK_Y, WM_BTN_CLOCK_W,
  401. WM_BTN_CLOCK_H, "Clock");
  402. bar_button(WM_BTN_COLORS_X, WM_BTN_COLORS_Y, WM_BTN_COLORS_W,
  403. WM_BTN_COLORS_H, "Colors");
  404. bar_button(WM_BTN_FILES_X, WM_BTN_FILES_Y, WM_BTN_FILES_W,
  405. WM_BTN_FILES_H, "Files");
  406. bar_button(WM_BTN_MINES_X, WM_BTN_MINES_Y, WM_BTN_MINES_W,
  407. WM_BTN_MINES_H, "Mines");
  408. bar_button(WM_BTN_WEB_X, WM_BTN_WEB_Y, WM_BTN_WEB_W,
  409. WM_BTN_WEB_H, "Web");
  410. bar_button(WM_BTN_CARDS_X, WM_BTN_CARDS_Y, WM_BTN_CARDS_W,
  411. WM_BTN_CARDS_H, "Cards");
  412. bar_button(WM_BTN_GALLERY_X, WM_BTN_GALLERY_Y, WM_BTN_GALLERY_W,
  413. WM_BTN_GALLERY_H, "Gallery");
  414. bar_button(WM_BTN_CHAT_X, WM_BTN_CHAT_Y, WM_BTN_CHAT_W,
  415. WM_BTN_CHAT_H, "Chat");
  416. bar_button(WM_BTN_DISKS_X, WM_BTN_DISKS_Y, WM_BTN_DISKS_W,
  417. WM_BTN_DISKS_H, "Disks");
  418. bar_button(WM_BTN_PROCESS_X, WM_BTN_PROCESS_Y, WM_BTN_PROCESS_W,
  419. WM_BTN_PROCESS_H, "Tasks");
  420. if (bar_msg_until &&
  421. (int)(u_ticks_ms() - bar_msg_until) < 0)
  422. usurf_text(&screen, (int)(WM_BTN_END_X + 8),
  423. 5, bar_msg, 0xFFD250u, 0, 0, fbw, WM_BAR_H);
  424. /* Rainbow separator (same y as the kernel's old bar). */
  425. for (x = 0; x < fbw; x++) {
  426. unsigned int c = rainbow_rgb((x * (6 * 256)) / (fbw ? fbw : 1));
  427. for (y = WM_BAR_H - 5; y < WM_BAR_H - 3; y++)
  428. usurf_fill(&screen, (int)x, (int)y, 1, 1, c);
  429. }
  430. if (bar_kexec_busy) {
  431. static const char msg[] = "loading new kernel over serial";
  432. unsigned int dots = bar_kexec_total ?
  433. (bar_kexec_progress * 12 / bar_kexec_total) : 0;
  434. bar_clock_x = -1;
  435. bar_clock_w = 0;
  436. bar_clock_hot = 0;
  437. bar_net_x = -1;
  438. bar_net_w = 0;
  439. bar_pwr_x = -1;
  440. bar_pwr_tip_x = -1;
  441. bar_pwr_tip_want = 0;
  442. n = 0;
  443. while (msg[n])
  444. dt[n] = msg[n], n++;
  445. for (i = 0; i < 12; i++)
  446. dt[n + i] = (i < dots) ? '.' : ' ';
  447. n += 12;
  448. dt[n] = '\0';
  449. usurf_text(&screen,
  450. (int)fbw - (int)usurf_text_width(&screen, dt) - 4,
  451. 5, dt, 0xFFC864u, 0, 0, fbw, WM_BAR_H);
  452. } else {
  453. unsigned int clock_chars = 0;
  454. int right = (int)fbw - 4;
  455. int min_x = (int)WM_BTN_END_X + 4;
  456. bar_clock_x = -1;
  457. bar_clock_w = 0;
  458. bar_net_x = -1;
  459. bar_net_w = 0;
  460. bar_pwr_x = -1;
  461. bar_pwr_tip_x = -1;
  462. bar_pwr_tip_want = 0;
  463. /* The AC/battery status icon is the rightmost item. */
  464. if (bar_batt_pct >= 0 || bar_ac_online > 0) {
  465. int px = right - BAR_PWR_W;
  466. if (px >= min_x) {
  467. if (bar_pwr_hot)
  468. usurf_fill(&screen, px - 3, 2,
  469. BAR_PWR_W + 6,
  470. WM_BAR_H - 7, 0x3A3A55u);
  471. if (bar_ac_online > 0)
  472. bar_icon_ac(px, BAR_PWR_Y, 0x55FF55u);
  473. else
  474. bar_icon_battery(px, BAR_PWR_Y,
  475. bar_batt_pct,
  476. batt_color(bar_batt_pct));
  477. bar_pwr_x = px;
  478. right = px - 8;
  479. if (bar_pwr_hot && bar_batt_pct >= 0) {
  480. char lb[8];
  481. unsigned int lw, bw;
  482. int bx;
  483. (void)pct_text(lb, bar_batt_pct);
  484. lw = usurf_text_width(&screen, lb);
  485. bw = lw + 10;
  486. bx = px + BAR_PWR_W - (int)bw;
  487. if (bx < 4)
  488. bx = 4;
  489. if (bx + (int)bw > (int)fbw - 4)
  490. bx = (int)fbw - 4 - (int)bw;
  491. bar_pwr_tip_x = bx;
  492. bar_pwr_tip_y = WM_BAR_H + 2;
  493. bar_pwr_tip_w = bw;
  494. bar_pwr_tip_h = BAR_TIP_H;
  495. bar_pwr_tip_want = 1;
  496. }
  497. }
  498. }
  499. /* The network status sits to the left of the power icon, so it
  500. * survives narrow screens and a full temperature cluster. */
  501. {
  502. int nx = right - BAR_NET_W;
  503. if (nx >= min_x) {
  504. if (bar_net_hot)
  505. usurf_fill(&screen, nx - 3, 2,
  506. BAR_NET_W + 6,
  507. WM_BAR_H - 7, 0x3A3A55u);
  508. bar_draw_net_icon(nx, BAR_NET_Y);
  509. bar_net_x = nx;
  510. bar_net_w = BAR_NET_W;
  511. right = nx - 8;
  512. }
  513. }
  514. if (u_rtc_datetime(dt, sizeof(dt)) == 0) {
  515. char cdt[12];
  516. const char *txt = dt;
  517. int tw = (int)usurf_text_width(&screen, dt);
  518. int tx = right - tw;
  519. unsigned int i;
  520. if (tx < min_x) {
  521. /* Compact form "MM-DD HH:MM" from
  522. * "YYYY-MM-DD HH:MM:SS" when the launcher row is
  523. * too wide for the full date. */
  524. for (i = 0; i < 11 && dt[5 + i]; i++)
  525. cdt[i] = dt[5 + i];
  526. cdt[i] = '\0';
  527. txt = cdt;
  528. tw = (int)usurf_text_width(&screen, cdt);
  529. tx = right - tw;
  530. }
  531. if (tx >= min_x) {
  532. bar_clock_x = tx;
  533. bar_clock_w = (unsigned int)tw;
  534. if (bar_clock_hot)
  535. usurf_fill(&screen, tx - 5, 2,
  536. (unsigned int)tw + 10,
  537. WM_BAR_H - 7, 0x3A3A55u);
  538. usurf_text(&screen, tx, 5, txt, COL_BANNER, 0, 0,
  539. fbw, WM_BAR_H);
  540. clock_chars = (unsigned int)tw;
  541. }
  542. }
  543. if (clock_chars)
  544. right -= (int)clock_chars + 8;
  545. if (bar_temp_c >= 0) {
  546. char tb[12];
  547. int tx;
  548. (void)temp_text(tb, bar_temp_c);
  549. tx = right - (int)usurf_text_width(&screen, tb);
  550. if (tx >= min_x) {
  551. usurf_text(&screen, tx, 5, tb,
  552. temp_color(bar_temp_c), 0, 0,
  553. fbw, WM_BAR_H);
  554. right = tx - 8;
  555. }
  556. }
  557. }
  558. }
  559. /* A popover is a flat, chromeless panel: the canvas fills the window. */
  560. static void draw_popover_clip(unsigned int idx, int cx, int cy,
  561. unsigned int cw, unsigned int ch)
  562. {
  563. struct uwin *u = &wins[idx];
  564. struct wm_window_info *w = &u->info;
  565. unsigned int cwid = w->content_w, chei = w->content_h;
  566. if (u->mapped && u->map.kind == WM_KIND_GFX) {
  567. struct uwm_canvas c;
  568. c.px = (const unsigned short *)u->map.va;
  569. c.w = u->map.w;
  570. c.h = u->map.h;
  571. if (c.w == cwid && c.h == chei) {
  572. usurf_canvas(&screen, &c, w->x, w->y,
  573. cx, cy, cw, ch);
  574. return;
  575. }
  576. }
  577. fill_clip(w->x, w->y, cwid, chei, 0x181C28u, cx, cy, cw, ch);
  578. }
  579. static void draw_window_clip(unsigned int idx, int cx, int cy,
  580. unsigned int cw, unsigned int ch)
  581. {
  582. struct uwin *u = &wins[idx];
  583. struct wm_window_info *w = &u->info;
  584. int x = w->x, y = w->y;
  585. unsigned int pw = w->w;
  586. int bx = x + (int)pw - WM_BORDER_W - WM_CLOSE_W;
  587. int cony = y + WM_BORDER_W + WM_TITLE_H;
  588. int content_x = x + WM_BORDER_W;
  589. unsigned int content_w = w->content_w;
  590. unsigned int content_h = w->content_h;
  591. if (w->popover) {
  592. draw_popover_clip(idx, cx, cy, cw, ch);
  593. return;
  594. }
  595. /* Title bar and close button; windows have no visible frame. */
  596. fill_clip(x, y, pw, WM_TITLE_H,
  597. w->focused ? COL_TITLE_ON : COL_TITLE_OFF, cx, cy, cw, ch);
  598. usurf_text(&screen, x + 3, y + 2, w->title, COL_WHITE,
  599. cx, cy, cw, ch);
  600. fill_clip(bx, y, WM_CLOSE_W, WM_TITLE_H, COL_CLOSE, cx, cy, cw, ch);
  601. usurf_text(&screen, bx + 4, y + 2, "x", COL_WHITE,
  602. cx, cy, cw, ch);
  603. /* Content: every window presents an RGB565 canvas (the terminal
  604. * renders its cells into one; gfx commands own theirs). A mapped
  605. * canvas covers the whole content area, so it does not need the black
  606. * underlay. */
  607. if (u->mapped && u->map.kind == WM_KIND_GFX) {
  608. struct uwm_canvas c;
  609. c.px = (const unsigned short *)u->map.va;
  610. c.w = u->map.w;
  611. c.h = u->map.h;
  612. if (c.w != content_w || c.h != content_h)
  613. fill_clip(content_x, cony, content_w, content_h,
  614. 0x000000u, cx, cy, cw, ch);
  615. usurf_canvas(&screen, &c, content_x, cony,
  616. cx, cy, cw, ch);
  617. } else {
  618. fill_clip(content_x, cony, content_w, content_h,
  619. 0x000000u, cx, cy, cw, ch);
  620. }
  621. }
  622. /* Front-to-back window order: popovers float above everything, then the
  623. * focused window, then the others in descending slot order. Painting and
  624. * hit-testing share it. */
  625. static unsigned int build_order(int *order)
  626. {
  627. unsigned int i, n = 0;
  628. for (i = 0; i < WM_MAX_WINDOWS; i++)
  629. if (wins[i].info.used && wins[i].info.popover)
  630. order[n++] = (int)i;
  631. for (i = 0; i < WM_MAX_WINDOWS; i++)
  632. if (wins[i].info.used && wins[i].info.focused &&
  633. !wins[i].info.popover)
  634. order[n++] = (int)i;
  635. for (i = WM_MAX_WINDOWS; i-- > 0;)
  636. if (wins[i].info.used && !wins[i].info.focused &&
  637. !wins[i].info.popover)
  638. order[n++] = (int)i;
  639. return n;
  640. }
  641. struct orect {
  642. int x, y;
  643. unsigned int w, h;
  644. };
  645. #define OCC_MAX 64
  646. /* r minus cut, split into up to four disjoint rectangles. */
  647. static unsigned int rect_sub(struct orect *out, const struct orect *r,
  648. const struct orect *cut)
  649. {
  650. int rx0 = r->x, ry0 = r->y;
  651. int rx1 = r->x + (int)r->w, ry1 = r->y + (int)r->h;
  652. int cx0 = cut->x, cy0 = cut->y;
  653. int cx1 = cut->x + (int)cut->w, cy1 = cut->y + (int)cut->h;
  654. unsigned int n = 0;
  655. if (cx0 < rx0) cx0 = rx0;
  656. if (cy0 < ry0) cy0 = ry0;
  657. if (cx1 > rx1) cx1 = rx1;
  658. if (cy1 > ry1) cy1 = ry1;
  659. if (cx1 <= cx0 || cy1 <= cy0) {
  660. out[0] = *r;
  661. return 1;
  662. }
  663. if (cy0 > ry0) {
  664. out[n].x = rx0;
  665. out[n].y = ry0;
  666. out[n].w = (unsigned int)(rx1 - rx0);
  667. out[n].h = (unsigned int)(cy0 - ry0);
  668. n++;
  669. }
  670. if (cy1 < ry1) {
  671. out[n].x = rx0;
  672. out[n].y = cy1;
  673. out[n].w = (unsigned int)(rx1 - rx0);
  674. out[n].h = (unsigned int)(ry1 - cy1);
  675. n++;
  676. }
  677. if (cx0 > rx0) {
  678. out[n].x = rx0;
  679. out[n].y = cy0;
  680. out[n].w = (unsigned int)(cx0 - rx0);
  681. out[n].h = (unsigned int)(cy1 - cy0);
  682. n++;
  683. }
  684. if (cx1 < rx1) {
  685. out[n].x = cx1;
  686. out[n].y = cy0;
  687. out[n].w = (unsigned int)(rx1 - cx1);
  688. out[n].h = (unsigned int)(cy1 - cy0);
  689. n++;
  690. }
  691. return n;
  692. }
  693. /* Back-to-front repaint, used only if the visible-region list overflows. */
  694. static void draw_rect_naive(int x, int y, unsigned int w, unsigned int h)
  695. {
  696. unsigned int i;
  697. usurf_fill(&screen, x, y, w, h, COL_BG);
  698. for (i = 0; i < WM_MAX_WINDOWS; i++)
  699. if (wins[i].info.used && !wins[i].info.focused &&
  700. !wins[i].info.popover &&
  701. x < wins[i].info.x + (int)wins[i].info.w &&
  702. x + (int)w > wins[i].info.x &&
  703. y < wins[i].info.y + (int)wins[i].info.h &&
  704. y + (int)h > wins[i].info.y)
  705. draw_window_clip(i, x, y, w, h);
  706. for (i = 0; i < WM_MAX_WINDOWS; i++)
  707. if (wins[i].info.used && wins[i].info.focused &&
  708. !wins[i].info.popover &&
  709. x < wins[i].info.x + (int)wins[i].info.w &&
  710. x + (int)w > wins[i].info.x &&
  711. y < wins[i].info.y + (int)wins[i].info.h &&
  712. y + (int)h > wins[i].info.y)
  713. draw_window_clip(i, x, y, w, h);
  714. for (i = 0; i < WM_MAX_WINDOWS; i++)
  715. if (wins[i].info.used && wins[i].info.popover &&
  716. x < wins[i].info.x + (int)wins[i].info.w &&
  717. x + (int)w > wins[i].info.x &&
  718. y < wins[i].info.y + (int)wins[i].info.h &&
  719. y + (int)h > wins[i].info.y)
  720. draw_window_clip(i, x, y, w, h);
  721. }
  722. static void draw_rect(int x, int y, unsigned int w, unsigned int h)
  723. {
  724. static struct orect cur[OCC_MAX], nxt[OCC_MAX];
  725. struct orect dmg;
  726. int order[WM_MAX_WINDOWS];
  727. unsigned int n, i, j, ncur, nnext;
  728. int overflow = 0;
  729. int x0 = x, y0 = y, x1 = x + (int)w, y1 = y + (int)h;
  730. if (w == 0 || h == 0)
  731. return;
  732. if (x0 < 0) x0 = 0;
  733. if (y0 < 0) y0 = 0;
  734. if (x1 > (int)fbw) x1 = (int)fbw;
  735. if (y1 > (int)fbh) y1 = (int)fbh;
  736. if (x1 <= x0 || y1 <= y0)
  737. return;
  738. dmg.x = x0;
  739. dmg.y = y0;
  740. dmg.w = (unsigned int)(x1 - x0);
  741. dmg.h = (unsigned int)(y1 - y0);
  742. usurf_fill(&screen, dmg.x, dmg.y, dmg.w, dmg.h, COL_BG);
  743. n = build_order(order);
  744. for (i = 0; i < n; i++) {
  745. struct uwin *u = &wins[order[i]];
  746. int wx0 = u->info.x, wy0 = u->info.y;
  747. int wx1 = wx0 + (int)u->info.w, wy1 = wy0 + (int)u->info.h;
  748. if (wx0 < dmg.x) wx0 = dmg.x;
  749. if (wy0 < dmg.y) wy0 = dmg.y;
  750. if (wx1 > x1) wx1 = x1;
  751. if (wy1 > y1) wy1 = y1;
  752. if (wx1 <= wx0 || wy1 <= wy0)
  753. continue;
  754. cur[0].x = wx0;
  755. cur[0].y = wy0;
  756. cur[0].w = (unsigned int)(wx1 - wx0);
  757. cur[0].h = (unsigned int)(wy1 - wy0);
  758. ncur = 1;
  759. for (j = 0; j < i && ncur; j++) {
  760. struct uwin *f = &wins[order[j]];
  761. struct orect fr;
  762. unsigned int k;
  763. fr.x = f->info.x;
  764. fr.y = f->info.y;
  765. fr.w = f->info.w;
  766. fr.h = f->info.h;
  767. nnext = 0;
  768. for (k = 0; k < ncur; k++) {
  769. if (nnext + 4 > OCC_MAX) {
  770. overflow = 1;
  771. break;
  772. }
  773. nnext += rect_sub(&nxt[nnext], &cur[k], &fr);
  774. }
  775. if (overflow)
  776. break;
  777. for (k = 0; k < nnext; k++)
  778. cur[k] = nxt[k];
  779. ncur = nnext;
  780. }
  781. if (overflow)
  782. break;
  783. for (j = 0; j < ncur; j++)
  784. draw_window_clip((unsigned int)order[i], cur[j].x,
  785. cur[j].y, cur[j].w, cur[j].h);
  786. }
  787. if (overflow)
  788. draw_rect_naive(dmg.x, dmg.y, dmg.w, dmg.h);
  789. }
  790. /* Invisible resize border: edge bits for a point in the band around the
  791. * window's outer edge (WM_RESIZE_IN px inside and WM_RESIZE_OUT px outside).
  792. * Popovers have no chrome and never resize. */
  793. #define WM_EDGE_LEFT 1
  794. #define WM_EDGE_RIGHT 2
  795. #define WM_EDGE_TOP 4
  796. #define WM_EDGE_BOTTOM 8
  797. static int resize_edges_at(const struct uwin *u, int mx, int my)
  798. {
  799. const struct wm_window_info *w = &u->info;
  800. int l = w->x, t = w->y;
  801. int r = l + (int)w->w, b = t + (int)w->h;
  802. int near_x, near_y, e = 0;
  803. if (w->popover)
  804. return 0;
  805. near_x = mx >= l - WM_RESIZE_OUT && mx < r + WM_RESIZE_OUT;
  806. near_y = my >= t - WM_RESIZE_OUT && my < b + WM_RESIZE_OUT;
  807. if (near_y && mx >= l - WM_RESIZE_OUT && mx < l + WM_RESIZE_IN)
  808. e |= WM_EDGE_LEFT;
  809. if (near_y && mx >= r - WM_RESIZE_IN && mx < r + WM_RESIZE_OUT)
  810. e |= WM_EDGE_RIGHT;
  811. if (near_x && my >= t - WM_RESIZE_OUT && my < t + WM_RESIZE_IN)
  812. e |= WM_EDGE_TOP;
  813. if (near_x && my >= b - WM_RESIZE_IN && my < b + WM_RESIZE_OUT)
  814. e |= WM_EDGE_BOTTOM;
  815. return e;
  816. }
  817. /* Topmost window at (mx,my): its resize band when the point is inside one,
  818. * otherwise the window rectangle. *edges is 0 for a plain rectangle hit.
  819. * Bands are not considered inside the top bar, which the WM owns. */
  820. static int resize_hit(int mx, int my, int *edges)
  821. {
  822. int order[WM_MAX_WINDOWS];
  823. unsigned int n = build_order(order), i;
  824. *edges = 0;
  825. for (i = 0; i < n; i++) {
  826. struct uwin *u = &wins[order[i]];
  827. int e = 0;
  828. if (!u->info.used)
  829. continue;
  830. if (my >= WM_BAR_H)
  831. e = resize_edges_at(u, mx, my);
  832. if (e) {
  833. *edges = e;
  834. return order[i];
  835. }
  836. if (mx >= u->info.x && my >= u->info.y &&
  837. mx < u->info.x + (int)u->info.w &&
  838. my < u->info.y + (int)u->info.h)
  839. return order[i];
  840. }
  841. return -1;
  842. }
  843. static int resize_cursor(int edges)
  844. {
  845. switch (edges) {
  846. case WM_EDGE_LEFT:
  847. case WM_EDGE_RIGHT:
  848. return CURSOR_EW_RESIZE;
  849. case WM_EDGE_TOP:
  850. case WM_EDGE_BOTTOM:
  851. return CURSOR_NS_RESIZE;
  852. case WM_EDGE_LEFT | WM_EDGE_TOP:
  853. case WM_EDGE_RIGHT | WM_EDGE_BOTTOM:
  854. return CURSOR_NWSE_RESIZE;
  855. case WM_EDGE_RIGHT | WM_EDGE_TOP:
  856. case WM_EDGE_LEFT | WM_EDGE_BOTTOM:
  857. return CURSOR_NESW_RESIZE;
  858. default:
  859. return CURSOR_DEFAULT;
  860. }
  861. }
  862. /* Close button hit area (the top-right WM_CLOSE_W columns of the title bar). */
  863. static int close_hit(const struct wm_window_info *w, int mx, int my)
  864. {
  865. if (w->popover)
  866. return 0;
  867. return mx >= w->x + (int)w->w - WM_CLOSE_W &&
  868. mx < w->x + (int)w->w &&
  869. my >= w->y && my < w->y + WM_TITLE_H;
  870. }
  871. static void clamp_geom(struct uwin *u, int *x, int *y)
  872. {
  873. /* Match wm_clamp_window(): allow the window to hang off the left,
  874. * right and bottom edges but keep the title bar reachable and below
  875. * the system bar. */
  876. int minx = WM_DRAG_VISIBLE - (int)u->info.w;
  877. int maxx = (int)fbw - WM_DRAG_VISIBLE;
  878. int miny = WM_BAR_H;
  879. int maxy = (int)fbh - WM_BORDER_W - WM_TITLE_H;
  880. if (minx > maxx)
  881. minx = maxx = 0;
  882. if (miny > maxy)
  883. miny = maxy = WM_BAR_H;
  884. if (*x < minx)
  885. *x = minx;
  886. else if (*x > maxx)
  887. *x = maxx;
  888. if (*y < miny)
  889. *y = miny;
  890. else if (*y > maxy)
  891. *y = maxy;
  892. }
  893. static void add_rect(struct wm_present_info *p, int x, int y,
  894. unsigned int w, unsigned int h)
  895. {
  896. if (w == 0 || h == 0)
  897. return;
  898. if (p->count < 4) {
  899. p->rect[p->count].x = x;
  900. p->rect[p->count].y = y;
  901. p->rect[p->count].w = w;
  902. p->rect[p->count].h = h;
  903. p->count++;
  904. } else {
  905. /* Four is enough for normal updates; fold the rest into the
  906. * last rect rather than dropping pixels. */
  907. int x0 = p->rect[3].x, y0 = p->rect[3].y;
  908. int x1 = x0 + (int)p->rect[3].w, y1 = y0 + (int)p->rect[3].h;
  909. int nx1 = x + (int)w, ny1 = y + (int)h;
  910. if (x < x0) x0 = x;
  911. if (y < y0) y0 = y;
  912. if (nx1 > x1) x1 = nx1;
  913. if (ny1 > y1) y1 = ny1;
  914. p->rect[3].x = x0;
  915. p->rect[3].y = y0;
  916. p->rect[3].w = (unsigned int)(x1 - x0);
  917. p->rect[3].h = (unsigned int)(y1 - y0);
  918. }
  919. }
  920. /* Redraw the power tooltip above whatever a render pass just painted. */
  921. static void bar_tooltip_overlay(struct wm_present_info *p)
  922. {
  923. if (!bar_pwr_tip_on || !bar_pwr_tip_want)
  924. return;
  925. bar_draw_tooltip();
  926. add_rect(p, bar_pwr_tip_on_x, bar_pwr_tip_on_y,
  927. bar_pwr_tip_on_w, bar_pwr_tip_on_h);
  928. }
  929. /* Center the brightness bubble in the work area, clamped on small screens. */
  930. static void osd_geom(void)
  931. {
  932. int x, y;
  933. osd_w = OSD_W;
  934. osd_h = OSD_H;
  935. x = ((int)fbw - (int)osd_w) / 2;
  936. y = WM_BAR_H + ((int)fbh - WM_BAR_H - (int)osd_h) * 3 / 5;
  937. if (x < 0)
  938. x = 0;
  939. if (x + (int)osd_w > (int)fbw)
  940. x = (int)fbw - (int)osd_w;
  941. if (y < WM_BAR_H)
  942. y = WM_BAR_H;
  943. if (y + (int)osd_h > (int)fbh)
  944. y = (int)fbh - (int)osd_h;
  945. osd_x = x;
  946. osd_y = y;
  947. }
  948. /* A small sun: a filled circle plus four rays, all from 1-pixel fills. */
  949. static void osd_sun(int cx, int cy)
  950. {
  951. static const unsigned char half[11] = { 2, 4, 5, 6, 6, 6, 6, 6, 5, 4, 2 };
  952. int i;
  953. for (i = -5; i <= 5; i++)
  954. usurf_fill(&screen, cx - half[i + 5], cy + i,
  955. half[i + 5] * 2 + 1, 1, 0xFFD250u);
  956. usurf_fill(&screen, cx - 1, cy - 9, 2, 3, 0xFFD250u);
  957. usurf_fill(&screen, cx - 1, cy + 7, 2, 3, 0xFFD250u);
  958. usurf_fill(&screen, cx - 9, cy - 1, 3, 2, 0xFFD250u);
  959. usurf_fill(&screen, cx + 7, cy - 1, 3, 2, 0xFFD250u);
  960. }
  961. static void osd_draw(void)
  962. {
  963. char pct[8];
  964. unsigned int n = 0;
  965. unsigned int bar_x, bar_y, bar_w;
  966. int x, y, tw;
  967. osd_geom();
  968. x = osd_x;
  969. y = osd_y;
  970. usurf_fill(&screen, x, y, osd_w, osd_h, 0x26263Au);
  971. usurf_fill(&screen, x, y, osd_w, 1, 0x70709Au);
  972. usurf_fill(&screen, x, y + (int)osd_h - 1, osd_w, 1, 0x70709Au);
  973. usurf_fill(&screen, x, y + 1, 1, osd_h - 2, 0x70709Au);
  974. usurf_fill(&screen, x + (int)osd_w - 1, y + 1, 1, osd_h - 2,
  975. 0x70709Au);
  976. osd_sun(x + 22, y + (int)osd_h / 2);
  977. if (osd_level >= 100) {
  978. pct[n++] = '1';
  979. pct[n++] = '0';
  980. pct[n++] = '0';
  981. } else {
  982. if (osd_level >= 10)
  983. pct[n++] = (char)('0' + osd_level / 10);
  984. pct[n++] = (char)('0' + osd_level % 10);
  985. }
  986. pct[n++] = '%';
  987. pct[n] = '\0';
  988. usurf_text(&screen, x + 42, y + 9, "Brightness", 0xE8E8F6u,
  989. x, y, osd_w, osd_h);
  990. tw = (int)usurf_text_width(&screen, pct);
  991. usurf_text(&screen, x + (int)osd_w - 14 - tw, y + 9, pct,
  992. 0xFFD250u, x, y, osd_w, osd_h);
  993. bar_x = (unsigned int)(x + 42);
  994. bar_y = (unsigned int)(y + 35);
  995. bar_w = osd_w - 42 - 14;
  996. usurf_fill(&screen, (int)bar_x, (int)bar_y, bar_w, 10, 0x44445Au);
  997. usurf_fill(&screen, (int)bar_x, (int)bar_y,
  998. (bar_w * (unsigned int)osd_level + 50) / 100, 10,
  999. 0xFFD250u);
  1000. }
  1001. /* Put the power tooltip and the brightness OSD back on top after a render
  1002. * pass that may have overwritten them. */
  1003. static void wm_overlays(struct wm_present_info *p)
  1004. {
  1005. bar_tooltip_overlay(p);
  1006. if (osd_show) {
  1007. osd_draw();
  1008. add_rect(p, osd_x, osd_y, osd_w, osd_h);
  1009. }
  1010. }
  1011. /* A focused window is topmost, so the shadow contains exactly its pixels in
  1012. * the old rectangle. Move that block and repaint only the old part the new
  1013. * rectangle does not cover. When either rectangle is partly off-screen the
  1014. * shadow has no valid pixels for the hidden part, so repaint the new rect
  1015. * instead of copying desktop background into the window. */
  1016. static void move_window_pixels(struct uwin *u, int ox, int oy, int nx, int ny)
  1017. {
  1018. struct wm_present_info present;
  1019. struct orect oldr, newr, pieces[4];
  1020. unsigned int np, k;
  1021. int w = (int)u->info.w, h = (int)u->info.h;
  1022. int old_on = ox >= 0 && oy >= 0 &&
  1023. ox + w <= (int)fbw && oy + h <= (int)fbh;
  1024. int new_on = nx >= 0 && ny >= 0 &&
  1025. nx + w <= (int)fbw && ny + h <= (int)fbh;
  1026. if (u_wm_begin() != 0)
  1027. return;
  1028. if (old_on && new_on)
  1029. usurf_copy(&screen, nx, ny, ox, oy, u->info.w, u->info.h);
  1030. else
  1031. draw_rect(nx, ny, u->info.w, u->info.h);
  1032. oldr.x = ox;
  1033. oldr.y = oy;
  1034. oldr.w = u->info.w;
  1035. oldr.h = u->info.h;
  1036. newr.x = nx;
  1037. newr.y = ny;
  1038. newr.w = u->info.w;
  1039. newr.h = u->info.h;
  1040. np = rect_sub(pieces, &oldr, &newr);
  1041. for (k = 0; k < np; k++)
  1042. draw_rect(pieces[k].x, pieces[k].y, pieces[k].w, pieces[k].h);
  1043. present.count = 0;
  1044. present.immediate = 1;
  1045. add_rect(&present, nx, ny, u->info.w, u->info.h);
  1046. for (k = 0; k < np; k++)
  1047. add_rect(&present, pieces[k].x, pieces[k].y,
  1048. pieces[k].w, pieces[k].h);
  1049. wm_overlays(&present);
  1050. u_wm_present(&present);
  1051. }
  1052. static int iabs(int v)
  1053. {
  1054. return v < 0 ? -v : v;
  1055. }
  1056. /* Repaint the old and new rectangles of a geometry change. */
  1057. static void redraw_geom(struct uwin *u, int ox, int oy,
  1058. unsigned int ow, unsigned int oh)
  1059. {
  1060. struct wm_present_info present;
  1061. unsigned int i;
  1062. present.count = 0;
  1063. present.immediate = 1;
  1064. add_rect(&present, ox, oy, ow, oh);
  1065. add_rect(&present, u->info.x, u->info.y, u->info.w, u->info.h);
  1066. if (u_wm_begin() == 0) {
  1067. for (i = 0; i < present.count; i++)
  1068. draw_rect(present.rect[i].x, present.rect[i].y,
  1069. present.rect[i].w, present.rect[i].h);
  1070. wm_overlays(&present);
  1071. u_wm_present(&present);
  1072. }
  1073. }
  1074. /* Resize (when needed), move and remap a window in one step. */
  1075. static int set_window_geometry(struct uwin *u, unsigned int idx,
  1076. int nx, int ny, unsigned int nw, unsigned int nh)
  1077. {
  1078. struct wm_window_info ni;
  1079. int ox = u->info.x, oy = u->info.y;
  1080. unsigned int ow = u->info.w, oh = u->info.h;
  1081. if ((int)nw != (int)ow || (int)nh != (int)oh) {
  1082. if (u_wm_resize(idx, nw - 2 * WM_BORDER_W,
  1083. nh - WM_TITLE_H - 2 * WM_BORDER_W) != 0)
  1084. return -1;
  1085. }
  1086. if (nx != ox || ny != oy)
  1087. u_wm_set_geom(idx, nx, ny);
  1088. u->mapped = 0;
  1089. if (u_wm_info(idx, &ni) == 1)
  1090. u->info = ni;
  1091. if (u_wm_map(idx, &u->map) == 0)
  1092. u->mapped = 1;
  1093. redraw_geom(u, ox, oy, ow, oh);
  1094. return 0;
  1095. }
  1096. static int restore_window(struct uwin *u, unsigned int idx)
  1097. {
  1098. if (u->state == WIN_NORMAL)
  1099. return 0;
  1100. if (set_window_geometry(u, idx, u->restore_x, u->restore_y,
  1101. u->restore_w, u->restore_h) != 0)
  1102. return -1;
  1103. u->state = WIN_NORMAL;
  1104. return 0;
  1105. }
  1106. /* Maximize or snap a window to a half of the work area below the system
  1107. * bar. The restore geometry is recorded only when the window leaves the
  1108. * normal state, so maximize -> snap -> restore returns to the original. */
  1109. static int place_window(struct uwin *u, unsigned int idx, int state)
  1110. {
  1111. unsigned int half = fbw / 2;
  1112. unsigned int h = fbh - WM_BAR_H;
  1113. int x = 0;
  1114. unsigned int w = fbw;
  1115. if (state == u->state)
  1116. return 0;
  1117. if (state == WIN_SNAP_LEFT) {
  1118. w = half;
  1119. } else if (state == WIN_SNAP_RIGHT) {
  1120. x = (int)half;
  1121. w = fbw - half;
  1122. }
  1123. if (u->state == WIN_NORMAL) {
  1124. u->restore_x = u->info.x;
  1125. u->restore_y = u->info.y;
  1126. u->restore_w = u->info.w;
  1127. u->restore_h = u->info.h;
  1128. }
  1129. if (set_window_geometry(u, idx, x, WM_BAR_H, w, h) != 0)
  1130. return -1;
  1131. u->state = state;
  1132. return 0;
  1133. }
  1134. /* Double-click title bar: fill the work area below the system bar, or go back
  1135. * to the geometry the window had before it was maximized. A snapped window
  1136. * maximizes (keeping its pre-snap restore geometry). */
  1137. static int toggle_maximize(struct uwin *u, unsigned int idx)
  1138. {
  1139. if (u->state == WIN_MAXIMIZED)
  1140. return restore_window(u, idx);
  1141. return place_window(u, idx, WIN_MAXIMIZED);
  1142. }
  1143. /* One axis of edge resistance. lo/len is the window's span on that axis
  1144. * before the move, *pos the proposed start. dir < 0: the window's start side
  1145. * (left/top) resists edge e while moving towards lower coordinates; dir > 0:
  1146. * its end side (right/bottom) while moving higher. Within WM_RESIST pixels
  1147. * of overshoot the window stays on the edge; *best keeps the nearest edge. */
  1148. static void resist_edge(int lo, int len, int *pos, int e, int dir, int *best)
  1149. {
  1150. int over;
  1151. if (dir < 0 && *pos < lo && lo >= e && *pos < e) {
  1152. over = e - *pos;
  1153. if (over <= WM_RESIST && over < *best) {
  1154. *best = over;
  1155. *pos = e;
  1156. }
  1157. } else if (dir > 0 && *pos > lo && lo + len <= e && *pos + len > e) {
  1158. over = *pos + len - e;
  1159. if (over <= WM_RESIST && over < *best) {
  1160. *best = over;
  1161. *pos = e - len;
  1162. }
  1163. }
  1164. }
  1165. /* Span overlap test: edges of another window only resist when the two
  1166. * windows share some extent on the other axis (they would actually touch). */
  1167. static int spans_overlap(int a, int alen, int b, int blen)
  1168. {
  1169. return a < b + blen && b < a + alen;
  1170. }
  1171. /* Apply edge resistance to a drag from (u->info.x, u->info.y) to (*nx, *ny):
  1172. * screen edges and the facing edges of every other window. The proposed
  1173. * position is still pure pointer arithmetic, so once the overshoot passes
  1174. * WM_RESIST the window jumps to follow the pointer again. */
  1175. static void resist_geom(struct uwin *u, int idx, int *nx, int *ny)
  1176. {
  1177. int ox = u->info.x, oy = u->info.y;
  1178. int w = (int)u->info.w, h = (int)u->info.h;
  1179. int px = *nx, py = *ny;
  1180. int bestx = WM_RESIST + 1, besty = WM_RESIST + 1;
  1181. int i;
  1182. resist_edge(ox, w, &px, 0, -1, &bestx);
  1183. resist_edge(ox, w, &px, (int)fbw, 1, &bestx);
  1184. resist_edge(oy, h, &py, (int)fbh, 1, &besty);
  1185. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1186. struct wm_window_info *o = &wins[i].info;
  1187. int t;
  1188. if (i == idx || !o->used)
  1189. continue;
  1190. if (spans_overlap(oy, h, o->y, (int)o->h)) {
  1191. /* our left side against its right, our right
  1192. * side against its left */
  1193. t = *nx;
  1194. resist_edge(ox, w, &t, o->x + (int)o->w, -1, &bestx);
  1195. resist_edge(ox, w, &t, o->x, 1, &bestx);
  1196. if (t != *nx)
  1197. px = t;
  1198. }
  1199. if (spans_overlap(ox, w, o->x, (int)o->w)) {
  1200. t = *ny;
  1201. resist_edge(oy, h, &t, o->y + (int)o->h, -1, &besty);
  1202. resist_edge(oy, h, &t, o->y, 1, &besty);
  1203. if (t != *ny)
  1204. py = t;
  1205. }
  1206. }
  1207. *nx = px;
  1208. *ny = py;
  1209. }
  1210. /* The top-bar date/time text is also a button: it opens the Date & Time
  1211. * editor. The rectangle is recorded by draw_bar(). */
  1212. static int bar_clock_hit(unsigned int mx, unsigned int my)
  1213. {
  1214. return bar_clock_w && bar_clock_x >= 0 && my < WM_BAR_H &&
  1215. (int)mx >= bar_clock_x - 5 &&
  1216. (int)mx < bar_clock_x + (int)bar_clock_w + 5;
  1217. }
  1218. static int bar_net_hit(unsigned int mx, unsigned int my)
  1219. {
  1220. return bar_net_w && bar_net_x >= 0 && my < WM_BAR_H &&
  1221. (int)mx >= bar_net_x - 3 &&
  1222. (int)mx < bar_net_x + (int)bar_net_w + 3;
  1223. }
  1224. /* The AC/battery icon opens the control panel on a click; hovering it also
  1225. * reveals the percentage tooltip below it. The tooltip area keeps the hover
  1226. * alive but is not a click target. */
  1227. static int bar_pwr_hit(unsigned int mx, unsigned int my)
  1228. {
  1229. if (my >= WM_BAR_H)
  1230. return bar_pwr_tip_on &&
  1231. (int)mx >= bar_pwr_tip_on_x &&
  1232. (int)mx < bar_pwr_tip_on_x + (int)bar_pwr_tip_on_w &&
  1233. (int)my >= bar_pwr_tip_on_y &&
  1234. (int)my < bar_pwr_tip_on_y + (int)bar_pwr_tip_on_h;
  1235. return bar_pwr_x >= 0 &&
  1236. (int)mx >= bar_pwr_x - 3 &&
  1237. (int)mx < bar_pwr_x + BAR_PWR_W + 3;
  1238. }
  1239. /* The whole right status area is one button: the clock, temperature, network
  1240. * and power items plus the gaps between them. The launcher buttons end at
  1241. * WM_BTN_END_X, so this never overlaps them. */
  1242. static int bar_status_hit(unsigned int mx, unsigned int my)
  1243. {
  1244. return my < WM_BAR_H && (int)mx >= WM_BTN_END_X + 4;
  1245. }
  1246. static void do_update(struct wm_status *st)
  1247. {
  1248. struct wm_present_info present;
  1249. int need = 0;
  1250. int bar_dirty = 0;
  1251. unsigned int i;
  1252. char dt[20];
  1253. static char last_dt[20];
  1254. int kexec_changed = (st->kexec_busy != bar_kexec_busy) ||
  1255. (st->kexec_busy && st->kexec_progress != bar_kexec_progress);
  1256. unsigned int msg_now = u_ticks_ms();
  1257. int osd_hide = 0, osd_repaint = 0;
  1258. /* The kernel bumps backlight_seq on every brightness key/command.
  1259. * Pop the OSD on a change and keep it up for OSD_MS after the last
  1260. * one. The first poll only records the current sequence, so a WM
  1261. * restart does not flash a stale bubble. */
  1262. if (st->backlight_present) {
  1263. if (!osd_seen_valid) {
  1264. osd_seen_valid = 1;
  1265. osd_seen_seq = st->backlight_seq;
  1266. } else if (st->backlight_seq != osd_seen_seq) {
  1267. osd_seen_seq = st->backlight_seq;
  1268. osd_level = (int)st->backlight_level;
  1269. osd_until = msg_now + OSD_MS;
  1270. osd_show = 1;
  1271. }
  1272. } else if (osd_show) {
  1273. osd_show = 0;
  1274. osd_hide = 1;
  1275. }
  1276. if (osd_show && (int)(msg_now - osd_until) >= 0) {
  1277. osd_show = 0;
  1278. osd_hide = 1;
  1279. }
  1280. if (osd_hide && osd_visible)
  1281. osd_repaint = 1;
  1282. /* The boot-scan window is created before the terminal so the terminal
  1283. * stays the newest (last) window and keeps the keyboard focus. Wait
  1284. * for the scan window to exist before starting the terminal; a failed
  1285. * or wedged helper is bounded so the shell is never held back. */
  1286. if (st->boot_ready && !boot_term_spawned) {
  1287. if (!boot_scan_spawned) {
  1288. boot_scan_spawned = 1;
  1289. if (free_window_slot()) {
  1290. boot_scan_pid = u_spawn("/ramfs/bin/bootscan",
  1291. "", 0, "/");
  1292. if (boot_scan_pid > 0)
  1293. boot_scan_deadline = msg_now + 2000u;
  1294. }
  1295. if (boot_scan_pid <= 0)
  1296. boot_scan_placed = 1;
  1297. }
  1298. if (boot_scan_pid > 0 && !boot_scan_placed) {
  1299. struct wm_window_info wi;
  1300. int idx = window_index_pid(boot_scan_pid, &wi);
  1301. if (idx >= 0) {
  1302. int nx = (int)fbw - (int)wi.w - 16;
  1303. int ny = WM_BAR_H + 8;
  1304. if (nx < 8)
  1305. nx = 8;
  1306. u_wm_set_geom((unsigned int)idx, nx, ny);
  1307. boot_scan_placed = 1;
  1308. } else if (!u_alive((unsigned int)boot_scan_pid) ||
  1309. (int)(msg_now - boot_scan_deadline) >= 0) {
  1310. if (u_alive((unsigned int)boot_scan_pid))
  1311. u_kill((unsigned int)boot_scan_pid);
  1312. boot_scan_placed = 1;
  1313. }
  1314. }
  1315. if (boot_scan_placed) {
  1316. boot_term_spawned = 1;
  1317. if (!free_window_slot()) {
  1318. bar_notice("window limit");
  1319. full_redraw = 1;
  1320. } else if (u_wm_create_term() < 0) {
  1321. bar_notice("cannot start term");
  1322. full_redraw = 1;
  1323. } else {
  1324. full_redraw = 1;
  1325. }
  1326. }
  1327. }
  1328. if (bar_msg_dirty ||
  1329. (bar_msg_until && (int)(msg_now - bar_msg_until) >= 0)) {
  1330. bar_dirty = 1;
  1331. bar_msg_dirty = 0;
  1332. if (bar_msg_until && (int)(msg_now - bar_msg_until) >= 0)
  1333. bar_msg_until = 0;
  1334. }
  1335. bar_kexec_busy = st->kexec_busy;
  1336. bar_kexec_progress = st->kexec_progress;
  1337. bar_kexec_total = st->kexec_total;
  1338. if (kexec_changed)
  1339. bar_dirty = 1;
  1340. {
  1341. int hot = bar_clock_hit(st->mouse.x, st->mouse.y);
  1342. if (hot != bar_clock_hot) {
  1343. bar_clock_hot = hot;
  1344. bar_dirty = 1;
  1345. }
  1346. }
  1347. {
  1348. int hot = bar_net_hit(st->mouse.x, st->mouse.y);
  1349. if (hot != bar_net_hot) {
  1350. bar_net_hot = hot;
  1351. bar_dirty = 1;
  1352. }
  1353. }
  1354. {
  1355. int hot = bar_pwr_hit(st->mouse.x, st->mouse.y);
  1356. if (hot != bar_pwr_hot) {
  1357. bar_pwr_hot = hot;
  1358. bar_dirty = 1;
  1359. }
  1360. }
  1361. /* Refresh cached window state and mappings before opening a frame:
  1362. * MAP may unmap/remap content. */
  1363. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1364. struct wm_window_info info;
  1365. int r = u_wm_info(i, &info);
  1366. if (r < 0) {
  1367. u_exit(1);
  1368. }
  1369. if (r == 0 || !info.used) {
  1370. if (wins[i].info.used)
  1371. full_redraw = 1;
  1372. wins[i].info.used = 0;
  1373. wins[i].mapped = 0;
  1374. continue;
  1375. }
  1376. {
  1377. int pid_changed = wins[i].info.used &&
  1378. info.pid != wins[i].info.pid;
  1379. if (!wins[i].info.used ||
  1380. info.content_gen != wins[i].info.content_gen ||
  1381. pid_changed)
  1382. full_redraw = 1;
  1383. wins[i].info = info;
  1384. if (!wins[i].mapped || pid_changed ||
  1385. wins[i].map.generation != info.content_gen) {
  1386. struct wm_map_info map;
  1387. if (u_wm_map(i, &map) == 0) {
  1388. wins[i].map = map;
  1389. wins[i].mapped = 1;
  1390. } else {
  1391. wins[i].mapped = 0;
  1392. }
  1393. }
  1394. }
  1395. }
  1396. /* Popovers are WM-anchored overlays, flush against the bottom of the
  1397. * bar and the right screen edge. The WM owns the geometry, so the app
  1398. * does not need a placement ABI. */
  1399. {
  1400. int control_found = 0;
  1401. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1402. struct wm_window_info ni;
  1403. int nx, ny;
  1404. if (!wins[i].info.used || !wins[i].info.popover)
  1405. continue;
  1406. if (control_pid > 0 &&
  1407. wins[i].info.pid == (unsigned int)control_pid)
  1408. control_found = 1;
  1409. nx = (int)fbw - (int)wins[i].info.w;
  1410. ny = WM_BAR_H;
  1411. if (nx != wins[i].info.x || ny != wins[i].info.y) {
  1412. if (u_wm_set_geom(i, nx, ny) == 0 &&
  1413. u_wm_info(i, &ni) == 1) {
  1414. wins[i].info = ni;
  1415. full_redraw = 1;
  1416. }
  1417. }
  1418. }
  1419. if (!control_found && control_pid > 0 &&
  1420. !u_alive((unsigned int)control_pid))
  1421. control_pid = 0;
  1422. }
  1423. /* The RTC is slow (CMOS polling) and the temperature/battery state only
  1424. * changes once a second; query them at 1 Hz instead of every 10 ms. */
  1425. {
  1426. static unsigned int last_rtc_ms;
  1427. unsigned int now_ms = u_ticks_ms();
  1428. if (full_redraw || now_ms - last_rtc_ms >= 1000) {
  1429. last_rtc_ms = now_ms;
  1430. if (u_rtc_datetime(dt, sizeof(dt)) == 0) {
  1431. for (i = 0; i < sizeof(dt); i++)
  1432. if (dt[i] != last_dt[i]) {
  1433. for (i = 0; i < sizeof(dt); i++)
  1434. last_dt[i] = dt[i];
  1435. bar_dirty = 1;
  1436. break;
  1437. }
  1438. }
  1439. {
  1440. struct sys_cputemp ct;
  1441. if (u_cpu_temp(&ct) == 0 &&
  1442. ct.temp_c != bar_temp_c) {
  1443. bar_temp_c = ct.temp_c;
  1444. bar_dirty = 1;
  1445. }
  1446. }
  1447. {
  1448. struct sys_powerinfo pi;
  1449. int pct = -1, charging = 0, ac = -1;
  1450. unsigned int bi;
  1451. if (u_power_info(&pi, 0) == 0) {
  1452. /* A laptop EC (ThinkPad or Chrome
  1453. * EC) marks a machine with AC/battery;
  1454. * desktops show neither. */
  1455. if (pi.present) {
  1456. ac = pi.ac_online;
  1457. for (bi = 0; bi < SYS_POWER_BATT_MAX;
  1458. bi++) {
  1459. struct sys_battery *b =
  1460. &pi.battery[bi];
  1461. if (!b->present ||
  1462. !b->percent_valid)
  1463. continue;
  1464. pct = (int)b->percent;
  1465. charging = (int)b->charging;
  1466. break;
  1467. }
  1468. }
  1469. if (pct != bar_batt_pct ||
  1470. charging != bar_batt_charging ||
  1471. ac != bar_ac_online) {
  1472. bar_batt_pct = pct;
  1473. bar_batt_charging = charging;
  1474. bar_ac_online = ac;
  1475. bar_dirty = 1;
  1476. }
  1477. }
  1478. }
  1479. if (poll_net_status())
  1480. bar_dirty = 1;
  1481. }
  1482. }
  1483. if (full_redraw) {
  1484. if (u_wm_begin() != 0) {
  1485. full_redraw = 1;
  1486. return;
  1487. }
  1488. full_redraw = 0;
  1489. need = 1;
  1490. {
  1491. draw_rect(0, 0, fbw, fbh);
  1492. draw_bar();
  1493. bar_tooltip_commit();
  1494. if (osd_show)
  1495. osd_draw();
  1496. osd_visible = osd_show;
  1497. present.count = 0;
  1498. add_rect(&present, 0, 0, fbw, fbh);
  1499. present.immediate = 0;
  1500. u_wm_present(&present);
  1501. /* Ack every window that was clean at the time of the
  1502. * redraw. If new damage arrived during it, its seq
  1503. * differs and the kernel leaves it dirty. */
  1504. for (i = 0; i < WM_MAX_WINDOWS; i++)
  1505. if (wins[i].info.used && wins[i].mapped)
  1506. u_wm_ack(i, wins[i].info.dirty_seq);
  1507. }
  1508. return;
  1509. }
  1510. /* Partial cell/canvas updates and the bar clock. */
  1511. present.count = 0;
  1512. present.immediate = 0;
  1513. if (osd_repaint) {
  1514. add_rect(&present, osd_x, osd_y, osd_w, osd_h);
  1515. need = 1;
  1516. }
  1517. if (osd_show)
  1518. need = 1;
  1519. if (bar_dirty) {
  1520. need = 1;
  1521. add_rect(&present, 0, 0, fbw, WM_BAR_H);
  1522. }
  1523. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1524. struct uwin *u = &wins[i];
  1525. struct wm_window_info *w = &u->info;
  1526. int rx, ry;
  1527. unsigned int rw, rh;
  1528. if (!w->used || !w->dirty)
  1529. continue;
  1530. if (!w->dmg_w || !w->dmg_h)
  1531. continue;
  1532. rx = w->x + (w->popover ? 0 : WM_BORDER_W) + (int)w->dmg_x;
  1533. ry = w->y + (w->popover ? 0 : WM_BORDER_W + WM_TITLE_H) +
  1534. (int)w->dmg_y;
  1535. rw = w->dmg_w;
  1536. rh = w->dmg_h;
  1537. add_rect(&present, rx, ry, rw, rh);
  1538. u->ack_seq = u->mapped ? w->dirty_seq : 0;
  1539. need = 1;
  1540. }
  1541. /* Repaint the previous tooltip area so hiding or moving it restores
  1542. * the window/desktop underneath; the new one is drawn after the
  1543. * windows below. */
  1544. if (bar_pwr_tip_on) {
  1545. need = 1;
  1546. add_rect(&present, bar_pwr_tip_on_x, bar_pwr_tip_on_y,
  1547. bar_pwr_tip_on_w, bar_pwr_tip_on_h);
  1548. }
  1549. if (!need) {
  1550. /* Drag movement is handled below; a stationary mouse can be
  1551. * left to the kernel's cursor overlay. */
  1552. return;
  1553. }
  1554. if (u_wm_begin() != 0)
  1555. return;
  1556. for (i = 0; i < present.count; i++)
  1557. draw_rect(present.rect[i].x, present.rect[i].y,
  1558. present.rect[i].w, present.rect[i].h);
  1559. if (bar_dirty)
  1560. draw_bar();
  1561. if (bar_pwr_tip_want) {
  1562. bar_tooltip_commit();
  1563. add_rect(&present, bar_pwr_tip_on_x, bar_pwr_tip_on_y,
  1564. bar_pwr_tip_on_w, bar_pwr_tip_on_h);
  1565. } else {
  1566. bar_pwr_tip_on = 0;
  1567. }
  1568. if (osd_show) {
  1569. osd_draw();
  1570. add_rect(&present, osd_x, osd_y, osd_w, osd_h);
  1571. }
  1572. osd_visible = osd_show;
  1573. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1574. struct uwin *u = &wins[i];
  1575. if (!u->info.used || !u->info.dirty || !u->mapped ||
  1576. !u->ack_seq)
  1577. continue;
  1578. if (u->ack_seq == u->info.dirty_seq)
  1579. u_wm_ack(i, u->info.dirty_seq);
  1580. }
  1581. u_wm_present(&present);
  1582. }
  1583. static int spawn_app(const char *path)
  1584. {
  1585. int pid = u_spawn(path, "", 0, "/");
  1586. if (pid > 0)
  1587. full_redraw = 1;
  1588. return pid;
  1589. }
  1590. static void spawn_app_checked(const char *path)
  1591. {
  1592. if (!free_window_slot()) {
  1593. bar_notice("window limit");
  1594. full_redraw = 1;
  1595. return;
  1596. }
  1597. if (spawn_app(path) < 0) {
  1598. bar_notice("spawn failed");
  1599. full_redraw = 1;
  1600. }
  1601. }
  1602. /* Any status target opens one control panel; a second click closes it. */
  1603. static void open_control_panel(void)
  1604. {
  1605. unsigned int i;
  1606. if (control_pid > 0 && u_alive((unsigned int)control_pid)) {
  1607. for (i = 0; i < WM_MAX_WINDOWS; i++)
  1608. if (wins[i].info.used &&
  1609. wins[i].info.pid == (unsigned int)control_pid) {
  1610. if (u_wm_close(i) == 0) {
  1611. control_pid = 0;
  1612. full_redraw = 1;
  1613. } else {
  1614. u_wm_focus(i);
  1615. full_redraw = 1;
  1616. }
  1617. return;
  1618. }
  1619. /* The process is alive but has not created its window yet. */
  1620. return;
  1621. }
  1622. if (!free_window_slot()) {
  1623. bar_notice("window limit");
  1624. full_redraw = 1;
  1625. return;
  1626. }
  1627. control_pid = u_spawn("/ramfs/bin/netmenu", "", 0, "/");
  1628. if (control_pid < 0) {
  1629. control_pid = 0;
  1630. bar_notice("spawn failed");
  1631. full_redraw = 1;
  1632. } else {
  1633. full_redraw = 1;
  1634. }
  1635. }
  1636. /* Index of a popover containing the point, or -1. */
  1637. static int popover_under(unsigned int mx, unsigned int my)
  1638. {
  1639. unsigned int i;
  1640. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1641. struct wm_window_info *w = &wins[i].info;
  1642. if (w->used && w->popover &&
  1643. mx >= (unsigned int)w->x && my >= (unsigned int)w->y &&
  1644. mx < (unsigned int)(w->x + (int)w->w) &&
  1645. my < (unsigned int)(w->y + (int)w->h))
  1646. return (int)i;
  1647. }
  1648. return -1;
  1649. }
  1650. /* A press anywhere outside an open popover dismisses it; the click then
  1651. * continues normally so the target under the pointer still reacts. */
  1652. static void dismiss_popover_outside(unsigned int mx, unsigned int my)
  1653. {
  1654. unsigned int i;
  1655. if (popover_under(mx, my) >= 0)
  1656. return;
  1657. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1658. if (!wins[i].info.used || !wins[i].info.popover)
  1659. continue;
  1660. if (wins[i].info.pid == (unsigned int)control_pid)
  1661. control_pid = 0;
  1662. if (u_wm_close(i) == 0) {
  1663. wins[i].info.used = 0;
  1664. wins[i].mapped = 0;
  1665. full_redraw = 1;
  1666. }
  1667. break;
  1668. }
  1669. }
  1670. /* Top-bar launcher under (mx, my)? Same rectangles as the click chain in
  1671. * handle_mouse(); every launcher shares WM_BTN_Y/WM_BTN_H. */
  1672. static int bar_launcher_hit(unsigned int mx, unsigned int my)
  1673. {
  1674. static const int x0[] = {
  1675. WM_BTN_X, WM_BTN_CLOCK_X, WM_BTN_COLORS_X, WM_BTN_FILES_X,
  1676. WM_BTN_MINES_X, WM_BTN_WEB_X, WM_BTN_CARDS_X, WM_BTN_GALLERY_X,
  1677. WM_BTN_CHAT_X, WM_BTN_DISKS_X, WM_BTN_PROCESS_X
  1678. };
  1679. static const int w[] = {
  1680. WM_BTN_W, WM_BTN_CLOCK_W, WM_BTN_COLORS_W, WM_BTN_FILES_W,
  1681. WM_BTN_MINES_W, WM_BTN_WEB_W, WM_BTN_CARDS_W, WM_BTN_GALLERY_W,
  1682. WM_BTN_CHAT_W, WM_BTN_DISKS_W, WM_BTN_PROCESS_W
  1683. };
  1684. unsigned int i;
  1685. if ((int)my < WM_BTN_Y || (int)my >= WM_BTN_Y + WM_BTN_H)
  1686. return 0;
  1687. for (i = 0; i < sizeof(x0) / sizeof(x0[0]); i++)
  1688. if ((int)mx >= x0[i] && (int)mx < x0[i] + w[i])
  1689. return 1;
  1690. return 0;
  1691. }
  1692. /* Apply one edge-drag step. The grabbed edges follow the pointer while the
  1693. * opposite edges stay put; the work area clamps the active edges. */
  1694. static void resize_window_drag(struct uwin *u, unsigned int idx,
  1695. unsigned int mx, unsigned int my)
  1696. {
  1697. int dx = (int)mx - (int)resize_mx;
  1698. int dy = (int)my - (int)resize_my;
  1699. int left = resize_ox;
  1700. int right = resize_ox + (int)resize_ow;
  1701. int top = resize_oy;
  1702. int bottom = resize_oy + (int)resize_oh;
  1703. int nx, ny, nw, nh;
  1704. if (resize_edges & WM_EDGE_LEFT) {
  1705. left = resize_ox + dx;
  1706. if (left < 0)
  1707. left = 0;
  1708. } else if (resize_edges & WM_EDGE_RIGHT) {
  1709. right = resize_ox + (int)resize_ow + dx;
  1710. if (right > (int)fbw)
  1711. right = (int)fbw;
  1712. }
  1713. if (resize_edges & WM_EDGE_TOP) {
  1714. top = resize_oy + dy;
  1715. if (top < WM_BAR_H)
  1716. top = WM_BAR_H;
  1717. } else if (resize_edges & WM_EDGE_BOTTOM) {
  1718. bottom = resize_oy + (int)resize_oh + dy;
  1719. if (bottom > (int)fbh)
  1720. bottom = (int)fbh;
  1721. }
  1722. if ((resize_edges & (WM_EDGE_LEFT | WM_EDGE_RIGHT)) &&
  1723. right - left < 200) {
  1724. if (resize_edges & WM_EDGE_LEFT)
  1725. left = right - 200;
  1726. else
  1727. right = left + 200;
  1728. }
  1729. if ((resize_edges & (WM_EDGE_TOP | WM_EDGE_BOTTOM)) &&
  1730. bottom - top < 140) {
  1731. if (resize_edges & WM_EDGE_TOP)
  1732. top = bottom - 140;
  1733. else
  1734. bottom = top + 140;
  1735. }
  1736. if (left < 0)
  1737. left = 0;
  1738. if (top < WM_BAR_H)
  1739. top = WM_BAR_H;
  1740. nx = left;
  1741. ny = top;
  1742. nw = right - left;
  1743. nh = bottom - top;
  1744. if (nw > (int)fbw)
  1745. nw = (int)fbw;
  1746. if (nh > (int)fbh - WM_BAR_H)
  1747. nh = (int)fbh - WM_BAR_H;
  1748. if (nx == u->info.x && ny == u->info.y &&
  1749. nw == (int)u->info.w && nh == (int)u->info.h)
  1750. return;
  1751. if (set_window_geometry(u, idx, nx, ny, (unsigned int)nw,
  1752. (unsigned int)nh) == 0)
  1753. u->state = WIN_NORMAL;
  1754. }
  1755. /* Capture the pointer for an edge resize that started at (mx,my). */
  1756. static void begin_resize(unsigned int idx, int edges, unsigned int mx,
  1757. unsigned int my)
  1758. {
  1759. struct uwin *u = &wins[idx];
  1760. resize_id = (int)idx;
  1761. resize_edges = edges;
  1762. resize_mx = mx;
  1763. resize_my = my;
  1764. resize_ox = u->info.x;
  1765. resize_oy = u->info.y;
  1766. resize_ow = u->info.w;
  1767. resize_oh = u->info.h;
  1768. u_wm_mouse_capture(1);
  1769. }
  1770. /* The WM's cursor override (SYS_WM_SET_CURSOR): resize arrows on the
  1771. * invisible resize border and while resizing, the move cross while a
  1772. * title-bar drag is under way, a hand over the launchers and the status
  1773. * area. Everywhere else the kernel picks the window's own content cursor
  1774. * (or the arrow). */
  1775. static void update_cursor(unsigned int mx, unsigned int my)
  1776. {
  1777. static int current = CURSOR_NO_OVERRIDE;
  1778. int want = CURSOR_NO_OVERRIDE;
  1779. if (resize_id >= 0) {
  1780. want = resize_cursor(resize_edges);
  1781. } else if (drag_id >= 0) {
  1782. if (drag_moved)
  1783. want = CURSOR_MOVE;
  1784. } else if (my < WM_BAR_H) {
  1785. if (bar_launcher_hit(mx, my) || bar_status_hit(mx, my))
  1786. want = CURSOR_POINTER;
  1787. } else {
  1788. int edges = 0;
  1789. int idx = resize_hit((int)mx, (int)my, &edges);
  1790. if (idx >= 0 && edges &&
  1791. !close_hit(&wins[idx].info, (int)mx, (int)my))
  1792. want = resize_cursor(edges);
  1793. }
  1794. if (want != current && u_wm_set_cursor(want) == 0)
  1795. current = want;
  1796. }
  1797. static void handle_mouse(struct wm_status *st)
  1798. {
  1799. unsigned int buttons = st->mouse.buttons;
  1800. unsigned int mx = st->mouse.x, my = st->mouse.y;
  1801. int down = (buttons & 1) != 0;
  1802. int was = (prev_buttons & 1) != 0;
  1803. if (down && !was) {
  1804. int prev_title_id = last_title_id;
  1805. unsigned int now = u_ticks_ms();
  1806. if (!bar_status_hit(mx, my))
  1807. dismiss_popover_outside(mx, my);
  1808. last_title_id = -1;
  1809. if (bar_status_hit(mx, my)) {
  1810. open_control_panel();
  1811. } else if (mx >= WM_BTN_X && my >= WM_BTN_Y &&
  1812. mx < WM_BTN_X + WM_BTN_W && my < WM_BTN_Y + WM_BTN_H) {
  1813. if (!free_window_slot()) {
  1814. bar_notice("window limit");
  1815. full_redraw = 1;
  1816. } else if (u_wm_create_term() < 0) {
  1817. bar_notice("cannot start term");
  1818. full_redraw = 1;
  1819. } else {
  1820. full_redraw = 1;
  1821. }
  1822. } else if (mx >= WM_BTN_CLOCK_X && my >= WM_BTN_CLOCK_Y &&
  1823. mx < WM_BTN_CLOCK_X + WM_BTN_CLOCK_W &&
  1824. my < WM_BTN_CLOCK_Y + WM_BTN_CLOCK_H) {
  1825. spawn_app_checked("/ramfs/bin/clock");
  1826. } else if (mx >= WM_BTN_COLORS_X && my >= WM_BTN_COLORS_Y &&
  1827. mx < WM_BTN_COLORS_X + WM_BTN_COLORS_W &&
  1828. my < WM_BTN_COLORS_Y + WM_BTN_COLORS_H) {
  1829. spawn_app_checked("/ramfs/bin/colors");
  1830. } else if (mx >= WM_BTN_FILES_X && my >= WM_BTN_FILES_Y &&
  1831. mx < WM_BTN_FILES_X + WM_BTN_FILES_W &&
  1832. my < WM_BTN_FILES_Y + WM_BTN_FILES_H) {
  1833. spawn_app_checked("/ramfs/bin/files");
  1834. } else if (mx >= WM_BTN_MINES_X && my >= WM_BTN_MINES_Y &&
  1835. mx < WM_BTN_MINES_X + WM_BTN_MINES_W &&
  1836. my < WM_BTN_MINES_Y + WM_BTN_MINES_H) {
  1837. spawn_app_checked("/ramfs/bin/mines");
  1838. } else if (mx >= WM_BTN_WEB_X && my >= WM_BTN_WEB_Y &&
  1839. mx < WM_BTN_WEB_X + WM_BTN_WEB_W &&
  1840. my < WM_BTN_WEB_Y + WM_BTN_WEB_H) {
  1841. spawn_app_checked("/ramfs/bin/browser");
  1842. } else if (mx >= WM_BTN_CARDS_X && my >= WM_BTN_CARDS_Y &&
  1843. mx < WM_BTN_CARDS_X + WM_BTN_CARDS_W &&
  1844. my < WM_BTN_CARDS_Y + WM_BTN_CARDS_H) {
  1845. spawn_app_checked("/ramfs/bin/solitaire");
  1846. } else if (mx >= WM_BTN_GALLERY_X && my >= WM_BTN_GALLERY_Y &&
  1847. mx < WM_BTN_GALLERY_X + WM_BTN_GALLERY_W &&
  1848. my < WM_BTN_GALLERY_Y + WM_BTN_GALLERY_H) {
  1849. spawn_app_checked("/ramfs/bin/gallery");
  1850. } else if (mx >= WM_BTN_CHAT_X && my >= WM_BTN_CHAT_Y &&
  1851. mx < WM_BTN_CHAT_X + WM_BTN_CHAT_W &&
  1852. my < WM_BTN_CHAT_Y + WM_BTN_CHAT_H) {
  1853. spawn_app_checked("/ramfs/bin/chat");
  1854. } else if (mx >= WM_BTN_DISKS_X && my >= WM_BTN_DISKS_Y &&
  1855. mx < WM_BTN_DISKS_X + WM_BTN_DISKS_W &&
  1856. my < WM_BTN_DISKS_Y + WM_BTN_DISKS_H) {
  1857. spawn_app_checked("/ramfs/bin/disks");
  1858. } else if (mx >= WM_BTN_PROCESS_X && my >= WM_BTN_PROCESS_Y &&
  1859. mx < WM_BTN_PROCESS_X + WM_BTN_PROCESS_W &&
  1860. my < WM_BTN_PROCESS_Y + WM_BTN_PROCESS_H) {
  1861. spawn_app_checked("/ramfs/bin/process");
  1862. } else {
  1863. int edges = 0;
  1864. int idx = resize_hit((int)mx, (int)my, &edges);
  1865. if (idx >= 0) {
  1866. struct uwin *u = &wins[idx];
  1867. int pmx = (int)mx, pmy = (int)my;
  1868. u_wm_focus((unsigned int)idx);
  1869. if (u->info.popover) {
  1870. /* A popover has no title bar, close
  1871. * button or resize border; the app owns
  1872. * its content. */
  1873. } else if (close_hit(&u->info, pmx, pmy)) {
  1874. u_wm_close((unsigned int)idx);
  1875. full_redraw = 1;
  1876. } else if (edges) {
  1877. begin_resize((unsigned int)idx, edges,
  1878. mx, my);
  1879. } else if (pmy < u->info.y + WM_TITLE_H) {
  1880. if (prev_title_id == idx &&
  1881. now - last_title_ms <= WM_DBLCLICK_MS &&
  1882. iabs(pmx - last_title_x) <= WM_DBLCLICK_SLOP &&
  1883. iabs(pmy - last_title_y) <= WM_DBLCLICK_SLOP) {
  1884. last_title_ms = 0;
  1885. if (toggle_maximize(u,
  1886. (unsigned int)idx) != 0)
  1887. bar_notice("resize failed");
  1888. } else {
  1889. last_title_id = idx;
  1890. last_title_ms = now;
  1891. last_title_x = pmx;
  1892. last_title_y = pmy;
  1893. drag_id = idx;
  1894. drag_mx = pmx;
  1895. drag_my = pmy;
  1896. drag_dx = pmx - u->info.x;
  1897. drag_dy = pmy - u->info.y;
  1898. drag_moved = 0;
  1899. u_wm_mouse_capture(1);
  1900. }
  1901. }
  1902. }
  1903. }
  1904. } else if (down && was && resize_id >= 0) {
  1905. struct uwin *u = &wins[resize_id];
  1906. if (u->info.used)
  1907. resize_window_drag(u, (unsigned int)resize_id, mx, my);
  1908. else {
  1909. resize_id = -1;
  1910. u_wm_mouse_capture(0);
  1911. }
  1912. } else if (down && was && drag_id >= 0) {
  1913. struct uwin *u = &wins[drag_id];
  1914. if (u->info.used) {
  1915. int ox, oy, nx, ny;
  1916. if (iabs((int)mx - drag_mx) >= WM_DRAG_START ||
  1917. iabs((int)my - drag_my) >= WM_DRAG_START)
  1918. drag_moved = 1;
  1919. /* A maximized or snapped window is restored as soon as
  1920. * the drag starts. Keep the grab point at the same
  1921. * fraction of the title bar, so the restored (narrower)
  1922. * window stays under the pointer. A click that does
  1923. * not move must leave it in place. */
  1924. if (u->state != WIN_NORMAL && drag_moved) {
  1925. unsigned int ow = u->info.w;
  1926. int gx = drag_mx - u->info.x;
  1927. int gy = drag_my - u->info.y;
  1928. if (restore_window(u,
  1929. (unsigned int)drag_id) == 0) {
  1930. if (ow)
  1931. gx = gx * (int)u->info.w /
  1932. (int)ow;
  1933. drag_dx = gx;
  1934. drag_dy = gy;
  1935. }
  1936. }
  1937. if (u->state == WIN_NORMAL) {
  1938. ox = u->info.x;
  1939. oy = u->info.y;
  1940. nx = (int)mx - drag_dx;
  1941. ny = (int)my - drag_dy;
  1942. resist_geom(u, drag_id, &nx, &ny);
  1943. clamp_geom(u, &nx, &ny);
  1944. if (nx != ox || ny != oy) {
  1945. struct wm_present_info present;
  1946. unsigned int i;
  1947. u_wm_set_geom((unsigned int)drag_id,
  1948. nx, ny);
  1949. u->info.x = nx;
  1950. u->info.y = ny;
  1951. if (u->info.focused) {
  1952. move_window_pixels(u, ox, oy,
  1953. nx, ny);
  1954. } else {
  1955. present.count = 0;
  1956. present.immediate = 1;
  1957. add_rect(&present, ox, oy,
  1958. u->info.w, u->info.h);
  1959. add_rect(&present, nx, ny,
  1960. u->info.w, u->info.h);
  1961. if (u_wm_begin() == 0) {
  1962. for (i = 0;
  1963. i < present.count; i++)
  1964. draw_rect(
  1965. present.rect[i].x,
  1966. present.rect[i].y,
  1967. present.rect[i].w,
  1968. present.rect[i].h);
  1969. wm_overlays(&present);
  1970. u_wm_present(&present);
  1971. }
  1972. }
  1973. }
  1974. }
  1975. } else {
  1976. drag_id = -1;
  1977. u_wm_mouse_capture(0);
  1978. }
  1979. } else if (!down && was) {
  1980. /* Dropping a window with the pointer on the left/right screen
  1981. * edge snaps it to that half; on the top edge it maximizes. */
  1982. if (drag_id >= 0 && drag_moved && wins[drag_id].info.used &&
  1983. wins[drag_id].state == WIN_NORMAL) {
  1984. struct uwin *u = &wins[drag_id];
  1985. int st = WIN_NORMAL;
  1986. if ((int)mx < WM_SNAP_EDGE)
  1987. st = WIN_SNAP_LEFT;
  1988. else if ((int)mx >= (int)fbw - WM_SNAP_EDGE)
  1989. st = WIN_SNAP_RIGHT;
  1990. else if ((int)my < WM_SNAP_EDGE)
  1991. st = WIN_MAXIMIZED;
  1992. if (st != WIN_NORMAL &&
  1993. place_window(u, (unsigned int)drag_id, st) != 0)
  1994. bar_notice("resize failed");
  1995. }
  1996. drag_id = -1;
  1997. resize_id = -1;
  1998. u_wm_mouse_capture(0);
  1999. }
  2000. prev_buttons = buttons;
  2001. update_cursor(mx, my);
  2002. }
  2003. static char *put_str(char *p, const char *s)
  2004. {
  2005. while (*s)
  2006. *p++ = *s++;
  2007. return p;
  2008. }
  2009. static char *put_dec(char *p, unsigned int v)
  2010. {
  2011. char b[10];
  2012. unsigned int n = 0;
  2013. do {
  2014. b[n++] = (char)('0' + v % 10);
  2015. v /= 10;
  2016. } while (v);
  2017. while (n)
  2018. *p++ = b[--n];
  2019. return p;
  2020. }
  2021. /* Expose the last interval in /share so the target can be measured without
  2022. * a serial console. */
  2023. static void write_stats(void)
  2024. {
  2025. char b[80], *p = b;
  2026. unsigned int now = u_ticks_ms();
  2027. p = put_str(p, "t=");
  2028. p = put_dec(p, now / 1000);
  2029. p = put_str(p, "s frames=");
  2030. p = put_dec(p, stat_frames);
  2031. p = put_str(p, " ms=");
  2032. p = put_dec(p, stat_ms);
  2033. p = put_str(p, " max=");
  2034. p = put_dec(p, stat_max);
  2035. p = put_str(p, " px=");
  2036. p = put_dec(p, stat_px);
  2037. p = put_str(p, "\n");
  2038. u_syscall3(SYS_WRITE_FILE, (int)"/share/wmstats", (int)b,
  2039. (int)(p - b));
  2040. }
  2041. void _start(const char *arg)
  2042. {
  2043. struct wm_screen_info si;
  2044. struct wm_status st;
  2045. unsigned int i;
  2046. (void)arg;
  2047. if (u_wm_attach(&si) != 0)
  2048. u_exit(1);
  2049. usurf_init(&screen, si.fb, si.width, si.height, si.pitch, si.bpp);
  2050. usurf_font_attach(&screen, "/ramfs/fonts/sans.ttf", 14);
  2051. fbw = si.width;
  2052. fbh = si.height;
  2053. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  2054. wins[i].info.used = 0;
  2055. wins[i].mapped = 0;
  2056. }
  2057. full_redraw = 1;
  2058. for (;;) {
  2059. unsigned int t0, t1;
  2060. if (u_wm_poll(&st) != 0)
  2061. u_exit(1);
  2062. if (st.full_redraw)
  2063. full_redraw = 1;
  2064. usurf_pixels = 0;
  2065. t0 = u_ticks_ms();
  2066. handle_mouse(&st);
  2067. do_update(&st);
  2068. t1 = u_ticks_ms();
  2069. if (usurf_pixels) {
  2070. unsigned int dt = t1 - t0;
  2071. stat_frames++;
  2072. stat_ms += dt;
  2073. stat_px += usurf_pixels;
  2074. if (dt > stat_max)
  2075. stat_max = dt;
  2076. }
  2077. if (t1 - stat_last >= 2000) {
  2078. stat_last = t1;
  2079. if (stat_frames) {
  2080. write_stats();
  2081. stat_frames = stat_ms = stat_px = stat_max = 0;
  2082. }
  2083. }
  2084. u_sleep_ms(10);
  2085. }
  2086. }