dinit 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. #!/usr/bin/env slua
  2. l5 = require("l5")
  3. function string.trim(self)
  4. str, _ = string.gsub(self, '^%s*(.-)%s*$', '%1')
  5. return str
  6. end
  7. function ftype(file)
  8. S_IFMT = 0x0000f000
  9. S_IFSOCK = 0x0000c000
  10. S_IFLNK = 0x0000a000
  11. S_IFREG = 0x00008000
  12. S_IFBLK = 0x00006000
  13. S_IFDIR = 0x00004000
  14. S_IFCHR = 0x00002000
  15. S_IFIFO = 0x00001000
  16. mode, _, _ = l5.lstat3(file)
  17. if not mode then
  18. return nil
  19. elseif mode & S_IFMT == S_IFREG then
  20. return "file"
  21. elseif mode & S_IFMT == S_IFDIR then
  22. return "dir"
  23. elseif mode & S_IFMT == S_IFLNK then
  24. return "link"
  25. else
  26. return "undefined"
  27. end
  28. end
  29. -- source: https://github.com/Dynodzzo/Lua_INI_Parser/blob/master/LIP.lua
  30. function ini_load(fileName)
  31. assert(type(fileName) == 'string', 'Parameter "fileName" must be a string.');
  32. local file = assert(io.open(fileName, 'r'), 'Error loading file : ' .. fileName);
  33. local data = {};
  34. local section;
  35. for line in file:lines() do
  36. local tempSection = line:match('^%[([^%[%]]+)%]$');
  37. if(tempSection)then
  38. section = tonumber(tempSection) and tonumber(tempSection) or tempSection;
  39. data[section] = data[section] or {};
  40. end
  41. local param, value = line:match('^([%w|_]+)%s-=%s-(.+)$');
  42. if(param and value ~= nil)then
  43. param = param:trim()
  44. value = value:trim()
  45. if(tonumber(value))then
  46. value = tonumber(value);
  47. elseif(value == 'true')then
  48. value = true;
  49. elseif(value == 'false')then
  50. value = false;
  51. end
  52. if(tonumber(param))then
  53. param = tonumber(param);
  54. end
  55. if fileName:match("masa.ini") and (section == "config_files" or section == "autostart") then
  56. table.insert(data[section],param .."@@"..value)
  57. else
  58. data[section][param] = value;
  59. end
  60. end
  61. end
  62. file:close();
  63. return data;
  64. end
  65. function ini_save(fileName, data)
  66. assert(type(fileName) == 'string', 'Parameter "fileName" must be a string.');
  67. assert(type(data) == 'table', 'Parameter "data" must be a table.');
  68. local file = assert(io.open(fileName, 'w+b'), 'Error loading file :' .. fileName);
  69. local contents = '';
  70. for section, param in pairs(data) do
  71. contents = contents .. ('[%s]\n'):format(section);
  72. for key, value in pairs(param) do
  73. contents = contents .. ('%s=%s\n'):format(key, tostring(value));
  74. end
  75. contents = contents .. '\n';
  76. end
  77. file:write(contents);
  78. file:close();
  79. end
  80. -- tanımlar
  81. config = ini_load("/home/" .. os.getenv("USER") .. "/.config/masa.ini")
  82. cmd = "%s > %s 2>&1 &"
  83. param = arg[1]
  84. wm_param = arg[2]
  85. --- işlevler
  86. function initialize()
  87. uid = l5.geteuid()
  88. xdg_rdir = "/tmp/runtime-"..tostring(uid)
  89. l5.setenv("XDG_RUNTIME_DIR",xdg_rdir)
  90. l5.mkdir(xdg_rdir,448) --700
  91. l5.chown(xdg_rdir, uid, uid)
  92. --l5.chmod(xdg_rdir,509) -- 775
  93. end
  94. function set_keyboard()
  95. if config.keyboard then
  96. layout="tr"
  97. variant=""
  98. _variant=""
  99. rules = ""
  100. model = ""
  101. for k,v in pairs(config.keyboard) do
  102. if k == "layout" and v ~= "" then
  103. layout=v
  104. end
  105. if k == "variant" and v ~= "" then
  106. _variant="("..v..")"
  107. variant = v
  108. end
  109. if k == "rules" then
  110. rules = v
  111. end
  112. if k == "model" then
  113. model = v
  114. end
  115. end
  116. l5.setenv("XKB_DEFAULT_LAYOUT",layout.._variant)
  117. wayfire_cf = "/home/" .. os.getenv("USER") .. "/.config/wayfire.ini"
  118. config_w = ini_load(wayfire_cf)
  119. if config_w then
  120. config_w.input["xkb_layout"] = layout
  121. config_w.input["xkb_model"] = model
  122. config_w.input["xkb_rules"] = rules
  123. config_w.input["xkb_variant"] = variant
  124. ini_save(wayfire_cf, config_w)
  125. end
  126. end
  127. end
  128. function set_output()
  129. for key,_ in pairs(config) do
  130. local handle = assert(io.popen("wlr-randr", 'r'))
  131. local outputs = assert(handle:read('*a'))
  132. handle:close()
  133. if key:find("output:") then
  134. randr_p = ""
  135. _, p=key:find(":")
  136. device=key:sub(p+1,key:len())
  137. if outputs:match(device:gsub("%-","%%-")) then
  138. randr_p = randr_p .. " --output " .. device
  139. for k,v in pairs(config[key]) do
  140. if k == "mode" then
  141. if v == "off" then
  142. randr_p = randr_p .. " --off"
  143. break
  144. end
  145. randr_p = randr_p .. " --mode " .. v .. " Hz"
  146. end
  147. if k == "position" then
  148. randr_p = randr_p .. " --pos " .. v
  149. end
  150. if k == "transform" then
  151. randr_p = randr_p .. " --transform " .. v
  152. end
  153. if k == "scale" then
  154. randr_p = randr_p .. " --scale " .. v
  155. end
  156. end
  157. -- wlr-randr uygula
  158. os.execute("wlr-randr" .. randr_p)
  159. end
  160. end
  161. end
  162. end
  163. function set_locale()
  164. if config.localization then
  165. for k,v in pairs(config.localization) do
  166. if k == "language" then
  167. l5.setenv("LC_ALL",v)
  168. l5.setenv("LANG",v)
  169. _,p=v:find("_")
  170. v2=v:sub(0,p-1)
  171. l5.setenv("LANGUAGE",v2)
  172. print("e",k,v)
  173. print("e",k,v2)
  174. end
  175. end
  176. end
  177. end
  178. function set_userdirs()
  179. dirs={
  180. music = "XDG_MUSIC_DIR",
  181. document = "XDG_DOCUMENTS_DIR",
  182. download = "XDG_DOWNLOAD_DIR",
  183. desktop = "XDG_DESKTOP_DIR",
  184. picture = "XDG_PICTURES_DIR",
  185. public = "XDG_PUBLICSHARE_DIR",
  186. video = "XDG_VIDEOS_DIR",
  187. template = "XDG_TEMPLATES_DIR",
  188. }
  189. if config.user_dirs then
  190. userfile= io.open(os.getenv("HOME").."/.config/user-dirs.dirs", 'w')
  191. for k,v in pairs(config.user_dirs) do
  192. if dirs[k] then
  193. xdg_dir = os.getenv("HOME").."/"..v
  194. l5.mkdir(xdg_dir,509) --775
  195. l5.setenv(dirs[k],xdg_dir)
  196. userfile:write(dirs[k]..("=\"$HOME/%s\"\n"):format(v))
  197. print("e",k,xdg_dir)
  198. end
  199. end
  200. userfile:close()
  201. end
  202. end
  203. function set_config_files()
  204. if config.config_files then
  205. for _,value in pairs(config.config_files) do
  206. _, p1=value:find("@@")
  207. k=value:sub(0,p1-2)
  208. line=value:sub(p1+1,value:len())
  209. if k == "script" then
  210. os.execute(line)
  211. else
  212. _, p=line:find(":")
  213. source=line:sub(0,p-1)
  214. target=line:sub(p+1,line:len())
  215. target=os.getenv("HOME").."/"..target
  216. cmd_cp_file = "install -Dm644 %s %s"
  217. cmd_cp_dir = "cp -r %s %s"
  218. if ftype(source) and not ftype(target) then
  219. if ftype(source) == "dir" then
  220. cmd = cmd_cp_dir:format(source,target)
  221. os.execute(cmd)
  222. elseif ftype(source) == "file" then
  223. cmd = cmd_cp_file:format(source,target)
  224. os.execute(cmd)
  225. else
  226. print("error:",source,ftype(source))
  227. end
  228. end
  229. --print(source,ftype(source), target, ftype(target))
  230. end
  231. end
  232. end
  233. end
  234. function set_milis_apps()
  235. desktop_list="/usr/milis/ayarlar/uygulama/desktop.list"
  236. user_app_dir=os.getenv("HOME").."/.local/share/applications/"
  237. user_cache_dir=os.getenv("HOME").."/.cache"
  238. os.execute(("mkdir -p %s"):format(user_app_dir))
  239. os.execute(("mkdir -p %s"):format(user_cache_dir))
  240. if ftype(desktop_list) == "file" then
  241. file = io.open(desktop_list,"r")
  242. for app in file:lines() do
  243. if ftype(app) == "file" then
  244. os.execute(("cp -vf %s %s"):format(app,user_app_dir))
  245. end
  246. if not ftype(app) then
  247. os.execute(("rm -vf %s/`basename %s`"):format(user_app_dir,app))
  248. end
  249. end
  250. end
  251. end
  252. function set_environment()
  253. -- genel ortam değişkenleri
  254. if config.environment then
  255. for k,v in pairs(config.environment) do
  256. l5.setenv(k,v)
  257. print("e",k,v)
  258. end
  259. end
  260. -- gömülü yamalar ayara aktarılacak todo!
  261. -- sanal makine ve bazı donanımlarda fare gözükmeme sorunu
  262. -- [ -f /sys/class/dmi/id/board_name ] && [[ $(cat /sys/class/dmi/id/board_name) =~ VirtualBox|PAV10 ]] && export WLR_NO_HARDWARE_CURSORS=1
  263. board_sys_f = "/sys/class/dmi/id/board_name"
  264. if ftype(board_sys_f) == "file" then
  265. file = io.open(board_sys_f,"r")
  266. for line in file:lines() do
  267. if line:match("VirtualBox") or line:match("PAV10") then
  268. l5.setenv("WLR_NO_HARDWARE_CURSORS",1)
  269. end
  270. end
  271. end
  272. end
  273. function do_configuration()
  274. set_environment()
  275. set_locale()
  276. set_config_files()
  277. set_keyboard()
  278. set_userdirs()
  279. set_milis_apps()
  280. end
  281. function autostart()
  282. if config.autostart then
  283. for _,value in pairs(config.autostart) do
  284. _, p1=value:find("@@")
  285. k=value:sub(0,p1-2)
  286. v=value:sub(p1+1,value:len())
  287. if config.log[k] then
  288. _cmd = cmd:format(v, config.log[k])
  289. else
  290. _cmd = cmd:format(v, "/dev/null")
  291. end
  292. print("a",_cmd)
  293. os.execute(_cmd)
  294. end
  295. end
  296. end
  297. function session()
  298. if config.session then
  299. for k,v in pairs(config.session) do
  300. _cmd = cmd:format(v, "/dev/null")
  301. print("s",_cmd)
  302. os.execute(_cmd)
  303. end
  304. end
  305. end
  306. function start_wm()
  307. --os.execute("export")
  308. --os.execute("sleep 18")
  309. _cmd = "dbus-run-session %s"
  310. masa = ""
  311. for k,v in pairs(config.desktop) do
  312. masa = v
  313. end
  314. if wm_param == nil then
  315. print("d",_cmd:format(masa))
  316. os.execute(_cmd:format(masa))
  317. else
  318. print("d",_cmd:format(wm_param))
  319. os.execute(_cmd:format(wm_param))
  320. end
  321. end
  322. ------ işlem
  323. if param == "--init" then
  324. initialize()
  325. end
  326. if param == "--wm" then
  327. initialize()
  328. do_configuration()
  329. start_wm()
  330. end
  331. if param == "--start" then
  332. set_output()
  333. autostart()
  334. os.execute("sleep 0.5")
  335. session()
  336. end
  337. if param == "--get" then
  338. getp = arg[2]
  339. _, p=getp:find("%.")
  340. secp=getp:sub(0,p-1)
  341. keyp=getp:sub(p+1,getp:len())
  342. if config[secp] then
  343. if config[secp][keyp] then
  344. print(config[secp][keyp])
  345. else
  346. print("echo")
  347. end
  348. end
  349. end