summaryrefslogtreecommitdiff
path: root/libraries/pake/source/window.cpp
blob: c1c773f69102539c1c9ff64f6b9c5c2217e9fc5b (plain) (blame)
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
#include <pake/window.hpp>
#include <cassert>

//TODO: handle errors on socket connection, read, and write

namespace pake {

  window::window(int width, int height, const std::string &title)
  : width(width), height(height), contents(width, height), root() {

    euler::syscall::connect_to_socket("hilbert.compositor", socket);

    assert(width >= 0 && height >= 0);

    struct [[gnu::packed]] { uint8_t type; uint32_t width; uint32_t height; }
      dimensions_pkt = {.type = 0x02, .width = (uint32_t)width, .height = (uint32_t)height };
    euler::syscall::write_to_stream(socket, sizeof(dimensions_pkt), &dimensions_pkt);

    assert(title.size() <= UINT32_MAX);

    struct [[gnu::packed]] { uint8_t type; uint32_t length; }
      title_pkt = {.type = 0x03, .length = (uint32_t)title.size() };
    euler::syscall::write_to_stream(socket, sizeof(title_pkt), &title_pkt);
    euler::syscall::write_to_stream(socket, title.size(), title.data());

  }

  window::~window() {
    euler::syscall::close_stream(socket);
  }

  void window::show() {
    uint8_t packet = 0;
    euler::syscall::write_to_stream(socket, 1, &packet);
  }

  void window::hide() {
    uint8_t packet = 1;
    euler::syscall::write_to_stream(socket, 1, &packet);
  }

  void window::set_root(std::unique_ptr<widget> &&w) {
    root = std::move(w);
    root->notify_size(width, height);
    root->render(contents, 0, 0, true);
  }

  void window::render_and_send_to_compositor() {

    root->render(contents, 0, 0, false);
    auto dirties = contents.get_dirty_regions();

    for (auto it = dirties.cbegin(); it != dirties.cend(); ++it) {

      struct [[gnu::packed]] {
        uint8_t type;
        uint32_t start_x; uint32_t start_y;
        uint32_t   width; uint32_t  height;
      } update_pkt = {
        .type = 0x04,
        .start_x = (uint32_t)it->start_x, .start_y = (uint32_t)it->start_y,
        .  width = (uint32_t)it->  width, . height = (uint32_t)it-> height
      };

      euler::syscall::write_to_stream(socket, sizeof(update_pkt), &update_pkt);

      for (int y = it->start_y; y < it->start_y + it->height; ++y)
        euler::syscall::write_to_stream(socket,
          it->width * sizeof(daguerre::hilbert_color),
          &contents.image.at(it->start_x, y));

    }

    contents.clear_dirty();

  }

}