ucmd_wm.c 92 KB

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