ucmd_wm.c 60 KB

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