/// Sets up a ScrollView window, depending on the constructor variables. void ScrollView::Initialize(const char* name, int x_pos, int y_pos, int x_size, int y_size, int x_canvas_size, int y_canvas_size, bool y_axis_reversed, const char* server_name) { // If this is the first ScrollView Window which gets created, there is no // network connection yet and we have to set it up in a different thread. if (stream_ == nullptr) { nr_created_windows_ = 0; stream_ = new SVNetwork(server_name, kSvPort); waiting_for_events_mu = new SVMutex(); svmap_mu = new SVMutex(); SendRawMessage( "svmain = luajava.bindClass('com.google.scrollview.ScrollView')\n"); SVSync::StartThread(MessageReceiver, nullptr); } // Set up the variables on the clientside. nr_created_windows_++; event_handler_ = nullptr; event_handler_ended_ = false; y_axis_is_reversed_ = y_axis_reversed; y_size_ = y_canvas_size; window_name_ = name; window_id_ = nr_created_windows_; // Set up polygon buffering. points_ = new SVPolyLineBuffer; points_->empty = true; svmap_mu->Lock(); svmap[window_id_] = this; svmap_mu->Unlock(); for (int i = 0; i < SVET_COUNT; i++) { event_table_[i] = nullptr; } mutex_ = new SVMutex(); semaphore_ = new SVSemaphore(); // Set up an actual Window on the client side. char message[kMaxMsgSize]; snprintf(message, sizeof(message), "w%u = luajava.newInstance('com.google.scrollview.ui" ".SVWindow','%s',%u,%u,%u,%u,%u,%u,%u)\n", window_id_, window_name_, window_id_, x_pos, y_pos, x_size, y_size, x_canvas_size, y_canvas_size); SendRawMessage(message); SVSync::StartThread(StartEventHandler, this); }
// Send the current buffered polygon (if any) and clear it. void ScrollView::SendPolygon() { if (!points_->empty) { points_->empty = true; // Allows us to use SendMsg. int length = points_->xcoords.size(); // length == 1 corresponds to 2 SetCursors in a row and only the // last setCursor has any effect. if (length == 2) { // An isolated line! SendMsg("drawLine(%d,%d,%d,%d)", points_->xcoords[0], points_->ycoords[0], points_->xcoords[1], points_->ycoords[1]); } else if (length > 2) { // A polyline. SendMsg("createPolyline(%d)", length); char coordpair[kMaxIntPairSize]; std::string decimal_coords; for (int i = 0; i < length; ++i) { snprintf(coordpair, kMaxIntPairSize, "%d,%d,", points_->xcoords[i], points_->ycoords[i]); decimal_coords += coordpair; } decimal_coords += '\n'; SendRawMessage(decimal_coords.c_str()); SendMsg("drawPolyline()"); } points_->xcoords.clear(); points_->ycoords.clear(); } }
already_AddRefed<Promise> JSWindowActor::SendQuery( JSContext* aCx, const nsAString& aMessageName, JS::Handle<JS::Value> aObj, JS::Handle<JS::Value> aTransfers, ErrorResult& aRv) { ipc::StructuredCloneData data; if (!aObj.isUndefined() && !nsFrameMessageManager::GetParamsForMessage( aCx, aObj, aTransfers, data)) { aRv.Throw(NS_ERROR_DOM_DATA_CLONE_ERR); return nullptr; } nsIGlobalObject* global = xpc::CurrentNativeGlobal(aCx); if (NS_WARN_IF(!global)) { aRv.Throw(NS_ERROR_UNEXPECTED); return nullptr; } RefPtr<Promise> promise = Promise::Create(global, aRv); if (NS_WARN_IF(aRv.Failed())) { return nullptr; } JSWindowActorMessageMeta meta; meta.actorName() = mName; meta.messageName() = aMessageName; meta.queryId() = mNextQueryId++; meta.kind() = JSWindowActorMessageKind::Query; mPendingQueries.Put(meta.queryId(), promise); SendRawMessage(meta, std::move(data), aRv); return promise.forget(); }
// Sends for each pixel either '1' or '0'. void ScrollView::TransferGrayImage(PIX* image) { char* pixel_data = new char[image->w * 2 + 2]; for (int y = 0; y < image->h; y++) { l_uint32* data = pixGetData(image) + y * pixGetWpl(image); for (int x = 0; x < image->w; x++) { snprintf(&pixel_data[x*2], 2, "%.2x", (GET_DATA_BYTE(data, x))); pixel_data[image->w * 2] = '\n'; pixel_data[image->w * 2 + 1] = '\0'; SendRawMessage(pixel_data); } } delete [] pixel_data; }
// Sends for each pixel either '1' or '0'. void ScrollView::TransferBinaryImage(PIX* image) { char* pixel_data = new char[image->w + 2]; for (int y = 0; y < image->h; y++) { l_uint32* data = pixGetData(image) + y * pixGetWpl(image); for (int x = 0; x < image->w; x++) { if (GET_DATA_BIT(data, x)) pixel_data[x] = '1'; else pixel_data[x] = '0'; } pixel_data[image->w] = '\n'; pixel_data[image->w + 1] = '\0'; SendRawMessage(pixel_data); } delete [] pixel_data; }
void JSWindowActor::SendAsyncMessage(JSContext* aCx, const nsAString& aMessageName, JS::Handle<JS::Value> aObj, JS::Handle<JS::Value> aTransfers, ErrorResult& aRv) { ipc::StructuredCloneData data; if (!aObj.isUndefined() && !nsFrameMessageManager::GetParamsForMessage( aCx, aObj, aTransfers, data)) { aRv.Throw(NS_ERROR_DOM_DATA_CLONE_ERR); return; } JSWindowActorMessageMeta meta; meta.actorName() = mName; meta.messageName() = aMessageName; meta.kind() = JSWindowActorMessageKind::Message; SendRawMessage(meta, std::move(data), aRv); }
// Sends each pixel as hex value like html, e.g. #00FF00 for green. void ScrollView::Transfer32bppImage(PIX* image) { int ppL = pixGetWidth(image); int h = pixGetHeight(image); int wpl = pixGetWpl(image); int transfer_size= ppL * 7 + 2; char* pixel_data = new char[transfer_size]; for (int y = 0; y < h; ++y) { l_uint32* data = pixGetData(image) + y*wpl; for (int x = 0; x < ppL; ++x, ++data) { snprintf(&pixel_data[x*7], 7, "#%.2x%.2x%.2x", GET_DATA_BYTE(data, COLOR_RED), GET_DATA_BYTE(data, COLOR_GREEN), GET_DATA_BYTE(data, COLOR_BLUE)); } pixel_data[transfer_size - 2] = '\n'; pixel_data[transfer_size - 1] = '\0'; SendRawMessage(pixel_data); } delete[] pixel_data; }
// Send an image of type Pix. void ScrollView::Image(struct Pix* image, int x_pos, int y_pos) { l_uint8* data; size_t size; pixWriteMem(&data, &size, image, IFF_PNG); int base64_len = (size + 2) / 3 * 4; y_pos = TranslateYCoordinate(y_pos); SendMsg("readImage(%d,%d,%d)", x_pos, y_pos, base64_len); // Base64 encode the data. const char kBase64Table[64] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', }; char* base64 = new char[base64_len + 1]; memset(base64, '=', base64_len); base64[base64_len] = '\0'; int remainder = 0; int bits_left = 0; int code_len = 0; for (size_t i = 0; i < size; ++i) { int code = (data[i] >> (bits_left + 2)) | remainder; base64[code_len++] = kBase64Table[code & 63]; bits_left += 2; remainder = data[i] << (6 - bits_left); if (bits_left == 6) { base64[code_len++] = kBase64Table[remainder & 63]; bits_left = 0; remainder = 0; } } if (bits_left > 0) base64[code_len++] = kBase64Table[remainder & 63]; SendRawMessage(base64); delete [] base64; lept_free(data); }
// Exit the client completely (and notify the server of it). void ScrollView::Exit() { SendRawMessage("svmain:exit()"); exit(0); }
void Bot::SendMessage(const QString& receiver, const char *message) { QString msg = "PRIVMSG " + receiver + " :" + message; SendRawMessage(msg); }