1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Copyright (C) 2021  Tassilo Horn <tsdh@gnu.org>
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along with
// this program.  If not, see <https://www.gnu.org/licenses/>.

//! Utility functions including selection between choices using a menu program.

use crate::config as cfg;
use lazy_static::lazy_static;
use std::collections::HashMap;
use std::io::{BufRead, Write};
use std::path as p;
use std::process as proc;

pub fn get_swayr_socket_path() -> String {
    // We prefer checking the env variable instead of
    // directories::BaseDirs::new().unwrap().runtime_dir().unwrap() because
    // directories errors if the XDG_RUNTIME_DIR isn't set or set to a relative
    // path which actually works fine for sway & swayr.
    let xdg_runtime_dir = std::env::var("XDG_RUNTIME_DIR");
    let wayland_display = std::env::var("WAYLAND_DISPLAY");
    format!(
        "{}/swayr-{}.sock",
        match xdg_runtime_dir {
            Ok(val) => val,
            Err(_e) => {
                eprintln!("Couldn't get XDG_RUNTIME_DIR!");
                String::from("/tmp")
            }
        },
        match wayland_display {
            Ok(val) => val,
            Err(_e) => {
                eprintln!("Couldn't get WAYLAND_DISPLAY!");
                String::from("unknown")
            }
        }
    )
}

fn desktop_entry_folders() -> Vec<Box<p::Path>> {
    let mut dirs: Vec<Box<p::Path>> = vec![];

    // XDG_DATA_HOME/applications
    if let Some(dd) = directories::BaseDirs::new() {
        dirs.push(dd.data_local_dir().to_path_buf().into_boxed_path());
    }

    let default_dirs =
        ["/usr/local/share/applications/", "/usr/share/applications/"];
    for dir in default_dirs {
        dirs.push(p::Path::new(dir).to_path_buf().into_boxed_path());
    }

    if let Ok(xdg_data_dirs) = std::env::var("XDG_DATA_DIRS") {
        for mut dir in std::env::split_paths(&xdg_data_dirs) {
            dir.push("applications/");
            dirs.push(dir.into_boxed_path());
        }
    }

    dirs.sort();
    dirs.dedup();

    dirs
}

fn desktop_entries() -> Vec<Box<p::Path>> {
    let mut entries = vec![];
    for dir in desktop_entry_folders() {
        if let Ok(readdir) = dir.read_dir() {
            for entry in readdir.flatten() {
                let path = entry.path();
                if path.is_file()
                    && path.extension().map(|ext| ext == "desktop")
                        == Some(true)
                {
                    entries.push(path.to_path_buf().into_boxed_path());
                }
            }
        }
    }
    entries
}

fn find_icon(icon_name: &str, icon_dirs: &[String]) -> Option<Box<p::Path>> {
    let p = p::Path::new(icon_name);
    if p.is_file() {
        println!("(1) Icon name '{}' -> {}", icon_name, p.display());
        return Some(p.to_path_buf().into_boxed_path());
    }

    for dir in icon_dirs {
        for ext in &["png", "svg"] {
            let mut pb = p::PathBuf::from(dir);
            pb.push(icon_name.to_owned() + "." + ext);
            let icon_file = pb.as_path();
            if icon_file.is_file() {
                println!(
                    "(2) Icon name '{}' -> {}",
                    icon_name,
                    icon_file.display()
                );
                return Some(icon_file.to_path_buf().into_boxed_path());
            }
        }
    }

    println!("(3) No icon for name {}", icon_name);
    None
}

lazy_static! {
    static ref WM_CLASS_OR_ICON_RX: regex::Regex =
        regex::Regex::new(r"(StartupWMClass|Icon)=(.+)").unwrap();
    static ref REV_DOMAIN_NAME_RX: regex::Regex =
        regex::Regex::new(r"^(?:[a-zA-Z0-9-]+\.)+([a-zA-Z0-9-]+)$").unwrap();
}

