ucmd_wm.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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: every window presents an RGB565 canvas (the terminal
  212. * renders its cells into one; gfx commands own theirs). */
  213. fill_clip((int)content_x, (int)cony, content_w, content_h,
  214. 0x000000u, cx, cy, cw, ch);
  215. if (u->mapped && u->map.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. }
  223. /* Resize grip (bottom-right corner). */
  224. if (w->focused) {
  225. unsigned int i;
  226. for (i = 0; i < 10; i++) {
  227. fill_clip(x + (int)pw - 3 - (int)i,
  228. y + (int)ph - 4, 1, 1, 0xD2D2D2u,
  229. cx, cy, cw, ch);
  230. fill_clip(x + (int)pw - 4,
  231. y + (int)ph - 3 - (int)i, 1, 1, 0xD2D2D2u,
  232. cx, cy, cw, ch);
  233. }
  234. }
  235. }
  236. static void draw_rect(int x, int y, unsigned int w, unsigned int h)
  237. {
  238. unsigned int i;
  239. if (w == 0 || h == 0 || x >= (int)fbw || y >= (int)fbh)
  240. return;
  241. usurf_fill(&screen, x, y, w, h, COL_BG);
  242. for (i = 0; i < WM_MAX_WINDOWS; i++)
  243. if (wins[i].info.used && !wins[i].info.focused &&
  244. x < wins[i].info.x + (int)wins[i].info.w &&
  245. x + (int)w > wins[i].info.x &&
  246. y < wins[i].info.y + (int)wins[i].info.h &&
  247. y + (int)h > wins[i].info.y)
  248. draw_window_clip(i, x, y, w, h);
  249. for (i = 0; i < WM_MAX_WINDOWS; i++)
  250. if (wins[i].info.used && wins[i].info.focused &&
  251. x < wins[i].info.x + (int)wins[i].info.w &&
  252. x + (int)w > wins[i].info.x &&
  253. y < wins[i].info.y + (int)wins[i].info.h &&
  254. y + (int)h > wins[i].info.y)
  255. draw_window_clip(i, x, y, w, h);
  256. }
  257. static int window_at(unsigned int mx, unsigned int my)
  258. {
  259. unsigned int i;
  260. for (i = 0; i < WM_MAX_WINDOWS; i++)
  261. if (wins[i].info.used && wins[i].info.focused &&
  262. mx >= (unsigned int)wins[i].info.x &&
  263. my >= (unsigned int)wins[i].info.y &&
  264. mx < (unsigned int)(wins[i].info.x + (int)wins[i].info.w) &&
  265. my < (unsigned int)(wins[i].info.y + (int)wins[i].info.h))
  266. return (int)i;
  267. /* Inactive windows are painted in slot order, so the highest slot is
  268. * the topmost one under the cursor. */
  269. for (i = WM_MAX_WINDOWS; i-- > 0; )
  270. if (wins[i].info.used && !wins[i].info.focused &&
  271. mx >= (unsigned int)wins[i].info.x &&
  272. my >= (unsigned int)wins[i].info.y &&
  273. mx < (unsigned int)(wins[i].info.x + (int)wins[i].info.w) &&
  274. my < (unsigned int)(wins[i].info.y + (int)wins[i].info.h))
  275. return (int)i;
  276. return -1;
  277. }
  278. static void clamp_geom(struct uwin *u, int *x, int *y)
  279. {
  280. int maxx = (int)fbw - (int)u->info.w;
  281. int maxy = (int)fbh - (int)u->info.h;
  282. if (*x < 0) *x = 0;
  283. if (*y < WM_BAR_H) *y = WM_BAR_H;
  284. if (maxx < 0) maxx = 0;
  285. if (maxy < WM_BAR_H) maxy = WM_BAR_H;
  286. if (*x > maxx) *x = maxx;
  287. if (*y > maxy) *y = maxy;
  288. }
  289. static void add_rect(struct wm_present_info *p, int x, int y,
  290. unsigned int w, unsigned int h)
  291. {
  292. if (w == 0 || h == 0)
  293. return;
  294. if (p->count < 4) {
  295. p->rect[p->count].x = x;
  296. p->rect[p->count].y = y;
  297. p->rect[p->count].w = w;
  298. p->rect[p->count].h = h;
  299. p->count++;
  300. } else {
  301. /* Four is enough for normal updates; fold the rest into the
  302. * last rect rather than dropping pixels. */
  303. int x0 = p->rect[3].x, y0 = p->rect[3].y;
  304. int x1 = x0 + (int)p->rect[3].w, y1 = y0 + (int)p->rect[3].h;
  305. int nx1 = x + (int)w, ny1 = y + (int)h;
  306. if (x < x0) x0 = x;
  307. if (y < y0) y0 = y;
  308. if (nx1 > x1) x1 = nx1;
  309. if (ny1 > y1) y1 = ny1;
  310. p->rect[3].x = x0;
  311. p->rect[3].y = y0;
  312. p->rect[3].w = (unsigned int)(x1 - x0);
  313. p->rect[3].h = (unsigned int)(y1 - y0);
  314. }
  315. }
  316. static void do_update(struct wm_status *st)
  317. {
  318. struct wm_present_info present;
  319. int need = 0;
  320. int bar_dirty = 0;
  321. unsigned int i;
  322. char dt[20];
  323. static char last_dt[20];
  324. int kexec_changed = (st->kexec_busy != bar_kexec_busy) ||
  325. (st->kexec_busy && st->kexec_progress != bar_kexec_progress);
  326. unsigned int msg_now = u_ticks_ms();
  327. if (bar_msg_dirty ||
  328. (bar_msg_until && (int)(msg_now - bar_msg_until) >= 0)) {
  329. bar_dirty = 1;
  330. bar_msg_dirty = 0;
  331. if (bar_msg_until && (int)(msg_now - bar_msg_until) >= 0)
  332. bar_msg_until = 0;
  333. }
  334. bar_kexec_busy = st->kexec_busy;
  335. bar_kexec_progress = st->kexec_progress;
  336. bar_kexec_total = st->kexec_total;
  337. if (kexec_changed)
  338. bar_dirty = 1;
  339. /* Refresh cached window state and mappings before opening a frame:
  340. * MAP may unmap/remap content. */
  341. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  342. struct wm_window_info info;
  343. int r = u_wm_info(i, &info);
  344. if (r < 0) {
  345. u_exit(1);
  346. }
  347. if (r == 0 || !info.used) {
  348. if (wins[i].info.used)
  349. full_redraw = 1;
  350. wins[i].info.used = 0;
  351. wins[i].mapped = 0;
  352. continue;
  353. }
  354. if (!wins[i].info.used ||
  355. info.content_gen != wins[i].info.content_gen)
  356. full_redraw = 1;
  357. wins[i].info = info;
  358. if (!wins[i].mapped ||
  359. wins[i].map.generation != info.content_gen) {
  360. struct wm_map_info map;
  361. if (u_wm_map(i, &map) == 0) {
  362. wins[i].map = map;
  363. wins[i].mapped = 1;
  364. } else {
  365. wins[i].mapped = 0;
  366. }
  367. }
  368. }
  369. /* The RTC is slow (CMOS polling) and the temperature only changes
  370. * once a second; query both at 1 Hz instead of every 10 ms. */
  371. {
  372. static unsigned int last_rtc_ms;
  373. unsigned int now_ms = u_ticks_ms();
  374. if (full_redraw || now_ms - last_rtc_ms >= 1000) {
  375. last_rtc_ms = now_ms;
  376. if (u_rtc_datetime(dt, sizeof(dt)) == 0) {
  377. for (i = 0; i < sizeof(dt); i++)
  378. if (dt[i] != last_dt[i]) {
  379. for (i = 0; i < sizeof(dt); i++)
  380. last_dt[i] = dt[i];
  381. bar_dirty = 1;
  382. break;
  383. }
  384. }
  385. {
  386. struct sys_cputemp ct;
  387. if (u_cpu_temp(&ct) == 0 &&
  388. ct.temp_c != bar_temp_c) {
  389. bar_temp_c = ct.temp_c;
  390. bar_dirty = 1;
  391. }
  392. }
  393. }
  394. }
  395. if (full_redraw) {
  396. if (u_wm_begin() != 0) {
  397. full_redraw = 1;
  398. return;
  399. }
  400. full_redraw = 0;
  401. need = 1;
  402. {
  403. usurf_fill(&screen, 0, 0, fbw, fbh, COL_BG);
  404. for (i = 0; i < WM_MAX_WINDOWS; i++)
  405. if (wins[i].info.used && !wins[i].info.focused)
  406. draw_window_clip(i, 0, 0, fbw, fbh);
  407. for (i = 0; i < WM_MAX_WINDOWS; i++)
  408. if (wins[i].info.used && wins[i].info.focused)
  409. draw_window_clip(i, 0, 0, fbw, fbh);
  410. draw_bar();
  411. present.count = 0;
  412. add_rect(&present, 0, 0, fbw, fbh);
  413. present.immediate = 0;
  414. u_wm_present(&present);
  415. /* Ack every window that was clean at the time of the
  416. * redraw. If new damage arrived during it, its seq
  417. * differs and the kernel leaves it dirty. */
  418. for (i = 0; i < WM_MAX_WINDOWS; i++)
  419. if (wins[i].info.used && wins[i].mapped)
  420. u_wm_ack(i, wins[i].info.dirty_seq);
  421. }
  422. return;
  423. }
  424. /* Partial cell/canvas updates and the bar clock. */
  425. present.count = 0;
  426. present.immediate = 0;
  427. if (bar_dirty) {
  428. need = 1;
  429. add_rect(&present, 0, 0, fbw, WM_BAR_H);
  430. }
  431. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  432. struct uwin *u = &wins[i];
  433. struct wm_window_info *w = &u->info;
  434. int rx, ry;
  435. unsigned int rw, rh;
  436. if (!w->used || !w->dirty)
  437. continue;
  438. if (!w->dmg_w || !w->dmg_h)
  439. continue;
  440. rx = w->x + 1 + (int)w->dmg_x;
  441. ry = w->y + 1 + WM_TITLE_H + (int)w->dmg_y;
  442. rw = w->dmg_w;
  443. rh = w->dmg_h;
  444. add_rect(&present, rx, ry, rw, rh);
  445. u->ack_seq = u->mapped ? w->dirty_seq : 0;
  446. need = 1;
  447. }
  448. if (!need) {
  449. /* Drag movement is handled below; a stationary mouse can be
  450. * left to the kernel's cursor overlay. */
  451. return;
  452. }
  453. if (u_wm_begin() != 0)
  454. return;
  455. for (i = 0; i < present.count; i++)
  456. draw_rect(present.rect[i].x, present.rect[i].y,
  457. present.rect[i].w, present.rect[i].h);
  458. if (bar_dirty)
  459. draw_bar();
  460. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  461. struct uwin *u = &wins[i];
  462. if (!u->info.used || !u->info.dirty || !u->mapped ||
  463. !u->ack_seq)
  464. continue;
  465. if (u->ack_seq == u->info.dirty_seq)
  466. u_wm_ack(i, u->info.dirty_seq);
  467. }
  468. u_wm_present(&present);
  469. }
  470. static int spawn_app(const char *path)
  471. {
  472. int pid = u_spawn(path, "", 0, "/");
  473. if (pid > 0)
  474. full_redraw = 1;
  475. return pid;
  476. }
  477. static void spawn_app_checked(const char *path)
  478. {
  479. if (!free_window_slot()) {
  480. bar_notice("window limit");
  481. full_redraw = 1;
  482. return;
  483. }
  484. if (spawn_app(path) < 0) {
  485. bar_notice("spawn failed");
  486. full_redraw = 1;
  487. }
  488. }
  489. static void handle_mouse(struct wm_status *st)
  490. {
  491. unsigned int buttons = st->mouse.buttons;
  492. unsigned int mx = st->mouse.x, my = st->mouse.y;
  493. int down = (buttons & 1) != 0;
  494. int was = (prev_buttons & 1) != 0;
  495. if (down && !was) {
  496. if (mx >= WM_BTN_X && my >= WM_BTN_Y &&
  497. mx < WM_BTN_X + WM_BTN_W && my < WM_BTN_Y + WM_BTN_H) {
  498. if (!free_window_slot()) {
  499. bar_notice("window limit");
  500. full_redraw = 1;
  501. } else if (u_wm_create_term() < 0) {
  502. bar_notice("cannot start term");
  503. full_redraw = 1;
  504. } else {
  505. full_redraw = 1;
  506. }
  507. } else if (mx >= WM_BTN_CLOCK_X && my >= WM_BTN_CLOCK_Y &&
  508. mx < WM_BTN_CLOCK_X + WM_BTN_CLOCK_W &&
  509. my < WM_BTN_CLOCK_Y + WM_BTN_CLOCK_H) {
  510. spawn_app_checked("/ramfs/bin/clock");
  511. } else if (mx >= WM_BTN_COLORS_X && my >= WM_BTN_COLORS_Y &&
  512. mx < WM_BTN_COLORS_X + WM_BTN_COLORS_W &&
  513. my < WM_BTN_COLORS_Y + WM_BTN_COLORS_H) {
  514. spawn_app_checked("/ramfs/bin/colors");
  515. } else if (mx >= WM_BTN_FILES_X && my >= WM_BTN_FILES_Y &&
  516. mx < WM_BTN_FILES_X + WM_BTN_FILES_W &&
  517. my < WM_BTN_FILES_Y + WM_BTN_FILES_H) {
  518. spawn_app_checked("/ramfs/bin/files");
  519. } else if (mx >= WM_BTN_MINES_X && my >= WM_BTN_MINES_Y &&
  520. mx < WM_BTN_MINES_X + WM_BTN_MINES_W &&
  521. my < WM_BTN_MINES_Y + WM_BTN_MINES_H) {
  522. spawn_app_checked("/ramfs/bin/mines");
  523. } else if (mx >= WM_BTN_WEB_X && my >= WM_BTN_WEB_Y &&
  524. mx < WM_BTN_WEB_X + WM_BTN_WEB_W &&
  525. my < WM_BTN_WEB_Y + WM_BTN_WEB_H) {
  526. spawn_app_checked("/ramfs/bin/browser");
  527. } else if (mx >= WM_BTN_CARDS_X && my >= WM_BTN_CARDS_Y &&
  528. mx < WM_BTN_CARDS_X + WM_BTN_CARDS_W &&
  529. my < WM_BTN_CARDS_Y + WM_BTN_CARDS_H) {
  530. spawn_app_checked("/ramfs/bin/solitaire");
  531. } else if (mx >= WM_BTN_GALLERY_X && my >= WM_BTN_GALLERY_Y &&
  532. mx < WM_BTN_GALLERY_X + WM_BTN_GALLERY_W &&
  533. my < WM_BTN_GALLERY_Y + WM_BTN_GALLERY_H) {
  534. spawn_app_checked("/ramfs/bin/gallery");
  535. } else {
  536. int idx = window_at(mx, my);
  537. if (idx >= 0) {
  538. struct uwin *u = &wins[idx];
  539. u_wm_focus((unsigned int)idx);
  540. if (mx >= (unsigned int)(u->info.x + (int)u->info.w -
  541. 1 - WM_CLOSE_W) &&
  542. mx < (unsigned int)(u->info.x + (int)u->info.w) &&
  543. my >= (unsigned int)(u->info.y + 1) &&
  544. my < (unsigned int)(u->info.y + 1 + WM_TITLE_H)) {
  545. u_wm_close((unsigned int)idx);
  546. full_redraw = 1;
  547. } else if (mx >= (unsigned int)(u->info.x + (int)u->info.w - 16) &&
  548. my >= (unsigned int)(u->info.y + (int)u->info.h - 16)) {
  549. resize_id = idx;
  550. resize_mx = mx;
  551. resize_my = my;
  552. resize_ow = u->info.w;
  553. resize_oh = u->info.h;
  554. } else if (my < (unsigned int)(u->info.y + WM_TITLE_H)) {
  555. drag_id = idx;
  556. drag_dx = (int)mx - u->info.x;
  557. drag_dy = (int)my - u->info.y;
  558. }
  559. }
  560. }
  561. } else if (down && was && resize_id >= 0) {
  562. struct uwin *u = &wins[resize_id];
  563. if (u->info.used) {
  564. unsigned int oldw = u->info.w, oldh = u->info.h;
  565. int nw = (int)resize_ow + (int)mx - (int)resize_mx;
  566. int nh = (int)resize_oh + (int)my - (int)resize_my;
  567. int maxw = (int)fbw - u->info.x;
  568. int maxh = (int)fbh - u->info.y;
  569. if (nw < 200) nw = 200;
  570. if (nh < 140) nh = 140;
  571. if (nw > maxw) nw = maxw;
  572. if (nh > maxh) nh = maxh;
  573. if (nw != (int)u->info.w || nh != (int)u->info.h) {
  574. struct wm_window_info ni;
  575. struct wm_map_info nm;
  576. struct wm_present_info present;
  577. unsigned int i;
  578. if (u_wm_resize((unsigned int)resize_id,
  579. (unsigned int)(nw - 2),
  580. (unsigned int)(nh - WM_TITLE_H - 2)) == 0) {
  581. if (u_wm_info((unsigned int)resize_id, &ni) == 1)
  582. u->info = ni;
  583. u->mapped = 0;
  584. if (u_wm_map((unsigned int)resize_id, &nm) == 0) {
  585. u->map = nm;
  586. u->mapped = 1;
  587. }
  588. present.count = 0;
  589. present.immediate = 1;
  590. add_rect(&present, u->info.x, u->info.y, oldw, oldh);
  591. add_rect(&present, u->info.x, u->info.y,
  592. u->info.w, u->info.h);
  593. if (u_wm_begin() == 0) {
  594. for (i = 0; i < present.count; i++)
  595. draw_rect(present.rect[i].x,
  596. present.rect[i].y,
  597. present.rect[i].w,
  598. present.rect[i].h);
  599. u_wm_present(&present);
  600. }
  601. }
  602. }
  603. } else {
  604. resize_id = -1;
  605. }
  606. } else if (down && was && drag_id >= 0) {
  607. struct uwin *u = &wins[drag_id];
  608. if (u->info.used) {
  609. int ox = u->info.x, oy = u->info.y;
  610. int nx = (int)mx - drag_dx;
  611. int ny = (int)my - drag_dy;
  612. clamp_geom(u, &nx, &ny);
  613. if (nx != ox || ny != oy) {
  614. struct wm_present_info present;
  615. unsigned int i;
  616. u_wm_set_geom((unsigned int)drag_id, nx, ny);
  617. u->info.x = nx;
  618. u->info.y = ny;
  619. present.count = 0;
  620. present.immediate = 1;
  621. add_rect(&present, ox, oy, u->info.w, u->info.h);
  622. add_rect(&present, nx, ny, u->info.w, u->info.h);
  623. if (u_wm_begin() == 0) {
  624. for (i = 0; i < present.count; i++)
  625. draw_rect(present.rect[i].x,
  626. present.rect[i].y,
  627. present.rect[i].w,
  628. present.rect[i].h);
  629. u_wm_present(&present);
  630. }
  631. }
  632. } else {
  633. drag_id = -1;
  634. }
  635. } else if (!down && was) {
  636. drag_id = -1;
  637. resize_id = -1;
  638. }
  639. prev_buttons = buttons;
  640. }
  641. void _start(const char *arg)
  642. {
  643. struct wm_screen_info si;
  644. struct wm_status st;
  645. unsigned int i;
  646. (void)arg;
  647. if (u_wm_attach(&si) != 0)
  648. u_exit(1);
  649. usurf_init(&screen, si.fb, si.width, si.height, si.pitch, si.bpp);
  650. fbw = si.width;
  651. fbh = si.height;
  652. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  653. wins[i].info.used = 0;
  654. wins[i].mapped = 0;
  655. }
  656. full_redraw = 1;
  657. for (;;) {
  658. if (u_wm_poll(&st) != 0)
  659. u_exit(1);
  660. if (st.full_redraw)
  661. full_redraw = 1;
  662. handle_mouse(&st);
  663. do_update(&st);
  664. u_sleep_ms(10);
  665. }
  666. }