ucmd_wm.c 20 KB

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