-
Notifications
You must be signed in to change notification settings - Fork 2
/
plugin.cc
95 lines (80 loc) · 2.45 KB
/
plugin.cc
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
// Copyright (c) 2011 The Native Client Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <ppapi/cpp/instance.h>
#include <ppapi/cpp/module.h>
#include <ppapi/cpp/rect.h>
#include <ppapi/cpp/size.h>
#include <SDL_video.h>
extern "C" {
extern int digger_main(int argc, const char *argv[]);
}
#include <SDL.h>
#include <SDL_nacl.h>
class PluginInstance : public pp::Instance {
public:
explicit PluginInstance(PP_Instance instance) : pp::Instance(instance),
sdl_main_thread_(0),
width_(0),
height_(0) {
RequestInputEvents(PP_INPUTEVENT_CLASS_MOUSE);
RequestFilteringInputEvents(PP_INPUTEVENT_CLASS_KEYBOARD);
}
~PluginInstance() {
if (sdl_main_thread_) {
pthread_join(sdl_main_thread_, NULL);
}
}
virtual void DidChangeView(const pp::Rect& position, const pp::Rect& clip) {
printf("did change view, new %dx%d, old %dx%d\n",
position.size().width(), position.size().height(),
width_, height_);
if (position.size().width() == width_ &&
position.size().height() == height_)
return; // Size didn't change, no need to update anything.
if (sdl_thread_started_ == false) {
width_ = position.size().width();
height_ = position.size().height();
SDL_NACL_SetInstance(pp_instance(), width_, height_);
// It seems this call to SDL_Init is required. Calling from
// sdl_main() isn't good enough.
// Perhaps it must be called from the main thread?
int lval = SDL_Init(SDL_INIT_VIDEO);
assert(lval >= 0);
if (0 == pthread_create(&sdl_main_thread_, NULL, sdl_thread, this)) {
sdl_thread_started_ = true;
}
}
}
bool HandleInputEvent(const pp::InputEvent& event) {
SDL_NACL_PushEvent(event);
return true;
}
bool Init(int argc, const char* argn[], const char* argv[]) {
return true;
}
private:
bool sdl_thread_started_;
pthread_t sdl_main_thread_;
int width_;
int height_;
static void* sdl_thread(void* param) {
digger_main(0, NULL);
return NULL;
}
};
class PepperModule : public pp::Module {
public:
// Create and return a PluginInstanceInstance object.
virtual pp::Instance* CreateInstance(PP_Instance instance) {
return new PluginInstance(instance);
}
};
namespace pp {
Module* CreateModule() {
return new PepperModule();
}
} // namespace pp