ucmd_wm.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114
  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 control_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. bar_button(WM_BTN_DISKS_X, WM_BTN_DISKS_Y, WM_BTN_DISKS_W,
  409. WM_BTN_DISKS_H, "Disks");
  410. if (bar_msg_until &&
  411. (int)(u_ticks_ms() - bar_msg_until) < 0)
  412. usurf_text(&screen, (int)(WM_BTN_END_X + 8),
  413. 5, bar_msg, 0xFFD250u, 0, 0, fbw, WM_BAR_H);
  414. /* Rainbow separator (same y as the kernel's old bar). */
  415. for (x = 0; x < fbw; x++) {
  416. unsigned int c = rainbow_rgb((x * (6 * 256)) / (fbw ? fbw : 1));
  417. for (y = WM_BAR_H - 5; y < WM_BAR_H - 3; y++)
  418. usurf_fill(&screen, (int)x, (int)y, 1, 1, c);
  419. }
  420. if (bar_kexec_busy) {
  421. static const char msg[] = "loading new kernel over serial";
  422. unsigned int dots = bar_kexec_total ?
  423. (bar_kexec_progress * 12 / bar_kexec_total) : 0;
  424. bar_clock_x = -1;
  425. bar_clock_w = 0;
  426. bar_clock_hot = 0;
  427. bar_net_x = -1;
  428. bar_net_w = 0;
  429. bar_pwr_x = -1;
  430. bar_pwr_tip_x = -1;
  431. bar_pwr_tip_want = 0;
  432. n = 0;
  433. while (msg[n])
  434. dt[n] = msg[n], n++;
  435. for (i = 0; i < 12; i++)
  436. dt[n + i] = (i < dots) ? '.' : ' ';
  437. n += 12;
  438. dt[n] = '\0';
  439. usurf_text(&screen,
  440. (int)fbw - (int)usurf_text_width(&screen, dt) - 4,
  441. 5, dt, 0xFFC864u, 0, 0, fbw, WM_BAR_H);
  442. } else {
  443. unsigned int clock_chars = 0;
  444. int right = (int)fbw - 4;
  445. int min_x = (int)WM_BTN_END_X + 4;
  446. bar_clock_x = -1;
  447. bar_clock_w = 0;
  448. bar_net_x = -1;
  449. bar_net_w = 0;
  450. bar_pwr_x = -1;
  451. bar_pwr_tip_x = -1;
  452. bar_pwr_tip_want = 0;
  453. /* The AC/battery status icon is the rightmost item. */
  454. if (bar_batt_pct >= 0 || bar_ac_online > 0) {
  455. int px = right - BAR_PWR_W;
  456. if (px >= min_x) {
  457. if (bar_pwr_hot)
  458. usurf_fill(&screen, px - 3, 2,
  459. BAR_PWR_W + 6,
  460. WM_BAR_H - 7, 0x3A3A55u);
  461. if (bar_ac_online > 0)
  462. bar_icon_plug(px, BAR_PWR_Y, 0x55FF55u);
  463. else
  464. bar_icon_battery(px, BAR_PWR_Y,
  465. bar_batt_pct,
  466. batt_color(bar_batt_pct));
  467. bar_pwr_x = px;
  468. right = px - 8;
  469. if (bar_pwr_hot && bar_batt_pct >= 0) {
  470. char lb[8];
  471. unsigned int lw, bw;
  472. int bx;
  473. (void)pct_text(lb, bar_batt_pct);
  474. lw = usurf_text_width(&screen, lb);
  475. bw = lw + 10;
  476. bx = px + BAR_PWR_W - (int)bw;
  477. if (bx < 4)
  478. bx = 4;
  479. if (bx + (int)bw > (int)fbw - 4)
  480. bx = (int)fbw - 4 - (int)bw;
  481. bar_pwr_tip_x = bx;
  482. bar_pwr_tip_y = WM_BAR_H + 2;
  483. bar_pwr_tip_w = bw;
  484. bar_pwr_tip_h = BAR_TIP_H;
  485. bar_pwr_tip_want = 1;
  486. }
  487. }
  488. }
  489. /* The network status sits to the left of the power icon, so it
  490. * survives narrow screens and a full temperature cluster. */
  491. {
  492. int nx = right - BAR_NET_W;
  493. if (nx >= min_x) {
  494. if (bar_net_hot)
  495. usurf_fill(&screen, nx - 3, 2,
  496. BAR_NET_W + 6,
  497. WM_BAR_H - 7, 0x3A3A55u);
  498. bar_draw_net_icon(nx, BAR_NET_Y);
  499. bar_net_x = nx;
  500. bar_net_w = BAR_NET_W;
  501. right = nx - 8;
  502. }
  503. }
  504. if (u_rtc_datetime(dt, sizeof(dt)) == 0) {
  505. int tw = (int)usurf_text_width(&screen, dt);
  506. int tx = right - tw;
  507. n = 0;
  508. while (dt[n] && n < sizeof(dt) - 1)
  509. n++;
  510. bar_clock_x = tx;
  511. bar_clock_w = (unsigned int)tw;
  512. if (bar_clock_hot)
  513. usurf_fill(&screen, tx - 5, 2,
  514. (unsigned int)tw + 10,
  515. WM_BAR_H - 7, 0x3A3A55u);
  516. usurf_text(&screen, tx, 5, dt, COL_BANNER, 0, 0,
  517. fbw, WM_BAR_H);
  518. clock_chars = (unsigned int)tw;
  519. }
  520. if (clock_chars)
  521. right -= (int)clock_chars + 8;
  522. if (bar_temp_c >= 0) {
  523. char tb[12];
  524. int tx;
  525. (void)temp_text(tb, bar_temp_c);
  526. tx = right - (int)usurf_text_width(&screen, tb);
  527. if (tx >= min_x) {
  528. usurf_text(&screen, tx, 5, tb,
  529. temp_color(bar_temp_c), 0, 0,
  530. fbw, WM_BAR_H);
  531. right = tx - 8;
  532. }
  533. }
  534. }
  535. }
  536. /* A popover is a flat, chromeless panel: the canvas fills the window. */
  537. static void draw_popover_clip(unsigned int idx, int cx, int cy,
  538. unsigned int cw, unsigned int ch)
  539. {
  540. struct uwin *u = &wins[idx];
  541. struct wm_window_info *w = &u->info;
  542. unsigned int cwid = w->content_w, chei = w->content_h;
  543. if (u->mapped && u->map.kind == WM_KIND_GFX) {
  544. struct uwm_canvas c;
  545. c.px = (const unsigned short *)u->map.va;
  546. c.w = u->map.w;
  547. c.h = u->map.h;
  548. if (c.w == cwid && c.h == chei) {
  549. usurf_canvas(&screen, &c, w->x, w->y,
  550. cx, cy, cw, ch);
  551. return;
  552. }
  553. }
  554. fill_clip(w->x, w->y, cwid, chei, 0x181C28u, cx, cy, cw, ch);
  555. }
  556. static void draw_window_clip(unsigned int idx, int cx, int cy,
  557. unsigned int cw, unsigned int ch)
  558. {
  559. struct uwin *u = &wins[idx];
  560. struct wm_window_info *w = &u->info;
  561. int x = w->x, y = w->y;
  562. unsigned int pw = w->w, ph = w->h;
  563. int bx = x + (int)pw - 1 - WM_CLOSE_W;
  564. int cony = y + 1 + WM_TITLE_H;
  565. int content_x = x + 1;
  566. unsigned int content_w = w->content_w;
  567. unsigned int content_h = w->content_h;
  568. if (w->popover) {
  569. draw_popover_clip(idx, cx, cy, cw, ch);
  570. return;
  571. }
  572. /* Border, title bar, close button. */
  573. fill_clip(x, y, pw, ph, COL_BORDER, cx, cy, cw, ch);
  574. fill_clip(x + 1, y + 1, pw - 2, WM_TITLE_H,
  575. w->focused ? COL_TITLE_ON : COL_TITLE_OFF, cx, cy, cw, ch);
  576. usurf_text(&screen, x + 4, y + 3, w->title, COL_WHITE,
  577. cx, cy, cw, ch);
  578. fill_clip(bx, y + 1, WM_CLOSE_W, WM_TITLE_H, COL_CLOSE,
  579. cx, cy, cw, ch);
  580. usurf_text(&screen, bx + 4, y + 3, "x", COL_WHITE,
  581. cx, cy, cw, ch);
  582. /* Content: every window presents an RGB565 canvas (the terminal
  583. * renders its cells into one; gfx commands own theirs). A mapped
  584. * canvas covers the whole content area, so it does not need the black
  585. * underlay. */
  586. if (u->mapped && u->map.kind == WM_KIND_GFX) {
  587. struct uwm_canvas c;
  588. c.px = (const unsigned short *)u->map.va;
  589. c.w = u->map.w;
  590. c.h = u->map.h;
  591. if (c.w != content_w || c.h != content_h)
  592. fill_clip(content_x, cony, content_w, content_h,
  593. 0x000000u, cx, cy, cw, ch);
  594. usurf_canvas(&screen, &c, content_x, cony,
  595. cx, cy, cw, ch);
  596. } else {
  597. fill_clip(content_x, cony, content_w, content_h,
  598. 0x000000u, cx, cy, cw, ch);
  599. }
  600. /* Resize grip (bottom-right corner). */
  601. if (w->focused) {
  602. unsigned int i;
  603. for (i = 0; i < 10; i++) {
  604. fill_clip(x + (int)pw - 3 - (int)i,
  605. y + (int)ph - 4, 1, 1, 0xD2D2D2u,
  606. cx, cy, cw, ch);
  607. fill_clip(x + (int)pw - 4,
  608. y + (int)ph - 3 - (int)i, 1, 1, 0xD2D2D2u,
  609. cx, cy, cw, ch);
  610. }
  611. }
  612. }
  613. /* Front-to-back window order: popovers float above everything, then the
  614. * focused window, then the others in descending slot order. Painting and
  615. * hit-testing share it. */
  616. static unsigned int build_order(int *order)
  617. {
  618. unsigned int i, n = 0;
  619. for (i = 0; i < WM_MAX_WINDOWS; i++)
  620. if (wins[i].info.used && wins[i].info.popover)
  621. order[n++] = (int)i;
  622. for (i = 0; i < WM_MAX_WINDOWS; i++)
  623. if (wins[i].info.used && wins[i].info.focused &&
  624. !wins[i].info.popover)
  625. order[n++] = (int)i;
  626. for (i = WM_MAX_WINDOWS; i-- > 0;)
  627. if (wins[i].info.used && !wins[i].info.focused &&
  628. !wins[i].info.popover)
  629. order[n++] = (int)i;
  630. return n;
  631. }
  632. struct orect {
  633. int x, y;
  634. unsigned int w, h;
  635. };
  636. #define OCC_MAX 64
  637. /* r minus cut, split into up to four disjoint rectangles. */
  638. static unsigned int rect_sub(struct orect *out, const struct orect *r,
  639. const struct orect *cut)
  640. {
  641. int rx0 = r->x, ry0 = r->y;
  642. int rx1 = r->x + (int)r->w, ry1 = r->y + (int)r->h;
  643. int cx0 = cut->x, cy0 = cut->y;
  644. int cx1 = cut->x + (int)cut->w, cy1 = cut->y + (int)cut->h;
  645. unsigned int n = 0;
  646. if (cx0 < rx0) cx0 = rx0;
  647. if (cy0 < ry0) cy0 = ry0;
  648. if (cx1 > rx1) cx1 = rx1;
  649. if (cy1 > ry1) cy1 = ry1;
  650. if (cx1 <= cx0 || cy1 <= cy0) {
  651. out[0] = *r;
  652. return 1;
  653. }
  654. if (cy0 > ry0) {
  655. out[n].x = rx0;
  656. out[n].y = ry0;
  657. out[n].w = (unsigned int)(rx1 - rx0);
  658. out[n].h = (unsigned int)(cy0 - ry0);
  659. n++;
  660. }
  661. if (cy1 < ry1) {
  662. out[n].x = rx0;
  663. out[n].y = cy1;
  664. out[n].w = (unsigned int)(rx1 - rx0);
  665. out[n].h = (unsigned int)(ry1 - cy1);
  666. n++;
  667. }
  668. if (cx0 > rx0) {
  669. out[n].x = rx0;
  670. out[n].y = cy0;
  671. out[n].w = (unsigned int)(cx0 - rx0);
  672. out[n].h = (unsigned int)(cy1 - cy0);
  673. n++;
  674. }
  675. if (cx1 < rx1) {
  676. out[n].x = cx1;
  677. out[n].y = cy0;
  678. out[n].w = (unsigned int)(rx1 - cx1);
  679. out[n].h = (unsigned int)(cy1 - cy0);
  680. n++;
  681. }
  682. return n;
  683. }
  684. /* Back-to-front repaint, used only if the visible-region list overflows. */
  685. static void draw_rect_naive(int x, int y, unsigned int w, unsigned int h)
  686. {
  687. unsigned int i;
  688. usurf_fill(&screen, x, y, w, h, COL_BG);
  689. for (i = 0; i < WM_MAX_WINDOWS; i++)
  690. if (wins[i].info.used && !wins[i].info.focused &&
  691. !wins[i].info.popover &&
  692. x < wins[i].info.x + (int)wins[i].info.w &&
  693. x + (int)w > wins[i].info.x &&
  694. y < wins[i].info.y + (int)wins[i].info.h &&
  695. y + (int)h > wins[i].info.y)
  696. draw_window_clip(i, x, y, w, h);
  697. for (i = 0; i < WM_MAX_WINDOWS; i++)
  698. if (wins[i].info.used && wins[i].info.focused &&
  699. !wins[i].info.popover &&
  700. x < wins[i].info.x + (int)wins[i].info.w &&
  701. x + (int)w > wins[i].info.x &&
  702. y < wins[i].info.y + (int)wins[i].info.h &&
  703. y + (int)h > wins[i].info.y)
  704. draw_window_clip(i, x, y, w, h);
  705. for (i = 0; i < WM_MAX_WINDOWS; i++)
  706. if (wins[i].info.used && wins[i].info.popover &&
  707. x < wins[i].info.x + (int)wins[i].info.w &&
  708. x + (int)w > wins[i].info.x &&
  709. y < wins[i].info.y + (int)wins[i].info.h &&
  710. y + (int)h > wins[i].info.y)
  711. draw_window_clip(i, x, y, w, h);
  712. }
  713. static void draw_rect(int x, int y, unsigned int w, unsigned int h)
  714. {
  715. static struct orect cur[OCC_MAX], nxt[OCC_MAX];
  716. struct orect dmg;
  717. int order[WM_MAX_WINDOWS];
  718. unsigned int n, i, j, ncur, nnext;
  719. int overflow = 0;
  720. int x0 = x, y0 = y, x1 = x + (int)w, y1 = y + (int)h;
  721. if (w == 0 || h == 0)
  722. return;
  723. if (x0 < 0) x0 = 0;
  724. if (y0 < 0) y0 = 0;
  725. if (x1 > (int)fbw) x1 = (int)fbw;
  726. if (y1 > (int)fbh) y1 = (int)fbh;
  727. if (x1 <= x0 || y1 <= y0)
  728. return;
  729. dmg.x = x0;
  730. dmg.y = y0;
  731. dmg.w = (unsigned int)(x1 - x0);
  732. dmg.h = (unsigned int)(y1 - y0);
  733. usurf_fill(&screen, dmg.x, dmg.y, dmg.w, dmg.h, COL_BG);
  734. n = build_order(order);
  735. for (i = 0; i < n; i++) {
  736. struct uwin *u = &wins[order[i]];
  737. int wx0 = u->info.x, wy0 = u->info.y;
  738. int wx1 = wx0 + (int)u->info.w, wy1 = wy0 + (int)u->info.h;
  739. if (wx0 < dmg.x) wx0 = dmg.x;
  740. if (wy0 < dmg.y) wy0 = dmg.y;
  741. if (wx1 > x1) wx1 = x1;
  742. if (wy1 > y1) wy1 = y1;
  743. if (wx1 <= wx0 || wy1 <= wy0)
  744. continue;
  745. cur[0].x = wx0;
  746. cur[0].y = wy0;
  747. cur[0].w = (unsigned int)(wx1 - wx0);
  748. cur[0].h = (unsigned int)(wy1 - wy0);
  749. ncur = 1;
  750. for (j = 0; j < i && ncur; j++) {
  751. struct uwin *f = &wins[order[j]];
  752. struct orect fr;
  753. unsigned int k;
  754. fr.x = f->info.x;
  755. fr.y = f->info.y;
  756. fr.w = f->info.w;
  757. fr.h = f->info.h;
  758. nnext = 0;
  759. for (k = 0; k < ncur; k++) {
  760. if (nnext + 4 > OCC_MAX) {
  761. overflow = 1;
  762. break;
  763. }
  764. nnext += rect_sub(&nxt[nnext], &cur[k], &fr);
  765. }
  766. if (overflow)
  767. break;
  768. for (k = 0; k < nnext; k++)
  769. cur[k] = nxt[k];
  770. ncur = nnext;
  771. }
  772. if (overflow)
  773. break;
  774. for (j = 0; j < ncur; j++)
  775. draw_window_clip((unsigned int)order[i], cur[j].x,
  776. cur[j].y, cur[j].w, cur[j].h);
  777. }
  778. if (overflow)
  779. draw_rect_naive(dmg.x, dmg.y, dmg.w, dmg.h);
  780. }
  781. static int window_at(int mx, int my)
  782. {
  783. int order[WM_MAX_WINDOWS];
  784. unsigned int n = build_order(order), i;
  785. for (i = 0; i < n; i++) {
  786. struct wm_window_info *w = &wins[order[i]].info;
  787. if (!w->popover)
  788. continue;
  789. if (mx >= w->x && my >= w->y &&
  790. mx < w->x + (int)w->w &&
  791. my < w->y + (int)w->h)
  792. return order[i];
  793. }
  794. for (i = 0; i < n; i++) {
  795. struct wm_window_info *w = &wins[order[i]].info;
  796. if (w->popover)
  797. continue;
  798. if (mx >= w->x && my >= w->y &&
  799. mx < w->x + (int)w->w &&
  800. my < w->y + (int)w->h)
  801. return order[i];
  802. }
  803. return -1;
  804. }
  805. static void clamp_geom(struct uwin *u, int *x, int *y)
  806. {
  807. /* Match wm_clamp_window(): allow the window to hang off the left,
  808. * right and bottom edges but keep the title bar reachable and below
  809. * the system bar. */
  810. int minx = WM_DRAG_VISIBLE - (int)u->info.w;
  811. int maxx = (int)fbw - WM_DRAG_VISIBLE;
  812. int miny = WM_BAR_H;
  813. int maxy = (int)fbh - WM_TITLE_H;
  814. if (minx > maxx)
  815. minx = maxx = 0;
  816. if (miny > maxy)
  817. miny = maxy = WM_BAR_H;
  818. if (*x < minx)
  819. *x = minx;
  820. else if (*x > maxx)
  821. *x = maxx;
  822. if (*y < miny)
  823. *y = miny;
  824. else if (*y > maxy)
  825. *y = maxy;
  826. }
  827. static void add_rect(struct wm_present_info *p, int x, int y,
  828. unsigned int w, unsigned int h)
  829. {
  830. if (w == 0 || h == 0)
  831. return;
  832. if (p->count < 4) {
  833. p->rect[p->count].x = x;
  834. p->rect[p->count].y = y;
  835. p->rect[p->count].w = w;
  836. p->rect[p->count].h = h;
  837. p->count++;
  838. } else {
  839. /* Four is enough for normal updates; fold the rest into the
  840. * last rect rather than dropping pixels. */
  841. int x0 = p->rect[3].x, y0 = p->rect[3].y;
  842. int x1 = x0 + (int)p->rect[3].w, y1 = y0 + (int)p->rect[3].h;
  843. int nx1 = x + (int)w, ny1 = y + (int)h;
  844. if (x < x0) x0 = x;
  845. if (y < y0) y0 = y;
  846. if (nx1 > x1) x1 = nx1;
  847. if (ny1 > y1) y1 = ny1;
  848. p->rect[3].x = x0;
  849. p->rect[3].y = y0;
  850. p->rect[3].w = (unsigned int)(x1 - x0);
  851. p->rect[3].h = (unsigned int)(y1 - y0);
  852. }
  853. }
  854. /* Redraw the power tooltip above whatever a render pass just painted. */
  855. static void bar_tooltip_overlay(struct wm_present_info *p)
  856. {
  857. if (!bar_pwr_tip_on || !bar_pwr_tip_want)
  858. return;
  859. bar_draw_tooltip();
  860. add_rect(p, bar_pwr_tip_on_x, bar_pwr_tip_on_y,
  861. bar_pwr_tip_on_w, bar_pwr_tip_on_h);
  862. }
  863. /* Center the brightness bubble in the work area, clamped on small screens. */
  864. static void osd_geom(void)
  865. {
  866. int x, y;
  867. osd_w = OSD_W;
  868. osd_h = OSD_H;
  869. x = ((int)fbw - (int)osd_w) / 2;
  870. y = WM_BAR_H + ((int)fbh - WM_BAR_H - (int)osd_h) * 3 / 5;
  871. if (x < 0)
  872. x = 0;
  873. if (x + (int)osd_w > (int)fbw)
  874. x = (int)fbw - (int)osd_w;
  875. if (y < WM_BAR_H)
  876. y = WM_BAR_H;
  877. if (y + (int)osd_h > (int)fbh)
  878. y = (int)fbh - (int)osd_h;
  879. osd_x = x;
  880. osd_y = y;
  881. }
  882. /* A small sun: a filled circle plus four rays, all from 1-pixel fills. */
  883. static void osd_sun(int cx, int cy)
  884. {
  885. static const unsigned char half[11] = { 2, 4, 5, 6, 6, 6, 6, 6, 5, 4, 2 };
  886. int i;
  887. for (i = -5; i <= 5; i++)
  888. usurf_fill(&screen, cx - half[i + 5], cy + i,
  889. half[i + 5] * 2 + 1, 1, 0xFFD250u);
  890. usurf_fill(&screen, cx - 1, cy - 9, 2, 3, 0xFFD250u);
  891. usurf_fill(&screen, cx - 1, cy + 7, 2, 3, 0xFFD250u);
  892. usurf_fill(&screen, cx - 9, cy - 1, 3, 2, 0xFFD250u);
  893. usurf_fill(&screen, cx + 7, cy - 1, 3, 2, 0xFFD250u);
  894. }
  895. static void osd_draw(void)
  896. {
  897. char pct[8];
  898. unsigned int n = 0;
  899. unsigned int bar_x, bar_y, bar_w;
  900. int x, y, tw;
  901. osd_geom();
  902. x = osd_x;
  903. y = osd_y;
  904. usurf_fill(&screen, x, y, osd_w, osd_h, 0x26263Au);
  905. usurf_fill(&screen, x, y, osd_w, 1, 0x70709Au);
  906. usurf_fill(&screen, x, y + (int)osd_h - 1, osd_w, 1, 0x70709Au);
  907. usurf_fill(&screen, x, y + 1, 1, osd_h - 2, 0x70709Au);
  908. usurf_fill(&screen, x + (int)osd_w - 1, y + 1, 1, osd_h - 2,
  909. 0x70709Au);
  910. osd_sun(x + 22, y + (int)osd_h / 2);
  911. if (osd_level >= 100) {
  912. pct[n++] = '1';
  913. pct[n++] = '0';
  914. pct[n++] = '0';
  915. } else {
  916. if (osd_level >= 10)
  917. pct[n++] = (char)('0' + osd_level / 10);
  918. pct[n++] = (char)('0' + osd_level % 10);
  919. }
  920. pct[n++] = '%';
  921. pct[n] = '\0';
  922. usurf_text(&screen, x + 42, y + 9, "Brightness", 0xE8E8F6u,
  923. x, y, osd_w, osd_h);
  924. tw = (int)usurf_text_width(&screen, pct);
  925. usurf_text(&screen, x + (int)osd_w - 14 - tw, y + 9, pct,
  926. 0xFFD250u, x, y, osd_w, osd_h);
  927. bar_x = (unsigned int)(x + 42);
  928. bar_y = (unsigned int)(y + 35);
  929. bar_w = osd_w - 42 - 14;
  930. usurf_fill(&screen, (int)bar_x, (int)bar_y, bar_w, 10, 0x44445Au);
  931. usurf_fill(&screen, (int)bar_x, (int)bar_y,
  932. (bar_w * (unsigned int)osd_level + 50) / 100, 10,
  933. 0xFFD250u);
  934. }
  935. /* Put the power tooltip and the brightness OSD back on top after a render
  936. * pass that may have overwritten them. */
  937. static void wm_overlays(struct wm_present_info *p)
  938. {
  939. bar_tooltip_overlay(p);
  940. if (osd_show) {
  941. osd_draw();
  942. add_rect(p, osd_x, osd_y, osd_w, osd_h);
  943. }
  944. }
  945. /* A focused window is topmost, so the shadow contains exactly its pixels in
  946. * the old rectangle. Move that block and repaint only the old part the new
  947. * rectangle does not cover. When either rectangle is partly off-screen the
  948. * shadow has no valid pixels for the hidden part, so repaint the new rect
  949. * instead of copying desktop background into the window. */
  950. static void move_window_pixels(struct uwin *u, int ox, int oy, int nx, int ny)
  951. {
  952. struct wm_present_info present;
  953. struct orect oldr, newr, pieces[4];
  954. unsigned int np, k;
  955. int w = (int)u->info.w, h = (int)u->info.h;
  956. int old_on = ox >= 0 && oy >= 0 &&
  957. ox + w <= (int)fbw && oy + h <= (int)fbh;
  958. int new_on = nx >= 0 && ny >= 0 &&
  959. nx + w <= (int)fbw && ny + h <= (int)fbh;
  960. if (u_wm_begin() != 0)
  961. return;
  962. if (old_on && new_on)
  963. usurf_copy(&screen, nx, ny, ox, oy, u->info.w, u->info.h);
  964. else
  965. draw_rect(nx, ny, u->info.w, u->info.h);
  966. oldr.x = ox;
  967. oldr.y = oy;
  968. oldr.w = u->info.w;
  969. oldr.h = u->info.h;
  970. newr.x = nx;
  971. newr.y = ny;
  972. newr.w = u->info.w;
  973. newr.h = u->info.h;
  974. np = rect_sub(pieces, &oldr, &newr);
  975. for (k = 0; k < np; k++)
  976. draw_rect(pieces[k].x, pieces[k].y, pieces[k].w, pieces[k].h);
  977. present.count = 0;
  978. present.immediate = 1;
  979. add_rect(&present, nx, ny, u->info.w, u->info.h);
  980. for (k = 0; k < np; k++)
  981. add_rect(&present, pieces[k].x, pieces[k].y,
  982. pieces[k].w, pieces[k].h);
  983. wm_overlays(&present);
  984. u_wm_present(&present);
  985. }
  986. static int iabs(int v)
  987. {
  988. return v < 0 ? -v : v;
  989. }
  990. /* Repaint the old and new rectangles of a geometry change. */
  991. static void redraw_geom(struct uwin *u, int ox, int oy,
  992. unsigned int ow, unsigned int oh)
  993. {
  994. struct wm_present_info present;
  995. unsigned int i;
  996. present.count = 0;
  997. present.immediate = 1;
  998. add_rect(&present, ox, oy, ow, oh);
  999. add_rect(&present, u->info.x, u->info.y, u->info.w, u->info.h);
  1000. if (u_wm_begin() == 0) {
  1001. for (i = 0; i < present.count; i++)
  1002. draw_rect(present.rect[i].x, present.rect[i].y,
  1003. present.rect[i].w, present.rect[i].h);
  1004. wm_overlays(&present);
  1005. u_wm_present(&present);
  1006. }
  1007. }
  1008. /* Resize (when needed), move and remap a window in one step. */
  1009. static int set_window_geometry(struct uwin *u, unsigned int idx,
  1010. int nx, int ny, unsigned int nw, unsigned int nh)
  1011. {
  1012. struct wm_window_info ni;
  1013. int ox = u->info.x, oy = u->info.y;
  1014. unsigned int ow = u->info.w, oh = u->info.h;
  1015. if ((int)nw != (int)ow || (int)nh != (int)oh) {
  1016. if (u_wm_resize(idx, nw - 2, nh - WM_TITLE_H - 2) != 0)
  1017. return -1;
  1018. }
  1019. if (nx != ox || ny != oy)
  1020. u_wm_set_geom(idx, nx, ny);
  1021. u->mapped = 0;
  1022. if (u_wm_info(idx, &ni) == 1)
  1023. u->info = ni;
  1024. if (u_wm_map(idx, &u->map) == 0)
  1025. u->mapped = 1;
  1026. redraw_geom(u, ox, oy, ow, oh);
  1027. return 0;
  1028. }
  1029. static int restore_window(struct uwin *u, unsigned int idx)
  1030. {
  1031. if (u->state == WIN_NORMAL)
  1032. return 0;
  1033. if (set_window_geometry(u, idx, u->restore_x, u->restore_y,
  1034. u->restore_w, u->restore_h) != 0)
  1035. return -1;
  1036. u->state = WIN_NORMAL;
  1037. return 0;
  1038. }
  1039. /* Maximize or snap a window to a half of the work area below the system
  1040. * bar. The restore geometry is recorded only when the window leaves the
  1041. * normal state, so maximize -> snap -> restore returns to the original. */
  1042. static int place_window(struct uwin *u, unsigned int idx, int state)
  1043. {
  1044. unsigned int half = fbw / 2;
  1045. unsigned int h = fbh - WM_BAR_H;
  1046. int x = 0;
  1047. unsigned int w = fbw;
  1048. if (state == u->state)
  1049. return 0;
  1050. if (state == WIN_SNAP_LEFT) {
  1051. w = half;
  1052. } else if (state == WIN_SNAP_RIGHT) {
  1053. x = (int)half;
  1054. w = fbw - half;
  1055. }
  1056. if (u->state == WIN_NORMAL) {
  1057. u->restore_x = u->info.x;
  1058. u->restore_y = u->info.y;
  1059. u->restore_w = u->info.w;
  1060. u->restore_h = u->info.h;
  1061. }
  1062. if (set_window_geometry(u, idx, x, WM_BAR_H, w, h) != 0)
  1063. return -1;
  1064. u->state = state;
  1065. return 0;
  1066. }
  1067. /* Double-click title bar: fill the work area below the system bar, or go back
  1068. * to the geometry the window had before it was maximized. A snapped window
  1069. * maximizes (keeping its pre-snap restore geometry). */
  1070. static int toggle_maximize(struct uwin *u, unsigned int idx)
  1071. {
  1072. if (u->state == WIN_MAXIMIZED)
  1073. return restore_window(u, idx);
  1074. return place_window(u, idx, WIN_MAXIMIZED);
  1075. }
  1076. /* One axis of edge resistance. lo/len is the window's span on that axis
  1077. * before the move, *pos the proposed start. dir < 0: the window's start side
  1078. * (left/top) resists edge e while moving towards lower coordinates; dir > 0:
  1079. * its end side (right/bottom) while moving higher. Within WM_RESIST pixels
  1080. * of overshoot the window stays on the edge; *best keeps the nearest edge. */
  1081. static void resist_edge(int lo, int len, int *pos, int e, int dir, int *best)
  1082. {
  1083. int over;
  1084. if (dir < 0 && *pos < lo && lo >= e && *pos < e) {
  1085. over = e - *pos;
  1086. if (over <= WM_RESIST && over < *best) {
  1087. *best = over;
  1088. *pos = e;
  1089. }
  1090. } else if (dir > 0 && *pos > lo && lo + len <= e && *pos + len > e) {
  1091. over = *pos + len - e;
  1092. if (over <= WM_RESIST && over < *best) {
  1093. *best = over;
  1094. *pos = e - len;
  1095. }
  1096. }
  1097. }
  1098. /* Span overlap test: edges of another window only resist when the two
  1099. * windows share some extent on the other axis (they would actually touch). */
  1100. static int spans_overlap(int a, int alen, int b, int blen)
  1101. {
  1102. return a < b + blen && b < a + alen;
  1103. }
  1104. /* Apply edge resistance to a drag from (u->info.x, u->info.y) to (*nx, *ny):
  1105. * screen edges and the facing edges of every other window. The proposed
  1106. * position is still pure pointer arithmetic, so once the overshoot passes
  1107. * WM_RESIST the window jumps to follow the pointer again. */
  1108. static void resist_geom(struct uwin *u, int idx, int *nx, int *ny)
  1109. {
  1110. int ox = u->info.x, oy = u->info.y;
  1111. int w = (int)u->info.w, h = (int)u->info.h;
  1112. int px = *nx, py = *ny;
  1113. int bestx = WM_RESIST + 1, besty = WM_RESIST + 1;
  1114. int i;
  1115. resist_edge(ox, w, &px, 0, -1, &bestx);
  1116. resist_edge(ox, w, &px, (int)fbw, 1, &bestx);
  1117. resist_edge(oy, h, &py, (int)fbh, 1, &besty);
  1118. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1119. struct wm_window_info *o = &wins[i].info;
  1120. int t;
  1121. if (i == idx || !o->used)
  1122. continue;
  1123. if (spans_overlap(oy, h, o->y, (int)o->h)) {
  1124. /* our left side against its right, our right
  1125. * side against its left */
  1126. t = *nx;
  1127. resist_edge(ox, w, &t, o->x + (int)o->w, -1, &bestx);
  1128. resist_edge(ox, w, &t, o->x, 1, &bestx);
  1129. if (t != *nx)
  1130. px = t;
  1131. }
  1132. if (spans_overlap(ox, w, o->x, (int)o->w)) {
  1133. t = *ny;
  1134. resist_edge(oy, h, &t, o->y + (int)o->h, -1, &besty);
  1135. resist_edge(oy, h, &t, o->y, 1, &besty);
  1136. if (t != *ny)
  1137. py = t;
  1138. }
  1139. }
  1140. *nx = px;
  1141. *ny = py;
  1142. }
  1143. /* The top-bar date/time text is also a button: it opens the Date & Time
  1144. * editor. The rectangle is recorded by draw_bar(). */
  1145. static int bar_clock_hit(unsigned int mx, unsigned int my)
  1146. {
  1147. return bar_clock_w && bar_clock_x >= 0 && my < WM_BAR_H &&
  1148. (int)mx >= bar_clock_x - 5 &&
  1149. (int)mx < bar_clock_x + (int)bar_clock_w + 5;
  1150. }
  1151. static int bar_net_hit(unsigned int mx, unsigned int my)
  1152. {
  1153. return bar_net_w && bar_net_x >= 0 && my < WM_BAR_H &&
  1154. (int)mx >= bar_net_x - 3 &&
  1155. (int)mx < bar_net_x + (int)bar_net_w + 3;
  1156. }
  1157. /* The AC/battery icon opens the control panel on a click; hovering it also
  1158. * reveals the percentage tooltip below it. The tooltip area keeps the hover
  1159. * alive but is not a click target. */
  1160. static int bar_pwr_hit(unsigned int mx, unsigned int my)
  1161. {
  1162. if (my >= WM_BAR_H)
  1163. return bar_pwr_tip_on &&
  1164. (int)mx >= bar_pwr_tip_on_x &&
  1165. (int)mx < bar_pwr_tip_on_x + (int)bar_pwr_tip_on_w &&
  1166. (int)my >= bar_pwr_tip_on_y &&
  1167. (int)my < bar_pwr_tip_on_y + (int)bar_pwr_tip_on_h;
  1168. return bar_pwr_x >= 0 &&
  1169. (int)mx >= bar_pwr_x - 3 &&
  1170. (int)mx < bar_pwr_x + BAR_PWR_W + 3;
  1171. }
  1172. /* The whole right status area is one button: the clock, temperature, network
  1173. * and power items plus the gaps between them. The launcher buttons end at
  1174. * WM_BTN_END_X, so this never overlaps them. */
  1175. static int bar_status_hit(unsigned int mx, unsigned int my)
  1176. {
  1177. return my < WM_BAR_H && (int)mx >= WM_BTN_END_X + 4;
  1178. }
  1179. static void do_update(struct wm_status *st)
  1180. {
  1181. struct wm_present_info present;
  1182. int need = 0;
  1183. int bar_dirty = 0;
  1184. unsigned int i;
  1185. char dt[20];
  1186. static char last_dt[20];
  1187. int kexec_changed = (st->kexec_busy != bar_kexec_busy) ||
  1188. (st->kexec_busy && st->kexec_progress != bar_kexec_progress);
  1189. unsigned int msg_now = u_ticks_ms();
  1190. int osd_hide = 0, osd_repaint = 0;
  1191. /* The kernel bumps backlight_seq on every brightness key/command.
  1192. * Pop the OSD on a change and keep it up for OSD_MS after the last
  1193. * one. The first poll only records the current sequence, so a WM
  1194. * restart does not flash a stale bubble. */
  1195. if (st->backlight_present) {
  1196. if (!osd_seen_valid) {
  1197. osd_seen_valid = 1;
  1198. osd_seen_seq = st->backlight_seq;
  1199. } else if (st->backlight_seq != osd_seen_seq) {
  1200. osd_seen_seq = st->backlight_seq;
  1201. osd_level = (int)st->backlight_level;
  1202. osd_until = msg_now + OSD_MS;
  1203. osd_show = 1;
  1204. }
  1205. } else if (osd_show) {
  1206. osd_show = 0;
  1207. osd_hide = 1;
  1208. }
  1209. if (osd_show && (int)(msg_now - osd_until) >= 0) {
  1210. osd_show = 0;
  1211. osd_hide = 1;
  1212. }
  1213. if (osd_hide && osd_visible)
  1214. osd_repaint = 1;
  1215. /* Spawn the first terminal once the kernel says the storage scan is
  1216. * done, so a shell command can never race the mount table. */
  1217. if (st->boot_ready && !boot_term_spawned) {
  1218. boot_term_spawned = 1;
  1219. if (!free_window_slot()) {
  1220. bar_notice("window limit");
  1221. full_redraw = 1;
  1222. } else if (u_wm_create_term() < 0) {
  1223. bar_notice("cannot start term");
  1224. full_redraw = 1;
  1225. } else {
  1226. full_redraw = 1;
  1227. }
  1228. }
  1229. if (bar_msg_dirty ||
  1230. (bar_msg_until && (int)(msg_now - bar_msg_until) >= 0)) {
  1231. bar_dirty = 1;
  1232. bar_msg_dirty = 0;
  1233. if (bar_msg_until && (int)(msg_now - bar_msg_until) >= 0)
  1234. bar_msg_until = 0;
  1235. }
  1236. bar_kexec_busy = st->kexec_busy;
  1237. bar_kexec_progress = st->kexec_progress;
  1238. bar_kexec_total = st->kexec_total;
  1239. if (kexec_changed)
  1240. bar_dirty = 1;
  1241. {
  1242. int hot = bar_clock_hit(st->mouse.x, st->mouse.y);
  1243. if (hot != bar_clock_hot) {
  1244. bar_clock_hot = hot;
  1245. bar_dirty = 1;
  1246. }
  1247. }
  1248. {
  1249. int hot = bar_net_hit(st->mouse.x, st->mouse.y);
  1250. if (hot != bar_net_hot) {
  1251. bar_net_hot = hot;
  1252. bar_dirty = 1;
  1253. }
  1254. }
  1255. {
  1256. int hot = bar_pwr_hit(st->mouse.x, st->mouse.y);
  1257. if (hot != bar_pwr_hot) {
  1258. bar_pwr_hot = hot;
  1259. bar_dirty = 1;
  1260. }
  1261. }
  1262. /* Refresh cached window state and mappings before opening a frame:
  1263. * MAP may unmap/remap content. */
  1264. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1265. struct wm_window_info info;
  1266. int r = u_wm_info(i, &info);
  1267. if (r < 0) {
  1268. u_exit(1);
  1269. }
  1270. if (r == 0 || !info.used) {
  1271. if (wins[i].info.used)
  1272. full_redraw = 1;
  1273. wins[i].info.used = 0;
  1274. wins[i].mapped = 0;
  1275. continue;
  1276. }
  1277. {
  1278. int pid_changed = wins[i].info.used &&
  1279. info.pid != wins[i].info.pid;
  1280. if (!wins[i].info.used ||
  1281. info.content_gen != wins[i].info.content_gen ||
  1282. pid_changed)
  1283. full_redraw = 1;
  1284. wins[i].info = info;
  1285. if (!wins[i].mapped || pid_changed ||
  1286. wins[i].map.generation != info.content_gen) {
  1287. struct wm_map_info map;
  1288. if (u_wm_map(i, &map) == 0) {
  1289. wins[i].map = map;
  1290. wins[i].mapped = 1;
  1291. } else {
  1292. wins[i].mapped = 0;
  1293. }
  1294. }
  1295. }
  1296. }
  1297. /* Popovers are WM-anchored overlays, flush against the bottom of the
  1298. * bar and the right screen edge. The WM owns the geometry, so the app
  1299. * does not need a placement ABI. */
  1300. {
  1301. int control_found = 0;
  1302. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1303. struct wm_window_info ni;
  1304. int nx, ny;
  1305. if (!wins[i].info.used || !wins[i].info.popover)
  1306. continue;
  1307. if (control_pid > 0 &&
  1308. wins[i].info.pid == (unsigned int)control_pid)
  1309. control_found = 1;
  1310. nx = (int)fbw - (int)wins[i].info.w;
  1311. ny = WM_BAR_H;
  1312. if (nx != wins[i].info.x || ny != wins[i].info.y) {
  1313. if (u_wm_set_geom(i, nx, ny) == 0 &&
  1314. u_wm_info(i, &ni) == 1) {
  1315. wins[i].info = ni;
  1316. full_redraw = 1;
  1317. }
  1318. }
  1319. }
  1320. if (!control_found && control_pid > 0 &&
  1321. !u_alive((unsigned int)control_pid))
  1322. control_pid = 0;
  1323. }
  1324. /* The RTC is slow (CMOS polling) and the temperature/battery state only
  1325. * changes once a second; query them at 1 Hz instead of every 10 ms. */
  1326. {
  1327. static unsigned int last_rtc_ms;
  1328. unsigned int now_ms = u_ticks_ms();
  1329. if (full_redraw || now_ms - last_rtc_ms >= 1000) {
  1330. last_rtc_ms = now_ms;
  1331. if (u_rtc_datetime(dt, sizeof(dt)) == 0) {
  1332. for (i = 0; i < sizeof(dt); i++)
  1333. if (dt[i] != last_dt[i]) {
  1334. for (i = 0; i < sizeof(dt); i++)
  1335. last_dt[i] = dt[i];
  1336. bar_dirty = 1;
  1337. break;
  1338. }
  1339. }
  1340. {
  1341. struct sys_cputemp ct;
  1342. if (u_cpu_temp(&ct) == 0 &&
  1343. ct.temp_c != bar_temp_c) {
  1344. bar_temp_c = ct.temp_c;
  1345. bar_dirty = 1;
  1346. }
  1347. }
  1348. {
  1349. struct sys_powerinfo pi;
  1350. int pct = -1, charging = 0, ac = -1;
  1351. unsigned int bi;
  1352. if (u_power_info(&pi, 0) == 0) {
  1353. /* A laptop EC (ThinkPad or Chrome
  1354. * EC) marks a machine with AC/battery;
  1355. * desktops show neither. */
  1356. if (pi.present) {
  1357. ac = pi.ac_online;
  1358. for (bi = 0; bi < SYS_POWER_BATT_MAX;
  1359. bi++) {
  1360. struct sys_battery *b =
  1361. &pi.battery[bi];
  1362. if (!b->present ||
  1363. !b->percent_valid)
  1364. continue;
  1365. pct = (int)b->percent;
  1366. charging = (int)b->charging;
  1367. break;
  1368. }
  1369. }
  1370. if (pct != bar_batt_pct ||
  1371. charging != bar_batt_charging ||
  1372. ac != bar_ac_online) {
  1373. bar_batt_pct = pct;
  1374. bar_batt_charging = charging;
  1375. bar_ac_online = ac;
  1376. bar_dirty = 1;
  1377. }
  1378. }
  1379. }
  1380. if (poll_net_status())
  1381. bar_dirty = 1;
  1382. }
  1383. }
  1384. if (full_redraw) {
  1385. if (u_wm_begin() != 0) {
  1386. full_redraw = 1;
  1387. return;
  1388. }
  1389. full_redraw = 0;
  1390. need = 1;
  1391. {
  1392. draw_rect(0, 0, fbw, fbh);
  1393. draw_bar();
  1394. bar_tooltip_commit();
  1395. if (osd_show)
  1396. osd_draw();
  1397. osd_visible = osd_show;
  1398. present.count = 0;
  1399. add_rect(&present, 0, 0, fbw, fbh);
  1400. present.immediate = 0;
  1401. u_wm_present(&present);
  1402. /* Ack every window that was clean at the time of the
  1403. * redraw. If new damage arrived during it, its seq
  1404. * differs and the kernel leaves it dirty. */
  1405. for (i = 0; i < WM_MAX_WINDOWS; i++)
  1406. if (wins[i].info.used && wins[i].mapped)
  1407. u_wm_ack(i, wins[i].info.dirty_seq);
  1408. }
  1409. return;
  1410. }
  1411. /* Partial cell/canvas updates and the bar clock. */
  1412. present.count = 0;
  1413. present.immediate = 0;
  1414. if (osd_repaint) {
  1415. add_rect(&present, osd_x, osd_y, osd_w, osd_h);
  1416. need = 1;
  1417. }
  1418. if (osd_show)
  1419. need = 1;
  1420. if (bar_dirty) {
  1421. need = 1;
  1422. add_rect(&present, 0, 0, fbw, WM_BAR_H);
  1423. }
  1424. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1425. struct uwin *u = &wins[i];
  1426. struct wm_window_info *w = &u->info;
  1427. int rx, ry;
  1428. unsigned int rw, rh;
  1429. if (!w->used || !w->dirty)
  1430. continue;
  1431. if (!w->dmg_w || !w->dmg_h)
  1432. continue;
  1433. rx = w->x + (w->popover ? 0 : 1) + (int)w->dmg_x;
  1434. ry = w->y + (w->popover ? 0 : 1 + WM_TITLE_H) +
  1435. (int)w->dmg_y;
  1436. rw = w->dmg_w;
  1437. rh = w->dmg_h;
  1438. add_rect(&present, rx, ry, rw, rh);
  1439. u->ack_seq = u->mapped ? w->dirty_seq : 0;
  1440. need = 1;
  1441. }
  1442. /* Repaint the previous tooltip area so hiding or moving it restores
  1443. * the window/desktop underneath; the new one is drawn after the
  1444. * windows below. */
  1445. if (bar_pwr_tip_on) {
  1446. need = 1;
  1447. add_rect(&present, bar_pwr_tip_on_x, bar_pwr_tip_on_y,
  1448. bar_pwr_tip_on_w, bar_pwr_tip_on_h);
  1449. }
  1450. if (!need) {
  1451. /* Drag movement is handled below; a stationary mouse can be
  1452. * left to the kernel's cursor overlay. */
  1453. return;
  1454. }
  1455. if (u_wm_begin() != 0)
  1456. return;
  1457. for (i = 0; i < present.count; i++)
  1458. draw_rect(present.rect[i].x, present.rect[i].y,
  1459. present.rect[i].w, present.rect[i].h);
  1460. if (bar_dirty)
  1461. draw_bar();
  1462. if (bar_pwr_tip_want) {
  1463. bar_tooltip_commit();
  1464. add_rect(&present, bar_pwr_tip_on_x, bar_pwr_tip_on_y,
  1465. bar_pwr_tip_on_w, bar_pwr_tip_on_h);
  1466. } else {
  1467. bar_pwr_tip_on = 0;
  1468. }
  1469. if (osd_show) {
  1470. osd_draw();
  1471. add_rect(&present, osd_x, osd_y, osd_w, osd_h);
  1472. }
  1473. osd_visible = osd_show;
  1474. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1475. struct uwin *u = &wins[i];
  1476. if (!u->info.used || !u->info.dirty || !u->mapped ||
  1477. !u->ack_seq)
  1478. continue;
  1479. if (u->ack_seq == u->info.dirty_seq)
  1480. u_wm_ack(i, u->info.dirty_seq);
  1481. }
  1482. u_wm_present(&present);
  1483. }
  1484. static int spawn_app(const char *path)
  1485. {
  1486. int pid = u_spawn(path, "", 0, "/");
  1487. if (pid > 0)
  1488. full_redraw = 1;
  1489. return pid;
  1490. }
  1491. static void spawn_app_checked(const char *path)
  1492. {
  1493. if (!free_window_slot()) {
  1494. bar_notice("window limit");
  1495. full_redraw = 1;
  1496. return;
  1497. }
  1498. if (spawn_app(path) < 0) {
  1499. bar_notice("spawn failed");
  1500. full_redraw = 1;
  1501. }
  1502. }
  1503. /* Any status target opens one control panel; a second click closes it. */
  1504. static void open_control_panel(void)
  1505. {
  1506. unsigned int i;
  1507. if (control_pid > 0 && u_alive((unsigned int)control_pid)) {
  1508. for (i = 0; i < WM_MAX_WINDOWS; i++)
  1509. if (wins[i].info.used &&
  1510. wins[i].info.pid == (unsigned int)control_pid) {
  1511. if (u_wm_close(i) == 0) {
  1512. control_pid = 0;
  1513. full_redraw = 1;
  1514. } else {
  1515. u_wm_focus(i);
  1516. full_redraw = 1;
  1517. }
  1518. return;
  1519. }
  1520. /* The process is alive but has not created its window yet. */
  1521. return;
  1522. }
  1523. if (!free_window_slot()) {
  1524. bar_notice("window limit");
  1525. full_redraw = 1;
  1526. return;
  1527. }
  1528. control_pid = u_spawn("/ramfs/bin/netmenu", "", 0, "/");
  1529. if (control_pid < 0) {
  1530. control_pid = 0;
  1531. bar_notice("spawn failed");
  1532. full_redraw = 1;
  1533. } else {
  1534. full_redraw = 1;
  1535. }
  1536. }
  1537. /* Index of a popover containing the point, or -1. */
  1538. static int popover_under(unsigned int mx, unsigned int my)
  1539. {
  1540. unsigned int i;
  1541. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1542. struct wm_window_info *w = &wins[i].info;
  1543. if (w->used && w->popover &&
  1544. mx >= (unsigned int)w->x && my >= (unsigned int)w->y &&
  1545. mx < (unsigned int)(w->x + (int)w->w) &&
  1546. my < (unsigned int)(w->y + (int)w->h))
  1547. return (int)i;
  1548. }
  1549. return -1;
  1550. }
  1551. /* A press anywhere outside an open popover dismisses it; the click then
  1552. * continues normally so the target under the pointer still reacts. */
  1553. static void dismiss_popover_outside(unsigned int mx, unsigned int my)
  1554. {
  1555. unsigned int i;
  1556. if (popover_under(mx, my) >= 0)
  1557. return;
  1558. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1559. if (!wins[i].info.used || !wins[i].info.popover)
  1560. continue;
  1561. if (wins[i].info.pid == (unsigned int)control_pid)
  1562. control_pid = 0;
  1563. if (u_wm_close(i) == 0) {
  1564. wins[i].info.used = 0;
  1565. wins[i].mapped = 0;
  1566. full_redraw = 1;
  1567. }
  1568. break;
  1569. }
  1570. }
  1571. /* Top-bar launcher under (mx, my)? Same rectangles as the click chain in
  1572. * handle_mouse(); every launcher shares WM_BTN_Y/WM_BTN_H. */
  1573. static int bar_launcher_hit(unsigned int mx, unsigned int my)
  1574. {
  1575. static const int x0[] = {
  1576. WM_BTN_X, WM_BTN_CLOCK_X, WM_BTN_COLORS_X, WM_BTN_FILES_X,
  1577. WM_BTN_MINES_X, WM_BTN_WEB_X, WM_BTN_CARDS_X, WM_BTN_GALLERY_X,
  1578. WM_BTN_CHAT_X, WM_BTN_DISKS_X
  1579. };
  1580. static const int w[] = {
  1581. WM_BTN_W, WM_BTN_CLOCK_W, WM_BTN_COLORS_W, WM_BTN_FILES_W,
  1582. WM_BTN_MINES_W, WM_BTN_WEB_W, WM_BTN_CARDS_W, WM_BTN_GALLERY_W,
  1583. WM_BTN_CHAT_W, WM_BTN_DISKS_W
  1584. };
  1585. unsigned int i;
  1586. if ((int)my < WM_BTN_Y || (int)my >= WM_BTN_Y + WM_BTN_H)
  1587. return 0;
  1588. for (i = 0; i < sizeof(x0) / sizeof(x0[0]); i++)
  1589. if ((int)mx >= x0[i] && (int)mx < x0[i] + w[i])
  1590. return 1;
  1591. return 0;
  1592. }
  1593. /* The WM's cursor override (SYS_WM_SET_CURSOR): resize arrows on the grip
  1594. * and while resizing, the move cross while a title-bar drag is under way,
  1595. * a hand over the launchers and the status area. Everywhere else the
  1596. * kernel picks the window's own content cursor (or the arrow). */
  1597. static void update_cursor(unsigned int mx, unsigned int my)
  1598. {
  1599. static int current = CURSOR_NO_OVERRIDE;
  1600. int want = CURSOR_NO_OVERRIDE;
  1601. if (resize_id >= 0) {
  1602. want = CURSOR_NWSE_RESIZE;
  1603. } else if (drag_id >= 0) {
  1604. if (drag_moved)
  1605. want = CURSOR_MOVE;
  1606. } else if (my < WM_BAR_H) {
  1607. if (bar_launcher_hit(mx, my) || bar_status_hit(mx, my))
  1608. want = CURSOR_POINTER;
  1609. } else {
  1610. int idx = window_at((int)mx, (int)my);
  1611. if (idx >= 0 && !wins[idx].info.popover &&
  1612. (int)mx >= wins[idx].info.x + (int)wins[idx].info.w - 16 &&
  1613. (int)my >= wins[idx].info.y + (int)wins[idx].info.h - 16)
  1614. want = CURSOR_NWSE_RESIZE;
  1615. }
  1616. if (want != current && u_wm_set_cursor(want) == 0)
  1617. current = want;
  1618. }
  1619. static void handle_mouse(struct wm_status *st)
  1620. {
  1621. unsigned int buttons = st->mouse.buttons;
  1622. unsigned int mx = st->mouse.x, my = st->mouse.y;
  1623. int down = (buttons & 1) != 0;
  1624. int was = (prev_buttons & 1) != 0;
  1625. if (down && !was) {
  1626. int prev_title_id = last_title_id;
  1627. unsigned int now = u_ticks_ms();
  1628. if (!bar_status_hit(mx, my))
  1629. dismiss_popover_outside(mx, my);
  1630. last_title_id = -1;
  1631. if (bar_status_hit(mx, my)) {
  1632. open_control_panel();
  1633. } else if (mx >= WM_BTN_X && my >= WM_BTN_Y &&
  1634. mx < WM_BTN_X + WM_BTN_W && my < WM_BTN_Y + WM_BTN_H) {
  1635. if (!free_window_slot()) {
  1636. bar_notice("window limit");
  1637. full_redraw = 1;
  1638. } else if (u_wm_create_term() < 0) {
  1639. bar_notice("cannot start term");
  1640. full_redraw = 1;
  1641. } else {
  1642. full_redraw = 1;
  1643. }
  1644. } else if (mx >= WM_BTN_CLOCK_X && my >= WM_BTN_CLOCK_Y &&
  1645. mx < WM_BTN_CLOCK_X + WM_BTN_CLOCK_W &&
  1646. my < WM_BTN_CLOCK_Y + WM_BTN_CLOCK_H) {
  1647. spawn_app_checked("/ramfs/bin/clock");
  1648. } else if (mx >= WM_BTN_COLORS_X && my >= WM_BTN_COLORS_Y &&
  1649. mx < WM_BTN_COLORS_X + WM_BTN_COLORS_W &&
  1650. my < WM_BTN_COLORS_Y + WM_BTN_COLORS_H) {
  1651. spawn_app_checked("/ramfs/bin/colors");
  1652. } else if (mx >= WM_BTN_FILES_X && my >= WM_BTN_FILES_Y &&
  1653. mx < WM_BTN_FILES_X + WM_BTN_FILES_W &&
  1654. my < WM_BTN_FILES_Y + WM_BTN_FILES_H) {
  1655. spawn_app_checked("/ramfs/bin/files");
  1656. } else if (mx >= WM_BTN_MINES_X && my >= WM_BTN_MINES_Y &&
  1657. mx < WM_BTN_MINES_X + WM_BTN_MINES_W &&
  1658. my < WM_BTN_MINES_Y + WM_BTN_MINES_H) {
  1659. spawn_app_checked("/ramfs/bin/mines");
  1660. } else if (mx >= WM_BTN_WEB_X && my >= WM_BTN_WEB_Y &&
  1661. mx < WM_BTN_WEB_X + WM_BTN_WEB_W &&
  1662. my < WM_BTN_WEB_Y + WM_BTN_WEB_H) {
  1663. spawn_app_checked("/ramfs/bin/browser");
  1664. } else if (mx >= WM_BTN_CARDS_X && my >= WM_BTN_CARDS_Y &&
  1665. mx < WM_BTN_CARDS_X + WM_BTN_CARDS_W &&
  1666. my < WM_BTN_CARDS_Y + WM_BTN_CARDS_H) {
  1667. spawn_app_checked("/ramfs/bin/solitaire");
  1668. } else if (mx >= WM_BTN_GALLERY_X && my >= WM_BTN_GALLERY_Y &&
  1669. mx < WM_BTN_GALLERY_X + WM_BTN_GALLERY_W &&
  1670. my < WM_BTN_GALLERY_Y + WM_BTN_GALLERY_H) {
  1671. spawn_app_checked("/ramfs/bin/gallery");
  1672. } else if (mx >= WM_BTN_CHAT_X && my >= WM_BTN_CHAT_Y &&
  1673. mx < WM_BTN_CHAT_X + WM_BTN_CHAT_W &&
  1674. my < WM_BTN_CHAT_Y + WM_BTN_CHAT_H) {
  1675. spawn_app_checked("/ramfs/bin/chat");
  1676. } else if (mx >= WM_BTN_DISKS_X && my >= WM_BTN_DISKS_Y &&
  1677. mx < WM_BTN_DISKS_X + WM_BTN_DISKS_W &&
  1678. my < WM_BTN_DISKS_Y + WM_BTN_DISKS_H) {
  1679. spawn_app_checked("/ramfs/bin/disks");
  1680. } else {
  1681. int idx = window_at((int)mx, (int)my);
  1682. if (idx >= 0) {
  1683. struct uwin *u = &wins[idx];
  1684. int pmx = (int)mx, pmy = (int)my;
  1685. int rx = u->info.x + (int)u->info.w - 1 -
  1686. WM_CLOSE_W;
  1687. int ry = u->info.y + 1;
  1688. u_wm_focus((unsigned int)idx);
  1689. if (u->info.popover) {
  1690. /* A popover has no title bar, close
  1691. * button or resize grip; the app owns
  1692. * its content. */
  1693. } else if (pmx >= rx &&
  1694. pmx < u->info.x + (int)u->info.w &&
  1695. pmy >= ry && pmy < ry + WM_TITLE_H) {
  1696. u_wm_close((unsigned int)idx);
  1697. full_redraw = 1;
  1698. } else if (pmx >= u->info.x + (int)u->info.w - 16 &&
  1699. pmy >= u->info.y + (int)u->info.h - 16) {
  1700. resize_id = idx;
  1701. resize_mx = mx;
  1702. resize_my = my;
  1703. resize_ow = u->info.w;
  1704. resize_oh = u->info.h;
  1705. } else if (pmy < u->info.y + WM_TITLE_H) {
  1706. if (prev_title_id == idx &&
  1707. now - last_title_ms <= WM_DBLCLICK_MS &&
  1708. iabs(pmx - last_title_x) <= WM_DBLCLICK_SLOP &&
  1709. iabs(pmy - last_title_y) <= WM_DBLCLICK_SLOP) {
  1710. last_title_ms = 0;
  1711. if (toggle_maximize(u,
  1712. (unsigned int)idx) != 0)
  1713. bar_notice("resize failed");
  1714. } else {
  1715. last_title_id = idx;
  1716. last_title_ms = now;
  1717. last_title_x = pmx;
  1718. last_title_y = pmy;
  1719. drag_id = idx;
  1720. drag_mx = pmx;
  1721. drag_my = pmy;
  1722. drag_dx = pmx - u->info.x;
  1723. drag_dy = pmy - u->info.y;
  1724. drag_moved = 0;
  1725. }
  1726. }
  1727. }
  1728. }
  1729. } else if (down && was && resize_id >= 0) {
  1730. struct uwin *u = &wins[resize_id];
  1731. if (u->info.used) {
  1732. int nw = (int)resize_ow + (int)mx - (int)resize_mx;
  1733. int nh = (int)resize_oh + (int)my - (int)resize_my;
  1734. int maxw = (int)fbw;
  1735. int maxh = (int)fbh - WM_BAR_H;
  1736. if (nw < 200) nw = 200;
  1737. if (nh < 140) nh = 140;
  1738. if (nw > maxw) nw = maxw;
  1739. if (nh > maxh) nh = maxh;
  1740. /* Dragging the grip into the screen corner means "as
  1741. * large as the work area", even though the grip starts
  1742. * 16 px inside the window edge. */
  1743. if ((int)mx >= (int)fbw - 1)
  1744. nw = maxw;
  1745. if ((int)my >= (int)fbh - 1)
  1746. nh = maxh;
  1747. if (nw != (int)u->info.w || nh != (int)u->info.h) {
  1748. if (set_window_geometry(u,
  1749. (unsigned int)resize_id, u->info.x,
  1750. u->info.y, (unsigned int)nw,
  1751. (unsigned int)nh) == 0)
  1752. u->state = WIN_NORMAL;
  1753. }
  1754. } else {
  1755. resize_id = -1;
  1756. }
  1757. } else if (down && was && drag_id >= 0) {
  1758. struct uwin *u = &wins[drag_id];
  1759. if (u->info.used) {
  1760. int ox, oy, nx, ny;
  1761. if (iabs((int)mx - drag_mx) >= WM_DRAG_START ||
  1762. iabs((int)my - drag_my) >= WM_DRAG_START)
  1763. drag_moved = 1;
  1764. /* A maximized or snapped window is restored as soon as
  1765. * the drag starts. Keep the grab point at the same
  1766. * fraction of the title bar, so the restored (narrower)
  1767. * window stays under the pointer. A click that does
  1768. * not move must leave it in place. */
  1769. if (u->state != WIN_NORMAL && drag_moved) {
  1770. unsigned int ow = u->info.w;
  1771. int gx = drag_mx - u->info.x;
  1772. int gy = drag_my - u->info.y;
  1773. if (restore_window(u,
  1774. (unsigned int)drag_id) == 0) {
  1775. if (ow)
  1776. gx = gx * (int)u->info.w /
  1777. (int)ow;
  1778. drag_dx = gx;
  1779. drag_dy = gy;
  1780. }
  1781. }
  1782. if (u->state == WIN_NORMAL) {
  1783. ox = u->info.x;
  1784. oy = u->info.y;
  1785. nx = (int)mx - drag_dx;
  1786. ny = (int)my - drag_dy;
  1787. resist_geom(u, drag_id, &nx, &ny);
  1788. clamp_geom(u, &nx, &ny);
  1789. if (nx != ox || ny != oy) {
  1790. struct wm_present_info present;
  1791. unsigned int i;
  1792. u_wm_set_geom((unsigned int)drag_id,
  1793. nx, ny);
  1794. u->info.x = nx;
  1795. u->info.y = ny;
  1796. if (u->info.focused) {
  1797. move_window_pixels(u, ox, oy,
  1798. nx, ny);
  1799. } else {
  1800. present.count = 0;
  1801. present.immediate = 1;
  1802. add_rect(&present, ox, oy,
  1803. u->info.w, u->info.h);
  1804. add_rect(&present, nx, ny,
  1805. u->info.w, u->info.h);
  1806. if (u_wm_begin() == 0) {
  1807. for (i = 0;
  1808. i < present.count; i++)
  1809. draw_rect(
  1810. present.rect[i].x,
  1811. present.rect[i].y,
  1812. present.rect[i].w,
  1813. present.rect[i].h);
  1814. wm_overlays(&present);
  1815. u_wm_present(&present);
  1816. }
  1817. }
  1818. }
  1819. }
  1820. } else {
  1821. drag_id = -1;
  1822. }
  1823. } else if (!down && was) {
  1824. /* Dropping a window with the pointer on the left/right screen
  1825. * edge snaps it to that half; on the top edge it maximizes. */
  1826. if (drag_id >= 0 && drag_moved && wins[drag_id].info.used &&
  1827. wins[drag_id].state == WIN_NORMAL) {
  1828. struct uwin *u = &wins[drag_id];
  1829. int st = WIN_NORMAL;
  1830. if ((int)mx < WM_SNAP_EDGE)
  1831. st = WIN_SNAP_LEFT;
  1832. else if ((int)mx >= (int)fbw - WM_SNAP_EDGE)
  1833. st = WIN_SNAP_RIGHT;
  1834. else if ((int)my < WM_SNAP_EDGE)
  1835. st = WIN_MAXIMIZED;
  1836. if (st != WIN_NORMAL &&
  1837. place_window(u, (unsigned int)drag_id, st) != 0)
  1838. bar_notice("resize failed");
  1839. }
  1840. drag_id = -1;
  1841. resize_id = -1;
  1842. }
  1843. prev_buttons = buttons;
  1844. update_cursor(mx, my);
  1845. }
  1846. static char *put_str(char *p, const char *s)
  1847. {
  1848. while (*s)
  1849. *p++ = *s++;
  1850. return p;
  1851. }
  1852. static char *put_dec(char *p, unsigned int v)
  1853. {
  1854. char b[10];
  1855. unsigned int n = 0;
  1856. do {
  1857. b[n++] = (char)('0' + v % 10);
  1858. v /= 10;
  1859. } while (v);
  1860. while (n)
  1861. *p++ = b[--n];
  1862. return p;
  1863. }
  1864. /* Expose the last interval in /share so the target can be measured without
  1865. * a serial console. */
  1866. static void write_stats(void)
  1867. {
  1868. char b[80], *p = b;
  1869. unsigned int now = u_ticks_ms();
  1870. p = put_str(p, "t=");
  1871. p = put_dec(p, now / 1000);
  1872. p = put_str(p, "s frames=");
  1873. p = put_dec(p, stat_frames);
  1874. p = put_str(p, " ms=");
  1875. p = put_dec(p, stat_ms);
  1876. p = put_str(p, " max=");
  1877. p = put_dec(p, stat_max);
  1878. p = put_str(p, " px=");
  1879. p = put_dec(p, stat_px);
  1880. p = put_str(p, "\n");
  1881. u_syscall3(SYS_WRITE_FILE, (int)"/share/wmstats", (int)b,
  1882. (int)(p - b));
  1883. }
  1884. void _start(const char *arg)
  1885. {
  1886. struct wm_screen_info si;
  1887. struct wm_status st;
  1888. unsigned int i;
  1889. (void)arg;
  1890. if (u_wm_attach(&si) != 0)
  1891. u_exit(1);
  1892. usurf_init(&screen, si.fb, si.width, si.height, si.pitch, si.bpp);
  1893. usurf_font_attach(&screen, "/ramfs/fonts/sans.ttf", 14);
  1894. fbw = si.width;
  1895. fbh = si.height;
  1896. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1897. wins[i].info.used = 0;
  1898. wins[i].mapped = 0;
  1899. }
  1900. full_redraw = 1;
  1901. for (;;) {
  1902. unsigned int t0, t1;
  1903. if (u_wm_poll(&st) != 0)
  1904. u_exit(1);
  1905. if (st.full_redraw)
  1906. full_redraw = 1;
  1907. usurf_pixels = 0;
  1908. t0 = u_ticks_ms();
  1909. handle_mouse(&st);
  1910. do_update(&st);
  1911. t1 = u_ticks_ms();
  1912. if (usurf_pixels) {
  1913. unsigned int dt = t1 - t0;
  1914. stat_frames++;
  1915. stat_ms += dt;
  1916. stat_px += usurf_pixels;
  1917. if (dt > stat_max)
  1918. stat_max = dt;
  1919. }
  1920. if (t1 - stat_last >= 2000) {
  1921. stat_last = t1;
  1922. if (stat_frames) {
  1923. write_stats();
  1924. stat_frames = stat_ms = stat_px = stat_max = 0;
  1925. }
  1926. }
  1927. u_sleep_ms(10);
  1928. }
  1929. }