source: xplra/src/plugin/src/xplra/plugin.cc@ 42:719f590e4878

Last change on this file since 42:719f590e4878 was 42:719f590e4878, checked in by István Váradi <ivaradi@…>, 11 years ago

Added initial support for the menu

File size: 5.8 KB
Line 
1// Copyright (c) 2013 by István Váradi
2
3// This file is part of XPLRA, a remote-access plugin for X-Plane
4
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are met:
7
8// 1. Redistributions of source code must retain the above copyright notice, this
9// list of conditions and the following disclaimer.
10// 2. Redistributions in binary form must reproduce the above copyright notice,
11// this list of conditions and the following disclaimer in the documentation
12// and/or other materials provided with the distribution.
13
14// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24
25// The views and conclusions contained in the software and documentation are those
26// of the authors and should not be interpreted as representing official policies,
27// either expressed or implied, of the FreeBSD Project.
28
29//------------------------------------------------------------------------------
30
31#include "ListenThread.h"
32
33#include "MessageWindow.h"
34
35#include <hu/varadiistvan/xplcommon/Util.h>
36#include <hu/varadiistvan/scpl/config.h>
37
38#include <XPLMDefs.h>
39#include <XPLMUtilities.h>
40#include <XPLMProcessing.h>
41#include <XPLMMenus.h>
42
43#include <cstdio>
44#include <cstring>
45#include <cstdarg>
46
47#if TARGET_API_POSIX
48#include <signal.h>
49#endif
50
51//------------------------------------------------------------------------------
52
53using xplra::ListenThread;
54using xplra::MessageWindow;
55
56using hu::varadiistvan::xplcommon::Util;
57
58//------------------------------------------------------------------------------
59
60/**
61 * The window to display messages from the clients.
62 */
63static MessageWindow* messageWindow = 0;
64
65/**
66 * The thread to use to listen for incoming connections.
67 */
68static ListenThread* listenThread = 0;
69
70/**
71 * The ID of the menu of the plugin.
72 */
73static XPLMMenuID menuID = 0;
74
75/**
76 * The index of the show message area menu item.
77 */
78static int showMessageAreaIndex = 0;
79
80//------------------------------------------------------------------------------
81
82static void menuHandler(void* /*menuRef*/, void* /*itemRef*/)
83{
84 // Util::debug("hu.varadiistvan.xplra menuHandler called\n");
85 XPLMMenuCheck checked = xplm_Menu_NoCheck;
86 XPLMCheckMenuItemState(menuID, showMessageAreaIndex, &checked);
87 if (checked==xplm_Menu_Unchecked) {
88 XPLMCheckMenuItem(menuID, showMessageAreaIndex, xplm_Menu_Checked);
89 messageWindow->show();
90 } else if (checked==xplm_Menu_Checked) {
91 XPLMCheckMenuItem(menuID, showMessageAreaIndex, xplm_Menu_Unchecked);
92 messageWindow->hide();
93 }
94}
95
96//------------------------------------------------------------------------------
97
98PLUGIN_API int XPluginStart(char* outName, char* outSignature,
99 char* outDescription)
100{
101 Util::debug("hu.varadiistvan.xplra called\n");
102
103 strcpy(outName, "X-Plane Remote Access");
104 strcpy(outSignature, "hu.varadiistvan.xplra");
105 strcpy(outDescription, "Provides remote access to datarefs");
106
107#if TARGET_API_POSIX
108 signal(SIGPIPE, SIG_IGN);
109#endif
110
111 return 1;
112}
113
114//------------------------------------------------------------------------------
115
116PLUGIN_API void XPluginEnable(void)
117{
118 Util::debug("hu.varadiistvan.xplra.XPluginEnable called\n");
119 // XPLMRegisterFlightLoopCallback(&callback, 5.0, 0);
120
121 int xplaneVersion = 0;
122 int xplmVersion = 0;
123 XPLMHostApplicationID hostID = 0;
124 XPLMGetVersions(&xplaneVersion, &xplmVersion, &hostID);
125
126 int menuIndex = XPLMAppendMenuItem(XPLMFindPluginsMenu(),
127 "Remote Access", NULL, 1);
128 menuID = XPLMCreateMenu("Remote Access", XPLMFindPluginsMenu(), menuIndex,
129 &menuHandler, 0);
130 showMessageAreaIndex = XPLMAppendMenuItem(menuID, "Show message area",
131 0, 1);
132 XPLMCheckMenuItem(menuID, showMessageAreaIndex, xplm_Menu_Unchecked);
133
134 messageWindow = new MessageWindow();
135
136 listenThread = new ListenThread(xplaneVersion, xplmVersion,
137 *messageWindow);
138 listenThread->start();
139}
140
141//------------------------------------------------------------------------------
142
143PLUGIN_API void XPluginReceiveMessage(XPLMPluginID inFrom, long inMessage,
144 void* inParam)
145{
146 Util::debug("hu.varadiistvan.xplra.XPluginReceiveMessage called, inFrom=%d, inMessage=%ld, inParam=%p\n",
147 inFrom, inMessage, inParam);
148}
149
150//------------------------------------------------------------------------------
151
152PLUGIN_API void XPluginDisable(void)
153{
154 Util::debug("hu.varadiistvan.xplra.XPluginDisable called\n");
155 // XPLMUnregisterFlightLoopCallback(&callback, 0);
156
157 if (listenThread!=0) {
158 listenThread->quit();
159 listenThread = 0;
160 }
161
162 delete messageWindow; messageWindow = 0;
163}
164
165//------------------------------------------------------------------------------
166
167PLUGIN_API void XPluginStop(void)
168{
169 Util::debug("hu.varadiistvan.xplra.XPluginStop called\n");
170}
171
172//------------------------------------------------------------------------------
173
174// Local Variables:
175// mode: C++
176// c-basic-offset: 4
177// indent-tabs-mode: nil
178// End:
Note: See TracBrowser for help on using the repository browser.