background.lua (3323B)
1 -- Background - LuajitOS Desktop Background 2 -- Displays /os/public/res/background.bmp as the desktop background 3 4 if osprint then 5 osprint("Background: Starting...\n") 6 end 7 8 -- Check if app instance is available 9 if not app then 10 if osprint then 11 osprint("Background: ERROR - app instance not available\n") 12 end 13 return 14 end 15 16 -- Check if required functions are available 17 if not CRamdiskOpen then 18 if osprint then 19 osprint("Background: ERROR - CRamdiskOpen not available (missing 'ramdisk' permission)\n") 20 end 21 return 22 end 23 24 -- Load the background image 25 local imagePath = "/os/public/res/background.bmp" 26 27 if osprint then 28 osprint("Background: Loading image from: " .. imagePath .. "\n") 29 end 30 31 -- Open and read the image file using direct ramdisk access 32 local handle, err = CRamdiskOpen(imagePath, "r") 33 if not handle then 34 if osprint then 35 osprint("Background: ERROR - Failed to open file: " .. tostring(err) .. "\n") 36 end 37 return 38 end 39 40 local bmp_data = CRamdiskRead(handle) 41 CRamdiskClose(handle) 42 43 if not bmp_data then 44 if osprint then 45 osprint("Background: ERROR - Failed to read file data\n") 46 end 47 return 48 end 49 50 if osprint then 51 osprint("Background: Read " .. #bmp_data .. " bytes from " .. imagePath .. "\n") 52 end 53 54 -- Decode the image ONCE at startup, not on every frame! 55 local image = BMPLoad(bmp_data) 56 if not image then 57 if osprint then 58 osprint("Background: ERROR - Failed to decode BMP at startup\n") 59 end 60 return 61 end 62 63 -- Get image info 64 local imageInfo = ImageGetInfo(image) 65 if imageInfo and osprint then 66 osprint("Background: Image decoded: " .. imageInfo.width .. "x" .. imageInfo.height .. "\n") 67 end 68 69 -- We don't need the raw BMP data anymore 70 bmp_data = nil 71 72 -- Create a fullscreen window (entire screen) 73 -- Get screen dimensions from sys.screen (requires 'export' or 'system-all' permission) 74 local screenWidth = (sys and sys.screen and sys.screen[1] and sys.screen[1].width) or 1200 75 local screenHeight = (sys and sys.screen and sys.screen[1] and sys.screen[1].height) or 900 76 local window = app:newWindow(0, 0, screenWidth, screenHeight) -- Full screen at top-left 77 78 if not window then 79 if osprint then 80 osprint("Background: ERROR - Failed to create window\n") 81 end 82 return 83 end 84 85 -- Make the window borderless (no title bar or border) 86 window.isBorderless = true 87 88 -- Mark as background window (always drawn first, never focusable) 89 window.isBackground = true 90 window.createdAt = -1 -- Ensure it's always first in Z-order 91 window.noTaskbar = true -- Don't show in taskbar 92 93 -- Calculate position to anchor image's bottom-left to screen's bottom-left 94 local imageHeight = imageInfo and imageInfo.height or screenHeight 95 local drawY = screenHeight - imageHeight -- Position so bottom of image aligns with bottom of screen 96 97 -- Set up the draw callback to render the pre-decoded background 98 window.onDraw = function(gfx) 99 -- Draw the background image anchored at bottom-left, extending up and right 100 ImageDraw(image, 0, drawY) 101 end 102 103 -- Set up click handler to notify when background is clicked 104 window.onClick = function(mx, my) 105 if sys and sys.hook and sys.hook.run then 106 sys.hook.run("BackgroundFocused") 107 end 108 end 109 110 if osprint then 111 osprint("Background: Window created and draw callback registered\n") 112 end