ucmd_wm.c 55 KB

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