ucmd_wm.c 34 KB

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