ucmd_wm.c 29 KB

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