swayは現在フォーカスされたコンテナのレイアウトを取得します。

swayは現在フォーカスされたコンテナのレイアウトを取得します。

現在フォーカスがあるコンテナ(「splith」、「tabbed」など)のレイアウトを取得するには?最良の方法はswaymsg -t get_tree(以下の例)を使用してJSON出力を解析することですが、これらのjq「n-debthツリー」で何をすべきかわかりません。

非常に基本的な質問かもしれませんが、jsonを扱うのは今回が初めてです。

「添付ファイル」:サンプルJSON出力swaymsg -t get-tree

これは、「focused」:true属性を持つフォーカスされた端末を持つ3つの端末が開いている上記のコマンドの出力です。

{
  "border": "none",
  "current_border_width": 0,
  "deco_rect": {
    "height": 0,
    "width": 0,
    "x": 0,
    "y": 0
  },
  "floating_nodes": [],
  "focus": [
    3
  ],
  "focused": false,
  "fullscreen_mode": 0,
  "geometry": {
    "height": 0,
    "width": 0,
    "x": 0,
    "y": 0
  },
  "id": 1,
  "layout": "splith",
  "marks": [],
  "name": "root",
  "nodes": [
    {
      "border": "none",
      "current_border_width": 0,
      "deco_rect": {
        "height": 0,
        "width": 0,
        "x": 0,
        "y": 0
      },
      "floating_nodes": [],
      "focus": [
        2147483646
      ],
      "focused": false,
      "fullscreen_mode": 0,
      "geometry": {
        "height": 0,
        "width": 0,
        "x": 0,
        "y": 0
      },
      "id": 2147483647,
      "layout": "output",
      "marks": [],
      "name": "__i3",
      "nodes": [
        {
          "border": "none",
          "current_border_width": 0,
          "deco_rect": {
            "height": 0,
            "width": 0,
            "x": 0,
            "y": 0
          },
          "floating_nodes": [],
          "focus": [],
          "focused": false,
          "fullscreen_mode": 1,
          "geometry": {
            "height": 0,
            "width": 0,
            "x": 0,
            "y": 0
          },
          "id": 2147483646,
          "layout": "splith",
          "marks": [],
          "name": "__i3_scratch",
          "nodes": [],
          "orientation": "horizontal",
          "percent": null,
          "rect": {
            "height": 720,
            "width": 360,
            "x": 0,
            "y": 0
          },
          "sticky": false,
          "type": "workspace",
          "urgent": false,
          "window": null,
          "window_rect": {
            "height": 0,
            "width": 0,
            "x": 0,
            "y": 0
          }
        }
      ],
      "orientation": "horizontal",
      "percent": null,
      "rect": {
        "height": 720,
        "width": 360,
        "x": 0,
        "y": 0
      },
      "sticky": false,
      "type": "output",
      "urgent": false,
      "window": null,
      "window_rect": {
        "height": 0,
        "width": 0,
        "x": 0,
        "y": 0
      }
    },
    {
      "active": true,
      "adaptive_sync_status": "disabled",
      "border": "none",
      "current_border_width": 0,
      "current_mode": {
        "height": 1440,
        "refresh": 60006,
        "width": 720
      },
      "current_workspace": "1",
      "deco_rect": {
        "height": 0,
        "width": 0,
        "x": 0,
        "y": 0
      },
      "dpms": false,
      "floating_nodes": [],
      "focus": [
        30,
        32
      ],
      "focused": false,
      "fullscreen_mode": 0,
      "geometry": {
        "height": 0,
        "width": 0,
        "x": 0,
        "y": 0
      },
      "id": 3,
      "layout": "output",
      "make": "<Unknown>",
      "marks": [],
      "max_render_time": 0,
      "model": "<Unknown>",
      "modes": [
        {
          "height": 1440,
          "refresh": 60006,
          "width": 720
        }
      ],
      "name": "DSI-1",
      "nodes": [
        {
          "border": "none",
          "current_border_width": 0,
          "deco_rect": {
            "height": 0,
            "width": 0,
            "x": 0,
            "y": 0
          },
          "floating_nodes": [],
          "focus": [
            36,
            35
          ],
          "focused": false,
          "fullscreen_mode": 1,
          "geometry": {
            "height": 0,
            "width": 0,
            "x": 0,
            "y": 0
          },
          "id": 30,
          "layout": "splith",
          "marks": [],
          "name": "1",
          "nodes": [
            {
              "app_id": "foot",
              "border": "pixel",
              "current_border_width": 3,
              "deco_rect": {
                "height": 0,
                "width": 0,
                "x": 0,
                "y": 0
              },
              "floating_nodes": [],
              "focus": [],
              "focused": false,
              "fullscreen_mode": 0,
              "geometry": {
                "height": 500,
                "width": 700,
                "x": 0,
                "y": 0
              },
              "id": 35,
              "idle_inhibitors": {
                "application": "none",
                "user": "none"
              },
              "inhibit_idle": false,
              "layout": "none",
              "marks": [],
              "max_render_time": 0,
              "name": "",
              "nodes": [],
              "orientation": "none",
              "percent": 0.5,
              "pid": 25044,
              "rect": {
                "height": 455,
                "width": 180,
                "x": 0,
                "y": 25
              },
              "shell": "xdg_shell",
              "sticky": false,
              "type": "con",
              "urgent": false,
              "visible": true,
              "window": null,
              "window_rect": {
                "height": 449,
                "width": 174,
                "x": 3,
                "y": 3
              }
            },
            {
              "border": "none",
              "current_border_width": 0,
              "deco_rect": {
                "height": 0,
                "width": 0,
                "x": 0,
                "y": 0
              },
              "floating_nodes": [],
              "focus": [
                34
              ],
              "focused": false,
              "fullscreen_mode": 0,
              "geometry": {
                "height": 0,
                "width": 0,
                "x": 0,
                "y": 0
              },
              "id": 36,
              "layout": "splitv",
              "marks": [],
              "name": null,
              "nodes": [
                {
                  "border": "none",
                  "current_border_width": 0,
                  "deco_rect": {
                    "height": 0,
                    "width": 0,
                    "x": 0,
                    "y": 0
                  },
                  "floating_nodes": [],
                  "focus": [
                    27,
                    33
                  ],
                  "focused": false,
                  "fullscreen_mode": 0,
                  "geometry": {
                    "height": 0,
                    "width": 0,
                    "x": 0,
                    "y": 0
                  },
                  "id": 34,
                  "layout": "splitv",
                  "marks": [],
                  "name": null,
                  "nodes": [
                    {
                      "app_id": "foot",
                      "border": "pixel",
                      "current_border_width": 3,
                      "deco_rect": {
                        "height": 0,
                        "width": 0,
                        "x": 0,
                        "y": 0
                      },
                      "floating_nodes": [],
                      "focus": [],
                      "focused": true,
                      "fullscreen_mode": 0,
                      "geometry": {
                        "height": 500,
                        "width": 700,
                        "x": 0,
                        "y": 0
                      },
                      "id": 27,
                      "idle_inhibitors": {
                        "application": "none",
                        "user": "none"
                      },
                      "inhibit_idle": false,
                      "layout": "none",
                      "marks": [],
                      "max_render_time": 0,
                      "name": "",
                      "nodes": [],
                      "orientation": "none",
                      "percent": 0.5010989010989011,
                      "pid": 28522,
                      "rect": {
                        "height": 228,
                        "width": 180,
                        "x": 180,
                        "y": 25
                      },
                      "shell": "xdg_shell",
                      "sticky": false,
                      "type": "con",
                      "urgent": false,
                      "visible": true,
                      "window": null,
                      "window_rect": {
                        "height": 222,
                        "width": 174,
                        "x": 3,
                        "y": 3
                      }
                    },
                    {
                      "app_id": "foot",
                      "border": "pixel",
                      "current_border_width": 3,
                      "deco_rect": {
                        "height": 0,
                        "width": 0,
                        "x": 0,
                        "y": 0
                      },
                      "floating_nodes": [],
                      "focus": [],
                      "focused": false,
                      "fullscreen_mode": 0,
                      "geometry": {
                        "height": 500,
                        "width": 700,
                        "x": 0,
                        "y": 0
                      },
                      "id": 33,
                      "idle_inhibitors": {
                        "application": "none",
                        "user": "none"
                      },
                      "inhibit_idle": false,
                      "layout": "none",
                      "marks": [],
                      "max_render_time": 0,
                      "name": "",
                      "nodes": [],
                      "orientation": "none",
                      "percent": 0.4989010989010989,
                      "pid": 23338,
                      "rect": {
                        "height": 227,
                        "width": 180,
                        "x": 180,
                        "y": 253
                      },
                      "shell": "xdg_shell",
                      "sticky": false,
                      "type": "con",
                      "urgent": false,
                      "visible": true,
                      "window": null,
                      "window_rect": {
                        "height": 221,
                        "width": 174,
                        "x": 3,
                        "y": 3
                      }
                    }
                  ],
                  "orientation": "vertical",
                  "percent": 1,
                  "rect": {
                    "height": 455,
                    "width": 180,
                    "x": 180,
                    "y": 25
                  },
                  "sticky": false,
                  "type": "con",
                  "urgent": false,
                  "window": null,
                  "window_rect": {
                    "height": 0,
                    "width": 0,
                    "x": 0,
                    "y": 0
                  }
                }
              ],
              "orientation": "vertical",
              "percent": 0.5,
              "rect": {
                "height": 455,
                "width": 180,
                "x": 180,
                "y": 25
              },
              "sticky": false,
              "type": "con",
              "urgent": false,
              "window": null,
              "window_rect": {
                "height": 0,
                "width": 0,
                "x": 0,
                "y": 0
              }
            }
          ],
          "num": 1,
          "orientation": "horizontal",
          "output": "DSI-1",
          "percent": null,
          "rect": {
            "height": 455,
            "width": 360,
            "x": 0,
            "y": 25
          },
          "representation": "H[foot V[V[foot foot]]]",
          "sticky": false,
          "type": "workspace",
          "urgent": false,
          "window": null,
          "window_rect": {
            "height": 0,
            "width": 0,
            "x": 0,
            "y": 0
          }
        },
        {
          "border": "none",
          "current_border_width": 0,
          "deco_rect": {
            "height": 0,
            "width": 0,
            "x": 0,
            "y": 0
          },
          "floating_nodes": [],
          "focus": [
            19
          ],
          "focused": false,
          "fullscreen_mode": 1,
          "geometry": {
            "height": 0,
            "width": 0,
            "x": 0,
            "y": 0
          },
          "id": 32,
          "layout": "splitv",
          "marks": [],
          "name": "4",
          "nodes": [
            {
              "app_id": "foot",
              "border": "pixel",
              "current_border_width": 3,
              "deco_rect": {
                "height": 0,
                "width": 0,
                "x": 0,
                "y": 0
              },
              "floating_nodes": [],
              "focus": [],
              "focused": false,
              "fullscreen_mode": 0,
              "geometry": {
                "height": 500,
                "width": 700,
                "x": 0,
                "y": 0
              },
              "id": 19,
              "idle_inhibitors": {
                "application": "none",
                "user": "none"
              },
              "inhibit_idle": false,
              "layout": "none",
              "marks": [],
              "max_render_time": 0,
              "name": "",
              "nodes": [],
              "orientation": "none",
              "percent": 1,
              "pid": 3363,
              "rect": {
                "height": 455,
                "width": 360,
                "x": 0,
                "y": 25
              },
              "shell": "xdg_shell",
              "sticky": false,
              "type": "con",
              "urgent": false,
              "visible": false,
              "window": null,
              "window_rect": {
                "height": 455,
                "width": 360,
                "x": 0,
                "y": 3
              }
            }
          ],
          "num": 4,
          "orientation": "vertical",
          "output": "DSI-1",
          "percent": null,
          "rect": {
            "height": 455,
            "width": 360,
            "x": 0,
            "y": 25
          },
          "representation": "V[foot]",
          "sticky": false,
          "type": "workspace",
          "urgent": false,
          "window": null,
          "window_rect": {
            "height": 0,
            "width": 0,
            "x": 0,
            "y": 0
          }
        }
      ],
      "orientation": "none",
      "percent": 1,
      "primary": false,
      "rect": {
        "height": 720,
        "width": 360,
        "x": 0,
        "y": 0
      },
      "scale": 2,
      "scale_filter": "nearest",
      "serial": "",
      "sticky": false,
      "transform": "normal",
      "type": "output",
      "urgent": false,
      "window": null,
      "window_rect": {
        "height": 0,
        "width": 0,
        "x": 0,
        "y": 0
      }
    }
  ],
  "orientation": "horizontal",
  "percent": null,
  "rect": {
    "height": 720,
    "width": 360,
    "x": 0,
    "y": 0
  },
  "sticky": false,
  "type": "root",
  "urgent": false,
  "window": null,
  "window_rect": {
    "height": 0,
    "width": 0,
    "x": 0,
    "y": 0
  }
}

ベストアンサー1

recurse関数を使用して、およびコンテナー(および入れ子になったコンテナー)を反復的jqに繰り返すことができます。この関数を使用すると、フォーカスコンテナのみをフィルタリングしてキー値を取得できます。nodesrootoutputworkspaceconselectlayout

swaymsg -t get_tree | jq 'recurse(.nodes[]) | select(.nodes[].focused == true).layout'

おすすめ記事