i3ipc++
An C++ implementaiton of the i3 IPC
 All Classes Functions Variables Enumerations Enumerator Modules
ipc-util.hpp
1 #pragma once
2 
3 #include <cstdint>
4 #include <string>
5 #include <memory>
6 #include <stdexcept>
7 
8 // extern "C" {
9 // #include <i3/ipc.h>
10 // }
11 
12 namespace i3ipc {
13 
19 // extern "C" {
20 
24 struct header_t {
25  /* 6 = strlen(I3_IPC_MAGIC) */
26  char magic[6];
27  uint32_t size;
28  uint32_t type;
29 } __attribute__ ((packed));
30 
31 
35 class ipc_error : public std::runtime_error { using std::runtime_error::runtime_error; };
36 
40 class invalid_header_error : public ipc_error { using ipc_error::ipc_error; };
41 
45 class eof_error : public ipc_error { using ipc_error::ipc_error; };
46 
50 class invalid_reply_payload_error : public ipc_error { using ipc_error::ipc_error; };
51 
55 class errno_error : public ipc_error {
56 public:
57  errno_error();
58  errno_error(const std::string& msg);
59 };
60 
61 
65 enum class ClientMessageType : uint32_t {
66  COMMAND = 0,
67  GET_WORKSPACES = 1,
68  SUBSCRIBE = 2,
69  GET_OUTPUTS = 3,
70  GET_TREE = 4,
71  GET_MARKS = 5,
72  GET_BAR_CONFIG = 6,
73  GET_VERSION = 7,
74 };
75 
76 
80 enum class ReplyType : uint32_t {
81  COMMAND = 0,
82  WORKSPACES = 1,
83  SUBSCRIBE = 2,
84  OUTPUTS = 3,
85  TREE = 4,
86  MARKS = 5,
87  BAR_CONFIG = 6,
88  VERSION = 7,
89 };
90 
91 
95 struct buf_t {
96  uint32_t size;
97  uint8_t* data;
98 
105 
111  char* payload;
112 
113  buf_t(uint32_t payload_size);
114  ~buf_t();
115 
120 };
121 
127 int32_t i3_connect(const std::string& socket_path);
128 
133 void i3_disconnect(const int32_t sockfd);
134 
140 void i3_send(const int32_t sockfd, const buf_t& buff);
141 
147 std::shared_ptr<buf_t> i3_recv(const int32_t sockfd) throw (invalid_header_error, eof_error);
148 
152 std::shared_ptr<buf_t> i3_pack(const ClientMessageType type, const std::string& payload);
153 
163 std::shared_ptr<buf_t> i3_msg(const int32_t sockfd, const ClientMessageType type, const std::string& payload = std::string()) throw (invalid_header_error, eof_error);
164 
169 }
void i3_disconnect(const int32_t sockfd)
Close the connection.
std::shared_ptr< buf_t > i3_msg(const int32_t sockfd, const ClientMessageType type, const std::string &payload=std::string())
Pack, send a message and receiv a reply.
ReplyType
Replies, that can be sended from the i3 to the client.
Definition: ipc-util.hpp:80
ClientMessageType
Messages (requests), that can be sended from the client.
Definition: ipc-util.hpp:65
char * payload
Message payload.
Definition: ipc-util.hpp:111
Definition: ipc-util.hpp:12
char magic[6]
Magic string.
Definition: ipc-util.hpp:26
header_t * header
i3 IPC message header
Definition: ipc-util.hpp:104
std::shared_ptr< buf_t > i3_pack(const ClientMessageType type, const std::string &payload)
Pack a buffer of message.
int32_t i3_connect(const std::string &socket_path)
Connect to the i3 socket.
void realloc_payload_to_header()
Resize payload to the payload_size in the header.
If any error occured, while using C-functions.
Definition: ipc-util.hpp:55
uint8_t * data
Pointer to the message.
Definition: ipc-util.hpp:97
uint32_t type
Message type.
Definition: ipc-util.hpp:28
uint32_t size
Size of payload.
Definition: ipc-util.hpp:27
If something wrong in a payload of i3's reply.
Definition: ipc-util.hpp:50
Socket return EOF, but expected a data.
Definition: ipc-util.hpp:45
i3 IPC header
Definition: ipc-util.hpp:24
Base class of i3 IPC errors.
Definition: ipc-util.hpp:35
Something wrong in message header (wrong magic number, message type etc.)
Definition: ipc-util.hpp:40
void i3_send(const int32_t sockfd, const buf_t &buff)
Send message to the socket.
i3 IPC message buffer
Definition: ipc-util.hpp:95
uint32_t size
Size of whole buffer.
Definition: ipc-util.hpp:96
std::shared_ptr< buf_t > i3_recv(const int32_t sockfd)
Recive a message from i3.