dinit 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. l5.setenv("XDG_DATA_DIRS","/usr/local/share:/usr/share")
  94. l5.setenv("XDG_CONFIG_DIRS","/etc/xdg")
  95. end
  96. function set_keyboard()
  97. if config.keyboard then
  98. layout="tr"
  99. variant=""
  100. _variant=""
  101. rules = ""
  102. model = ""
  103. for k,v in pairs(config.keyboard) do
  104. if k == "layout" and v ~= "" then
  105. layout=v
  106. end
  107. if k == "variant" and v ~= "" then
  108. _variant="("..v..")"
  109. variant = v
  110. end
  111. if k == "rules" then
  112. rules = v
  113. end
  114. if k == "model" then
  115. model = v
  116. end
  117. end
  118. l5.setenv("XKB_DEFAULT_LAYOUT",layout.._variant)
  119. wayfire_cf = "/home/" .. os.getenv("USER") .. "/.config/wayfire.ini"
  120. config_w = ini_load(wayfire_cf)
  121. if config_w then
  122. config_w.input["xkb_layout"] = layout
  123. config_w.input["xkb_model"] = model
  124. config_w.input["xkb_rules"] = rules
  125. config_w.input["xkb_variant"] = variant
  126. ini_save(wayfire_cf, config_w)
  127. end
  128. end
  129. end
  130. function set_output()
  131. for key,_ in pairs(config) do
  132. local handle = assert(io.popen("wlr-randr", 'r'))
  133. local outputs = assert(handle:read('*a'))
  134. handle:close()
  135. if key:find("output:") then
  136. randr_p = ""
  137. _, p=key:find(":")
  138. device=key:sub(p+1,key:len())
  139. if outputs:match(device:gsub("%-","%%-")) then
  140. randr_p = randr_p .. " --output " .. device
  141. for k,v in pairs(config[key]) do
  142. if k == "mode" then
  143. if v == "off" then
  144. randr_p = randr_p .. " --off"
  145. break
  146. end
  147. randr_p = randr_p .. " --mode " .. v .. " Hz"
  148. end
  149. if k == "position" then
  150. randr_p = randr_p .. " --pos " .. v
  151. end
  152. if k == "transform" then
  153. randr_p = randr_p .. " --transform " .. v
  154. end
  155. if k == "scale" then
  156. randr_p = randr_p .. " --scale " .. v
  157. end
  158. end
  159. -- wlr-randr uygula
  160. os.execute("wlr-randr" .. randr_p)
  161. end
  162. end
  163. end
  164. end
  165. function set_locale()
  166. if config.localization then
  167. for k,v in pairs(config.localization) do
  168. if k == "language" then
  169. l5.setenv("LC_ALL",v)
  170. l5.setenv("LANG",v)
  171. _,p=v:find("_")
  172. v2=v:sub(0,p-1)
  173. l5.setenv("LANGUAGE",v2)
  174. print("e",k,v)
  175. print("e",k,v2)
  176. end
  177. end
  178. end
  179. end
  180. function set_userdirs()
  181. dirs={
  182. music = "XDG_MUSIC_DIR",
  183. document = "XDG_DOCUMENTS_DIR",
  184. download = "XDG_DOWNLOAD_DIR",
  185. desktop = "XDG_DESKTOP_DIR",
  186. picture = "XDG_PICTURES_DIR",
  187. public = "XDG_PUBLICSHARE_DIR",
  188. video = "XDG_VIDEOS_DIR",
  189. template = "XDG_TEMPLATES_DIR",
  190. }
  191. if config.user_dirs then
  192. userfile= io.open(os.getenv("HOME").."/.config/user-dirs.dirs", 'w')
  193. for k,v in pairs(config.user_dirs) do
  194. if dirs[k] then
  195. xdg_dir = os.getenv("HOME").."/"..v
  196. l5.mkdir(xdg_dir,509) --775
  197. l5.setenv(dirs[k],xdg_dir)
  198. userfile:write(dirs[k]..("=\"$HOME/%s\"\n"):format(v))
  199. print("e",k,xdg_dir)
  200. end
  201. end
  202. userfile:close()
  203. end
  204. end
  205. function set_config_files()
  206. if config.config_files then
  207. for _,value in pairs(config.config_files) do
  208. _, p1=value:find("@@")
  209. k=value:sub(0,p1-2)
  210. line=value:sub(p1+1,value:len())
  211. if k == "script" then
  212. os.execute(line)
  213. else
  214. _, p=line:find(":")
  215. source=line:sub(0,p-1)
  216. target=line:sub(p+1,line:len())
  217. target=os.getenv("HOME").."/"..target
  218. cmd_cp_file = "install -Dm644 %s %s"
  219. cmd_cp_dir = "cp -r %s %s"
  220. if ftype(source) and not ftype(target) then
  221. if ftype(source) == "dir" then
  222. cmd = cmd_cp_dir:format(source,target)
  223. os.execute(cmd)
  224. elseif ftype(source) == "file" then
  225. cmd = cmd_cp_file:format(source,target)
  226. os.execute(cmd)
  227. else
  228. print("error:",source,ftype(source))
  229. end
  230. end
  231. --print(source,ftype(source), target, ftype(target))
  232. end
  233. end
  234. end
  235. end
  236. function set_milis_apps()
  237. desktop_list="/usr/milis/ayarlar/uygulama/desktop.list"
  238. user_app_dir=os.getenv("HOME").."/.local/share/applications/"
  239. user_cache_dir=os.getenv("HOME").."/.cache"
  240. os.execute(("mkdir -p %s"):format(user_app_dir))
  241. os.execute(("mkdir -p %s"):format(user_cache_dir))
  242. if ftype(desktop_list) == "file" then
  243. file = io.open(desktop_list,"r")
  244. for app in file:lines() do
  245. if ftype(app) == "file" then
  246. os.execute(("cp -vf %s %s"):format(app,user_app_dir))
  247. end
  248. if not ftype(app) then
  249. os.execute(("rm -vf %s/`basename %s`"):format(user_app_dir,app))
  250. end
  251. end
  252. end
  253. end
  254. function set_environment()
  255. -- genel ortam değişkenleri
  256. if config.environment then
  257. for k,v in pairs(config.environment) do
  258. l5.setenv(k,v)
  259. print("e",k,v)
  260. end
  261. end
  262. -- gömülü yamalar ayara aktarılacak todo!
  263. -- sanal makine ve bazı donanımlarda fare gözükmeme sorunu
  264. -- [ -f /sys/class/dmi/id/board_name ] && [[ $(cat /sys/class/dmi/id/board_name) =~ VirtualBox|PAV10 ]] && export WLR_NO_HARDWARE_CURSORS=1
  265. board_sys_f = "/sys/class/dmi/id/board_name"
  266. if ftype(board_sys_f) == "file" then
  267. file = io.open(board_sys_f,"r")
  268. for line in file:lines() do
  269. if line:match("VirtualBox") or line:match("PAV10") then
  270. l5.setenv("WLR_NO_HARDWARE_CURSORS",1)
  271. end
  272. end
  273. end
  274. end
  275. function do_configuration()
  276. set_environment()
  277. set_locale()
  278. set_config_files()
  279. set_keyboard()
  280. set_userdirs()
  281. set_milis_apps()
  282. end
  283. function autostart()
  284. if config.autostart then
  285. for _,value in pairs(config.autostart) do
  286. _, p1=value:find("@@")
  287. k=value:sub(0,p1-2)
  288. v=value:sub(p1+1,value:len())
  289. if config.log[k] then
  290. _cmd = cmd:format(v, config.log[k])
  291. else
  292. _cmd = cmd:format(v, "/dev/null")
  293. end
  294. print("a",_cmd)
  295. os.execute(_cmd)
  296. end
  297. end
  298. end
  299. function session()
  300. if config.session then
  301. for k,v in pairs(config.session) do
  302. _cmd = cmd:format(v, "/dev/null")
  303. print("s",_cmd)
  304. os.execute(_cmd)
  305. end
  306. end
  307. end
  308. function start_wm()
  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. masa= wm_param
  316. end
  317. -- Exec değerini al ve dbus-run a ver
  318. -- masa wayland-sessions altındaki dosya ismi olmalı
  319. local exec_str = ""
  320. file = io.open("/usr/share/wayland-sessions/".. masa .. ".desktop","r")
  321. for line in file:lines() do
  322. if line:match("Exec=") then
  323. exec_str,_ = line:match("^Exec=(.*)")
  324. break
  325. end
  326. end
  327. print("d",_cmd:format(masa))
  328. os.execute(_cmd:format(exec_str))
  329. end
  330. ------ işlem
  331. if param == "--init" then
  332. initialize()
  333. end
  334. if param == "--wm" then
  335. initialize()
  336. do_configuration()
  337. start_wm()
  338. end
  339. if param == "--start" then
  340. set_output()
  341. autostart()
  342. os.execute("sleep 0.5")
  343. session()
  344. end
  345. if param == "--get" then
  346. getp = arg[2]
  347. _, p=getp:find("%.")
  348. secp=getp:sub(0,p-1)
  349. keyp=getp:sub(p+1,getp:len())
  350. if config[secp] then
  351. if config[secp][keyp] then
  352. print(config[secp][keyp])
  353. else
  354. print("echo")
  355. end
  356. end
  357. end