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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
|
/*
SuperCollider real time audio synthesis system
Copyright (c) 2002 James McCartney. All rights reserved.
http://www.audiosynth.com
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
// ********** this version for windows and linux. for mac see UIUGens.mm
#include <SC_Lock.h>
#include <atomic>
#ifndef _WIN32
# include <X11/Intrinsic.h>
#else
# include "SC_Win32Utils.h"
# include <windows.h>
#endif
#include "SC_PlugIn.h"
static InterfaceTable* ft;
struct KeyboardUGenGlobalState {
uint8 keys[32];
} gKeyStateGlobals;
struct KeyState : public Unit {
float m_y1, m_b1, m_lag;
};
struct MouseUGenGlobalState {
float mouseX, mouseY;
bool mouseButton;
} gMouseUGenGlobals;
struct MouseInputUGen : public Unit {
float m_y1, m_b1, m_lag;
};
//////////////////////////////////////////////////////////////////////////////////////////////////
std::atomic_bool inputThreadRunning = { false };
#ifdef _WIN32
void gstate_update_func() {
POINT p;
int mButton;
if (GetSystemMetrics(SM_SWAPBUTTON))
mButton = VK_RBUTTON; // if swapped
else
mButton = VK_LBUTTON; // not swapped (normal)
int screenWidth = GetSystemMetrics(SM_CXSCREEN);
int screenHeight = GetSystemMetrics(SM_CYSCREEN);
// default: SM_CX/CYSCREEN gets the size of a primary screen.
// lines uncommented below are just for a specially need on multi-display.
// int screenWidth = GetSystemMetrics( SM_CXVIRTUALSCREEN );
// int screenHeight = GetSystemMetrics( SM_CYVIRTUALSCREEN );
float r_screenWidth = 1.f / (float)(screenWidth - 1);
float r_screenHeight = 1.f / (float)(screenHeight - 1);
while (inputThreadRunning.load(std::memory_order_relaxed)) {
// "KeyState" is disabled for now, on Windows...
// GetKey((long*)gstate->keys);
GetCursorPos(&p);
gMouseUGenGlobals.mouseX = (float)p.x * r_screenWidth;
gMouseUGenGlobals.mouseY = 1.f - (float)p.y * r_screenHeight;
gMouseUGenGlobals.mouseButton = (GetKeyState(mButton) < 0);
std::this_thread::sleep_for(std::chrono::milliseconds(17));
}
}
#else
static Display* d = 0;
void gstate_update_func() {
Window r;
struct timespec requested_time, remaining_time;
// NOTE: should not be required as this is the only thread accessing the x11 API
// but omitting seems to cause troubles.
XInitThreads();
d = XOpenDisplay(NULL);
if (!d)
return;
Window rep_root, rep_child;
XWindowAttributes attributes;
int rep_rootx, rep_rooty;
unsigned int rep_mask;
int dx, dy;
float r_width;
float r_height;
r = DefaultRootWindow(d);
XGetWindowAttributes(d, r, &attributes);
r_width = 1.0 / (float)attributes.width;
r_height = 1.0 / (float)attributes.height;
while (inputThreadRunning.load(std::memory_order_relaxed)) {
XQueryKeymap(d, (char*)(gKeyStateGlobals.keys));
XQueryPointer(d, r, &rep_root, &rep_child, &rep_rootx, &rep_rooty, &dx, &dy, &rep_mask);
gMouseUGenGlobals.mouseX = (float)dx * r_width;
gMouseUGenGlobals.mouseY = 1.f - ((float)dy * r_height);
gMouseUGenGlobals.mouseButton = (bool)(rep_mask & Button1Mask);
std::this_thread::sleep_for(std::chrono::milliseconds(17));
}
}
#endif
//////////////////////////////////////////////////////////////////////////////////////////////////
void KeyState_next(KeyState* unit, int inNumSamples) {
// minval, maxval, warp, lag
uint8* keys = (uint8*)gKeyStateGlobals.keys;
int keynum = (int)ZIN0(0);
int byte = (keynum >> 3) & 31;
int bit = keynum & 7;
int val = keys[byte] & (1 << bit);
float minval = ZIN0(1);
float maxval = ZIN0(2);
float lag = ZIN0(3);
float y1 = unit->m_y1;
float b1 = unit->m_b1;
if (lag != unit->m_lag) {
unit->m_b1 = lag == 0.f ? 0.f : exp(log001 / (lag * unit->mRate->mSampleRate));
unit->m_lag = lag;
}
float y0 = val ? maxval : minval;
ZOUT0(0) = y1 = y0 + b1 * (y1 - y0);
unit->m_y1 = zapgremlins(y1);
}
void KeyState_Ctor(KeyState* unit) {
SETCALC(KeyState_next);
unit->m_b1 = 0.f;
unit->m_lag = 0.f;
KeyState_next(unit, 1);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
void MouseX_next(MouseInputUGen* unit, int inNumSamples) {
// minval, maxval, warp, lag
float minval = ZIN0(0);
float maxval = ZIN0(1);
float warp = ZIN0(2);
float lag = ZIN0(3);
float y1 = unit->m_y1;
float b1 = unit->m_b1;
if (lag != unit->m_lag) {
unit->m_b1 = lag == 0.f ? 0.f : (float)exp(log001 / (lag * unit->mRate->mSampleRate));
unit->m_lag = lag;
}
float y0 = gMouseUGenGlobals.mouseX;
if (warp == 0.0) {
y0 = (maxval - minval) * y0 + minval;
} else {
y0 = pow(maxval / minval, y0) * minval;
}
ZOUT0(0) = y1 = y0 + b1 * (y1 - y0);
unit->m_y1 = zapgremlins(y1);
}
void MouseX_Ctor(MouseInputUGen* unit) {
SETCALC(MouseX_next);
unit->m_b1 = 0.f;
unit->m_lag = 0.f;
MouseX_next(unit, 1);
}
void MouseY_next(MouseInputUGen* unit, int inNumSamples) {
// minval, maxval, warp, lag
float minval = ZIN0(0);
float maxval = ZIN0(1);
float warp = ZIN0(2);
float lag = ZIN0(3);
float y1 = unit->m_y1;
float b1 = unit->m_b1;
if (lag != unit->m_lag) {
unit->m_b1 = lag == 0.f ? 0.f : (float)exp(log001 / (lag * unit->mRate->mSampleRate));
unit->m_lag = lag;
}
float y0 = gMouseUGenGlobals.mouseY;
if (warp == 0.0) {
y0 = (maxval - minval) * y0 + minval;
} else {
y0 = pow(maxval / minval, y0) * minval;
}
ZOUT0(0) = y1 = y0 + b1 * (y1 - y0);
unit->m_y1 = zapgremlins(y1);
}
void MouseY_Ctor(MouseInputUGen* unit) {
SETCALC(MouseY_next);
unit->m_b1 = 0.f;
unit->m_lag = 0.f;
MouseY_next(unit, 1);
}
void MouseButton_next(MouseInputUGen* unit, int inNumSamples) {
// minval, maxval, warp, lag
float minval = ZIN0(0);
float maxval = ZIN0(1);
float lag = ZIN0(2);
float y1 = unit->m_y1;
float b1 = unit->m_b1;
if (lag != unit->m_lag) {
unit->m_b1 = lag == 0.f ? 0.f : (float)exp(log001 / (lag * unit->mRate->mSampleRate));
unit->m_lag = lag;
}
float y0 = gMouseUGenGlobals.mouseButton ? maxval : minval;
ZOUT0(0) = y1 = y0 + b1 * (y1 - y0);
unit->m_y1 = zapgremlins(y1);
}
void MouseButton_Ctor(MouseInputUGen* unit) {
SETCALC(MouseButton_next);
unit->m_b1 = 0.f;
unit->m_lag = 0.f;
MouseButton_next(unit, 1);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
// example of implementing a plug in command with async execution.
struct MyPluginData // data for the global instance of the plugin
{
float a, b;
};
struct MyCmdData // data for each command
{
MyPluginData* myPlugin;
float x, y;
char* name;
};
MyPluginData gMyPlugin; // global
bool cmdStage2(World* world, void* inUserData) {
// user data is the command.
MyCmdData* myCmdData = (MyCmdData*)inUserData;
// just print out the values
Print("cmdStage2 a %g b %g x %g y %g name %s\n", myCmdData->myPlugin->a, myCmdData->myPlugin->b, myCmdData->x,
myCmdData->y, myCmdData->name);
return true;
}
bool cmdStage3(World* world, void* inUserData) {
// user data is the command.
MyCmdData* myCmdData = (MyCmdData*)inUserData;
// just print out the values
Print("cmdStage3 a %g b %g x %g y %g name %s\n", myCmdData->myPlugin->a, myCmdData->myPlugin->b, myCmdData->x,
myCmdData->y, myCmdData->name);
// scsynth will perform completion message after this returns
return true;
}
bool cmdStage4(World* world, void* inUserData) {
// user data is the command.
MyCmdData* myCmdData = (MyCmdData*)inUserData;
// just print out the values
Print("cmdStage4 a %g b %g x %g y %g name %s\n", myCmdData->myPlugin->a, myCmdData->myPlugin->b, myCmdData->x,
myCmdData->y, myCmdData->name);
// scsynth will send /done after this returns
return true;
}
void cmdCleanup(World* world, void* inUserData) {
// user data is the command.
MyCmdData* myCmdData = (MyCmdData*)inUserData;
Print("cmdCleanup a %g b %g x %g y %g name %s\n", myCmdData->myPlugin->a, myCmdData->myPlugin->b, myCmdData->x,
myCmdData->y, myCmdData->name);
RTFree(world, myCmdData->name); // free the string
RTFree(world, myCmdData); // free command data
// scsynth will delete the completion message for you.
}
void cmdDemoFunc(World* inWorld, void* inUserData, struct sc_msg_iter* args, void* replyAddr) {
Print("->cmdDemoFunc %p\n", inUserData);
// user data is the plug-in's user data.
MyPluginData* thePlugInData = (MyPluginData*)inUserData;
// allocate command data, free it in cmdCleanup.
MyCmdData* myCmdData = (MyCmdData*)RTAlloc(inWorld, sizeof(MyCmdData));
myCmdData->myPlugin = thePlugInData;
// ..get data from args..
myCmdData->x = 0.;
myCmdData->y = 0.;
myCmdData->name = 0;
// float arguments
myCmdData->x = args->getf();
myCmdData->y = args->getf();
// how to pass a string argument:
const char* name = args->gets(); // get the string argument
if (name) {
myCmdData->name = (char*)RTAlloc(inWorld, strlen(name) + 1); // allocate space, free it in cmdCleanup.
strcpy(myCmdData->name, name); // copy the string
}
// how to pass a completion message
int msgSize = args->getbsize();
char* msgData = 0;
if (msgSize) {
// allocate space for completion message
// scsynth will delete the completion message for you.
msgData = (char*)RTAlloc(inWorld, msgSize);
args->getb(msgData, msgSize); // copy completion message.
}
DoAsynchronousCommand(inWorld, replyAddr, "cmdDemoFunc", (void*)myCmdData, (AsyncStageFn)cmdStage2,
(AsyncStageFn)cmdStage3, (AsyncStageFn)cmdStage4, cmdCleanup, msgSize, msgData);
Print("<-cmdDemoFunc\n");
}
/*
* to test the above, send the server these commands:
*
*
* SynthDef(\sine, { Out.ar(0, SinOsc.ar(800,0,0.2)) }).load(s);
* s.sendMsg(\cmd, \pluginCmdDemo, 7, 9, \mno, [\s_new, \sine, 900, 0, 0]);
* s.sendMsg(\n_free, 900);
* s.sendMsg(\cmd, \pluginCmdDemo, 7, 9, \mno);
* s.sendMsg(\cmd, \pluginCmdDemo, 7, 9);
* s.sendMsg(\cmd, \pluginCmdDemo, 7);
* s.sendMsg(\cmd, \pluginCmdDemo);
*
*/
SC_Thread uiListenThread;
PluginLoad(UIUGens) {
ft = inTable;
inputThreadRunning = true;
uiListenThread = std::thread(gstate_update_func);
DefineSimpleUnit(KeyState);
DefineUnit("MouseX", sizeof(MouseInputUGen), (UnitCtorFunc)&MouseX_Ctor, 0, 0);
DefineUnit("MouseY", sizeof(MouseInputUGen), (UnitCtorFunc)&MouseY_Ctor, 0, 0);
DefineUnit("MouseButton", sizeof(MouseInputUGen), (UnitCtorFunc)&MouseButton_Ctor, 0, 0);
// define a plugin command - example code
gMyPlugin.a = 1.2f;
gMyPlugin.b = 3.4f;
DefinePlugInCmd("pluginCmdDemo", cmdDemoFunc, (void*)&gMyPlugin);
}
C_LINKAGE SC_API_EXPORT void unload(InterfaceTable* inTable) {
inputThreadRunning = false;
uiListenThread.join();
#ifndef _WIN32
if (d)
XCloseDisplay(d);
#endif
}
|