-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtool_get_screen_orientation.go
More file actions
97 lines (82 loc) · 2.52 KB
/
tool_get_screen_orientation.go
File metadata and controls
97 lines (82 loc) · 2.52 KB
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
//go:build linux
package main
import (
"context"
"encoding/json"
"fmt"
"strings"
"github.com/facebookincubator/go-belt/tool/logger"
"github.com/mark3labs/mcp-go/mcp"
"github.com/mark3labs/mcp-go/server"
)
// ScreenOrientationResult holds parsed orientation data.
type ScreenOrientationResult struct {
Rotation string `json:"rotation"`
UserLocked string `json:"user_locked,omitempty"`
RawRotation string `json:"raw_rotation,omitempty"`
}
func registerGetScreenOrientation(s *server.MCPServer) {
tool := mcp.NewTool("get_screen_orientation",
mcp.WithDescription(
"Get the current screen orientation/rotation. "+
"Returns rotation value (0=portrait, 1=landscape, 2=reverse-portrait, 3=reverse-landscape).",
),
mcp.WithReadOnlyHintAnnotation(true),
mcp.WithDestructiveHintAnnotation(false),
mcp.WithIdempotentHintAnnotation(true),
)
s.AddTool(tool, handleGetScreenOrientation)
}
func handleGetScreenOrientation(
ctx context.Context,
_ mcp.CallToolRequest,
) (*mcp.CallToolResult, error) {
logger.Tracef(ctx, "handleGetScreenOrientation")
defer func() { logger.Tracef(ctx, "/handleGetScreenOrientation") }()
out, err := shellExec("dumpsys window displays | grep -E 'mCurrentRotation|mUserRotation'")
if err != nil {
// dumpsys may still return partial output.
if out == "" {
return mcp.NewToolResultError(fmt.Sprintf("dumpsys window: %v", err)), nil
}
}
result := parseScreenOrientation(out)
data, err := json.Marshal(result)
if err != nil {
return nil, fmt.Errorf("marshaling orientation: %w", err)
}
return mcp.NewToolResultText(string(data)), nil
}
func parseScreenOrientation(output string) ScreenOrientationResult {
result := ScreenOrientationResult{}
for _, line := range strings.Split(output, "\n") {
line = strings.TrimSpace(line)
switch {
case strings.Contains(line, "mCurrentRotation"):
// Format: mCurrentRotation=ROTATION_0
if idx := strings.Index(line, "="); idx >= 0 {
result.RawRotation = strings.TrimSpace(line[idx+1:])
result.Rotation = rotationToName(result.RawRotation)
}
case strings.Contains(line, "mUserRotation"):
if idx := strings.Index(line, "="); idx >= 0 {
result.UserLocked = strings.TrimSpace(line[idx+1:])
}
}
}
return result
}
func rotationToName(raw string) string {
switch {
case strings.Contains(raw, "0"):
return "portrait"
case strings.Contains(raw, "1"):
return "landscape"
case strings.Contains(raw, "2"):
return "reverse-portrait"
case strings.Contains(raw, "3"):
return "reverse-landscape"
default:
return raw
}
}