ucmd_wm.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  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. };
  21. static struct usurf screen;
  22. static struct uwin wins[WM_MAX_WINDOWS];
  23. static unsigned int fbw, fbh;
  24. static int drag_id = -1;
  25. static int drag_dx, drag_dy;
  26. static int resize_id = -1;
  27. static unsigned int resize_mx, resize_my, resize_ow, resize_oh;
  28. static unsigned int prev_buttons;
  29. static int full_redraw = 1;
  30. static int boot_term_spawned;
  31. static unsigned int bar_kexec_busy, bar_kexec_progress, bar_kexec_total;
  32. static int bar_temp_c = -1;
  33. static int bar_batt_pct = -1;
  34. static int bar_batt_charging;
  35. static int bar_batt_ac;
  36. static char bar_msg[24];
  37. static unsigned int bar_msg_until;
  38. static int bar_msg_dirty;
  39. static unsigned int stat_frames, stat_ms, stat_px, stat_max, stat_last;
  40. static void bar_notice(const char *msg)
  41. {
  42. unsigned int i;
  43. for (i = 0; msg[i] && i + 1 < sizeof(bar_msg); i++)
  44. bar_msg[i] = msg[i];
  45. bar_msg[i] = '\0';
  46. bar_msg_until = u_ticks_ms() + 2000;
  47. bar_msg_dirty = 1;
  48. }
  49. static int free_window_slot(void)
  50. {
  51. unsigned int i;
  52. for (i = 0; i < WM_MAX_WINDOWS; i++)
  53. if (!wins[i].info.used)
  54. return 1;
  55. return 0;
  56. }
  57. static unsigned int rgb16(unsigned int r, unsigned int g, unsigned int b)
  58. {
  59. return (r << 16) | (g << 8) | b;
  60. }
  61. /* Rainbow gradient: hue 0..1535 (6*256) maps to R→Y→G→C→B→M→R, the same
  62. * integer interpolation the kernel bar uses. */
  63. static unsigned int rainbow_rgb(unsigned int hue)
  64. {
  65. unsigned int seg = hue / 256, f = hue % 256;
  66. unsigned int r1, g1, b1, r2, g2, b2;
  67. switch (seg) {
  68. case 0: r1=255; g1=0; b1=0; r2=255; g2=255; b2=0; break;
  69. case 1: r1=255; g1=255; b1=0; r2=0; g2=255; b2=0; break;
  70. case 2: r1=0; g1=255; b1=0; r2=0; g2=255; b2=255; break;
  71. case 3: r1=0; g1=255; b1=255; r2=0; g2=0; b2=255; break;
  72. case 4: r1=0; g1=0; b1=255; r2=255; g2=0; b2=255; break;
  73. default:r1=255; g1=0; b1=255; r2=255; g2=0; b2=0; break;
  74. }
  75. return rgb16((r1 * (256 - f) + r2 * f) / 256,
  76. (g1 * (256 - f) + g2 * f) / 256,
  77. (b1 * (256 - f) + b2 * f) / 256);
  78. }
  79. static void fill_clip(int x, int y, unsigned int w, unsigned int h,
  80. unsigned int rgb, int cx, int cy,
  81. unsigned int cw, unsigned int ch)
  82. {
  83. int x0 = x, y0 = y, x1 = x + (int)w, y1 = y + (int)h;
  84. if (x0 < cx) x0 = cx;
  85. if (y0 < cy) y0 = cy;
  86. if (x1 > cx + (int)cw) x1 = cx + (int)cw;
  87. if (y1 > cy + (int)ch) y1 = cy + (int)ch;
  88. if (x1 > x0 && y1 > y0)
  89. usurf_fill(&screen, x0, y0, (unsigned int)(x1 - x0),
  90. (unsigned int)(y1 - y0), rgb);
  91. }
  92. static void bar_button(unsigned int x, unsigned int y, unsigned int w,
  93. unsigned int h, const char *label)
  94. {
  95. unsigned int n = 0;
  96. usurf_fill(&screen, x, y, w, h, 0x963CD2u);
  97. while (label[n])
  98. n++;
  99. usurf_text(&screen, (int)(x + (w - n * 8) / 2), (int)(y + 3),
  100. label, COL_WHITE, 0, 0, fbw, WM_BAR_H);
  101. }
  102. static unsigned int temp_color(int c)
  103. {
  104. if (c >= 80)
  105. return 0xFF5050u;
  106. if (c >= 60)
  107. return 0xFFD250u;
  108. return 0x55FF55u;
  109. }
  110. /* "CPU 43C" (no libc printf here). */
  111. static unsigned int temp_text(char *b, int c)
  112. {
  113. unsigned int n = 0, v = (unsigned int)c;
  114. b[n++] = 'C';
  115. b[n++] = 'P';
  116. b[n++] = 'U';
  117. b[n++] = ' ';
  118. if (v >= 100)
  119. b[n++] = (char)('0' + (v / 100) % 10);
  120. if (v >= 10)
  121. b[n++] = (char)('0' + (v / 10) % 10);
  122. b[n++] = (char)('0' + v % 10);
  123. b[n++] = 'C';
  124. b[n] = '\0';
  125. return n;
  126. }
  127. /* "BAT 87%" (no libc printf here). */
  128. static unsigned int batt_text(char *b, int pct)
  129. {
  130. unsigned int n = 0, v = (unsigned int)pct;
  131. b[n++] = 'B';
  132. b[n++] = 'A';
  133. b[n++] = 'T';
  134. b[n++] = ' ';
  135. if (v >= 100)
  136. b[n++] = (char)('0' + (v / 100) % 10);
  137. if (v >= 10)
  138. b[n++] = (char)('0' + (v / 10) % 10);
  139. b[n++] = (char)('0' + v % 10);
  140. b[n++] = '%';
  141. b[n] = '\0';
  142. return n;
  143. }
  144. static unsigned int batt_color(int pct, int charging, int ac)
  145. {
  146. if (pct <= 10)
  147. return 0xFF5050u;
  148. if (pct <= 20)
  149. return 0xFFD250u;
  150. if (charging || ac)
  151. return 0x55FF55u;
  152. return 0x55D2FFu;
  153. }
  154. static void draw_bar(void)
  155. {
  156. char dt[48];
  157. unsigned int n, i, x, y;
  158. usurf_fill(&screen, 0, 0, fbw, WM_BAR_H, COL_BG);
  159. usurf_text(&screen, 4, 5, OS_NAME " v" OS_VERSION, COL_BANNER,
  160. 0, 0, fbw, WM_BAR_H);
  161. bar_button(WM_BTN_X, WM_BTN_Y, WM_BTN_W, WM_BTN_H, "New");
  162. bar_button(WM_BTN_CLOCK_X, WM_BTN_CLOCK_Y, WM_BTN_CLOCK_W,
  163. WM_BTN_CLOCK_H, "Clock");
  164. bar_button(WM_BTN_COLORS_X, WM_BTN_COLORS_Y, WM_BTN_COLORS_W,
  165. WM_BTN_COLORS_H, "Colors");
  166. bar_button(WM_BTN_FILES_X, WM_BTN_FILES_Y, WM_BTN_FILES_W,
  167. WM_BTN_FILES_H, "Files");
  168. bar_button(WM_BTN_MINES_X, WM_BTN_MINES_Y, WM_BTN_MINES_W,
  169. WM_BTN_MINES_H, "Mines");
  170. bar_button(WM_BTN_WEB_X, WM_BTN_WEB_Y, WM_BTN_WEB_W,
  171. WM_BTN_WEB_H, "Web");
  172. bar_button(WM_BTN_CARDS_X, WM_BTN_CARDS_Y, WM_BTN_CARDS_W,
  173. WM_BTN_CARDS_H, "Cards");
  174. bar_button(WM_BTN_GALLERY_X, WM_BTN_GALLERY_Y, WM_BTN_GALLERY_W,
  175. WM_BTN_GALLERY_H, "Gallery");
  176. if (bar_msg_until &&
  177. (int)(u_ticks_ms() - bar_msg_until) < 0)
  178. usurf_text(&screen, (int)(WM_BTN_GALLERY_X + WM_BTN_GALLERY_W + 8),
  179. 5, bar_msg, 0xFFD250u, 0, 0, fbw, WM_BAR_H);
  180. /* Rainbow separator (same y as the kernel's old bar). */
  181. for (x = 0; x < fbw; x++) {
  182. unsigned int c = rainbow_rgb((x * (6 * 256)) / (fbw ? fbw : 1));
  183. for (y = WM_BAR_H - 5; y < WM_BAR_H - 3; y++)
  184. usurf_fill(&screen, (int)x, (int)y, 1, 1, c);
  185. }
  186. if (bar_kexec_busy) {
  187. static const char msg[] = "loading new kernel over serial";
  188. unsigned int dots = bar_kexec_total ?
  189. (bar_kexec_progress * 12 / bar_kexec_total) : 0;
  190. n = 0;
  191. while (msg[n])
  192. dt[n] = msg[n], n++;
  193. for (i = 0; i < 12; i++)
  194. dt[n + i] = (i < dots) ? '.' : ' ';
  195. n += 12;
  196. dt[n] = '\0';
  197. usurf_text(&screen, (int)fbw - (int)n * 8 - 4, 5, dt,
  198. 0xFFC864u, 0, 0, fbw, WM_BAR_H);
  199. } else {
  200. unsigned int clock_chars = 0;
  201. int right = (int)fbw - 4;
  202. int min_x = WM_BTN_GALLERY_X + (int)WM_BTN_GALLERY_W + 4;
  203. if (u_rtc_datetime(dt, sizeof(dt)) == 0) {
  204. n = 0;
  205. while (dt[n] && n < sizeof(dt) - 1)
  206. n++;
  207. usurf_text(&screen, right - (int)n * 8, 5, dt,
  208. COL_BANNER, 0, 0, fbw, WM_BAR_H);
  209. clock_chars = n;
  210. }
  211. if (clock_chars)
  212. right -= (int)clock_chars * 8 + 8;
  213. if (bar_temp_c >= 0) {
  214. char tb[12];
  215. unsigned int tn = temp_text(tb, bar_temp_c);
  216. int tx = right - (int)tn * 8;
  217. if (tx >= min_x) {
  218. usurf_text(&screen, tx, 5, tb,
  219. temp_color(bar_temp_c), 0, 0,
  220. fbw, WM_BAR_H);
  221. right = tx - 8;
  222. }
  223. }
  224. if (bar_batt_pct >= 0) {
  225. char bb[10];
  226. unsigned int bn = batt_text(bb, bar_batt_pct);
  227. int bx = right - (int)bn * 8;
  228. if (bx >= min_x)
  229. usurf_text(&screen, bx, 5, bb,
  230. batt_color(bar_batt_pct,
  231. bar_batt_charging,
  232. bar_batt_ac),
  233. 0, 0, fbw, WM_BAR_H);
  234. }
  235. }
  236. }
  237. static void draw_window_clip(unsigned int idx, int cx, int cy,
  238. unsigned int cw, unsigned int ch)
  239. {
  240. struct uwin *u = &wins[idx];
  241. struct wm_window_info *w = &u->info;
  242. int x = w->x, y = w->y;
  243. unsigned int pw = w->w, ph = w->h;
  244. unsigned int bx = (unsigned int)(x + (int)pw - 1 - WM_CLOSE_W);
  245. unsigned int cony = (unsigned int)(y + 1 + WM_TITLE_H);
  246. unsigned int content_x = (unsigned int)(x + 1);
  247. unsigned int content_w = w->content_w;
  248. unsigned int content_h = w->content_h;
  249. /* Border, title bar, close button. */
  250. fill_clip(x, y, pw, ph, COL_BORDER, cx, cy, cw, ch);
  251. fill_clip(x + 1, y + 1, pw - 2, WM_TITLE_H,
  252. w->focused ? COL_TITLE_ON : COL_TITLE_OFF, cx, cy, cw, ch);
  253. usurf_text(&screen, x + 4, y + 3, w->title, COL_WHITE,
  254. cx, cy, cw, ch);
  255. fill_clip((int)bx, y + 1, WM_CLOSE_W, WM_TITLE_H, COL_CLOSE,
  256. cx, cy, cw, ch);
  257. usurf_text(&screen, (int)bx + 4, y + 3, "x", COL_WHITE,
  258. cx, cy, cw, ch);
  259. /* Content: every window presents an RGB565 canvas (the terminal
  260. * renders its cells into one; gfx commands own theirs). A mapped
  261. * canvas covers the whole content area, so it does not need the black
  262. * underlay. */
  263. if (u->mapped && u->map.kind == WM_KIND_GFX) {
  264. struct uwm_canvas c;
  265. c.px = (const unsigned short *)u->map.va;
  266. c.w = u->map.w;
  267. c.h = u->map.h;
  268. if (c.w != content_w || c.h != content_h)
  269. fill_clip((int)content_x, (int)cony, content_w, content_h,
  270. 0x000000u, cx, cy, cw, ch);
  271. usurf_canvas(&screen, &c, (int)content_x, (int)cony,
  272. cx, cy, cw, ch);
  273. } else {
  274. fill_clip((int)content_x, (int)cony, content_w, content_h,
  275. 0x000000u, cx, cy, cw, ch);
  276. }
  277. /* Resize grip (bottom-right corner). */
  278. if (w->focused) {
  279. unsigned int i;
  280. for (i = 0; i < 10; i++) {
  281. fill_clip(x + (int)pw - 3 - (int)i,
  282. y + (int)ph - 4, 1, 1, 0xD2D2D2u,
  283. cx, cy, cw, ch);
  284. fill_clip(x + (int)pw - 4,
  285. y + (int)ph - 3 - (int)i, 1, 1, 0xD2D2D2u,
  286. cx, cy, cw, ch);
  287. }
  288. }
  289. }
  290. /* Front-to-back window order: the focused window is on top, the others
  291. * follow in descending slot order. Painting and hit-testing share it. */
  292. static unsigned int build_order(int *order)
  293. {
  294. unsigned int i, n = 0;
  295. for (i = 0; i < WM_MAX_WINDOWS; i++)
  296. if (wins[i].info.used && wins[i].info.focused)
  297. order[n++] = (int)i;
  298. for (i = WM_MAX_WINDOWS; i-- > 0;)
  299. if (wins[i].info.used && !wins[i].info.focused)
  300. order[n++] = (int)i;
  301. return n;
  302. }
  303. struct orect {
  304. int x, y;
  305. unsigned int w, h;
  306. };
  307. #define OCC_MAX 64
  308. /* r minus cut, split into up to four disjoint rectangles. */
  309. static unsigned int rect_sub(struct orect *out, const struct orect *r,
  310. const struct orect *cut)
  311. {
  312. int rx0 = r->x, ry0 = r->y;
  313. int rx1 = r->x + (int)r->w, ry1 = r->y + (int)r->h;
  314. int cx0 = cut->x, cy0 = cut->y;
  315. int cx1 = cut->x + (int)cut->w, cy1 = cut->y + (int)cut->h;
  316. unsigned int n = 0;
  317. if (cx0 < rx0) cx0 = rx0;
  318. if (cy0 < ry0) cy0 = ry0;
  319. if (cx1 > rx1) cx1 = rx1;
  320. if (cy1 > ry1) cy1 = ry1;
  321. if (cx1 <= cx0 || cy1 <= cy0) {
  322. out[0] = *r;
  323. return 1;
  324. }
  325. if (cy0 > ry0) {
  326. out[n].x = rx0;
  327. out[n].y = ry0;
  328. out[n].w = (unsigned int)(rx1 - rx0);
  329. out[n].h = (unsigned int)(cy0 - ry0);
  330. n++;
  331. }
  332. if (cy1 < ry1) {
  333. out[n].x = rx0;
  334. out[n].y = cy1;
  335. out[n].w = (unsigned int)(rx1 - rx0);
  336. out[n].h = (unsigned int)(ry1 - cy1);
  337. n++;
  338. }
  339. if (cx0 > rx0) {
  340. out[n].x = rx0;
  341. out[n].y = cy0;
  342. out[n].w = (unsigned int)(cx0 - rx0);
  343. out[n].h = (unsigned int)(cy1 - cy0);
  344. n++;
  345. }
  346. if (cx1 < rx1) {
  347. out[n].x = cx1;
  348. out[n].y = cy0;
  349. out[n].w = (unsigned int)(rx1 - cx1);
  350. out[n].h = (unsigned int)(cy1 - cy0);
  351. n++;
  352. }
  353. return n;
  354. }
  355. /* Back-to-front repaint, used only if the visible-region list overflows. */
  356. static void draw_rect_naive(int x, int y, unsigned int w, unsigned int h)
  357. {
  358. unsigned int i;
  359. usurf_fill(&screen, x, y, w, h, COL_BG);
  360. for (i = 0; i < WM_MAX_WINDOWS; i++)
  361. if (wins[i].info.used && !wins[i].info.focused &&
  362. x < wins[i].info.x + (int)wins[i].info.w &&
  363. x + (int)w > wins[i].info.x &&
  364. y < wins[i].info.y + (int)wins[i].info.h &&
  365. y + (int)h > wins[i].info.y)
  366. draw_window_clip(i, x, y, w, h);
  367. for (i = 0; i < WM_MAX_WINDOWS; i++)
  368. if (wins[i].info.used && wins[i].info.focused &&
  369. x < wins[i].info.x + (int)wins[i].info.w &&
  370. x + (int)w > wins[i].info.x &&
  371. y < wins[i].info.y + (int)wins[i].info.h &&
  372. y + (int)h > wins[i].info.y)
  373. draw_window_clip(i, x, y, w, h);
  374. }
  375. static void draw_rect(int x, int y, unsigned int w, unsigned int h)
  376. {
  377. static struct orect cur[OCC_MAX], nxt[OCC_MAX];
  378. struct orect dmg;
  379. int order[WM_MAX_WINDOWS];
  380. unsigned int n, i, j, ncur, nnext;
  381. int overflow = 0;
  382. int x0 = x, y0 = y, x1 = x + (int)w, y1 = y + (int)h;
  383. if (w == 0 || h == 0)
  384. return;
  385. if (x0 < 0) x0 = 0;
  386. if (y0 < 0) y0 = 0;
  387. if (x1 > (int)fbw) x1 = (int)fbw;
  388. if (y1 > (int)fbh) y1 = (int)fbh;
  389. if (x1 <= x0 || y1 <= y0)
  390. return;
  391. dmg.x = x0;
  392. dmg.y = y0;
  393. dmg.w = (unsigned int)(x1 - x0);
  394. dmg.h = (unsigned int)(y1 - y0);
  395. usurf_fill(&screen, dmg.x, dmg.y, dmg.w, dmg.h, COL_BG);
  396. n = build_order(order);
  397. for (i = 0; i < n; i++) {
  398. struct uwin *u = &wins[order[i]];
  399. int wx0 = u->info.x, wy0 = u->info.y;
  400. int wx1 = wx0 + (int)u->info.w, wy1 = wy0 + (int)u->info.h;
  401. if (wx0 < dmg.x) wx0 = dmg.x;
  402. if (wy0 < dmg.y) wy0 = dmg.y;
  403. if (wx1 > x1) wx1 = x1;
  404. if (wy1 > y1) wy1 = y1;
  405. if (wx1 <= wx0 || wy1 <= wy0)
  406. continue;
  407. cur[0].x = wx0;
  408. cur[0].y = wy0;
  409. cur[0].w = (unsigned int)(wx1 - wx0);
  410. cur[0].h = (unsigned int)(wy1 - wy0);
  411. ncur = 1;
  412. for (j = 0; j < i && ncur; j++) {
  413. struct uwin *f = &wins[order[j]];
  414. struct orect fr;
  415. unsigned int k;
  416. fr.x = f->info.x;
  417. fr.y = f->info.y;
  418. fr.w = f->info.w;
  419. fr.h = f->info.h;
  420. nnext = 0;
  421. for (k = 0; k < ncur; k++) {
  422. if (nnext + 4 > OCC_MAX) {
  423. overflow = 1;
  424. break;
  425. }
  426. nnext += rect_sub(&nxt[nnext], &cur[k], &fr);
  427. }
  428. if (overflow)
  429. break;
  430. for (k = 0; k < nnext; k++)
  431. cur[k] = nxt[k];
  432. ncur = nnext;
  433. }
  434. if (overflow)
  435. break;
  436. for (j = 0; j < ncur; j++)
  437. draw_window_clip((unsigned int)order[i], cur[j].x,
  438. cur[j].y, cur[j].w, cur[j].h);
  439. }
  440. if (overflow)
  441. draw_rect_naive(dmg.x, dmg.y, dmg.w, dmg.h);
  442. }
  443. static int window_at(unsigned int mx, unsigned int my)
  444. {
  445. int order[WM_MAX_WINDOWS];
  446. unsigned int n = build_order(order), i;
  447. for (i = 0; i < n; i++) {
  448. struct wm_window_info *w = &wins[order[i]].info;
  449. if (mx >= (unsigned int)w->x && my >= (unsigned int)w->y &&
  450. mx < (unsigned int)(w->x + (int)w->w) &&
  451. my < (unsigned int)(w->y + (int)w->h))
  452. return order[i];
  453. }
  454. return -1;
  455. }
  456. static void clamp_geom(struct uwin *u, int *x, int *y)
  457. {
  458. int maxx = (int)fbw - (int)u->info.w;
  459. int maxy = (int)fbh - (int)u->info.h;
  460. if (*x < 0) *x = 0;
  461. if (*y < WM_BAR_H) *y = WM_BAR_H;
  462. if (maxx < 0) maxx = 0;
  463. if (maxy < WM_BAR_H) maxy = WM_BAR_H;
  464. if (*x > maxx) *x = maxx;
  465. if (*y > maxy) *y = maxy;
  466. }
  467. static void add_rect(struct wm_present_info *p, int x, int y,
  468. unsigned int w, unsigned int h)
  469. {
  470. if (w == 0 || h == 0)
  471. return;
  472. if (p->count < 4) {
  473. p->rect[p->count].x = x;
  474. p->rect[p->count].y = y;
  475. p->rect[p->count].w = w;
  476. p->rect[p->count].h = h;
  477. p->count++;
  478. } else {
  479. /* Four is enough for normal updates; fold the rest into the
  480. * last rect rather than dropping pixels. */
  481. int x0 = p->rect[3].x, y0 = p->rect[3].y;
  482. int x1 = x0 + (int)p->rect[3].w, y1 = y0 + (int)p->rect[3].h;
  483. int nx1 = x + (int)w, ny1 = y + (int)h;
  484. if (x < x0) x0 = x;
  485. if (y < y0) y0 = y;
  486. if (nx1 > x1) x1 = nx1;
  487. if (ny1 > y1) y1 = ny1;
  488. p->rect[3].x = x0;
  489. p->rect[3].y = y0;
  490. p->rect[3].w = (unsigned int)(x1 - x0);
  491. p->rect[3].h = (unsigned int)(y1 - y0);
  492. }
  493. }
  494. /* A focused window is topmost, so the shadow contains exactly its pixels in
  495. * the old rectangle. Move that block and repaint only the old part the new
  496. * rectangle does not cover. */
  497. static void move_window_pixels(struct uwin *u, int ox, int oy, int nx, int ny)
  498. {
  499. struct wm_present_info present;
  500. struct orect oldr, newr, pieces[4];
  501. unsigned int np, k;
  502. if (u_wm_begin() != 0)
  503. return;
  504. usurf_copy(&screen, nx, ny, ox, oy, u->info.w, u->info.h);
  505. oldr.x = ox;
  506. oldr.y = oy;
  507. oldr.w = u->info.w;
  508. oldr.h = u->info.h;
  509. newr.x = nx;
  510. newr.y = ny;
  511. newr.w = u->info.w;
  512. newr.h = u->info.h;
  513. np = rect_sub(pieces, &oldr, &newr);
  514. for (k = 0; k < np; k++)
  515. draw_rect(pieces[k].x, pieces[k].y, pieces[k].w, pieces[k].h);
  516. present.count = 0;
  517. present.immediate = 1;
  518. add_rect(&present, nx, ny, u->info.w, u->info.h);
  519. for (k = 0; k < np; k++)
  520. add_rect(&present, pieces[k].x, pieces[k].y,
  521. pieces[k].w, pieces[k].h);
  522. u_wm_present(&present);
  523. }
  524. static void do_update(struct wm_status *st)
  525. {
  526. struct wm_present_info present;
  527. int need = 0;
  528. int bar_dirty = 0;
  529. unsigned int i;
  530. char dt[20];
  531. static char last_dt[20];
  532. int kexec_changed = (st->kexec_busy != bar_kexec_busy) ||
  533. (st->kexec_busy && st->kexec_progress != bar_kexec_progress);
  534. unsigned int msg_now = u_ticks_ms();
  535. /* Spawn the first terminal once the kernel says the storage scan is
  536. * done, so a shell command can never race the mount table. */
  537. if (st->boot_ready && !boot_term_spawned) {
  538. boot_term_spawned = 1;
  539. if (!free_window_slot()) {
  540. bar_notice("window limit");
  541. full_redraw = 1;
  542. } else if (u_wm_create_term() < 0) {
  543. bar_notice("cannot start term");
  544. full_redraw = 1;
  545. } else {
  546. full_redraw = 1;
  547. }
  548. }
  549. if (bar_msg_dirty ||
  550. (bar_msg_until && (int)(msg_now - bar_msg_until) >= 0)) {
  551. bar_dirty = 1;
  552. bar_msg_dirty = 0;
  553. if (bar_msg_until && (int)(msg_now - bar_msg_until) >= 0)
  554. bar_msg_until = 0;
  555. }
  556. bar_kexec_busy = st->kexec_busy;
  557. bar_kexec_progress = st->kexec_progress;
  558. bar_kexec_total = st->kexec_total;
  559. if (kexec_changed)
  560. bar_dirty = 1;
  561. /* Refresh cached window state and mappings before opening a frame:
  562. * MAP may unmap/remap content. */
  563. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  564. struct wm_window_info info;
  565. int r = u_wm_info(i, &info);
  566. if (r < 0) {
  567. u_exit(1);
  568. }
  569. if (r == 0 || !info.used) {
  570. if (wins[i].info.used)
  571. full_redraw = 1;
  572. wins[i].info.used = 0;
  573. wins[i].mapped = 0;
  574. continue;
  575. }
  576. if (!wins[i].info.used ||
  577. info.content_gen != wins[i].info.content_gen)
  578. full_redraw = 1;
  579. wins[i].info = info;
  580. if (!wins[i].mapped ||
  581. wins[i].map.generation != info.content_gen) {
  582. struct wm_map_info map;
  583. if (u_wm_map(i, &map) == 0) {
  584. wins[i].map = map;
  585. wins[i].mapped = 1;
  586. } else {
  587. wins[i].mapped = 0;
  588. }
  589. }
  590. }
  591. /* The RTC is slow (CMOS polling) and the temperature/battery state only
  592. * changes once a second; query them at 1 Hz instead of every 10 ms. */
  593. {
  594. static unsigned int last_rtc_ms;
  595. unsigned int now_ms = u_ticks_ms();
  596. if (full_redraw || now_ms - last_rtc_ms >= 1000) {
  597. last_rtc_ms = now_ms;
  598. if (u_rtc_datetime(dt, sizeof(dt)) == 0) {
  599. for (i = 0; i < sizeof(dt); i++)
  600. if (dt[i] != last_dt[i]) {
  601. for (i = 0; i < sizeof(dt); i++)
  602. last_dt[i] = dt[i];
  603. bar_dirty = 1;
  604. break;
  605. }
  606. }
  607. {
  608. struct sys_cputemp ct;
  609. if (u_cpu_temp(&ct) == 0 &&
  610. ct.temp_c != bar_temp_c) {
  611. bar_temp_c = ct.temp_c;
  612. bar_dirty = 1;
  613. }
  614. }
  615. {
  616. struct sys_powerinfo pi;
  617. int pct = -1, charging = 0, ac = 0;
  618. unsigned int bi;
  619. if (u_power_info(&pi, 0) == 0) {
  620. ac = pi.ac_online > 0;
  621. if (pi.present) {
  622. for (bi = 0; bi < SYS_POWER_BATT_MAX;
  623. bi++) {
  624. struct sys_battery *b =
  625. &pi.battery[bi];
  626. if (!b->present ||
  627. !b->percent_valid)
  628. continue;
  629. pct = (int)b->percent;
  630. charging = (int)b->charging;
  631. break;
  632. }
  633. }
  634. if (pct != bar_batt_pct ||
  635. charging != bar_batt_charging ||
  636. ac != bar_batt_ac) {
  637. bar_batt_pct = pct;
  638. bar_batt_charging = charging;
  639. bar_batt_ac = ac;
  640. bar_dirty = 1;
  641. }
  642. }
  643. }
  644. }
  645. }
  646. if (full_redraw) {
  647. if (u_wm_begin() != 0) {
  648. full_redraw = 1;
  649. return;
  650. }
  651. full_redraw = 0;
  652. need = 1;
  653. {
  654. draw_rect(0, 0, fbw, fbh);
  655. draw_bar();
  656. present.count = 0;
  657. add_rect(&present, 0, 0, fbw, fbh);
  658. present.immediate = 0;
  659. u_wm_present(&present);
  660. /* Ack every window that was clean at the time of the
  661. * redraw. If new damage arrived during it, its seq
  662. * differs and the kernel leaves it dirty. */
  663. for (i = 0; i < WM_MAX_WINDOWS; i++)
  664. if (wins[i].info.used && wins[i].mapped)
  665. u_wm_ack(i, wins[i].info.dirty_seq);
  666. }
  667. return;
  668. }
  669. /* Partial cell/canvas updates and the bar clock. */
  670. present.count = 0;
  671. present.immediate = 0;
  672. if (bar_dirty) {
  673. need = 1;
  674. add_rect(&present, 0, 0, fbw, WM_BAR_H);
  675. }
  676. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  677. struct uwin *u = &wins[i];
  678. struct wm_window_info *w = &u->info;
  679. int rx, ry;
  680. unsigned int rw, rh;
  681. if (!w->used || !w->dirty)
  682. continue;
  683. if (!w->dmg_w || !w->dmg_h)
  684. continue;
  685. rx = w->x + 1 + (int)w->dmg_x;
  686. ry = w->y + 1 + WM_TITLE_H + (int)w->dmg_y;
  687. rw = w->dmg_w;
  688. rh = w->dmg_h;
  689. add_rect(&present, rx, ry, rw, rh);
  690. u->ack_seq = u->mapped ? w->dirty_seq : 0;
  691. need = 1;
  692. }
  693. if (!need) {
  694. /* Drag movement is handled below; a stationary mouse can be
  695. * left to the kernel's cursor overlay. */
  696. return;
  697. }
  698. if (u_wm_begin() != 0)
  699. return;
  700. for (i = 0; i < present.count; i++)
  701. draw_rect(present.rect[i].x, present.rect[i].y,
  702. present.rect[i].w, present.rect[i].h);
  703. if (bar_dirty)
  704. draw_bar();
  705. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  706. struct uwin *u = &wins[i];
  707. if (!u->info.used || !u->info.dirty || !u->mapped ||
  708. !u->ack_seq)
  709. continue;
  710. if (u->ack_seq == u->info.dirty_seq)
  711. u_wm_ack(i, u->info.dirty_seq);
  712. }
  713. u_wm_present(&present);
  714. }
  715. static int spawn_app(const char *path)
  716. {
  717. int pid = u_spawn(path, "", 0, "/");
  718. if (pid > 0)
  719. full_redraw = 1;
  720. return pid;
  721. }
  722. static void spawn_app_checked(const char *path)
  723. {
  724. if (!free_window_slot()) {
  725. bar_notice("window limit");
  726. full_redraw = 1;
  727. return;
  728. }
  729. if (spawn_app(path) < 0) {
  730. bar_notice("spawn failed");
  731. full_redraw = 1;
  732. }
  733. }
  734. static void handle_mouse(struct wm_status *st)
  735. {
  736. unsigned int buttons = st->mouse.buttons;
  737. unsigned int mx = st->mouse.x, my = st->mouse.y;
  738. int down = (buttons & 1) != 0;
  739. int was = (prev_buttons & 1) != 0;
  740. if (down && !was) {
  741. if (mx >= WM_BTN_X && my >= WM_BTN_Y &&
  742. mx < WM_BTN_X + WM_BTN_W && my < WM_BTN_Y + WM_BTN_H) {
  743. if (!free_window_slot()) {
  744. bar_notice("window limit");
  745. full_redraw = 1;
  746. } else if (u_wm_create_term() < 0) {
  747. bar_notice("cannot start term");
  748. full_redraw = 1;
  749. } else {
  750. full_redraw = 1;
  751. }
  752. } else if (mx >= WM_BTN_CLOCK_X && my >= WM_BTN_CLOCK_Y &&
  753. mx < WM_BTN_CLOCK_X + WM_BTN_CLOCK_W &&
  754. my < WM_BTN_CLOCK_Y + WM_BTN_CLOCK_H) {
  755. spawn_app_checked("/ramfs/bin/clock");
  756. } else if (mx >= WM_BTN_COLORS_X && my >= WM_BTN_COLORS_Y &&
  757. mx < WM_BTN_COLORS_X + WM_BTN_COLORS_W &&
  758. my < WM_BTN_COLORS_Y + WM_BTN_COLORS_H) {
  759. spawn_app_checked("/ramfs/bin/colors");
  760. } else if (mx >= WM_BTN_FILES_X && my >= WM_BTN_FILES_Y &&
  761. mx < WM_BTN_FILES_X + WM_BTN_FILES_W &&
  762. my < WM_BTN_FILES_Y + WM_BTN_FILES_H) {
  763. spawn_app_checked("/ramfs/bin/files");
  764. } else if (mx >= WM_BTN_MINES_X && my >= WM_BTN_MINES_Y &&
  765. mx < WM_BTN_MINES_X + WM_BTN_MINES_W &&
  766. my < WM_BTN_MINES_Y + WM_BTN_MINES_H) {
  767. spawn_app_checked("/ramfs/bin/mines");
  768. } else if (mx >= WM_BTN_WEB_X && my >= WM_BTN_WEB_Y &&
  769. mx < WM_BTN_WEB_X + WM_BTN_WEB_W &&
  770. my < WM_BTN_WEB_Y + WM_BTN_WEB_H) {
  771. spawn_app_checked("/ramfs/bin/browser");
  772. } else if (mx >= WM_BTN_CARDS_X && my >= WM_BTN_CARDS_Y &&
  773. mx < WM_BTN_CARDS_X + WM_BTN_CARDS_W &&
  774. my < WM_BTN_CARDS_Y + WM_BTN_CARDS_H) {
  775. spawn_app_checked("/ramfs/bin/solitaire");
  776. } else if (mx >= WM_BTN_GALLERY_X && my >= WM_BTN_GALLERY_Y &&
  777. mx < WM_BTN_GALLERY_X + WM_BTN_GALLERY_W &&
  778. my < WM_BTN_GALLERY_Y + WM_BTN_GALLERY_H) {
  779. spawn_app_checked("/ramfs/bin/gallery");
  780. } else {
  781. int idx = window_at(mx, my);
  782. if (idx >= 0) {
  783. struct uwin *u = &wins[idx];
  784. u_wm_focus((unsigned int)idx);
  785. if (mx >= (unsigned int)(u->info.x + (int)u->info.w -
  786. 1 - WM_CLOSE_W) &&
  787. mx < (unsigned int)(u->info.x + (int)u->info.w) &&
  788. my >= (unsigned int)(u->info.y + 1) &&
  789. my < (unsigned int)(u->info.y + 1 + WM_TITLE_H)) {
  790. u_wm_close((unsigned int)idx);
  791. full_redraw = 1;
  792. } else if (mx >= (unsigned int)(u->info.x + (int)u->info.w - 16) &&
  793. my >= (unsigned int)(u->info.y + (int)u->info.h - 16)) {
  794. resize_id = idx;
  795. resize_mx = mx;
  796. resize_my = my;
  797. resize_ow = u->info.w;
  798. resize_oh = u->info.h;
  799. } else if (my < (unsigned int)(u->info.y + WM_TITLE_H)) {
  800. drag_id = idx;
  801. drag_dx = (int)mx - u->info.x;
  802. drag_dy = (int)my - u->info.y;
  803. }
  804. }
  805. }
  806. } else if (down && was && resize_id >= 0) {
  807. struct uwin *u = &wins[resize_id];
  808. if (u->info.used) {
  809. unsigned int oldw = u->info.w, oldh = u->info.h;
  810. int nw = (int)resize_ow + (int)mx - (int)resize_mx;
  811. int nh = (int)resize_oh + (int)my - (int)resize_my;
  812. int maxw = (int)fbw - u->info.x;
  813. int maxh = (int)fbh - u->info.y;
  814. if (nw < 200) nw = 200;
  815. if (nh < 140) nh = 140;
  816. if (nw > maxw) nw = maxw;
  817. if (nh > maxh) nh = maxh;
  818. if (nw != (int)u->info.w || nh != (int)u->info.h) {
  819. struct wm_window_info ni;
  820. struct wm_map_info nm;
  821. struct wm_present_info present;
  822. unsigned int i;
  823. if (u_wm_resize((unsigned int)resize_id,
  824. (unsigned int)(nw - 2),
  825. (unsigned int)(nh - WM_TITLE_H - 2)) == 0) {
  826. if (u_wm_info((unsigned int)resize_id, &ni) == 1)
  827. u->info = ni;
  828. u->mapped = 0;
  829. if (u_wm_map((unsigned int)resize_id, &nm) == 0) {
  830. u->map = nm;
  831. u->mapped = 1;
  832. }
  833. present.count = 0;
  834. present.immediate = 1;
  835. add_rect(&present, u->info.x, u->info.y, oldw, oldh);
  836. add_rect(&present, u->info.x, u->info.y,
  837. u->info.w, u->info.h);
  838. if (u_wm_begin() == 0) {
  839. for (i = 0; i < present.count; i++)
  840. draw_rect(present.rect[i].x,
  841. present.rect[i].y,
  842. present.rect[i].w,
  843. present.rect[i].h);
  844. u_wm_present(&present);
  845. }
  846. }
  847. }
  848. } else {
  849. resize_id = -1;
  850. }
  851. } else if (down && was && drag_id >= 0) {
  852. struct uwin *u = &wins[drag_id];
  853. if (u->info.used) {
  854. int ox = u->info.x, oy = u->info.y;
  855. int nx = (int)mx - drag_dx;
  856. int ny = (int)my - drag_dy;
  857. clamp_geom(u, &nx, &ny);
  858. if (nx != ox || ny != oy) {
  859. struct wm_present_info present;
  860. unsigned int i;
  861. u_wm_set_geom((unsigned int)drag_id, nx, ny);
  862. u->info.x = nx;
  863. u->info.y = ny;
  864. if (u->info.focused) {
  865. move_window_pixels(u, ox, oy, nx, ny);
  866. } else {
  867. present.count = 0;
  868. present.immediate = 1;
  869. add_rect(&present, ox, oy, u->info.w, u->info.h);
  870. add_rect(&present, nx, ny, u->info.w, u->info.h);
  871. if (u_wm_begin() == 0) {
  872. for (i = 0; i < present.count; i++)
  873. draw_rect(present.rect[i].x,
  874. present.rect[i].y,
  875. present.rect[i].w,
  876. present.rect[i].h);
  877. u_wm_present(&present);
  878. }
  879. }
  880. }
  881. } else {
  882. drag_id = -1;
  883. }
  884. } else if (!down && was) {
  885. drag_id = -1;
  886. resize_id = -1;
  887. }
  888. prev_buttons = buttons;
  889. }
  890. static char *put_str(char *p, const char *s)
  891. {
  892. while (*s)
  893. *p++ = *s++;
  894. return p;
  895. }
  896. static char *put_dec(char *p, unsigned int v)
  897. {
  898. char b[10];
  899. unsigned int n = 0;
  900. do {
  901. b[n++] = (char)('0' + v % 10);
  902. v /= 10;
  903. } while (v);
  904. while (n)
  905. *p++ = b[--n];
  906. return p;
  907. }
  908. /* Expose the last interval in /share so the target can be measured without
  909. * a serial console. */
  910. static void write_stats(void)
  911. {
  912. char b[80], *p = b;
  913. unsigned int now = u_ticks_ms();
  914. p = put_str(p, "t=");
  915. p = put_dec(p, now / 1000);
  916. p = put_str(p, "s frames=");
  917. p = put_dec(p, stat_frames);
  918. p = put_str(p, " ms=");
  919. p = put_dec(p, stat_ms);
  920. p = put_str(p, " max=");
  921. p = put_dec(p, stat_max);
  922. p = put_str(p, " px=");
  923. p = put_dec(p, stat_px);
  924. p = put_str(p, "\n");
  925. u_syscall3(SYS_WRITE_FILE, (int)"/share/wmstats", (int)b,
  926. (int)(p - b));
  927. }
  928. void _start(const char *arg)
  929. {
  930. struct wm_screen_info si;
  931. struct wm_status st;
  932. unsigned int i;
  933. (void)arg;
  934. if (u_wm_attach(&si) != 0)
  935. u_exit(1);
  936. usurf_init(&screen, si.fb, si.width, si.height, si.pitch, si.bpp);
  937. fbw = si.width;
  938. fbh = si.height;
  939. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  940. wins[i].info.used = 0;
  941. wins[i].mapped = 0;
  942. }
  943. full_redraw = 1;
  944. for (;;) {
  945. unsigned int t0, t1;
  946. if (u_wm_poll(&st) != 0)
  947. u_exit(1);
  948. if (st.full_redraw)
  949. full_redraw = 1;
  950. usurf_pixels = 0;
  951. t0 = u_ticks_ms();
  952. handle_mouse(&st);
  953. do_update(&st);
  954. t1 = u_ticks_ms();
  955. if (usurf_pixels) {
  956. unsigned int dt = t1 - t0;
  957. stat_frames++;
  958. stat_ms += dt;
  959. stat_px += usurf_pixels;
  960. if (dt > stat_max)
  961. stat_max = dt;
  962. }
  963. if (t1 - stat_last >= 2000) {
  964. stat_last = t1;
  965. if (stat_frames) {
  966. write_stats();
  967. stat_frames = stat_ms = stat_px = stat_max = 0;
  968. }
  969. }
  970. u_sleep_ms(10);
  971. }
  972. }