ucmd_wm.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  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 unsigned int bar_kexec_busy, bar_kexec_progress, bar_kexec_total;
  31. static int bar_temp_c = -1;
  32. static char bar_msg[24];
  33. static unsigned int bar_msg_until;
  34. static int bar_msg_dirty;
  35. static void bar_notice(const char *msg)
  36. {
  37. unsigned int i;
  38. for (i = 0; msg[i] && i + 1 < sizeof(bar_msg); i++)
  39. bar_msg[i] = msg[i];
  40. bar_msg[i] = '\0';
  41. bar_msg_until = u_ticks_ms() + 2000;
  42. bar_msg_dirty = 1;
  43. }
  44. static int free_window_slot(void)
  45. {
  46. unsigned int i;
  47. for (i = 0; i < WM_MAX_WINDOWS; i++)
  48. if (!wins[i].info.used)
  49. return 1;
  50. return 0;
  51. }
  52. static unsigned int rgb16(unsigned int r, unsigned int g, unsigned int b)
  53. {
  54. return (r << 16) | (g << 8) | b;
  55. }
  56. /* Rainbow gradient: hue 0..1535 (6*256) maps to R→Y→G→C→B→M→R, the same
  57. * integer interpolation the kernel bar uses. */
  58. static unsigned int rainbow_rgb(unsigned int hue)
  59. {
  60. unsigned int seg = hue / 256, f = hue % 256;
  61. unsigned int r1, g1, b1, r2, g2, b2;
  62. switch (seg) {
  63. case 0: r1=255; g1=0; b1=0; r2=255; g2=255; b2=0; break;
  64. case 1: r1=255; g1=255; b1=0; r2=0; g2=255; b2=0; break;
  65. case 2: r1=0; g1=255; b1=0; r2=0; g2=255; b2=255; break;
  66. case 3: r1=0; g1=255; b1=255; r2=0; g2=0; b2=255; break;
  67. case 4: r1=0; g1=0; b1=255; r2=255; g2=0; b2=255; break;
  68. default:r1=255; g1=0; b1=255; r2=255; g2=0; b2=0; break;
  69. }
  70. return rgb16((r1 * (256 - f) + r2 * f) / 256,
  71. (g1 * (256 - f) + g2 * f) / 256,
  72. (b1 * (256 - f) + b2 * f) / 256);
  73. }
  74. static void fill_clip(int x, int y, unsigned int w, unsigned int h,
  75. unsigned int rgb, int cx, int cy,
  76. unsigned int cw, unsigned int ch)
  77. {
  78. int x0 = x, y0 = y, x1 = x + (int)w, y1 = y + (int)h;
  79. if (x0 < cx) x0 = cx;
  80. if (y0 < cy) y0 = cy;
  81. if (x1 > cx + (int)cw) x1 = cx + (int)cw;
  82. if (y1 > cy + (int)ch) y1 = cy + (int)ch;
  83. if (x1 > x0 && y1 > y0)
  84. usurf_fill(&screen, x0, y0, (unsigned int)(x1 - x0),
  85. (unsigned int)(y1 - y0), rgb);
  86. }
  87. static void bar_button(unsigned int x, unsigned int y, unsigned int w,
  88. unsigned int h, const char *label)
  89. {
  90. unsigned int n = 0;
  91. usurf_fill(&screen, x, y, w, h, 0x963CD2u);
  92. while (label[n])
  93. n++;
  94. usurf_text(&screen, (int)(x + (w - n * 8) / 2), (int)(y + 3),
  95. label, COL_WHITE, 0, 0, fbw, WM_BAR_H);
  96. }
  97. static unsigned int temp_color(int c)
  98. {
  99. if (c >= 80)
  100. return 0xFF5050u;
  101. if (c >= 60)
  102. return 0xFFD250u;
  103. return 0x55FF55u;
  104. }
  105. /* "CPU 43C" (no libc printf here). */
  106. static unsigned int temp_text(char *b, int c)
  107. {
  108. unsigned int n = 0, v = (unsigned int)c;
  109. b[n++] = 'C';
  110. b[n++] = 'P';
  111. b[n++] = 'U';
  112. b[n++] = ' ';
  113. if (v >= 100)
  114. b[n++] = (char)('0' + (v / 100) % 10);
  115. if (v >= 10)
  116. b[n++] = (char)('0' + (v / 10) % 10);
  117. b[n++] = (char)('0' + v % 10);
  118. b[n++] = 'C';
  119. b[n] = '\0';
  120. return n;
  121. }
  122. static void draw_bar(void)
  123. {
  124. char dt[48];
  125. unsigned int n, i, x, y;
  126. usurf_fill(&screen, 0, 0, fbw, WM_BAR_H, COL_BG);
  127. usurf_text(&screen, 4, 5, OS_NAME " v" OS_VERSION, COL_BANNER,
  128. 0, 0, fbw, WM_BAR_H);
  129. bar_button(WM_BTN_X, WM_BTN_Y, WM_BTN_W, WM_BTN_H, "New");
  130. bar_button(WM_BTN_CLOCK_X, WM_BTN_CLOCK_Y, WM_BTN_CLOCK_W,
  131. WM_BTN_CLOCK_H, "Clock");
  132. bar_button(WM_BTN_COLORS_X, WM_BTN_COLORS_Y, WM_BTN_COLORS_W,
  133. WM_BTN_COLORS_H, "Colors");
  134. bar_button(WM_BTN_FILES_X, WM_BTN_FILES_Y, WM_BTN_FILES_W,
  135. WM_BTN_FILES_H, "Files");
  136. bar_button(WM_BTN_MINES_X, WM_BTN_MINES_Y, WM_BTN_MINES_W,
  137. WM_BTN_MINES_H, "Mines");
  138. bar_button(WM_BTN_WEB_X, WM_BTN_WEB_Y, WM_BTN_WEB_W,
  139. WM_BTN_WEB_H, "Web");
  140. bar_button(WM_BTN_CARDS_X, WM_BTN_CARDS_Y, WM_BTN_CARDS_W,
  141. WM_BTN_CARDS_H, "Cards");
  142. bar_button(WM_BTN_GALLERY_X, WM_BTN_GALLERY_Y, WM_BTN_GALLERY_W,
  143. WM_BTN_GALLERY_H, "Gallery");
  144. if (bar_msg_until &&
  145. (int)(u_ticks_ms() - bar_msg_until) < 0)
  146. usurf_text(&screen, (int)(WM_BTN_GALLERY_X + WM_BTN_GALLERY_W + 8),
  147. 5, bar_msg, 0xFFD250u, 0, 0, fbw, WM_BAR_H);
  148. /* Rainbow separator (same y as the kernel's old bar). */
  149. for (x = 0; x < fbw; x++) {
  150. unsigned int c = rainbow_rgb((x * (6 * 256)) / (fbw ? fbw : 1));
  151. for (y = WM_BAR_H - 5; y < WM_BAR_H - 3; y++)
  152. usurf_fill(&screen, (int)x, (int)y, 1, 1, c);
  153. }
  154. if (bar_kexec_busy) {
  155. static const char msg[] = "loading new kernel over serial";
  156. unsigned int dots = bar_kexec_total ?
  157. (bar_kexec_progress * 12 / bar_kexec_total) : 0;
  158. n = 0;
  159. while (msg[n])
  160. dt[n] = msg[n], n++;
  161. for (i = 0; i < 12; i++)
  162. dt[n + i] = (i < dots) ? '.' : ' ';
  163. n += 12;
  164. dt[n] = '\0';
  165. usurf_text(&screen, (int)fbw - (int)n * 8 - 4, 5, dt,
  166. 0xFFC864u, 0, 0, fbw, WM_BAR_H);
  167. } else {
  168. unsigned int clock_chars = 0;
  169. if (u_rtc_datetime(dt, sizeof(dt)) == 0) {
  170. n = 0;
  171. while (dt[n] && n < sizeof(dt) - 1)
  172. n++;
  173. usurf_text(&screen, (int)fbw - (int)n * 8 - 4, 5, dt,
  174. COL_BANNER, 0, 0, fbw, WM_BAR_H);
  175. clock_chars = n;
  176. }
  177. if (bar_temp_c >= 0) {
  178. char tb[12];
  179. unsigned int tn = temp_text(tb, bar_temp_c);
  180. int tx = (int)fbw - 4 - (int)clock_chars * 8 -
  181. (clock_chars ? 8 : 0) - (int)tn * 8;
  182. if (tx >= WM_BTN_GALLERY_X + (int)WM_BTN_GALLERY_W + 4)
  183. usurf_text(&screen, tx, 5, tb,
  184. temp_color(bar_temp_c), 0, 0,
  185. fbw, WM_BAR_H);
  186. }
  187. }
  188. }
  189. static void draw_window_clip(unsigned int idx, int cx, int cy,
  190. unsigned int cw, unsigned int ch)
  191. {
  192. struct uwin *u = &wins[idx];
  193. struct wm_window_info *w = &u->info;
  194. int x = w->x, y = w->y;
  195. unsigned int pw = w->w, ph = w->h;
  196. unsigned int bx = (unsigned int)(x + (int)pw - 1 - WM_CLOSE_W);
  197. unsigned int cony = (unsigned int)(y + 1 + WM_TITLE_H);
  198. unsigned int content_x = (unsigned int)(x + 1);
  199. unsigned int content_w = w->content_w;
  200. unsigned int content_h = w->content_h;
  201. /* Border, title bar, close button. */
  202. fill_clip(x, y, pw, ph, COL_BORDER, cx, cy, cw, ch);
  203. fill_clip(x + 1, y + 1, pw - 2, WM_TITLE_H,
  204. w->focused ? COL_TITLE_ON : COL_TITLE_OFF, cx, cy, cw, ch);
  205. usurf_text(&screen, x + 4, y + 3, w->title, COL_WHITE,
  206. cx, cy, cw, ch);
  207. fill_clip((int)bx, y + 1, WM_CLOSE_W, WM_TITLE_H, COL_CLOSE,
  208. cx, cy, cw, ch);
  209. usurf_text(&screen, (int)bx + 4, y + 3, "x", COL_WHITE,
  210. cx, cy, cw, ch);
  211. /* Content. */
  212. fill_clip((int)content_x, (int)cony, content_w, content_h,
  213. 0x000000u, cx, cy, cw, ch);
  214. if (u->mapped) {
  215. if (w->kind == WM_KIND_GFX) {
  216. struct uwm_canvas c;
  217. c.px = (const unsigned short *)u->map.va;
  218. c.w = u->map.w;
  219. c.h = u->map.h;
  220. usurf_canvas(&screen, &c, (int)content_x, (int)cony,
  221. cx, cy, cw, ch);
  222. } else if (u->map.kind == WM_KIND_TEXT) {
  223. usurf_cells(&screen, (const unsigned short *)u->map.va,
  224. w->cols, w->rows, (int)content_x, (int)cony,
  225. cx, cy, cw, ch);
  226. }
  227. }
  228. /* Text cursor overlay (only for the focused terminal). */
  229. if (w->kind == WM_KIND_TEXT && w->focused && w->cursor_visible &&
  230. w->cols && w->rows && w->cursor < w->cols * w->rows) {
  231. unsigned int col = w->cursor % w->cols;
  232. unsigned int row = w->cursor / w->cols;
  233. unsigned int px = content_x + col * 8;
  234. unsigned int py = cony + row * 10;
  235. fill_clip((int)px, (int)py, 2, 10, COL_WHITE,
  236. cx, cy, cw, ch);
  237. }
  238. /* Resize grip (bottom-right corner). */
  239. if (w->focused) {
  240. unsigned int i;
  241. for (i = 0; i < 10; i++) {
  242. fill_clip(x + (int)pw - 3 - (int)i,
  243. y + (int)ph - 4, 1, 1, 0xD2D2D2u,
  244. cx, cy, cw, ch);
  245. fill_clip(x + (int)pw - 4,
  246. y + (int)ph - 3 - (int)i, 1, 1, 0xD2D2D2u,
  247. cx, cy, cw, ch);
  248. }
  249. }
  250. }
  251. static void draw_rect(int x, int y, unsigned int w, unsigned int h)
  252. {
  253. unsigned int i;
  254. if (w == 0 || h == 0 || x >= (int)fbw || y >= (int)fbh)
  255. return;
  256. usurf_fill(&screen, x, y, w, h, COL_BG);
  257. for (i = 0; i < WM_MAX_WINDOWS; i++)
  258. if (wins[i].info.used && !wins[i].info.focused &&
  259. x < wins[i].info.x + (int)wins[i].info.w &&
  260. x + (int)w > wins[i].info.x &&
  261. y < wins[i].info.y + (int)wins[i].info.h &&
  262. y + (int)h > wins[i].info.y)
  263. draw_window_clip(i, x, y, w, h);
  264. for (i = 0; i < WM_MAX_WINDOWS; i++)
  265. if (wins[i].info.used && wins[i].info.focused &&
  266. x < wins[i].info.x + (int)wins[i].info.w &&
  267. x + (int)w > wins[i].info.x &&
  268. y < wins[i].info.y + (int)wins[i].info.h &&
  269. y + (int)h > wins[i].info.y)
  270. draw_window_clip(i, x, y, w, h);
  271. }
  272. static int window_at(unsigned int mx, unsigned int my)
  273. {
  274. unsigned int i;
  275. for (i = 0; i < WM_MAX_WINDOWS; i++)
  276. if (wins[i].info.used && wins[i].info.focused &&
  277. mx >= (unsigned int)wins[i].info.x &&
  278. my >= (unsigned int)wins[i].info.y &&
  279. mx < (unsigned int)(wins[i].info.x + (int)wins[i].info.w) &&
  280. my < (unsigned int)(wins[i].info.y + (int)wins[i].info.h))
  281. return (int)i;
  282. /* Inactive windows are painted in slot order, so the highest slot is
  283. * the topmost one under the cursor. */
  284. for (i = WM_MAX_WINDOWS; i-- > 0; )
  285. if (wins[i].info.used && !wins[i].info.focused &&
  286. mx >= (unsigned int)wins[i].info.x &&
  287. my >= (unsigned int)wins[i].info.y &&
  288. mx < (unsigned int)(wins[i].info.x + (int)wins[i].info.w) &&
  289. my < (unsigned int)(wins[i].info.y + (int)wins[i].info.h))
  290. return (int)i;
  291. return -1;
  292. }
  293. static void clamp_geom(struct uwin *u, int *x, int *y)
  294. {
  295. int maxx = (int)fbw - (int)u->info.w;
  296. int maxy = (int)fbh - (int)u->info.h;
  297. if (*x < 0) *x = 0;
  298. if (*y < WM_BAR_H) *y = WM_BAR_H;
  299. if (maxx < 0) maxx = 0;
  300. if (maxy < WM_BAR_H) maxy = WM_BAR_H;
  301. if (*x > maxx) *x = maxx;
  302. if (*y > maxy) *y = maxy;
  303. }
  304. static void add_rect(struct wm_present_info *p, int x, int y,
  305. unsigned int w, unsigned int h)
  306. {
  307. if (w == 0 || h == 0)
  308. return;
  309. if (p->count < 4) {
  310. p->rect[p->count].x = x;
  311. p->rect[p->count].y = y;
  312. p->rect[p->count].w = w;
  313. p->rect[p->count].h = h;
  314. p->count++;
  315. } else {
  316. /* Four is enough for normal updates; fold the rest into the
  317. * last rect rather than dropping pixels. */
  318. int x0 = p->rect[3].x, y0 = p->rect[3].y;
  319. int x1 = x0 + (int)p->rect[3].w, y1 = y0 + (int)p->rect[3].h;
  320. int nx1 = x + (int)w, ny1 = y + (int)h;
  321. if (x < x0) x0 = x;
  322. if (y < y0) y0 = y;
  323. if (nx1 > x1) x1 = nx1;
  324. if (ny1 > y1) y1 = ny1;
  325. p->rect[3].x = x0;
  326. p->rect[3].y = y0;
  327. p->rect[3].w = (unsigned int)(x1 - x0);
  328. p->rect[3].h = (unsigned int)(y1 - y0);
  329. }
  330. }
  331. static void do_update(struct wm_status *st)
  332. {
  333. struct wm_present_info present;
  334. int need = 0;
  335. int bar_dirty = 0;
  336. unsigned int i;
  337. char dt[20];
  338. static char last_dt[20];
  339. int kexec_changed = (st->kexec_busy != bar_kexec_busy) ||
  340. (st->kexec_busy && st->kexec_progress != bar_kexec_progress);
  341. unsigned int msg_now = u_ticks_ms();
  342. if (bar_msg_dirty ||
  343. (bar_msg_until && (int)(msg_now - bar_msg_until) >= 0)) {
  344. bar_dirty = 1;
  345. bar_msg_dirty = 0;
  346. if (bar_msg_until && (int)(msg_now - bar_msg_until) >= 0)
  347. bar_msg_until = 0;
  348. }
  349. bar_kexec_busy = st->kexec_busy;
  350. bar_kexec_progress = st->kexec_progress;
  351. bar_kexec_total = st->kexec_total;
  352. if (kexec_changed)
  353. bar_dirty = 1;
  354. /* Refresh cached window state and mappings before opening a frame:
  355. * MAP may unmap/remap content. */
  356. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  357. struct wm_window_info info;
  358. int r = u_wm_info(i, &info);
  359. if (r < 0) {
  360. u_exit(1);
  361. }
  362. if (r == 0 || !info.used) {
  363. if (wins[i].info.used)
  364. full_redraw = 1;
  365. wins[i].info.used = 0;
  366. wins[i].mapped = 0;
  367. continue;
  368. }
  369. if (!wins[i].info.used || info.content_gen != wins[i].info.content_gen ||
  370. info.kind != wins[i].info.kind)
  371. full_redraw = 1;
  372. wins[i].info = info;
  373. if (!wins[i].mapped || wins[i].map.kind != info.kind ||
  374. wins[i].map.generation != info.content_gen) {
  375. struct wm_map_info map;
  376. if (u_wm_map(i, &map) == 0) {
  377. wins[i].map = map;
  378. wins[i].mapped = 1;
  379. } else {
  380. wins[i].mapped = 0;
  381. }
  382. }
  383. }
  384. /* The RTC is slow (CMOS polling) and the temperature only changes
  385. * once a second; query both at 1 Hz instead of every 10 ms. */
  386. {
  387. static unsigned int last_rtc_ms;
  388. unsigned int now_ms = u_ticks_ms();
  389. if (full_redraw || now_ms - last_rtc_ms >= 1000) {
  390. last_rtc_ms = now_ms;
  391. if (u_rtc_datetime(dt, sizeof(dt)) == 0) {
  392. for (i = 0; i < sizeof(dt); i++)
  393. if (dt[i] != last_dt[i]) {
  394. for (i = 0; i < sizeof(dt); i++)
  395. last_dt[i] = dt[i];
  396. bar_dirty = 1;
  397. break;
  398. }
  399. }
  400. {
  401. struct sys_cputemp ct;
  402. if (u_cpu_temp(&ct) == 0 &&
  403. ct.temp_c != bar_temp_c) {
  404. bar_temp_c = ct.temp_c;
  405. bar_dirty = 1;
  406. }
  407. }
  408. }
  409. }
  410. if (full_redraw) {
  411. if (u_wm_begin() != 0) {
  412. full_redraw = 1;
  413. return;
  414. }
  415. full_redraw = 0;
  416. need = 1;
  417. {
  418. usurf_fill(&screen, 0, 0, fbw, fbh, COL_BG);
  419. for (i = 0; i < WM_MAX_WINDOWS; i++)
  420. if (wins[i].info.used && !wins[i].info.focused)
  421. draw_window_clip(i, 0, 0, fbw, fbh);
  422. for (i = 0; i < WM_MAX_WINDOWS; i++)
  423. if (wins[i].info.used && wins[i].info.focused)
  424. draw_window_clip(i, 0, 0, fbw, fbh);
  425. draw_bar();
  426. present.count = 0;
  427. add_rect(&present, 0, 0, fbw, fbh);
  428. present.immediate = 0;
  429. u_wm_present(&present);
  430. /* Ack every window that was clean at the time of the
  431. * redraw. If new damage arrived during it, its seq
  432. * differs and the kernel leaves it dirty. */
  433. for (i = 0; i < WM_MAX_WINDOWS; i++)
  434. if (wins[i].info.used && wins[i].mapped)
  435. u_wm_ack(i, wins[i].info.dirty_seq);
  436. }
  437. return;
  438. }
  439. /* Partial cell/canvas updates and the bar clock. */
  440. present.count = 0;
  441. present.immediate = 0;
  442. if (bar_dirty) {
  443. need = 1;
  444. add_rect(&present, 0, 0, fbw, WM_BAR_H);
  445. }
  446. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  447. struct uwin *u = &wins[i];
  448. struct wm_window_info *w = &u->info;
  449. int rx, ry;
  450. unsigned int rw, rh;
  451. if (!w->used || !w->dirty)
  452. continue;
  453. if (w->kind == WM_KIND_GFX) {
  454. rx = w->x + 1;
  455. ry = w->y + 1 + WM_TITLE_H;
  456. rw = w->content_w;
  457. rh = w->content_h;
  458. } else {
  459. if (!w->dmg_cols || !w->dmg_rows)
  460. continue;
  461. rx = w->x + 1 + (int)w->dmg_col0 * 8;
  462. ry = w->y + 1 + WM_TITLE_H + (int)w->dmg_row0 * 10;
  463. rw = w->dmg_cols * 8;
  464. rh = w->dmg_rows * 10;
  465. }
  466. add_rect(&present, rx, ry, rw, rh);
  467. u->ack_seq = u->mapped ? w->dirty_seq : 0;
  468. need = 1;
  469. }
  470. if (!need) {
  471. /* Drag movement is handled below; a stationary mouse can be
  472. * left to the kernel's cursor overlay. */
  473. return;
  474. }
  475. if (u_wm_begin() != 0)
  476. return;
  477. for (i = 0; i < present.count; i++)
  478. draw_rect(present.rect[i].x, present.rect[i].y,
  479. present.rect[i].w, present.rect[i].h);
  480. if (bar_dirty)
  481. draw_bar();
  482. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  483. struct uwin *u = &wins[i];
  484. if (!u->info.used || !u->info.dirty || !u->mapped ||
  485. !u->ack_seq)
  486. continue;
  487. if (u->ack_seq == u->info.dirty_seq)
  488. u_wm_ack(i, u->info.dirty_seq);
  489. }
  490. u_wm_present(&present);
  491. }
  492. static int spawn_app(const char *path)
  493. {
  494. int pid = u_spawn(path, "", 0, "/");
  495. if (pid > 0)
  496. full_redraw = 1;
  497. return pid;
  498. }
  499. static void spawn_app_checked(const char *path)
  500. {
  501. if (!free_window_slot()) {
  502. bar_notice("window limit");
  503. full_redraw = 1;
  504. return;
  505. }
  506. if (spawn_app(path) < 0) {
  507. bar_notice("spawn failed");
  508. full_redraw = 1;
  509. }
  510. }
  511. static void handle_mouse(struct wm_status *st)
  512. {
  513. unsigned int buttons = st->mouse.buttons;
  514. unsigned int mx = st->mouse.x, my = st->mouse.y;
  515. int down = (buttons & 1) != 0;
  516. int was = (prev_buttons & 1) != 0;
  517. if (down && !was) {
  518. if (mx >= WM_BTN_X && my >= WM_BTN_Y &&
  519. mx < WM_BTN_X + WM_BTN_W && my < WM_BTN_Y + WM_BTN_H) {
  520. if (!free_window_slot()) {
  521. bar_notice("window limit");
  522. full_redraw = 1;
  523. } else if (u_wm_create_term() < 0) {
  524. bar_notice("cannot start term");
  525. full_redraw = 1;
  526. } else {
  527. full_redraw = 1;
  528. }
  529. } else if (mx >= WM_BTN_CLOCK_X && my >= WM_BTN_CLOCK_Y &&
  530. mx < WM_BTN_CLOCK_X + WM_BTN_CLOCK_W &&
  531. my < WM_BTN_CLOCK_Y + WM_BTN_CLOCK_H) {
  532. spawn_app_checked("/ramfs/bin/clock");
  533. } else if (mx >= WM_BTN_COLORS_X && my >= WM_BTN_COLORS_Y &&
  534. mx < WM_BTN_COLORS_X + WM_BTN_COLORS_W &&
  535. my < WM_BTN_COLORS_Y + WM_BTN_COLORS_H) {
  536. spawn_app_checked("/ramfs/bin/colors");
  537. } else if (mx >= WM_BTN_FILES_X && my >= WM_BTN_FILES_Y &&
  538. mx < WM_BTN_FILES_X + WM_BTN_FILES_W &&
  539. my < WM_BTN_FILES_Y + WM_BTN_FILES_H) {
  540. spawn_app_checked("/ramfs/bin/files");
  541. } else if (mx >= WM_BTN_MINES_X && my >= WM_BTN_MINES_Y &&
  542. mx < WM_BTN_MINES_X + WM_BTN_MINES_W &&
  543. my < WM_BTN_MINES_Y + WM_BTN_MINES_H) {
  544. spawn_app_checked("/ramfs/bin/mines");
  545. } else if (mx >= WM_BTN_WEB_X && my >= WM_BTN_WEB_Y &&
  546. mx < WM_BTN_WEB_X + WM_BTN_WEB_W &&
  547. my < WM_BTN_WEB_Y + WM_BTN_WEB_H) {
  548. spawn_app_checked("/ramfs/bin/browser");
  549. } else if (mx >= WM_BTN_CARDS_X && my >= WM_BTN_CARDS_Y &&
  550. mx < WM_BTN_CARDS_X + WM_BTN_CARDS_W &&
  551. my < WM_BTN_CARDS_Y + WM_BTN_CARDS_H) {
  552. spawn_app_checked("/ramfs/bin/solitaire");
  553. } else if (mx >= WM_BTN_GALLERY_X && my >= WM_BTN_GALLERY_Y &&
  554. mx < WM_BTN_GALLERY_X + WM_BTN_GALLERY_W &&
  555. my < WM_BTN_GALLERY_Y + WM_BTN_GALLERY_H) {
  556. spawn_app_checked("/ramfs/bin/gallery");
  557. } else {
  558. int idx = window_at(mx, my);
  559. if (idx >= 0) {
  560. struct uwin *u = &wins[idx];
  561. u_wm_focus((unsigned int)idx);
  562. if (mx >= (unsigned int)(u->info.x + (int)u->info.w -
  563. 1 - WM_CLOSE_W) &&
  564. mx < (unsigned int)(u->info.x + (int)u->info.w) &&
  565. my >= (unsigned int)(u->info.y + 1) &&
  566. my < (unsigned int)(u->info.y + 1 + WM_TITLE_H)) {
  567. u_wm_close((unsigned int)idx);
  568. full_redraw = 1;
  569. } else if (mx >= (unsigned int)(u->info.x + (int)u->info.w - 16) &&
  570. my >= (unsigned int)(u->info.y + (int)u->info.h - 16)) {
  571. resize_id = idx;
  572. resize_mx = mx;
  573. resize_my = my;
  574. resize_ow = u->info.w;
  575. resize_oh = u->info.h;
  576. } else if (my < (unsigned int)(u->info.y + WM_TITLE_H)) {
  577. drag_id = idx;
  578. drag_dx = (int)mx - u->info.x;
  579. drag_dy = (int)my - u->info.y;
  580. }
  581. }
  582. }
  583. } else if (down && was && resize_id >= 0) {
  584. struct uwin *u = &wins[resize_id];
  585. if (u->info.used) {
  586. unsigned int oldw = u->info.w, oldh = u->info.h;
  587. int nw = (int)resize_ow + (int)mx - (int)resize_mx;
  588. int nh = (int)resize_oh + (int)my - (int)resize_my;
  589. int maxw = (int)fbw - u->info.x;
  590. int maxh = (int)fbh - u->info.y;
  591. if (nw < 200) nw = 200;
  592. if (nh < 140) nh = 140;
  593. if (nw > maxw) nw = maxw;
  594. if (nh > maxh) nh = maxh;
  595. if (nw != (int)u->info.w || nh != (int)u->info.h) {
  596. struct wm_window_info ni;
  597. struct wm_map_info nm;
  598. struct wm_present_info present;
  599. unsigned int i;
  600. if (u_wm_resize((unsigned int)resize_id,
  601. (unsigned int)(nw - 2),
  602. (unsigned int)(nh - WM_TITLE_H - 2)) == 0) {
  603. if (u_wm_info((unsigned int)resize_id, &ni) == 1)
  604. u->info = ni;
  605. u->mapped = 0;
  606. if (u_wm_map((unsigned int)resize_id, &nm) == 0) {
  607. u->map = nm;
  608. u->mapped = 1;
  609. }
  610. present.count = 0;
  611. present.immediate = 1;
  612. add_rect(&present, u->info.x, u->info.y, oldw, oldh);
  613. add_rect(&present, u->info.x, u->info.y,
  614. u->info.w, u->info.h);
  615. if (u_wm_begin() == 0) {
  616. for (i = 0; i < present.count; i++)
  617. draw_rect(present.rect[i].x,
  618. present.rect[i].y,
  619. present.rect[i].w,
  620. present.rect[i].h);
  621. u_wm_present(&present);
  622. }
  623. }
  624. }
  625. } else {
  626. resize_id = -1;
  627. }
  628. } else if (down && was && drag_id >= 0) {
  629. struct uwin *u = &wins[drag_id];
  630. if (u->info.used) {
  631. int ox = u->info.x, oy = u->info.y;
  632. int nx = (int)mx - drag_dx;
  633. int ny = (int)my - drag_dy;
  634. clamp_geom(u, &nx, &ny);
  635. if (nx != ox || ny != oy) {
  636. struct wm_present_info present;
  637. unsigned int i;
  638. u_wm_set_geom((unsigned int)drag_id, nx, ny);
  639. u->info.x = nx;
  640. u->info.y = ny;
  641. present.count = 0;
  642. present.immediate = 1;
  643. add_rect(&present, ox, oy, u->info.w, u->info.h);
  644. add_rect(&present, nx, ny, u->info.w, u->info.h);
  645. if (u_wm_begin() == 0) {
  646. for (i = 0; i < present.count; i++)
  647. draw_rect(present.rect[i].x,
  648. present.rect[i].y,
  649. present.rect[i].w,
  650. present.rect[i].h);
  651. u_wm_present(&present);
  652. }
  653. }
  654. } else {
  655. drag_id = -1;
  656. }
  657. } else if (!down && was) {
  658. drag_id = -1;
  659. resize_id = -1;
  660. }
  661. prev_buttons = buttons;
  662. }
  663. void _start(const char *arg)
  664. {
  665. struct wm_screen_info si;
  666. struct wm_status st;
  667. unsigned int i;
  668. (void)arg;
  669. if (u_wm_attach(&si) != 0)
  670. u_exit(1);
  671. usurf_init(&screen, si.fb, si.width, si.height, si.pitch, si.bpp);
  672. fbw = si.width;
  673. fbh = si.height;
  674. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  675. wins[i].info.used = 0;
  676. wins[i].mapped = 0;
  677. }
  678. full_redraw = 1;
  679. for (;;) {
  680. if (u_wm_poll(&st) != 0)
  681. u_exit(1);
  682. if (st.full_redraw)
  683. full_redraw = 1;
  684. handle_mouse(&st);
  685. do_update(&st);
  686. u_sleep_ms(10);
  687. }
  688. }