api/lua: Use API values, make client not a fn

This commit is contained in:
Ottatop 2024-07-12 14:56:26 -05:00
parent c1936c8a87
commit 81716cc8df
12 changed files with 195 additions and 251 deletions

View file

@ -27,17 +27,33 @@ local pinnacle = {
---@type Render
render = require("pinnacle.render"),
---@type pinnacle.Snowcap
snowcap = require("pinnacle.snowcap"),
snowcap = nil,
}
---Quit Pinnacle.
function pinnacle.quit()
client():unary_request(pinnacle_service.Quit, {})
client:unary_request(pinnacle_service.Quit, {})
end
---Reload the active config.
function pinnacle.reload_config()
client():unary_request(pinnacle_service.ReloadConfig, {})
client:unary_request(pinnacle_service.ReloadConfig, {})
end
function pinnacle.init()
require("pinnacle.grpc.protobuf").build_protos()
require("pinnacle.grpc.client").connect()
local success, snowcap = pcall(require, "snowcap")
if success then
if pcall(snowcap.init) then
pinnacle.snowcap = require("pinnacle.snowcap")
-- Make Snowcap use Pinnacle's cqueues loop
require("snowcap.grpc.client").client.loop = client.loop
end
end
end
---Setup a Pinnacle config.
@ -53,23 +69,14 @@ end
---
---@see Pinnacle.run
function pinnacle.setup(config_fn)
require("pinnacle.grpc.protobuf").build_protos()
local success, snowcap = pcall(require, "snowcap")
if success then
snowcap.init()
end
require("pinnacle.grpc.client").connect()
-- Make Snowcap use Pinnacle's cqueues loop
require("snowcap.grpc.client").client().loop = client().loop
pinnacle.init()
-- This function ensures a config won't run forever if Pinnacle is killed
-- and doesn't kill configs on drop.
client().loop:wrap(function()
client.loop:wrap(function()
while true do
require("cqueues").sleep(60)
local success, err, errno = client().conn:ping(10)
local success, err, errno = client.conn:ping(10)
if not success then
print("Compositor ping failed:", err, errno)
os.exit(1)
@ -79,7 +86,7 @@ function pinnacle.setup(config_fn)
config_fn(pinnacle)
local success, err = client().loop:loop()
local success, err = client.loop:loop()
if not success then
print(err)
end
@ -100,13 +107,7 @@ end
---
---@param run_fn fun(pinnacle: Pinnacle)
function pinnacle.run(run_fn)
require("pinnacle.grpc.protobuf").build_protos()
local success, snowcap = pcall(require, "snowcap")
if success then
snowcap.init()
end
require("pinnacle.grpc.client").connect()
pinnacle.init()
run_fn(pinnacle)
end

View file

@ -2,19 +2,18 @@
-- License, v. 2.0. If a copy of the MPL was not distributed with this
-- file, You can obtain one at https://mozilla.org/MPL/2.0/.
local client_inner = nil
local client = {
---@type fun(): grpc_client.Client
client = function()
return client_inner
end,
---@type grpc_client.Client
---@diagnostic disable-next-line: missing-fields
client = {},
}
function client.connect()
client_inner = require("grpc_client").new({
local c = require("grpc_client").new({
path = os.getenv("PINNACLE_GRPC_SOCKET"),
})
setmetatable(client.client, { __index = c })
end
return client

View file

@ -3,19 +3,21 @@
-- file, You can obtain one at https://mozilla.org/MPL/2.0/.
local client = require("pinnacle.grpc.client").client
local input_service = require("pinnacle.grpc.defs").pinnacle.input.v0alpha1.InputService
-- This is an @enum and not an @alias because with an @alias the completion replaces tables with a string,
-- which is annoying
local defs = require("pinnacle.grpc.defs")
local input_v0alpha1 = defs.pinnacle.input.v0alpha1
local input_service = defs.pinnacle.input.v0alpha1.InputService
---@enum (key) Modifier
local modifier_values = {
shift = 1,
ctrl = 2,
alt = 3,
super = 4,
shift = input_v0alpha1.Modifier.MODIFIER_SHIFT,
ctrl = input_v0alpha1.Modifier.MODIFIER_CTRL,
alt = input_v0alpha1.Modifier.MODIFIER_ALT,
super = input_v0alpha1.Modifier.MODIFIER_SUPER,
}
require("pinnacle.util").make_bijective(modifier_values)
---@enum (key) MouseButton
local mouse_button_values = {
--- Left
[1] = 0x110,
@ -39,30 +41,12 @@ local mouse_button_values = {
btn_forward = 0x115,
btn_back = 0x116,
}
-- This alias is because I can't get @enum completion to work
---@alias MouseButton
---| 1 Left
---| 2 Right
---| 3 Middle
---| 4 Side
---| 5 Extra
---| 6 Forward
---| 7 Back,
---| "btn_left"
---| "btn_right"
---| "btn_middle"
---| "btn_side"
---| "btn_extra"
---| "btn_forward"
---| "btn_back"
---@enum (key) MouseEdge
local mouse_edge_values = {
press = 1,
release = 2,
press = input_v0alpha1.SetMousebindRequest.MouseEdge.MOUSE_EDGE_PRESS,
release = input_v0alpha1.SetMousebindRequest.MouseEdge.MOUSE_EDGE_RELEASE,
}
---@alias MouseEdge
---| "press" Trigger on mouse button press
---| "release" Trigger on mouse button release
---Input management.
---
@ -131,7 +115,7 @@ function input.keybind(mods, key, action, keybind_info)
table.insert(mod_values, modifier_values[mod])
end
client():server_streaming_request(input_service.SetKeybind, {
client:server_streaming_request(input_service.SetKeybind, {
modifiers = mod_values,
raw_code = raw_code,
xkb_name = xkb_name,
@ -165,7 +149,7 @@ function input.mousebind(mods, button, edge, action)
table.insert(mod_values, modifier_values[mod])
end
client():server_streaming_request(input_service.SetMousebind, {
client:server_streaming_request(input_service.SetMousebind, {
modifiers = mod_values,
button = mouse_button_values[button],
edge = edge,
@ -184,7 +168,7 @@ end
---@return KeybindDescription[]
function input.keybind_descriptions()
---@type pinnacle.input.v0alpha1.KeybindDescriptionsResponse
local descs = client():unary_request(input_service.KeybindDescriptions, {})
local descs = client:unary_request(input_service.KeybindDescriptions, {})
local descs = descs.descriptions or {}
local ret = {}
@ -233,7 +217,7 @@ end
---
---@param xkb_config XkbConfig The new xkbconfig
function input.set_xkb_config(xkb_config)
client():unary_request(input_service.SetXkbConfig, xkb_config)
client:unary_request(input_service.SetXkbConfig, xkb_config)
end
---Set the keyboard's repeat rate and delay.
@ -246,47 +230,47 @@ end
---@param rate integer The time between repeats in milliseconds
---@param delay integer The duration a key needs to be held down before repeating starts in milliseconds
function input.set_repeat_rate(rate, delay)
client():unary_request(input_service.SetRepeatRate, {
client:unary_request(input_service.SetRepeatRate, {
rate = rate,
delay = delay,
})
end
---@enum (key) AccelProfile
local accel_profile_values = {
flat = 1,
adaptive = 2,
---No pointer acceleration
flat = input_v0alpha1.SetLibinputSettingRequest.AccelProfile.ACCEL_PROFILE_FLAT,
---Pointer acceleration
adaptive = input_v0alpha1.SetLibinputSettingRequest.AccelProfile.ACCEL_PROFILE_ADAPTIVE,
}
---@alias AccelProfile
---| "flat" No pointer acceleration
---| "adaptive" Pointer acceleration
---@enum (key) ClickMethod
local click_method_values = {
button_areas = 1,
click_finger = 2,
---Button presses are generated according to where on the device the click occurs
button_areas = input_v0alpha1.SetLibinputSettingRequest.ClickMethod.CLICK_METHOD_BUTTON_AREAS,
---Button presses are generated according to the number of fingers used
click_finger = input_v0alpha1.SetLibinputSettingRequest.ClickMethod.CLICK_METHOD_CLICK_FINGER,
}
---@alias ClickMethod
---| "button_areas" Button presses are generated according to where on the device the click occurs
---| "click_finger" Button presses are generated according to the number of fingers used
---@enum (key) ScrollMethod
local scroll_method_values = {
no_scroll = 1,
two_finger = 2,
edge = 3,
on_button_down = 4,
---Never send scroll events instead of pointer motion events
no_scroll = input_v0alpha1.SetLibinputSettingRequest.ScrollMethod.SCROLL_METHOD_NO_SCROLL,
---Send scroll events when two fingers are logically down on the device
two_finger = input_v0alpha1.SetLibinputSettingRequest.ScrollMethod.SCROLL_METHOD_TWO_FINGER,
---Send scroll events when a finger moves along the bottom or right edge of a device
edge = input_v0alpha1.SetLibinputSettingRequest.ScrollMethod.SCROLL_METHOD_EDGE,
---Send scroll events when a button is down and the device moves along a scroll-capable axis
on_button_down = input_v0alpha1.SetLibinputSettingRequest.ScrollMethod.SCROLL_METHOD_ON_BUTTON_DOWN,
}
---@alias ScrollMethod
---| "no_scroll" Never send scroll events instead of pointer motion events
---| "two_finger" Send scroll events when two fingers are logically down on the device
---| "edge" Send scroll events when a finger moves along the bottom or right edge of a device
---| "on_button_down" Send scroll events when a button is down and the device moves along a scroll-capable axis
---@enum (key) TapButtonMap
local tap_button_map_values = {
left_right_middle = 1,
left_middle_right = 2,
---1/2/3 finger tap maps to left/right/middle
left_right_middle = input_v0alpha1.SetLibinputSettingRequest.TapButtonMap.TAP_BUTTON_MAP_LEFT_RIGHT_MIDDLE,
---1/2/3 finger tap maps to left/middle/right
left_middle_right = input_v0alpha1.SetLibinputSettingRequest.TapButtonMap.TAP_BUTTON_MAP_LEFT_MIDDLE_RIGHT,
}
---@alias TapButtonMap
---| "left_right_middle" 1/2/3 finger tap maps to left/right/middle
---| "left_middle_right" 1/2/3 finger tap maps to left/middle/right
---@class LibinputSettings
---@field accel_profile AccelProfile? Set pointer acceleration
@ -322,32 +306,32 @@ local tap_button_map_values = {
function input.set_libinput_settings(settings)
for setting, value in pairs(settings) do
if setting == "accel_profile" then
client():unary_request(
client:unary_request(
input_service.SetLibinputSetting,
{ [setting] = accel_profile_values[value] }
)
elseif setting == "calibration_matrix" then
client():unary_request(
client:unary_request(
input_service.SetLibinputSetting,
{ [setting] = { matrix = value } }
)
elseif setting == "click_method" then
client():unary_request(
client:unary_request(
input_service.SetLibinputSetting,
{ [setting] = click_method_values[value] }
)
elseif setting == "scroll_method" then
client():unary_request(
client:unary_request(
input_service.SetLibinputSetting,
{ [setting] = scroll_method_values[value] }
)
elseif setting == "tap_button_map" then
client():unary_request(
client:unary_request(
input_service.SetLibinputSetting,
{ [setting] = tap_button_map_values[value] }
)
else
client():unary_request(input_service.SetLibinputSetting, { [setting] = value })
client:unary_request(input_service.SetLibinputSetting, { [setting] = value })
end
end
end
@ -359,7 +343,7 @@ end
---
---@param theme string
function input.set_xcursor_theme(theme)
client():unary_request(input_service.SetXcursor, {
client:unary_request(input_service.SetXcursor, {
theme = theme,
})
end
@ -371,7 +355,7 @@ end
---
---@param size integer
function input.set_xcursor_size(size)
client():unary_request(input_service.SetXcursor, {
client:unary_request(input_service.SetXcursor, {
size = size,
})
end

View file

@ -983,7 +983,7 @@ local layout = {
---
---@param manager LayoutManager
function layout.set_manager(manager)
layout.stream = client():bidirectional_streaming_request(layout_service.Layout, {
layout.stream = client:bidirectional_streaming_request(layout_service.Layout, {
layout = {},
}, function(response, stream)
local request_id = response.request_id

View file

@ -3,6 +3,7 @@
-- file, You can obtain one at https://mozilla.org/MPL/2.0/.
local client = require("pinnacle.grpc.client").client
local output_v0alpha1 = require("pinnacle.grpc.defs").pinnacle.output.v0alpha1
local output_service = require("pinnacle.grpc.defs").pinnacle.output.v0alpha1.OutputService
---@nodoc
@ -40,7 +41,7 @@ output.handle = output_handle
---
---@return OutputHandle[]
function output.get_all()
local response = client():unary_request(output_service.Get, {})
local response = client:unary_request(output_service.Get, {})
---@type OutputHandle[]
local handles = {}
@ -603,7 +604,6 @@ function output.setup_locs(update_locs_on, locs)
end
end
---@type table<string, SignalServiceMethod>
local signal_name_to_SignalName = {
connect = "OutputConnect",
disconnect = "OutputDisconnect",
@ -690,7 +690,7 @@ end
---
---@see OutputHandle.set_loc_adj_to
function OutputHandle:set_location(loc)
client():unary_request(output_service.SetLocation, {
client:unary_request(output_service.SetLocation, {
output_name = self.name,
x = loc.x,
y = loc.y,
@ -827,7 +827,7 @@ end
---@param pixel_height integer
---@param refresh_rate_millihz integer?
function OutputHandle:set_mode(pixel_width, pixel_height, refresh_rate_millihz)
client():unary_request(output_service.SetMode, {
client:unary_request(output_service.SetMode, {
output_name = self.name,
pixel_width = pixel_width,
pixel_height = pixel_height,
@ -880,21 +880,21 @@ function OutputHandle:set_modeline(modeline)
vsync_pos = modeline.vsync,
}
client():unary_request(output_service.SetModeline, request)
client:unary_request(output_service.SetModeline, request)
end
---Set this output's scaling factor.
---
---@param scale number
function OutputHandle:set_scale(scale)
client():unary_request(output_service.SetScale, { output_name = self.name, absolute = scale })
client:unary_request(output_service.SetScale, { output_name = self.name, absolute = scale })
end
---Increase this output's scaling factor.
---
---@param increase_by number
function OutputHandle:increase_scale(increase_by)
client():unary_request(
client:unary_request(
output_service.SetScale,
{ output_name = self.name, relative = increase_by }
)
@ -909,32 +909,22 @@ end
---@enum (key) Transform
local transform_name_to_code = {
normal = 1,
["90"] = 2,
["180"] = 3,
["270"] = 4,
flipped = 5,
flipped_90 = 6,
flipped_180 = 7,
flipped_270 = 8,
}
local transform_code_to_name = {
[1] = "normal",
[2] = "90",
[3] = "180",
[4] = "270",
[5] = "flipped",
[6] = "flipped_90",
[7] = "flipped_180",
[8] = "flipped_270",
normal = output_v0alpha1.Transform.TRANSFORM_NORMAL,
["90"] = output_v0alpha1.Transform.TRANSFORM_90,
["180"] = output_v0alpha1.Transform.TRANSFORM_180,
["270"] = output_v0alpha1.Transform.TRANSFORM_270,
flipped = output_v0alpha1.Transform.TRANSFORM_FLIPPED,
flipped_90 = output_v0alpha1.Transform.TRANSFORM_FLIPPED_90,
flipped_180 = output_v0alpha1.Transform.TRANSFORM_FLIPPED_180,
flipped_270 = output_v0alpha1.Transform.TRANSFORM_FLIPPED_270,
}
require("pinnacle.util").make_bijective(transform_name_to_code)
---Set this output's transform.
---
---@param transform Transform
function OutputHandle:set_transform(transform)
client():unary_request(
client:unary_request(
output_service.SetTransform,
{ output_name = self.name, transform = transform_name_to_code[transform] }
)
@ -944,10 +934,7 @@ end
---
---@param powered boolean
function OutputHandle:set_powered(powered)
client():unary_request(
output_service.SetPowered,
{ output_name = self.name, powered = powered }
)
client:unary_request(output_service.SetPowered, { output_name = self.name, powered = powered })
end
---@class Mode
@ -981,8 +968,7 @@ end
---@return OutputProperties
function OutputHandle:props()
---@type pinnacle.output.v0alpha1.GetPropertiesResponse
local response =
client():unary_request(output_service.GetProperties, { output_name = self.name })
local response = client:unary_request(output_service.GetProperties, { output_name = self.name })
---@diagnostic disable-next-line: invisible
local tag_handles = require("pinnacle.tag").handle.new_from_table(response.tag_ids or {})
@ -1000,7 +986,7 @@ function OutputHandle:props()
response.tags = tag_handles
response.modes = response.modes or {}
response.transform = transform_code_to_name[response.transform]
response.transform = transform_name_to_code[response.transform]
response.keyboard_focus_stack = keyboard_focus_stack_handles
return response

View file

@ -31,7 +31,7 @@ local function spawn_inner(args, callbacks, once)
end
end
client():server_streaming_request(process_service.Spawn, {
client:server_streaming_request(process_service.Spawn, {
args = args,
once = once,
has_callback = callbacks ~= nil,
@ -106,7 +106,7 @@ end
---@param key string The environment variable key
---@param value string The environment variable value
function process.set_env(key, value)
client():unary_request(process_service.SetEnv, {
client:unary_request(process_service.SetEnv, {
key = key,
value = value,
})

View file

@ -3,6 +3,7 @@
-- file, You can obtain one at https://mozilla.org/MPL/2.0/.
local client = require("pinnacle.grpc.client").client
local render_v0alpha1 = require("pinnacle.grpc.defs").pinnacle.render.v0alpha1
local render_service = require("pinnacle.grpc.defs").pinnacle.render.v0alpha1.RenderService
---Rendering management.
@ -10,21 +11,19 @@ local render_service = require("pinnacle.grpc.defs").pinnacle.render.v0alpha1.Re
---@class Render
local render = {}
---@alias ScalingFilter
---| "bilinear" Blend between the four closest pixels. May cause scaling to be blurry.
---| "nearest_neighbor" Choose the closest pixel. Causes scaling to look pixelated.
---@type table<ScalingFilter, integer>
---@enum (key) ScalingFilter
local filter_name_to_filter_value = {
bilinear = 1,
nearest_neighbor = 2,
---Blend between the four closest pixels. May cause scaling to be blurry.
bilinear = render_v0alpha1.Filter.FILTER_BILINEAR,
---Choose the closest pixel. Causes scaling to look pixelated.
nearest_neighbor = render_v0alpha1.Filter.FILTER_NEAREST_NEIGHBOR,
}
---Set the upscale filter the renderer will use to upscale buffers.
---
---@param filter ScalingFilter
function render.set_upscale_filter(filter)
client():unary_request(
client:unary_request(
render_service.SetUpscaleFilter,
{ filter = filter_name_to_filter_value[filter] }
)
@ -34,7 +33,7 @@ end
---
---@param filter ScalingFilter
function render.set_downscale_filter(filter)
client():unary_request(
client:unary_request(
render_service.SetDownscaleFilter,
{ filter = filter_name_to_filter_value[filter] }
)

View file

@ -5,91 +5,65 @@
local client = require("pinnacle.grpc.client").client
local signal_service = require("pinnacle.grpc.defs").pinnacle.signal.v0alpha1.SignalService
local stream_control = {
UNSPECIFIED = 0,
READY = 1,
DISCONNECT = 2,
}
local stream_control = require("pinnacle.grpc.defs").pinnacle.signal.v0alpha1.StreamControl
-- TODO: rewrite ldoc_gen so you don't have to stick @nodoc everywhere
---@nodoc
---@type table<SignalServiceMethod, { sender: H2Stream?, callbacks: function[], on_response: fun(response: table) }>
---@type table<SignalServiceMethod, { sender: grpc_client.h2.Stream?, callbacks: function[], on_response: fun(response: table) }>
local signals = {
OutputConnect = {
---@nodoc
---@type H2Stream?
---@type grpc_client.h2.Stream?
sender = nil,
---@nodoc
---@type (fun(output: OutputHandle))[]
callbacks = {},
---@nodoc
---@type fun(response: table)
on_response = nil,
},
OutputDisconnect = {
---@nodoc
---@type H2Stream?
---@type grpc_client.h2.Stream?
sender = nil,
---@nodoc
---@type (fun(output: OutputHandle))[]
callbacks = {},
---@nodoc
---@type fun(response: table)
on_response = nil,
},
OutputResize = {
---@nodoc
---@type H2Stream?
---@type grpc_client.h2.Stream?
sender = nil,
---@nodoc
---@type (fun(output: OutputHandle, logical_width: integer, logical_height: integer))[]
callbacks = {},
---@nodoc
---@type fun(response: table)
on_response = nil,
},
OutputMove = {
---@nodoc
---@type H2Stream?
---@type grpc_client.h2.Stream?
sender = nil,
---@nodoc
---@type (fun(output: OutputHandle, x: integer, y: integer))[]
callbacks = {},
---@nodoc
---@type fun(response: table)
on_response = nil,
},
WindowPointerEnter = {
---@nodoc
---@type H2Stream?
---@type grpc_client.h2.Stream?
sender = nil,
---@nodoc
---@type (fun(window: WindowHandle))[]
callbacks = {},
---@nodoc
---@type fun(response: table)
on_response = nil,
},
WindowPointerLeave = {
---@nodoc
---@type H2Stream?
---@type grpc_client.h2.Stream?
sender = nil,
---@nodoc
---@type (fun(window: WindowHandle))[]
callbacks = {},
---@nodoc
---@type fun(response: table)
on_response = nil,
},
TagActive = {
---@nodoc
---@type H2Stream?
---@type grpc_client.h2.Stream?
sender = nil,
---@nodoc
---@type (fun(tag: TagHandle, active: boolean))[]
callbacks = {},
---@nodoc
---@type fun(response: table)
on_response = nil,
},
@ -250,8 +224,8 @@ end
---@param request SignalServiceMethod
---@param callback fun(response: table)
function signal.connect(request, callback)
local stream = client():bidirectional_streaming_request(signal_service[request], {
control = stream_control.READY,
local stream = client:bidirectional_streaming_request(signal_service[request], {
control = stream_control.STREAM_CONTROL_READY,
}, function(response)
callback(response)
@ -259,7 +233,7 @@ function signal.connect(request, callback)
local chunk = require("pinnacle.grpc.protobuf").encode(
"pinnacle.signal.v0alpha1." .. request .. "Request",
{
control = stream_control.READY,
control = stream_control.STREAM_CONTROL_READY,
}
)
@ -284,7 +258,7 @@ function signal.disconnect(request)
local chunk = require("pinnacle.grpc.protobuf").encode(
"pinnacle.signal.v0alpha1." .. request .. "Request",
{
control = stream_control.DISCONNECT,
control = stream_control.STREAM_CONTROL_DISCONNECT,
}
)

View file

@ -6,11 +6,11 @@ local client = require("pinnacle.grpc.client").client
local tag_service = require("pinnacle.grpc.defs").pinnacle.tag.v0alpha1.TagService
local set_or_toggle = {
SET = 1,
[true] = 1,
UNSET = 2,
[false] = 2,
TOGGLE = 3,
SET = require("pinnacle.grpc.defs").pinnacle.v0alpha1.SetOrToggle.SET_OR_TOGGLE_SET,
[true] = require("pinnacle.grpc.defs").pinnacle.v0alpha1.SetOrToggle.SET_OR_TOGGLE_SET,
UNSET = require("pinnacle.grpc.defs").pinnacle.v0alpha1.SetOrToggle.SET_OR_TOGGLE_UNSET,
[false] = require("pinnacle.grpc.defs").pinnacle.v0alpha1.SetOrToggle.SET_OR_TOGGLE_UNSET,
TOGGLE = require("pinnacle.grpc.defs").pinnacle.v0alpha1.SetOrToggle.SET_OR_TOGGLE_TOGGLE,
}
---@nodoc
@ -51,7 +51,7 @@ tag.handle = tag_handle
---
---@return TagHandle[]
function tag.get_all()
local response = client():unary_request(tag_service.Get, {})
local response = client:unary_request(tag_service.Get, {})
---@type TagHandle[]
local handles = {}
@ -136,7 +136,7 @@ function tag.add(output, ...)
tag_names = tag_names[1] --[=[@as string[]]=]
end
local response = client():unary_request(tag_service.Add, {
local response = client:unary_request(tag_service.Add, {
output_name = output.name,
tag_names = tag_names,
})
@ -169,7 +169,7 @@ function tag.remove(tags)
table.insert(ids, tg.id)
end
client():unary_request(tag_service.Remove, { tag_ids = ids })
client:unary_request(tag_service.Remove, { tag_ids = ids })
end
---@type table<string, SignalServiceMethod>
@ -245,7 +245,7 @@ end
---Tag.get("3"):switch_to() -- Displays Steam
---```
function TagHandle:switch_to()
client():unary_request(tag_service.SwitchTo, { tag_id = self.id })
client:unary_request(tag_service.SwitchTo, { tag_id = self.id })
end
---Set whether or not this tag is active.
@ -263,7 +263,7 @@ end
---
---@param active boolean
function TagHandle:set_active(active)
client():unary_request(
client:unary_request(
tag_service.SetActive,
{ tag_id = self.id, set_or_toggle = set_or_toggle[active] }
)
@ -281,7 +281,7 @@ end
---Tag.get("2"):toggle_active() -- Displays nothing
---```
function TagHandle:toggle_active()
client():unary_request(
client:unary_request(
tag_service.SetActive,
{ tag_id = self.id, set_or_toggle = set_or_toggle.TOGGLE }
)
@ -297,7 +297,7 @@ end
---
---@return TagProperties
function TagHandle:props()
local response = client():unary_request(tag_service.GetProperties, { tag_id = self.id })
local response = client:unary_request(tag_service.GetProperties, { tag_id = self.id })
return {
active = response.active,

View file

@ -304,7 +304,7 @@ local function deep_copy_rec(obj, seen)
return no
end
---Create a deep copy of an object.
---Creates a deep copy of an object.
---
---@generic T
---
@ -315,7 +315,7 @@ function util.deep_copy(obj)
return deep_copy_rec(obj, nil)
end
---Create a table with entries key->value and value->key for all given pairs.
---Creates a table with entries key->value and value->key for all given pairs.
---
---@generic T
---@param key_value_pairs T
@ -332,4 +332,19 @@ function util.bijective_table(key_value_pairs)
return ret
end
---Makes a table bijective by inserting `value = key` entries for every key-value pair.
---
---@param table table
function util.make_bijective(table)
local temp = {}
for k, v in pairs(table) do
temp[v] = k
end
for k, v in pairs(temp) do
table[k] = v
end
end
return util

View file

@ -3,14 +3,15 @@
-- file, You can obtain one at https://mozilla.org/MPL/2.0/.
local client = require("pinnacle.grpc.client").client
local window_v0alpha1 = require("pinnacle.grpc.defs").pinnacle.window.v0alpha1
local window_service = require("pinnacle.grpc.defs").pinnacle.window.v0alpha1.WindowService
local set_or_toggle = {
SET = 1,
[true] = 1,
UNSET = 2,
[false] = 2,
TOGGLE = 3,
SET = require("pinnacle.grpc.defs").pinnacle.v0alpha1.SetOrToggle.SET_OR_TOGGLE_SET,
[true] = require("pinnacle.grpc.defs").pinnacle.v0alpha1.SetOrToggle.SET_OR_TOGGLE_SET,
UNSET = require("pinnacle.grpc.defs").pinnacle.v0alpha1.SetOrToggle.SET_OR_TOGGLE_UNSET,
[false] = require("pinnacle.grpc.defs").pinnacle.v0alpha1.SetOrToggle.SET_OR_TOGGLE_UNSET,
TOGGLE = require("pinnacle.grpc.defs").pinnacle.v0alpha1.SetOrToggle.SET_OR_TOGGLE_TOGGLE,
}
---@nodoc
@ -50,7 +51,7 @@ window.handle = window_handle
---```
---@return WindowHandle[] windows Handles to all windows
function window.get_all()
local response = client():unary_request(window_service.Get, {})
local response = client:unary_request(window_service.Get, {})
local handles = window_handle.new_from_table(response.window_ids or {})
@ -105,7 +106,7 @@ end
function window.begin_move(button)
---@diagnostic disable-next-line: redefined-local, invisible
local button = require("pinnacle.input").mouse_button_values[button]
client():unary_request(window_service.MoveGrab, { button = button })
client:unary_request(window_service.MoveGrab, { button = button })
end
---Begin resizing this window using the specified mouse button.
@ -123,7 +124,7 @@ end
function window.begin_resize(button)
---@diagnostic disable-next-line: redefined-local, invisible
local button = require("pinnacle.input").mouse_button_values[button]
client():unary_request(window_service.ResizeGrab, { button = button })
client:unary_request(window_service.ResizeGrab, { button = button })
end
---@class WindowRuleCondition
@ -133,11 +134,14 @@ end
---@field titles string[]?
---@field tags TagHandle[]?
---@alias WindowState
---| "tiled"
---| "floating"
---| "fullscreen"
---| "maximized"
---@enum (key) WindowState
local window_state = {
tiled = window_v0alpha1.WindowState.WINDOW_STATE_TILED,
floating = window_v0alpha1.WindowState.WINDOW_STATE_FLOATING,
fullscreen = window_v0alpha1.WindowState.WINDOW_STATE_FULLSCREEN,
maximized = window_v0alpha1.WindowState.WINDOW_STATE_MAXIMIZED,
}
require("pinnacle.util").make_bijective(window_state)
---@class WindowRule
---@field output OutputHandle?
@ -152,17 +156,12 @@ end
---@field state WindowState?
---@enum (key) FullscreenOrMaximized
local _fullscreen_or_maximized = {
neither = 1,
fullscreen = 2,
maximized = 3,
}
local _fullscreen_or_maximized_keys = {
[1] = "neither",
[2] = "fullscreen",
[3] = "maximized",
local fullscreen_or_maximized = {
neither = window_v0alpha1.FullscreenOrMaximized.FULLSCREEN_OR_MAXIMIZED_NEITHER,
fullscreen = window_v0alpha1.FullscreenOrMaximized.FULLSCREEN_OR_MAXIMIZED_FULLSCREEN,
maximized = window_v0alpha1.FullscreenOrMaximized.FULLSCREEN_OR_MAXIMIZED_MAXIMIZED,
}
require("pinnacle.util").make_bijective(fullscreen_or_maximized)
---@param rule WindowRule
local function process_window_rule(rule)
@ -180,7 +179,7 @@ local function process_window_rule(rule)
end
if rule.fullscreen_or_maximized then
rule.fullscreen_or_maximized = _fullscreen_or_maximized[rule.fullscreen_or_maximized]
rule.fullscreen_or_maximized = fullscreen_or_maximized[rule.fullscreen_or_maximized]
end
if rule.decoration_mode then
@ -328,7 +327,7 @@ function window.add_window_rule(rule)
process_window_rule_cond(rule.cond)
client():unary_request(window_service.AddWindowRule, {
client:unary_request(window_service.AddWindowRule, {
cond = rule.cond,
rule = rule.rule,
})
@ -390,7 +389,7 @@ end
---if focused then focused:close() end
---```
function WindowHandle:close()
client():unary_request(window_service.Close, { window_id = self.id })
client:unary_request(window_service.Close, { window_id = self.id })
end
---Set this window's location and/or size.
@ -420,7 +419,7 @@ end
---```
---@param geo { x: integer?, y: integer?, width: integer?, height: integer? } The new location and/or size
function WindowHandle:set_geometry(geo)
client():unary_request(window_service.SetGeometry, { window_id = self.id, geometry = geo })
client:unary_request(window_service.SetGeometry, { window_id = self.id, geometry = geo })
end
---Set this window to fullscreen or not.
@ -436,7 +435,7 @@ end
---
---@param fullscreen boolean
function WindowHandle:set_fullscreen(fullscreen)
client():unary_request(
client:unary_request(
window_service.SetFullscreen,
{ window_id = self.id, set_or_toggle = set_or_toggle[fullscreen] }
)
@ -452,7 +451,7 @@ end
---end
---```
function WindowHandle:toggle_fullscreen()
client():unary_request(
client:unary_request(
window_service.SetFullscreen,
{ window_id = self.id, set_or_toggle = set_or_toggle.TOGGLE }
)
@ -471,7 +470,7 @@ end
---
---@param maximized boolean
function WindowHandle:set_maximized(maximized)
client():unary_request(
client:unary_request(
window_service.SetMaximized,
{ window_id = self.id, set_or_toggle = set_or_toggle[maximized] }
)
@ -487,7 +486,7 @@ end
---end
---```
function WindowHandle:toggle_maximized()
client():unary_request(
client:unary_request(
window_service.SetMaximized,
{ window_id = self.id, set_or_toggle = set_or_toggle.TOGGLE }
)
@ -506,7 +505,7 @@ end
---
---@param floating boolean
function WindowHandle:set_floating(floating)
client():unary_request(
client:unary_request(
window_service.SetFloating,
{ window_id = self.id, set_or_toggle = set_or_toggle[floating] }
)
@ -522,7 +521,7 @@ end
---end
---```
function WindowHandle:toggle_floating()
client():unary_request(
client:unary_request(
window_service.SetFloating,
{ window_id = self.id, set_or_toggle = set_or_toggle.TOGGLE }
)
@ -540,7 +539,7 @@ end
---
---@param focused boolean
function WindowHandle:set_focused(focused)
client():unary_request(
client:unary_request(
window_service.SetFocused,
{ window_id = self.id, set_or_toggle = set_or_toggle[focused] }
)
@ -556,7 +555,7 @@ end
---end
---```
function WindowHandle:toggle_focused()
client():unary_request(
client:unary_request(
window_service.SetFocused,
{ window_id = self.id, set_or_toggle = set_or_toggle.TOGGLE }
)
@ -577,7 +576,7 @@ end
---
---@param tag TagHandle The tag to move this window to
function WindowHandle:move_to_tag(tag)
client():unary_request(window_service.MoveToTag, { window_id = self.id, tag_id = tag.id })
client:unary_request(window_service.MoveToTag, { window_id = self.id, tag_id = tag.id })
end
---Tag or untag the given tag on this window.
@ -599,7 +598,7 @@ end
---@param tag TagHandle The tag to set or unset
---@param set boolean
function WindowHandle:set_tag(tag, set)
client():unary_request(
client:unary_request(
window_service.SetTag,
{ window_id = self.id, tag_id = tag.id, set_or_toggle = set_or_toggle[set] }
)
@ -624,7 +623,7 @@ end
---
---@param tag TagHandle The tag to toggle
function WindowHandle:toggle_tag(tag)
client():unary_request(
client:unary_request(
window_service.SetTag,
{ window_id = self.id, tag_id = tag.id, set_or_toggle = set_or_toggle.TOGGLE }
)
@ -642,7 +641,7 @@ end
---end
---```
function WindowHandle:raise()
client():unary_request(window_service.Raise, { window_id = self.id })
client:unary_request(window_service.Raise, { window_id = self.id })
end
---Returns whether or not this window is on an active tag.
@ -685,10 +684,9 @@ end
---
---@return WindowProperties
function WindowHandle:props()
local response = client():unary_request(window_service.GetProperties, { window_id = self.id })
local response = client:unary_request(window_service.GetProperties, { window_id = self.id })
response.fullscreen_or_maximized =
_fullscreen_or_maximized_keys[response.fullscreen_or_maximized]
response.fullscreen_or_maximized = fullscreen_or_maximized[response.fullscreen_or_maximized]
response.tags = response.tag_ids
---@diagnostic disable-next-line: invisible
@ -696,19 +694,7 @@ function WindowHandle:props()
response.tag_ids = nil
if response.state then
local WindowState = require("pinnacle.grpc.defs").pinnacle.window.v0alpha1.WindowState
---@type WindowState?
local state = nil
if response.state == WindowState.WINDOW_STATE_TILED then
state = "tiled"
elseif response.state == WindowState.WINDOW_STATE_FLOATING then
state = "floating"
elseif response.state == WindowState.WINDOW_STATE_FULLSCREEN then
state = "fullscreen"
elseif response.state == WindowState.WINDOW_STATE_MAXIMIZED then
state = "maximized"
end
response.state = state
response.state = window_state[response.state]
end
return response

@ -1 +1 @@
Subproject commit 3783530a00124f1c1ba9ebaccb413a3d3e18ef1b
Subproject commit e93857b9f6ca8a2ebbef1e1acad1350bb971b208