ucmd_wm.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220
  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. /* The active title bar uses the toolkit highlight (ngui_theme_dark.selection);
  10. * the inactive one stays neutral. Windows have no visible frame. */
  11. #define COL_TITLE_ON 0x365CA6u
  12. #define COL_TITLE_OFF 0x646464u
  13. #define COL_CLOSE 0xC64040u
  14. #define COL_WHITE 0xFFFFFFu
  15. #define COL_BANNER 0x55FF55u
  16. struct uwin {
  17. struct wm_window_info info;
  18. struct wm_map_info map;
  19. int mapped;
  20. unsigned int ack_seq;
  21. /* WIN_NORMAL, or maximized/snapped to a screen half; restore_* is the
  22. * geometry to return to when the window leaves that state. */
  23. int state;
  24. int restore_x, restore_y;
  25. unsigned int restore_w, restore_h;
  26. };
  27. /* Double-click detection on the title bar. */
  28. #define WM_DBLCLICK_MS 500
  29. #define WM_DBLCLICK_SLOP 8
  30. /* A maximized window restores only after the pointer actually starts a drag,
  31. * so a plain click on its title bar leaves it maximized. */
  32. #define WM_DRAG_START 3
  33. /* Window placement states. Maximize fills the work area below the bar; the
  34. * snap states fill its left or right half at full height. */
  35. #define WIN_NORMAL 0
  36. #define WIN_MAXIMIZED 1
  37. #define WIN_SNAP_LEFT 2
  38. #define WIN_SNAP_RIGHT 3
  39. /* Releasing a title-bar drag with the pointer this close to the left/right
  40. * screen edge snaps the window to that half; at the top edge it maximizes. */
  41. #define WM_SNAP_EDGE 2
  42. /* Edge resistance: a dragged window's edge stops at a screen edge or at the
  43. * facing edge of another window until the pointer overshoots it by this
  44. * many pixels, which makes exact side-by-side placement easy. */
  45. #define WM_RESIST 24
  46. static struct usurf screen;
  47. static struct uwin wins[WM_MAX_WINDOWS];
  48. static unsigned int fbw, fbh;
  49. static int drag_id = -1;
  50. static int drag_dx, drag_dy;
  51. static int drag_mx, drag_my; /* press position, for restore-on-drag */
  52. static int drag_moved; /* the pointer passed WM_DRAG_START */
  53. static int resize_id = -1;
  54. static int resize_edges; /* WM_EDGE_* bits captured at the press */
  55. static int resize_ox, resize_oy;
  56. static unsigned int resize_mx, resize_my, resize_ow, resize_oh;
  57. static unsigned int last_title_ms;
  58. static int last_title_id = -1, last_title_x, last_title_y;
  59. static unsigned int prev_buttons;
  60. static int full_redraw = 1;
  61. static int boot_term_spawned;
  62. static unsigned int bar_kexec_busy, bar_kexec_progress, bar_kexec_total;
  63. static int bar_temp_c = -1;
  64. static int bar_batt_pct = -1;
  65. static int bar_batt_charging;
  66. static int bar_ac_online = -1;
  67. /* Battery/AC status icon: a plug on AC, a level-filled battery otherwise.
  68. * The rightmost bar item; hovering it shows a percentage tooltip below it. */
  69. #define BAR_PWR_W 24
  70. #define BAR_PWR_Y 6
  71. #define BAR_TIP_H 20
  72. static int bar_pwr_x = -1;
  73. static int bar_pwr_hot;
  74. static int bar_pwr_tip_x = -1;
  75. static int bar_pwr_tip_y;
  76. static unsigned int bar_pwr_tip_w, bar_pwr_tip_h;
  77. static int bar_pwr_tip_want; /* tooltip computed for this frame */
  78. static int bar_pwr_tip_on; /* tooltip pixels currently on screen */
  79. static int bar_pwr_tip_on_x, bar_pwr_tip_on_y;
  80. static unsigned int bar_pwr_tip_on_w, bar_pwr_tip_on_h;
  81. /* Brightness OSD: a centered bubble shown after the kernel's backlight
  82. * seq changes, then hidden after OSD_MS. */
  83. #define OSD_MS 3000
  84. #define OSD_W 210
  85. #define OSD_H 60
  86. static int osd_show; /* should be visible this frame */
  87. static int osd_visible; /* pixels are on screen */
  88. static int osd_level;
  89. static unsigned int osd_until;
  90. static unsigned int osd_seen_seq;
  91. static int osd_seen_valid;
  92. static int osd_x, osd_y;
  93. static unsigned int osd_w, osd_h;
  94. static char bar_msg[24];
  95. static unsigned int bar_msg_until;
  96. static int bar_msg_dirty;
  97. /* Rectangle of the right-aligned date/time text, so a click there can open
  98. * the Date & Time app. w == 0 means "not drawn" (e.g. kexec banner). */
  99. static int bar_clock_x = -1;
  100. static unsigned int bar_clock_w;
  101. static int bar_clock_hot;
  102. static unsigned int stat_frames, stat_ms, stat_px, stat_max, stat_last;
  103. /* Network status icon (right side of the bar). */
  104. #define BAR_NET_W 24
  105. #define BAR_NET_Y 6
  106. #define BAR_NET_NONE 0
  107. #define BAR_NET_WIRED 1
  108. #define BAR_NET_WIFI 2
  109. #define BAR_NET_USB 3
  110. static int bar_net_x = -1;
  111. static unsigned int bar_net_w;
  112. static int bar_net_hot;
  113. static int bar_net_kind;
  114. static int bar_net_bars;
  115. static unsigned int bar_net_color = 0xFF5050u;
  116. static int control_pid;
  117. static void bar_notice(const char *msg)
  118. {
  119. unsigned int i;
  120. for (i = 0; msg[i] && i + 1 < sizeof(bar_msg); i++)
  121. bar_msg[i] = msg[i];
  122. bar_msg[i] = '\0';
  123. bar_msg_until = u_ticks_ms() + 2000;
  124. bar_msg_dirty = 1;
  125. }
  126. static int free_window_slot(void)
  127. {
  128. unsigned int i;
  129. for (i = 0; i < WM_MAX_WINDOWS; i++)
  130. if (!wins[i].info.used)
  131. return 1;
  132. return 0;
  133. }
  134. static unsigned int rgb16(unsigned int r, unsigned int g, unsigned int b)
  135. {
  136. return (r << 16) | (g << 8) | b;
  137. }
  138. /* Rainbow gradient: hue 0..1535 (6*256) maps to R→Y→G→C→B→M→R, the same
  139. * integer interpolation the kernel bar uses. */
  140. static unsigned int rainbow_rgb(unsigned int hue)
  141. {
  142. unsigned int seg = hue / 256, f = hue % 256;
  143. unsigned int r1, g1, b1, r2, g2, b2;
  144. switch (seg) {
  145. case 0: r1=255; g1=0; b1=0; r2=255; g2=255; b2=0; break;
  146. case 1: r1=255; g1=255; b1=0; r2=0; g2=255; b2=0; break;
  147. case 2: r1=0; g1=255; b1=0; r2=0; g2=255; b2=255; break;
  148. case 3: r1=0; g1=255; b1=255; r2=0; g2=0; b2=255; break;
  149. case 4: r1=0; g1=0; b1=255; r2=255; g2=0; b2=255; break;
  150. default:r1=255; g1=0; b1=255; r2=255; g2=0; b2=0; break;
  151. }
  152. return rgb16((r1 * (256 - f) + r2 * f) / 256,
  153. (g1 * (256 - f) + g2 * f) / 256,
  154. (b1 * (256 - f) + b2 * f) / 256);
  155. }
  156. static void fill_clip(int x, int y, unsigned int w, unsigned int h,
  157. unsigned int rgb, int cx, int cy,
  158. unsigned int cw, unsigned int ch)
  159. {
  160. int x0 = x, y0 = y, x1 = x + (int)w, y1 = y + (int)h;
  161. if (x0 < cx) x0 = cx;
  162. if (y0 < cy) y0 = cy;
  163. if (x1 > cx + (int)cw) x1 = cx + (int)cw;
  164. if (y1 > cy + (int)ch) y1 = cy + (int)ch;
  165. if (x1 > x0 && y1 > y0)
  166. usurf_fill(&screen, x0, y0, (unsigned int)(x1 - x0),
  167. (unsigned int)(y1 - y0), rgb);
  168. }
  169. static void bar_button(unsigned int x, unsigned int y, unsigned int w,
  170. unsigned int h, const char *label)
  171. {
  172. int tw = (int)usurf_text_width(&screen, label);
  173. usurf_fill(&screen, x, y, w, h, 0x963CD2u);
  174. usurf_text(&screen, (int)x + ((int)w - tw) / 2, (int)(y + 3),
  175. label, COL_WHITE, 0, 0, fbw, WM_BAR_H);
  176. }
  177. static int name_is(const char *a, const char *b)
  178. {
  179. while (*a && *b) {
  180. if (*a != *b)
  181. return 0;
  182. a++;
  183. b++;
  184. }
  185. return *a == *b;
  186. }
  187. static int net_rssi_bars(int rssi)
  188. {
  189. if (rssi >= -55)
  190. return 4;
  191. if (rssi >= -65)
  192. return 3;
  193. if (rssi >= -75)
  194. return 2;
  195. return 1;
  196. }
  197. /* Small top-bar pictograms, drawn directly into the shadow. */
  198. static void bar_icon_cable(int x, int y, unsigned int c)
  199. {
  200. usurf_fill(&screen, x + 1, y + 6, 9, 8, c);
  201. usurf_fill(&screen, x + 3, y + 8, 2, 2, COL_BG);
  202. usurf_fill(&screen, x + 6, y + 8, 2, 2, COL_BG);
  203. usurf_fill(&screen, x + 10, y + 9, 7, 2, c);
  204. usurf_fill(&screen, x + 17, y + 5, 5, 10, c);
  205. usurf_fill(&screen, x + 19, y + 7, 2, 3, COL_BG);
  206. }
  207. static void bar_icon_wifi(int x, int y, unsigned int c, int bars)
  208. {
  209. int i;
  210. for (i = 0; i < 4; i++) {
  211. int h = 3 + i * 3;
  212. usurf_fill(&screen, x + 3 + i * 5, y + 16 - h, 3, h,
  213. i < bars ? c : 0x4A4A5Au);
  214. }
  215. }
  216. static void bar_icon_x(int x, int y, unsigned int c)
  217. {
  218. int i;
  219. for (i = 0; i < 12; i++) {
  220. usurf_fill(&screen, x + 6 + i, y + 4 + i, 2, 2, c);
  221. usurf_fill(&screen, x + 17 - i, y + 4 + i, 2, 2, c);
  222. }
  223. }
  224. static void bar_icon_usb(int x, int y, unsigned int c)
  225. {
  226. usurf_text(&screen, x + 1, y + 1, "USB", c, 0, 0, fbw, WM_BAR_H);
  227. }
  228. static void bar_draw_net_icon(int x, int y)
  229. {
  230. switch (bar_net_kind) {
  231. case BAR_NET_WIRED:
  232. bar_icon_cable(x, y, bar_net_color);
  233. break;
  234. case BAR_NET_WIFI:
  235. bar_icon_wifi(x, y, bar_net_color, bar_net_bars);
  236. break;
  237. case BAR_NET_USB:
  238. bar_icon_usb(x, y, bar_net_color);
  239. break;
  240. default:
  241. bar_icon_x(x, y, bar_net_color);
  242. break;
  243. }
  244. }
  245. /* 1 Hz poll: cached state -> the bar's network icon. The WiFi status call is
  246. * only made when WiFi is the active netdev, so a machine without a card does
  247. * not run a PCI scan on every tick. */
  248. static int poll_net_status(void)
  249. {
  250. struct sys_netinfo ni;
  251. int kind = BAR_NET_NONE, bars = 0, have_ip = 0;
  252. unsigned int color = 0xFF5050u;
  253. int changed;
  254. int i;
  255. if (u_net_info(&ni) == 1 && ni.present) {
  256. for (i = 0; i < 4; i++)
  257. if (ni.ip[i])
  258. have_ip = 1;
  259. if (name_is(ni.name, "wifi")) {
  260. struct sys_wifi_status ws;
  261. if (u_wifi(SYS_WIFI_STATUS, (int)&ws, 0) == 0 &&
  262. ws.present && ws.state >= SYS_WIFI_ST_ASSOC) {
  263. kind = BAR_NET_WIFI;
  264. bars = net_rssi_bars(ws.rssi);
  265. color = (ws.state >= SYS_WIFI_ST_LINK && have_ip) ?
  266. 0x55FF55u : 0xFFD250u;
  267. }
  268. } else if (name_is(ni.name, "rndis")) {
  269. kind = BAR_NET_USB;
  270. color = have_ip ? 0x55FF55u : 0xFFD250u;
  271. } else {
  272. kind = BAR_NET_WIRED;
  273. color = have_ip ? 0x55FF55u : 0xFFD250u;
  274. }
  275. }
  276. changed = kind != bar_net_kind || bars != bar_net_bars ||
  277. color != bar_net_color;
  278. bar_net_kind = kind;
  279. bar_net_bars = bars;
  280. bar_net_color = color;
  281. return changed;
  282. }
  283. static unsigned int temp_color(int c)
  284. {
  285. if (c >= 80)
  286. return 0xFF5050u;
  287. if (c >= 60)
  288. return 0xFFD250u;
  289. return 0x55FF55u;
  290. }
  291. /* "43C" (no libc printf here). */
  292. static unsigned int temp_text(char *b, int c)
  293. {
  294. unsigned int n = 0, v = (unsigned int)c;
  295. if (v >= 100)
  296. b[n++] = (char)('0' + (v / 100) % 10);
  297. if (v >= 10)
  298. b[n++] = (char)('0' + (v / 10) % 10);
  299. b[n++] = (char)('0' + v % 10);
  300. b[n++] = 'C';
  301. b[n] = '\0';
  302. return n;
  303. }
  304. /* "87%" (no libc printf here). */
  305. static unsigned int pct_text(char *b, int pct)
  306. {
  307. unsigned int n = 0, v = (unsigned int)pct;
  308. if (v >= 100)
  309. b[n++] = (char)('0' + (v / 100) % 10);
  310. if (v >= 10)
  311. b[n++] = (char)('0' + (v / 10) % 10);
  312. b[n++] = (char)('0' + v % 10);
  313. b[n++] = '%';
  314. b[n] = '\0';
  315. return n;
  316. }
  317. static unsigned int batt_color(int pct)
  318. {
  319. if (pct <= 10)
  320. return 0xFF5050u;
  321. return 0x55FF55u;
  322. }
  323. /* Battery pictogram: outline, terminal and a fill proportional to the
  324. * percentage. A critical charge turns the outline red as well. */
  325. static void bar_icon_battery(int x, int y, int pct, unsigned int c)
  326. {
  327. unsigned int frame = c;
  328. unsigned int fill = (unsigned int)((14 * pct + 99) / 100);
  329. usurf_fill(&screen, x + 1, y + 2, 18, 2, frame);
  330. usurf_fill(&screen, x + 1, y + 14, 18, 2, frame);
  331. usurf_fill(&screen, x + 1, y + 2, 2, 14, frame);
  332. usurf_fill(&screen, x + 17, y + 2, 2, 14, frame);
  333. usurf_fill(&screen, x + 19, y + 6, 3, 6, frame);
  334. if (fill > 14)
  335. fill = 14;
  336. if (fill)
  337. usurf_fill(&screen, x + 3, y + 4, fill, 10, c);
  338. }
  339. /* AC plug pictogram: two prongs and the body, kept compact in height. */
  340. static void bar_icon_plug(int x, int y, unsigned int c)
  341. {
  342. usurf_fill(&screen, x + 7, y + 2, 3, 5, c);
  343. usurf_fill(&screen, x + 14, y + 2, 3, 5, c);
  344. usurf_fill(&screen, x + 5, y + 7, 14, 7, c);
  345. usurf_fill(&screen, x + 7, y + 9, 10, 3, COL_BG);
  346. }
  347. /* Percentage tooltip: a small box below the status icon. It is drawn after
  348. * the windows (see bar_tooltip_overlay), so it always sits on top. */
  349. static void bar_draw_tooltip(void)
  350. {
  351. char lb[8];
  352. unsigned int lw;
  353. int tx;
  354. if (!bar_pwr_tip_want || bar_pwr_tip_x < 0)
  355. return;
  356. usurf_fill(&screen, bar_pwr_tip_x, bar_pwr_tip_y,
  357. bar_pwr_tip_w, bar_pwr_tip_h, 0x161622u);
  358. usurf_fill(&screen, bar_pwr_tip_x, bar_pwr_tip_y,
  359. bar_pwr_tip_w, 1, 0x646464u);
  360. usurf_fill(&screen, bar_pwr_tip_x,
  361. bar_pwr_tip_y + (int)bar_pwr_tip_h - 1,
  362. bar_pwr_tip_w, 1, 0x646464u);
  363. usurf_fill(&screen, bar_pwr_tip_x, bar_pwr_tip_y,
  364. 1, bar_pwr_tip_h, 0x646464u);
  365. usurf_fill(&screen, bar_pwr_tip_x + (int)bar_pwr_tip_w - 1,
  366. bar_pwr_tip_y, 1, bar_pwr_tip_h, 0x646464u);
  367. (void)pct_text(lb, bar_batt_pct);
  368. lw = usurf_text_width(&screen, lb);
  369. tx = bar_pwr_tip_x + ((int)bar_pwr_tip_w - (int)lw) / 2;
  370. usurf_text(&screen, tx, bar_pwr_tip_y + 3, lb,
  371. batt_color(bar_batt_pct), 0, 0, fbw, fbh);
  372. }
  373. /* Publish the geometry computed by draw_bar() and redraw the tooltip. */
  374. static void bar_tooltip_commit(void)
  375. {
  376. if (bar_pwr_tip_want) {
  377. bar_draw_tooltip();
  378. bar_pwr_tip_on = 1;
  379. bar_pwr_tip_on_x = bar_pwr_tip_x;
  380. bar_pwr_tip_on_y = bar_pwr_tip_y;
  381. bar_pwr_tip_on_w = bar_pwr_tip_w;
  382. bar_pwr_tip_on_h = bar_pwr_tip_h;
  383. } else {
  384. bar_pwr_tip_on = 0;
  385. }
  386. }
  387. static void draw_bar(void)
  388. {
  389. char dt[48];
  390. unsigned int n, i, x, y;
  391. usurf_fill(&screen, 0, 0, fbw, WM_BAR_H, COL_BG);
  392. usurf_text(&screen, 4, 5, OS_NAME " v" OS_VERSION, COL_BANNER,
  393. 0, 0, fbw, WM_BAR_H);
  394. bar_button(WM_BTN_X, WM_BTN_Y, WM_BTN_W, WM_BTN_H, "New");
  395. bar_button(WM_BTN_CLOCK_X, WM_BTN_CLOCK_Y, WM_BTN_CLOCK_W,
  396. WM_BTN_CLOCK_H, "Clock");
  397. bar_button(WM_BTN_COLORS_X, WM_BTN_COLORS_Y, WM_BTN_COLORS_W,
  398. WM_BTN_COLORS_H, "Colors");
  399. bar_button(WM_BTN_FILES_X, WM_BTN_FILES_Y, WM_BTN_FILES_W,
  400. WM_BTN_FILES_H, "Files");
  401. bar_button(WM_BTN_MINES_X, WM_BTN_MINES_Y, WM_BTN_MINES_W,
  402. WM_BTN_MINES_H, "Mines");
  403. bar_button(WM_BTN_WEB_X, WM_BTN_WEB_Y, WM_BTN_WEB_W,
  404. WM_BTN_WEB_H, "Web");
  405. bar_button(WM_BTN_CARDS_X, WM_BTN_CARDS_Y, WM_BTN_CARDS_W,
  406. WM_BTN_CARDS_H, "Cards");
  407. bar_button(WM_BTN_GALLERY_X, WM_BTN_GALLERY_Y, WM_BTN_GALLERY_W,
  408. WM_BTN_GALLERY_H, "Gallery");
  409. bar_button(WM_BTN_CHAT_X, WM_BTN_CHAT_Y, WM_BTN_CHAT_W,
  410. WM_BTN_CHAT_H, "Chat");
  411. bar_button(WM_BTN_DISKS_X, WM_BTN_DISKS_Y, WM_BTN_DISKS_W,
  412. WM_BTN_DISKS_H, "Disks");
  413. if (bar_msg_until &&
  414. (int)(u_ticks_ms() - bar_msg_until) < 0)
  415. usurf_text(&screen, (int)(WM_BTN_END_X + 8),
  416. 5, bar_msg, 0xFFD250u, 0, 0, fbw, WM_BAR_H);
  417. /* Rainbow separator (same y as the kernel's old bar). */
  418. for (x = 0; x < fbw; x++) {
  419. unsigned int c = rainbow_rgb((x * (6 * 256)) / (fbw ? fbw : 1));
  420. for (y = WM_BAR_H - 5; y < WM_BAR_H - 3; y++)
  421. usurf_fill(&screen, (int)x, (int)y, 1, 1, c);
  422. }
  423. if (bar_kexec_busy) {
  424. static const char msg[] = "loading new kernel over serial";
  425. unsigned int dots = bar_kexec_total ?
  426. (bar_kexec_progress * 12 / bar_kexec_total) : 0;
  427. bar_clock_x = -1;
  428. bar_clock_w = 0;
  429. bar_clock_hot = 0;
  430. bar_net_x = -1;
  431. bar_net_w = 0;
  432. bar_pwr_x = -1;
  433. bar_pwr_tip_x = -1;
  434. bar_pwr_tip_want = 0;
  435. n = 0;
  436. while (msg[n])
  437. dt[n] = msg[n], n++;
  438. for (i = 0; i < 12; i++)
  439. dt[n + i] = (i < dots) ? '.' : ' ';
  440. n += 12;
  441. dt[n] = '\0';
  442. usurf_text(&screen,
  443. (int)fbw - (int)usurf_text_width(&screen, dt) - 4,
  444. 5, dt, 0xFFC864u, 0, 0, fbw, WM_BAR_H);
  445. } else {
  446. unsigned int clock_chars = 0;
  447. int right = (int)fbw - 4;
  448. int min_x = (int)WM_BTN_END_X + 4;
  449. bar_clock_x = -1;
  450. bar_clock_w = 0;
  451. bar_net_x = -1;
  452. bar_net_w = 0;
  453. bar_pwr_x = -1;
  454. bar_pwr_tip_x = -1;
  455. bar_pwr_tip_want = 0;
  456. /* The AC/battery status icon is the rightmost item. */
  457. if (bar_batt_pct >= 0 || bar_ac_online > 0) {
  458. int px = right - BAR_PWR_W;
  459. if (px >= min_x) {
  460. if (bar_pwr_hot)
  461. usurf_fill(&screen, px - 3, 2,
  462. BAR_PWR_W + 6,
  463. WM_BAR_H - 7, 0x3A3A55u);
  464. if (bar_ac_online > 0)
  465. bar_icon_plug(px, BAR_PWR_Y, 0x55FF55u);
  466. else
  467. bar_icon_battery(px, BAR_PWR_Y,
  468. bar_batt_pct,
  469. batt_color(bar_batt_pct));
  470. bar_pwr_x = px;
  471. right = px - 8;
  472. if (bar_pwr_hot && bar_batt_pct >= 0) {
  473. char lb[8];
  474. unsigned int lw, bw;
  475. int bx;
  476. (void)pct_text(lb, bar_batt_pct);
  477. lw = usurf_text_width(&screen, lb);
  478. bw = lw + 10;
  479. bx = px + BAR_PWR_W - (int)bw;
  480. if (bx < 4)
  481. bx = 4;
  482. if (bx + (int)bw > (int)fbw - 4)
  483. bx = (int)fbw - 4 - (int)bw;
  484. bar_pwr_tip_x = bx;
  485. bar_pwr_tip_y = WM_BAR_H + 2;
  486. bar_pwr_tip_w = bw;
  487. bar_pwr_tip_h = BAR_TIP_H;
  488. bar_pwr_tip_want = 1;
  489. }
  490. }
  491. }
  492. /* The network status sits to the left of the power icon, so it
  493. * survives narrow screens and a full temperature cluster. */
  494. {
  495. int nx = right - BAR_NET_W;
  496. if (nx >= min_x) {
  497. if (bar_net_hot)
  498. usurf_fill(&screen, nx - 3, 2,
  499. BAR_NET_W + 6,
  500. WM_BAR_H - 7, 0x3A3A55u);
  501. bar_draw_net_icon(nx, BAR_NET_Y);
  502. bar_net_x = nx;
  503. bar_net_w = BAR_NET_W;
  504. right = nx - 8;
  505. }
  506. }
  507. if (u_rtc_datetime(dt, sizeof(dt)) == 0) {
  508. int tw = (int)usurf_text_width(&screen, dt);
  509. int tx = right - tw;
  510. n = 0;
  511. while (dt[n] && n < sizeof(dt) - 1)
  512. n++;
  513. bar_clock_x = tx;
  514. bar_clock_w = (unsigned int)tw;
  515. if (bar_clock_hot)
  516. usurf_fill(&screen, tx - 5, 2,
  517. (unsigned int)tw + 10,
  518. WM_BAR_H - 7, 0x3A3A55u);
  519. usurf_text(&screen, tx, 5, dt, COL_BANNER, 0, 0,
  520. fbw, WM_BAR_H);
  521. clock_chars = (unsigned int)tw;
  522. }
  523. if (clock_chars)
  524. right -= (int)clock_chars + 8;
  525. if (bar_temp_c >= 0) {
  526. char tb[12];
  527. int tx;
  528. (void)temp_text(tb, bar_temp_c);
  529. tx = right - (int)usurf_text_width(&screen, tb);
  530. if (tx >= min_x) {
  531. usurf_text(&screen, tx, 5, tb,
  532. temp_color(bar_temp_c), 0, 0,
  533. fbw, WM_BAR_H);
  534. right = tx - 8;
  535. }
  536. }
  537. }
  538. }
  539. /* A popover is a flat, chromeless panel: the canvas fills the window. */
  540. static void draw_popover_clip(unsigned int idx, int cx, int cy,
  541. unsigned int cw, unsigned int ch)
  542. {
  543. struct uwin *u = &wins[idx];
  544. struct wm_window_info *w = &u->info;
  545. unsigned int cwid = w->content_w, chei = w->content_h;
  546. if (u->mapped && u->map.kind == WM_KIND_GFX) {
  547. struct uwm_canvas c;
  548. c.px = (const unsigned short *)u->map.va;
  549. c.w = u->map.w;
  550. c.h = u->map.h;
  551. if (c.w == cwid && c.h == chei) {
  552. usurf_canvas(&screen, &c, w->x, w->y,
  553. cx, cy, cw, ch);
  554. return;
  555. }
  556. }
  557. fill_clip(w->x, w->y, cwid, chei, 0x181C28u, cx, cy, cw, ch);
  558. }
  559. static void draw_window_clip(unsigned int idx, int cx, int cy,
  560. unsigned int cw, unsigned int ch)
  561. {
  562. struct uwin *u = &wins[idx];
  563. struct wm_window_info *w = &u->info;
  564. int x = w->x, y = w->y;
  565. unsigned int pw = w->w;
  566. int bx = x + (int)pw - WM_BORDER_W - WM_CLOSE_W;
  567. int cony = y + WM_BORDER_W + WM_TITLE_H;
  568. int content_x = x + WM_BORDER_W;
  569. unsigned int content_w = w->content_w;
  570. unsigned int content_h = w->content_h;
  571. if (w->popover) {
  572. draw_popover_clip(idx, cx, cy, cw, ch);
  573. return;
  574. }
  575. /* Title bar and close button; windows have no visible frame. */
  576. fill_clip(x, y, pw, WM_TITLE_H,
  577. w->focused ? COL_TITLE_ON : COL_TITLE_OFF, cx, cy, cw, ch);
  578. usurf_text(&screen, x + 3, y + 2, w->title, COL_WHITE,
  579. cx, cy, cw, ch);
  580. fill_clip(bx, y, WM_CLOSE_W, WM_TITLE_H, COL_CLOSE, cx, cy, cw, ch);
  581. usurf_text(&screen, bx + 4, y + 2, "x", COL_WHITE,
  582. cx, cy, cw, ch);
  583. /* Content: every window presents an RGB565 canvas (the terminal
  584. * renders its cells into one; gfx commands own theirs). A mapped
  585. * canvas covers the whole content area, so it does not need the black
  586. * underlay. */
  587. if (u->mapped && u->map.kind == WM_KIND_GFX) {
  588. struct uwm_canvas c;
  589. c.px = (const unsigned short *)u->map.va;
  590. c.w = u->map.w;
  591. c.h = u->map.h;
  592. if (c.w != content_w || c.h != content_h)
  593. fill_clip(content_x, cony, content_w, content_h,
  594. 0x000000u, cx, cy, cw, ch);
  595. usurf_canvas(&screen, &c, content_x, cony,
  596. cx, cy, cw, ch);
  597. } else {
  598. fill_clip(content_x, cony, content_w, content_h,
  599. 0x000000u, cx, cy, cw, ch);
  600. }
  601. }
  602. /* Front-to-back window order: popovers float above everything, then the
  603. * focused window, then the others in descending slot order. Painting and
  604. * hit-testing share it. */
  605. static unsigned int build_order(int *order)
  606. {
  607. unsigned int i, n = 0;
  608. for (i = 0; i < WM_MAX_WINDOWS; i++)
  609. if (wins[i].info.used && wins[i].info.popover)
  610. order[n++] = (int)i;
  611. for (i = 0; i < WM_MAX_WINDOWS; i++)
  612. if (wins[i].info.used && wins[i].info.focused &&
  613. !wins[i].info.popover)
  614. order[n++] = (int)i;
  615. for (i = WM_MAX_WINDOWS; i-- > 0;)
  616. if (wins[i].info.used && !wins[i].info.focused &&
  617. !wins[i].info.popover)
  618. order[n++] = (int)i;
  619. return n;
  620. }
  621. struct orect {
  622. int x, y;
  623. unsigned int w, h;
  624. };
  625. #define OCC_MAX 64
  626. /* r minus cut, split into up to four disjoint rectangles. */
  627. static unsigned int rect_sub(struct orect *out, const struct orect *r,
  628. const struct orect *cut)
  629. {
  630. int rx0 = r->x, ry0 = r->y;
  631. int rx1 = r->x + (int)r->w, ry1 = r->y + (int)r->h;
  632. int cx0 = cut->x, cy0 = cut->y;
  633. int cx1 = cut->x + (int)cut->w, cy1 = cut->y + (int)cut->h;
  634. unsigned int n = 0;
  635. if (cx0 < rx0) cx0 = rx0;
  636. if (cy0 < ry0) cy0 = ry0;
  637. if (cx1 > rx1) cx1 = rx1;
  638. if (cy1 > ry1) cy1 = ry1;
  639. if (cx1 <= cx0 || cy1 <= cy0) {
  640. out[0] = *r;
  641. return 1;
  642. }
  643. if (cy0 > ry0) {
  644. out[n].x = rx0;
  645. out[n].y = ry0;
  646. out[n].w = (unsigned int)(rx1 - rx0);
  647. out[n].h = (unsigned int)(cy0 - ry0);
  648. n++;
  649. }
  650. if (cy1 < ry1) {
  651. out[n].x = rx0;
  652. out[n].y = cy1;
  653. out[n].w = (unsigned int)(rx1 - rx0);
  654. out[n].h = (unsigned int)(ry1 - cy1);
  655. n++;
  656. }
  657. if (cx0 > rx0) {
  658. out[n].x = rx0;
  659. out[n].y = cy0;
  660. out[n].w = (unsigned int)(cx0 - rx0);
  661. out[n].h = (unsigned int)(cy1 - cy0);
  662. n++;
  663. }
  664. if (cx1 < rx1) {
  665. out[n].x = cx1;
  666. out[n].y = cy0;
  667. out[n].w = (unsigned int)(rx1 - cx1);
  668. out[n].h = (unsigned int)(cy1 - cy0);
  669. n++;
  670. }
  671. return n;
  672. }
  673. /* Back-to-front repaint, used only if the visible-region list overflows. */
  674. static void draw_rect_naive(int x, int y, unsigned int w, unsigned int h)
  675. {
  676. unsigned int i;
  677. usurf_fill(&screen, x, y, w, h, COL_BG);
  678. for (i = 0; i < WM_MAX_WINDOWS; i++)
  679. if (wins[i].info.used && !wins[i].info.focused &&
  680. !wins[i].info.popover &&
  681. x < wins[i].info.x + (int)wins[i].info.w &&
  682. x + (int)w > wins[i].info.x &&
  683. y < wins[i].info.y + (int)wins[i].info.h &&
  684. y + (int)h > wins[i].info.y)
  685. draw_window_clip(i, x, y, w, h);
  686. for (i = 0; i < WM_MAX_WINDOWS; i++)
  687. if (wins[i].info.used && wins[i].info.focused &&
  688. !wins[i].info.popover &&
  689. x < wins[i].info.x + (int)wins[i].info.w &&
  690. x + (int)w > wins[i].info.x &&
  691. y < wins[i].info.y + (int)wins[i].info.h &&
  692. y + (int)h > wins[i].info.y)
  693. draw_window_clip(i, x, y, w, h);
  694. for (i = 0; i < WM_MAX_WINDOWS; i++)
  695. if (wins[i].info.used && wins[i].info.popover &&
  696. x < wins[i].info.x + (int)wins[i].info.w &&
  697. x + (int)w > wins[i].info.x &&
  698. y < wins[i].info.y + (int)wins[i].info.h &&
  699. y + (int)h > wins[i].info.y)
  700. draw_window_clip(i, x, y, w, h);
  701. }
  702. static void draw_rect(int x, int y, unsigned int w, unsigned int h)
  703. {
  704. static struct orect cur[OCC_MAX], nxt[OCC_MAX];
  705. struct orect dmg;
  706. int order[WM_MAX_WINDOWS];
  707. unsigned int n, i, j, ncur, nnext;
  708. int overflow = 0;
  709. int x0 = x, y0 = y, x1 = x + (int)w, y1 = y + (int)h;
  710. if (w == 0 || h == 0)
  711. return;
  712. if (x0 < 0) x0 = 0;
  713. if (y0 < 0) y0 = 0;
  714. if (x1 > (int)fbw) x1 = (int)fbw;
  715. if (y1 > (int)fbh) y1 = (int)fbh;
  716. if (x1 <= x0 || y1 <= y0)
  717. return;
  718. dmg.x = x0;
  719. dmg.y = y0;
  720. dmg.w = (unsigned int)(x1 - x0);
  721. dmg.h = (unsigned int)(y1 - y0);
  722. usurf_fill(&screen, dmg.x, dmg.y, dmg.w, dmg.h, COL_BG);
  723. n = build_order(order);
  724. for (i = 0; i < n; i++) {
  725. struct uwin *u = &wins[order[i]];
  726. int wx0 = u->info.x, wy0 = u->info.y;
  727. int wx1 = wx0 + (int)u->info.w, wy1 = wy0 + (int)u->info.h;
  728. if (wx0 < dmg.x) wx0 = dmg.x;
  729. if (wy0 < dmg.y) wy0 = dmg.y;
  730. if (wx1 > x1) wx1 = x1;
  731. if (wy1 > y1) wy1 = y1;
  732. if (wx1 <= wx0 || wy1 <= wy0)
  733. continue;
  734. cur[0].x = wx0;
  735. cur[0].y = wy0;
  736. cur[0].w = (unsigned int)(wx1 - wx0);
  737. cur[0].h = (unsigned int)(wy1 - wy0);
  738. ncur = 1;
  739. for (j = 0; j < i && ncur; j++) {
  740. struct uwin *f = &wins[order[j]];
  741. struct orect fr;
  742. unsigned int k;
  743. fr.x = f->info.x;
  744. fr.y = f->info.y;
  745. fr.w = f->info.w;
  746. fr.h = f->info.h;
  747. nnext = 0;
  748. for (k = 0; k < ncur; k++) {
  749. if (nnext + 4 > OCC_MAX) {
  750. overflow = 1;
  751. break;
  752. }
  753. nnext += rect_sub(&nxt[nnext], &cur[k], &fr);
  754. }
  755. if (overflow)
  756. break;
  757. for (k = 0; k < nnext; k++)
  758. cur[k] = nxt[k];
  759. ncur = nnext;
  760. }
  761. if (overflow)
  762. break;
  763. for (j = 0; j < ncur; j++)
  764. draw_window_clip((unsigned int)order[i], cur[j].x,
  765. cur[j].y, cur[j].w, cur[j].h);
  766. }
  767. if (overflow)
  768. draw_rect_naive(dmg.x, dmg.y, dmg.w, dmg.h);
  769. }
  770. /* Invisible resize border: edge bits for a point in the band around the
  771. * window's outer edge (WM_RESIZE_IN px inside and WM_RESIZE_OUT px outside).
  772. * Popovers have no chrome and never resize. */
  773. #define WM_EDGE_LEFT 1
  774. #define WM_EDGE_RIGHT 2
  775. #define WM_EDGE_TOP 4
  776. #define WM_EDGE_BOTTOM 8
  777. static int resize_edges_at(const struct uwin *u, int mx, int my)
  778. {
  779. const struct wm_window_info *w = &u->info;
  780. int l = w->x, t = w->y;
  781. int r = l + (int)w->w, b = t + (int)w->h;
  782. int near_x, near_y, e = 0;
  783. if (w->popover)
  784. return 0;
  785. near_x = mx >= l - WM_RESIZE_OUT && mx < r + WM_RESIZE_OUT;
  786. near_y = my >= t - WM_RESIZE_OUT && my < b + WM_RESIZE_OUT;
  787. if (near_y && mx >= l - WM_RESIZE_OUT && mx < l + WM_RESIZE_IN)
  788. e |= WM_EDGE_LEFT;
  789. if (near_y && mx >= r - WM_RESIZE_IN && mx < r + WM_RESIZE_OUT)
  790. e |= WM_EDGE_RIGHT;
  791. if (near_x && my >= t - WM_RESIZE_OUT && my < t + WM_RESIZE_IN)
  792. e |= WM_EDGE_TOP;
  793. if (near_x && my >= b - WM_RESIZE_IN && my < b + WM_RESIZE_OUT)
  794. e |= WM_EDGE_BOTTOM;
  795. return e;
  796. }
  797. /* Topmost window at (mx,my): its resize band when the point is inside one,
  798. * otherwise the window rectangle. *edges is 0 for a plain rectangle hit.
  799. * Bands are not considered inside the top bar, which the WM owns. */
  800. static int resize_hit(int mx, int my, int *edges)
  801. {
  802. int order[WM_MAX_WINDOWS];
  803. unsigned int n = build_order(order), i;
  804. *edges = 0;
  805. for (i = 0; i < n; i++) {
  806. struct uwin *u = &wins[order[i]];
  807. int e = 0;
  808. if (!u->info.used)
  809. continue;
  810. if (my >= WM_BAR_H)
  811. e = resize_edges_at(u, mx, my);
  812. if (e) {
  813. *edges = e;
  814. return order[i];
  815. }
  816. if (mx >= u->info.x && my >= u->info.y &&
  817. mx < u->info.x + (int)u->info.w &&
  818. my < u->info.y + (int)u->info.h)
  819. return order[i];
  820. }
  821. return -1;
  822. }
  823. static int resize_cursor(int edges)
  824. {
  825. switch (edges) {
  826. case WM_EDGE_LEFT:
  827. case WM_EDGE_RIGHT:
  828. return CURSOR_EW_RESIZE;
  829. case WM_EDGE_TOP:
  830. case WM_EDGE_BOTTOM:
  831. return CURSOR_NS_RESIZE;
  832. case WM_EDGE_LEFT | WM_EDGE_TOP:
  833. case WM_EDGE_RIGHT | WM_EDGE_BOTTOM:
  834. return CURSOR_NWSE_RESIZE;
  835. case WM_EDGE_RIGHT | WM_EDGE_TOP:
  836. case WM_EDGE_LEFT | WM_EDGE_BOTTOM:
  837. return CURSOR_NESW_RESIZE;
  838. default:
  839. return CURSOR_DEFAULT;
  840. }
  841. }
  842. /* Close button hit area (the top-right WM_CLOSE_W columns of the title bar). */
  843. static int close_hit(const struct wm_window_info *w, int mx, int my)
  844. {
  845. if (w->popover)
  846. return 0;
  847. return mx >= w->x + (int)w->w - WM_CLOSE_W &&
  848. mx < w->x + (int)w->w &&
  849. my >= w->y && my < w->y + WM_TITLE_H;
  850. }
  851. static void clamp_geom(struct uwin *u, int *x, int *y)
  852. {
  853. /* Match wm_clamp_window(): allow the window to hang off the left,
  854. * right and bottom edges but keep the title bar reachable and below
  855. * the system bar. */
  856. int minx = WM_DRAG_VISIBLE - (int)u->info.w;
  857. int maxx = (int)fbw - WM_DRAG_VISIBLE;
  858. int miny = WM_BAR_H;
  859. int maxy = (int)fbh - WM_BORDER_W - WM_TITLE_H;
  860. if (minx > maxx)
  861. minx = maxx = 0;
  862. if (miny > maxy)
  863. miny = maxy = WM_BAR_H;
  864. if (*x < minx)
  865. *x = minx;
  866. else if (*x > maxx)
  867. *x = maxx;
  868. if (*y < miny)
  869. *y = miny;
  870. else if (*y > maxy)
  871. *y = maxy;
  872. }
  873. static void add_rect(struct wm_present_info *p, int x, int y,
  874. unsigned int w, unsigned int h)
  875. {
  876. if (w == 0 || h == 0)
  877. return;
  878. if (p->count < 4) {
  879. p->rect[p->count].x = x;
  880. p->rect[p->count].y = y;
  881. p->rect[p->count].w = w;
  882. p->rect[p->count].h = h;
  883. p->count++;
  884. } else {
  885. /* Four is enough for normal updates; fold the rest into the
  886. * last rect rather than dropping pixels. */
  887. int x0 = p->rect[3].x, y0 = p->rect[3].y;
  888. int x1 = x0 + (int)p->rect[3].w, y1 = y0 + (int)p->rect[3].h;
  889. int nx1 = x + (int)w, ny1 = y + (int)h;
  890. if (x < x0) x0 = x;
  891. if (y < y0) y0 = y;
  892. if (nx1 > x1) x1 = nx1;
  893. if (ny1 > y1) y1 = ny1;
  894. p->rect[3].x = x0;
  895. p->rect[3].y = y0;
  896. p->rect[3].w = (unsigned int)(x1 - x0);
  897. p->rect[3].h = (unsigned int)(y1 - y0);
  898. }
  899. }
  900. /* Redraw the power tooltip above whatever a render pass just painted. */
  901. static void bar_tooltip_overlay(struct wm_present_info *p)
  902. {
  903. if (!bar_pwr_tip_on || !bar_pwr_tip_want)
  904. return;
  905. bar_draw_tooltip();
  906. add_rect(p, bar_pwr_tip_on_x, bar_pwr_tip_on_y,
  907. bar_pwr_tip_on_w, bar_pwr_tip_on_h);
  908. }
  909. /* Center the brightness bubble in the work area, clamped on small screens. */
  910. static void osd_geom(void)
  911. {
  912. int x, y;
  913. osd_w = OSD_W;
  914. osd_h = OSD_H;
  915. x = ((int)fbw - (int)osd_w) / 2;
  916. y = WM_BAR_H + ((int)fbh - WM_BAR_H - (int)osd_h) * 3 / 5;
  917. if (x < 0)
  918. x = 0;
  919. if (x + (int)osd_w > (int)fbw)
  920. x = (int)fbw - (int)osd_w;
  921. if (y < WM_BAR_H)
  922. y = WM_BAR_H;
  923. if (y + (int)osd_h > (int)fbh)
  924. y = (int)fbh - (int)osd_h;
  925. osd_x = x;
  926. osd_y = y;
  927. }
  928. /* A small sun: a filled circle plus four rays, all from 1-pixel fills. */
  929. static void osd_sun(int cx, int cy)
  930. {
  931. static const unsigned char half[11] = { 2, 4, 5, 6, 6, 6, 6, 6, 5, 4, 2 };
  932. int i;
  933. for (i = -5; i <= 5; i++)
  934. usurf_fill(&screen, cx - half[i + 5], cy + i,
  935. half[i + 5] * 2 + 1, 1, 0xFFD250u);
  936. usurf_fill(&screen, cx - 1, cy - 9, 2, 3, 0xFFD250u);
  937. usurf_fill(&screen, cx - 1, cy + 7, 2, 3, 0xFFD250u);
  938. usurf_fill(&screen, cx - 9, cy - 1, 3, 2, 0xFFD250u);
  939. usurf_fill(&screen, cx + 7, cy - 1, 3, 2, 0xFFD250u);
  940. }
  941. static void osd_draw(void)
  942. {
  943. char pct[8];
  944. unsigned int n = 0;
  945. unsigned int bar_x, bar_y, bar_w;
  946. int x, y, tw;
  947. osd_geom();
  948. x = osd_x;
  949. y = osd_y;
  950. usurf_fill(&screen, x, y, osd_w, osd_h, 0x26263Au);
  951. usurf_fill(&screen, x, y, osd_w, 1, 0x70709Au);
  952. usurf_fill(&screen, x, y + (int)osd_h - 1, osd_w, 1, 0x70709Au);
  953. usurf_fill(&screen, x, y + 1, 1, osd_h - 2, 0x70709Au);
  954. usurf_fill(&screen, x + (int)osd_w - 1, y + 1, 1, osd_h - 2,
  955. 0x70709Au);
  956. osd_sun(x + 22, y + (int)osd_h / 2);
  957. if (osd_level >= 100) {
  958. pct[n++] = '1';
  959. pct[n++] = '0';
  960. pct[n++] = '0';
  961. } else {
  962. if (osd_level >= 10)
  963. pct[n++] = (char)('0' + osd_level / 10);
  964. pct[n++] = (char)('0' + osd_level % 10);
  965. }
  966. pct[n++] = '%';
  967. pct[n] = '\0';
  968. usurf_text(&screen, x + 42, y + 9, "Brightness", 0xE8E8F6u,
  969. x, y, osd_w, osd_h);
  970. tw = (int)usurf_text_width(&screen, pct);
  971. usurf_text(&screen, x + (int)osd_w - 14 - tw, y + 9, pct,
  972. 0xFFD250u, x, y, osd_w, osd_h);
  973. bar_x = (unsigned int)(x + 42);
  974. bar_y = (unsigned int)(y + 35);
  975. bar_w = osd_w - 42 - 14;
  976. usurf_fill(&screen, (int)bar_x, (int)bar_y, bar_w, 10, 0x44445Au);
  977. usurf_fill(&screen, (int)bar_x, (int)bar_y,
  978. (bar_w * (unsigned int)osd_level + 50) / 100, 10,
  979. 0xFFD250u);
  980. }
  981. /* Put the power tooltip and the brightness OSD back on top after a render
  982. * pass that may have overwritten them. */
  983. static void wm_overlays(struct wm_present_info *p)
  984. {
  985. bar_tooltip_overlay(p);
  986. if (osd_show) {
  987. osd_draw();
  988. add_rect(p, osd_x, osd_y, osd_w, osd_h);
  989. }
  990. }
  991. /* A focused window is topmost, so the shadow contains exactly its pixels in
  992. * the old rectangle. Move that block and repaint only the old part the new
  993. * rectangle does not cover. When either rectangle is partly off-screen the
  994. * shadow has no valid pixels for the hidden part, so repaint the new rect
  995. * instead of copying desktop background into the window. */
  996. static void move_window_pixels(struct uwin *u, int ox, int oy, int nx, int ny)
  997. {
  998. struct wm_present_info present;
  999. struct orect oldr, newr, pieces[4];
  1000. unsigned int np, k;
  1001. int w = (int)u->info.w, h = (int)u->info.h;
  1002. int old_on = ox >= 0 && oy >= 0 &&
  1003. ox + w <= (int)fbw && oy + h <= (int)fbh;
  1004. int new_on = nx >= 0 && ny >= 0 &&
  1005. nx + w <= (int)fbw && ny + h <= (int)fbh;
  1006. if (u_wm_begin() != 0)
  1007. return;
  1008. if (old_on && new_on)
  1009. usurf_copy(&screen, nx, ny, ox, oy, u->info.w, u->info.h);
  1010. else
  1011. draw_rect(nx, ny, u->info.w, u->info.h);
  1012. oldr.x = ox;
  1013. oldr.y = oy;
  1014. oldr.w = u->info.w;
  1015. oldr.h = u->info.h;
  1016. newr.x = nx;
  1017. newr.y = ny;
  1018. newr.w = u->info.w;
  1019. newr.h = u->info.h;
  1020. np = rect_sub(pieces, &oldr, &newr);
  1021. for (k = 0; k < np; k++)
  1022. draw_rect(pieces[k].x, pieces[k].y, pieces[k].w, pieces[k].h);
  1023. present.count = 0;
  1024. present.immediate = 1;
  1025. add_rect(&present, nx, ny, u->info.w, u->info.h);
  1026. for (k = 0; k < np; k++)
  1027. add_rect(&present, pieces[k].x, pieces[k].y,
  1028. pieces[k].w, pieces[k].h);
  1029. wm_overlays(&present);
  1030. u_wm_present(&present);
  1031. }
  1032. static int iabs(int v)
  1033. {
  1034. return v < 0 ? -v : v;
  1035. }
  1036. /* Repaint the old and new rectangles of a geometry change. */
  1037. static void redraw_geom(struct uwin *u, int ox, int oy,
  1038. unsigned int ow, unsigned int oh)
  1039. {
  1040. struct wm_present_info present;
  1041. unsigned int i;
  1042. present.count = 0;
  1043. present.immediate = 1;
  1044. add_rect(&present, ox, oy, ow, oh);
  1045. add_rect(&present, u->info.x, u->info.y, u->info.w, u->info.h);
  1046. if (u_wm_begin() == 0) {
  1047. for (i = 0; i < present.count; i++)
  1048. draw_rect(present.rect[i].x, present.rect[i].y,
  1049. present.rect[i].w, present.rect[i].h);
  1050. wm_overlays(&present);
  1051. u_wm_present(&present);
  1052. }
  1053. }
  1054. /* Resize (when needed), move and remap a window in one step. */
  1055. static int set_window_geometry(struct uwin *u, unsigned int idx,
  1056. int nx, int ny, unsigned int nw, unsigned int nh)
  1057. {
  1058. struct wm_window_info ni;
  1059. int ox = u->info.x, oy = u->info.y;
  1060. unsigned int ow = u->info.w, oh = u->info.h;
  1061. if ((int)nw != (int)ow || (int)nh != (int)oh) {
  1062. if (u_wm_resize(idx, nw - 2 * WM_BORDER_W,
  1063. nh - WM_TITLE_H - 2 * WM_BORDER_W) != 0)
  1064. return -1;
  1065. }
  1066. if (nx != ox || ny != oy)
  1067. u_wm_set_geom(idx, nx, ny);
  1068. u->mapped = 0;
  1069. if (u_wm_info(idx, &ni) == 1)
  1070. u->info = ni;
  1071. if (u_wm_map(idx, &u->map) == 0)
  1072. u->mapped = 1;
  1073. redraw_geom(u, ox, oy, ow, oh);
  1074. return 0;
  1075. }
  1076. static int restore_window(struct uwin *u, unsigned int idx)
  1077. {
  1078. if (u->state == WIN_NORMAL)
  1079. return 0;
  1080. if (set_window_geometry(u, idx, u->restore_x, u->restore_y,
  1081. u->restore_w, u->restore_h) != 0)
  1082. return -1;
  1083. u->state = WIN_NORMAL;
  1084. return 0;
  1085. }
  1086. /* Maximize or snap a window to a half of the work area below the system
  1087. * bar. The restore geometry is recorded only when the window leaves the
  1088. * normal state, so maximize -> snap -> restore returns to the original. */
  1089. static int place_window(struct uwin *u, unsigned int idx, int state)
  1090. {
  1091. unsigned int half = fbw / 2;
  1092. unsigned int h = fbh - WM_BAR_H;
  1093. int x = 0;
  1094. unsigned int w = fbw;
  1095. if (state == u->state)
  1096. return 0;
  1097. if (state == WIN_SNAP_LEFT) {
  1098. w = half;
  1099. } else if (state == WIN_SNAP_RIGHT) {
  1100. x = (int)half;
  1101. w = fbw - half;
  1102. }
  1103. if (u->state == WIN_NORMAL) {
  1104. u->restore_x = u->info.x;
  1105. u->restore_y = u->info.y;
  1106. u->restore_w = u->info.w;
  1107. u->restore_h = u->info.h;
  1108. }
  1109. if (set_window_geometry(u, idx, x, WM_BAR_H, w, h) != 0)
  1110. return -1;
  1111. u->state = state;
  1112. return 0;
  1113. }
  1114. /* Double-click title bar: fill the work area below the system bar, or go back
  1115. * to the geometry the window had before it was maximized. A snapped window
  1116. * maximizes (keeping its pre-snap restore geometry). */
  1117. static int toggle_maximize(struct uwin *u, unsigned int idx)
  1118. {
  1119. if (u->state == WIN_MAXIMIZED)
  1120. return restore_window(u, idx);
  1121. return place_window(u, idx, WIN_MAXIMIZED);
  1122. }
  1123. /* One axis of edge resistance. lo/len is the window's span on that axis
  1124. * before the move, *pos the proposed start. dir < 0: the window's start side
  1125. * (left/top) resists edge e while moving towards lower coordinates; dir > 0:
  1126. * its end side (right/bottom) while moving higher. Within WM_RESIST pixels
  1127. * of overshoot the window stays on the edge; *best keeps the nearest edge. */
  1128. static void resist_edge(int lo, int len, int *pos, int e, int dir, int *best)
  1129. {
  1130. int over;
  1131. if (dir < 0 && *pos < lo && lo >= e && *pos < e) {
  1132. over = e - *pos;
  1133. if (over <= WM_RESIST && over < *best) {
  1134. *best = over;
  1135. *pos = e;
  1136. }
  1137. } else if (dir > 0 && *pos > lo && lo + len <= e && *pos + len > e) {
  1138. over = *pos + len - e;
  1139. if (over <= WM_RESIST && over < *best) {
  1140. *best = over;
  1141. *pos = e - len;
  1142. }
  1143. }
  1144. }
  1145. /* Span overlap test: edges of another window only resist when the two
  1146. * windows share some extent on the other axis (they would actually touch). */
  1147. static int spans_overlap(int a, int alen, int b, int blen)
  1148. {
  1149. return a < b + blen && b < a + alen;
  1150. }
  1151. /* Apply edge resistance to a drag from (u->info.x, u->info.y) to (*nx, *ny):
  1152. * screen edges and the facing edges of every other window. The proposed
  1153. * position is still pure pointer arithmetic, so once the overshoot passes
  1154. * WM_RESIST the window jumps to follow the pointer again. */
  1155. static void resist_geom(struct uwin *u, int idx, int *nx, int *ny)
  1156. {
  1157. int ox = u->info.x, oy = u->info.y;
  1158. int w = (int)u->info.w, h = (int)u->info.h;
  1159. int px = *nx, py = *ny;
  1160. int bestx = WM_RESIST + 1, besty = WM_RESIST + 1;
  1161. int i;
  1162. resist_edge(ox, w, &px, 0, -1, &bestx);
  1163. resist_edge(ox, w, &px, (int)fbw, 1, &bestx);
  1164. resist_edge(oy, h, &py, (int)fbh, 1, &besty);
  1165. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1166. struct wm_window_info *o = &wins[i].info;
  1167. int t;
  1168. if (i == idx || !o->used)
  1169. continue;
  1170. if (spans_overlap(oy, h, o->y, (int)o->h)) {
  1171. /* our left side against its right, our right
  1172. * side against its left */
  1173. t = *nx;
  1174. resist_edge(ox, w, &t, o->x + (int)o->w, -1, &bestx);
  1175. resist_edge(ox, w, &t, o->x, 1, &bestx);
  1176. if (t != *nx)
  1177. px = t;
  1178. }
  1179. if (spans_overlap(ox, w, o->x, (int)o->w)) {
  1180. t = *ny;
  1181. resist_edge(oy, h, &t, o->y + (int)o->h, -1, &besty);
  1182. resist_edge(oy, h, &t, o->y, 1, &besty);
  1183. if (t != *ny)
  1184. py = t;
  1185. }
  1186. }
  1187. *nx = px;
  1188. *ny = py;
  1189. }
  1190. /* The top-bar date/time text is also a button: it opens the Date & Time
  1191. * editor. The rectangle is recorded by draw_bar(). */
  1192. static int bar_clock_hit(unsigned int mx, unsigned int my)
  1193. {
  1194. return bar_clock_w && bar_clock_x >= 0 && my < WM_BAR_H &&
  1195. (int)mx >= bar_clock_x - 5 &&
  1196. (int)mx < bar_clock_x + (int)bar_clock_w + 5;
  1197. }
  1198. static int bar_net_hit(unsigned int mx, unsigned int my)
  1199. {
  1200. return bar_net_w && bar_net_x >= 0 && my < WM_BAR_H &&
  1201. (int)mx >= bar_net_x - 3 &&
  1202. (int)mx < bar_net_x + (int)bar_net_w + 3;
  1203. }
  1204. /* The AC/battery icon opens the control panel on a click; hovering it also
  1205. * reveals the percentage tooltip below it. The tooltip area keeps the hover
  1206. * alive but is not a click target. */
  1207. static int bar_pwr_hit(unsigned int mx, unsigned int my)
  1208. {
  1209. if (my >= WM_BAR_H)
  1210. return bar_pwr_tip_on &&
  1211. (int)mx >= bar_pwr_tip_on_x &&
  1212. (int)mx < bar_pwr_tip_on_x + (int)bar_pwr_tip_on_w &&
  1213. (int)my >= bar_pwr_tip_on_y &&
  1214. (int)my < bar_pwr_tip_on_y + (int)bar_pwr_tip_on_h;
  1215. return bar_pwr_x >= 0 &&
  1216. (int)mx >= bar_pwr_x - 3 &&
  1217. (int)mx < bar_pwr_x + BAR_PWR_W + 3;
  1218. }
  1219. /* The whole right status area is one button: the clock, temperature, network
  1220. * and power items plus the gaps between them. The launcher buttons end at
  1221. * WM_BTN_END_X, so this never overlaps them. */
  1222. static int bar_status_hit(unsigned int mx, unsigned int my)
  1223. {
  1224. return my < WM_BAR_H && (int)mx >= WM_BTN_END_X + 4;
  1225. }
  1226. static void do_update(struct wm_status *st)
  1227. {
  1228. struct wm_present_info present;
  1229. int need = 0;
  1230. int bar_dirty = 0;
  1231. unsigned int i;
  1232. char dt[20];
  1233. static char last_dt[20];
  1234. int kexec_changed = (st->kexec_busy != bar_kexec_busy) ||
  1235. (st->kexec_busy && st->kexec_progress != bar_kexec_progress);
  1236. unsigned int msg_now = u_ticks_ms();
  1237. int osd_hide = 0, osd_repaint = 0;
  1238. /* The kernel bumps backlight_seq on every brightness key/command.
  1239. * Pop the OSD on a change and keep it up for OSD_MS after the last
  1240. * one. The first poll only records the current sequence, so a WM
  1241. * restart does not flash a stale bubble. */
  1242. if (st->backlight_present) {
  1243. if (!osd_seen_valid) {
  1244. osd_seen_valid = 1;
  1245. osd_seen_seq = st->backlight_seq;
  1246. } else if (st->backlight_seq != osd_seen_seq) {
  1247. osd_seen_seq = st->backlight_seq;
  1248. osd_level = (int)st->backlight_level;
  1249. osd_until = msg_now + OSD_MS;
  1250. osd_show = 1;
  1251. }
  1252. } else if (osd_show) {
  1253. osd_show = 0;
  1254. osd_hide = 1;
  1255. }
  1256. if (osd_show && (int)(msg_now - osd_until) >= 0) {
  1257. osd_show = 0;
  1258. osd_hide = 1;
  1259. }
  1260. if (osd_hide && osd_visible)
  1261. osd_repaint = 1;
  1262. /* Spawn the first terminal once the kernel says the storage scan is
  1263. * done, so a shell command can never race the mount table. */
  1264. if (st->boot_ready && !boot_term_spawned) {
  1265. boot_term_spawned = 1;
  1266. if (!free_window_slot()) {
  1267. bar_notice("window limit");
  1268. full_redraw = 1;
  1269. } else if (u_wm_create_term() < 0) {
  1270. bar_notice("cannot start term");
  1271. full_redraw = 1;
  1272. } else {
  1273. full_redraw = 1;
  1274. }
  1275. }
  1276. if (bar_msg_dirty ||
  1277. (bar_msg_until && (int)(msg_now - bar_msg_until) >= 0)) {
  1278. bar_dirty = 1;
  1279. bar_msg_dirty = 0;
  1280. if (bar_msg_until && (int)(msg_now - bar_msg_until) >= 0)
  1281. bar_msg_until = 0;
  1282. }
  1283. bar_kexec_busy = st->kexec_busy;
  1284. bar_kexec_progress = st->kexec_progress;
  1285. bar_kexec_total = st->kexec_total;
  1286. if (kexec_changed)
  1287. bar_dirty = 1;
  1288. {
  1289. int hot = bar_clock_hit(st->mouse.x, st->mouse.y);
  1290. if (hot != bar_clock_hot) {
  1291. bar_clock_hot = hot;
  1292. bar_dirty = 1;
  1293. }
  1294. }
  1295. {
  1296. int hot = bar_net_hit(st->mouse.x, st->mouse.y);
  1297. if (hot != bar_net_hot) {
  1298. bar_net_hot = hot;
  1299. bar_dirty = 1;
  1300. }
  1301. }
  1302. {
  1303. int hot = bar_pwr_hit(st->mouse.x, st->mouse.y);
  1304. if (hot != bar_pwr_hot) {
  1305. bar_pwr_hot = hot;
  1306. bar_dirty = 1;
  1307. }
  1308. }
  1309. /* Refresh cached window state and mappings before opening a frame:
  1310. * MAP may unmap/remap content. */
  1311. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1312. struct wm_window_info info;
  1313. int r = u_wm_info(i, &info);
  1314. if (r < 0) {
  1315. u_exit(1);
  1316. }
  1317. if (r == 0 || !info.used) {
  1318. if (wins[i].info.used)
  1319. full_redraw = 1;
  1320. wins[i].info.used = 0;
  1321. wins[i].mapped = 0;
  1322. continue;
  1323. }
  1324. {
  1325. int pid_changed = wins[i].info.used &&
  1326. info.pid != wins[i].info.pid;
  1327. if (!wins[i].info.used ||
  1328. info.content_gen != wins[i].info.content_gen ||
  1329. pid_changed)
  1330. full_redraw = 1;
  1331. wins[i].info = info;
  1332. if (!wins[i].mapped || pid_changed ||
  1333. wins[i].map.generation != info.content_gen) {
  1334. struct wm_map_info map;
  1335. if (u_wm_map(i, &map) == 0) {
  1336. wins[i].map = map;
  1337. wins[i].mapped = 1;
  1338. } else {
  1339. wins[i].mapped = 0;
  1340. }
  1341. }
  1342. }
  1343. }
  1344. /* Popovers are WM-anchored overlays, flush against the bottom of the
  1345. * bar and the right screen edge. The WM owns the geometry, so the app
  1346. * does not need a placement ABI. */
  1347. {
  1348. int control_found = 0;
  1349. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1350. struct wm_window_info ni;
  1351. int nx, ny;
  1352. if (!wins[i].info.used || !wins[i].info.popover)
  1353. continue;
  1354. if (control_pid > 0 &&
  1355. wins[i].info.pid == (unsigned int)control_pid)
  1356. control_found = 1;
  1357. nx = (int)fbw - (int)wins[i].info.w;
  1358. ny = WM_BAR_H;
  1359. if (nx != wins[i].info.x || ny != wins[i].info.y) {
  1360. if (u_wm_set_geom(i, nx, ny) == 0 &&
  1361. u_wm_info(i, &ni) == 1) {
  1362. wins[i].info = ni;
  1363. full_redraw = 1;
  1364. }
  1365. }
  1366. }
  1367. if (!control_found && control_pid > 0 &&
  1368. !u_alive((unsigned int)control_pid))
  1369. control_pid = 0;
  1370. }
  1371. /* The RTC is slow (CMOS polling) and the temperature/battery state only
  1372. * changes once a second; query them at 1 Hz instead of every 10 ms. */
  1373. {
  1374. static unsigned int last_rtc_ms;
  1375. unsigned int now_ms = u_ticks_ms();
  1376. if (full_redraw || now_ms - last_rtc_ms >= 1000) {
  1377. last_rtc_ms = now_ms;
  1378. if (u_rtc_datetime(dt, sizeof(dt)) == 0) {
  1379. for (i = 0; i < sizeof(dt); i++)
  1380. if (dt[i] != last_dt[i]) {
  1381. for (i = 0; i < sizeof(dt); i++)
  1382. last_dt[i] = dt[i];
  1383. bar_dirty = 1;
  1384. break;
  1385. }
  1386. }
  1387. {
  1388. struct sys_cputemp ct;
  1389. if (u_cpu_temp(&ct) == 0 &&
  1390. ct.temp_c != bar_temp_c) {
  1391. bar_temp_c = ct.temp_c;
  1392. bar_dirty = 1;
  1393. }
  1394. }
  1395. {
  1396. struct sys_powerinfo pi;
  1397. int pct = -1, charging = 0, ac = -1;
  1398. unsigned int bi;
  1399. if (u_power_info(&pi, 0) == 0) {
  1400. /* A laptop EC (ThinkPad or Chrome
  1401. * EC) marks a machine with AC/battery;
  1402. * desktops show neither. */
  1403. if (pi.present) {
  1404. ac = pi.ac_online;
  1405. for (bi = 0; bi < SYS_POWER_BATT_MAX;
  1406. bi++) {
  1407. struct sys_battery *b =
  1408. &pi.battery[bi];
  1409. if (!b->present ||
  1410. !b->percent_valid)
  1411. continue;
  1412. pct = (int)b->percent;
  1413. charging = (int)b->charging;
  1414. break;
  1415. }
  1416. }
  1417. if (pct != bar_batt_pct ||
  1418. charging != bar_batt_charging ||
  1419. ac != bar_ac_online) {
  1420. bar_batt_pct = pct;
  1421. bar_batt_charging = charging;
  1422. bar_ac_online = ac;
  1423. bar_dirty = 1;
  1424. }
  1425. }
  1426. }
  1427. if (poll_net_status())
  1428. bar_dirty = 1;
  1429. }
  1430. }
  1431. if (full_redraw) {
  1432. if (u_wm_begin() != 0) {
  1433. full_redraw = 1;
  1434. return;
  1435. }
  1436. full_redraw = 0;
  1437. need = 1;
  1438. {
  1439. draw_rect(0, 0, fbw, fbh);
  1440. draw_bar();
  1441. bar_tooltip_commit();
  1442. if (osd_show)
  1443. osd_draw();
  1444. osd_visible = osd_show;
  1445. present.count = 0;
  1446. add_rect(&present, 0, 0, fbw, fbh);
  1447. present.immediate = 0;
  1448. u_wm_present(&present);
  1449. /* Ack every window that was clean at the time of the
  1450. * redraw. If new damage arrived during it, its seq
  1451. * differs and the kernel leaves it dirty. */
  1452. for (i = 0; i < WM_MAX_WINDOWS; i++)
  1453. if (wins[i].info.used && wins[i].mapped)
  1454. u_wm_ack(i, wins[i].info.dirty_seq);
  1455. }
  1456. return;
  1457. }
  1458. /* Partial cell/canvas updates and the bar clock. */
  1459. present.count = 0;
  1460. present.immediate = 0;
  1461. if (osd_repaint) {
  1462. add_rect(&present, osd_x, osd_y, osd_w, osd_h);
  1463. need = 1;
  1464. }
  1465. if (osd_show)
  1466. need = 1;
  1467. if (bar_dirty) {
  1468. need = 1;
  1469. add_rect(&present, 0, 0, fbw, WM_BAR_H);
  1470. }
  1471. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1472. struct uwin *u = &wins[i];
  1473. struct wm_window_info *w = &u->info;
  1474. int rx, ry;
  1475. unsigned int rw, rh;
  1476. if (!w->used || !w->dirty)
  1477. continue;
  1478. if (!w->dmg_w || !w->dmg_h)
  1479. continue;
  1480. rx = w->x + (w->popover ? 0 : WM_BORDER_W) + (int)w->dmg_x;
  1481. ry = w->y + (w->popover ? 0 : WM_BORDER_W + WM_TITLE_H) +
  1482. (int)w->dmg_y;
  1483. rw = w->dmg_w;
  1484. rh = w->dmg_h;
  1485. add_rect(&present, rx, ry, rw, rh);
  1486. u->ack_seq = u->mapped ? w->dirty_seq : 0;
  1487. need = 1;
  1488. }
  1489. /* Repaint the previous tooltip area so hiding or moving it restores
  1490. * the window/desktop underneath; the new one is drawn after the
  1491. * windows below. */
  1492. if (bar_pwr_tip_on) {
  1493. need = 1;
  1494. add_rect(&present, bar_pwr_tip_on_x, bar_pwr_tip_on_y,
  1495. bar_pwr_tip_on_w, bar_pwr_tip_on_h);
  1496. }
  1497. if (!need) {
  1498. /* Drag movement is handled below; a stationary mouse can be
  1499. * left to the kernel's cursor overlay. */
  1500. return;
  1501. }
  1502. if (u_wm_begin() != 0)
  1503. return;
  1504. for (i = 0; i < present.count; i++)
  1505. draw_rect(present.rect[i].x, present.rect[i].y,
  1506. present.rect[i].w, present.rect[i].h);
  1507. if (bar_dirty)
  1508. draw_bar();
  1509. if (bar_pwr_tip_want) {
  1510. bar_tooltip_commit();
  1511. add_rect(&present, bar_pwr_tip_on_x, bar_pwr_tip_on_y,
  1512. bar_pwr_tip_on_w, bar_pwr_tip_on_h);
  1513. } else {
  1514. bar_pwr_tip_on = 0;
  1515. }
  1516. if (osd_show) {
  1517. osd_draw();
  1518. add_rect(&present, osd_x, osd_y, osd_w, osd_h);
  1519. }
  1520. osd_visible = osd_show;
  1521. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1522. struct uwin *u = &wins[i];
  1523. if (!u->info.used || !u->info.dirty || !u->mapped ||
  1524. !u->ack_seq)
  1525. continue;
  1526. if (u->ack_seq == u->info.dirty_seq)
  1527. u_wm_ack(i, u->info.dirty_seq);
  1528. }
  1529. u_wm_present(&present);
  1530. }
  1531. static int spawn_app(const char *path)
  1532. {
  1533. int pid = u_spawn(path, "", 0, "/");
  1534. if (pid > 0)
  1535. full_redraw = 1;
  1536. return pid;
  1537. }
  1538. static void spawn_app_checked(const char *path)
  1539. {
  1540. if (!free_window_slot()) {
  1541. bar_notice("window limit");
  1542. full_redraw = 1;
  1543. return;
  1544. }
  1545. if (spawn_app(path) < 0) {
  1546. bar_notice("spawn failed");
  1547. full_redraw = 1;
  1548. }
  1549. }
  1550. /* Any status target opens one control panel; a second click closes it. */
  1551. static void open_control_panel(void)
  1552. {
  1553. unsigned int i;
  1554. if (control_pid > 0 && u_alive((unsigned int)control_pid)) {
  1555. for (i = 0; i < WM_MAX_WINDOWS; i++)
  1556. if (wins[i].info.used &&
  1557. wins[i].info.pid == (unsigned int)control_pid) {
  1558. if (u_wm_close(i) == 0) {
  1559. control_pid = 0;
  1560. full_redraw = 1;
  1561. } else {
  1562. u_wm_focus(i);
  1563. full_redraw = 1;
  1564. }
  1565. return;
  1566. }
  1567. /* The process is alive but has not created its window yet. */
  1568. return;
  1569. }
  1570. if (!free_window_slot()) {
  1571. bar_notice("window limit");
  1572. full_redraw = 1;
  1573. return;
  1574. }
  1575. control_pid = u_spawn("/ramfs/bin/netmenu", "", 0, "/");
  1576. if (control_pid < 0) {
  1577. control_pid = 0;
  1578. bar_notice("spawn failed");
  1579. full_redraw = 1;
  1580. } else {
  1581. full_redraw = 1;
  1582. }
  1583. }
  1584. /* Index of a popover containing the point, or -1. */
  1585. static int popover_under(unsigned int mx, unsigned int my)
  1586. {
  1587. unsigned int i;
  1588. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1589. struct wm_window_info *w = &wins[i].info;
  1590. if (w->used && w->popover &&
  1591. mx >= (unsigned int)w->x && my >= (unsigned int)w->y &&
  1592. mx < (unsigned int)(w->x + (int)w->w) &&
  1593. my < (unsigned int)(w->y + (int)w->h))
  1594. return (int)i;
  1595. }
  1596. return -1;
  1597. }
  1598. /* A press anywhere outside an open popover dismisses it; the click then
  1599. * continues normally so the target under the pointer still reacts. */
  1600. static void dismiss_popover_outside(unsigned int mx, unsigned int my)
  1601. {
  1602. unsigned int i;
  1603. if (popover_under(mx, my) >= 0)
  1604. return;
  1605. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1606. if (!wins[i].info.used || !wins[i].info.popover)
  1607. continue;
  1608. if (wins[i].info.pid == (unsigned int)control_pid)
  1609. control_pid = 0;
  1610. if (u_wm_close(i) == 0) {
  1611. wins[i].info.used = 0;
  1612. wins[i].mapped = 0;
  1613. full_redraw = 1;
  1614. }
  1615. break;
  1616. }
  1617. }
  1618. /* Top-bar launcher under (mx, my)? Same rectangles as the click chain in
  1619. * handle_mouse(); every launcher shares WM_BTN_Y/WM_BTN_H. */
  1620. static int bar_launcher_hit(unsigned int mx, unsigned int my)
  1621. {
  1622. static const int x0[] = {
  1623. WM_BTN_X, WM_BTN_CLOCK_X, WM_BTN_COLORS_X, WM_BTN_FILES_X,
  1624. WM_BTN_MINES_X, WM_BTN_WEB_X, WM_BTN_CARDS_X, WM_BTN_GALLERY_X,
  1625. WM_BTN_CHAT_X, WM_BTN_DISKS_X
  1626. };
  1627. static const int w[] = {
  1628. WM_BTN_W, WM_BTN_CLOCK_W, WM_BTN_COLORS_W, WM_BTN_FILES_W,
  1629. WM_BTN_MINES_W, WM_BTN_WEB_W, WM_BTN_CARDS_W, WM_BTN_GALLERY_W,
  1630. WM_BTN_CHAT_W, WM_BTN_DISKS_W
  1631. };
  1632. unsigned int i;
  1633. if ((int)my < WM_BTN_Y || (int)my >= WM_BTN_Y + WM_BTN_H)
  1634. return 0;
  1635. for (i = 0; i < sizeof(x0) / sizeof(x0[0]); i++)
  1636. if ((int)mx >= x0[i] && (int)mx < x0[i] + w[i])
  1637. return 1;
  1638. return 0;
  1639. }
  1640. /* Apply one edge-drag step. The grabbed edges follow the pointer while the
  1641. * opposite edges stay put; the work area clamps the active edges. */
  1642. static void resize_window_drag(struct uwin *u, unsigned int idx,
  1643. unsigned int mx, unsigned int my)
  1644. {
  1645. int dx = (int)mx - (int)resize_mx;
  1646. int dy = (int)my - (int)resize_my;
  1647. int left = resize_ox;
  1648. int right = resize_ox + (int)resize_ow;
  1649. int top = resize_oy;
  1650. int bottom = resize_oy + (int)resize_oh;
  1651. int nx, ny, nw, nh;
  1652. if (resize_edges & WM_EDGE_LEFT) {
  1653. left = resize_ox + dx;
  1654. if (left < 0)
  1655. left = 0;
  1656. } else if (resize_edges & WM_EDGE_RIGHT) {
  1657. right = resize_ox + (int)resize_ow + dx;
  1658. if (right > (int)fbw)
  1659. right = (int)fbw;
  1660. }
  1661. if (resize_edges & WM_EDGE_TOP) {
  1662. top = resize_oy + dy;
  1663. if (top < WM_BAR_H)
  1664. top = WM_BAR_H;
  1665. } else if (resize_edges & WM_EDGE_BOTTOM) {
  1666. bottom = resize_oy + (int)resize_oh + dy;
  1667. if (bottom > (int)fbh)
  1668. bottom = (int)fbh;
  1669. }
  1670. if ((resize_edges & (WM_EDGE_LEFT | WM_EDGE_RIGHT)) &&
  1671. right - left < 200) {
  1672. if (resize_edges & WM_EDGE_LEFT)
  1673. left = right - 200;
  1674. else
  1675. right = left + 200;
  1676. }
  1677. if ((resize_edges & (WM_EDGE_TOP | WM_EDGE_BOTTOM)) &&
  1678. bottom - top < 140) {
  1679. if (resize_edges & WM_EDGE_TOP)
  1680. top = bottom - 140;
  1681. else
  1682. bottom = top + 140;
  1683. }
  1684. if (left < 0)
  1685. left = 0;
  1686. if (top < WM_BAR_H)
  1687. top = WM_BAR_H;
  1688. nx = left;
  1689. ny = top;
  1690. nw = right - left;
  1691. nh = bottom - top;
  1692. if (nw > (int)fbw)
  1693. nw = (int)fbw;
  1694. if (nh > (int)fbh - WM_BAR_H)
  1695. nh = (int)fbh - WM_BAR_H;
  1696. if (nx == u->info.x && ny == u->info.y &&
  1697. nw == (int)u->info.w && nh == (int)u->info.h)
  1698. return;
  1699. if (set_window_geometry(u, idx, nx, ny, (unsigned int)nw,
  1700. (unsigned int)nh) == 0)
  1701. u->state = WIN_NORMAL;
  1702. }
  1703. /* Capture the pointer for an edge resize that started at (mx,my). */
  1704. static void begin_resize(unsigned int idx, int edges, unsigned int mx,
  1705. unsigned int my)
  1706. {
  1707. struct uwin *u = &wins[idx];
  1708. resize_id = (int)idx;
  1709. resize_edges = edges;
  1710. resize_mx = mx;
  1711. resize_my = my;
  1712. resize_ox = u->info.x;
  1713. resize_oy = u->info.y;
  1714. resize_ow = u->info.w;
  1715. resize_oh = u->info.h;
  1716. u_wm_mouse_capture(1);
  1717. }
  1718. /* The WM's cursor override (SYS_WM_SET_CURSOR): resize arrows on the
  1719. * invisible resize border and while resizing, the move cross while a
  1720. * title-bar drag is under way, a hand over the launchers and the status
  1721. * area. Everywhere else the kernel picks the window's own content cursor
  1722. * (or the arrow). */
  1723. static void update_cursor(unsigned int mx, unsigned int my)
  1724. {
  1725. static int current = CURSOR_NO_OVERRIDE;
  1726. int want = CURSOR_NO_OVERRIDE;
  1727. if (resize_id >= 0) {
  1728. want = resize_cursor(resize_edges);
  1729. } else if (drag_id >= 0) {
  1730. if (drag_moved)
  1731. want = CURSOR_MOVE;
  1732. } else if (my < WM_BAR_H) {
  1733. if (bar_launcher_hit(mx, my) || bar_status_hit(mx, my))
  1734. want = CURSOR_POINTER;
  1735. } else {
  1736. int edges = 0;
  1737. int idx = resize_hit((int)mx, (int)my, &edges);
  1738. if (idx >= 0 && edges &&
  1739. !close_hit(&wins[idx].info, (int)mx, (int)my))
  1740. want = resize_cursor(edges);
  1741. }
  1742. if (want != current && u_wm_set_cursor(want) == 0)
  1743. current = want;
  1744. }
  1745. static void handle_mouse(struct wm_status *st)
  1746. {
  1747. unsigned int buttons = st->mouse.buttons;
  1748. unsigned int mx = st->mouse.x, my = st->mouse.y;
  1749. int down = (buttons & 1) != 0;
  1750. int was = (prev_buttons & 1) != 0;
  1751. if (down && !was) {
  1752. int prev_title_id = last_title_id;
  1753. unsigned int now = u_ticks_ms();
  1754. if (!bar_status_hit(mx, my))
  1755. dismiss_popover_outside(mx, my);
  1756. last_title_id = -1;
  1757. if (bar_status_hit(mx, my)) {
  1758. open_control_panel();
  1759. } else if (mx >= WM_BTN_X && my >= WM_BTN_Y &&
  1760. mx < WM_BTN_X + WM_BTN_W && my < WM_BTN_Y + WM_BTN_H) {
  1761. if (!free_window_slot()) {
  1762. bar_notice("window limit");
  1763. full_redraw = 1;
  1764. } else if (u_wm_create_term() < 0) {
  1765. bar_notice("cannot start term");
  1766. full_redraw = 1;
  1767. } else {
  1768. full_redraw = 1;
  1769. }
  1770. } else if (mx >= WM_BTN_CLOCK_X && my >= WM_BTN_CLOCK_Y &&
  1771. mx < WM_BTN_CLOCK_X + WM_BTN_CLOCK_W &&
  1772. my < WM_BTN_CLOCK_Y + WM_BTN_CLOCK_H) {
  1773. spawn_app_checked("/ramfs/bin/clock");
  1774. } else if (mx >= WM_BTN_COLORS_X && my >= WM_BTN_COLORS_Y &&
  1775. mx < WM_BTN_COLORS_X + WM_BTN_COLORS_W &&
  1776. my < WM_BTN_COLORS_Y + WM_BTN_COLORS_H) {
  1777. spawn_app_checked("/ramfs/bin/colors");
  1778. } else if (mx >= WM_BTN_FILES_X && my >= WM_BTN_FILES_Y &&
  1779. mx < WM_BTN_FILES_X + WM_BTN_FILES_W &&
  1780. my < WM_BTN_FILES_Y + WM_BTN_FILES_H) {
  1781. spawn_app_checked("/ramfs/bin/files");
  1782. } else if (mx >= WM_BTN_MINES_X && my >= WM_BTN_MINES_Y &&
  1783. mx < WM_BTN_MINES_X + WM_BTN_MINES_W &&
  1784. my < WM_BTN_MINES_Y + WM_BTN_MINES_H) {
  1785. spawn_app_checked("/ramfs/bin/mines");
  1786. } else if (mx >= WM_BTN_WEB_X && my >= WM_BTN_WEB_Y &&
  1787. mx < WM_BTN_WEB_X + WM_BTN_WEB_W &&
  1788. my < WM_BTN_WEB_Y + WM_BTN_WEB_H) {
  1789. spawn_app_checked("/ramfs/bin/browser");
  1790. } else if (mx >= WM_BTN_CARDS_X && my >= WM_BTN_CARDS_Y &&
  1791. mx < WM_BTN_CARDS_X + WM_BTN_CARDS_W &&
  1792. my < WM_BTN_CARDS_Y + WM_BTN_CARDS_H) {
  1793. spawn_app_checked("/ramfs/bin/solitaire");
  1794. } else if (mx >= WM_BTN_GALLERY_X && my >= WM_BTN_GALLERY_Y &&
  1795. mx < WM_BTN_GALLERY_X + WM_BTN_GALLERY_W &&
  1796. my < WM_BTN_GALLERY_Y + WM_BTN_GALLERY_H) {
  1797. spawn_app_checked("/ramfs/bin/gallery");
  1798. } else if (mx >= WM_BTN_CHAT_X && my >= WM_BTN_CHAT_Y &&
  1799. mx < WM_BTN_CHAT_X + WM_BTN_CHAT_W &&
  1800. my < WM_BTN_CHAT_Y + WM_BTN_CHAT_H) {
  1801. spawn_app_checked("/ramfs/bin/chat");
  1802. } else if (mx >= WM_BTN_DISKS_X && my >= WM_BTN_DISKS_Y &&
  1803. mx < WM_BTN_DISKS_X + WM_BTN_DISKS_W &&
  1804. my < WM_BTN_DISKS_Y + WM_BTN_DISKS_H) {
  1805. spawn_app_checked("/ramfs/bin/disks");
  1806. } else {
  1807. int edges = 0;
  1808. int idx = resize_hit((int)mx, (int)my, &edges);
  1809. if (idx >= 0) {
  1810. struct uwin *u = &wins[idx];
  1811. int pmx = (int)mx, pmy = (int)my;
  1812. u_wm_focus((unsigned int)idx);
  1813. if (u->info.popover) {
  1814. /* A popover has no title bar, close
  1815. * button or resize border; the app owns
  1816. * its content. */
  1817. } else if (close_hit(&u->info, pmx, pmy)) {
  1818. u_wm_close((unsigned int)idx);
  1819. full_redraw = 1;
  1820. } else if (edges) {
  1821. begin_resize((unsigned int)idx, edges,
  1822. mx, my);
  1823. } else if (pmy < u->info.y + WM_TITLE_H) {
  1824. if (prev_title_id == idx &&
  1825. now - last_title_ms <= WM_DBLCLICK_MS &&
  1826. iabs(pmx - last_title_x) <= WM_DBLCLICK_SLOP &&
  1827. iabs(pmy - last_title_y) <= WM_DBLCLICK_SLOP) {
  1828. last_title_ms = 0;
  1829. if (toggle_maximize(u,
  1830. (unsigned int)idx) != 0)
  1831. bar_notice("resize failed");
  1832. } else {
  1833. last_title_id = idx;
  1834. last_title_ms = now;
  1835. last_title_x = pmx;
  1836. last_title_y = pmy;
  1837. drag_id = idx;
  1838. drag_mx = pmx;
  1839. drag_my = pmy;
  1840. drag_dx = pmx - u->info.x;
  1841. drag_dy = pmy - u->info.y;
  1842. drag_moved = 0;
  1843. u_wm_mouse_capture(1);
  1844. }
  1845. }
  1846. }
  1847. }
  1848. } else if (down && was && resize_id >= 0) {
  1849. struct uwin *u = &wins[resize_id];
  1850. if (u->info.used)
  1851. resize_window_drag(u, (unsigned int)resize_id, mx, my);
  1852. else {
  1853. resize_id = -1;
  1854. u_wm_mouse_capture(0);
  1855. }
  1856. } else if (down && was && drag_id >= 0) {
  1857. struct uwin *u = &wins[drag_id];
  1858. if (u->info.used) {
  1859. int ox, oy, nx, ny;
  1860. if (iabs((int)mx - drag_mx) >= WM_DRAG_START ||
  1861. iabs((int)my - drag_my) >= WM_DRAG_START)
  1862. drag_moved = 1;
  1863. /* A maximized or snapped window is restored as soon as
  1864. * the drag starts. Keep the grab point at the same
  1865. * fraction of the title bar, so the restored (narrower)
  1866. * window stays under the pointer. A click that does
  1867. * not move must leave it in place. */
  1868. if (u->state != WIN_NORMAL && drag_moved) {
  1869. unsigned int ow = u->info.w;
  1870. int gx = drag_mx - u->info.x;
  1871. int gy = drag_my - u->info.y;
  1872. if (restore_window(u,
  1873. (unsigned int)drag_id) == 0) {
  1874. if (ow)
  1875. gx = gx * (int)u->info.w /
  1876. (int)ow;
  1877. drag_dx = gx;
  1878. drag_dy = gy;
  1879. }
  1880. }
  1881. if (u->state == WIN_NORMAL) {
  1882. ox = u->info.x;
  1883. oy = u->info.y;
  1884. nx = (int)mx - drag_dx;
  1885. ny = (int)my - drag_dy;
  1886. resist_geom(u, drag_id, &nx, &ny);
  1887. clamp_geom(u, &nx, &ny);
  1888. if (nx != ox || ny != oy) {
  1889. struct wm_present_info present;
  1890. unsigned int i;
  1891. u_wm_set_geom((unsigned int)drag_id,
  1892. nx, ny);
  1893. u->info.x = nx;
  1894. u->info.y = ny;
  1895. if (u->info.focused) {
  1896. move_window_pixels(u, ox, oy,
  1897. nx, ny);
  1898. } else {
  1899. present.count = 0;
  1900. present.immediate = 1;
  1901. add_rect(&present, ox, oy,
  1902. u->info.w, u->info.h);
  1903. add_rect(&present, nx, ny,
  1904. u->info.w, u->info.h);
  1905. if (u_wm_begin() == 0) {
  1906. for (i = 0;
  1907. i < present.count; i++)
  1908. draw_rect(
  1909. present.rect[i].x,
  1910. present.rect[i].y,
  1911. present.rect[i].w,
  1912. present.rect[i].h);
  1913. wm_overlays(&present);
  1914. u_wm_present(&present);
  1915. }
  1916. }
  1917. }
  1918. }
  1919. } else {
  1920. drag_id = -1;
  1921. u_wm_mouse_capture(0);
  1922. }
  1923. } else if (!down && was) {
  1924. /* Dropping a window with the pointer on the left/right screen
  1925. * edge snaps it to that half; on the top edge it maximizes. */
  1926. if (drag_id >= 0 && drag_moved && wins[drag_id].info.used &&
  1927. wins[drag_id].state == WIN_NORMAL) {
  1928. struct uwin *u = &wins[drag_id];
  1929. int st = WIN_NORMAL;
  1930. if ((int)mx < WM_SNAP_EDGE)
  1931. st = WIN_SNAP_LEFT;
  1932. else if ((int)mx >= (int)fbw - WM_SNAP_EDGE)
  1933. st = WIN_SNAP_RIGHT;
  1934. else if ((int)my < WM_SNAP_EDGE)
  1935. st = WIN_MAXIMIZED;
  1936. if (st != WIN_NORMAL &&
  1937. place_window(u, (unsigned int)drag_id, st) != 0)
  1938. bar_notice("resize failed");
  1939. }
  1940. drag_id = -1;
  1941. resize_id = -1;
  1942. u_wm_mouse_capture(0);
  1943. }
  1944. prev_buttons = buttons;
  1945. update_cursor(mx, my);
  1946. }
  1947. static char *put_str(char *p, const char *s)
  1948. {
  1949. while (*s)
  1950. *p++ = *s++;
  1951. return p;
  1952. }
  1953. static char *put_dec(char *p, unsigned int v)
  1954. {
  1955. char b[10];
  1956. unsigned int n = 0;
  1957. do {
  1958. b[n++] = (char)('0' + v % 10);
  1959. v /= 10;
  1960. } while (v);
  1961. while (n)
  1962. *p++ = b[--n];
  1963. return p;
  1964. }
  1965. /* Expose the last interval in /share so the target can be measured without
  1966. * a serial console. */
  1967. static void write_stats(void)
  1968. {
  1969. char b[80], *p = b;
  1970. unsigned int now = u_ticks_ms();
  1971. p = put_str(p, "t=");
  1972. p = put_dec(p, now / 1000);
  1973. p = put_str(p, "s frames=");
  1974. p = put_dec(p, stat_frames);
  1975. p = put_str(p, " ms=");
  1976. p = put_dec(p, stat_ms);
  1977. p = put_str(p, " max=");
  1978. p = put_dec(p, stat_max);
  1979. p = put_str(p, " px=");
  1980. p = put_dec(p, stat_px);
  1981. p = put_str(p, "\n");
  1982. u_syscall3(SYS_WRITE_FILE, (int)"/share/wmstats", (int)b,
  1983. (int)(p - b));
  1984. }
  1985. void _start(const char *arg)
  1986. {
  1987. struct wm_screen_info si;
  1988. struct wm_status st;
  1989. unsigned int i;
  1990. (void)arg;
  1991. if (u_wm_attach(&si) != 0)
  1992. u_exit(1);
  1993. usurf_init(&screen, si.fb, si.width, si.height, si.pitch, si.bpp);
  1994. usurf_font_attach(&screen, "/ramfs/fonts/sans.ttf", 14);
  1995. fbw = si.width;
  1996. fbh = si.height;
  1997. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1998. wins[i].info.used = 0;
  1999. wins[i].mapped = 0;
  2000. }
  2001. full_redraw = 1;
  2002. for (;;) {
  2003. unsigned int t0, t1;
  2004. if (u_wm_poll(&st) != 0)
  2005. u_exit(1);
  2006. if (st.full_redraw)
  2007. full_redraw = 1;
  2008. usurf_pixels = 0;
  2009. t0 = u_ticks_ms();
  2010. handle_mouse(&st);
  2011. do_update(&st);
  2012. t1 = u_ticks_ms();
  2013. if (usurf_pixels) {
  2014. unsigned int dt = t1 - t0;
  2015. stat_frames++;
  2016. stat_ms += dt;
  2017. stat_px += usurf_pixels;
  2018. if (dt > stat_max)
  2019. stat_max = dt;
  2020. }
  2021. if (t1 - stat_last >= 2000) {
  2022. stat_last = t1;
  2023. if (stat_frames) {
  2024. write_stats();
  2025. stat_frames = stat_ms = stat_px = stat_max = 0;
  2026. }
  2027. }
  2028. u_sleep_ms(10);
  2029. }
  2030. }