File: msg.hpp

package info (click to toggle)
polybar 3.7.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,108 kB
  • sloc: cpp: 30,424; python: 3,750; sh: 284; makefile: 83
file content (86 lines) | stat: -rw-r--r-- 2,136 bytes parent folder | download
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
#pragma once

#include "common.hpp"

#include <array>
#include <cstdint>

POLYBAR_NS

/**
 * Defines the binary message format for IPC communications over the IPC socket.
 *
 * This is an internal API, do not connect to the socket using 3rd party programs, always use `polybar-msg`.
 */
namespace ipc {
  /**
   * Magic string prefixed to every ipc message.
   *
   * THIS MUST NEVER CHANGE.
   */
  static constexpr std::array<uint8_t, 7> MAGIC = {'p', 'o', 'l', 'y', 'i', 'p', 'c'};
  static const string MAGIC_STR = string(reinterpret_cast<const char*>(MAGIC.data()), MAGIC.size());

  static constexpr uint8_t VERSION = 0;

  using type_t = uint8_t;

  /**
   * Message type indicating success.
   */
  static constexpr type_t TYPE_OK = 0;
  static constexpr type_t TYPE_ERR = 255;

  union header {
    struct header_data {
      uint8_t magic[MAGIC.size()];
      /**
       * Version number of the message format.
       */
      uint8_t version;
      /**
       * Size of the following message in bytes
       */
      uint32_t size;
      /**
       * Type of the message that follows.
       *
       * Meaning of the values depend on version.
       * Only TYPE_OK(0) indicate success and TYPE_ERR(255) always indicates an error, in which case the entire message
       * is a string.
       */
      type_t type;
    } __attribute__((packed)) s;
    uint8_t d[sizeof(header_data)];
  };

  /**
   * Size of the standard header shared by all versions.
   *
   * THIS MUST NEVER CHANGE.
   */
  static constexpr size_t HEADER_SIZE = 13;
  static_assert(sizeof(header) == HEADER_SIZE, "");
  static_assert(sizeof(header::header_data) == HEADER_SIZE, "");

  /**
   * Definitions for version 0 of the IPC message format.
   *
   * The format is very simple. The header defines the type (cmd or action) and the payload is the message for that type
   * as a string.
   */
  namespace v0 {
    enum class ipc_type : type_t {
      /**
       * Message type for ipc commands
       */
      CMD = 1,
      /**
       * Message type for ipc module actions
       */
      ACTION = 2,
    };
  }
}  // namespace ipc

POLYBAR_NS_END