ucmd_wm.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664
  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. #define COL_BORDER 0xC8C8C8u
  10. #define COL_TITLE_ON 0x465A96u
  11. #define COL_TITLE_OFF 0x646464u
  12. #define COL_CLOSE 0xC64040u
  13. #define COL_WHITE 0xFFFFFFu
  14. #define COL_BANNER 0x55FF55u
  15. struct uwin {
  16. struct wm_window_info info;
  17. struct wm_map_info map;
  18. int mapped;
  19. unsigned int ack_seq;
  20. /* Double-click title bar: the geometry to return to when maximized. */
  21. int maximized;
  22. int restore_x, restore_y;
  23. unsigned int restore_w, restore_h;
  24. };
  25. /* Double-click detection on the title bar. */
  26. #define WM_DBLCLICK_MS 500
  27. #define WM_DBLCLICK_SLOP 8
  28. /* A maximized window restores only after the pointer actually starts a drag,
  29. * so a plain click on its title bar leaves it maximized. */
  30. #define WM_DRAG_START 3
  31. static struct usurf screen;
  32. static struct uwin wins[WM_MAX_WINDOWS];
  33. static unsigned int fbw, fbh;
  34. static int drag_id = -1;
  35. static int drag_dx, drag_dy;
  36. static int drag_mx, drag_my; /* press position, for restore-on-drag */
  37. static int resize_id = -1;
  38. static unsigned int resize_mx, resize_my, resize_ow, resize_oh;
  39. static unsigned int last_title_ms;
  40. static int last_title_id = -1, last_title_x, last_title_y;
  41. static unsigned int prev_buttons;
  42. static int full_redraw = 1;
  43. static int boot_term_spawned;
  44. static unsigned int bar_kexec_busy, bar_kexec_progress, bar_kexec_total;
  45. static int bar_temp_c = -1;
  46. static int bar_batt_pct = -1;
  47. static int bar_batt_charging;
  48. static int bar_ac_online = -1;
  49. /* Battery/AC status icon: a plug on AC, a level-filled battery otherwise.
  50. * The rightmost bar item; hovering it shows a percentage tooltip below it. */
  51. #define BAR_PWR_W 24
  52. #define BAR_PWR_Y 6
  53. #define BAR_TIP_H 20
  54. static int bar_pwr_x = -1;
  55. static int bar_pwr_hot;
  56. static int bar_pwr_tip_x = -1;
  57. static int bar_pwr_tip_y;
  58. static unsigned int bar_pwr_tip_w, bar_pwr_tip_h;
  59. static int bar_pwr_tip_want; /* tooltip computed for this frame */
  60. static int bar_pwr_tip_on; /* tooltip pixels currently on screen */
  61. static int bar_pwr_tip_on_x, bar_pwr_tip_on_y;
  62. static unsigned int bar_pwr_tip_on_w, bar_pwr_tip_on_h;
  63. static char bar_msg[24];
  64. static unsigned int bar_msg_until;
  65. static int bar_msg_dirty;
  66. /* Rectangle of the right-aligned date/time text, so a click there can open
  67. * the Date & Time app. w == 0 means "not drawn" (e.g. kexec banner). */
  68. static int bar_clock_x = -1;
  69. static unsigned int bar_clock_w;
  70. static int bar_clock_hot;
  71. static unsigned int stat_frames, stat_ms, stat_px, stat_max, stat_last;
  72. /* Network status icon (right side of the bar). */
  73. #define BAR_NET_W 24
  74. #define BAR_NET_Y 6
  75. #define BAR_NET_NONE 0
  76. #define BAR_NET_WIRED 1
  77. #define BAR_NET_WIFI 2
  78. #define BAR_NET_USB 3
  79. static int bar_net_x = -1;
  80. static unsigned int bar_net_w;
  81. static int bar_net_hot;
  82. static int bar_net_kind;
  83. static int bar_net_bars;
  84. static unsigned int bar_net_color = 0xFF5050u;
  85. static int netmenu_pid;
  86. static int netmenu_placed;
  87. static void bar_notice(const char *msg)
  88. {
  89. unsigned int i;
  90. for (i = 0; msg[i] && i + 1 < sizeof(bar_msg); i++)
  91. bar_msg[i] = msg[i];
  92. bar_msg[i] = '\0';
  93. bar_msg_until = u_ticks_ms() + 2000;
  94. bar_msg_dirty = 1;
  95. }
  96. static int free_window_slot(void)
  97. {
  98. unsigned int i;
  99. for (i = 0; i < WM_MAX_WINDOWS; i++)
  100. if (!wins[i].info.used)
  101. return 1;
  102. return 0;
  103. }
  104. static unsigned int rgb16(unsigned int r, unsigned int g, unsigned int b)
  105. {
  106. return (r << 16) | (g << 8) | b;
  107. }
  108. /* Rainbow gradient: hue 0..1535 (6*256) maps to R→Y→G→C→B→M→R, the same
  109. * integer interpolation the kernel bar uses. */
  110. static unsigned int rainbow_rgb(unsigned int hue)
  111. {
  112. unsigned int seg = hue / 256, f = hue % 256;
  113. unsigned int r1, g1, b1, r2, g2, b2;
  114. switch (seg) {
  115. case 0: r1=255; g1=0; b1=0; r2=255; g2=255; b2=0; break;
  116. case 1: r1=255; g1=255; b1=0; r2=0; g2=255; b2=0; break;
  117. case 2: r1=0; g1=255; b1=0; r2=0; g2=255; b2=255; break;
  118. case 3: r1=0; g1=255; b1=255; r2=0; g2=0; b2=255; break;
  119. case 4: r1=0; g1=0; b1=255; r2=255; g2=0; b2=255; break;
  120. default:r1=255; g1=0; b1=255; r2=255; g2=0; b2=0; break;
  121. }
  122. return rgb16((r1 * (256 - f) + r2 * f) / 256,
  123. (g1 * (256 - f) + g2 * f) / 256,
  124. (b1 * (256 - f) + b2 * f) / 256);
  125. }
  126. static void fill_clip(int x, int y, unsigned int w, unsigned int h,
  127. unsigned int rgb, int cx, int cy,
  128. unsigned int cw, unsigned int ch)
  129. {
  130. int x0 = x, y0 = y, x1 = x + (int)w, y1 = y + (int)h;
  131. if (x0 < cx) x0 = cx;
  132. if (y0 < cy) y0 = cy;
  133. if (x1 > cx + (int)cw) x1 = cx + (int)cw;
  134. if (y1 > cy + (int)ch) y1 = cy + (int)ch;
  135. if (x1 > x0 && y1 > y0)
  136. usurf_fill(&screen, x0, y0, (unsigned int)(x1 - x0),
  137. (unsigned int)(y1 - y0), rgb);
  138. }
  139. static void bar_button(unsigned int x, unsigned int y, unsigned int w,
  140. unsigned int h, const char *label)
  141. {
  142. int tw = (int)usurf_text_width(&screen, label);
  143. usurf_fill(&screen, x, y, w, h, 0x963CD2u);
  144. usurf_text(&screen, (int)x + ((int)w - tw) / 2, (int)(y + 3),
  145. label, COL_WHITE, 0, 0, fbw, WM_BAR_H);
  146. }
  147. static int name_is(const char *a, const char *b)
  148. {
  149. while (*a && *b) {
  150. if (*a != *b)
  151. return 0;
  152. a++;
  153. b++;
  154. }
  155. return *a == *b;
  156. }
  157. static int net_rssi_bars(int rssi)
  158. {
  159. if (rssi >= -55)
  160. return 4;
  161. if (rssi >= -65)
  162. return 3;
  163. if (rssi >= -75)
  164. return 2;
  165. return 1;
  166. }
  167. /* Small top-bar pictograms, drawn directly into the shadow. */
  168. static void bar_icon_cable(int x, int y, unsigned int c)
  169. {
  170. usurf_fill(&screen, x + 1, y + 6, 9, 8, c);
  171. usurf_fill(&screen, x + 3, y + 8, 2, 2, COL_BG);
  172. usurf_fill(&screen, x + 6, y + 8, 2, 2, COL_BG);
  173. usurf_fill(&screen, x + 10, y + 9, 7, 2, c);
  174. usurf_fill(&screen, x + 17, y + 5, 5, 10, c);
  175. usurf_fill(&screen, x + 19, y + 7, 2, 3, COL_BG);
  176. }
  177. static void bar_icon_wifi(int x, int y, unsigned int c, int bars)
  178. {
  179. int i;
  180. for (i = 0; i < 4; i++) {
  181. int h = 3 + i * 3;
  182. usurf_fill(&screen, x + 3 + i * 5, y + 16 - h, 3, h,
  183. i < bars ? c : 0x4A4A5Au);
  184. }
  185. }
  186. static void bar_icon_x(int x, int y, unsigned int c)
  187. {
  188. int i;
  189. for (i = 0; i < 12; i++) {
  190. usurf_fill(&screen, x + 6 + i, y + 4 + i, 2, 2, c);
  191. usurf_fill(&screen, x + 17 - i, y + 4 + i, 2, 2, c);
  192. }
  193. }
  194. static void bar_icon_usb(int x, int y, unsigned int c)
  195. {
  196. usurf_text(&screen, x + 1, y + 1, "USB", c, 0, 0, fbw, WM_BAR_H);
  197. }
  198. static void bar_draw_net_icon(int x, int y)
  199. {
  200. switch (bar_net_kind) {
  201. case BAR_NET_WIRED:
  202. bar_icon_cable(x, y, bar_net_color);
  203. break;
  204. case BAR_NET_WIFI:
  205. bar_icon_wifi(x, y, bar_net_color, bar_net_bars);
  206. break;
  207. case BAR_NET_USB:
  208. bar_icon_usb(x, y, bar_net_color);
  209. break;
  210. default:
  211. bar_icon_x(x, y, bar_net_color);
  212. break;
  213. }
  214. }
  215. /* 1 Hz poll: cached state -> the bar's network icon. The WiFi status call is
  216. * only made when WiFi is the active netdev, so a machine without a card does
  217. * not run a PCI scan on every tick. */
  218. static int poll_net_status(void)
  219. {
  220. struct sys_netinfo ni;
  221. int kind = BAR_NET_NONE, bars = 0, have_ip = 0;
  222. unsigned int color = 0xFF5050u;
  223. int changed;
  224. int i;
  225. if (u_net_info(&ni) == 1 && ni.present) {
  226. for (i = 0; i < 4; i++)
  227. if (ni.ip[i])
  228. have_ip = 1;
  229. if (name_is(ni.name, "wifi")) {
  230. struct sys_wifi_status ws;
  231. if (u_wifi(SYS_WIFI_STATUS, (int)&ws, 0) == 0 &&
  232. ws.present && ws.state >= SYS_WIFI_ST_ASSOC) {
  233. kind = BAR_NET_WIFI;
  234. bars = net_rssi_bars(ws.rssi);
  235. color = (ws.state >= SYS_WIFI_ST_LINK && have_ip) ?
  236. 0x55FF55u : 0xFFD250u;
  237. }
  238. } else if (name_is(ni.name, "rndis")) {
  239. kind = BAR_NET_USB;
  240. color = have_ip ? 0x55FF55u : 0xFFD250u;
  241. } else {
  242. kind = BAR_NET_WIRED;
  243. color = have_ip ? 0x55FF55u : 0xFFD250u;
  244. }
  245. }
  246. changed = kind != bar_net_kind || bars != bar_net_bars ||
  247. color != bar_net_color;
  248. bar_net_kind = kind;
  249. bar_net_bars = bars;
  250. bar_net_color = color;
  251. return changed;
  252. }
  253. static unsigned int temp_color(int c)
  254. {
  255. if (c >= 80)
  256. return 0xFF5050u;
  257. if (c >= 60)
  258. return 0xFFD250u;
  259. return 0x55FF55u;
  260. }
  261. /* "43C" (no libc printf here). */
  262. static unsigned int temp_text(char *b, int c)
  263. {
  264. unsigned int n = 0, v = (unsigned int)c;
  265. if (v >= 100)
  266. b[n++] = (char)('0' + (v / 100) % 10);
  267. if (v >= 10)
  268. b[n++] = (char)('0' + (v / 10) % 10);
  269. b[n++] = (char)('0' + v % 10);
  270. b[n++] = 'C';
  271. b[n] = '\0';
  272. return n;
  273. }
  274. /* "87%" (no libc printf here). */
  275. static unsigned int pct_text(char *b, int pct)
  276. {
  277. unsigned int n = 0, v = (unsigned int)pct;
  278. if (v >= 100)
  279. b[n++] = (char)('0' + (v / 100) % 10);
  280. if (v >= 10)
  281. b[n++] = (char)('0' + (v / 10) % 10);
  282. b[n++] = (char)('0' + v % 10);
  283. b[n++] = '%';
  284. b[n] = '\0';
  285. return n;
  286. }
  287. static unsigned int batt_color(int pct, int charging, int ac)
  288. {
  289. if (pct <= 10)
  290. return 0xFF5050u;
  291. if (pct <= 20)
  292. return 0xFFD250u;
  293. if (charging || ac > 0)
  294. return 0x55FF55u;
  295. return 0x55D2FFu;
  296. }
  297. /* Battery pictogram: outline, terminal and a fill proportional to the
  298. * percentage. A critical charge turns the outline red as well. */
  299. static void bar_icon_battery(int x, int y, int pct, unsigned int c)
  300. {
  301. unsigned int frame = pct <= 10 ? c : 0xD2D2D2u;
  302. unsigned int fill = (unsigned int)((14 * pct + 99) / 100);
  303. usurf_fill(&screen, x + 1, y + 2, 18, 2, frame);
  304. usurf_fill(&screen, x + 1, y + 14, 18, 2, frame);
  305. usurf_fill(&screen, x + 1, y + 2, 2, 14, frame);
  306. usurf_fill(&screen, x + 17, y + 2, 2, 14, frame);
  307. usurf_fill(&screen, x + 19, y + 6, 3, 6, frame);
  308. if (fill > 14)
  309. fill = 14;
  310. if (fill)
  311. usurf_fill(&screen, x + 3, y + 4, fill, 8, c);
  312. }
  313. /* AC plug pictogram: two prongs and the body, kept compact in height. */
  314. static void bar_icon_plug(int x, int y, unsigned int c)
  315. {
  316. usurf_fill(&screen, x + 7, y + 2, 3, 5, c);
  317. usurf_fill(&screen, x + 14, y + 2, 3, 5, c);
  318. usurf_fill(&screen, x + 5, y + 7, 14, 7, c);
  319. usurf_fill(&screen, x + 7, y + 9, 10, 3, COL_BG);
  320. }
  321. /* Percentage tooltip: a small box below the status icon. It is drawn after
  322. * the windows (see bar_tooltip_overlay), so it always sits on top. */
  323. static void bar_draw_tooltip(void)
  324. {
  325. char lb[8];
  326. unsigned int lw;
  327. int tx;
  328. if (!bar_pwr_tip_want || bar_pwr_tip_x < 0)
  329. return;
  330. usurf_fill(&screen, bar_pwr_tip_x, bar_pwr_tip_y,
  331. bar_pwr_tip_w, bar_pwr_tip_h, 0x161622u);
  332. usurf_fill(&screen, bar_pwr_tip_x, bar_pwr_tip_y,
  333. bar_pwr_tip_w, 1, 0x646464u);
  334. usurf_fill(&screen, bar_pwr_tip_x,
  335. bar_pwr_tip_y + (int)bar_pwr_tip_h - 1,
  336. bar_pwr_tip_w, 1, 0x646464u);
  337. usurf_fill(&screen, bar_pwr_tip_x, bar_pwr_tip_y,
  338. 1, bar_pwr_tip_h, 0x646464u);
  339. usurf_fill(&screen, bar_pwr_tip_x + (int)bar_pwr_tip_w - 1,
  340. bar_pwr_tip_y, 1, bar_pwr_tip_h, 0x646464u);
  341. (void)pct_text(lb, bar_batt_pct);
  342. lw = usurf_text_width(&screen, lb);
  343. tx = bar_pwr_tip_x + ((int)bar_pwr_tip_w - (int)lw) / 2;
  344. usurf_text(&screen, tx, bar_pwr_tip_y + 3, lb,
  345. batt_color(bar_batt_pct, bar_batt_charging, bar_ac_online),
  346. 0, 0, fbw, fbh);
  347. }
  348. /* Publish the geometry computed by draw_bar() and redraw the tooltip. */
  349. static void bar_tooltip_commit(void)
  350. {
  351. if (bar_pwr_tip_want) {
  352. bar_draw_tooltip();
  353. bar_pwr_tip_on = 1;
  354. bar_pwr_tip_on_x = bar_pwr_tip_x;
  355. bar_pwr_tip_on_y = bar_pwr_tip_y;
  356. bar_pwr_tip_on_w = bar_pwr_tip_w;
  357. bar_pwr_tip_on_h = bar_pwr_tip_h;
  358. } else {
  359. bar_pwr_tip_on = 0;
  360. }
  361. }
  362. static void draw_bar(void)
  363. {
  364. char dt[48];
  365. unsigned int n, i, x, y;
  366. usurf_fill(&screen, 0, 0, fbw, WM_BAR_H, COL_BG);
  367. usurf_text(&screen, 4, 5, OS_NAME " v" OS_VERSION, COL_BANNER,
  368. 0, 0, fbw, WM_BAR_H);
  369. bar_button(WM_BTN_X, WM_BTN_Y, WM_BTN_W, WM_BTN_H, "New");
  370. bar_button(WM_BTN_CLOCK_X, WM_BTN_CLOCK_Y, WM_BTN_CLOCK_W,
  371. WM_BTN_CLOCK_H, "Clock");
  372. bar_button(WM_BTN_COLORS_X, WM_BTN_COLORS_Y, WM_BTN_COLORS_W,
  373. WM_BTN_COLORS_H, "Colors");
  374. bar_button(WM_BTN_FILES_X, WM_BTN_FILES_Y, WM_BTN_FILES_W,
  375. WM_BTN_FILES_H, "Files");
  376. bar_button(WM_BTN_MINES_X, WM_BTN_MINES_Y, WM_BTN_MINES_W,
  377. WM_BTN_MINES_H, "Mines");
  378. bar_button(WM_BTN_WEB_X, WM_BTN_WEB_Y, WM_BTN_WEB_W,
  379. WM_BTN_WEB_H, "Web");
  380. bar_button(WM_BTN_CARDS_X, WM_BTN_CARDS_Y, WM_BTN_CARDS_W,
  381. WM_BTN_CARDS_H, "Cards");
  382. bar_button(WM_BTN_GALLERY_X, WM_BTN_GALLERY_Y, WM_BTN_GALLERY_W,
  383. WM_BTN_GALLERY_H, "Gallery");
  384. bar_button(WM_BTN_CHAT_X, WM_BTN_CHAT_Y, WM_BTN_CHAT_W,
  385. WM_BTN_CHAT_H, "Chat");
  386. if (bar_msg_until &&
  387. (int)(u_ticks_ms() - bar_msg_until) < 0)
  388. usurf_text(&screen, (int)(WM_BTN_END_X + 8),
  389. 5, bar_msg, 0xFFD250u, 0, 0, fbw, WM_BAR_H);
  390. /* Rainbow separator (same y as the kernel's old bar). */
  391. for (x = 0; x < fbw; x++) {
  392. unsigned int c = rainbow_rgb((x * (6 * 256)) / (fbw ? fbw : 1));
  393. for (y = WM_BAR_H - 5; y < WM_BAR_H - 3; y++)
  394. usurf_fill(&screen, (int)x, (int)y, 1, 1, c);
  395. }
  396. if (bar_kexec_busy) {
  397. static const char msg[] = "loading new kernel over serial";
  398. unsigned int dots = bar_kexec_total ?
  399. (bar_kexec_progress * 12 / bar_kexec_total) : 0;
  400. bar_clock_x = -1;
  401. bar_clock_w = 0;
  402. bar_clock_hot = 0;
  403. bar_net_x = -1;
  404. bar_net_w = 0;
  405. bar_pwr_x = -1;
  406. bar_pwr_tip_x = -1;
  407. bar_pwr_tip_want = 0;
  408. n = 0;
  409. while (msg[n])
  410. dt[n] = msg[n], n++;
  411. for (i = 0; i < 12; i++)
  412. dt[n + i] = (i < dots) ? '.' : ' ';
  413. n += 12;
  414. dt[n] = '\0';
  415. usurf_text(&screen,
  416. (int)fbw - (int)usurf_text_width(&screen, dt) - 4,
  417. 5, dt, 0xFFC864u, 0, 0, fbw, WM_BAR_H);
  418. } else {
  419. unsigned int clock_chars = 0;
  420. int right = (int)fbw - 4;
  421. int min_x = (int)WM_BTN_END_X + 4;
  422. bar_clock_x = -1;
  423. bar_clock_w = 0;
  424. bar_net_x = -1;
  425. bar_net_w = 0;
  426. bar_pwr_x = -1;
  427. bar_pwr_tip_x = -1;
  428. bar_pwr_tip_want = 0;
  429. /* The AC/battery status icon is the rightmost item. */
  430. if (bar_batt_pct >= 0 || bar_ac_online > 0) {
  431. int px = right - BAR_PWR_W;
  432. if (px >= min_x) {
  433. if (bar_pwr_hot)
  434. usurf_fill(&screen, px - 3, 2,
  435. BAR_PWR_W + 6,
  436. WM_BAR_H - 7, 0x3A3A55u);
  437. if (bar_ac_online > 0)
  438. bar_icon_plug(px, BAR_PWR_Y, 0x55FF55u);
  439. else
  440. bar_icon_battery(px, BAR_PWR_Y,
  441. bar_batt_pct,
  442. batt_color(bar_batt_pct,
  443. bar_batt_charging,
  444. bar_ac_online));
  445. bar_pwr_x = px;
  446. right = px - 8;
  447. if (bar_pwr_hot && bar_batt_pct >= 0) {
  448. char lb[8];
  449. unsigned int lw, bw;
  450. int bx;
  451. (void)pct_text(lb, bar_batt_pct);
  452. lw = usurf_text_width(&screen, lb);
  453. bw = lw + 10;
  454. bx = px + BAR_PWR_W - (int)bw;
  455. if (bx < 4)
  456. bx = 4;
  457. if (bx + (int)bw > (int)fbw - 4)
  458. bx = (int)fbw - 4 - (int)bw;
  459. bar_pwr_tip_x = bx;
  460. bar_pwr_tip_y = WM_BAR_H + 2;
  461. bar_pwr_tip_w = bw;
  462. bar_pwr_tip_h = BAR_TIP_H;
  463. bar_pwr_tip_want = 1;
  464. }
  465. }
  466. }
  467. /* The network status sits to the left of the power icon, so it
  468. * survives narrow screens and a full temperature cluster. */
  469. {
  470. int nx = right - BAR_NET_W;
  471. if (nx >= min_x) {
  472. if (bar_net_hot)
  473. usurf_fill(&screen, nx - 3, 2,
  474. BAR_NET_W + 6,
  475. WM_BAR_H - 7, 0x3A3A55u);
  476. bar_draw_net_icon(nx, BAR_NET_Y);
  477. bar_net_x = nx;
  478. bar_net_w = BAR_NET_W;
  479. right = nx - 8;
  480. }
  481. }
  482. if (u_rtc_datetime(dt, sizeof(dt)) == 0) {
  483. int tw = (int)usurf_text_width(&screen, dt);
  484. int tx = right - tw;
  485. n = 0;
  486. while (dt[n] && n < sizeof(dt) - 1)
  487. n++;
  488. bar_clock_x = tx;
  489. bar_clock_w = (unsigned int)tw;
  490. if (bar_clock_hot)
  491. usurf_fill(&screen, tx - 5, 2,
  492. (unsigned int)tw + 10,
  493. WM_BAR_H - 7, 0x3A3A55u);
  494. usurf_text(&screen, tx, 5, dt, COL_BANNER, 0, 0,
  495. fbw, WM_BAR_H);
  496. clock_chars = (unsigned int)tw;
  497. }
  498. if (clock_chars)
  499. right -= (int)clock_chars + 8;
  500. if (bar_temp_c >= 0) {
  501. char tb[12];
  502. int tx;
  503. (void)temp_text(tb, bar_temp_c);
  504. tx = right - (int)usurf_text_width(&screen, tb);
  505. if (tx >= min_x) {
  506. usurf_text(&screen, tx, 5, tb,
  507. temp_color(bar_temp_c), 0, 0,
  508. fbw, WM_BAR_H);
  509. right = tx - 8;
  510. }
  511. }
  512. }
  513. }
  514. static void draw_window_clip(unsigned int idx, int cx, int cy,
  515. unsigned int cw, unsigned int ch)
  516. {
  517. struct uwin *u = &wins[idx];
  518. struct wm_window_info *w = &u->info;
  519. int x = w->x, y = w->y;
  520. unsigned int pw = w->w, ph = w->h;
  521. int bx = x + (int)pw - 1 - WM_CLOSE_W;
  522. int cony = y + 1 + WM_TITLE_H;
  523. int content_x = x + 1;
  524. unsigned int content_w = w->content_w;
  525. unsigned int content_h = w->content_h;
  526. /* Border, title bar, close button. */
  527. fill_clip(x, y, pw, ph, COL_BORDER, cx, cy, cw, ch);
  528. fill_clip(x + 1, y + 1, pw - 2, WM_TITLE_H,
  529. w->focused ? COL_TITLE_ON : COL_TITLE_OFF, cx, cy, cw, ch);
  530. usurf_text(&screen, x + 4, y + 3, w->title, COL_WHITE,
  531. cx, cy, cw, ch);
  532. fill_clip(bx, y + 1, WM_CLOSE_W, WM_TITLE_H, COL_CLOSE,
  533. cx, cy, cw, ch);
  534. usurf_text(&screen, bx + 4, y + 3, "x", COL_WHITE,
  535. cx, cy, cw, ch);
  536. /* Content: every window presents an RGB565 canvas (the terminal
  537. * renders its cells into one; gfx commands own theirs). A mapped
  538. * canvas covers the whole content area, so it does not need the black
  539. * underlay. */
  540. if (u->mapped && u->map.kind == WM_KIND_GFX) {
  541. struct uwm_canvas c;
  542. c.px = (const unsigned short *)u->map.va;
  543. c.w = u->map.w;
  544. c.h = u->map.h;
  545. if (c.w != content_w || c.h != content_h)
  546. fill_clip(content_x, cony, content_w, content_h,
  547. 0x000000u, cx, cy, cw, ch);
  548. usurf_canvas(&screen, &c, content_x, cony,
  549. cx, cy, cw, ch);
  550. } else {
  551. fill_clip(content_x, cony, content_w, content_h,
  552. 0x000000u, cx, cy, cw, ch);
  553. }
  554. /* Resize grip (bottom-right corner). */
  555. if (w->focused) {
  556. unsigned int i;
  557. for (i = 0; i < 10; i++) {
  558. fill_clip(x + (int)pw - 3 - (int)i,
  559. y + (int)ph - 4, 1, 1, 0xD2D2D2u,
  560. cx, cy, cw, ch);
  561. fill_clip(x + (int)pw - 4,
  562. y + (int)ph - 3 - (int)i, 1, 1, 0xD2D2D2u,
  563. cx, cy, cw, ch);
  564. }
  565. }
  566. }
  567. /* Front-to-back window order: the focused window is on top, the others
  568. * follow in descending slot order. Painting and hit-testing share it. */
  569. static unsigned int build_order(int *order)
  570. {
  571. unsigned int i, n = 0;
  572. for (i = 0; i < WM_MAX_WINDOWS; i++)
  573. if (wins[i].info.used && wins[i].info.focused)
  574. order[n++] = (int)i;
  575. for (i = WM_MAX_WINDOWS; i-- > 0;)
  576. if (wins[i].info.used && !wins[i].info.focused)
  577. order[n++] = (int)i;
  578. return n;
  579. }
  580. struct orect {
  581. int x, y;
  582. unsigned int w, h;
  583. };
  584. #define OCC_MAX 64
  585. /* r minus cut, split into up to four disjoint rectangles. */
  586. static unsigned int rect_sub(struct orect *out, const struct orect *r,
  587. const struct orect *cut)
  588. {
  589. int rx0 = r->x, ry0 = r->y;
  590. int rx1 = r->x + (int)r->w, ry1 = r->y + (int)r->h;
  591. int cx0 = cut->x, cy0 = cut->y;
  592. int cx1 = cut->x + (int)cut->w, cy1 = cut->y + (int)cut->h;
  593. unsigned int n = 0;
  594. if (cx0 < rx0) cx0 = rx0;
  595. if (cy0 < ry0) cy0 = ry0;
  596. if (cx1 > rx1) cx1 = rx1;
  597. if (cy1 > ry1) cy1 = ry1;
  598. if (cx1 <= cx0 || cy1 <= cy0) {
  599. out[0] = *r;
  600. return 1;
  601. }
  602. if (cy0 > ry0) {
  603. out[n].x = rx0;
  604. out[n].y = ry0;
  605. out[n].w = (unsigned int)(rx1 - rx0);
  606. out[n].h = (unsigned int)(cy0 - ry0);
  607. n++;
  608. }
  609. if (cy1 < ry1) {
  610. out[n].x = rx0;
  611. out[n].y = cy1;
  612. out[n].w = (unsigned int)(rx1 - rx0);
  613. out[n].h = (unsigned int)(ry1 - cy1);
  614. n++;
  615. }
  616. if (cx0 > rx0) {
  617. out[n].x = rx0;
  618. out[n].y = cy0;
  619. out[n].w = (unsigned int)(cx0 - rx0);
  620. out[n].h = (unsigned int)(cy1 - cy0);
  621. n++;
  622. }
  623. if (cx1 < rx1) {
  624. out[n].x = cx1;
  625. out[n].y = cy0;
  626. out[n].w = (unsigned int)(rx1 - cx1);
  627. out[n].h = (unsigned int)(cy1 - cy0);
  628. n++;
  629. }
  630. return n;
  631. }
  632. /* Back-to-front repaint, used only if the visible-region list overflows. */
  633. static void draw_rect_naive(int x, int y, unsigned int w, unsigned int h)
  634. {
  635. unsigned int i;
  636. usurf_fill(&screen, x, y, w, h, COL_BG);
  637. for (i = 0; i < WM_MAX_WINDOWS; i++)
  638. if (wins[i].info.used && !wins[i].info.focused &&
  639. x < wins[i].info.x + (int)wins[i].info.w &&
  640. x + (int)w > wins[i].info.x &&
  641. y < wins[i].info.y + (int)wins[i].info.h &&
  642. y + (int)h > wins[i].info.y)
  643. draw_window_clip(i, x, y, w, h);
  644. for (i = 0; i < WM_MAX_WINDOWS; i++)
  645. if (wins[i].info.used && wins[i].info.focused &&
  646. x < wins[i].info.x + (int)wins[i].info.w &&
  647. x + (int)w > wins[i].info.x &&
  648. y < wins[i].info.y + (int)wins[i].info.h &&
  649. y + (int)h > wins[i].info.y)
  650. draw_window_clip(i, x, y, w, h);
  651. }
  652. static void draw_rect(int x, int y, unsigned int w, unsigned int h)
  653. {
  654. static struct orect cur[OCC_MAX], nxt[OCC_MAX];
  655. struct orect dmg;
  656. int order[WM_MAX_WINDOWS];
  657. unsigned int n, i, j, ncur, nnext;
  658. int overflow = 0;
  659. int x0 = x, y0 = y, x1 = x + (int)w, y1 = y + (int)h;
  660. if (w == 0 || h == 0)
  661. return;
  662. if (x0 < 0) x0 = 0;
  663. if (y0 < 0) y0 = 0;
  664. if (x1 > (int)fbw) x1 = (int)fbw;
  665. if (y1 > (int)fbh) y1 = (int)fbh;
  666. if (x1 <= x0 || y1 <= y0)
  667. return;
  668. dmg.x = x0;
  669. dmg.y = y0;
  670. dmg.w = (unsigned int)(x1 - x0);
  671. dmg.h = (unsigned int)(y1 - y0);
  672. usurf_fill(&screen, dmg.x, dmg.y, dmg.w, dmg.h, COL_BG);
  673. n = build_order(order);
  674. for (i = 0; i < n; i++) {
  675. struct uwin *u = &wins[order[i]];
  676. int wx0 = u->info.x, wy0 = u->info.y;
  677. int wx1 = wx0 + (int)u->info.w, wy1 = wy0 + (int)u->info.h;
  678. if (wx0 < dmg.x) wx0 = dmg.x;
  679. if (wy0 < dmg.y) wy0 = dmg.y;
  680. if (wx1 > x1) wx1 = x1;
  681. if (wy1 > y1) wy1 = y1;
  682. if (wx1 <= wx0 || wy1 <= wy0)
  683. continue;
  684. cur[0].x = wx0;
  685. cur[0].y = wy0;
  686. cur[0].w = (unsigned int)(wx1 - wx0);
  687. cur[0].h = (unsigned int)(wy1 - wy0);
  688. ncur = 1;
  689. for (j = 0; j < i && ncur; j++) {
  690. struct uwin *f = &wins[order[j]];
  691. struct orect fr;
  692. unsigned int k;
  693. fr.x = f->info.x;
  694. fr.y = f->info.y;
  695. fr.w = f->info.w;
  696. fr.h = f->info.h;
  697. nnext = 0;
  698. for (k = 0; k < ncur; k++) {
  699. if (nnext + 4 > OCC_MAX) {
  700. overflow = 1;
  701. break;
  702. }
  703. nnext += rect_sub(&nxt[nnext], &cur[k], &fr);
  704. }
  705. if (overflow)
  706. break;
  707. for (k = 0; k < nnext; k++)
  708. cur[k] = nxt[k];
  709. ncur = nnext;
  710. }
  711. if (overflow)
  712. break;
  713. for (j = 0; j < ncur; j++)
  714. draw_window_clip((unsigned int)order[i], cur[j].x,
  715. cur[j].y, cur[j].w, cur[j].h);
  716. }
  717. if (overflow)
  718. draw_rect_naive(dmg.x, dmg.y, dmg.w, dmg.h);
  719. }
  720. static int window_at(int mx, int my)
  721. {
  722. int order[WM_MAX_WINDOWS];
  723. unsigned int n = build_order(order), i;
  724. for (i = 0; i < n; i++) {
  725. struct wm_window_info *w = &wins[order[i]].info;
  726. if (mx >= w->x && my >= w->y &&
  727. mx < w->x + (int)w->w &&
  728. my < w->y + (int)w->h)
  729. return order[i];
  730. }
  731. return -1;
  732. }
  733. static void clamp_geom(struct uwin *u, int *x, int *y)
  734. {
  735. /* Match wm_clamp_window(): allow the window to hang off the left,
  736. * right and bottom edges but keep the title bar reachable and below
  737. * the system bar. */
  738. int minx = WM_DRAG_VISIBLE - (int)u->info.w;
  739. int maxx = (int)fbw - WM_DRAG_VISIBLE;
  740. int miny = WM_BAR_H;
  741. int maxy = (int)fbh - WM_TITLE_H;
  742. if (minx > maxx)
  743. minx = maxx = 0;
  744. if (miny > maxy)
  745. miny = maxy = WM_BAR_H;
  746. if (*x < minx)
  747. *x = minx;
  748. else if (*x > maxx)
  749. *x = maxx;
  750. if (*y < miny)
  751. *y = miny;
  752. else if (*y > maxy)
  753. *y = maxy;
  754. }
  755. static void add_rect(struct wm_present_info *p, int x, int y,
  756. unsigned int w, unsigned int h)
  757. {
  758. if (w == 0 || h == 0)
  759. return;
  760. if (p->count < 4) {
  761. p->rect[p->count].x = x;
  762. p->rect[p->count].y = y;
  763. p->rect[p->count].w = w;
  764. p->rect[p->count].h = h;
  765. p->count++;
  766. } else {
  767. /* Four is enough for normal updates; fold the rest into the
  768. * last rect rather than dropping pixels. */
  769. int x0 = p->rect[3].x, y0 = p->rect[3].y;
  770. int x1 = x0 + (int)p->rect[3].w, y1 = y0 + (int)p->rect[3].h;
  771. int nx1 = x + (int)w, ny1 = y + (int)h;
  772. if (x < x0) x0 = x;
  773. if (y < y0) y0 = y;
  774. if (nx1 > x1) x1 = nx1;
  775. if (ny1 > y1) y1 = ny1;
  776. p->rect[3].x = x0;
  777. p->rect[3].y = y0;
  778. p->rect[3].w = (unsigned int)(x1 - x0);
  779. p->rect[3].h = (unsigned int)(y1 - y0);
  780. }
  781. }
  782. /* Redraw the power tooltip above whatever a render pass just painted. */
  783. static void bar_tooltip_overlay(struct wm_present_info *p)
  784. {
  785. if (!bar_pwr_tip_on || !bar_pwr_tip_want)
  786. return;
  787. bar_draw_tooltip();
  788. add_rect(p, bar_pwr_tip_on_x, bar_pwr_tip_on_y,
  789. bar_pwr_tip_on_w, bar_pwr_tip_on_h);
  790. }
  791. /* A focused window is topmost, so the shadow contains exactly its pixels in
  792. * the old rectangle. Move that block and repaint only the old part the new
  793. * rectangle does not cover. When either rectangle is partly off-screen the
  794. * shadow has no valid pixels for the hidden part, so repaint the new rect
  795. * instead of copying desktop background into the window. */
  796. static void move_window_pixels(struct uwin *u, int ox, int oy, int nx, int ny)
  797. {
  798. struct wm_present_info present;
  799. struct orect oldr, newr, pieces[4];
  800. unsigned int np, k;
  801. int w = (int)u->info.w, h = (int)u->info.h;
  802. int old_on = ox >= 0 && oy >= 0 &&
  803. ox + w <= (int)fbw && oy + h <= (int)fbh;
  804. int new_on = nx >= 0 && ny >= 0 &&
  805. nx + w <= (int)fbw && ny + h <= (int)fbh;
  806. if (u_wm_begin() != 0)
  807. return;
  808. if (old_on && new_on)
  809. usurf_copy(&screen, nx, ny, ox, oy, u->info.w, u->info.h);
  810. else
  811. draw_rect(nx, ny, u->info.w, u->info.h);
  812. oldr.x = ox;
  813. oldr.y = oy;
  814. oldr.w = u->info.w;
  815. oldr.h = u->info.h;
  816. newr.x = nx;
  817. newr.y = ny;
  818. newr.w = u->info.w;
  819. newr.h = u->info.h;
  820. np = rect_sub(pieces, &oldr, &newr);
  821. for (k = 0; k < np; k++)
  822. draw_rect(pieces[k].x, pieces[k].y, pieces[k].w, pieces[k].h);
  823. present.count = 0;
  824. present.immediate = 1;
  825. add_rect(&present, nx, ny, u->info.w, u->info.h);
  826. for (k = 0; k < np; k++)
  827. add_rect(&present, pieces[k].x, pieces[k].y,
  828. pieces[k].w, pieces[k].h);
  829. bar_tooltip_overlay(&present);
  830. u_wm_present(&present);
  831. }
  832. static int iabs(int v)
  833. {
  834. return v < 0 ? -v : v;
  835. }
  836. /* Repaint the old and new rectangles of a geometry change. */
  837. static void redraw_geom(struct uwin *u, int ox, int oy,
  838. unsigned int ow, unsigned int oh)
  839. {
  840. struct wm_present_info present;
  841. unsigned int i;
  842. present.count = 0;
  843. present.immediate = 1;
  844. add_rect(&present, ox, oy, ow, oh);
  845. add_rect(&present, u->info.x, u->info.y, u->info.w, u->info.h);
  846. if (u_wm_begin() == 0) {
  847. for (i = 0; i < present.count; i++)
  848. draw_rect(present.rect[i].x, present.rect[i].y,
  849. present.rect[i].w, present.rect[i].h);
  850. bar_tooltip_overlay(&present);
  851. u_wm_present(&present);
  852. }
  853. }
  854. /* Resize (when needed), move and remap a window in one step. */
  855. static int set_window_geometry(struct uwin *u, unsigned int idx,
  856. int nx, int ny, unsigned int nw, unsigned int nh)
  857. {
  858. struct wm_window_info ni;
  859. int ox = u->info.x, oy = u->info.y;
  860. unsigned int ow = u->info.w, oh = u->info.h;
  861. if ((int)nw != (int)ow || (int)nh != (int)oh) {
  862. if (u_wm_resize(idx, nw - 2, nh - WM_TITLE_H - 2) != 0)
  863. return -1;
  864. }
  865. if (nx != ox || ny != oy)
  866. u_wm_set_geom(idx, nx, ny);
  867. u->mapped = 0;
  868. if (u_wm_info(idx, &ni) == 1)
  869. u->info = ni;
  870. if (u_wm_map(idx, &u->map) == 0)
  871. u->mapped = 1;
  872. redraw_geom(u, ox, oy, ow, oh);
  873. return 0;
  874. }
  875. static int restore_window(struct uwin *u, unsigned int idx)
  876. {
  877. if (!u->maximized)
  878. return 0;
  879. if (set_window_geometry(u, idx, u->restore_x, u->restore_y,
  880. u->restore_w, u->restore_h) != 0)
  881. return -1;
  882. u->maximized = 0;
  883. return 0;
  884. }
  885. /* Double-click title bar: fill the work area below the system bar, or go back
  886. * to the geometry the window had before it was maximized. */
  887. static int toggle_maximize(struct uwin *u, unsigned int idx)
  888. {
  889. if (u->maximized)
  890. return restore_window(u, idx);
  891. u->restore_x = u->info.x;
  892. u->restore_y = u->info.y;
  893. u->restore_w = u->info.w;
  894. u->restore_h = u->info.h;
  895. if (set_window_geometry(u, idx, 0, WM_BAR_H,
  896. fbw, fbh - WM_BAR_H) != 0)
  897. return -1;
  898. u->maximized = 1;
  899. return 0;
  900. }
  901. /* The top-bar date/time text is also a button: it opens the Date & Time
  902. * editor. The rectangle is recorded by draw_bar(). */
  903. static int bar_clock_hit(unsigned int mx, unsigned int my)
  904. {
  905. return bar_clock_w && bar_clock_x >= 0 && my < WM_BAR_H &&
  906. (int)mx >= bar_clock_x - 5 &&
  907. (int)mx < bar_clock_x + (int)bar_clock_w + 5;
  908. }
  909. static int bar_net_hit(unsigned int mx, unsigned int my)
  910. {
  911. return bar_net_w && bar_net_x >= 0 && my < WM_BAR_H &&
  912. (int)mx >= bar_net_x - 3 &&
  913. (int)mx < bar_net_x + (int)bar_net_w + 3;
  914. }
  915. /* The AC/battery icon has no click action, but hovering it reveals the
  916. * percentage tooltip below it; the tooltip area keeps the hover alive. */
  917. static int bar_pwr_hit(unsigned int mx, unsigned int my)
  918. {
  919. if (my >= WM_BAR_H)
  920. return bar_pwr_tip_on &&
  921. (int)mx >= bar_pwr_tip_on_x &&
  922. (int)mx < bar_pwr_tip_on_x + (int)bar_pwr_tip_on_w &&
  923. (int)my >= bar_pwr_tip_on_y &&
  924. (int)my < bar_pwr_tip_on_y + (int)bar_pwr_tip_on_h;
  925. return bar_pwr_x >= 0 &&
  926. (int)mx >= bar_pwr_x - 3 &&
  927. (int)mx < bar_pwr_x + BAR_PWR_W + 3;
  928. }
  929. static void do_update(struct wm_status *st)
  930. {
  931. struct wm_present_info present;
  932. int need = 0;
  933. int bar_dirty = 0;
  934. unsigned int i;
  935. char dt[20];
  936. static char last_dt[20];
  937. int kexec_changed = (st->kexec_busy != bar_kexec_busy) ||
  938. (st->kexec_busy && st->kexec_progress != bar_kexec_progress);
  939. unsigned int msg_now = u_ticks_ms();
  940. /* Spawn the first terminal once the kernel says the storage scan is
  941. * done, so a shell command can never race the mount table. */
  942. if (st->boot_ready && !boot_term_spawned) {
  943. boot_term_spawned = 1;
  944. if (!free_window_slot()) {
  945. bar_notice("window limit");
  946. full_redraw = 1;
  947. } else if (u_wm_create_term() < 0) {
  948. bar_notice("cannot start term");
  949. full_redraw = 1;
  950. } else {
  951. full_redraw = 1;
  952. }
  953. }
  954. if (bar_msg_dirty ||
  955. (bar_msg_until && (int)(msg_now - bar_msg_until) >= 0)) {
  956. bar_dirty = 1;
  957. bar_msg_dirty = 0;
  958. if (bar_msg_until && (int)(msg_now - bar_msg_until) >= 0)
  959. bar_msg_until = 0;
  960. }
  961. bar_kexec_busy = st->kexec_busy;
  962. bar_kexec_progress = st->kexec_progress;
  963. bar_kexec_total = st->kexec_total;
  964. if (kexec_changed)
  965. bar_dirty = 1;
  966. {
  967. int hot = bar_clock_hit(st->mouse.x, st->mouse.y);
  968. if (hot != bar_clock_hot) {
  969. bar_clock_hot = hot;
  970. bar_dirty = 1;
  971. }
  972. }
  973. {
  974. int hot = bar_net_hit(st->mouse.x, st->mouse.y);
  975. if (hot != bar_net_hot) {
  976. bar_net_hot = hot;
  977. bar_dirty = 1;
  978. }
  979. }
  980. {
  981. int hot = bar_pwr_hit(st->mouse.x, st->mouse.y);
  982. if (hot != bar_pwr_hot) {
  983. bar_pwr_hot = hot;
  984. bar_dirty = 1;
  985. }
  986. }
  987. /* Refresh cached window state and mappings before opening a frame:
  988. * MAP may unmap/remap content. */
  989. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  990. struct wm_window_info info;
  991. int r = u_wm_info(i, &info);
  992. if (r < 0) {
  993. u_exit(1);
  994. }
  995. if (r == 0 || !info.used) {
  996. if (wins[i].info.used)
  997. full_redraw = 1;
  998. wins[i].info.used = 0;
  999. wins[i].mapped = 0;
  1000. continue;
  1001. }
  1002. if (!wins[i].info.used ||
  1003. info.content_gen != wins[i].info.content_gen)
  1004. full_redraw = 1;
  1005. wins[i].info = info;
  1006. if (!wins[i].mapped ||
  1007. wins[i].map.generation != info.content_gen) {
  1008. struct wm_map_info map;
  1009. if (u_wm_map(i, &map) == 0) {
  1010. wins[i].map = map;
  1011. wins[i].mapped = 1;
  1012. } else {
  1013. wins[i].mapped = 0;
  1014. }
  1015. }
  1016. }
  1017. /* Anchor the network menu under its bar icon once it has created its
  1018. * window. The WM owns the geometry syscall, so the app itself does
  1019. * not need a special placement ABI. */
  1020. if (netmenu_pid > 0) {
  1021. int found = 0;
  1022. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1023. struct wm_window_info ni;
  1024. if (!wins[i].info.used ||
  1025. wins[i].info.pid != (unsigned int)netmenu_pid)
  1026. continue;
  1027. found = 1;
  1028. if (!netmenu_placed) {
  1029. int nx = (bar_net_x >= 0 ?
  1030. bar_net_x + (int)bar_net_w :
  1031. (int)fbw) - (int)wins[i].info.w;
  1032. int ny = WM_BAR_H + 2;
  1033. if (nx < 4)
  1034. nx = 4;
  1035. if (nx + (int)wins[i].info.w > (int)fbw - 4)
  1036. nx = (int)fbw - 4 -
  1037. (int)wins[i].info.w;
  1038. if (u_wm_set_geom(i, nx, ny) == 0 &&
  1039. u_wm_info(i, &ni) == 1) {
  1040. wins[i].info = ni;
  1041. full_redraw = 1;
  1042. }
  1043. netmenu_placed = 1;
  1044. }
  1045. break;
  1046. }
  1047. if (!found && !u_alive((unsigned int)netmenu_pid)) {
  1048. netmenu_pid = 0;
  1049. netmenu_placed = 0;
  1050. }
  1051. }
  1052. /* The RTC is slow (CMOS polling) and the temperature/battery state only
  1053. * changes once a second; query them at 1 Hz instead of every 10 ms. */
  1054. {
  1055. static unsigned int last_rtc_ms;
  1056. unsigned int now_ms = u_ticks_ms();
  1057. if (full_redraw || now_ms - last_rtc_ms >= 1000) {
  1058. last_rtc_ms = now_ms;
  1059. if (u_rtc_datetime(dt, sizeof(dt)) == 0) {
  1060. for (i = 0; i < sizeof(dt); i++)
  1061. if (dt[i] != last_dt[i]) {
  1062. for (i = 0; i < sizeof(dt); i++)
  1063. last_dt[i] = dt[i];
  1064. bar_dirty = 1;
  1065. break;
  1066. }
  1067. }
  1068. {
  1069. struct sys_cputemp ct;
  1070. if (u_cpu_temp(&ct) == 0 &&
  1071. ct.temp_c != bar_temp_c) {
  1072. bar_temp_c = ct.temp_c;
  1073. bar_dirty = 1;
  1074. }
  1075. }
  1076. {
  1077. struct sys_powerinfo pi;
  1078. int pct = -1, charging = 0, ac = -1;
  1079. unsigned int bi;
  1080. if (u_power_info(&pi, 0) == 0) {
  1081. /* A laptop EC (ThinkPad or Chrome
  1082. * EC) marks a machine with AC/battery;
  1083. * desktops show neither. */
  1084. if (pi.present) {
  1085. ac = pi.ac_online;
  1086. for (bi = 0; bi < SYS_POWER_BATT_MAX;
  1087. bi++) {
  1088. struct sys_battery *b =
  1089. &pi.battery[bi];
  1090. if (!b->present ||
  1091. !b->percent_valid)
  1092. continue;
  1093. pct = (int)b->percent;
  1094. charging = (int)b->charging;
  1095. break;
  1096. }
  1097. }
  1098. if (pct != bar_batt_pct ||
  1099. charging != bar_batt_charging ||
  1100. ac != bar_ac_online) {
  1101. bar_batt_pct = pct;
  1102. bar_batt_charging = charging;
  1103. bar_ac_online = ac;
  1104. bar_dirty = 1;
  1105. }
  1106. }
  1107. }
  1108. if (poll_net_status())
  1109. bar_dirty = 1;
  1110. }
  1111. }
  1112. if (full_redraw) {
  1113. if (u_wm_begin() != 0) {
  1114. full_redraw = 1;
  1115. return;
  1116. }
  1117. full_redraw = 0;
  1118. need = 1;
  1119. {
  1120. draw_rect(0, 0, fbw, fbh);
  1121. draw_bar();
  1122. bar_tooltip_commit();
  1123. present.count = 0;
  1124. add_rect(&present, 0, 0, fbw, fbh);
  1125. present.immediate = 0;
  1126. u_wm_present(&present);
  1127. /* Ack every window that was clean at the time of the
  1128. * redraw. If new damage arrived during it, its seq
  1129. * differs and the kernel leaves it dirty. */
  1130. for (i = 0; i < WM_MAX_WINDOWS; i++)
  1131. if (wins[i].info.used && wins[i].mapped)
  1132. u_wm_ack(i, wins[i].info.dirty_seq);
  1133. }
  1134. return;
  1135. }
  1136. /* Partial cell/canvas updates and the bar clock. */
  1137. present.count = 0;
  1138. present.immediate = 0;
  1139. if (bar_dirty) {
  1140. need = 1;
  1141. add_rect(&present, 0, 0, fbw, WM_BAR_H);
  1142. }
  1143. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1144. struct uwin *u = &wins[i];
  1145. struct wm_window_info *w = &u->info;
  1146. int rx, ry;
  1147. unsigned int rw, rh;
  1148. if (!w->used || !w->dirty)
  1149. continue;
  1150. if (!w->dmg_w || !w->dmg_h)
  1151. continue;
  1152. rx = w->x + 1 + (int)w->dmg_x;
  1153. ry = w->y + 1 + WM_TITLE_H + (int)w->dmg_y;
  1154. rw = w->dmg_w;
  1155. rh = w->dmg_h;
  1156. add_rect(&present, rx, ry, rw, rh);
  1157. u->ack_seq = u->mapped ? w->dirty_seq : 0;
  1158. need = 1;
  1159. }
  1160. /* Repaint the previous tooltip area so hiding or moving it restores
  1161. * the window/desktop underneath; the new one is drawn after the
  1162. * windows below. */
  1163. if (bar_pwr_tip_on) {
  1164. need = 1;
  1165. add_rect(&present, bar_pwr_tip_on_x, bar_pwr_tip_on_y,
  1166. bar_pwr_tip_on_w, bar_pwr_tip_on_h);
  1167. }
  1168. if (!need) {
  1169. /* Drag movement is handled below; a stationary mouse can be
  1170. * left to the kernel's cursor overlay. */
  1171. return;
  1172. }
  1173. if (u_wm_begin() != 0)
  1174. return;
  1175. for (i = 0; i < present.count; i++)
  1176. draw_rect(present.rect[i].x, present.rect[i].y,
  1177. present.rect[i].w, present.rect[i].h);
  1178. if (bar_dirty)
  1179. draw_bar();
  1180. if (bar_pwr_tip_want) {
  1181. bar_tooltip_commit();
  1182. add_rect(&present, bar_pwr_tip_on_x, bar_pwr_tip_on_y,
  1183. bar_pwr_tip_on_w, bar_pwr_tip_on_h);
  1184. } else {
  1185. bar_pwr_tip_on = 0;
  1186. }
  1187. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1188. struct uwin *u = &wins[i];
  1189. if (!u->info.used || !u->info.dirty || !u->mapped ||
  1190. !u->ack_seq)
  1191. continue;
  1192. if (u->ack_seq == u->info.dirty_seq)
  1193. u_wm_ack(i, u->info.dirty_seq);
  1194. }
  1195. u_wm_present(&present);
  1196. }
  1197. static int spawn_app(const char *path)
  1198. {
  1199. int pid = u_spawn(path, "", 0, "/");
  1200. if (pid > 0)
  1201. full_redraw = 1;
  1202. return pid;
  1203. }
  1204. static void spawn_app_checked(const char *path)
  1205. {
  1206. if (!free_window_slot()) {
  1207. bar_notice("window limit");
  1208. full_redraw = 1;
  1209. return;
  1210. }
  1211. if (spawn_app(path) < 0) {
  1212. bar_notice("spawn failed");
  1213. full_redraw = 1;
  1214. }
  1215. }
  1216. /* The network icon opens one menu at a time; a second click focuses it. */
  1217. static void open_netmenu(void)
  1218. {
  1219. unsigned int i;
  1220. if (netmenu_pid > 0 && u_alive((unsigned int)netmenu_pid)) {
  1221. for (i = 0; i < WM_MAX_WINDOWS; i++)
  1222. if (wins[i].info.used &&
  1223. wins[i].info.pid == (unsigned int)netmenu_pid) {
  1224. u_wm_focus(i);
  1225. full_redraw = 1;
  1226. return;
  1227. }
  1228. }
  1229. if (!free_window_slot()) {
  1230. bar_notice("window limit");
  1231. full_redraw = 1;
  1232. return;
  1233. }
  1234. netmenu_pid = u_spawn("/ramfs/bin/netmenu", "", 0, "/");
  1235. netmenu_placed = 0;
  1236. if (netmenu_pid < 0) {
  1237. netmenu_pid = 0;
  1238. bar_notice("spawn failed");
  1239. full_redraw = 1;
  1240. } else {
  1241. full_redraw = 1;
  1242. }
  1243. }
  1244. static void handle_mouse(struct wm_status *st)
  1245. {
  1246. unsigned int buttons = st->mouse.buttons;
  1247. unsigned int mx = st->mouse.x, my = st->mouse.y;
  1248. int down = (buttons & 1) != 0;
  1249. int was = (prev_buttons & 1) != 0;
  1250. if (down && !was) {
  1251. int prev_title_id = last_title_id;
  1252. unsigned int now = u_ticks_ms();
  1253. last_title_id = -1;
  1254. if (bar_net_hit(mx, my)) {
  1255. open_netmenu();
  1256. } else if (bar_clock_hit(mx, my)) {
  1257. spawn_app_checked("/ramfs/bin/settime");
  1258. } else if (mx >= WM_BTN_X && my >= WM_BTN_Y &&
  1259. mx < WM_BTN_X + WM_BTN_W && my < WM_BTN_Y + WM_BTN_H) {
  1260. if (!free_window_slot()) {
  1261. bar_notice("window limit");
  1262. full_redraw = 1;
  1263. } else if (u_wm_create_term() < 0) {
  1264. bar_notice("cannot start term");
  1265. full_redraw = 1;
  1266. } else {
  1267. full_redraw = 1;
  1268. }
  1269. } else if (mx >= WM_BTN_CLOCK_X && my >= WM_BTN_CLOCK_Y &&
  1270. mx < WM_BTN_CLOCK_X + WM_BTN_CLOCK_W &&
  1271. my < WM_BTN_CLOCK_Y + WM_BTN_CLOCK_H) {
  1272. spawn_app_checked("/ramfs/bin/clock");
  1273. } else if (mx >= WM_BTN_COLORS_X && my >= WM_BTN_COLORS_Y &&
  1274. mx < WM_BTN_COLORS_X + WM_BTN_COLORS_W &&
  1275. my < WM_BTN_COLORS_Y + WM_BTN_COLORS_H) {
  1276. spawn_app_checked("/ramfs/bin/colors");
  1277. } else if (mx >= WM_BTN_FILES_X && my >= WM_BTN_FILES_Y &&
  1278. mx < WM_BTN_FILES_X + WM_BTN_FILES_W &&
  1279. my < WM_BTN_FILES_Y + WM_BTN_FILES_H) {
  1280. spawn_app_checked("/ramfs/bin/files");
  1281. } else if (mx >= WM_BTN_MINES_X && my >= WM_BTN_MINES_Y &&
  1282. mx < WM_BTN_MINES_X + WM_BTN_MINES_W &&
  1283. my < WM_BTN_MINES_Y + WM_BTN_MINES_H) {
  1284. spawn_app_checked("/ramfs/bin/mines");
  1285. } else if (mx >= WM_BTN_WEB_X && my >= WM_BTN_WEB_Y &&
  1286. mx < WM_BTN_WEB_X + WM_BTN_WEB_W &&
  1287. my < WM_BTN_WEB_Y + WM_BTN_WEB_H) {
  1288. spawn_app_checked("/ramfs/bin/browser");
  1289. } else if (mx >= WM_BTN_CARDS_X && my >= WM_BTN_CARDS_Y &&
  1290. mx < WM_BTN_CARDS_X + WM_BTN_CARDS_W &&
  1291. my < WM_BTN_CARDS_Y + WM_BTN_CARDS_H) {
  1292. spawn_app_checked("/ramfs/bin/solitaire");
  1293. } else if (mx >= WM_BTN_GALLERY_X && my >= WM_BTN_GALLERY_Y &&
  1294. mx < WM_BTN_GALLERY_X + WM_BTN_GALLERY_W &&
  1295. my < WM_BTN_GALLERY_Y + WM_BTN_GALLERY_H) {
  1296. spawn_app_checked("/ramfs/bin/gallery");
  1297. } else if (mx >= WM_BTN_CHAT_X && my >= WM_BTN_CHAT_Y &&
  1298. mx < WM_BTN_CHAT_X + WM_BTN_CHAT_W &&
  1299. my < WM_BTN_CHAT_Y + WM_BTN_CHAT_H) {
  1300. spawn_app_checked("/ramfs/bin/chat");
  1301. } else {
  1302. int idx = window_at((int)mx, (int)my);
  1303. if (idx >= 0) {
  1304. struct uwin *u = &wins[idx];
  1305. int pmx = (int)mx, pmy = (int)my;
  1306. int rx = u->info.x + (int)u->info.w - 1 -
  1307. WM_CLOSE_W;
  1308. int ry = u->info.y + 1;
  1309. u_wm_focus((unsigned int)idx);
  1310. if (pmx >= rx &&
  1311. pmx < u->info.x + (int)u->info.w &&
  1312. pmy >= ry && pmy < ry + WM_TITLE_H) {
  1313. u_wm_close((unsigned int)idx);
  1314. full_redraw = 1;
  1315. } else if (pmx >= u->info.x + (int)u->info.w - 16 &&
  1316. pmy >= u->info.y + (int)u->info.h - 16) {
  1317. resize_id = idx;
  1318. resize_mx = mx;
  1319. resize_my = my;
  1320. resize_ow = u->info.w;
  1321. resize_oh = u->info.h;
  1322. } else if (pmy < u->info.y + WM_TITLE_H) {
  1323. if (prev_title_id == idx &&
  1324. now - last_title_ms <= WM_DBLCLICK_MS &&
  1325. iabs(pmx - last_title_x) <= WM_DBLCLICK_SLOP &&
  1326. iabs(pmy - last_title_y) <= WM_DBLCLICK_SLOP) {
  1327. last_title_ms = 0;
  1328. if (toggle_maximize(u,
  1329. (unsigned int)idx) != 0)
  1330. bar_notice("resize failed");
  1331. } else {
  1332. last_title_id = idx;
  1333. last_title_ms = now;
  1334. last_title_x = pmx;
  1335. last_title_y = pmy;
  1336. drag_id = idx;
  1337. drag_mx = pmx;
  1338. drag_my = pmy;
  1339. drag_dx = pmx - u->info.x;
  1340. drag_dy = pmy - u->info.y;
  1341. }
  1342. }
  1343. }
  1344. }
  1345. } else if (down && was && resize_id >= 0) {
  1346. struct uwin *u = &wins[resize_id];
  1347. if (u->info.used) {
  1348. int nw = (int)resize_ow + (int)mx - (int)resize_mx;
  1349. int nh = (int)resize_oh + (int)my - (int)resize_my;
  1350. int maxw = (int)fbw;
  1351. int maxh = (int)fbh - WM_BAR_H;
  1352. if (nw < 200) nw = 200;
  1353. if (nh < 140) nh = 140;
  1354. if (nw > maxw) nw = maxw;
  1355. if (nh > maxh) nh = maxh;
  1356. /* Dragging the grip into the screen corner means "as
  1357. * large as the work area", even though the grip starts
  1358. * 16 px inside the window edge. */
  1359. if ((int)mx >= (int)fbw - 1)
  1360. nw = maxw;
  1361. if ((int)my >= (int)fbh - 1)
  1362. nh = maxh;
  1363. if (nw != (int)u->info.w || nh != (int)u->info.h) {
  1364. if (set_window_geometry(u,
  1365. (unsigned int)resize_id, u->info.x,
  1366. u->info.y, (unsigned int)nw,
  1367. (unsigned int)nh) == 0)
  1368. u->maximized = 0;
  1369. }
  1370. } else {
  1371. resize_id = -1;
  1372. }
  1373. } else if (down && was && drag_id >= 0) {
  1374. struct uwin *u = &wins[drag_id];
  1375. if (u->info.used) {
  1376. int ox, oy, nx, ny;
  1377. int started = !u->maximized ||
  1378. iabs((int)mx - drag_mx) >= WM_DRAG_START ||
  1379. iabs((int)my - drag_my) >= WM_DRAG_START;
  1380. /* A maximized window is restored as soon as the drag
  1381. * starts; keep the pointer's grab point relative to the
  1382. * restored geometry. A click that does not move must
  1383. * leave it maximized. */
  1384. if (u->maximized && started &&
  1385. restore_window(u, (unsigned int)drag_id) == 0) {
  1386. drag_dx = drag_mx - u->info.x;
  1387. drag_dy = drag_my - u->info.y;
  1388. }
  1389. if (!u->maximized) {
  1390. ox = u->info.x;
  1391. oy = u->info.y;
  1392. nx = (int)mx - drag_dx;
  1393. ny = (int)my - drag_dy;
  1394. clamp_geom(u, &nx, &ny);
  1395. if (nx != ox || ny != oy) {
  1396. struct wm_present_info present;
  1397. unsigned int i;
  1398. u_wm_set_geom((unsigned int)drag_id,
  1399. nx, ny);
  1400. u->info.x = nx;
  1401. u->info.y = ny;
  1402. if (u->info.focused) {
  1403. move_window_pixels(u, ox, oy,
  1404. nx, ny);
  1405. } else {
  1406. present.count = 0;
  1407. present.immediate = 1;
  1408. add_rect(&present, ox, oy,
  1409. u->info.w, u->info.h);
  1410. add_rect(&present, nx, ny,
  1411. u->info.w, u->info.h);
  1412. if (u_wm_begin() == 0) {
  1413. for (i = 0;
  1414. i < present.count; i++)
  1415. draw_rect(
  1416. present.rect[i].x,
  1417. present.rect[i].y,
  1418. present.rect[i].w,
  1419. present.rect[i].h);
  1420. bar_tooltip_overlay(&present);
  1421. u_wm_present(&present);
  1422. }
  1423. }
  1424. }
  1425. }
  1426. } else {
  1427. drag_id = -1;
  1428. }
  1429. } else if (!down && was) {
  1430. drag_id = -1;
  1431. resize_id = -1;
  1432. }
  1433. prev_buttons = buttons;
  1434. }
  1435. static char *put_str(char *p, const char *s)
  1436. {
  1437. while (*s)
  1438. *p++ = *s++;
  1439. return p;
  1440. }
  1441. static char *put_dec(char *p, unsigned int v)
  1442. {
  1443. char b[10];
  1444. unsigned int n = 0;
  1445. do {
  1446. b[n++] = (char)('0' + v % 10);
  1447. v /= 10;
  1448. } while (v);
  1449. while (n)
  1450. *p++ = b[--n];
  1451. return p;
  1452. }
  1453. /* Expose the last interval in /share so the target can be measured without
  1454. * a serial console. */
  1455. static void write_stats(void)
  1456. {
  1457. char b[80], *p = b;
  1458. unsigned int now = u_ticks_ms();
  1459. p = put_str(p, "t=");
  1460. p = put_dec(p, now / 1000);
  1461. p = put_str(p, "s frames=");
  1462. p = put_dec(p, stat_frames);
  1463. p = put_str(p, " ms=");
  1464. p = put_dec(p, stat_ms);
  1465. p = put_str(p, " max=");
  1466. p = put_dec(p, stat_max);
  1467. p = put_str(p, " px=");
  1468. p = put_dec(p, stat_px);
  1469. p = put_str(p, "\n");
  1470. u_syscall3(SYS_WRITE_FILE, (int)"/share/wmstats", (int)b,
  1471. (int)(p - b));
  1472. }
  1473. void _start(const char *arg)
  1474. {
  1475. struct wm_screen_info si;
  1476. struct wm_status st;
  1477. unsigned int i;
  1478. (void)arg;
  1479. if (u_wm_attach(&si) != 0)
  1480. u_exit(1);
  1481. usurf_init(&screen, si.fb, si.width, si.height, si.pitch, si.bpp);
  1482. usurf_font_attach(&screen, "/ramfs/fonts/sans.ttf", 14);
  1483. fbw = si.width;
  1484. fbh = si.height;
  1485. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1486. wins[i].info.used = 0;
  1487. wins[i].mapped = 0;
  1488. }
  1489. full_redraw = 1;
  1490. for (;;) {
  1491. unsigned int t0, t1;
  1492. if (u_wm_poll(&st) != 0)
  1493. u_exit(1);
  1494. if (st.full_redraw)
  1495. full_redraw = 1;
  1496. usurf_pixels = 0;
  1497. t0 = u_ticks_ms();
  1498. handle_mouse(&st);
  1499. do_update(&st);
  1500. t1 = u_ticks_ms();
  1501. if (usurf_pixels) {
  1502. unsigned int dt = t1 - t0;
  1503. stat_frames++;
  1504. stat_ms += dt;
  1505. stat_px += usurf_pixels;
  1506. if (dt > stat_max)
  1507. stat_max = dt;
  1508. }
  1509. if (t1 - stat_last >= 2000) {
  1510. stat_last = t1;
  1511. if (stat_frames) {
  1512. write_stats();
  1513. stat_frames = stat_ms = stat_px = stat_max = 0;
  1514. }
  1515. }
  1516. u_sleep_ms(10);
  1517. }
  1518. }