aboutsummaryrefslogtreecommitdiffstats
path: root/support/hololens/ServoApp/Servo.cpp
blob: 43350aa54befbec0ec606f3a8e4b25600cb3c0c3 (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
#include "pch.h"
#include "Servo.h"

namespace servo {

void on_load_started() { sServo->Delegate().OnLoadStarted(); }
void on_load_ended() { sServo->Delegate().OnLoadEnded(); }
void on_history_changed(bool back, bool forward) {
  sServo->Delegate().OnHistoryChanged(back, forward);
}
void on_shutdown_complete() { sServo->Delegate().OnShutdownComplete(); }
void on_alert(const char *message) {
  sServo->Delegate().OnAlert(char2w(message));
}
void on_title_changed(const char *title) {
  sServo->Delegate().OnTitleChanged(char2w(title));
}
void on_url_changed(const char *url) {
  sServo->Delegate().OnURLChanged(char2w(url));
}
void flush() { sServo->Delegate().Flush(); }
void make_current() { sServo->Delegate().MakeCurrent(); }
void wakeup() { sServo->Delegate().WakeUp(); }
bool on_allow_navigation(const char *url) {
 return sServo->Delegate().OnAllowNavigation(char2w(url));
};
void on_animating_changed(bool aAnimating) {
  sServo->Delegate().OnAnimatingChanged(aAnimating);
}

Servo::Servo(GLsizei width, GLsizei height, ServoDelegate &aDelegate)
    : mWindowHeight(height), mWindowWidth(width), mDelegate(aDelegate) {

  capi::CInitOptions o;
  o.args = "--pref dom.webxr.enabled";
  o.url = "https://servo.org";
  o.width = mWindowWidth;
  o.height = mWindowHeight;
  o.density = 1.0;
  o.enable_subpixel_text_antialiasing = false;
  o.vr_pointer = NULL;

  sServo = this; // FIXME;

  capi::CHostCallbacks c;
  c.flush = &flush;
  c.make_current = &make_current;
  c.on_alert = &on_alert;
  c.on_load_started = &on_load_started;
  c.on_load_ended = &on_load_ended;
  c.on_title_changed = &on_title_changed;
  c.on_url_changed = &on_url_changed;
  c.on_history_changed = &on_history_changed;
  c.on_animating_changed = &on_animating_changed;
  c.on_shutdown_complete = &on_shutdown_complete;
  c.on_allow_navigation = &on_allow_navigation;

  init_with_egl(o, &wakeup, c);
}

Servo::~Servo() { sServo = nullptr; }

std::wstring char2w(const char *c_str) {
  auto str = std::string(c_str);
  int size_needed =
      MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0);
  std::wstring str2(size_needed, 0);
  MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &str2[0],
                      size_needed);
  return str2;
}

} // namespace servo