ucmd_wm.c 47 KB

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