dinit 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 section == "config_files" 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. for k,v in pairs(config.keyboard) do
  97. if k == "layout" then
  98. l5.setenv("XKB_DEFAULT_LAYOUT",v)
  99. end
  100. end
  101. end
  102. end
  103. function set_locale()
  104. if config.localization then
  105. for k,v in pairs(config.localization) do
  106. if k == "language" then
  107. l5.setenv("LC_ALL",v)
  108. l5.setenv("LANG",v)
  109. _,p=v:find("_")
  110. v2=v:sub(0,p-1)
  111. l5.setenv("LANGUAGE",v2)
  112. print("e",k,v)
  113. print("e",k,v2)
  114. end
  115. end
  116. end
  117. end
  118. function set_userdirs()
  119. dirs={
  120. music = "XDG_MUSIC_DIR",
  121. document = "XDG_DOCUMENTS_DIR",
  122. download = "XDG_MUSIC_DIR",
  123. desktop = "XDG_DESKTOP_DIR",
  124. picture = "XDG_PICTURES_DIR",
  125. public = "XDG_PUBLICSHARE_DIR",
  126. video = "XDG_VIDEOS_DIR",
  127. template = "XDG_TEMPLATES_DIR",
  128. }
  129. if config.user_dirs then
  130. for k,v in pairs(config.user_dirs) do
  131. if dirs[k] then
  132. xdg_dir = os.getenv("HOME").."/"..v
  133. l5.mkdir(xdg_dir,509) --775
  134. l5.setenv(dirs[k],xdg_dir)
  135. print("e",k,xdg_dir)
  136. end
  137. end
  138. end
  139. end
  140. function set_config_files()
  141. if config.config_files then
  142. for _,value in pairs(config.config_files) do
  143. _, p1=value:find("@@")
  144. k=value:sub(0,p1-2)
  145. line=value:sub(p1+1,value:len())
  146. if k == "script" then
  147. os.execute(line)
  148. else
  149. _, p=line:find(":")
  150. source=line:sub(0,p-1)
  151. target=line:sub(p+1,line:len())
  152. target=os.getenv("HOME").."/"..target
  153. cmd_cp_file = "install -Dm644 %s %s"
  154. cmd_cp_dir = "cp -r %s %s"
  155. if ftype(source) and not ftype(target) then
  156. if ftype(source) == "dir" then
  157. cmd = cmd_cp_dir:format(source,target)
  158. os.execute(cmd)
  159. elseif ftype(source) == "file" then
  160. cmd = cmd_cp_file:format(source,target)
  161. os.execute(cmd)
  162. else
  163. print("error:",source,ftype(source))
  164. end
  165. end
  166. --print(source,ftype(source), target, ftype(target))
  167. end
  168. end
  169. end
  170. end
  171. function set_milis_apps()
  172. desktop_list="/usr/milis/ayarlar/uygulama/desktop.list"
  173. user_app_dir=os.getenv("HOME").."/.local/share/applications/"
  174. os.execute(("mkdir -p %s"):format(user_app_dir))
  175. if ftype(desktop_list) == "file" then
  176. file = io.open(desktop_list,"r")
  177. for app in file:lines() do
  178. if ftype(app) == "file" then
  179. os.execute(("cp -vf %s %s"):format(app,user_app_dir))
  180. end
  181. if not ftype(app) then
  182. os.execute(("rm -vf %s/`basename %s`"):format(user_app_dir,app))
  183. end
  184. end
  185. end
  186. end
  187. function set_environment()
  188. -- genel ortam değişkenleri
  189. if config.environment then
  190. for k,v in pairs(config.environment) do
  191. l5.setenv(k,v)
  192. print("e",k,v)
  193. end
  194. end
  195. -- gömülü yamalar ayara aktarılacak todo!
  196. -- sanal makine ve bazı donanımlarda fare gözükmeme sorunu
  197. -- [ -f /sys/class/dmi/id/board_name ] && [[ $(cat /sys/class/dmi/id/board_name) =~ VirtualBox|PAV10 ]] && export WLR_NO_HARDWARE_CURSORS=1
  198. board_sys_f = "/sys/class/dmi/id/board_name"
  199. if ftype(board_sys_f) == "file" then
  200. file = io.open(board_sys_f,"r")
  201. for line in file:lines() do
  202. if line:match("VirtualBox") or line:match("PAV10") then
  203. l5.setenv("WLR_NO_HARDWARE_CURSORS",1)
  204. end
  205. end
  206. end
  207. end
  208. function do_configuration()
  209. set_environment()
  210. set_locale()
  211. set_config_files()
  212. set_keyboard()
  213. set_userdirs()
  214. set_milis_apps()
  215. end
  216. function autostart()
  217. if config.autostart then
  218. for k,v in pairs(config.autostart) do
  219. if config.log[k] then
  220. _cmd = cmd:format(v, config.log[k])
  221. else
  222. _cmd = cmd:format(v, "/dev/null")
  223. end
  224. print("a",_cmd)
  225. os.execute(_cmd)
  226. end
  227. end
  228. end
  229. function session()
  230. if config.session then
  231. for k,v in pairs(config.session) do
  232. _cmd = cmd:format(v, "/dev/null")
  233. print("s",_cmd)
  234. os.execute(_cmd)
  235. end
  236. end
  237. end
  238. function start_wm()
  239. --os.execute("export")
  240. --os.execute("sleep 18")
  241. _cmd = "dbus-run-session %s"
  242. masa = ""
  243. for k,v in pairs(config.desktop) do
  244. masa = v
  245. end
  246. if wm_param == nil then
  247. print("d",_cmd:format(masa))
  248. os.execute(_cmd:format(masa))
  249. else
  250. print("d",_cmd:format(wm_param))
  251. os.execute(_cmd:format(wm_param))
  252. end
  253. end
  254. ------ işlem
  255. if param == "--init" then
  256. initialize()
  257. end
  258. if param == "--wm" then
  259. initialize()
  260. do_configuration()
  261. start_wm()
  262. end
  263. if param == "--start" then
  264. autostart()
  265. os.execute("sleep 1")
  266. session()
  267. end
  268. if param == "--get" then
  269. getp = arg[2]
  270. _, p=getp:find("%.")
  271. secp=getp:sub(0,p-1)
  272. keyp=getp:sub(p+1,getp:len())
  273. if config[secp] then
  274. if config[secp][keyp] then
  275. print(config[secp][keyp])
  276. else
  277. print("echo")
  278. end
  279. end
  280. end