summaryrefslogtreecommitdiff
path: root/applications/goldman/source/socket.cpp
blob: b175fe9ac45531d82b0ab533f91cc587c4d432a3 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <daguerre/image.hpp>
#include "socket.hpp"
#include "window.hpp"
#include "main.hpp"
#include <memory>
#include <vector>

struct socket_state {

  euler::syscall::stream_handle socket;
  std::vector<window *> windows;
  daguerre::hilbert_color window_bg = euler::syscall::encode_color(0, 0, 0);

  bool try_open_window() {

    struct [[gnu::packed]] {
      uint32_t width;
      uint32_t height;
    } body;

    if (euler::syscall::read_from_stream(socket, sizeof(body), &body) !=
        euler::syscall::stream_result::success)
      return false;

    window *w = new window(body.width, body.height);
    w->contents.fill(window_bg);

    uint16_t wid = 0;

    while (wid < windows.size())
      if (windows[wid] != 0)
        ++wid;
      else
        break;

    if (wid == windows.size())
      windows.push_back(w);
    else
      windows[wid] = w;

    r->lock();
    r->add_window(w);
    r->unlock();

    struct [[gnu::packed]] {
      uint8_t type;
      uint16_t the_window;
    } response {
      .type = 0x00,
      .the_window = wid
    };

    return
      euler::syscall::write_to_stream(socket, sizeof(response), &response) ==
      euler::syscall::stream_result::success;

  }

  bool try_update_window_region() {

    struct [[gnu::packed]] {
      uint16_t window;
      uint32_t start_x;
      uint32_t start_y;
      uint32_t width;
      uint32_t height;
    } body_head;

    if (euler::syscall::read_from_stream(socket, sizeof(body_head), &body_head) !=
        euler::syscall::stream_result::success)
      return false;

    std::vector<daguerre::hilbert_color> data(body_head.width * body_head.height);

    if (euler::syscall::read_from_stream(socket, data.size() * 4, data.data()) !=
        euler::syscall::stream_result::success)
      return false;

    daguerre::image<daguerre::hilbert_color>
      data_as_image(body_head.width, body_head.height, data.data(), body_head.width, false);

    if (body_head.window >= windows.size() || !windows[body_head.window])
      return false;

    window *w = windows[body_head.window];

    r->lock();

    if ((int)body_head.start_x + data_as_image.width > w->contents.width ||
        (int)body_head.start_y + data_as_image.height > w->contents.height) {
      r->unlock();
      return false;
    }

    w->contents.copy_from(data_as_image, body_head.start_x, body_head.start_y);

    r->unlock();
    r->dispatch_render();
    return true;

  }

  bool try_close_window() {

    uint16_t wid;

    if (euler::syscall::read_from_stream(socket, 2, &wid) !=
        euler::syscall::stream_result::success)
      return false;

    if (wid >= windows.size() || !windows[wid])
      return false;

    r->lock();

    r->remove_window(windows[wid]);
    windows[wid] = 0;

    r->unlock();
    r->dispatch_render();
    return true;

  }

  bool try_process_request() {

    uint8_t type;
    if (euler::syscall::read_from_stream(socket, 1, &type) !=
        euler::syscall::stream_result::success)
      return false;

    switch (type) {
    case 0x00: return try_open_window();
    case 0x01: return try_update_window_region();
    case 0x02: return try_close_window();
    default: return false;
    }

  }

};

[[noreturn]] void socket_thread_main(euler::syscall::stream_handle socket) {

  euler::syscall::set_thread_name("socket thread");

  socket_state *state = new socket_state {
    .socket = socket, .windows = {} };
  while (state->try_process_request()) ;

  r->lock();
  for (unsigned i = 0; i < state->windows.size(); ++i) {
    r->remove_window(state->windows[i]);
    delete state->windows[i];
  }
  r->unlock();

  delete state;
  euler::syscall::close_stream(socket);
  euler::syscall::end_this_thread(0);

}