New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reimplement EditBox for android #3534

Merged
merged 0 commits into from Oct 29, 2013

Conversation

Projects
None yet
3 participants
@ledyba
Copy link
Contributor

ledyba commented Sep 3, 2013

In cocos2d-x in 3, implementation of "EditBox" for Android is broken.

So, I reimplemented that. Here are the points.

( I sent this pull request before, but Travis CI has crashed, so I resend this. )

Create Cocos2dxHelperListenerImpl

In version 2, Cocos2dxActivity implements Cocos2dxHelperListener, but that activity does not exist any more.

So I create the classs "Cocos2dxHelperListenerImpl" that implements Cocos2dxHelperListener.

This class is used only in Cocos2dxHelper, so I locate that class in Cocos2dxHelper.java.

Use std::atomic to send modified text instead of executing function on GLThread.

In version 2, Cocos2dxActivity used GLSurfaceView#queueEvent (in runOnGLThread) to invoke native funtion that update native EditBox object, but in version 3, we do not used GLSurfaceView.

There is no way to invoke any function under cocos2d-x loop thread ( just like Android Handler class), so I use std::atomic to send the modified text to cocos2d-x main loop thread.

thanks.

@folecr

This comment has been minimized.

Copy link
Contributor

folecr commented Sep 3, 2013

@ledyba the more intuitive solution might be to pass a message to the cocos thread like discussed briefly here
https://groups.google.com/forum/#!topic/android-ndk/v2OITtaZTes

And also adding a generic API so that multiple parts of the system : text input, plugins, game code, etc. may pass messages to the cocos thread from other threads. If we can use C++ lambdas as the payload of the message the API would be clean.

Would you like to try this for CCEditBox?

@ledyba

This comment has been minimized.

Copy link
Contributor

ledyba commented Sep 4, 2013

Thank you for your comment!

There is no guarantee that std::function can be passed through pipe.
For example, this code always crashes.

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <iostream>
#include <cstdlib>
#include <functional>

void pushFunction(int fd)
{
    std::string message = "hello";
    std::function<std::string()> f = [message](){
        return message; // message is "copy captured".
    };
    write(fd, &f, sizeof(f));

}

int main()
{
    int     fd[2];
    pipe( fd );

    pushFunction(fd[1]);
    std::function<std::string()>* fptr =
        reinterpret_cast<std::function<std::string()>*>( malloc(sizeof(std::function<std::string()>)) );
    read(fd[0], fptr, sizeof(*fptr));
    std::cout << "Function call: " << (*fptr)() <<std::endl;
    close(fd[0]);
    close(fd[1]);
    free(fptr);
    return 0;
}

http://ideone.com/ESrHgm

But, we can make the thread-safe queue with std::deque and std::mutex.
I would like to implement that queue and use for message passing in CCEditBox.
Should that thread safe queue (CCThreadSafeQueue?) be global? Or should that queue be used only in CCEditBox?

@folecr

This comment has been minimized.

Copy link
Contributor

folecr commented Sep 4, 2013

But, we can make the thread-safe queue with std::deque and std::mutex.

Yes.

I would like to implement that queue and use for message passing in CCEditBox.

Great!

Should that thread safe queue (CCThreadSafeQueue?) be global? Or should that queue be used only in CCEditBox?

Currently there are two use cases that I know of

  1. Edit Box
  2. Plugins (such as twitter, admobs, etc. that will callback into cocos)

I would suggest adding a cocos::android::Queue
with methods

  1. for adding a function to this queue to be used by any thread and which will send a message via fd and using the ALooper
  2. and to pop a function (cocos thread only) - reading from this queue to be done in nativeactivity.cpp when the fd is notified.
@ledyba

This comment has been minimized.

Copy link
Contributor

ledyba commented Sep 7, 2013

Thank you for your advice!

We cannot use ALooper because ALooper use FileDescriptor, on witch we cannot send std::function.
So, I created "cocos2d::android::Queue" and "cocos2d::android::Executor".

"Queue" is a thread-safe queue. We can send std::string, Object_, Array_, int ,float, std::function<void()> on this queue.CCEditBoxImplAndroid use this.

// on remote thread
cocos2d::android::Queue queue;
queue.push( EVENT_ID_SEND_STRING, "string message" ); //you can use any integer as event id.

//on main loop thread.
cocos2d::android::Queue::Payload p;
while( queue.pop(p) ){
    println("message code: %d\n", p.code());
    println("message: %s\n", p.as<std::string>().c_str());
    //-> "message:  string message"
}

Executor is a class equavalent to "Handler" in Java. You can execute arbitrary function on cocos2d main loop thread.

Example:

// on remote thread.
cocos2d::android::Executor::getInstance()->post([](){
   // this lambda expression runs on main loop thread.
    printf("Hello world on cocos2d thread!");
});

And to tell you the truth, both Queue and Executor is available not only android, but also other platforms.

@minggo minggo merged commit ef636b6 into cocos2d:develop Oct 29, 2013

dumganhar pushed a commit that referenced this pull request Dec 27, 2013

James Chen
Merge pull request #4513 from gamechefs/dcurlfix
closed #3534: Fixes to asset manager to ensure there is no crash on DNS resolver as well as no hanging if connection breaks during download

angeltown pushed a commit to angeltown/cocos2d-x that referenced this pull request Apr 28, 2014

James Chen
Merge pull request cocos2d#4513 from gamechefs/dcurlfix
closed cocos2d#3534: Fixes to asset manager to ensure there is no crash on DNS resolver as well as no hanging if connection breaks during download

angeltown pushed a commit to angeltown/cocos2d-x that referenced this pull request Apr 29, 2014

James Chen
Merge pull request cocos2d#4513 from gamechefs/dcurlfix
closed cocos2d#3534: Fixes to asset manager to ensure there is no crash on DNS resolver as well as no hanging if connection breaks during download

angeltown pushed a commit to angeltown/cocos2d-x that referenced this pull request Apr 30, 2014

James Chen
Merge pull request cocos2d#4513 from gamechefs/dcurlfix
closed cocos2d#3534: Fixes to asset manager to ensure there is no crash on DNS resolver as well as no hanging if connection breaks during download

angeltown pushed a commit to angeltown/cocos2d-x that referenced this pull request Apr 30, 2014

James Chen
Merge pull request cocos2d#4513 from gamechefs/dcurlfix
closed cocos2d#3534: Fixes to asset manager to ensure there is no crash on DNS resolver as well as no hanging if connection breaks during download
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment