3 * Copyright (c) 2012 Jakob Leben & Tim Blechmann
4 * http://www.audiosynth.com
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <QtNetwork/QLocalSocket>
25 #include <yaml-cpp/emitter.h>
27 #include "PyrPrimitive.h"
30 #include "PyrKernel.h"
31 #include "PyrSymbol.h"
36 QLocalSocket
* mSocket
;
37 SCIpcClient( const char * ideName
):
40 mSocket
= new QLocalSocket();
41 mSocket
->connectToServer(QString(ideName
));
44 void send(const char * data
, size_t length
)
46 mSocket
->write(data
, length
);
51 mSocket
->disconnectFromServer();
55 static SCIpcClient
* gIpcClient
= NULL
;
58 int ScIDE_Connect(struct VMGlobals
*g
, int numArgsPushed
)
61 error("ScIDE already connected\n");
65 PyrSlot
* ideNameSlot
= g
->sp
;
68 int status
= slotStrVal(ideNameSlot
, ideName
, 1024);
69 if (status
!= errNone
)
72 gIpcClient
= new SCIpcClient(ideName
);
77 int ScIDE_Connected(struct VMGlobals
*g
, int numArgsPushed
)
79 PyrSlot
* returnSlot
= g
->sp
- numArgsPushed
+ 1;
81 SetBool(returnSlot
, gIpcClient
!= 0);
88 YAML::Emitter emitter
;
91 explicit YAMLSerializer (PyrSlot
* slot
)
98 return emitter
.c_str();
103 return emitter
.size();
107 void serialize(PyrSlot
* slot
)
110 emitter
<< slotRawFloat(slot
);
114 switch (GetTag(slot
)) {
116 emitter
<< YAML::Null
;
120 emitter
<< slotRawInt(slot
);
132 serialize(slotRawObject(slot
));
136 emitter
<< YAML::DoubleQuoted
<< slotRawSymbol(slot
)->name
;
140 printf ("type: %d\n", GetTag(slot
));
141 throw std::runtime_error("YAMLSerializer: not implementation for this type");
145 void serialize(PyrObject
* object
)
147 if (isKindOf(object
, class_string
)) {
148 PyrObjectHdr
* hdr
= static_cast<PyrObjectHdr
*>(object
);
149 PyrString
* str
= static_cast<PyrString
*>(hdr
);
151 size_t len
= str
->size
;
152 char * cstr
= new char[len
+ 10];
153 memcpy(cstr
, str
->s
, len
);
154 cstr
[len
] = 0; // zero-terminate
155 emitter
<< YAML::DoubleQuoted
<< cstr
;
160 if (isKindOf(object
, class_arrayed_collection
)) {
161 emitter
<< YAML::BeginSeq
;
162 for (size_t i
= 0; i
!= object
->size
; ++i
)
163 serialize(object
->slots
+ i
);
165 emitter
<< YAML::EndSeq
;
168 throw std::runtime_error("YAMLSerializer: not implementation for this type");
172 int ScIDE_Send(struct VMGlobals
*g
, int numArgsPushed
)
175 error("ScIDE not connected\n");
179 PyrSlot
* idSlot
= g
->sp
- 1;
181 if (slotStrVal( idSlot
, id
, 255 ))
184 PyrSlot
* argSlot
= g
->sp
;
187 YAMLSerializer
serializer(argSlot
);
189 QDataStream
stream(gIpcClient
->mSocket
);
190 stream
.setVersion(QDataStream::Qt_4_6
);
191 stream
<< QString(id
);
192 stream
<< QString::fromUtf8(serializer
.data());
193 } catch (std::exception
const & e
) {
194 postfl("Exception during ScIDE_Send: %s\n", e
.what());
202 void initScIDEPrimitives()
204 int base
= nextPrimitiveIndex();
206 definePrimitive(base
, index
++, "_ScIDE_Connect", ScIDE_Connect
, 2, 0);
207 definePrimitive(base
, index
++, "_ScIDE_Connected", ScIDE_Connected
, 1, 0);
208 definePrimitive(base
, index
++, "_ScIDE_Send", ScIDE_Send
, 3, 0);