source: trunk/src/platform/nacl/nacl_instance.cpp @ 1435

Last change on this file since 1435 was 1435, checked in by sam, 8 years ago

nacl: remove our old hack of #including the file containing main() from
the NaCl instance object. Now we properly override main() and we can at
last build all the executables as NaCl binaries.

  • Property svn:keywords set to Id
File size: 3.8 KB
Line 
1// Copyright (c) 2011 The Native Client Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#if defined HAVE_CONFIG_H
6#   include "config.h"
7#endif
8
9#include <cstdlib>
10#include <cstdio>
11#include <cstring>
12#include <string>
13#include <vector>
14
15#include <ppapi/cpp/rect.h>
16#include <ppapi/cpp/size.h>
17#include <ppapi/cpp/var.h>
18#include <ppapi/cpp/module.h>
19#include <ppapi/cpp/completion_callback.h>
20#include <ppapi/cpp/input_event.h>
21
22#include "core.h"
23#include "debug/quad.h"
24
25#include "platform/nacl/nacl_instance.h"
26#include "platform/nacl/opengl_context.h"
27
28/* One of these wrappers will be overridden by the user's version */
29void lol_nacl_main(void) __attribute__((weak));
30void lol_nacl_main(void) {}
31void lol_nacl_main(int argc, char **argv) __attribute__((weak));
32void lol_nacl_main(int argc, char **argv) {}
33void lol_nacl_main(int argc, char **argv, char **envp) __attribute__((weak));
34void lol_nacl_main(int argc, char **argv, char **envp) {}
35
36namespace lol
37{
38
39NaClInstance::NaClInstance(PP_Instance instance)
40    : pp::Instance(instance),
41      m_size(0, 0)
42{
43    RequestInputEvents(PP_INPUTEVENT_CLASS_MOUSE);
44}
45
46NaClInstance::~NaClInstance()
47{
48    // Destroy the cube view while GL context is current.
49    opengl_context_->MakeContextCurrent(this);
50}
51
52static double const DELTA_MS = 1000.0 / 60.0;
53
54void TickCallback(void* data, int32_t result)
55{
56    NaClInstance *instance = (NaClInstance *)data;
57    instance->DrawSelf();
58
59    /* FIXME: only set if if Ticker isn't finished */
60    pp::Module::Get()->core()->CallOnMainThread(
61            DELTA_MS, pp::CompletionCallback(&TickCallback, data), PP_OK);
62}
63
64bool NaClInstance::Init(uint32_t argc,
65                        const char* /* argn */[],
66                        const char* argv[])
67{
68    // My timer callback
69    pp::Module::Get()->core()->CallOnMainThread(
70            DELTA_MS, pp::CompletionCallback(&TickCallback, this), PP_OK);
71
72    /* Call the user's main() function. FIXME: run it in a thread */
73    char *env[] = { NULL };
74    lol_nacl_main();
75    lol_nacl_main(argc, const_cast<char **>(argv));
76    lol_nacl_main(argc, const_cast<char **>(argv), (char **)env);
77
78    return true;
79}
80
81void NaClInstance::HandleMessage(const pp::Var& message)
82{
83    if (!message.is_string())
84        return;
85    /* FIXME: do some shit here */
86}
87
88void NaClInstance::DidChangeView(const pp::Rect& position, const pp::Rect& clip)
89{
90    if (position.size().width() == m_size.x &&
91        position.size().height() == m_size.y)
92        return;  // Size didn't change, no need to update anything.
93
94    m_size = ivec2(position.size().width(), position.size().height());
95
96    if (opengl_context_ == NULL)
97        opengl_context_.reset(new OpenGLContext(this));
98    opengl_context_->InvalidateContext(this);
99    opengl_context_->ResizeContext(position.size());
100    if (!opengl_context_->MakeContextCurrent(this))
101        return;
102
103    Video::Setup(m_size);
104    DrawSelf();
105}
106
107bool NaClInstance::HandleInputEvent(const pp::InputEvent& event)
108{
109    switch (event.GetType())
110    {
111    case PP_INPUTEVENT_TYPE_MOUSEDOWN:
112        Input::SetMouseButton(pp::MouseInputEvent(event).GetButton());
113        break;
114    case PP_INPUTEVENT_TYPE_MOUSEUP:
115        Input::UnsetMouseButton(pp::MouseInputEvent(event).GetButton());
116        break;
117    case PP_INPUTEVENT_TYPE_MOUSEMOVE:
118        Input::SetMousePos(ivec2(pp::MouseInputEvent(event).GetPosition().x(), opengl_context_->GetSize().height() - 1 - pp::MouseInputEvent(event).GetPosition().y()));
119        break;
120    default:
121        break;
122    }
123    return true;
124}
125
126void NaClInstance::DrawSelf()
127{
128    if (opengl_context_ == NULL)
129        return;
130
131    opengl_context_->MakeContextCurrent(this);
132    Ticker::TickDraw();
133    opengl_context_->FlushContext();
134}
135
136}  // namespace lol
137
Note: See TracBrowser for help on using the repository browser.