-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGazify.cpp
212 lines (182 loc) · 7.04 KB
/
Gazify.cpp
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/**********************************************************\
Auto-generated Gazify.cpp
This file contains the auto-generated main plugin object
implementation for the Gazify project
\**********************************************************/
#include "GazifyAPI.h"
#include "DOM.h"
#include "variant_list.h"
#include "coordinate_server.h"
#include "Gazify.h"
#include "spotify.h"
#include "audio.h"
#include "coordinate_server.h"
#include "base64.h"
#define USE_REMOTE 1
using namespace FB;
///////////////////////////////////////////////////////////////////////////////
/// @fn Gazify::StaticInitialize()
///
/// @brief Called from PluginFactory::globalPluginInitialize()
///
/// @see FB::FactoryBase::globalPluginInitialize
///////////////////////////////////////////////////////////////////////////////
void Gazify::StaticInitialize()
{
// Place one-time initialization stuff here; As of FireBreath 1.4 this should only
// be called once per process
}
///////////////////////////////////////////////////////////////////////////////
/// @fn Gazify::StaticInitialize()
///
/// @brief Called from PluginFactory::globalPluginDeinitialize()
///
/// @see FB::FactoryBase::globalPluginDeinitialize
///////////////////////////////////////////////////////////////////////////////
void Gazify::StaticDeinitialize()
{
// Place one-time deinitialization stuff here. As of FireBreath 1.4 this should
// always be called just before the plugin library is unloaded
spotify_exit();
}
///////////////////////////////////////////////////////////////////////////////
/// @brief Gazify constructor. Note that your API is not available
/// at this point, nor the window. For best results wait to use
/// the JSAPI object until the onPluginReady method is called
///////////////////////////////////////////////////////////////////////////////
Gazify::Gazify()
{
}
///////////////////////////////////////////////////////////////////////////////
/// @brief Gazify destructor.
///////////////////////////////////////////////////////////////////////////////
Gazify::~Gazify()
{
// This is optional, but if you reset m_api (the shared_ptr to your JSAPI
// root object) and tell the host to free the retained JSAPI objects then
// unless you are holding another shared_ptr reference to your JSAPI object
// they will be released here.
releaseRootJSAPI();
m_host->freeRetainedObjects();
}
void Gazify::postTrackInfo(std::string artist,std::string album,std::string title,const void *data,size_t img_size)
{
if(!m_host->isMainThread()) {
try {
m_host->CallOnMainThread(boost::bind(&Gazify::postTrackInfo, this, artist, album,title,data,img_size));
} catch (...) {
}
return;
}
std::string cover_base64 = base64_encode((unsigned char const*)data , img_size);
DOM::DocumentPtr doc = m_host->getDOMDocument();
doc->callMethod<void>("gazeInfo", variant_list_of(artist)(album)(title)(cover_base64));
}
void Gazify::gaze(int x,int y)
{
printf("gaze %d %d\n", x, y);
if(!m_host->isMainThread()) {
try {
m_host->CallOnMainThread(boost::bind(&Gazify::gaze, this, x, y));
} catch (...) {
}
return;
}
DOM::DocumentPtr doc = m_host->getDOMDocument();
doc->callMethod<void>("gaze", variant_list_of(x)(y));
}
void Gazify::onPluginReady()
{
// When this is called, the BrowserHost is attached, the JSAPI object is
// created, and we are ready to interact with the page and such. The
// PluginWindow may or may not have already fire the AttachedEvent at
// this point.
#ifdef USE_REMOTE
pthread_t p;
pthread_create(&p,NULL,&coordiate_thread,this);
#endif
spotify_init(this, SPOTIFY_USER,SPOTIFY_PASSWORD);
}
///////////////////////////////////////////////////////////////////////////////
/// @brief Creates an instance of the JSAPI object that provides your main
/// Javascript interface.
///
/// Note that m_host is your BrowserHost and shared_ptr returns a
/// FB::PluginCorePtr, which can be used to provide a
/// boost::weak_ptr<Gazify> for your JSAPI class.
///
/// Be very careful where you hold a shared_ptr to your plugin class from,
/// as it could prevent your plugin class from getting destroyed properly.
///////////////////////////////////////////////////////////////////////////////
FB::JSAPIPtr Gazify::createJSAPI()
{
// m_host is the BrowserHost
return boost::make_shared<GazifyAPI>(FB::ptr_cast<Gazify>(shared_from_this()), m_host);
}
bool Gazify::onMouseDown(FB::MouseDownEvent *evt, FB::PluginWindow *)
{
return false;
}
bool Gazify::onMouseUp(FB::MouseUpEvent *evt, FB::PluginWindow *)
{
return false;
}
bool Gazify::onMouseMove(FB::MouseMoveEvent *evt, FB::PluginWindow *)
{
return false;
}
bool Gazify::onWindowAttached(FB::AttachedEvent *evt, FB::PluginWindow *)
{
// The window is attached; act appropriately
return false;
}
bool Gazify::onWindowDetached(FB::DetachedEvent *evt, FB::PluginWindow *)
{
// The window is about to be detached; act appropriately
return false;
}
std::string Gazify::trackAtScreenCoordinates(float x, float y)
{
/*
std::string ret = "";
DOM::WindowPtr win = m_host->getDOMWindow();
int win_x = win->getProperty<int>("screenX");
int win_y = win->getProperty<int>("screenY");
int win_w = win->getProperty<int>("innerWidth");
int win_h = win->getProperty<int>("innerHeight");
fprintf(stderr,"inp: %d,%d and %d,%d,%d,%d\n",(int)x,(int)y,win_x,win_y,win_w,win_h);
x -= win_x;
y -= win_y;
if(x < 0 || y < 0 || x > win_w || y > win_h)
{
printf("Coordinate outside window\n");
}
x += win->getProperty<int>("pageXOffset");
y += win->getProperty<int>("pageYOffset");
printf("PageXOffset: %d, YOff: %d\n",win->getProperty<int>("pageXOffset"),win->getProperty<int>("pageYOffset"));
printf("Window coordinates: %d,%d\n",(int)x,(int)y);
JSObjectPtr elementUnderGazeJs = doc->callMethod<JSObjectPtr>("elementFromPoint", variant_list_of(x)(y));
//DEBUG
FB::DOM::ElementPtr blobb = doc->getElementById("red_blobb");
printf("style: %s\n",blobb->getStringAttribute("class").c_str());
blobb->setProperty("real_left","100px");
blobb->setProperty("real_top","100px");
// printf("%d\n",blobb->getWidth());
// blobb->setInnerHTML("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
// printf("TYPE: %s\n",blobb->getProperty("nodeType").c_str());
// blobb->setProperty("top",y);
int depth = 0;
while (elementUnderGazeJs && depth < 100) { // walking up the tree looking for gazemusic attr
DOM::ElementPtr elementUnderGaze = DOM::Element::create(elementUnderGazeJs);
if(elementUnderGaze->callMethod<bool>("hasAttribute", variant_list_of("gazemusic"))) {
try {
ret = elementUnderGaze->getStringAttribute("gazemusic");
if(!ret.empty()) break;
} catch (bad_variant_cast bvc) {}
}
elementUnderGaze = elementUnderGaze->getParentNode();
depth++;
}
return ret;
*/
}