fn get_app_id_to_icon_map(
    icon_dirs: &[String],
) -> HashMap<String, Box<p::Path>> {
    let mut map: HashMap<String, Box<p::Path>> = HashMap::new();

    for e in desktop_entries() {
        if let Ok(f) = std::fs::File::open(&e) {
            let buf = std::io::BufReader::new(f);
            let mut wm_class: Option<String> = None;
            let mut icon: Option<Box<p::Path>> = None;

            // Get App-Id and Icon from desktop file.
            for line in buf.lines() {
                if wm_class.is_some() && icon.is_some() {
                    break;
                }
                if let Ok(line) = line {
                    if let Some(cap) = WM_CLASS_OR_ICON_RX.captures(&line) {
                        if "StartupWMClass" == cap.get(1).unwrap().as_str() {
                            wm_class.replace(
                                cap.get(2).unwrap().as_str().to_string(),
                            );
                        } else if let Some(icon_file) =
                            find_icon(cap.get(2).unwrap().as_str(), icon_dirs)
                        {
                            icon.replace(icon_file);
                        }
                    }
                }
            }

            if let Some(icon) = icon {
                // Sometimes the StartupWMClass is the app_id, e.g. FF Dev
                // Edition has StartupWMClass firefoxdeveloperedition although
                // the desktop file is named firefox-developer-edition.
                if let Some(wm_class) = wm_class {
                    map.insert(wm_class, icon.clone());
                }

                // Some apps have a reverse domain name desktop file, e.g.,
                // org.gnome.eog.desktop but reports as just eog.
                let desktop_file_name = String::from(
                    e.with_extension("").file_name().unwrap().to_string_lossy(),
                );
                if let Some(caps) =
                    REV_DOMAIN_NAME_RX.captures(&desktop_file_name)
                {
                    map.insert(
                        caps.get(1).unwrap().as_str().to_string(),
                        icon.clone(),
                    );
                }

                // The usual case is that the app with foo.desktop also has the
                // app_id foo.
                map.insert(desktop_file_name.clone(), icon);
            }
        }
    }

    println!(
        "Desktop entries to icon files ({} entries):\n{:#?}",
        map.len(),
        map
    );
    map
}

lazy_static! {
    static ref APP_ID_TO_ICON_MAP: std::sync::Mutex<Option<HashMap<String, Box<p::Path>>>> =
        std::sync::Mutex::new(None);
}

pub fn get_icon(app_id: &str, icon_dirs: &[String]) -> Option<Box<p::Path>> {
    let mut opt = APP_ID_TO_ICON_MAP.lock().unwrap();

    if opt.is_none() {
        opt.replace(get_app_id_to_icon_map(icon_dirs));
    }

    opt.as_ref().unwrap().get(app_id).map(|i| i.to_owned())
}

#[test]
fn test_icon_stuff() {
    let icon_dirs = vec![
        String::from("/usr/share/icons/hicolor/scalable/apps"),
        String::from("/usr/share/icons/hicolor/48x48/apps"),
        String::from("/usr/share/icons/Adwaita/48x48/apps"),
        String::from("/usr/share/pixmaps"),
    ];
    let m = get_app_id_to_icon_map(&icon_dirs);
    println!("Found {} icon entries:\n{:#?}", m.len(), m);

    let apps = vec!["Emacs", "Alacritty", "firefoxdeveloperedition", "gimp"];
    for app in apps {
        println!("Icon for {}: {:?}", app, get_icon(app, &icon_dirs))
    }
}

pub trait DisplayFormat {
    fn format_for_display(&self, config: &cfg::Config) -> String;
}

pub fn select_from_menu<'a, 'b, TS>(
    prompt: &'a str,
    choices: &'b [TS],
) -> Result<&'b TS, String>
where
    TS: DisplayFormat + Sized,
{
    let mut map: HashMap<String, &TS> = HashMap::new();
    let mut strs: Vec<String> = vec![];
    let cfg = cfg::load_config();
    for c in choices {
        let s = c.format_for_display(&cfg);
        strs.push(s.clone());

        // Workaround: rofi has "\u0000icon\u001f/path/to/icon.png" as image
        // escape sequence which comes after the actual text but returns only
        // the text, not the escape sequence.
        if s.contains('\0') {
            if let Some(prefix) = s.split('\0').next() {
                map.insert(prefix.to_string(), c);
            }
        }

        map.insert(s, c);
    }

    let menu_exec = cfg.get_menu_executable();
    let args: Vec<String> = cfg
        .get_menu_args()
        .iter()
        .map(|a| a.replace("{prompt}", prompt))
        .collect();

    let mut menu = proc::Command::new(&menu_exec)
        .args(args)
        .stdin(proc::Stdio::piped())
        .stdout(proc::Stdio::piped())
        .spawn()
        .expect(&("Error running ".to_owned() + &menu_exec));

    {
        let stdin = menu
            .stdin
            .as_mut()
            .expect("Failed to open the menu program's stdin");
        let input = strs.join("\n");
        println!("Menu program {} input:\n{}", menu_exec, input);
        stdin
            .write_all(input.as_bytes())
            .expect("Failed to write to the menu program's stdin");
    }

    let output = menu.wait_with_output().expect("Failed to read stdout");
    let choice = String::from_utf8_lossy(&output.stdout);
    let mut choice = String::from(choice);
    choice.pop(); // Remove trailing \n from choice.
    map.get(&choice).copied().ok_or(choice)
}