|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | + * SPDX-FileCopyrightText: 2025 elementary, Inc. <https://elementary.io> |
| 4 | + * |
| 5 | + * Authored by: Leonardo Lemos <leonardolemos@live.com> |
| 6 | + */ |
| 7 | + |
| 8 | +public class Display.MonitorLayoutManager : GLib.Object { |
| 9 | + private Settings settings; |
| 10 | + |
| 11 | + private const string PREFERRED_MONITOR_LAYOUTS_KEY = "preferred-display-layouts"; |
| 12 | + |
| 13 | + public MonitorLayoutManager () { |
| 14 | + Object (); |
| 15 | + } |
| 16 | + |
| 17 | + construct { |
| 18 | + settings = new Settings ("io.elementary.settings.display"); |
| 19 | + } |
| 20 | + |
| 21 | + public void arrange_monitors (Gee.LinkedList<VirtualMonitor> virtual_monitors) { |
| 22 | + if (virtual_monitors.size == 1) { |
| 23 | + // If there's only one monitor, no need to arrange |
| 24 | + // Cloned monitors only have one virtual monitor so will return here |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + var layout_key = get_layout_key (virtual_monitors); |
| 29 | + // Layouts format are 'a{sa{sa{sv}}}' |
| 30 | + var layouts = settings.get_value (PREFERRED_MONITOR_LAYOUTS_KEY); |
| 31 | + Variant? monitors = null; |
| 32 | + if (layouts != null) { |
| 33 | + monitors = layouts.lookup_value (layout_key, VariantType.VARDICT); |
| 34 | + foreach (var virtual_monitor in virtual_monitors) { |
| 35 | + Variant? props = monitors.lookup_value (virtual_monitor.id, VariantType.VARDICT); |
| 36 | + if (props != null) { |
| 37 | + int32 x = 0, y = 0; |
| 38 | + uint32 t = 0; |
| 39 | + bool p = false, e = false; |
| 40 | + if (props.lookup ("x", "i", out x) && |
| 41 | + props.lookup ("y", "i", out y) && |
| 42 | + props.lookup ("transform", "u", out t) && |
| 43 | + props.lookup ("primary", "b", out p) && |
| 44 | + props.lookup ("enabled", "b", out e)) { |
| 45 | + |
| 46 | + virtual_monitor.x = x; |
| 47 | + virtual_monitor.y = y; |
| 48 | + virtual_monitor.transform = t; |
| 49 | + virtual_monitor.primary = p; |
| 50 | + virtual_monitor.is_active = e; |
| 51 | + } else { |
| 52 | + warning ("property setting missing for monitor %s", virtual_monitor.get_display_name ()); |
| 53 | + } |
| 54 | + } else { |
| 55 | + warning ("no property dictionary found for monitor.id %s", virtual_monitor.get_display_name ()); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + return; |
| 60 | + } else { |
| 61 | + warning ("layout key %s not found", layout_key); |
| 62 | + } |
| 63 | + |
| 64 | + // If no layout found, we save the current layout to use later |
| 65 | + save_layout (virtual_monitors); |
| 66 | + } |
| 67 | + |
| 68 | + public void save_layout (Gee.LinkedList<VirtualMonitor> virtual_monitors) { |
| 69 | + var save_key = get_layout_key (virtual_monitors); |
| 70 | + |
| 71 | + var monitor_dict = new VariantDict (); |
| 72 | + foreach (var monitor in virtual_monitors) { |
| 73 | + var props_dict = new VariantDict (); |
| 74 | + props_dict.insert_value ("x", new Variant.int32 (monitor.x)); |
| 75 | + props_dict.insert_value ("y", new Variant.int32 (monitor.y)); |
| 76 | + props_dict.insert_value ("transform", new Variant.uint32 (monitor.transform)); |
| 77 | + props_dict.insert_value ("primary", new Variant.boolean (monitor.primary)); |
| 78 | + props_dict.insert_value ("enabled", new Variant.boolean (monitor.is_active)); |
| 79 | + monitor_dict.insert_value (monitor.id, props_dict.end ()); |
| 80 | + } |
| 81 | + |
| 82 | + // Add or update the layouts setting |
| 83 | + var layouts = settings.get_value (PREFERRED_MONITOR_LAYOUTS_KEY); |
| 84 | + var layouts_dict = new VariantDict (layouts); |
| 85 | + layouts_dict.insert_value (save_key, monitor_dict.end ()); |
| 86 | + |
| 87 | + // Save to settings |
| 88 | + //NOTE The variant yielded by VariantDict.end () always has type "a{sv}" |
| 89 | + settings.set_value (PREFERRED_MONITOR_LAYOUTS_KEY, layouts_dict.end ()); |
| 90 | + } |
| 91 | + |
| 92 | + private string get_layout_key (Gee.LinkedList<VirtualMonitor> virtual_monitors) { |
| 93 | + // Generate a unique key based on the virtual monitors' monitors hashes |
| 94 | + //NOTE The key depends on the order of the list which will change depending on whether monitors are |
| 95 | + // active or not (and possibly on the order they were connected). |
| 96 | + //TODO Consider whether a more controlled key is needed |
| 97 | + var key = new StringBuilder (); |
| 98 | + |
| 99 | + foreach (var virtual_monitor in virtual_monitors) { |
| 100 | + foreach (var monitor in virtual_monitor.monitors) { |
| 101 | + key.append (virtual_monitor.id); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + return key.str; |
| 106 | + } |
| 107 | +} |
0 commit comments