luajitos

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

postinit.lua (21659B)


      1 -- Post-initialization script
      2 -- Called after Sys.lua and Run.lua are loaded, before the render loop starts
      3 
      4 osprint("\n=== Running Post-Init ===\n")
      5 osprint("DEBUG: Post-init started\n")
      6 
      7 -- Create /mnt directory and mount detected ATA drives
      8 osprint("Setting up /mnt for ATA drives...\n")
      9 if CRamdiskMkdir and CRamdiskExists then
     10     -- Create /mnt directory if it doesn't exist
     11     if not CRamdiskExists("/mnt") then
     12         CRamdiskMkdir("/mnt")
     13         osprint("Created /mnt directory\n")
     14     end
     15 
     16     -- List all ATA drives and create mount point directories
     17     if ata and ata.listDrives then
     18         local drives = ata.listDrives()
     19         if drives and #drives > 0 then
     20             osprint("Found " .. #drives .. " ATA drive(s):\n")
     21             for i, drive in ipairs(drives) do
     22                 -- Create mount point name: hd0, hd1, etc.
     23                 local mountName = "hd" .. (i - 1)
     24                 local mountPath = "/mnt/" .. mountName
     25 
     26                 -- Create the mount point directory
     27                 if not CRamdiskExists(mountPath) then
     28                     CRamdiskMkdir(mountPath)
     29                 end
     30 
     31                 -- Calculate size in MB
     32                 local sizeMB = math.floor((drive.sectors * 512) / (1024 * 1024))
     33 
     34                 osprint("  " .. mountName .. ": " .. drive.model .. " (" .. sizeMB .. " MB)\n")
     35                 osprint("       Bus: " .. drive.bus .. ", Drive: " .. drive.drive .. "\n")
     36                 osprint("       Mounted at: " .. mountPath .. "\n")
     37 
     38                 -- Store drive info in a global table for later access
     39                 if not _G.mountedDrives then
     40                     _G.mountedDrives = {}
     41                 end
     42                 _G.mountedDrives[mountName] = {
     43                     bus = drive.bus,
     44                     drive = drive.drive,
     45                     model = drive.model,
     46                     sectors = drive.sectors,
     47                     sizeMB = sizeMB,
     48                     mountPath = mountPath,
     49                     -- Check if FDE formatted
     50                     encrypted = fde and fde.isFormatted and fde.isFormatted(drive.bus, drive.drive) or false
     51                 }
     52             end
     53             osprint("ATA drives mounted successfully\n")
     54 
     55             -- Format and copy test file to first drive if diskfs is available
     56             if diskfs and drives[1] then
     57                 local drive = drives[1]
     58                 osprint("Checking if hd0 is formatted...\n")
     59 
     60                 -- Check if drive is already formatted
     61                 local mounts = diskfs.getMounts()
     62                 local isFormatted = false
     63                 if mounts then
     64                     for _, m in ipairs(mounts) do
     65                         if m.bus == drive.bus and m.drive == drive.drive and m.formatted then
     66                             isFormatted = true
     67                             break
     68                         end
     69                     end
     70                 end
     71 
     72                 -- Format if not already formatted
     73                 if not isFormatted then
     74                     osprint("Formatting hd0 with DiskFS...\n")
     75                     local ok, err = diskfs.format(drive.bus, drive.drive, "LuajitOS")
     76                     if ok then
     77                         osprint("hd0 formatted successfully!\n")
     78                         isFormatted = true
     79                     else
     80                         osprint("Failed to format hd0: " .. tostring(err) .. "\n")
     81                     end
     82                 else
     83                     osprint("hd0 already formatted\n")
     84                 end
     85 
     86                 -- Copy wolf.png to disk
     87                 if isFormatted and CRamdiskOpen and CRamdiskRead and CRamdiskClose then
     88                     osprint("Copying /home/Pictures/wolf.png to /mnt/hd0/wolf.png...\n")
     89                     local srcHandle = CRamdiskOpen("/home/Pictures/wolf.png", "r")
     90                     if srcHandle then
     91                         local content = CRamdiskRead(srcHandle)
     92                         CRamdiskClose(srcHandle)
     93                         if content then
     94                             osprint("Read " .. #content .. " bytes from wolf.png\n")
     95                             local dstHandle = diskfs.open("/mnt/hd0/wolf.png", "w")
     96                             if dstHandle then
     97                                 local writeOk = diskfs.write(dstHandle, content)
     98                                 diskfs.close(dstHandle)
     99                                 if writeOk then
    100                                     osprint("Successfully copied wolf.png to /mnt/hd0/\n")
    101                                 else
    102                                     osprint("Failed to write wolf.png to disk\n")
    103                                 end
    104                             else
    105                                 osprint("Failed to open /mnt/hd0/wolf.png for writing\n")
    106                             end
    107                         else
    108                             osprint("Failed to read wolf.png content\n")
    109                         end
    110                     else
    111                         osprint("Failed to open /home/Pictures/wolf.png\n")
    112                     end
    113                 end
    114             end
    115         else
    116             osprint("No ATA drives detected\n")
    117         end
    118     else
    119         osprint("ATA driver not available, skipping drive mounting\n")
    120     end
    121 else
    122     osprint("Ramdisk functions not available, skipping /mnt setup\n")
    123 end
    124 
    125 -- Skip SafeFS setup for now to isolate the issue
    126 osprint("DEBUG: Skipping SafeFS setup temporarily\n")
    127 
    128 --[[
    129 -- Create system pseudo files in /sys/
    130 osprint("Creating system pseudo files...\n")
    131 
    132 -- Load SafeFS module
    133 osprint("Opening SafeFS module...\n")
    134 local safefs_handle = CRamdiskOpen("/os/libs/SafeFS.lua", "r")
    135 if safefs_handle then
    136     osprint("Reading SafeFS module...\n")
    137     local safefs_code = CRamdiskRead(safefs_handle)
    138     CRamdiskClose(safefs_handle)
    139 
    140     if safefs_code then
    141         osprint("Compiling SafeFS module...\n")
    142         local safefs_func, err = load(safefs_code, "/os/libs/SafeFS.lua", "t")
    143         if safefs_func then
    144             osprint("Executing SafeFS module...\n")
    145             local SafeFS = safefs_func()
    146 
    147             -- Ensure /sys/ directory exists
    148             osprint("Checking if /sys/ exists...\n")
    149             if CRamdiskExists and not CRamdiskExists("/sys") then
    150                 osprint("/sys/ doesn't exist, creating it...\n")
    151                 if CRamdiskMkdir then
    152                     CRamdiskMkdir("/sys")
    153                     osprint("/sys/ created\n")
    154                 else
    155                     osprint("ERROR: CRamdiskMkdir not available\n")
    156                 end
    157             end
    158 
    159             osprint("Creating SafeFS instance for /sys/...\n")
    160             osprint("DEBUG: About to call SafeFS.new\n")
    161             osprint("DEBUG: _G.root_fs = " .. tostring(_G.root_fs) .. "\n")
    162 
    163             -- Create a SafeFS instance with access to /sys/
    164             local sys_fs, err = SafeFS.new(_G.root_fs, {"/sys/*"}, "system")
    165 
    166             osprint("DEBUG: SafeFS.new returned\n")
    167             osprint("DEBUG: sys_fs = " .. tostring(sys_fs) .. ", err = " .. tostring(err) .. "\n")
    168 
    169             if not sys_fs then
    170                 osprint("ERROR: Failed to create SafeFS instance: " .. tostring(err) .. "\n")
    171             else
    172                 osprint("SafeFS instance created\n")
    173             end
    174 
    175             if sys_fs then
    176 
    177             -- Create /sys/date pseudo file
    178             osprint("Creating /sys/date pseudo file...\n")
    179             local date_file = sys_fs:createPseudoFile("/sys/date")
    180             osprint("createPseudoFile returned: " .. tostring(date_file) .. "\n")
    181             if date_file then
    182                 date_file.onRead = function()
    183                     local timestamp = os.time()
    184                     -- Calculate date from Unix timestamp
    185                     local days = math.floor(timestamp / 86400)
    186                     local years = math.floor(days / 365.25)
    187                     local year = 1970 + years
    188                     local remaining_days = days - math.floor(years * 365.25)
    189                     local month = math.floor(remaining_days / 30.44) + 1
    190                     local day = math.floor(remaining_days % 30.44) + 1
    191 
    192                     return string.format("%04d-%02d-%02d\n", year, month, day)
    193                 end
    194                 osprint("Created /sys/date\n")
    195             else
    196                 osprint("ERROR: Failed to create /sys/date\n")
    197             end
    198 
    199             -- Create /sys/time pseudo file
    200             local time_file = sys_fs:createPseudoFile("/sys/time")
    201             if time_file then
    202                 time_file.onRead = function()
    203                     local timestamp = os.time()
    204                     local seconds = timestamp % 60
    205                     local minutes = math.floor(timestamp / 60) % 60
    206                     local hours = math.floor(timestamp / 3600) % 24
    207 
    208                     return string.format("%02d:%02d:%02d\n", hours, minutes, seconds)
    209                 end
    210                 osprint("Created /sys/time\n")
    211             else
    212                 osprint("ERROR: Failed to create /sys/time\n")
    213             end
    214             end  -- if sys_fs then
    215         else
    216             osprint("ERROR: Failed to compile SafeFS: " .. tostring(err) .. "\n")
    217         end
    218     else
    219         osprint("ERROR: Failed to read SafeFS module\n")
    220     end
    221 else
    222     osprint("ERROR: Failed to open SafeFS module\n")
    223 end
    224 --]]
    225 
    226 osprint("DEBUG: Finished SafeFS section (commented out)\n")
    227 
    228 -- Load Dialog library
    229 osprint("Loading Dialog library...\n")
    230 local dialog_handle = CRamdiskOpen("/os/libs/Dialog.lua", "r")
    231 if dialog_handle then
    232     osprint("Reading Dialog module...\n")
    233     local dialog_code = CRamdiskRead(dialog_handle)
    234     CRamdiskClose(dialog_handle)
    235 
    236     if dialog_code then
    237         osprint("Compiling Dialog module...\n")
    238         local dialog_func, err = load(dialog_code, "/os/libs/Dialog.lua", "t")
    239         if dialog_func then
    240             osprint("Executing Dialog module...\n")
    241             _G.Dialog = dialog_func()
    242             osprint("Dialog library loaded and available as _G.Dialog\n")
    243         else
    244             osprint("ERROR: Failed to compile Dialog module: " .. tostring(err) .. "\n")
    245         end
    246     else
    247         osprint("ERROR: Failed to read Dialog module\n")
    248     end
    249 else
    250     osprint("ERROR: Failed to open Dialog module at /os/libs/Dialog.lua\n")
    251 end
    252 
    253 -- Load Hotkey library
    254 osprint("Loading Hotkey library...\n")
    255 local hotkey_handle = CRamdiskOpen("/os/libs/Hotkey.lua", "r")
    256 if hotkey_handle then
    257     osprint("Reading Hotkey module...\n")
    258     local hotkey_code = CRamdiskRead(hotkey_handle)
    259     CRamdiskClose(hotkey_handle)
    260 
    261     if hotkey_code then
    262         osprint("Compiling Hotkey module...\n")
    263         local hotkey_func, err = load(hotkey_code, "/os/libs/Hotkey.lua", "t")
    264         if hotkey_func then
    265             osprint("Executing Hotkey module...\n")
    266             local Hotkey = hotkey_func()
    267             if _G.sys then
    268                 _G.sys.hotkey = Hotkey.newInstance()
    269                 osprint("Hotkey library loaded and available as _G.sys.hotkey\n")
    270             else
    271                 osprint("ERROR: _G.sys not available, cannot initialize hotkey manager\n")
    272             end
    273         else
    274             osprint("ERROR: Failed to compile Hotkey module: " .. tostring(err) .. "\n")
    275         end
    276     else
    277         osprint("ERROR: Failed to read Hotkey module\n")
    278     end
    279 else
    280     osprint("ERROR: Failed to open Hotkey module at /os/libs/Hotkey.lua\n")
    281 end
    282 
    283 -- Register hotkeys
    284 if _G.sys and _G.sys.hotkey then
    285     osprint("Registering hotkeys...\n")
    286     osprint("DEBUG: sys.hotkey = " .. tostring(_G.sys.hotkey) .. "\n")
    287 
    288     -- Meta+R to launch shell
    289     local normalized = _G.sys.hotkey.add("meta+r", function()
    290         osprint("Hotkey triggered: Meta+R - Launching shell\n")
    291         if _G.run then
    292             local success, app = _G.run.execute("com.luajitos.shell", _G.fsRoot)
    293             osprint("DEBUG: run.execute returned success=" .. tostring(success) .. " app=" .. tostring(app) .. "\n")
    294             if success and app then
    295                 osprint("DEBUG: app.windows=" .. tostring(app.windows) .. " count=" .. tostring(app.windows and #app.windows or 0) .. "\n")
    296                 if app.windows and #app.windows > 0 then
    297                     -- Set the new shell window as active
    298                     local newWindow = app.windows[#app.windows]
    299                     osprint("DEBUG: newWindow=" .. tostring(newWindow) .. " has onInput=" .. tostring(newWindow.onInput ~= nil) .. "\n")
    300                     if _G.sys and _G.sys.setActiveWindow then
    301                         _G.sys.setActiveWindow(newWindow)
    302                         osprint("Set new shell window as active\n")
    303                         osprint("DEBUG: sys.activeWindow=" .. tostring(_G.sys.activeWindow) .. "\n")
    304                     else
    305                         osprint("ERROR: sys.setActiveWindow not available\n")
    306                     end
    307                 else
    308                     osprint("ERROR: No windows in new app\n")
    309                 end
    310             else
    311                 osprint("ERROR: Failed to launch shell\n")
    312             end
    313         else
    314             osprint("ERROR: _G.run not available\n")
    315         end
    316     end)
    317     osprint("DEBUG: Registered hotkey as: " .. tostring(normalized) .. "\n")
    318 
    319     -- Initialize global clipboard
    320     _G.clipboard = {
    321         content = nil,
    322         type = nil  -- "text" or other types
    323     }
    324 
    325     -- Ctrl+C to copy selection from active window
    326     _G.sys.hotkey.add("ctrl+c", function()
    327         local win = _G.sys.activeWindow
    328         if win and win.selection and win.selection.content and win.selection.content ~= "" then
    329             _G.clipboard.content = win.selection.content
    330             _G.clipboard.type = win.selection.type or "text"
    331             osprint("[CLIPBOARD] Copied: '" .. (_G.clipboard.content:sub(1, 50)) .. "' type=" .. tostring(_G.clipboard.type) .. "\n")
    332         end
    333     end)
    334 
    335     -- Ctrl+V to paste to active window
    336     _G.sys.hotkey.add("ctrl+v", function()
    337         local win = _G.sys.activeWindow
    338         if win and _G.clipboard.content then
    339             -- Check if there's an active selection - use onSelectionEditted
    340             local hasSelection = win.selection and win.selection.start and win.selection.finish and
    341                                  win.selection.content and win.selection.content ~= ""
    342 
    343             if hasSelection and win.onSelectionEditted then
    344                 -- Convert selection indices to x,y coordinates
    345                 local startText = win.selectableText and win.selectableText[win.selection.start.index]
    346                 local finishText = win.selectableText and win.selectableText[win.selection.finish.index]
    347 
    348                 local point1, point2
    349                 if startText then
    350                     local charWidth = 8 * (startText.scale or 1)
    351                     point1 = {
    352                         x = startText.x + (win.selection.start.pos - 1) * charWidth,
    353                         y = startText.y
    354                     }
    355                 end
    356                 if finishText then
    357                     local charWidth = 8 * (finishText.scale or 1)
    358                     point2 = {
    359                         x = finishText.x + (win.selection.finish.pos - 1) * charWidth,
    360                         y = finishText.y
    361                     }
    362                 end
    363 
    364                 if point1 and point2 then
    365                     local success, err = pcall(win.onSelectionEditted, point1, point2, _G.clipboard.content)
    366                     if not success and osprint then
    367                         osprint("[CLIPBOARD] onSelectionEditted error: " .. tostring(err) .. "\n")
    368                     end
    369                 end
    370                 -- Clear selection after paste
    371                 win.selection = nil
    372                 win.dirty = true
    373             elseif win.onPaste then
    374                 -- Try onPaste
    375                 local success, err = pcall(win.onPaste, _G.clipboard.content, _G.clipboard.type)
    376                 if not success and osprint then
    377                     osprint("[CLIPBOARD] onPaste error: " .. tostring(err) .. "\n")
    378                 end
    379             elseif win.onInput then
    380                 -- Send each character to onInput (except \n, \t, \r)
    381                 for i = 1, #_G.clipboard.content do
    382                     local char = _G.clipboard.content:sub(i, i)
    383                     if char ~= "\n" and char ~= "\t" and char ~= "\r" then
    384                         local success, err = pcall(win.onInput, char, 0, true)  -- isPasted = true
    385                         if not success and osprint then
    386                             osprint("[CLIPBOARD] onInput error: " .. tostring(err) .. "\n")
    387                         end
    388                     end
    389                 end
    390             end
    391         end
    392     end)
    393 
    394     -- Check if it was registered
    395     local hasIt = _G.sys.hotkey.has("meta+r")
    396     osprint("DEBUG: Hotkey 'meta+r' registered: " .. tostring(hasIt) .. "\n")
    397 
    398     -- List all hotkeys
    399     local allHotkeys = _G.sys.hotkey.getAll()
    400     osprint("DEBUG: Total hotkeys registered: " .. #allHotkeys .. "\n")
    401     for i, hk in ipairs(allHotkeys) do
    402         osprint("DEBUG: Hotkey " .. i .. ": " .. hk .. "\n")
    403     end
    404 
    405     osprint("Hotkeys registered\n")
    406 else
    407     osprint("ERROR: Cannot register hotkeys - sys.hotkey not available\n")
    408     osprint("DEBUG: _G.sys = " .. tostring(_G.sys) .. "\n")
    409     if _G.sys then
    410         osprint("DEBUG: _G.sys.hotkey = " .. tostring(_G.sys.hotkey) .. "\n")
    411     end
    412 end
    413 
    414 -- Helper function to parse manifest (simplified version for autostart scanning)
    415 local function parse_manifest_simple(manifest_code)
    416     if not manifest_code then return {} end
    417 
    418     local manifest = {}
    419 
    420     -- Check for autostart = true
    421     if manifest_code:match("autostart%s*=%s*true") then
    422         manifest.autostart = true
    423     end
    424 
    425     -- Check for autorun = true (runs at end of startup)
    426     if manifest_code:match("autorun%s*=%s*true") then
    427         manifest.autorun = true
    428     end
    429 
    430     -- Check for autostartPriority = <number>
    431     local priority = manifest_code:match("autostartPriority%s*=%s*(%d+)")
    432     if priority then
    433         manifest.autostartPriority = tonumber(priority)
    434     end
    435 
    436     -- Check for type (gui, cli, background, taskbar, service)
    437     local app_type = manifest_code:match('type%s*=%s*"([^"]+)"')
    438     if app_type then
    439         manifest.type = app_type
    440     end
    441 
    442     return manifest
    443 end
    444 
    445 -- Scan all apps for autostart and autorun fields
    446 osprint("Scanning apps for autostart/autorun...\n")
    447 local autostart_apps = {}
    448 local autorun_apps = {}
    449 
    450 if CRamdiskList and CRamdiskOpen and CRamdiskRead and CRamdiskClose then
    451     local apps = CRamdiskList("/apps")
    452     if apps then
    453         for _, entry in ipairs(apps) do
    454             if entry.type == "directory" or entry.type == "dir" then
    455                 local app_id = entry.name
    456                 local manifest_path = "/apps/" .. app_id .. "/manifest.lua"
    457 
    458                 local handle = CRamdiskOpen(manifest_path, "r")
    459                 if handle then
    460                     local manifest_code = CRamdiskRead(handle)
    461                     CRamdiskClose(handle)
    462 
    463                     if manifest_code then
    464                         local manifest = parse_manifest_simple(manifest_code)
    465 
    466                         -- Check for autostart (background, taskbar, early gui apps)
    467                         if manifest.autostart then
    468                             table.insert(autostart_apps, {
    469                                 id = app_id,
    470                                 priority = manifest.autostartPriority or 100,
    471                                 type = manifest.type or "gui"
    472                             })
    473                             osprint("  Found autostart app: " .. app_id .. " (priority: " .. (manifest.autostartPriority or 100) .. ", type: " .. (manifest.type or "gui") .. ")\n")
    474                         end
    475 
    476                         -- Check for autorun (services that run at end of startup)
    477                         if manifest.autorun then
    478                             table.insert(autorun_apps, {
    479                                 id = app_id,
    480                                 priority = manifest.autostartPriority or 100,
    481                                 type = manifest.type or "service"
    482                             })
    483                             osprint("  Found autorun app: " .. app_id .. " (type: " .. (manifest.type or "service") .. ")\n")
    484                         end
    485                     end
    486                 end
    487             end
    488         end
    489     end
    490 end
    491 
    492 -- Sort by priority (lower number = earlier launch)
    493 table.sort(autostart_apps, function(a, b)
    494     return a.priority < b.priority
    495 end)
    496 
    497 table.sort(autorun_apps, function(a, b)
    498     return a.priority < b.priority
    499 end)
    500 
    501 -- Launch autostart apps in priority order (background, taskbar first)
    502 osprint("Launching " .. #autostart_apps .. " autostart app(s)...\n")
    503 for _, app_info in ipairs(autostart_apps) do
    504     osprint("Launching autostart app: " .. app_info.id .. "...\n")
    505     local success, app = _G.run.execute(app_info.id, _G.fsRoot)
    506     if success and app then
    507         osprint("  " .. app_info.id .. " launched with PID: " .. tostring(app.pid) .. "\n")
    508     else
    509         osprint("  ERROR: Failed to launch " .. app_info.id .. "\n")
    510     end
    511 end
    512 
    513 -- Launch autorun apps at end of startup (services, libraries)
    514 osprint("Launching " .. #autorun_apps .. " autorun app(s)...\n")
    515 for _, app_info in ipairs(autorun_apps) do
    516     osprint("Launching autorun app: " .. app_info.id .. "...\n")
    517     local success, app = _G.run.execute(app_info.id, _G.fsRoot)
    518     if success and app then
    519         osprint("  " .. app_info.id .. " launched with PID: " .. tostring(app.pid) .. "\n")
    520     else
    521         osprint("  ERROR: Failed to launch " .. app_info.id .. "\n")
    522     end
    523 end
    524 
    525 osprint("Post-init complete\n")