Forum

Results 1 to 9 of 9
  1. #1
    Join Date
    August 2012
    Posts
    8

    How to implementing program loop in client plugin

    I want to make a client plugin that basically is 'active'. What I mean active is it's not only activated when some event occurs in the ts3 client via callbacks functions. It should be able to monitor some external event (eg. serial port, tcp connection) and perform action (calling ts3 clientlib functions) when necessary. I see standar plugin Clientquery perfectly does it.

    In order to achieve that, what cross my mind is that I should be able to implement some sort of 'plugin loop' or message loop, which is always active to monitor certain event and perform actions asynchronously with ts3 client events.
    Should I spawn a new thread in plugin_init() and implement the loop inside it ? or what is the recommended / best way to do that ? is it safe to call ts3 functions inside the thread ?

    can someone reveal at least the skeleton of the ClientQuery plugin ? full source code would be much better :-)

    Thanks in advance.

  2. #2
    Join Date
    November 2011
    Posts
    11
    You can create a thread within the code that's called to initialise the plugin to make a while(1) loop.

    DWORD WINAPI MainThread( LPVOID lpParam );

    int ts3plugin_init()
    {
    //All the SDK init code here
    CreateThread(NULL, 0, MainThread, NULL, 0, NULL);
    }

    DWORD WINAPI MainThread( LPVOID lpParam )
    {
    while(1) //Infinite loop
    {
    //Do your stuff here safely without blocking anything
    }
    }

  3. #3
    Join Date
    January 2010
    Location
    Germany
    Posts
    2,029
    Or you can use the well documented Qt Framework, which would also work on Unix and Mac which above Example does not. If you are going to use Qt you have to make sure to use the same Version as the TS3 Client uses, otherwise you might encounter unexplainable crashes and other problems. Currently TS3 is using Qt 4.7.2.

  4. #4
    Join Date
    August 2012
    Posts
    8
    Thanks for your replies.

    SilentStorm, is there any sample code on using Qt Framework for developing ts3 client plugin ?

  5. #5
    Join Date
    January 2010
    Location
    Germany
    Posts
    2,029
    There is no difference really. In my personal experience with writing Plugins for the TS Client I found that using Qt for certain things just makes the life easier as someone else did much work already and you can just use it, whereas without using Qt you would need to do all the 'dirty work' yourself (like creating and managing Threads in your example).
    You can obviously write Plugins without the use of Qt and they will work just as good.

    Just grab a copy of Qt 4.7.2 (you might have to compile it yourself using the compiler you intend to create your Plugin with) and then inside your Plugin just include whatever Parts of the Framework you need and use them.
    There is a pretty good Documentation available with lots of Examples and so on.

  6. #6
    Join Date
    August 2012
    Posts
    6
    Don't forget the signal/slot of Qt (the main good function from this framework).

    You have several exemples on google about the Thread and espacially with Qt Creator (included in the Qt SDK FrameWork).

    So a little example (without the h page):

    Code:
    #in the main cpp page#
    ...
    void MainProgram::startThread()
    {
         loopThread = new TimeLoopThread(this);
         connect(loopThread, SIGNAL(sig_signalLoop()), this, SLOT(slot_getSignalLoop()));
    
         // To start it
         loopThread->start();
    }
    
    
    #Action for the signal
    void MainProgram::slot_getSignalLoop()
    {
         //Here the action you want
    }
    Now the Thread himself
    Code:
    TimeLoopThread::TimeLoopThread(QObject * parent) : QThread(parent)
    {
    
    }
    
    void TimeLoopThread::run()
    {
         int compteur = 0;
        while(1)
        {
            sleep(1);
            compteur++;
    
            if(compteur == 300)
            {
                emit sig_SignalLoop();
                compteur = 0;
            }
        }
    }
    This is a simple example with a simple Thread construct.
    Make the link with the documentation or/and the information from google (there is several)

  7. #7
    Join Date
    August 2012
    Posts
    8
    Ok. Thanks all. I'm going deeper on it.

  8. #8
    Join Date
    August 2012
    Posts
    8
    Hi, I have all the setup done for developing TS3 plugin with QT 4.7.2 toolchain.

    I need to display some UI when my plugin is active. However, I haven't get the right way to do that.

    Here're what I did:
    1. In ts3plugin_init() I create my plugin thread (QThread descendant), note pluginThread is static global variable in the plugin DLL.

    Code:
    pluginThread=new PluginThread;
    PluginThread->run();
    2. In the run method of the thread I initialize QApplication and show the user interface, note: pluginApplication is public variable of PluginThread class.

    Code:
    void PluginThread::run()
    {
        char ** argv;
        int i=1;
    
        pluginApplication= new QApplication(i,argv);
    
        Dialog dialog;
        dialog.show();
    
        pluginApplication->exec();
    
        printf("Thread ended\n");
    }
    3. In ts3plugin_shutdown in the plugin DLL, I put
    Code:
    pluginThread->pluginApplication->exit(0);
    delete pluginThread->pluginApplication;
    delete pluginThread;
    
    printf("Plugin Shutdown");
    but it doesn't work as I expected:
    When I activate and deactivate the plugin from TeamSpeak's Plugin dialog (by checking and uncheking the checkbox at the left of my plugin). When it's activated for the first time yes the dialog shown. But if I uncheck the checkbox, the dialog isn't closed automatically, and "Plugin Shutdown" is not written in the console (means ts3plugin_shutdown is not called ?)

    What I expect is that the dialog is created/shown if the checkbox checked. And dissapear/destroyed when it's unchecked. I've seen a few other plugin succesfully do that.

    The questions are:
    1. It's the right way to initialize QApplication in the plugin thread ?
    2. What function / callback called if checkbox of the plugin checked or unchecked ?
    3. What is the proper way to code plugin with desired behavior ?

    Thanks,

  9. #9
    Join Date
    September 2012
    Posts
    6,076
    Quote Originally Posted by Qt Documentation
    For any GUI application using Qt, there is precisely one QApplication object, no matter whether the application has 0, 1, 2 or more windows at any given time.
    from the QApplication Documentation.

    Because TeamSpeak is using Qt and your code is running in the same process there already is a QApplication object for the process and you mustn't create another one.

    When a plugin gets enabled several functions are called in the plugin that gets activated including the ts3plugin_init() function which you can use to initialize stuff you globally need. At this point you can also check some requirements which are needed for your plugin and tell the client to unload the plugin again if these are not met.

    When a plugin gets disabled the ts3plugin_shutdown() function is executed and afterwards the plugin gets unloaded from memory.




    Depending on your needs you probably don't need a separate thread.
    You can create an instance of your dialog in the init() function and display it instantly or at some later point in time. Keep in mind that instantly popping up a dialog when the plugin is activated is probably not what a user would expect / want depending on the plugin and its intended purpose.

    Inside your Class (which could be derived from QDialog if you want to display a UI) you already have an event loop that can call your own functions inside your class(es) on certain events.
    For TCP connections for example you could create a QTcpSocket in your class and wait for connections, then when a connection is established have a function called in your class that can then do whatever you need to do.

    See Signals and Slot Documentation.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Teamspeak 3 Lua Plugin: How do I put an interval in my loop?
    By Fattyshow in forum Client Plugins / Lua Scripts
    Replies: 1
    Last Post: October 18th, 2013, 07:20 PM
  2. what program for plugin creation
    By roun512 in forum Client Plugins / Lua Scripts
    Replies: 2
    Last Post: November 27th, 2012, 02:21 PM
  3. [On Todo] Rc1 client: Sound lags / kind of echos / Loop
    By Meldanor in forum Bug Reports [EN/DE]
    Replies: 78
    Last Post: December 27th, 2011, 08:33 AM
  4. Client gets disc, infinite loop.
    By rejnie in forum Windows
    Replies: 1
    Last Post: December 7th, 2011, 07:05 PM
  5. [On Todo] Rc1 client: Sound lags / kind of echos / Loop
    By Meldanor in forum General Questions
    Replies: 61
    Last Post: May 24th, 2011, 08:42 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •