ucmd_wm.c 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739
  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. /* Desktop wallpaper: the viewer stores a header plus RGB565 pixels; the WM
  50. * only has to stretch that small buffer to the framebuffer, so no image
  51. * decoder is linked in. The static buffer avoids pulling malloc into the
  52. * WM and stays within the 4 MB user image window. */
  53. #define WALL_MAP_MAX 2048
  54. static unsigned char wall_data[sizeof(struct wm_wallpaper_hdr) +
  55. (unsigned int)WM_WALLPAPER_MAX_W * WM_WALLPAPER_MAX_H * 2u + 512];
  56. static unsigned int wall_w, wall_h;
  57. static int wall_ready;
  58. static unsigned int wall_seq_seen;
  59. static int wall_boot_seen;
  60. static int wall_retries;
  61. static unsigned int wall_retry_ms;
  62. static unsigned short wall_xmap[WALL_MAP_MAX];
  63. static unsigned short wall_ymap[WALL_MAP_MAX];
  64. /* RGB565 -> 0xFFRRGGBB, built when the wallpaper loads so the 32 bpp
  65. * desktop path is a table lookup instead of per-pixel shifts. */
  66. static unsigned int wall_rgb32[65536];
  67. static int drag_id = -1;
  68. static int drag_dx, drag_dy;
  69. static int drag_mx, drag_my; /* press position, for restore-on-drag */
  70. static int drag_moved; /* the pointer passed WM_DRAG_START */
  71. static int resize_id = -1;
  72. static int resize_edges; /* WM_EDGE_* bits captured at the press */
  73. static int resize_ox, resize_oy;
  74. static unsigned int resize_mx, resize_my, resize_ow, resize_oh;
  75. static unsigned int last_title_ms;
  76. static int last_title_id = -1, last_title_x, last_title_y;
  77. static unsigned int prev_buttons;
  78. static int full_redraw = 1;
  79. static int boot_term_spawned;
  80. static int boot_scan_spawned;
  81. static int boot_scan_pid;
  82. static int boot_scan_placed;
  83. static unsigned int boot_scan_deadline;
  84. static unsigned int bar_kexec_busy, bar_kexec_progress, bar_kexec_total;
  85. static void remap_window_contents(void);
  86. static int bar_temp_c = -1;
  87. static int bar_batt_pct = -1;
  88. static int bar_batt_charging;
  89. static int bar_ac_online = -1;
  90. /* Both top-bar buttons share this geometry: one rounded pill that stops one
  91. * pixel above the rainbow separator (which starts at WM_BAR_H - 5). */
  92. #define BAR_BTN_Y 2
  93. #define BAR_BTN_H 22
  94. #define BAR_BTN_RADIUS 6
  95. /* Battery/AC status icon: the Adwaita AC adapter on AC, the battery outline
  96. * with our own level fill otherwise. The rightmost bar item. */
  97. #define BAR_PWR_W 24
  98. #define BAR_PWR_Y 7
  99. /* The clock, temperature, network and AC/battery labels are one status
  100. * button. draw_bar() records their bounding rectangle; only that rectangle
  101. * opens the control panel, so the empty part of the bar is not clickable. */
  102. static int bar_status_x = -1;
  103. static unsigned int bar_status_w;
  104. static int bar_status_hot;
  105. /* Brightness OSD: a centered bubble shown after the kernel's backlight
  106. * seq changes, then hidden after OSD_MS. */
  107. #define OSD_MS 3000
  108. #define OSD_W 210
  109. #define OSD_H 60
  110. static int osd_show; /* should be visible this frame */
  111. static int osd_visible; /* pixels are on screen */
  112. static int osd_level;
  113. static unsigned int osd_until;
  114. static unsigned int osd_seen_seq;
  115. static int osd_seen_valid;
  116. static int osd_x, osd_y;
  117. static unsigned int osd_w, osd_h;
  118. static char bar_msg[24];
  119. static unsigned int bar_msg_until;
  120. static int bar_msg_dirty;
  121. static unsigned int stat_frames, stat_ms, stat_px, stat_max, stat_last;
  122. /* ---- Start menu --------------------------------------------------------
  123. * The NebOS banner in the top-left corner is the start button. It opens a
  124. * Windows-like panel drawn by the WM itself (not a window, so it never
  125. * consumes a WM_MAX_WINDOWS slot and floats above every canvas). The panel
  126. * lists every GUI app in the pack and ends with a Power row whose submenu
  127. * reboots or powers off the machine through the privileged kernel command
  128. * registry (SYS_SPAWN_KCMD). */
  129. /* The panel mirrors ngui_theme_dark and the netmenu popover: a flat
  130. * theme.bg surface without a rim, rounded theme.panel summary cards,
  131. * theme.panel_alt hover rows and the selection colour for an active item. */
  132. #define SM_BG 0x181C28u
  133. #define SM_PANEL 0x262B3Au
  134. #define SM_PANEL_ALT 0x32384Au
  135. #define SM_TEXT 0xE4E6F0u
  136. #define SM_TEXT_DIM 0x9197AAu
  137. #define SM_SEL 0x365CA6u
  138. #define SM_BTN_HOT 0x46516Cu
  139. #define SM_ERROR 0xEE7474u
  140. #define SM_W 252
  141. #define SM_PAD 12
  142. #define SM_TOP 10
  143. #define SM_GAP 6
  144. #define SM_SECT_H 22
  145. #define SM_ROW_H 26
  146. #define SM_MIN_ROW 20
  147. #define SM_BOTTOM 10
  148. #define SM_RADIUS 6
  149. #define SM_SUB_W 160
  150. #define SM_TILE 18
  151. #define SM_HIT_NONE (-1)
  152. #define SM_HIT_START (-2)
  153. #define SM_HIT_POWER (-3)
  154. #define SM_HIT_REBOOT (-4)
  155. #define SM_HIT_POWEROFF (-5)
  156. #define SM_HIT_MENU (-6) /* inside the panel but not on an entry */
  157. #define SM_ACT_SPAWN 0
  158. #define SM_ACT_TERM 1
  159. #define SM_ACT_SETTINGS 2
  160. struct sm_app {
  161. const char *label;
  162. const char *path;
  163. const char *arg;
  164. unsigned int tile;
  165. char letter;
  166. unsigned char action;
  167. };
  168. /* Every self-contained GUI application in the pack. Terminal and Settings
  169. * are WM actions (a new terminal canvas and the control panel); edit gets a
  170. * scratch file so it can start without a file argument. */
  171. static const struct sm_app sm_apps[] = {
  172. { "Terminal", "", "", 0x3C8CD2u, 'T', SM_ACT_TERM },
  173. { "Files", "/ramfs/bin/files", "", 0xE0A030u, 'F', SM_ACT_SPAWN },
  174. { "Web", "/ramfs/bin/browser", "", 0x4CA0E0u, 'W', SM_ACT_SPAWN },
  175. { "Gallery", "/ramfs/bin/gallery", "", 0xC864B4u, 'G', SM_ACT_SPAWN },
  176. { "Chat", "/ramfs/bin/chat", "", 0x55A055u, 'C', SM_ACT_SPAWN },
  177. { "Clock", "/ramfs/bin/clock", "", 0x8C8C9Cu, 'C', SM_ACT_SPAWN },
  178. { "Text editor", "/ramfs/bin/edit", "/share/UNTITLED.TXT", 0xD27850u, 'E', SM_ACT_SPAWN },
  179. { "Colors", "/ramfs/bin/colors", "", 0xC85A5Au, 'P', SM_ACT_SPAWN },
  180. { "Mines", "/ramfs/bin/mines", "", 0x8C6CD2u, 'M', SM_ACT_SPAWN },
  181. { "Cards", "/ramfs/bin/solitaire","", 0x3CA08Cu, 'S', SM_ACT_SPAWN },
  182. { "Disks", "/ramfs/bin/disks", "", 0x5A8CC8u, 'D', SM_ACT_SPAWN },
  183. { "Tasks", "/ramfs/bin/process", "", 0xE0B040u, 'P', SM_ACT_SPAWN },
  184. { "Settings", "", "", 0x7A7A9Au, 'S', SM_ACT_SETTINGS },
  185. { "Date & Time", "/ramfs/bin/settime", "", 0x5AA0A0u, 'D', SM_ACT_SPAWN },
  186. };
  187. #define SM_NAPP ((int)(sizeof(sm_apps) / sizeof(sm_apps[0])))
  188. struct sm_layout {
  189. int x, y, w, h;
  190. int row_h;
  191. int sect_y;
  192. int row_x, row_w;
  193. int list_y;
  194. int power_y;
  195. int sub_x, sub_y, sub_w, sub_h;
  196. };
  197. static int sm_open;
  198. static int sm_power_open;
  199. static int sm_start_hot;
  200. static int sm_hot; /* hovered app index, -1 = none */
  201. static int sm_power_hot; /* 0 none, 1 Power, 2 Reboot, 3 Power off */
  202. static void sm_layout_get(struct sm_layout *l);
  203. static void sm_draw(void);
  204. static void sm_add_present(struct wm_present_info *p);
  205. static int sm_update_hover(unsigned int mx, unsigned int my);
  206. static void sm_close(void);
  207. static void sm_launch(int idx);
  208. /* Network status icon (right side of the bar). */
  209. #define BAR_NET_W 24
  210. #define BAR_NET_Y 7
  211. #define BAR_NET_NONE 0
  212. #define BAR_NET_WIRED 1
  213. #define BAR_NET_WIFI 2
  214. #define BAR_NET_USB 3
  215. static int bar_net_kind;
  216. static int bar_net_bars;
  217. static unsigned int bar_net_color = 0xFF5050u;
  218. static int control_pid;
  219. static void bar_notice(const char *msg)
  220. {
  221. unsigned int i;
  222. for (i = 0; msg[i] && i + 1 < sizeof(bar_msg); i++)
  223. bar_msg[i] = msg[i];
  224. bar_msg[i] = '\0';
  225. bar_msg_until = u_ticks_ms() + 2000;
  226. bar_msg_dirty = 1;
  227. }
  228. static int free_window_slot(void)
  229. {
  230. unsigned int i;
  231. for (i = 0; i < WM_MAX_WINDOWS; i++)
  232. if (!wins[i].info.used)
  233. return 1;
  234. return 0;
  235. }
  236. /* Find a window by owner pid without waiting for the cached sync state:
  237. * boot ordering needs it before the terminal is spawned. */
  238. static int window_index_pid(int pid, struct wm_window_info *out)
  239. {
  240. unsigned int i;
  241. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  242. struct wm_window_info info;
  243. if (u_wm_info(i, &info) == 1 && info.used &&
  244. (int)info.pid == pid) {
  245. if (out)
  246. *out = info;
  247. return (int)i;
  248. }
  249. }
  250. return -1;
  251. }
  252. static unsigned int rgb16(unsigned int r, unsigned int g, unsigned int b)
  253. {
  254. return (r << 16) | (g << 8) | b;
  255. }
  256. /* Rainbow gradient: hue 0..1535 (6*256) maps to R→Y→G→C→B→M→R, the same
  257. * integer interpolation the kernel bar uses. */
  258. static unsigned int rainbow_rgb(unsigned int hue)
  259. {
  260. unsigned int seg = hue / 256, f = hue % 256;
  261. unsigned int r1, g1, b1, r2, g2, b2;
  262. switch (seg) {
  263. case 0: r1=255; g1=0; b1=0; r2=255; g2=255; b2=0; break;
  264. case 1: r1=255; g1=255; b1=0; r2=0; g2=255; b2=0; break;
  265. case 2: r1=0; g1=255; b1=0; r2=0; g2=255; b2=255; break;
  266. case 3: r1=0; g1=255; b1=255; r2=0; g2=0; b2=255; break;
  267. case 4: r1=0; g1=0; b1=255; r2=255; g2=0; b2=255; break;
  268. default:r1=255; g1=0; b1=255; r2=255; g2=0; b2=0; break;
  269. }
  270. return rgb16((r1 * (256 - f) + r2 * f) / 256,
  271. (g1 * (256 - f) + g2 * f) / 256,
  272. (b1 * (256 - f) + b2 * f) / 256);
  273. }
  274. static void fill_clip(int x, int y, unsigned int w, unsigned int h,
  275. unsigned int rgb, int cx, int cy,
  276. unsigned int cw, unsigned int ch)
  277. {
  278. int x0 = x, y0 = y, x1 = x + (int)w, y1 = y + (int)h;
  279. if (x0 < cx) x0 = cx;
  280. if (y0 < cy) y0 = cy;
  281. if (x1 > cx + (int)cw) x1 = cx + (int)cw;
  282. if (y1 > cy + (int)ch) y1 = cy + (int)ch;
  283. if (x1 > x0 && y1 > y0)
  284. usurf_fill(&screen, x0, y0, (unsigned int)(x1 - x0),
  285. (unsigned int)(y1 - y0), rgb);
  286. }
  287. /* Rounded rectangle fill, matching ngui_fill_round_rect()'s pixel-centre
  288. * corner sampling (the popover's cards and tiles use a 6 px radius). */
  289. static int sm_isqrt(int v)
  290. {
  291. int r = 0, bit = 1 << 30;
  292. if (v <= 0)
  293. return 0;
  294. while (bit > v)
  295. bit >>= 2;
  296. while (bit) {
  297. if (v >= r + bit) {
  298. v -= r + bit;
  299. r = (r >> 1) + bit;
  300. } else {
  301. r >>= 1;
  302. }
  303. bit >>= 2;
  304. }
  305. return r;
  306. }
  307. static int sm_round_inset(int y, int h, int r)
  308. {
  309. int dy2, d2;
  310. if (r <= 0)
  311. return 0;
  312. if (y < r)
  313. dy2 = 2 * r - 2 * y - 1;
  314. else if (y >= h - r)
  315. dy2 = 2 * (y - (h - r)) + 1;
  316. else
  317. return 0;
  318. d2 = 4 * r * r - dy2 * dy2;
  319. if (d2 < 0)
  320. d2 = 0;
  321. return r - (sm_isqrt(d2) + 1) / 2;
  322. }
  323. static void sm_fill_round(int x, int y, int w, int h, int r,
  324. unsigned int rgb)
  325. {
  326. int yy, half;
  327. if (w <= 0 || h <= 0)
  328. return;
  329. half = w < h ? w / 2 : h / 2;
  330. if (r > half)
  331. r = half;
  332. if (r <= 0) {
  333. usurf_fill(&screen, x, y, (unsigned int)w, (unsigned int)h,
  334. rgb);
  335. return;
  336. }
  337. for (yy = 0; yy < h; yy++) {
  338. int li = 0, ri = 0;
  339. if (yy < r) {
  340. li = sm_round_inset(yy, h, r);
  341. ri = li;
  342. }
  343. if (yy >= h - r) {
  344. int i = sm_round_inset(yy, h, r);
  345. if (i > li)
  346. li = i;
  347. if (i > ri)
  348. ri = i;
  349. }
  350. if (w - li - ri > 0)
  351. usurf_fill(&screen, x + li, y + yy,
  352. (unsigned int)(w - li - ri), 1, rgb);
  353. }
  354. }
  355. /* One shared renderer for the start and status buttons: the same height,
  356. * corner radius and hover/active colours. */
  357. static void bar_button_bg(int x, int w, int active, int hot)
  358. {
  359. if (active)
  360. sm_fill_round(x, BAR_BTN_Y, w, BAR_BTN_H, BAR_BTN_RADIUS,
  361. SM_SEL);
  362. else if (hot)
  363. sm_fill_round(x, BAR_BTN_Y, w, BAR_BTN_H, BAR_BTN_RADIUS,
  364. SM_PANEL_ALT);
  365. }
  366. /* The NebOS banner doubles as the start button. Its width follows the
  367. * proportional font, so the hit area and the highlight use the same
  368. * geometry. */
  369. static void bar_start_geom(int *x, int *y, int *w, int *h)
  370. {
  371. *x = 2;
  372. *y = BAR_BTN_Y;
  373. *h = BAR_BTN_H;
  374. *w = (int)usurf_text_width(&screen, OS_NAME " v" OS_VERSION) + 20;
  375. }
  376. static int bar_start_hit(unsigned int mx, unsigned int my)
  377. {
  378. int x, y, w, h;
  379. bar_start_geom(&x, &y, &w, &h);
  380. return (int)mx >= x && (int)mx < x + w &&
  381. (int)my >= y && (int)my < y + h;
  382. }
  383. static void bar_start_button(void)
  384. {
  385. int x, y, w, h;
  386. bar_start_geom(&x, &y, &w, &h);
  387. bar_button_bg(x, w, sm_open, sm_start_hot);
  388. usurf_text(&screen, x + 10, y + (h - 16) / 2,
  389. OS_NAME " v" OS_VERSION,
  390. sm_open ? COL_WHITE : COL_BANNER, 0, 0, fbw, WM_BAR_H);
  391. }
  392. static int name_is(const char *a, const char *b)
  393. {
  394. while (*a && *b) {
  395. if (*a != *b)
  396. return 0;
  397. a++;
  398. b++;
  399. }
  400. return *a == *b;
  401. }
  402. static int net_rssi_bars(int rssi)
  403. {
  404. if (rssi >= -55)
  405. return 4;
  406. if (rssi >= -65)
  407. return 3;
  408. if (rssi >= -75)
  409. return 2;
  410. return 1;
  411. }
  412. /* Small top-bar pictograms: Adwaita symbolic masks from the pack, centered
  413. * in their 24 px hit slots. */
  414. static void bar_draw_net_icon(int x, int y)
  415. {
  416. enum uwm_icon ic;
  417. switch (bar_net_kind) {
  418. case BAR_NET_WIRED:
  419. ic = UWM_ICON_NET_WIRED;
  420. break;
  421. case BAR_NET_WIFI:
  422. ic = bar_net_bars > 0 ? UWM_ICON_NET_WIFI_0 + bar_net_bars :
  423. UWM_ICON_NET_WIFI_OFF;
  424. break;
  425. case BAR_NET_USB:
  426. ic = UWM_ICON_NET_USB;
  427. break;
  428. default:
  429. ic = UWM_ICON_NET_WIRED_OFF;
  430. break;
  431. }
  432. (void)usurf_icon(&screen, x + 4, y, ic, bar_net_color);
  433. }
  434. /* 1 Hz poll: cached state -> the bar's network icon. The WiFi status call is
  435. * only made when WiFi is the active netdev, so a machine without a card does
  436. * not run a PCI scan on every tick. */
  437. static int poll_net_status(void)
  438. {
  439. struct sys_netinfo ni;
  440. int kind = BAR_NET_NONE, bars = 0, have_ip = 0;
  441. unsigned int color = 0xFF5050u;
  442. int changed;
  443. int i;
  444. if (u_net_info(&ni) == 1 && ni.present) {
  445. for (i = 0; i < 4; i++)
  446. if (ni.ip[i])
  447. have_ip = 1;
  448. if (name_is(ni.name, "wifi")) {
  449. struct sys_wifi_status ws;
  450. if (u_wifi(SYS_WIFI_STATUS, (int)&ws, 0) == 0 &&
  451. ws.present) {
  452. kind = BAR_NET_WIFI;
  453. if (ws.state >= SYS_WIFI_ST_ASSOC) {
  454. bars = net_rssi_bars(ws.rssi);
  455. color = (ws.state >= SYS_WIFI_ST_LINK &&
  456. have_ip) ?
  457. 0x55FF55u : 0xFFD250u;
  458. } else {
  459. /* Card present, not associated. */
  460. bars = 0;
  461. color = 0xFF5050u;
  462. }
  463. }
  464. } else if (name_is(ni.name, "rndis")) {
  465. kind = BAR_NET_USB;
  466. color = have_ip ? 0x55FF55u : 0xFFD250u;
  467. } else {
  468. kind = BAR_NET_WIRED;
  469. color = have_ip ? 0x55FF55u : 0xFFD250u;
  470. }
  471. }
  472. changed = kind != bar_net_kind || bars != bar_net_bars ||
  473. color != bar_net_color;
  474. bar_net_kind = kind;
  475. bar_net_bars = bars;
  476. bar_net_color = color;
  477. return changed;
  478. }
  479. static unsigned int temp_color(int c)
  480. {
  481. if (c >= 80)
  482. return 0xFF5050u;
  483. if (c >= 60)
  484. return 0xFFD250u;
  485. return 0x55FF55u;
  486. }
  487. /* "43C" (no libc printf here). */
  488. static unsigned int temp_text(char *b, int c)
  489. {
  490. unsigned int n = 0, v = (unsigned int)c;
  491. if (v >= 100)
  492. b[n++] = (char)('0' + (v / 100) % 10);
  493. if (v >= 10)
  494. b[n++] = (char)('0' + (v / 10) % 10);
  495. b[n++] = (char)('0' + v % 10);
  496. b[n++] = 'C';
  497. b[n] = '\0';
  498. return n;
  499. }
  500. static unsigned int batt_color(int pct)
  501. {
  502. if (pct <= 10)
  503. return 0xFF5050u;
  504. return 0x55FF55u;
  505. }
  506. /* Battery: the Adwaita outline with our own level fill over the lower
  507. * interior. The symbol's body is striped, so the interior is cleared to a
  508. * dim shade of the state colour first; the level then reads as a bright
  509. * block inside the outline. A critical charge turns the outline red too.
  510. * Geometry of battery-symbolic at 16 px: body x2..13, cavity x4..11 and
  511. * y4..13. */
  512. static unsigned int batt_dim(unsigned int c)
  513. {
  514. return (c >> 2) & 0x3F3F3F;
  515. }
  516. static void bar_icon_battery(int x, int y, int pct, unsigned int c)
  517. {
  518. unsigned int fill = (unsigned int)((10 * pct + 99) / 100);
  519. int ix = x + 4;
  520. if (fill > 10)
  521. fill = 10;
  522. if (!usurf_icon(&screen, ix, y, UWM_ICON_BATTERY, c))
  523. return;
  524. usurf_fill(&screen, ix + 4, y + 4, 8, 10, batt_dim(c));
  525. if (fill)
  526. usurf_fill(&screen, ix + 4, y + 14 - (int)fill, 8, fill, c);
  527. }
  528. /* AC adapter: the Adwaita plug symbol. */
  529. static void bar_icon_ac(int x, int y, unsigned int c)
  530. {
  531. (void)usurf_icon(&screen, x + 4, y, UWM_ICON_AC, c);
  532. }
  533. static void draw_bar(void)
  534. {
  535. char dt[48];
  536. unsigned int n, i, x, y;
  537. usurf_fill(&screen, 0, 0, fbw, WM_BAR_H, COL_BG);
  538. bar_start_button();
  539. if (bar_msg_until &&
  540. (int)(u_ticks_ms() - bar_msg_until) < 0)
  541. usurf_text(&screen, (int)(WM_BAR_LEFT_END + 8),
  542. 5, bar_msg, 0xFFD250u, 0, 0, fbw, WM_BAR_H);
  543. /* Rainbow separator (same y as the kernel's old bar). */
  544. for (x = 0; x < fbw; x++) {
  545. unsigned int c = rainbow_rgb((x * (6 * 256)) / (fbw ? fbw : 1));
  546. for (y = WM_BAR_H - 5; y < WM_BAR_H - 3; y++)
  547. usurf_fill(&screen, (int)x, (int)y, 1, 1, c);
  548. }
  549. if (bar_kexec_busy) {
  550. static const char msg[] = "loading new kernel over serial";
  551. unsigned int dots = bar_kexec_total ?
  552. (bar_kexec_progress * 12 / bar_kexec_total) : 0;
  553. bar_status_x = -1;
  554. bar_status_w = 0;
  555. n = 0;
  556. while (msg[n])
  557. dt[n] = msg[n], n++;
  558. for (i = 0; i < 12; i++)
  559. dt[n + i] = (i < dots) ? '.' : ' ';
  560. n += 12;
  561. dt[n] = '\0';
  562. usurf_text(&screen,
  563. (int)fbw - (int)usurf_text_width(&screen, dt) - 4,
  564. 5, dt, 0xFFC864u, 0, 0, fbw, WM_BAR_H);
  565. } else {
  566. int right = (int)fbw - 4;
  567. int min_x = (int)WM_BAR_LEFT_END + 4;
  568. int pwr_px = -1, net_nx = -1, clock_tx = -1, temp_tx = -1;
  569. int temp_tw;
  570. int left;
  571. char cdt[12], tb[12];
  572. const char *clock_txt = dt;
  573. /* Lay the cluster out right to left: AC/battery, network, the
  574. * date/time and the CPU temperature. */
  575. if (bar_batt_pct >= 0 || bar_ac_online > 0) {
  576. pwr_px = right - BAR_PWR_W;
  577. if (pwr_px >= min_x)
  578. right = pwr_px - 8;
  579. else
  580. pwr_px = -1;
  581. }
  582. net_nx = right - BAR_NET_W;
  583. if (net_nx >= min_x)
  584. right = net_nx - 8;
  585. else
  586. net_nx = -1;
  587. if (u_rtc_datetime(dt, sizeof(dt)) == 0) {
  588. int tw = (int)usurf_text_width(&screen, dt);
  589. int tx = right - tw;
  590. unsigned int k;
  591. if (tx < min_x) {
  592. /* Compact form "MM-DD HH:MM" from
  593. * "YYYY-MM-DD HH:MM:SS" when the status
  594. * cluster is too wide for the full date. */
  595. for (k = 0; k < 11 && dt[5 + k]; k++)
  596. cdt[k] = dt[5 + k];
  597. cdt[k] = '\0';
  598. clock_txt = cdt;
  599. tw = (int)usurf_text_width(&screen, cdt);
  600. tx = right - tw;
  601. }
  602. if (tx >= min_x) {
  603. clock_tx = tx;
  604. right = tx - 8;
  605. }
  606. }
  607. if (bar_temp_c >= 0) {
  608. (void)temp_text(tb, bar_temp_c);
  609. temp_tw = (int)usurf_text_width(&screen, tb);
  610. temp_tx = right - temp_tw;
  611. if (temp_tx < min_x)
  612. temp_tx = -1;
  613. }
  614. /* The status labels form one click target: record the whole
  615. * cluster and paint one rounded hover pill behind it. */
  616. left = pwr_px;
  617. if (net_nx >= 0)
  618. left = net_nx;
  619. if (clock_tx >= 0)
  620. left = clock_tx;
  621. if (temp_tx >= 0)
  622. left = temp_tx;
  623. if (left >= 0) {
  624. bar_status_x = left - 4;
  625. if (bar_status_x < 0)
  626. bar_status_x = 0;
  627. bar_status_w = (unsigned int)((int)fbw - 2 -
  628. bar_status_x);
  629. bar_button_bg(bar_status_x, (int)bar_status_w,
  630. control_pid > 0, bar_status_hot);
  631. } else {
  632. bar_status_x = -1;
  633. bar_status_w = 0;
  634. }
  635. if (pwr_px >= 0) {
  636. if (bar_ac_online > 0)
  637. bar_icon_ac(pwr_px, BAR_PWR_Y, 0x55FF55u);
  638. else
  639. bar_icon_battery(pwr_px, BAR_PWR_Y,
  640. bar_batt_pct,
  641. batt_color(bar_batt_pct));
  642. }
  643. if (net_nx >= 0)
  644. bar_draw_net_icon(net_nx, BAR_NET_Y);
  645. if (clock_tx >= 0)
  646. usurf_text(&screen, clock_tx, 5, clock_txt, COL_BANNER,
  647. 0, 0, fbw, WM_BAR_H);
  648. if (temp_tx >= 0)
  649. usurf_text(&screen, temp_tx, 5, tb,
  650. temp_color(bar_temp_c), 0, 0, fbw,
  651. WM_BAR_H);
  652. }
  653. }
  654. /* A popover is a flat, chromeless panel: the canvas fills the window. */
  655. static void draw_popover_clip(unsigned int idx, int cx, int cy,
  656. unsigned int cw, unsigned int ch)
  657. {
  658. struct uwin *u = &wins[idx];
  659. struct wm_window_info *w = &u->info;
  660. unsigned int cwid = w->content_w, chei = w->content_h;
  661. if (u->mapped && u->map.kind == WM_KIND_GFX) {
  662. struct uwm_canvas c;
  663. c.px = (const unsigned short *)u->map.va;
  664. c.w = u->map.w;
  665. c.h = u->map.h;
  666. if (c.w == cwid && c.h == chei) {
  667. usurf_canvas(&screen, &c, w->x, w->y,
  668. cx, cy, cw, ch);
  669. return;
  670. }
  671. }
  672. fill_clip(w->x, w->y, cwid, chei, 0x181C28u, cx, cy, cw, ch);
  673. }
  674. static void draw_window_clip(unsigned int idx, int cx, int cy,
  675. unsigned int cw, unsigned int ch)
  676. {
  677. struct uwin *u = &wins[idx];
  678. struct wm_window_info *w = &u->info;
  679. int x = w->x, y = w->y;
  680. unsigned int pw = w->w;
  681. int bx = x + (int)pw - WM_BORDER_W - WM_CLOSE_W;
  682. int cony = y + WM_BORDER_W + WM_TITLE_H;
  683. int content_x = x + WM_BORDER_W;
  684. unsigned int content_w = w->content_w;
  685. unsigned int content_h = w->content_h;
  686. if (w->popover) {
  687. draw_popover_clip(idx, cx, cy, cw, ch);
  688. return;
  689. }
  690. /* Title bar and close button; windows have no visible frame. */
  691. fill_clip(x, y, pw, WM_TITLE_H,
  692. w->focused ? COL_TITLE_ON : COL_TITLE_OFF, cx, cy, cw, ch);
  693. usurf_text(&screen, x + 3, y + 2, w->title, COL_WHITE,
  694. cx, cy, cw, ch);
  695. fill_clip(bx, y, WM_CLOSE_W, WM_TITLE_H, COL_CLOSE, cx, cy, cw, ch);
  696. usurf_text(&screen, bx + 4, y + 2, "x", COL_WHITE,
  697. cx, cy, cw, ch);
  698. /* Content: every window presents an RGB565 canvas (the terminal
  699. * renders its cells into one; gfx commands own theirs). A mapped
  700. * canvas covers the whole content area, so it does not need the black
  701. * underlay. */
  702. if (u->mapped && u->map.kind == WM_KIND_GFX) {
  703. struct uwm_canvas c;
  704. c.px = (const unsigned short *)u->map.va;
  705. c.w = u->map.w;
  706. c.h = u->map.h;
  707. if (c.w != content_w || c.h != content_h)
  708. fill_clip(content_x, cony, content_w, content_h,
  709. 0x000000u, cx, cy, cw, ch);
  710. usurf_canvas(&screen, &c, content_x, cony,
  711. cx, cy, cw, ch);
  712. } else {
  713. fill_clip(content_x, cony, content_w, content_h,
  714. 0x000000u, cx, cy, cw, ch);
  715. }
  716. }
  717. /* Front-to-back window order: popovers float above everything, then the
  718. * focused window, then the others in descending slot order. Painting and
  719. * hit-testing share it. */
  720. static unsigned int build_order(int *order)
  721. {
  722. unsigned int i, n = 0;
  723. for (i = 0; i < WM_MAX_WINDOWS; i++)
  724. if (wins[i].info.used && wins[i].info.popover)
  725. order[n++] = (int)i;
  726. for (i = 0; i < WM_MAX_WINDOWS; i++)
  727. if (wins[i].info.used && wins[i].info.focused &&
  728. !wins[i].info.popover)
  729. order[n++] = (int)i;
  730. for (i = WM_MAX_WINDOWS; i-- > 0;)
  731. if (wins[i].info.used && !wins[i].info.focused &&
  732. !wins[i].info.popover)
  733. order[n++] = (int)i;
  734. return n;
  735. }
  736. struct orect {
  737. int x, y;
  738. unsigned int w, h;
  739. };
  740. #define OCC_MAX 64
  741. /* r minus cut, split into up to four disjoint rectangles. */
  742. static unsigned int rect_sub(struct orect *out, const struct orect *r,
  743. const struct orect *cut)
  744. {
  745. int rx0 = r->x, ry0 = r->y;
  746. int rx1 = r->x + (int)r->w, ry1 = r->y + (int)r->h;
  747. int cx0 = cut->x, cy0 = cut->y;
  748. int cx1 = cut->x + (int)cut->w, cy1 = cut->y + (int)cut->h;
  749. unsigned int n = 0;
  750. if (cx0 < rx0) cx0 = rx0;
  751. if (cy0 < ry0) cy0 = ry0;
  752. if (cx1 > rx1) cx1 = rx1;
  753. if (cy1 > ry1) cy1 = ry1;
  754. if (cx1 <= cx0 || cy1 <= cy0) {
  755. out[0] = *r;
  756. return 1;
  757. }
  758. if (cy0 > ry0) {
  759. out[n].x = rx0;
  760. out[n].y = ry0;
  761. out[n].w = (unsigned int)(rx1 - rx0);
  762. out[n].h = (unsigned int)(cy0 - ry0);
  763. n++;
  764. }
  765. if (cy1 < ry1) {
  766. out[n].x = rx0;
  767. out[n].y = cy1;
  768. out[n].w = (unsigned int)(rx1 - rx0);
  769. out[n].h = (unsigned int)(ry1 - cy1);
  770. n++;
  771. }
  772. if (cx0 > rx0) {
  773. out[n].x = rx0;
  774. out[n].y = cy0;
  775. out[n].w = (unsigned int)(cx0 - rx0);
  776. out[n].h = (unsigned int)(cy1 - cy0);
  777. n++;
  778. }
  779. if (cx1 < rx1) {
  780. out[n].x = cx1;
  781. out[n].y = cy0;
  782. out[n].w = (unsigned int)(rx1 - cx1);
  783. out[n].h = (unsigned int)(cy1 - cy0);
  784. n++;
  785. }
  786. return n;
  787. }
  788. static void wall_build_maps(void)
  789. {
  790. unsigned int i;
  791. if (screen.bpp == 32) {
  792. for (i = 0; i < 65536; i++) {
  793. unsigned int r = (i >> 11) & 31;
  794. unsigned int g = (i >> 5) & 63;
  795. unsigned int b = i & 31;
  796. wall_rgb32[i] = 0xFF000000u |
  797. ((r << 3 | r >> 2) << 16) |
  798. ((g << 2 | g >> 4) << 8) |
  799. (b << 3 | b >> 2);
  800. }
  801. }
  802. for (i = 0; i < WALL_MAP_MAX; i++) {
  803. unsigned int sx = 0, sy = 0;
  804. if (fbw && i < fbw)
  805. sx = (unsigned int)((unsigned long long)i * wall_w / fbw);
  806. if (fbh && i < fbh)
  807. sy = (unsigned int)((unsigned long long)i * wall_h / fbh);
  808. if (sx >= wall_w)
  809. sx = wall_w - 1;
  810. if (sy >= wall_h)
  811. sy = wall_h - 1;
  812. wall_xmap[i] = (unsigned short)sx;
  813. wall_ymap[i] = (unsigned short)sy;
  814. }
  815. }
  816. static int wall_magic_ok(const struct wm_wallpaper_hdr *h)
  817. {
  818. unsigned int i;
  819. for (i = 0; i < sizeof(h->magic); i++)
  820. if (h->magic[i] != WM_WALLPAPER_MAGIC[i])
  821. return 0;
  822. return 1;
  823. }
  824. /* Read the persistent copy first, then the RAM copy. The FAT driver may
  825. * write a final partial sector past the requested length, so wall_data has
  826. * 512 bytes of slack and the read is capped one sector below its size. */
  827. static void wall_load(void)
  828. {
  829. static const char *const paths[] = {
  830. WM_WALLPAPER_DATA, WM_WALLPAPER_SHARE
  831. };
  832. struct wm_wallpaper_hdr *h = (struct wm_wallpaper_hdr *)wall_data;
  833. unsigned int i, pixbytes;
  834. int rn;
  835. wall_ready = 0;
  836. for (i = 0; i < sizeof(paths) / sizeof(paths[0]); i++) {
  837. rn = u_syscall3(SYS_READ_FILE, (int)paths[i], (int)wall_data,
  838. (int)(sizeof(wall_data) - 512));
  839. if (rn < (int)sizeof(*h) || !wall_magic_ok(h))
  840. continue;
  841. if (!h->width || !h->height ||
  842. h->width > WM_WALLPAPER_MAX_W ||
  843. h->height > WM_WALLPAPER_MAX_H)
  844. continue;
  845. pixbytes = h->width * h->height * 2u;
  846. if ((unsigned int)rn < sizeof(*h) + pixbytes)
  847. continue;
  848. wall_w = h->width;
  849. wall_h = h->height;
  850. wall_ready = 1;
  851. wall_build_maps();
  852. return;
  853. }
  854. }
  855. /* Paint the desktop part of one damage rectangle. */
  856. static void wall_draw(int x, int y, unsigned int w, unsigned int h)
  857. {
  858. const unsigned short *src = (const unsigned short *)(wall_data +
  859. sizeof(struct wm_wallpaper_hdr));
  860. int x0 = x, y0 = y, x1 = x + (int)w, y1 = y + (int)h;
  861. unsigned int yy, xx;
  862. if (!wall_ready) {
  863. usurf_fill(&screen, x, y, w, h, COL_BG);
  864. return;
  865. }
  866. if (x0 < 0) x0 = 0;
  867. if (y0 < 0) y0 = 0;
  868. if (x1 > (int)fbw) x1 = (int)fbw;
  869. if (y1 > (int)fbh) y1 = (int)fbh;
  870. if (x1 > WALL_MAP_MAX) x1 = WALL_MAP_MAX;
  871. if (y1 > WALL_MAP_MAX) y1 = WALL_MAP_MAX;
  872. if (x1 <= x0 || y1 <= y0)
  873. return;
  874. usurf_pixels += (unsigned int)(x1 - x0) * (unsigned int)(y1 - y0);
  875. for (yy = (unsigned int)y0; yy < (unsigned int)y1; yy++) {
  876. const unsigned short *srow = src + (unsigned int)wall_ymap[yy] *
  877. wall_w;
  878. if (screen.bpp == 16) {
  879. unsigned short *d = (unsigned short *)(screen.px +
  880. (unsigned long)yy * screen.pitch) + x0;
  881. for (xx = (unsigned int)x0; xx < (unsigned int)x1; xx++)
  882. d[xx] = srow[wall_xmap[xx]];
  883. } else if (screen.bpp == 32) {
  884. unsigned int *d = (unsigned int *)(screen.px +
  885. (unsigned long)yy * screen.pitch) + x0;
  886. for (xx = (unsigned int)x0; xx < (unsigned int)x1; xx++)
  887. d[xx] = wall_rgb32[srow[wall_xmap[xx]]];
  888. } else {
  889. unsigned char *d = screen.px +
  890. (unsigned long)yy * screen.pitch + x0 * 3;
  891. for (xx = (unsigned int)x0; xx < (unsigned int)x1; xx++) {
  892. unsigned int c = srow[wall_xmap[xx]];
  893. unsigned int r = (c >> 11) & 31;
  894. unsigned int g = (c >> 5) & 63;
  895. unsigned int b = c & 31;
  896. d[0] = (unsigned char)(b << 3 | b >> 2);
  897. d[1] = (unsigned char)(g << 2 | g >> 4);
  898. d[2] = (unsigned char)(r << 3 | r >> 2);
  899. d += 3;
  900. }
  901. }
  902. }
  903. }
  904. /* Back-to-front repaint, used only if the visible-region list overflows. */
  905. static void draw_rect_naive(int x, int y, unsigned int w, unsigned int h)
  906. {
  907. unsigned int i;
  908. wall_draw(x, y, w, h);
  909. for (i = 0; i < WM_MAX_WINDOWS; i++)
  910. if (wins[i].info.used && !wins[i].info.focused &&
  911. !wins[i].info.popover &&
  912. x < wins[i].info.x + (int)wins[i].info.w &&
  913. x + (int)w > wins[i].info.x &&
  914. y < wins[i].info.y + (int)wins[i].info.h &&
  915. y + (int)h > wins[i].info.y)
  916. draw_window_clip(i, x, y, w, h);
  917. for (i = 0; i < WM_MAX_WINDOWS; i++)
  918. if (wins[i].info.used && wins[i].info.focused &&
  919. !wins[i].info.popover &&
  920. x < wins[i].info.x + (int)wins[i].info.w &&
  921. x + (int)w > wins[i].info.x &&
  922. y < wins[i].info.y + (int)wins[i].info.h &&
  923. y + (int)h > wins[i].info.y)
  924. draw_window_clip(i, x, y, w, h);
  925. for (i = 0; i < WM_MAX_WINDOWS; i++)
  926. if (wins[i].info.used && wins[i].info.popover &&
  927. x < wins[i].info.x + (int)wins[i].info.w &&
  928. x + (int)w > wins[i].info.x &&
  929. y < wins[i].info.y + (int)wins[i].info.h &&
  930. y + (int)h > wins[i].info.y)
  931. draw_window_clip(i, x, y, w, h);
  932. }
  933. static void draw_rect(int x, int y, unsigned int w, unsigned int h)
  934. {
  935. static struct orect cur[OCC_MAX], nxt[OCC_MAX];
  936. struct orect dmg;
  937. int order[WM_MAX_WINDOWS];
  938. unsigned int n, i, j, ncur, nnext;
  939. int overflow = 0;
  940. int x0 = x, y0 = y, x1 = x + (int)w, y1 = y + (int)h;
  941. if (w == 0 || h == 0)
  942. return;
  943. if (x0 < 0) x0 = 0;
  944. if (y0 < 0) y0 = 0;
  945. if (x1 > (int)fbw) x1 = (int)fbw;
  946. if (y1 > (int)fbh) y1 = (int)fbh;
  947. if (x1 <= x0 || y1 <= y0)
  948. return;
  949. dmg.x = x0;
  950. dmg.y = y0;
  951. dmg.w = (unsigned int)(x1 - x0);
  952. dmg.h = (unsigned int)(y1 - y0);
  953. wall_draw(dmg.x, dmg.y, dmg.w, dmg.h);
  954. n = build_order(order);
  955. for (i = 0; i < n; i++) {
  956. struct uwin *u = &wins[order[i]];
  957. int wx0 = u->info.x, wy0 = u->info.y;
  958. int wx1 = wx0 + (int)u->info.w, wy1 = wy0 + (int)u->info.h;
  959. if (wx0 < dmg.x) wx0 = dmg.x;
  960. if (wy0 < dmg.y) wy0 = dmg.y;
  961. if (wx1 > x1) wx1 = x1;
  962. if (wy1 > y1) wy1 = y1;
  963. if (wx1 <= wx0 || wy1 <= wy0)
  964. continue;
  965. cur[0].x = wx0;
  966. cur[0].y = wy0;
  967. cur[0].w = (unsigned int)(wx1 - wx0);
  968. cur[0].h = (unsigned int)(wy1 - wy0);
  969. ncur = 1;
  970. for (j = 0; j < i && ncur; j++) {
  971. struct uwin *f = &wins[order[j]];
  972. struct orect fr;
  973. unsigned int k;
  974. fr.x = f->info.x;
  975. fr.y = f->info.y;
  976. fr.w = f->info.w;
  977. fr.h = f->info.h;
  978. nnext = 0;
  979. for (k = 0; k < ncur; k++) {
  980. if (nnext + 4 > OCC_MAX) {
  981. overflow = 1;
  982. break;
  983. }
  984. nnext += rect_sub(&nxt[nnext], &cur[k], &fr);
  985. }
  986. if (overflow)
  987. break;
  988. for (k = 0; k < nnext; k++)
  989. cur[k] = nxt[k];
  990. ncur = nnext;
  991. }
  992. if (overflow)
  993. break;
  994. for (j = 0; j < ncur; j++)
  995. draw_window_clip((unsigned int)order[i], cur[j].x,
  996. cur[j].y, cur[j].w, cur[j].h);
  997. }
  998. if (overflow)
  999. draw_rect_naive(dmg.x, dmg.y, dmg.w, dmg.h);
  1000. }
  1001. /* Invisible resize border: edge bits for a point in the band around the
  1002. * window's outer edge (WM_RESIZE_IN px inside and WM_RESIZE_OUT px outside).
  1003. * Popovers have no chrome and never resize. */
  1004. #define WM_EDGE_LEFT 1
  1005. #define WM_EDGE_RIGHT 2
  1006. #define WM_EDGE_TOP 4
  1007. #define WM_EDGE_BOTTOM 8
  1008. static int resize_edges_at(const struct uwin *u, int mx, int my)
  1009. {
  1010. const struct wm_window_info *w = &u->info;
  1011. int l = w->x, t = w->y;
  1012. int r = l + (int)w->w, b = t + (int)w->h;
  1013. int near_x, near_y, e = 0;
  1014. if (w->popover)
  1015. return 0;
  1016. near_x = mx >= l - WM_RESIZE_OUT && mx < r + WM_RESIZE_OUT;
  1017. near_y = my >= t - WM_RESIZE_OUT && my < b + WM_RESIZE_OUT;
  1018. if (near_y && mx >= l - WM_RESIZE_OUT && mx < l + WM_RESIZE_IN)
  1019. e |= WM_EDGE_LEFT;
  1020. if (near_y && mx >= r - WM_RESIZE_IN && mx < r + WM_RESIZE_OUT)
  1021. e |= WM_EDGE_RIGHT;
  1022. if (near_x && my >= t - WM_RESIZE_OUT && my < t + WM_RESIZE_IN)
  1023. e |= WM_EDGE_TOP;
  1024. if (near_x && my >= b - WM_RESIZE_IN && my < b + WM_RESIZE_OUT)
  1025. e |= WM_EDGE_BOTTOM;
  1026. return e;
  1027. }
  1028. /* Topmost window at (mx,my): its resize band when the point is inside one,
  1029. * otherwise the window rectangle. *edges is 0 for a plain rectangle hit.
  1030. * Bands are not considered inside the top bar, which the WM owns. */
  1031. static int resize_hit(int mx, int my, int *edges)
  1032. {
  1033. int order[WM_MAX_WINDOWS];
  1034. unsigned int n = build_order(order), i;
  1035. *edges = 0;
  1036. for (i = 0; i < n; i++) {
  1037. struct uwin *u = &wins[order[i]];
  1038. int e = 0;
  1039. if (!u->info.used)
  1040. continue;
  1041. if (my >= WM_BAR_H)
  1042. e = resize_edges_at(u, mx, my);
  1043. if (e) {
  1044. *edges = e;
  1045. return order[i];
  1046. }
  1047. if (mx >= u->info.x && my >= u->info.y &&
  1048. mx < u->info.x + (int)u->info.w &&
  1049. my < u->info.y + (int)u->info.h)
  1050. return order[i];
  1051. }
  1052. return -1;
  1053. }
  1054. static int resize_cursor(int edges)
  1055. {
  1056. switch (edges) {
  1057. case WM_EDGE_LEFT:
  1058. case WM_EDGE_RIGHT:
  1059. return CURSOR_EW_RESIZE;
  1060. case WM_EDGE_TOP:
  1061. case WM_EDGE_BOTTOM:
  1062. return CURSOR_NS_RESIZE;
  1063. case WM_EDGE_LEFT | WM_EDGE_TOP:
  1064. case WM_EDGE_RIGHT | WM_EDGE_BOTTOM:
  1065. return CURSOR_NWSE_RESIZE;
  1066. case WM_EDGE_RIGHT | WM_EDGE_TOP:
  1067. case WM_EDGE_LEFT | WM_EDGE_BOTTOM:
  1068. return CURSOR_NESW_RESIZE;
  1069. default:
  1070. return CURSOR_DEFAULT;
  1071. }
  1072. }
  1073. /* Close button hit area (the top-right WM_CLOSE_W columns of the title bar). */
  1074. static int close_hit(const struct wm_window_info *w, int mx, int my)
  1075. {
  1076. if (w->popover)
  1077. return 0;
  1078. return mx >= w->x + (int)w->w - WM_CLOSE_W &&
  1079. mx < w->x + (int)w->w &&
  1080. my >= w->y && my < w->y + WM_TITLE_H;
  1081. }
  1082. static void clamp_geom(struct uwin *u, int *x, int *y)
  1083. {
  1084. /* Match wm_clamp_window(): allow the window to hang off the left,
  1085. * right and bottom edges but keep the title bar reachable and below
  1086. * the system bar. */
  1087. int minx = WM_DRAG_VISIBLE - (int)u->info.w;
  1088. int maxx = (int)fbw - WM_DRAG_VISIBLE;
  1089. int miny = WM_BAR_H;
  1090. int maxy = (int)fbh - WM_BORDER_W - WM_TITLE_H;
  1091. if (minx > maxx)
  1092. minx = maxx = 0;
  1093. if (miny > maxy)
  1094. miny = maxy = WM_BAR_H;
  1095. if (*x < minx)
  1096. *x = minx;
  1097. else if (*x > maxx)
  1098. *x = maxx;
  1099. if (*y < miny)
  1100. *y = miny;
  1101. else if (*y > maxy)
  1102. *y = maxy;
  1103. }
  1104. static void add_rect(struct wm_present_info *p, int x, int y,
  1105. unsigned int w, unsigned int h)
  1106. {
  1107. if (w == 0 || h == 0)
  1108. return;
  1109. if (p->count < 4) {
  1110. p->rect[p->count].x = x;
  1111. p->rect[p->count].y = y;
  1112. p->rect[p->count].w = w;
  1113. p->rect[p->count].h = h;
  1114. p->count++;
  1115. } else {
  1116. /* Four is enough for normal updates; fold the rest into the
  1117. * last rect rather than dropping pixels. */
  1118. int x0 = p->rect[3].x, y0 = p->rect[3].y;
  1119. int x1 = x0 + (int)p->rect[3].w, y1 = y0 + (int)p->rect[3].h;
  1120. int nx1 = x + (int)w, ny1 = y + (int)h;
  1121. if (x < x0) x0 = x;
  1122. if (y < y0) y0 = y;
  1123. if (nx1 > x1) x1 = nx1;
  1124. if (ny1 > y1) y1 = ny1;
  1125. p->rect[3].x = x0;
  1126. p->rect[3].y = y0;
  1127. p->rect[3].w = (unsigned int)(x1 - x0);
  1128. p->rect[3].h = (unsigned int)(y1 - y0);
  1129. }
  1130. }
  1131. /* Center the brightness bubble in the work area, clamped on small screens. */
  1132. static void osd_geom(void)
  1133. {
  1134. int x, y;
  1135. osd_w = OSD_W;
  1136. osd_h = OSD_H;
  1137. x = ((int)fbw - (int)osd_w) / 2;
  1138. y = WM_BAR_H + ((int)fbh - WM_BAR_H - (int)osd_h) * 3 / 5;
  1139. if (x < 0)
  1140. x = 0;
  1141. if (x + (int)osd_w > (int)fbw)
  1142. x = (int)fbw - (int)osd_w;
  1143. if (y < WM_BAR_H)
  1144. y = WM_BAR_H;
  1145. if (y + (int)osd_h > (int)fbh)
  1146. y = (int)fbh - (int)osd_h;
  1147. osd_x = x;
  1148. osd_y = y;
  1149. }
  1150. /* A small sun: a filled circle plus four rays, all from 1-pixel fills. */
  1151. static void osd_sun(int cx, int cy)
  1152. {
  1153. static const unsigned char half[11] = { 2, 4, 5, 6, 6, 6, 6, 6, 5, 4, 2 };
  1154. int i;
  1155. for (i = -5; i <= 5; i++)
  1156. usurf_fill(&screen, cx - half[i + 5], cy + i,
  1157. half[i + 5] * 2 + 1, 1, 0xFFD250u);
  1158. usurf_fill(&screen, cx - 1, cy - 9, 2, 3, 0xFFD250u);
  1159. usurf_fill(&screen, cx - 1, cy + 7, 2, 3, 0xFFD250u);
  1160. usurf_fill(&screen, cx - 9, cy - 1, 3, 2, 0xFFD250u);
  1161. usurf_fill(&screen, cx + 7, cy - 1, 3, 2, 0xFFD250u);
  1162. }
  1163. static void osd_draw(void)
  1164. {
  1165. char pct[8];
  1166. unsigned int n = 0;
  1167. unsigned int bar_x, bar_y, bar_w;
  1168. int x, y, tw;
  1169. osd_geom();
  1170. x = osd_x;
  1171. y = osd_y;
  1172. usurf_fill(&screen, x, y, osd_w, osd_h, 0x26263Au);
  1173. usurf_fill(&screen, x, y, osd_w, 1, 0x70709Au);
  1174. usurf_fill(&screen, x, y + (int)osd_h - 1, osd_w, 1, 0x70709Au);
  1175. usurf_fill(&screen, x, y + 1, 1, osd_h - 2, 0x70709Au);
  1176. usurf_fill(&screen, x + (int)osd_w - 1, y + 1, 1, osd_h - 2,
  1177. 0x70709Au);
  1178. osd_sun(x + 22, y + (int)osd_h / 2);
  1179. if (osd_level >= 100) {
  1180. pct[n++] = '1';
  1181. pct[n++] = '0';
  1182. pct[n++] = '0';
  1183. } else {
  1184. if (osd_level >= 10)
  1185. pct[n++] = (char)('0' + osd_level / 10);
  1186. pct[n++] = (char)('0' + osd_level % 10);
  1187. }
  1188. pct[n++] = '%';
  1189. pct[n] = '\0';
  1190. usurf_text(&screen, x + 42, y + 9, "Brightness", 0xE8E8F6u,
  1191. x, y, osd_w, osd_h);
  1192. tw = (int)usurf_text_width(&screen, pct);
  1193. usurf_text(&screen, x + (int)osd_w - 14 - tw, y + 9, pct,
  1194. 0xFFD250u, x, y, osd_w, osd_h);
  1195. bar_x = (unsigned int)(x + 42);
  1196. bar_y = (unsigned int)(y + 35);
  1197. bar_w = osd_w - 42 - 14;
  1198. usurf_fill(&screen, (int)bar_x, (int)bar_y, bar_w, 10, 0x44445Au);
  1199. usurf_fill(&screen, (int)bar_x, (int)bar_y,
  1200. (bar_w * (unsigned int)osd_level + 50) / 100, 10,
  1201. 0xFFD250u);
  1202. }
  1203. /* Put the brightness OSD and the start menu back on top after a render pass
  1204. * that may have overwritten them. */
  1205. static void wm_overlays(struct wm_present_info *p)
  1206. {
  1207. if (osd_show) {
  1208. osd_draw();
  1209. add_rect(p, osd_x, osd_y, osd_w, osd_h);
  1210. }
  1211. if (sm_open) {
  1212. sm_draw();
  1213. sm_add_present(p);
  1214. }
  1215. }
  1216. /* A focused window is topmost, so the shadow contains exactly its pixels in
  1217. * the old rectangle. Move that block and repaint only the old part the new
  1218. * rectangle does not cover. When either rectangle is partly off-screen the
  1219. * shadow has no valid pixels for the hidden part, so repaint the new rect
  1220. * instead of copying desktop background into the window. */
  1221. static void move_window_pixels(struct uwin *u, int ox, int oy, int nx, int ny)
  1222. {
  1223. struct wm_present_info present;
  1224. struct orect oldr, newr, pieces[4];
  1225. unsigned int np, k;
  1226. int w = (int)u->info.w, h = (int)u->info.h;
  1227. int old_on = ox >= 0 && oy >= 0 &&
  1228. ox + w <= (int)fbw && oy + h <= (int)fbh;
  1229. int new_on = nx >= 0 && ny >= 0 &&
  1230. nx + w <= (int)fbw && ny + h <= (int)fbh;
  1231. if (u_wm_begin() != 0)
  1232. return;
  1233. remap_window_contents();
  1234. if (old_on && new_on)
  1235. usurf_copy(&screen, nx, ny, ox, oy, u->info.w, u->info.h);
  1236. else
  1237. draw_rect(nx, ny, u->info.w, u->info.h);
  1238. oldr.x = ox;
  1239. oldr.y = oy;
  1240. oldr.w = u->info.w;
  1241. oldr.h = u->info.h;
  1242. newr.x = nx;
  1243. newr.y = ny;
  1244. newr.w = u->info.w;
  1245. newr.h = u->info.h;
  1246. np = rect_sub(pieces, &oldr, &newr);
  1247. for (k = 0; k < np; k++)
  1248. draw_rect(pieces[k].x, pieces[k].y, pieces[k].w, pieces[k].h);
  1249. present.count = 0;
  1250. present.immediate = 1;
  1251. add_rect(&present, nx, ny, u->info.w, u->info.h);
  1252. for (k = 0; k < np; k++)
  1253. add_rect(&present, pieces[k].x, pieces[k].y,
  1254. pieces[k].w, pieces[k].h);
  1255. wm_overlays(&present);
  1256. u_wm_present(&present);
  1257. }
  1258. static int iabs(int v)
  1259. {
  1260. return v < 0 ? -v : v;
  1261. }
  1262. /* Repaint the old and new rectangles of a geometry change. */
  1263. static void redraw_geom(struct uwin *u, int ox, int oy,
  1264. unsigned int ow, unsigned int oh)
  1265. {
  1266. struct wm_present_info present;
  1267. unsigned int i;
  1268. present.count = 0;
  1269. present.immediate = 1;
  1270. add_rect(&present, ox, oy, ow, oh);
  1271. add_rect(&present, u->info.x, u->info.y, u->info.w, u->info.h);
  1272. if (u_wm_begin() == 0) {
  1273. remap_window_contents();
  1274. for (i = 0; i < present.count; i++)
  1275. draw_rect(present.rect[i].x, present.rect[i].y,
  1276. present.rect[i].w, present.rect[i].h);
  1277. wm_overlays(&present);
  1278. u_wm_present(&present);
  1279. }
  1280. }
  1281. /* Resize (when needed), move and remap a window in one step. */
  1282. static int set_window_geometry(struct uwin *u, unsigned int idx,
  1283. int nx, int ny, unsigned int nw, unsigned int nh)
  1284. {
  1285. struct wm_window_info ni;
  1286. int ox = u->info.x, oy = u->info.y;
  1287. unsigned int ow = u->info.w, oh = u->info.h;
  1288. if ((int)nw != (int)ow || (int)nh != (int)oh) {
  1289. if (u_wm_resize(idx, nw - 2 * WM_BORDER_W,
  1290. nh - WM_TITLE_H - 2 * WM_BORDER_W) != 0)
  1291. return -1;
  1292. }
  1293. if (nx != ox || ny != oy)
  1294. u_wm_set_geom(idx, nx, ny);
  1295. u->mapped = 0;
  1296. if (u_wm_info(idx, &ni) == 1)
  1297. u->info = ni;
  1298. if (u_wm_map(idx, &u->map) == 0)
  1299. u->mapped = 1;
  1300. redraw_geom(u, ox, oy, ow, oh);
  1301. return 0;
  1302. }
  1303. static int restore_window(struct uwin *u, unsigned int idx)
  1304. {
  1305. if (u->state == WIN_NORMAL)
  1306. return 0;
  1307. if (set_window_geometry(u, idx, u->restore_x, u->restore_y,
  1308. u->restore_w, u->restore_h) != 0)
  1309. return -1;
  1310. u->state = WIN_NORMAL;
  1311. return 0;
  1312. }
  1313. /* Maximize or snap a window to a half of the work area below the system
  1314. * bar. The restore geometry is recorded only when the window leaves the
  1315. * normal state, so maximize -> snap -> restore returns to the original. */
  1316. static int place_window(struct uwin *u, unsigned int idx, int state)
  1317. {
  1318. unsigned int half = fbw / 2;
  1319. unsigned int h = fbh - WM_BAR_H;
  1320. int x = 0;
  1321. unsigned int w = fbw;
  1322. if (state == u->state)
  1323. return 0;
  1324. if (state == WIN_SNAP_LEFT) {
  1325. w = half;
  1326. } else if (state == WIN_SNAP_RIGHT) {
  1327. x = (int)half;
  1328. w = fbw - half;
  1329. }
  1330. if (u->state == WIN_NORMAL) {
  1331. u->restore_x = u->info.x;
  1332. u->restore_y = u->info.y;
  1333. u->restore_w = u->info.w;
  1334. u->restore_h = u->info.h;
  1335. }
  1336. if (set_window_geometry(u, idx, x, WM_BAR_H, w, h) != 0)
  1337. return -1;
  1338. u->state = state;
  1339. return 0;
  1340. }
  1341. /* Double-click title bar: fill the work area below the system bar, or go back
  1342. * to the geometry the window had before it was maximized. A snapped window
  1343. * maximizes (keeping its pre-snap restore geometry). */
  1344. static int toggle_maximize(struct uwin *u, unsigned int idx)
  1345. {
  1346. if (u->state == WIN_MAXIMIZED)
  1347. return restore_window(u, idx);
  1348. return place_window(u, idx, WIN_MAXIMIZED);
  1349. }
  1350. /* One axis of edge resistance. lo/len is the window's span on that axis
  1351. * before the move, *pos the proposed start. dir < 0: the window's start side
  1352. * (left/top) resists edge e while moving towards lower coordinates; dir > 0:
  1353. * its end side (right/bottom) while moving higher. Within WM_RESIST pixels
  1354. * of overshoot the window stays on the edge; *best keeps the nearest edge. */
  1355. static void resist_edge(int lo, int len, int *pos, int e, int dir, int *best)
  1356. {
  1357. int over;
  1358. if (dir < 0 && *pos < lo && lo >= e && *pos < e) {
  1359. over = e - *pos;
  1360. if (over <= WM_RESIST && over < *best) {
  1361. *best = over;
  1362. *pos = e;
  1363. }
  1364. } else if (dir > 0 && *pos > lo && lo + len <= e && *pos + len > e) {
  1365. over = *pos + len - e;
  1366. if (over <= WM_RESIST && over < *best) {
  1367. *best = over;
  1368. *pos = e - len;
  1369. }
  1370. }
  1371. }
  1372. /* Span overlap test: edges of another window only resist when the two
  1373. * windows share some extent on the other axis (they would actually touch). */
  1374. static int spans_overlap(int a, int alen, int b, int blen)
  1375. {
  1376. return a < b + blen && b < a + alen;
  1377. }
  1378. /* Apply edge resistance to a drag from (u->info.x, u->info.y) to (*nx, *ny):
  1379. * screen edges and the facing edges of every other window. The proposed
  1380. * position is still pure pointer arithmetic, so once the overshoot passes
  1381. * WM_RESIST the window jumps to follow the pointer again. */
  1382. static void resist_geom(struct uwin *u, int idx, int *nx, int *ny)
  1383. {
  1384. int ox = u->info.x, oy = u->info.y;
  1385. int w = (int)u->info.w, h = (int)u->info.h;
  1386. int px = *nx, py = *ny;
  1387. int bestx = WM_RESIST + 1, besty = WM_RESIST + 1;
  1388. int i;
  1389. resist_edge(ox, w, &px, 0, -1, &bestx);
  1390. resist_edge(ox, w, &px, (int)fbw, 1, &bestx);
  1391. resist_edge(oy, h, &py, (int)fbh, 1, &besty);
  1392. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1393. struct wm_window_info *o = &wins[i].info;
  1394. int t;
  1395. if (i == idx || !o->used)
  1396. continue;
  1397. if (spans_overlap(oy, h, o->y, (int)o->h)) {
  1398. /* our left side against its right, our right
  1399. * side against its left */
  1400. t = *nx;
  1401. resist_edge(ox, w, &t, o->x + (int)o->w, -1, &bestx);
  1402. resist_edge(ox, w, &t, o->x, 1, &bestx);
  1403. if (t != *nx)
  1404. px = t;
  1405. }
  1406. if (spans_overlap(ox, w, o->x, (int)o->w)) {
  1407. t = *ny;
  1408. resist_edge(oy, h, &t, o->y + (int)o->h, -1, &besty);
  1409. resist_edge(oy, h, &t, o->y, 1, &besty);
  1410. if (t != *ny)
  1411. py = t;
  1412. }
  1413. }
  1414. *nx = px;
  1415. *ny = py;
  1416. }
  1417. /* The top-bar date/time text is also a button: it opens the Date & Time
  1418. * editor. The rectangle is recorded by draw_bar(). */
  1419. /* The whole right status cluster is one button: the clock, temperature,
  1420. * network and power items plus the gaps between them. Only the rectangle
  1421. * draw_bar() recorded is clickable, so the empty bar between the start
  1422. * button and the cluster does nothing. */
  1423. static int bar_status_hit(unsigned int mx, unsigned int my)
  1424. {
  1425. return bar_status_w && bar_status_x >= 0 &&
  1426. (int)my >= BAR_BTN_Y && (int)my < BAR_BTN_Y + BAR_BTN_H &&
  1427. (int)mx >= bar_status_x &&
  1428. (int)mx < bar_status_x + (int)bar_status_w;
  1429. }
  1430. static void sm_layout_get(struct sm_layout *l)
  1431. {
  1432. int avail = (int)fbh - WM_BAR_H - 8;
  1433. int row = SM_ROW_H;
  1434. int total = SM_TOP + SM_SECT_H + SM_NAPP * row + SM_GAP + row +
  1435. SM_BOTTOM;
  1436. /* Shrink the rows to the work area, so a 540-600 px panel still fits
  1437. * the whole list. The low bound is the smallest legible row. */
  1438. while (row > SM_MIN_ROW && total > avail) {
  1439. row--;
  1440. total = SM_TOP + SM_SECT_H + SM_NAPP * row + SM_GAP + row +
  1441. SM_BOTTOM;
  1442. }
  1443. l->x = 4;
  1444. l->y = WM_BAR_H + 2;
  1445. l->w = SM_W;
  1446. l->h = total;
  1447. l->row_h = row;
  1448. l->sect_y = l->y + SM_TOP;
  1449. l->row_x = l->x + SM_PAD;
  1450. l->row_w = l->w - 2 * SM_PAD;
  1451. l->list_y = l->sect_y + SM_SECT_H;
  1452. l->power_y = l->list_y + SM_NAPP * row + SM_GAP;
  1453. l->sub_w = SM_SUB_W;
  1454. l->sub_h = 2 * row + 8;
  1455. l->sub_x = l->x + l->w + 4;
  1456. if (l->sub_x + l->sub_w > (int)fbw - 2) {
  1457. if (l->x - l->sub_w - 4 >= 0)
  1458. l->sub_x = l->x - l->sub_w - 4;
  1459. else
  1460. l->sub_x = (int)fbw - 2 - l->sub_w;
  1461. }
  1462. /* The power row is the last menu row: open the submenu upwards and
  1463. * align its bottom with that row. */
  1464. l->sub_y = l->power_y + row - l->sub_h;
  1465. if (l->sub_y < WM_BAR_H + 2)
  1466. l->sub_y = WM_BAR_H + 2;
  1467. if (l->sub_y + l->sub_h > (int)fbh - 2)
  1468. l->sub_y = (int)fbh - 2 - l->sub_h;
  1469. if (l->sub_y < 0)
  1470. l->sub_y = 0;
  1471. }
  1472. static void sm_tile(int x, int y, int size, unsigned int rgb, char letter)
  1473. {
  1474. char s[2];
  1475. int tw, tx, ty;
  1476. if (size < 12)
  1477. size = 12;
  1478. s[0] = letter;
  1479. s[1] = '\0';
  1480. sm_fill_round(x, y, size, size, 4, rgb);
  1481. tw = (int)usurf_text_width(&screen, s);
  1482. tx = x + (size - tw) / 2;
  1483. ty = y + (size - 16) / 2;
  1484. usurf_text(&screen, tx, ty, s, COL_WHITE,
  1485. x, y, (unsigned int)size, (unsigned int)size);
  1486. }
  1487. static void sm_draw_app(int i, const struct sm_layout *l)
  1488. {
  1489. const struct sm_app *a = &sm_apps[i];
  1490. int ry = l->list_y + i * l->row_h;
  1491. int ty = ry + (l->row_h - 16) / 2;
  1492. int tile = l->row_h - 8;
  1493. if (tile > SM_TILE)
  1494. tile = SM_TILE;
  1495. if (sm_hot == i)
  1496. sm_fill_round(l->row_x, ry + 1, l->row_w,
  1497. l->row_h - 2, SM_RADIUS, SM_PANEL_ALT);
  1498. sm_tile(l->row_x + 10, ry + (l->row_h - tile) / 2, tile,
  1499. a->tile, a->letter);
  1500. usurf_text(&screen, l->row_x + 36, ty, a->label, SM_TEXT,
  1501. l->x, l->y, (unsigned int)l->w, (unsigned int)l->h);
  1502. }
  1503. static void sm_draw(void)
  1504. {
  1505. struct sm_layout l;
  1506. int i, ty;
  1507. if (!sm_open)
  1508. return;
  1509. sm_layout_get(&l);
  1510. /* Flat theme.bg panel with no rim, exactly like the netmenu popover.
  1511. * The Applications caption is the first item: there is no separate
  1512. * banner card. */
  1513. usurf_fill(&screen, l.x, l.y, (unsigned int)l.w, (unsigned int)l.h,
  1514. SM_BG);
  1515. usurf_text(&screen, l.x + SM_PAD, l.sect_y + 3, "Applications",
  1516. SM_TEXT_DIM, l.x, l.y, (unsigned int)l.w,
  1517. (unsigned int)l.h);
  1518. for (i = 0; i < SM_NAPP; i++)
  1519. sm_draw_app(i, &l);
  1520. /* The Power row is a quick-settings tile rather than a plain list
  1521. * row, so the submenu entry reads as the panel's last action. */
  1522. sm_fill_round(l.row_x, l.power_y, l.row_w, l.row_h, SM_RADIUS,
  1523. sm_power_open ? SM_SEL :
  1524. (sm_power_hot == 1 ? SM_BTN_HOT : SM_PANEL_ALT));
  1525. ty = l.power_y + (l.row_h - 16) / 2;
  1526. usurf_text(&screen, l.row_x + 12, ty, "Power",
  1527. sm_power_open || sm_power_hot == 1 ? COL_WHITE : SM_TEXT,
  1528. l.x, l.y, (unsigned int)l.w, (unsigned int)l.h);
  1529. {
  1530. const char *arrow = sm_power_open ? "v" : ">";
  1531. int aw = (int)usurf_text_width(&screen, arrow);
  1532. usurf_text(&screen, l.row_x + l.row_w - 12 - aw, ty, arrow,
  1533. sm_power_open || sm_power_hot == 1 ? COL_WHITE :
  1534. SM_TEXT_DIM,
  1535. l.x, l.y, (unsigned int)l.w, (unsigned int)l.h);
  1536. }
  1537. if (!sm_power_open)
  1538. return;
  1539. /* Submenu: a rounded panel card with two rounded selection rows. */
  1540. sm_fill_round(l.sub_x, l.sub_y, l.sub_w, l.sub_h, SM_RADIUS,
  1541. SM_PANEL);
  1542. for (i = 0; i < 2; i++) {
  1543. int sy = l.sub_y + 4 + i * l.row_h;
  1544. int sty = sy + (l.row_h - 16) / 2;
  1545. if (sm_power_hot == i + 2)
  1546. sm_fill_round(l.sub_x + 4, sy + 1, l.sub_w - 8,
  1547. l.row_h - 2, 4, SM_SEL);
  1548. usurf_text(&screen, l.sub_x + 12, sty,
  1549. i == 0 ? "Reboot" : "Power off",
  1550. i == 1 ? (sm_power_hot == 3 ? COL_WHITE : SM_ERROR)
  1551. : (sm_power_hot == 2 ? COL_WHITE : SM_TEXT),
  1552. l.sub_x, l.sub_y, (unsigned int)l.sub_w,
  1553. (unsigned int)l.sub_h);
  1554. }
  1555. }
  1556. static int sm_hit(unsigned int mx, unsigned int my)
  1557. {
  1558. struct sm_layout l;
  1559. int x = (int)mx, y = (int)my;
  1560. if (bar_start_hit(mx, my))
  1561. return SM_HIT_START;
  1562. if (!sm_open)
  1563. return SM_HIT_NONE;
  1564. sm_layout_get(&l);
  1565. if (sm_power_open &&
  1566. x >= l.sub_x && x < l.sub_x + l.sub_w &&
  1567. y >= l.sub_y && y < l.sub_y + l.sub_h) {
  1568. if (y >= l.sub_y + 4 && y < l.sub_y + 4 + 2 * l.row_h)
  1569. return (y - (l.sub_y + 4)) / l.row_h == 0 ?
  1570. SM_HIT_REBOOT : SM_HIT_POWEROFF;
  1571. return SM_HIT_MENU;
  1572. }
  1573. if (x < l.x || x >= l.x + l.w || y < l.y || y >= l.y + l.h)
  1574. return SM_HIT_NONE;
  1575. if (y >= l.list_y && y < l.list_y + SM_NAPP * l.row_h)
  1576. return (y - l.list_y) / l.row_h;
  1577. if (y >= l.power_y && y < l.power_y + l.row_h)
  1578. return SM_HIT_POWER;
  1579. return SM_HIT_MENU;
  1580. }
  1581. static int sm_update_hover(unsigned int mx, unsigned int my)
  1582. {
  1583. int hot = -1, pwr = 0, hit;
  1584. if (sm_open) {
  1585. hit = sm_hit(mx, my);
  1586. if (hit >= 0)
  1587. hot = hit;
  1588. else if (hit == SM_HIT_POWER)
  1589. pwr = 1;
  1590. else if (hit == SM_HIT_REBOOT)
  1591. pwr = 2;
  1592. else if (hit == SM_HIT_POWEROFF)
  1593. pwr = 3;
  1594. }
  1595. if (hot == sm_hot && pwr == sm_power_hot)
  1596. return 0;
  1597. sm_hot = hot;
  1598. sm_power_hot = pwr;
  1599. return 1;
  1600. }
  1601. static void sm_add_present(struct wm_present_info *p)
  1602. {
  1603. struct sm_layout l;
  1604. if (!sm_open)
  1605. return;
  1606. sm_layout_get(&l);
  1607. add_rect(p, l.x, l.y, (unsigned int)l.w, (unsigned int)l.h);
  1608. if (sm_power_open)
  1609. add_rect(p, l.sub_x, l.sub_y, (unsigned int)l.sub_w,
  1610. (unsigned int)l.sub_h);
  1611. }
  1612. static void sm_close(void)
  1613. {
  1614. if (!sm_open && !sm_power_open)
  1615. return;
  1616. sm_open = 0;
  1617. sm_power_open = 0;
  1618. sm_hot = -1;
  1619. sm_power_hot = 0;
  1620. full_redraw = 1;
  1621. }
  1622. /* A client can self-resize between the pre-frame info/map refresh and
  1623. * u_wm_begin(); that unmaps and remaps its content slot, leaving the map
  1624. * cached above pointing at released pages. Once the frame is active the
  1625. * kernel refuses further self-resizes, so re-reading the generation and
  1626. * remapping here makes the cached canvas safe for the draw calls. The
  1627. * window info itself is left for the next loop, which then sees the
  1628. * generation change and does a full redraw. */
  1629. static void remap_window_contents(void)
  1630. {
  1631. unsigned int i;
  1632. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1633. struct wm_window_info info;
  1634. struct wm_map_info map;
  1635. if (u_wm_info(i, &info) != 1 || !info.used) {
  1636. wins[i].mapped = 0;
  1637. continue;
  1638. }
  1639. if (wins[i].mapped &&
  1640. wins[i].map.generation == info.content_gen)
  1641. continue;
  1642. if (u_wm_map(i, &map) == 0) {
  1643. wins[i].map = map;
  1644. wins[i].mapped = 1;
  1645. } else {
  1646. wins[i].mapped = 0;
  1647. }
  1648. }
  1649. }
  1650. static void do_update(struct wm_status *st)
  1651. {
  1652. struct wm_present_info present;
  1653. int need = 0;
  1654. int bar_dirty = 0;
  1655. unsigned int i;
  1656. char dt[20];
  1657. static char last_dt[20];
  1658. int kexec_changed = (st->kexec_busy != bar_kexec_busy) ||
  1659. (st->kexec_busy && st->kexec_progress != bar_kexec_progress);
  1660. unsigned int msg_now = u_ticks_ms();
  1661. int osd_hide = 0, osd_repaint = 0;
  1662. int sm_dirty;
  1663. /* The kernel bumps backlight_seq on every brightness key/command.
  1664. * Pop the OSD on a change and keep it up for OSD_MS after the last
  1665. * one. The first poll only records the current sequence, so a WM
  1666. * restart does not flash a stale bubble. */
  1667. if (st->backlight_present) {
  1668. if (!osd_seen_valid) {
  1669. osd_seen_valid = 1;
  1670. osd_seen_seq = st->backlight_seq;
  1671. } else if (st->backlight_seq != osd_seen_seq) {
  1672. osd_seen_seq = st->backlight_seq;
  1673. osd_level = (int)st->backlight_level;
  1674. osd_until = msg_now + OSD_MS;
  1675. osd_show = 1;
  1676. }
  1677. } else if (osd_show) {
  1678. osd_show = 0;
  1679. osd_hide = 1;
  1680. }
  1681. if (osd_show && (int)(msg_now - osd_until) >= 0) {
  1682. osd_show = 0;
  1683. osd_hide = 1;
  1684. }
  1685. if (osd_hide && osd_visible)
  1686. osd_repaint = 1;
  1687. sm_dirty = sm_update_hover(st->mouse.x, st->mouse.y);
  1688. /* The boot-scan window is created before the terminal so the terminal
  1689. * stays the newest (last) window and keeps the keyboard focus. Wait
  1690. * for the scan window to exist before starting the terminal; a failed
  1691. * or wedged helper is bounded so the shell is never held back. */
  1692. if (st->boot_ready && !boot_term_spawned) {
  1693. if (!boot_scan_spawned) {
  1694. boot_scan_spawned = 1;
  1695. if (free_window_slot()) {
  1696. boot_scan_pid = u_spawn("/ramfs/bin/bootscan",
  1697. "", 0, "/");
  1698. if (boot_scan_pid > 0)
  1699. boot_scan_deadline = msg_now + 2000u;
  1700. }
  1701. if (boot_scan_pid <= 0)
  1702. boot_scan_placed = 1;
  1703. }
  1704. if (boot_scan_pid > 0 && !boot_scan_placed) {
  1705. struct wm_window_info wi;
  1706. int idx = window_index_pid(boot_scan_pid, &wi);
  1707. if (idx >= 0) {
  1708. int nx = (int)fbw - (int)wi.w - 16;
  1709. int ny = WM_BAR_H + 8;
  1710. if (nx < 8)
  1711. nx = 8;
  1712. u_wm_set_geom((unsigned int)idx, nx, ny);
  1713. boot_scan_placed = 1;
  1714. } else if (!u_alive((unsigned int)boot_scan_pid) ||
  1715. (int)(msg_now - boot_scan_deadline) >= 0) {
  1716. if (u_alive((unsigned int)boot_scan_pid))
  1717. u_kill((unsigned int)boot_scan_pid);
  1718. boot_scan_placed = 1;
  1719. }
  1720. }
  1721. if (boot_scan_placed) {
  1722. boot_term_spawned = 1;
  1723. if (!free_window_slot()) {
  1724. bar_notice("window limit");
  1725. full_redraw = 1;
  1726. } else if (u_wm_create_term() < 0) {
  1727. bar_notice("cannot start term");
  1728. full_redraw = 1;
  1729. } else {
  1730. full_redraw = 1;
  1731. }
  1732. }
  1733. }
  1734. if (bar_msg_dirty ||
  1735. (bar_msg_until && (int)(msg_now - bar_msg_until) >= 0)) {
  1736. bar_dirty = 1;
  1737. bar_msg_dirty = 0;
  1738. if (bar_msg_until && (int)(msg_now - bar_msg_until) >= 0)
  1739. bar_msg_until = 0;
  1740. }
  1741. bar_kexec_busy = st->kexec_busy;
  1742. bar_kexec_progress = st->kexec_progress;
  1743. bar_kexec_total = st->kexec_total;
  1744. if (kexec_changed)
  1745. bar_dirty = 1;
  1746. {
  1747. int hot = bar_start_hit(st->mouse.x, st->mouse.y);
  1748. if (hot != sm_start_hot) {
  1749. sm_start_hot = hot;
  1750. bar_dirty = 1;
  1751. }
  1752. }
  1753. {
  1754. int hot = bar_status_hit(st->mouse.x, st->mouse.y);
  1755. if (hot != bar_status_hot) {
  1756. bar_status_hot = hot;
  1757. bar_dirty = 1;
  1758. }
  1759. }
  1760. /* Refresh cached window state and mappings before opening a frame:
  1761. * MAP may unmap/remap content. */
  1762. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1763. struct wm_window_info info;
  1764. int r = u_wm_info(i, &info);
  1765. if (r < 0) {
  1766. u_exit(1);
  1767. }
  1768. if (r == 0 || !info.used) {
  1769. if (wins[i].info.used)
  1770. full_redraw = 1;
  1771. wins[i].info.used = 0;
  1772. wins[i].mapped = 0;
  1773. continue;
  1774. }
  1775. {
  1776. int pid_changed = wins[i].info.used &&
  1777. info.pid != wins[i].info.pid;
  1778. if (!wins[i].info.used ||
  1779. info.content_gen != wins[i].info.content_gen ||
  1780. pid_changed)
  1781. full_redraw = 1;
  1782. wins[i].info = info;
  1783. if (!wins[i].mapped || pid_changed ||
  1784. wins[i].map.generation != info.content_gen) {
  1785. struct wm_map_info map;
  1786. if (u_wm_map(i, &map) == 0) {
  1787. wins[i].map = map;
  1788. wins[i].mapped = 1;
  1789. } else {
  1790. wins[i].mapped = 0;
  1791. }
  1792. }
  1793. }
  1794. }
  1795. /* Popovers are WM-anchored overlays, flush against the bottom of the
  1796. * bar and the right screen edge. The WM owns the geometry, so the app
  1797. * does not need a placement ABI. */
  1798. {
  1799. int control_found = 0;
  1800. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1801. struct wm_window_info ni;
  1802. int nx, ny;
  1803. if (!wins[i].info.used || !wins[i].info.popover)
  1804. continue;
  1805. if (control_pid > 0 &&
  1806. wins[i].info.pid == (unsigned int)control_pid)
  1807. control_found = 1;
  1808. nx = (int)fbw - (int)wins[i].info.w;
  1809. ny = WM_BAR_H;
  1810. if (nx != wins[i].info.x || ny != wins[i].info.y) {
  1811. if (u_wm_set_geom(i, nx, ny) == 0 &&
  1812. u_wm_info(i, &ni) == 1) {
  1813. wins[i].info = ni;
  1814. full_redraw = 1;
  1815. }
  1816. }
  1817. }
  1818. if (!control_found && control_pid > 0 &&
  1819. !u_alive((unsigned int)control_pid)) {
  1820. control_pid = 0;
  1821. bar_dirty = 1;
  1822. }
  1823. }
  1824. /* The RTC is slow (CMOS polling) and the temperature/battery state only
  1825. * changes once a second; query them at 1 Hz instead of every 10 ms. */
  1826. {
  1827. static unsigned int last_rtc_ms;
  1828. unsigned int now_ms = u_ticks_ms();
  1829. if (full_redraw || now_ms - last_rtc_ms >= 1000) {
  1830. last_rtc_ms = now_ms;
  1831. if (u_rtc_datetime(dt, sizeof(dt)) == 0) {
  1832. for (i = 0; i < sizeof(dt); i++)
  1833. if (dt[i] != last_dt[i]) {
  1834. for (i = 0; i < sizeof(dt); i++)
  1835. last_dt[i] = dt[i];
  1836. bar_dirty = 1;
  1837. break;
  1838. }
  1839. }
  1840. {
  1841. struct sys_cputemp ct;
  1842. if (u_cpu_temp(&ct) == 0 &&
  1843. ct.temp_c != bar_temp_c) {
  1844. bar_temp_c = ct.temp_c;
  1845. bar_dirty = 1;
  1846. }
  1847. }
  1848. {
  1849. struct sys_powerinfo pi;
  1850. int pct = -1, charging = 0, ac = -1;
  1851. unsigned int bi;
  1852. if (u_power_info(&pi, 0) == 0) {
  1853. /* A laptop EC (ThinkPad or Chrome
  1854. * EC) marks a machine with AC/battery;
  1855. * desktops show neither. */
  1856. if (pi.present) {
  1857. ac = pi.ac_online;
  1858. for (bi = 0; bi < SYS_POWER_BATT_MAX;
  1859. bi++) {
  1860. struct sys_battery *b =
  1861. &pi.battery[bi];
  1862. if (!b->present ||
  1863. !b->percent_valid)
  1864. continue;
  1865. pct = (int)b->percent;
  1866. charging = (int)b->charging;
  1867. break;
  1868. }
  1869. }
  1870. if (pct != bar_batt_pct ||
  1871. charging != bar_batt_charging ||
  1872. ac != bar_ac_online) {
  1873. bar_batt_pct = pct;
  1874. bar_batt_charging = charging;
  1875. bar_ac_online = ac;
  1876. bar_dirty = 1;
  1877. }
  1878. }
  1879. }
  1880. if (poll_net_status())
  1881. bar_dirty = 1;
  1882. }
  1883. }
  1884. if (full_redraw) {
  1885. if (u_wm_begin() != 0) {
  1886. full_redraw = 1;
  1887. return;
  1888. }
  1889. full_redraw = 0;
  1890. remap_window_contents();
  1891. need = 1;
  1892. {
  1893. draw_rect(0, 0, fbw, fbh);
  1894. draw_bar();
  1895. if (osd_show)
  1896. osd_draw();
  1897. osd_visible = osd_show;
  1898. sm_draw();
  1899. present.count = 0;
  1900. add_rect(&present, 0, 0, fbw, fbh);
  1901. present.immediate = 0;
  1902. u_wm_present(&present);
  1903. /* Ack every window that was clean at the time of the
  1904. * redraw. If new damage arrived during it, its seq
  1905. * differs and the kernel leaves it dirty. */
  1906. for (i = 0; i < WM_MAX_WINDOWS; i++)
  1907. if (wins[i].info.used && wins[i].mapped)
  1908. u_wm_ack(i, wins[i].info.dirty_seq);
  1909. }
  1910. return;
  1911. }
  1912. /* Partial cell/canvas updates and the bar clock. */
  1913. present.count = 0;
  1914. present.immediate = 0;
  1915. if (osd_repaint) {
  1916. add_rect(&present, osd_x, osd_y, osd_w, osd_h);
  1917. need = 1;
  1918. }
  1919. if (osd_show)
  1920. need = 1;
  1921. if (bar_dirty) {
  1922. need = 1;
  1923. add_rect(&present, 0, 0, fbw, WM_BAR_H);
  1924. }
  1925. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1926. struct uwin *u = &wins[i];
  1927. struct wm_window_info *w = &u->info;
  1928. int rx, ry;
  1929. unsigned int rw, rh;
  1930. if (!w->used || !w->dirty)
  1931. continue;
  1932. if (!w->dmg_w || !w->dmg_h)
  1933. continue;
  1934. rx = w->x + (w->popover ? 0 : WM_BORDER_W) + (int)w->dmg_x;
  1935. ry = w->y + (w->popover ? 0 : WM_BORDER_W + WM_TITLE_H) +
  1936. (int)w->dmg_y;
  1937. rw = w->dmg_w;
  1938. rh = w->dmg_h;
  1939. add_rect(&present, rx, ry, rw, rh);
  1940. u->ack_seq = u->mapped ? w->dirty_seq : 0;
  1941. need = 1;
  1942. }
  1943. if (sm_dirty && sm_open)
  1944. need = 1;
  1945. if (!need) {
  1946. /* Drag movement is handled below; a stationary mouse can be
  1947. * left to the kernel's cursor overlay. */
  1948. return;
  1949. }
  1950. if (sm_open)
  1951. sm_add_present(&present);
  1952. if (u_wm_begin() != 0)
  1953. return;
  1954. remap_window_contents();
  1955. for (i = 0; i < present.count; i++)
  1956. draw_rect(present.rect[i].x, present.rect[i].y,
  1957. present.rect[i].w, present.rect[i].h);
  1958. if (bar_dirty)
  1959. draw_bar();
  1960. if (osd_show) {
  1961. osd_draw();
  1962. add_rect(&present, osd_x, osd_y, osd_w, osd_h);
  1963. }
  1964. osd_visible = osd_show;
  1965. sm_draw();
  1966. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  1967. struct uwin *u = &wins[i];
  1968. if (!u->info.used || !u->info.dirty || !u->mapped ||
  1969. !u->ack_seq)
  1970. continue;
  1971. if (u->ack_seq == u->info.dirty_seq)
  1972. u_wm_ack(i, u->info.dirty_seq);
  1973. }
  1974. u_wm_present(&present);
  1975. }
  1976. static void spawn_app_arg_checked(const char *path, const char *arg)
  1977. {
  1978. if (!free_window_slot()) {
  1979. bar_notice("window limit");
  1980. full_redraw = 1;
  1981. return;
  1982. }
  1983. if (u_spawn(path, arg, 0, "/") < 0) {
  1984. bar_notice("spawn failed");
  1985. full_redraw = 1;
  1986. } else {
  1987. full_redraw = 1;
  1988. }
  1989. }
  1990. /* Any status target opens one control panel; a second click closes it. */
  1991. static void open_control_panel(void)
  1992. {
  1993. unsigned int i;
  1994. if (control_pid > 0 && u_alive((unsigned int)control_pid)) {
  1995. for (i = 0; i < WM_MAX_WINDOWS; i++)
  1996. if (wins[i].info.used &&
  1997. wins[i].info.pid == (unsigned int)control_pid) {
  1998. if (u_wm_close(i) == 0) {
  1999. control_pid = 0;
  2000. full_redraw = 1;
  2001. } else {
  2002. u_wm_focus(i);
  2003. full_redraw = 1;
  2004. }
  2005. return;
  2006. }
  2007. /* The process is alive but has not created its window yet. */
  2008. return;
  2009. }
  2010. if (!free_window_slot()) {
  2011. bar_notice("window limit");
  2012. full_redraw = 1;
  2013. return;
  2014. }
  2015. control_pid = u_spawn("/ramfs/bin/netmenu", "", 0, "/");
  2016. if (control_pid < 0) {
  2017. control_pid = 0;
  2018. bar_notice("spawn failed");
  2019. full_redraw = 1;
  2020. } else {
  2021. full_redraw = 1;
  2022. }
  2023. }
  2024. /* Launch one start-menu entry. The terminal and the control panel are WM
  2025. * actions; every other entry spawns its ELF from the pack. */
  2026. static void sm_launch(int idx)
  2027. {
  2028. const struct sm_app *a;
  2029. if (idx < 0 || idx >= SM_NAPP) {
  2030. sm_close();
  2031. return;
  2032. }
  2033. a = &sm_apps[idx];
  2034. sm_close();
  2035. if (a->action == SM_ACT_TERM) {
  2036. if (!free_window_slot()) {
  2037. bar_notice("window limit");
  2038. full_redraw = 1;
  2039. } else if (u_wm_create_term() < 0) {
  2040. bar_notice("cannot start term");
  2041. full_redraw = 1;
  2042. } else {
  2043. full_redraw = 1;
  2044. }
  2045. } else if (a->action == SM_ACT_SETTINGS) {
  2046. open_control_panel();
  2047. } else {
  2048. spawn_app_arg_checked(a->path, a->arg);
  2049. }
  2050. }
  2051. /* Index of a popover containing the point, or -1. */
  2052. static int popover_under(unsigned int mx, unsigned int my)
  2053. {
  2054. unsigned int i;
  2055. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  2056. struct wm_window_info *w = &wins[i].info;
  2057. if (w->used && w->popover &&
  2058. mx >= (unsigned int)w->x && my >= (unsigned int)w->y &&
  2059. mx < (unsigned int)(w->x + (int)w->w) &&
  2060. my < (unsigned int)(w->y + (int)w->h))
  2061. return (int)i;
  2062. }
  2063. return -1;
  2064. }
  2065. /* A press anywhere outside an open popover dismisses it; the click then
  2066. * continues normally so the target under the pointer still reacts. */
  2067. static void dismiss_popover_outside(unsigned int mx, unsigned int my)
  2068. {
  2069. unsigned int i;
  2070. if (popover_under(mx, my) >= 0)
  2071. return;
  2072. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  2073. if (!wins[i].info.used || !wins[i].info.popover)
  2074. continue;
  2075. if (wins[i].info.pid == (unsigned int)control_pid)
  2076. control_pid = 0;
  2077. if (u_wm_close(i) == 0) {
  2078. wins[i].info.used = 0;
  2079. wins[i].mapped = 0;
  2080. full_redraw = 1;
  2081. }
  2082. break;
  2083. }
  2084. }
  2085. /* Apply one edge-drag step. The grabbed edges follow the pointer while the
  2086. * opposite edges stay put; the work area clamps the active edges. */
  2087. static void resize_window_drag(struct uwin *u, unsigned int idx,
  2088. unsigned int mx, unsigned int my)
  2089. {
  2090. int dx = (int)mx - (int)resize_mx;
  2091. int dy = (int)my - (int)resize_my;
  2092. int left = resize_ox;
  2093. int right = resize_ox + (int)resize_ow;
  2094. int top = resize_oy;
  2095. int bottom = resize_oy + (int)resize_oh;
  2096. int nx, ny, nw, nh;
  2097. if (resize_edges & WM_EDGE_LEFT) {
  2098. left = resize_ox + dx;
  2099. if (left < 0)
  2100. left = 0;
  2101. } else if (resize_edges & WM_EDGE_RIGHT) {
  2102. right = resize_ox + (int)resize_ow + dx;
  2103. if (right > (int)fbw)
  2104. right = (int)fbw;
  2105. }
  2106. if (resize_edges & WM_EDGE_TOP) {
  2107. top = resize_oy + dy;
  2108. if (top < WM_BAR_H)
  2109. top = WM_BAR_H;
  2110. } else if (resize_edges & WM_EDGE_BOTTOM) {
  2111. bottom = resize_oy + (int)resize_oh + dy;
  2112. if (bottom > (int)fbh)
  2113. bottom = (int)fbh;
  2114. }
  2115. if ((resize_edges & (WM_EDGE_LEFT | WM_EDGE_RIGHT)) &&
  2116. right - left < 200) {
  2117. if (resize_edges & WM_EDGE_LEFT)
  2118. left = right - 200;
  2119. else
  2120. right = left + 200;
  2121. }
  2122. if ((resize_edges & (WM_EDGE_TOP | WM_EDGE_BOTTOM)) &&
  2123. bottom - top < 140) {
  2124. if (resize_edges & WM_EDGE_TOP)
  2125. top = bottom - 140;
  2126. else
  2127. bottom = top + 140;
  2128. }
  2129. if (left < 0)
  2130. left = 0;
  2131. if (top < WM_BAR_H)
  2132. top = WM_BAR_H;
  2133. nx = left;
  2134. ny = top;
  2135. nw = right - left;
  2136. nh = bottom - top;
  2137. if (nw > (int)fbw)
  2138. nw = (int)fbw;
  2139. if (nh > (int)fbh - WM_BAR_H)
  2140. nh = (int)fbh - WM_BAR_H;
  2141. if (nx == u->info.x && ny == u->info.y &&
  2142. nw == (int)u->info.w && nh == (int)u->info.h)
  2143. return;
  2144. if (set_window_geometry(u, idx, nx, ny, (unsigned int)nw,
  2145. (unsigned int)nh) == 0)
  2146. u->state = WIN_NORMAL;
  2147. }
  2148. /* Capture the pointer for an edge resize that started at (mx,my). */
  2149. static void begin_resize(unsigned int idx, int edges, unsigned int mx,
  2150. unsigned int my)
  2151. {
  2152. struct uwin *u = &wins[idx];
  2153. resize_id = (int)idx;
  2154. resize_edges = edges;
  2155. resize_mx = mx;
  2156. resize_my = my;
  2157. resize_ox = u->info.x;
  2158. resize_oy = u->info.y;
  2159. resize_ow = u->info.w;
  2160. resize_oh = u->info.h;
  2161. u_wm_mouse_capture(1);
  2162. }
  2163. /* The WM's cursor override (SYS_WM_SET_CURSOR): resize arrows on the
  2164. * invisible resize border and while resizing, the move cross while a
  2165. * title-bar drag is under way, a hand over the start button, the open start
  2166. * menu and the status area. Everywhere else the kernel picks the window's
  2167. * own content cursor (or the arrow). */
  2168. static void update_cursor(unsigned int mx, unsigned int my)
  2169. {
  2170. static int current = CURSOR_NO_OVERRIDE;
  2171. int want = CURSOR_NO_OVERRIDE;
  2172. if (resize_id >= 0) {
  2173. want = resize_cursor(resize_edges);
  2174. } else if (drag_id >= 0) {
  2175. if (drag_moved)
  2176. want = CURSOR_MOVE;
  2177. } else if (sm_open) {
  2178. /* The menu is modal: a hand over its items, the arrow
  2179. * elsewhere, never a window's content cursor underneath. */
  2180. int hit = sm_hit(mx, my);
  2181. want = (hit == SM_HIT_NONE || hit == SM_HIT_MENU) ?
  2182. CURSOR_DEFAULT : CURSOR_POINTER;
  2183. } else if (my < WM_BAR_H) {
  2184. if (bar_start_hit(mx, my) || bar_status_hit(mx, my))
  2185. want = CURSOR_POINTER;
  2186. } else {
  2187. int edges = 0;
  2188. int idx = resize_hit((int)mx, (int)my, &edges);
  2189. if (idx >= 0 && edges &&
  2190. !close_hit(&wins[idx].info, (int)mx, (int)my))
  2191. want = resize_cursor(edges);
  2192. }
  2193. if (want != current && u_wm_set_cursor(want) == 0)
  2194. current = want;
  2195. }
  2196. static void handle_mouse(struct wm_status *st)
  2197. {
  2198. unsigned int buttons = st->mouse.buttons;
  2199. unsigned int mx = st->mouse.x, my = st->mouse.y;
  2200. int down = (buttons & 1) != 0;
  2201. int was = (prev_buttons & 1) != 0;
  2202. if (down && !was) {
  2203. int prev_title_id = last_title_id;
  2204. unsigned int now = u_ticks_ms();
  2205. int hit;
  2206. /* The start menu is modal: a press on an entry launches it,
  2207. * a press on Power opens its submenu, and any other press
  2208. * dismisses the menu without reaching the window below. */
  2209. if (sm_open) {
  2210. hit = sm_hit(mx, my);
  2211. last_title_id = -1;
  2212. if (hit == SM_HIT_START) {
  2213. sm_close();
  2214. } else if (hit == SM_HIT_POWER) {
  2215. sm_power_open = !sm_power_open;
  2216. full_redraw = 1;
  2217. } else if (hit == SM_HIT_REBOOT) {
  2218. sm_close();
  2219. if (u_spawn_kcmd("reboot", "", 0) < 0)
  2220. bar_notice("reboot failed");
  2221. } else if (hit == SM_HIT_POWEROFF) {
  2222. sm_close();
  2223. if (u_spawn_kcmd("poweroff", "", 0) < 0)
  2224. bar_notice("poweroff failed");
  2225. } else if (hit >= 0) {
  2226. sm_launch(hit);
  2227. } else if (hit != SM_HIT_MENU) {
  2228. sm_close();
  2229. }
  2230. goto mouse_done;
  2231. }
  2232. if (bar_start_hit(mx, my)) {
  2233. /* Opening the menu also closes an open control
  2234. * panel, so only one floating panel is visible. */
  2235. dismiss_popover_outside(mx, my);
  2236. last_title_id = -1;
  2237. sm_open = 1;
  2238. sm_power_open = 0;
  2239. sm_hot = -1;
  2240. sm_power_hot = 0;
  2241. full_redraw = 1;
  2242. goto mouse_done;
  2243. }
  2244. if (!bar_status_hit(mx, my))
  2245. dismiss_popover_outside(mx, my);
  2246. last_title_id = -1;
  2247. if (bar_status_hit(mx, my)) {
  2248. open_control_panel();
  2249. } else {
  2250. int edges = 0;
  2251. int idx = resize_hit((int)mx, (int)my, &edges);
  2252. if (idx >= 0) {
  2253. struct uwin *u = &wins[idx];
  2254. int pmx = (int)mx, pmy = (int)my;
  2255. u_wm_focus((unsigned int)idx);
  2256. if (u->info.popover) {
  2257. /* A popover has no title bar, close
  2258. * button or resize border; the app owns
  2259. * its content. */
  2260. } else if (close_hit(&u->info, pmx, pmy)) {
  2261. u_wm_close((unsigned int)idx);
  2262. full_redraw = 1;
  2263. } else if (edges) {
  2264. begin_resize((unsigned int)idx, edges,
  2265. mx, my);
  2266. } else if (pmy < u->info.y + WM_TITLE_H) {
  2267. if (prev_title_id == idx &&
  2268. now - last_title_ms <= WM_DBLCLICK_MS &&
  2269. iabs(pmx - last_title_x) <= WM_DBLCLICK_SLOP &&
  2270. iabs(pmy - last_title_y) <= WM_DBLCLICK_SLOP) {
  2271. last_title_ms = 0;
  2272. if (toggle_maximize(u,
  2273. (unsigned int)idx) != 0)
  2274. bar_notice("resize failed");
  2275. } else {
  2276. last_title_id = idx;
  2277. last_title_ms = now;
  2278. last_title_x = pmx;
  2279. last_title_y = pmy;
  2280. drag_id = idx;
  2281. drag_mx = pmx;
  2282. drag_my = pmy;
  2283. drag_dx = pmx - u->info.x;
  2284. drag_dy = pmy - u->info.y;
  2285. drag_moved = 0;
  2286. u_wm_mouse_capture(1);
  2287. }
  2288. }
  2289. }
  2290. }
  2291. } else if (down && was && resize_id >= 0) {
  2292. struct uwin *u = &wins[resize_id];
  2293. if (u->info.used)
  2294. resize_window_drag(u, (unsigned int)resize_id, mx, my);
  2295. else {
  2296. resize_id = -1;
  2297. u_wm_mouse_capture(0);
  2298. }
  2299. } else if (down && was && drag_id >= 0) {
  2300. struct uwin *u = &wins[drag_id];
  2301. if (u->info.used) {
  2302. int ox, oy, nx, ny;
  2303. if (iabs((int)mx - drag_mx) >= WM_DRAG_START ||
  2304. iabs((int)my - drag_my) >= WM_DRAG_START)
  2305. drag_moved = 1;
  2306. /* A maximized or snapped window is restored as soon as
  2307. * the drag starts. Keep the grab point at the same
  2308. * fraction of the title bar, so the restored (narrower)
  2309. * window stays under the pointer. A click that does
  2310. * not move must leave it in place. */
  2311. if (u->state != WIN_NORMAL && drag_moved) {
  2312. unsigned int ow = u->info.w;
  2313. int gx = drag_mx - u->info.x;
  2314. int gy = drag_my - u->info.y;
  2315. if (restore_window(u,
  2316. (unsigned int)drag_id) == 0) {
  2317. if (ow)
  2318. gx = gx * (int)u->info.w /
  2319. (int)ow;
  2320. drag_dx = gx;
  2321. drag_dy = gy;
  2322. }
  2323. }
  2324. if (u->state == WIN_NORMAL) {
  2325. ox = u->info.x;
  2326. oy = u->info.y;
  2327. nx = (int)mx - drag_dx;
  2328. ny = (int)my - drag_dy;
  2329. resist_geom(u, drag_id, &nx, &ny);
  2330. clamp_geom(u, &nx, &ny);
  2331. if (nx != ox || ny != oy) {
  2332. struct wm_present_info present;
  2333. unsigned int i;
  2334. u_wm_set_geom((unsigned int)drag_id,
  2335. nx, ny);
  2336. u->info.x = nx;
  2337. u->info.y = ny;
  2338. if (u->info.focused) {
  2339. move_window_pixels(u, ox, oy,
  2340. nx, ny);
  2341. } else {
  2342. present.count = 0;
  2343. present.immediate = 1;
  2344. add_rect(&present, ox, oy,
  2345. u->info.w, u->info.h);
  2346. add_rect(&present, nx, ny,
  2347. u->info.w, u->info.h);
  2348. if (u_wm_begin() == 0) {
  2349. remap_window_contents();
  2350. for (i = 0;
  2351. i < present.count; i++)
  2352. draw_rect(
  2353. present.rect[i].x,
  2354. present.rect[i].y,
  2355. present.rect[i].w,
  2356. present.rect[i].h);
  2357. wm_overlays(&present);
  2358. u_wm_present(&present);
  2359. }
  2360. }
  2361. }
  2362. }
  2363. } else {
  2364. drag_id = -1;
  2365. u_wm_mouse_capture(0);
  2366. }
  2367. } else if (!down && was) {
  2368. /* Dropping a window with the pointer on the left/right screen
  2369. * edge snaps it to that half; on the top edge it maximizes. */
  2370. if (drag_id >= 0 && drag_moved && wins[drag_id].info.used &&
  2371. wins[drag_id].state == WIN_NORMAL) {
  2372. struct uwin *u = &wins[drag_id];
  2373. int st = WIN_NORMAL;
  2374. if ((int)mx < WM_SNAP_EDGE)
  2375. st = WIN_SNAP_LEFT;
  2376. else if ((int)mx >= (int)fbw - WM_SNAP_EDGE)
  2377. st = WIN_SNAP_RIGHT;
  2378. else if ((int)my < WM_SNAP_EDGE)
  2379. st = WIN_MAXIMIZED;
  2380. if (st != WIN_NORMAL &&
  2381. place_window(u, (unsigned int)drag_id, st) != 0)
  2382. bar_notice("resize failed");
  2383. }
  2384. drag_id = -1;
  2385. resize_id = -1;
  2386. u_wm_mouse_capture(0);
  2387. }
  2388. mouse_done:
  2389. prev_buttons = buttons;
  2390. update_cursor(mx, my);
  2391. }
  2392. static char *put_str(char *p, const char *s)
  2393. {
  2394. while (*s)
  2395. *p++ = *s++;
  2396. return p;
  2397. }
  2398. static char *put_dec(char *p, unsigned int v)
  2399. {
  2400. char b[10];
  2401. unsigned int n = 0;
  2402. do {
  2403. b[n++] = (char)('0' + v % 10);
  2404. v /= 10;
  2405. } while (v);
  2406. while (n)
  2407. *p++ = b[--n];
  2408. return p;
  2409. }
  2410. /* Expose the last interval in /share so the target can be measured without
  2411. * a serial console. */
  2412. static void write_stats(void)
  2413. {
  2414. char b[80], *p = b;
  2415. unsigned int now = u_ticks_ms();
  2416. p = put_str(p, "t=");
  2417. p = put_dec(p, now / 1000);
  2418. p = put_str(p, "s frames=");
  2419. p = put_dec(p, stat_frames);
  2420. p = put_str(p, " ms=");
  2421. p = put_dec(p, stat_ms);
  2422. p = put_str(p, " max=");
  2423. p = put_dec(p, stat_max);
  2424. p = put_str(p, " px=");
  2425. p = put_dec(p, stat_px);
  2426. p = put_str(p, "\n");
  2427. u_syscall3(SYS_WRITE_FILE, (int)"/share/wmstats", (int)b,
  2428. (int)(p - b));
  2429. }
  2430. void _start(const char *arg)
  2431. {
  2432. struct wm_screen_info si;
  2433. struct wm_status st;
  2434. unsigned int i;
  2435. (void)arg;
  2436. if (u_wm_attach(&si) != 0)
  2437. u_exit(1);
  2438. usurf_init(&screen, si.fb, si.width, si.height, si.pitch, si.bpp);
  2439. usurf_font_attach(&screen, "/ramfs/fonts/sans.ttf", 14);
  2440. fbw = si.width;
  2441. fbh = si.height;
  2442. wall_load();
  2443. for (i = 0; i < WM_MAX_WINDOWS; i++) {
  2444. wins[i].info.used = 0;
  2445. wins[i].mapped = 0;
  2446. }
  2447. full_redraw = 1;
  2448. for (;;) {
  2449. unsigned int t0, t1;
  2450. if (u_wm_poll(&st) != 0)
  2451. u_exit(1);
  2452. if (st.wallpaper_seq != wall_seq_seen) {
  2453. wall_seq_seen = st.wallpaper_seq;
  2454. wall_load();
  2455. full_redraw = 1;
  2456. }
  2457. /* The WM starts before the storage scan, so the first load may not
  2458. * see /data yet. Reload when boot_ready says the mounts are up,
  2459. * and retry for ten seconds for the deferred USB /data volume. */
  2460. if (st.boot_ready && !wall_boot_seen) {
  2461. wall_boot_seen = 1;
  2462. wall_retries = 0;
  2463. wall_retry_ms = u_ticks_ms() + 1000;
  2464. wall_load();
  2465. full_redraw = 1;
  2466. } else if (wall_boot_seen && !wall_ready && wall_retries < 10 &&
  2467. (int)(u_ticks_ms() - wall_retry_ms) >= 0) {
  2468. wall_retries++;
  2469. wall_retry_ms += 1000;
  2470. wall_load();
  2471. if (wall_ready)
  2472. full_redraw = 1;
  2473. }
  2474. if (st.full_redraw)
  2475. full_redraw = 1;
  2476. usurf_pixels = 0;
  2477. t0 = u_ticks_ms();
  2478. handle_mouse(&st);
  2479. do_update(&st);
  2480. t1 = u_ticks_ms();
  2481. if (usurf_pixels) {
  2482. unsigned int dt = t1 - t0;
  2483. stat_frames++;
  2484. stat_ms += dt;
  2485. stat_px += usurf_pixels;
  2486. if (dt > stat_max)
  2487. stat_max = dt;
  2488. }
  2489. if (t1 - stat_last >= 2000) {
  2490. stat_last = t1;
  2491. if (stat_frames) {
  2492. write_stats();
  2493. stat_frames = stat_ms = stat_px = stat_max = 0;
  2494. }
  2495. }
  2496. u_sleep_ms(10);
  2497. }
  2498. }