-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
event: add initial support for events
- Loading branch information
1 parent
3c62338
commit ea275f4
Showing
5 changed files
with
134 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package hyprland | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"strings" | ||
) | ||
|
||
const SEPARATOR = ">>" | ||
|
||
func MustEventClient() *EventClient { | ||
return must1(NewEventClient(mustSocket(".socket2.sock"))) | ||
} | ||
|
||
// Initiate a new event client. | ||
// Receive as parameters a socket that is generally localised in | ||
// '$XDG_RUNTIME_DIR/hypr/$HYPRLAND_INSTANCE_SIGNATURE/.socket2.sock'. | ||
func NewEventClient(socket string) (*EventClient, error) { | ||
conn, err := net.Dial("unix", socket) | ||
if err != nil { | ||
return nil, fmt.Errorf("error while connecting to socket: %w", err) | ||
} | ||
return &EventClient{ conn: conn }, nil | ||
} | ||
|
||
// Low-level receive event method, should be avoided unless there is no | ||
// alternative. | ||
func (c *EventClient) Receive() ([]ReceivedData, error) { | ||
buf := make([]byte, BUF_SIZE) | ||
n, err := c.conn.Read(buf) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
buf = buf[:n] | ||
|
||
var recv []ReceivedData | ||
raw := strings.Split(string(buf), "\n") | ||
for _, event := range raw { | ||
if event == "" { | ||
continue | ||
} | ||
|
||
split := strings.Split(event, SEPARATOR) | ||
if split[0] == "" || split[1] == "" || split[1] == "," { | ||
continue | ||
} | ||
|
||
recv = append(recv, ReceivedData{ | ||
Type: EventType(split[0]), | ||
Data: RawData(split[1]), | ||
}) | ||
} | ||
|
||
return recv, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package hyprland | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
) | ||
|
||
var ec *EventClient | ||
|
||
func init() { | ||
if os.Getenv("HYPRLAND_INSTANCE_SIGNATURE") != "" { | ||
ec = MustEventClient() | ||
} | ||
} | ||
|
||
func TestReceive(t *testing.T) { | ||
if ec == nil { | ||
t.Skip("HYPRLAND_INSTANCE_SIGNATURE not set, skipping test") | ||
} | ||
msg, err := ec.Receive() | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
fmt.Println(msg) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters