123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105(* The stub works on the native top-level window rather than on a [Webview.t]:
that is what keeps icon_stubs.cpp free of any reference to the webview C
API. The handle is resolved here with [Webview.get_window], which is part of
the core library's public interface — so this library needs nothing from
owebview beyond what any other user of it can reach. *)externalraw_set_app_icon:nativeint->string->unit="ocaml_webview_set_app_icon"externalset_app_id:string->unit="ocaml_webview_set_app_id"letset_app_iconwpath=raw_set_app_icon(Webview.get_windoww)path(* ---- the desktop entry --------------------------------------------------
[set_app_icon] only sets the X11 _NET_WM_ICON property, which native
Wayland does not have: there, the shell resolves an application's icon from
an installed .desktop file matched by the process's app id. So on a Wayland
desktop the icon never appears until such a file exists, which is what
[install_desktop_entry] writes. *)(* $XDG_DATA_HOME/applications, falling back to the specified default. *)letapplications_dir()=letdata_home=matchSys.getenv_opt"XDG_DATA_HOME"with|Somedirwhendir<>""->dir|_->(matchSys.getenv_opt"HOME"with|Somehome->Filename.concathome".local/share"|None->failwith"install_desktop_entry: neither XDG_DATA_HOME nor HOME is set")inFilename.concatdata_home"applications"(* [Sys.mkdir] creates one level, so walk the path. *)letrecmkdir_pdir=ifnot(Sys.file_existsdir)then(letparent=Filename.dirnamedirinifparent<>dirthenmkdir_pparent;trySys.mkdirdir0o755withSys_error_whenSys.file_existsdir->())(* A newline in a value would start a new key, so it is refused rather than
written out: the entry format has no escape for it. *)letcheck_valuefieldv=ifString.exists(func->c='\n'||c='\r')vthenfailwith(Printf.sprintf"install_desktop_entry: %s must not contain a newline"field)(* Refresh the desktop database, if the tool is there. Some desktops cache the
applications directory and would not notice the new file otherwise. Failure
is not reported: the entry is written either way, and the cache catches up
on the next login. *)letrefresh_databasedir=ifSys.command"command -v update-desktop-database >/dev/null 2>&1"=0thenignore(Sys.command(Printf.sprintf"update-desktop-database %s >/dev/null 2>&1"(Filename.quotedir)))letinstall_desktop_entry?name?comment?categories?exec~app_id~icon()=(* This library links neither Unix nor threads, so there is no [uname] to
call; /proc/version is the cheapest marker of a Linux kernel. *)ifnot(Sys.file_exists"/proc/version")thenNoneelsebeginletexec=matchexecwithSomee->e|None->Sys.executable_nameinletname=matchnamewithSomen->n|None->app_idincheck_value"app_id"app_id;check_value"icon"icon;check_value"exec"exec;check_value"name"name;Option.iter(check_value"comment")comment;ifnot(Sys.file_existsicon)thenfailwith(Printf.sprintf"install_desktop_entry: no such icon file: %s"icon);letbuf=Buffer.create256inletaddkv=Buffer.add_stringbuf(k^"="^v^"\n")inBuffer.add_stringbuf"[Desktop Entry]\n";add"Type""Application";add"Name"name;Option.iter(add"Comment")comment;add"Exec"exec;add"Icon"icon;(* What the shell matches the running process against. *)add"StartupWMClass"app_id;(* This describes a windowed application, never a console one. *)add"Terminal""false";Option.iter(funcs->add"Categories"(String.concat";"cs^";"))categories;letdir=applications_dir()in(* The file's own name need not match [app_id], since StartupWMClass says
which process it describes; it is namespaced to avoid colliding with
anything else in a shared directory. *)letpath=Filename.concatdir("owebview-"^app_id^".desktop")inmkdir_pdir;letoc=open_outpathinFun.protect~finally:(fun()->close_out_noerroc)(fun()->output_stringoc(Buffer.contentsbuf));refresh_databasedir;Somepathend