Does someone know this? I want to unregister from the GSM network, but not enter the flightmode (I still need SIM access, and it should be fast). The following code works fine on a S100 (WM2003), but the 9100 (WM5) refuses to unregister. Registering works fine on both devices.
Code:
GsmControl::GsmControl()
{
errorMsg = TEXT("No ERROR/SUCCESS message to display.");
hLineApp = NULL;
hLine = NULL;
LineInitializeExParams.dwTotalSize = sizeof(LineInitializeExParams);
LineInitializeExParams.dwNeededSize = 0;
LineInitializeExParams.dwUsedSize = 0;
LineInitializeExParams.dwOptions = LINEINITIALIZEEXOPTION_USEEVENT;
LineInitializeExParams.Handles.hEvent = NULL;
LineInitializeExParams.dwCompletionKey = 0;
dwLineAPIVersion = TAPI_CURRENT_VERSION;
GsmInitialize();
}
HRESULT GsmControl::GsmInitialize()
// Initializes TAPI interface
{
res = lineInitializeEx(&hLineApp, NULL, NULL, NULL, &dwNumDevs, &dwLineAPIVersion, &LineInitializeExParams);
res = lineOpen(hLineApp, 0, &hLine, dwLineAPIVersion, 0, 0, LINECALLPRIVILEGE_MONITOR, 0, NULL);
return res;
}
HRESULT GsmControl::GsmLineUnregister()
{
res = lineUnregister(hLine);
// res = lineSetEquipmentState(hLine, LINEEQUIPSTATE_NOTXRX); /* Edit: This is commented out, as I don't want any kind of flightmode behaviour */
return res;
}
HRESULT GsmControl::GsmLineRegister()
{
// lineSetEquipmentState(hLine, LINEEQUIPSTATE_FULL); /* Edit: This is commented out, as I don't want any kind of flightmode behaviour */
res = lineRegister(hLine, LINEREGMODE_AUTOMATIC, NULL, 0);
if ( res != 0 ) //FAILED(res) )
{
errorMsg = TEXT("ERROR GsmLineRegister - lineRegister\n");
} else {
errorMsg = TEXT("SUCCESS GsmLineRegister - lineRegister");
}
return res;
}
Whats the return value of the lineUnregister() call?
Sorry, I have a problem getting the value. It's larger than zero.
Shouldn't the following code open a message window that says "GsmLineUnregister returned X"?
Code:
LPTSTR lpText = new WCHAR[1024];
swprintf(lpText, TEXT("GsmLineUnregister returned $d"), gsmCtrl->GsmLineUnregister());
MessageBox(NULL, lpText, TEXT("Deva Daemon"), MB_ICONINFORMATION);
Edit: I'm making too many mistakes lately. I also commented out lineSetEquipmentState, as that's how I want it to work but it doesn't (on a 9100)
OK, I've got it now. lineUnregister returns '11'.
Edit: It's not stable, it seems to return quite all kinds of values larger than ten. Each value is larger than its predecessor. After a reboot, I got a 12, and then again increasing numbers (19, 25, 29, 31). It doesn't seem to make any sense.
(Will I ever manage to write a post with no edits this week?)
I'm try write a application to addjust LCD backlight.But it failed at the
first step Create file.GetLastError return 161.
Somesone can tell me what's wrong with it?
Sorry for my poor english.
thx
Below is my code
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
TCHAR *pszDevice=_T("\\\\.\\LCD");
HANDLE hLCD=NULL;
hLCD=CreateFileW(pszDevice,
GENERIC_READ,
FILE_SHARE_WRITE,
NULL,
OPEN_ALWAYS,
0,
NULL);
DWORD dwError=GetLastError();
if(INVALID_HANDLE_VALUE!=hLCD)
{
MessageBox(NULL,_T("Got Lcd"),_T("MyMessage"),MB_OK);
}
CloseHandle(hLCD);
return 0;
}
Hi ft1860!
I don't think you can do it that way, although I never tried it. You should check out power management API:
http://msdn.microsoft.com/library/d...pplicationsToTurnSmartphoneBacklightOffOn.asp
This example is for smartphones but should work on a PPC just as well.
P.S.:
Error 161 is "The specified path is invalid" so you should check the device name (no idea what that might be).
levenum said:
Hi ft1860!
I don't think you can do it that way, although I never tried it. You should check out power management API:
http://msdn.microsoft.com/library/d...pplicationsToTurnSmartphoneBacklightOffOn.asp
This example is for smartphones but should work on a PPC just as well.
P.S.:
Error 161 is "The specified path is invalid" so you should check the device name (no idea what that might be).
Click to expand...
Click to collapse
hi levenum
Thanks your comment,but according to this document
http://windowssdk.msdn.microsoft.co...r/base/ioctl_video_set_display_brightness.asp
It say we can set the current AC and DC backlight levels by invoke DeviceIoControl with IOCTL_VIDEO_SET_DISPLAY_BRIGHTNESS parameter.
So first I must create a handle of LCD,but I cann't open it successfully,
why?
oh,I make a mistake
IOCTL_VIDEO_SET_DISPLAY_BRIGHTNESS is Win32 function.
On smart device,I should use SetPowerRequirement to set the backlight.
But when I invoke SetPowerRequirement on my Universal with D0 or D4,it say success ,but nothing happened.
maybe you have to access backlight by BKL1: device.
CreateFile(L"BKL1:", ....
or write the brightness value into registry.. that works for sure.
buzz
buzz_lightyear said:
maybe you have to access backlight by BKL1: device.
CreateFile(L"BKL1:", ....
or write the brightness value into registry.. that works for sure.
buzz
Click to expand...
Click to collapse
Thanks
I'll try it.
It seem that SetPowerRequirement D0-D4 just Open or close backlight,
but what I want is adjust the backlight's brightness.
ft1860 said:
It seem that SetPowerRequirement D0-D4 just Open or close backlight,
but what I want is adjust the backlight's brightness.
Click to expand...
Click to collapse
then just do it through registry.
buzz
buzz_lightyear said:
ft1860 said:
It seem that SetPowerRequirement D0-D4 just Open or close backlight,
but what I want is adjust the backlight's brightness.
Click to expand...
Click to collapse
then just do it through registry.
buzz
Click to expand...
Click to collapse
oh,thanks
It almost work.
It works fine on my Universal,but it doesn't work on my x50v with WM5.
I should not debug it on my x50v,there is so many bugs in Dell's WM5 update.
Another question is how to get the max brightness value.On my Universal it is 10,but on my x50v it is 8;
below is my new code
#include <WinReg.h>
#include <pm.h>
TCHAR tszAppName[] = TEXT("PMSAMPLE");
TCHAR tszTitle[] = TEXT("Power Manager Sample");
HINSTANCE g_hInst = NULL; // Local copy of hInstance
HANDLE g_hEventShutDown = NULL;
HANDLE g_hPowerNotificationThread = NULL;
HWND g_hSystemState = NULL;
//***************************************************************************
// Function Name: SetBacklightRequirement
//
// Purpose: Sets or releases the device power requirement to keep the
// backlight at D0
//
// Arguments:
// IN BOOL fBacklightOn - TRUE to leave the backlight on
//
void SetBacklightRequirement(BOOL fBacklightOn)
{
// the name of the backlight device
TCHAR tszBacklightName[] = TEXT("BKL1:");
static HANDLE s_hBacklightReq = NULL;
if (fBacklightOn)
{
if (NULL == s_hBacklightReq)
{
// Set the requirement that the backlight device must remain
// in device state D0 (full power)
s_hBacklightReq = SetPowerRequirement(tszBacklightName, D4,
POWER_NAME, NULL, 0);
if (!s_hBacklightReq)
{
RETAILMSG(1, (L"SetPowerRequirement failed: %X\n",
GetLastError()));
}
}
}
else
{
if (s_hBacklightReq)
{
if (ERROR_SUCCESS != ReleasePowerRequirement(s_hBacklightReq))
{
RETAILMSG(1, (L"ReleasePowerRequirement failed: %X\n",
GetLastError()));
}
s_hBacklightReq = NULL;
}
}
}
#define REG_BACKLIGHT L"ControlPanel\\Backlight"
//#define REG_VAL_BATT_TO L"BatteryTimeout"
//#define REG_VAL_AC_TO L"ACTimeout"
#define REG_VAL_BN L"BrightNess"
#define REG_VAL_ACBN L"ACBrightness"
unsigned int OldBattBN=0;
unsigned int OldACBN=0;
void RegOptionBL( DWORD dw1,DWORD dw2)
{
HKEY hKey = 0;
DWORD dwSize;
HANDLE hBL;
static bool bOpened=false;
if ( ERROR_SUCCESS == RegOpenKeyEx( HKEY_CURRENT_USER,REG_BACKLIGHT, 0, 0, &hKey ) )
{
if( !bOpened )
{
bOpened=true;
dwSize = 4;
RegQueryValueEx( hKey, REG_VAL_BN,NULL,NULL,(unsigned char*)&OldBattBN,&dwSize );
dwSize = 4;
RegQueryValueEx( hKey, REG_VAL_ACBN,NULL,NULL,(unsigned char*) &OldACBN,&dwSize );
// dwSize = 4;
// dwValue = 0xefff ;
// RegSetValueEx( hKey,REG_VAL_BATT_TO,NULL,REG_DWORD,(unsigned char *)&dwValue,dwSize );
// dwSize = 4;
// dwValue = 0xefff ;
// RegSetValueEx( hKey,REG_VAL_AC_TO,NULL,REG_DWORD,(unsigned char *)&dwValue,dwSize );
dwSize = 4;
RegSetValueEx( hKey,REG_VAL_BN,NULL,REG_DWORD,(unsigned char *)&dw1,dwSize );
dwSize = 4;
RegSetValueEx( hKey,REG_VAL_ACBN,NULL,REG_DWORD,(unsigned char *)&dw2,dwSize );
}
else
{
if (OldBattBN)
{
dwSize = 4;
RegSetValueEx( hKey,REG_VAL_BN,NULL,REG_DWORD,(unsigned char *)&OldBattBN,dwSize );
}
if (OldACBN)
{
OldACBN = 4;
RegSetValueEx( hKey,REG_VAL_ACBN,NULL,REG_DWORD,(unsigned char *)&OldACBN,dwSize );
}
}
RegCloseKey( hKey );
hBL = CreateEvent( NULL, FALSE, FALSE,L"BackLightChangeEvent" );
if( hBL )
{
SetEvent(hBL);
CloseHandle( hBL );
}
}
}
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
int nResult = 0;
// Create mutex to track whether or not an application is already running
HANDLE hMutex = CreateMutex(0, 0, _T("_PMSAMPLE_EXE_MUTEX_"));
// check the result code
if (NULL != hMutex)
{
if (ERROR_ALREADY_EXISTS != GetLastError())
{
SetBacklightRequirement(TRUE);
RegOptionBL(8,8);
Sleep(5000);
RegOptionBL(-1,-1);
SetBacklightRequirement(FALSE);
}
else
{
}
CloseHandle(hMutex);
}
return ( nResult >= 0 );
}
Possible to make the Xbox360 controller connect to your Android phone ?
there's alot of software for both Mac and Windows letting you connect it to your computer, so without too much research i wondered if there is a possibility to port one of those apps over to android and use the controller for emulators and games.
there must be some open-source apps for win/osx out there i think so if any dev would have a look at it, for me to read those codes would be the same as to try to teach me greek..
just an idea, but if there's anyone that could make something out of this i think there will be alot of people wanting to use it
kinda like the SNES controller the streak's got, only wireless instead.. haha
Xbox 360 controllers aren't bluetooth
Sent from my HTC Droid Incredible. This Droid Does More.
its not just software to allow an Xbox controller to be connected to a PC, unless you're talking about the wired version, you need the reciever.
Like generamerica says, Xbox controllers aren't BT. PS3 ones are, but the bluetooth it uses is slightly different than normal and if i recall there have been issues in the past trying to get it to connect, no idea where that project has gone since then.
The only controller that can work with android from the big 3 atm is the wiimote, and even then you have to be running the stack from AOSP instead of the sense stack.
You got a lot of Bluetooth receivers !
It's possible !
GéVé said:
You got a lot of Bluetooth receivers !
It's possible !
Click to expand...
Click to collapse
!!!!!!! lo voglioooo contropper per android...cosi gioco a game psx gba
why not ps3 controller?
Yeah 360 would be impracticle if not impossible as you would have to either use a wired controller or have a wireless adapter for it to work. All of which means more stuff to lug around with an already large controller.
It would be cool to get the ps3 controller working on android but the controllerr is fairly big to carry with your phone.
Btw, the ps3 controller works great on windows. I use it with my netbook to play games in between classes. It connects to my normal Bluetooth receiver without any issues using a custom driver and software to pair the devices.
The only problem I can see coming from getting a ps3 controller working on android would be that once you pair the controller to the receiver you can no longer use it with any other devices unless you unpaid and uninstall driver to original. This is how it is with the controller on windows and most people use 2 Bluetooth receivers to deal with the problem.
Regardless of how practicle it would be to carry a ps3 or Xbox controller around with your phone it would be sweet to have working especially to get some of the other bluetooth controllers that are out there working. I have seen Asus controllers that are Bluetooth and have all the same buttons as ps3 controllers but they fold up and are a lot smaller and easy to carry around.
And a Bluetooth controller would be great for us who do not have enough hardware keys to play games with - no hardware keyboard.
Sent from my HTC Hero CDMA using XDA App
Its hardly impractical, if you was chilling at work on a break and you and a friend wanted a game of street fighter on Tiger mame or something then i think linking two xbox pads to your phone and having a few round would be superb, I would avoid even bothering with wireless with the xbox pad because it's not bluetooth and the ps3 is problematic for most devices using sense (which means half of them) so it's just the wired xbox pad, a port or a fresh app which can link the onscreen buttons to games or emulators to the digital buttons would be fantastic.
Dev's would pick up on this and would probably develop around them if it became successful enough, I think it's a good idea, the reason i say that is because the onscreen keyboards for the devices out now have some achillies heel's on them, first one is the fact that it's touchscreen and it's just not the same as a pad, the second is there is only ever going to be one player per device the likes of street fighter 2 turbo and king of fighters were ment to be played with two players, not to mention the amount of other game types which would be played better on a pad other than touchscreen.
In an ideal world it would be a cable to your tv and a cable to your pad, run a game on your phone and play it on your tv with your xbox pad wired.
I know it can be done, might look into doing it myself, usb can be split too, analogue could come later, hmmmmm.
There is an actual implementation of Xbox360 controler for the Elocity A7 tablet,
[ROM] Emulator / XBox360 Controller Mod 1-21-2011
But idk if it could be adapted to other android devices since it seems to need an usb port support.
Sine. said:
There is an actual implementation of Xbox360 controler for the Elocity A7 tablet,
[ROM] Emulator / XBox360 Controller Mod 1-21-2011
But idk if it could be adapted to other android devices since it seems to need an usb port support.
Click to expand...
Click to collapse
Yeah, in order to use the wired controller, you would need USB Host mode drivers, which at the moment not a lot of Android phones have (yet). At least, not to my knowledge anyway. Then you would need some sort of adapter to go from Mini/Micro USB (depending on which phone you have) to regular USB.
Sine. said:
There is an actual implementation of Xbox360 controler for the Elocity A7 tablet,
[ROM] Emulator / XBox360 Controller Mod 1-21-2011
But idk if it could be adapted to other android devices since it seems to need an usb port support.
Click to expand...
Click to collapse
abrigham said:
Yeah, in order to use the wired controller, you would need USB Host mode drivers, which at the moment not a lot of Android phones have (yet). At least, not to my knowledge anyway. Then you would need some sort of adapter to go from Mini/Micro USB (depending on which phone you have) to regular USB.
Click to expand...
Click to collapse
well I've seen some articles where people have some sort of adapter that turnes mini/micro usb into a dual input for (mouse / keyboard) so I guess two controllers shouldn't be that much further off... it's a matter of who has the knowledge and can implement it... unfortunately, it isn't me =( heh
But for the devices with usb ports how would one go about getting the required support in their rom?
When you plug the 360 controller in nothing happens.. I would like to know what files I may need to push to my android device that HAS full usb ports.
sent from gv1.5 on g2
Get a micro usb to female usb cable, plug in the wireless adapter. Your device has to be running Android 4.0 to work, but it does work flawlessly. You then have to go to market and download a free app that allows you to configure the buttons on the controller....then you're all set. There are also a few games that are already set in options to run third party controllers as well. I'm planning on doing this as soon as we get some type on ICS love from any direction for the SGS2 SGH-T989, so. I hooked this setting up on a customers Galaxy Tab, so. Awesome...
This doesn't have to do with the xbox controller, but On my old itouch, I could use a wii controller to play games on a nes emulator. If it's possible with ios, then it has to be with android.
Sent from my GT-I9100 using Tapatalk
Well, maybe it's possible to make Android using pads already connected to PC? I have Xbox 360 pad with PC receiver and I am wondering if it's actually possible to connect that controller to Android device (mine is HTC Desire Z).
I'm afraid that there can be some input lag but maybe that can be done?
Plausible and an achievable concept... is it worth it? Lol?
This got me thinking.
I dont know why anyone would want to do this now a days but it is very possible.
Back in 2007 remotejoy came out on the psp-1001 Phat.
Basically the concept is simple you would put a couple of .prx files on the memory stick pro duo in the root directory in a folder named plugins. And set up a windows drivers and a small application I think. I'm not 100% about that but seems logical.
Then interface the psp with the pc via USB but before interfacing, you would have to go into the vsh menu which you would hit a button combo and a text overlay would appear over the xmb in the top left hand corner. You could then select what plugin you wanted to load. Then you would load up the prx's and plug in your controller and USB type B from the psp to the pc. It would detect it though libusb and then you simply execute remotejoy.exe "I think" don't quote me on any of this. It was a long time ago....
Then you would literally be able to play your game on your monitor or on a flat screen like me. Via dvi output from an ant 1600xt graphics card in crossfire. And my 10000 pound Sony professional 42 inch $4000 plasma..... and then thru remotejoy gui you could go to controllers and basically use any analog controller that there were drivers for...... Man things have really changed in 13 years!!!!.... Jeez.
These plugins could add all kinds of extra features. Fps counter, background music player, an ir yeah infrared.... lol irmanager a badass file explorer with every feature you could ever imagine, well there was an irshell .prx, and a psx one called pops before Sony ever even released it.....
Sound familiar? Should, first there was xposed framework, now we have magisk plugins with support for xposed modules....
With that being said this is very possible and a feasible feat. (Not that anyone should take it on).....
It was coded under unix. So same kernel we use on androids..... It could be forked/updated/shimed/referenced/converted/reverse engineer to remotejoy magisk plugin but it's old code.
I think the last time it was updated was in 09 when I graduated high school.... so someone way more talented then me could possibly modify and retrofit or port to android using adb and USB debugging on a rooted android.
But this is not my area of expertise ports aren't my bag, man.
It would be a huge undertaking.
For controller support it would use socks. TCP specifically.
I didn't dig into it too much because well everyone now a days has smart share or screen mirroring and bluetooth xbox one s controllers and 4k TV's......
Just wanted to say it it a very plausible and achievable project.
Sorry to dig up an old thread. I have the original source code somewhere.
Here's an excerpt for controller support and mapping via universal serial ports it's in raw.
/*
* PSPLINK
* -----------------------------------------------------------------------
* Licensed under the BSD license, see LICENSE in PSPLINK root for details.
*
* remotejoy.c - PSPLINK PC remote joystick handler (SDL Version)
*
* Copyright (c) 2006 James F <[email protected]>
*
* $HeadURL: svn://svn.pspdev.org/psp/branches/psplinkusb/tools/remotejoy/pcsdl/remotejoy.c $
* $Id: remotejoy.c 2187 2007-02-20 19:28:00Z tyranid $
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <limits.h>
#include <errno.h>
#include <ctype.h>
#include <signal.h>
#include <string.h>
#include <SDL.h>
#include <SDL_thread.h>
#include "../remotejoy.h"
#define DEFAULT_PORT 10004
#define DEFAULT_IP "localhost"
#define MAX_AXES_NUM 32767
#define DIGITAL_TOL 10000
#define PSP_SCREEN_W 480
#define PSP_SCREEN_H 272
#define EVENT_ENABLE_SCREEN 1
#define EVENT_RENDER_FRAME_1 2
#define EVENT_RENDER_FRAME_2 3
#define EVENT_DISABLE_SCREEN 4
#ifndef SOL_TCP
#define SOL_TCP IPPROTO_TCP
#endif
#if defined BUILD_BIGENDIAN || defined _BIG_ENDIAN
uint16_t swap16(uint16_t i)
{
uint8_t *p = (uint8_t *) &i;
uint16_t ret;
ret = (p[1] << 8) | p[0];
return ret;
}
uint32_t swap32(uint32_t i)
{
uint8_t *p = (uint8_t *) &i;
uint32_t ret;
ret = (p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0];
return ret;
}
uint64_t swap64(uint64_t i)
{
uint8_t *p = (uint8_t *) &i;
uint64_t ret;
ret = (uint64_t) p[0] | ((uint64_t) p[1] << 8) | ((uint64_t) p[2] << 16) | ((uint64_t) p[3] << 24)
| ((uint64_t) p[4] << 32) | ((uint64_t) p[5] << 40) | ((uint64_t) p[6] << 48) | ((uint64_t) p[7] << 56);
return ret;
}
#define LE16(x) swap16(x)
#define LE32(x) swap32(x)
#define LE64(x) swap64(x)
#else
#define LE16(x) (x)
#define LE32(x) (x)
#define LE64(x) (x)
#endif
enum PspCtrlButtons
{
/** Select button. */
PSP_CTRL_SELECT = 0x000001,
/** Start button. */
PSP_CTRL_START = 0x000008,
/** Up D-Pad button. */
PSP_CTRL_UP = 0x000010,
/** Right D-Pad button. */
PSP_CTRL_RIGHT = 0x000020,
/** Down D-Pad button. */
PSP_CTRL_DOWN = 0x000040,
/** Left D-Pad button. */
PSP_CTRL_LEFT = 0x000080,
/** Left trigger. */
PSP_CTRL_LTRIGGER = 0x000100,
/** Right trigger. */
PSP_CTRL_RTRIGGER = 0x000200,
/** Triangle button. */
PSP_CTRL_TRIANGLE = 0x001000,
/** Circle button. */
PSP_CTRL_CIRCLE = 0x002000,
/** Cross button. */
PSP_CTRL_CROSS = 0x004000,
/** Square button. */
PSP_CTRL_SQUARE = 0x008000,
/** Home button. */
PSP_CTRL_HOME = 0x010000,
/** Music Note button. */
PSP_CTRL_NOTE = 0x800000,
/** Screen button. */
PSP_CTRL_SCREEN = 0x400000,
/** Volume up button. */
PSP_CTRL_VOLUP = 0x100000,
/** Volume down button. */
PSP_CTRL_VOLDOWN = 0x200000,
};
enum PspButtons
{
PSP_BUTTON_CROSS = 0,
PSP_BUTTON_CIRCLE = 1,
PSP_BUTTON_TRIANGLE = 2,
PSP_BUTTON_SQUARE = 3,
PSP_BUTTON_LTRIGGER = 4,
PSP_BUTTON_RTRIGGER = 5,
PSP_BUTTON_START = 6,
PSP_BUTTON_SELECT = 7,
PSP_BUTTON_UP = 8,
PSP_BUTTON_DOWN = 9,
PSP_BUTTON_LEFT = 10,
PSP_BUTTON_RIGHT = 11,
PSP_BUTTON_HOME = 12,
PSP_BUTTON_NOTE = 13,
PSP_BUTTON_SCREEN = 14,
PSP_BUTTON_VOLUP = 15,
PSP_BUTTON_VOLDOWN = 16,
PSP_BUTTON_MAX = 17
};
unsigned int g_bitmap[PSP_BUTTON_MAX] = {
PSP_CTRL_CROSS, PSP_CTRL_CIRCLE, PSP_CTRL_TRIANGLE, PSP_CTRL_SQUARE,
PSP_CTRL_LTRIGGER, PSP_CTRL_RTRIGGER, PSP_CTRL_START, PSP_CTRL_SELECT,
PSP_CTRL_UP, PSP_CTRL_DOWN, PSP_CTRL_LEFT, PSP_CTRL_RIGHT, PSP_CTRL_HOME,
PSP_CTRL_NOTE, PSP_CTRL_SCREEN, PSP_CTRL_VOLUP, PSP_CTRL_VOLDOWN
};
const char *map_names[PSP_BUTTON_MAX] = {
"cross", "circle", "triangle", "square",
"ltrig", "rtrig", "start", "select",
"up", "down", "left", "right", "home",
"note", "screen", "volup", "voldown"
};
/* Maps the buttons on the joystick to the buttons on the PSP controller */
unsigned int *g_buttmap = NULL;
struct Args
{
const char *ip;
unsigned short port;
const char *dev;
const char *mapfile;
int verbose;
int video;
int fullscreen;
int droprate;
int fullcolour;
int halfsize;
int showfps;
};
struct GlobalContext
{
struct Args args;
struct sockaddr_in serv;
char name[128];
unsigned int version;
unsigned char axes;
unsigned char buttons;
int exit;
int digital;
int analog;
int tol;
int scron;
};
struct GlobalContext g_context;
struct ScreenBuffer
{
unsigned char buf[PSP_SCREEN_W * PSP_SCREEN_H * 4];
struct JoyScrHeader head;
/* Mutex? */
};
static struct ScreenBuffer g_buffers[2];
void init_font(void);
void print_text(SDL_Surface *screen, int x, int y, const char *fmt, ...);
/* Should have a mutex on each screen */
#define VERBOSE (g_context.args.verbose)
int fixed_write(int s, const void *buf, int len)
{
int written = 0;
while(written < len)
{
int ret;
ret = write(s, buf+written, len-written);
if(ret < 0)
{
if(errno != EINTR)
{
perror("write");
written = -1;
break;
}
}
else
{
written += ret;
}
}
return written;
}
int parse_args(int argc, char **argv, struct Args *args)
{
memset(args, 0, sizeof(*args));
args->ip = DEFAULT_IP;
args->port = DEFAULT_PORT;
while(1)
{
int ch;
int error = 0;
ch = getopt(argc, argv, "vsfchldp:i:m:r:");
if(ch < 0)
{
break;
}
switch(ch)
{
case 'p': args->port = atoi(optarg);
break;
case 'i': args->ip = optarg;
break;
case 'm': args->mapfile = optarg;
break;
case 'v': args->verbose = 1;
break;
case 'd': args->video = 1;
break;
case 'f': args->fullscreen = 1;
break;
case 'c': args->fullcolour = 1;
break;
case 'l': args->halfsize = 1;
break;
case 's': args->showfps = 1;
break;
case 'r': args->droprate = atoi(optarg);
if((args->droprate < 0) || (args->droprate > 59))
{
fprintf(stderr, "Invalid drop rate (0 <= r < 60)\n");
error = 1;
}
break;
case 'h':
default : error = 1;
break;
};
if(error)
{
return 0;
}
}
argc -= optind;
argv += optind;
return 1;
}
void print_help(void)
{
fprintf(stderr, "Remotejoy Help\n");
fprintf(stderr, "Usage: remotejoy [options]\n");
fprintf(stderr, "Options:\n");
fprintf(stderr, "-p port : Specify the port number\n");
fprintf(stderr, "-i ip : Specify the ip address (default %s)\n", DEFAULT_IP);
fprintf(stderr, "-m mapfile : Specify a file to map joystick buttons to the PSP\n");
fprintf(stderr, "-d : Auto enable display support\n");
fprintf(stderr, "-f : Full screen mode\n");
fprintf(stderr, "-r drop : Frame Skip, 0 (auto), 1 (1/2), 2 (1/3), 3(1/4) etc.\n");
fprintf(stderr, "-c : Full colour mode\n");
fprintf(stderr, "-l : Half size mode (both X and Y)\n");
fprintf(stderr, "-s : Show fps\n");
fprintf(stderr, "-v : Verbose mode\n");
}
int init_sockaddr(struct sockaddr_in *name, const char *ipaddr, unsigned short port)
{
struct hostent *hostinfo;
name->sin_family = AF_INET;
name->sin_port = htons(port);
hostinfo = gethostbyname(ipaddr);
if(hostinfo == NULL)
{
fprintf(stderr, "Unknown host %s\n", ipaddr);
return 0;
}
name->sin_addr = *(struct in_addr *) hostinfo->h_addr;
return 1;
}
int connect_to(const char *ipaddr, unsigned short port)
{
struct sockaddr_in name;
int sock = -1;
int flag = 1;
sock = socket(PF_INET, SOCK_STREAM, 0);
if(sock < 0)
{
perror("socket");
return -1;
}
if(!init_sockaddr(&name, ipaddr, port))
{
printf("Could not initialise socket address\n");
close(sock);
return -1;
}
if(connect(sock, (struct sockaddr *) &name, sizeof(name)) < 0)
{
perror("connect");
close(sock);
return -1;
}
/* Disable NAGLE's algorithm to prevent the packets being joined */
setsockopt(sock, SOL_TCP, TCP_NODELAY, &flag, sizeof(int));
return sock;
}
int get_joyinfo(SDL_Joystick *stick)
{
const char *name;
name = SDL_JoystickName(0);
if(!name)
{
return 0;
}
strcpy(g_context.name, name);
g_context.axes = SDL_JoystickNumAxes(stick);
g_context.buttons = SDL_JoystickNumButtons(stick);
return 1;
}
void remove_wsp(char *buf)
{
int len = strlen(buf);
int i = 0;
while(isspace(buf))
{
i++;
}
if(i > 0)
{
len -= i;
memmove(buf, &buf, len + 1);
}
if(len <= 0)
{
return;
}
i = len-1;
while(isspace(buf))
{
buf[i--] = 0;
}
}
int build_map(const char *mapfile, int buttons)
{
int i;
FILE *fp;
g_context.analog = -1;
g_context.digital = -1;
g_context.tol = DIGITAL_TOL;
g_buttmap = (unsigned int *) malloc(buttons * sizeof(unsigned int));
if(g_buttmap == NULL)
{
return 0;
}
for(i = 0; i < buttons; i++)
{
/* Fill with mappings, repeat if more than 8 buttons */
g_buttmap = i % 8;
}
if(mapfile)
{
char buffer[512];
int line = 0;
fp = fopen(mapfile, "r");
if(fp == NULL)
{
fprintf(stderr, "Could not open mapfile %s\n", mapfile);
return 0;
}
while(fgets(buffer, sizeof(buffer), fp))
{
char *tok, *val;
int butt;
line++;
remove_wsp(buffer);
if((buffer[0] == '#') || (buffer[0] == 0)) /* Comment or empty line */
{
continue;
}
tok = strtok(buffer, ":");
val = strtok(NULL, "");
if((tok == NULL) || (val == NULL))
{
printf("Invalid mapping on line %d\n", line);
continue;
}
butt = atoi(val);
for(i = 0; i < PSP_BUTTON_MAX; i++)
{
if(strcasecmp(map_names, tok) == 0)
{
g_buttmap[butt] = i;
break;
}
}
if(i == PSP_BUTTON_MAX)
{
if(strcasecmp("analog", tok) == 0)
{
g_context.analog = butt;
}
else if(strcasecmp("digital", tok) == 0)
{
g_context.digital = butt;
}
else if(strcasecmp("tol", tok) == 0)
{
g_context.tol = atoi(val);
}
else
{
fprintf(stderr, "Unknown map type %s\n", tok);
}
}
}
fclose(fp);
}
return 1;
}
int send_event(int sock, int type, unsigned int value)
{
struct JoyEvent event;
if(sock < 0)
{
return 0;
}
/* Note, should swap endian */
event.magic = LE32(JOY_MAGIC);
event.type = LE32(type);
event.value = LE32(value);
if(fixed_write(sock, &event, sizeof(event)) != sizeof(event))
{
fprintf(stderr, "Could not write out data to socket\n");
return 0;
}
return 1;
}
void post_event(int no)
{
SDL_Event event;
event.type = SDL_USEREVENT;
event.user.code = no;
event.user.data1 = NULL;
event.user.data2 = NULL;
SDL_PushEvent(&event);
}
int flush_socket(int sock)
{
/* If we encounter some horrible error which means we are desynced
* then send a video off packet to remotejoy, wait around for a second sucking up
* any more data from the socket and then reenable */
return 0;
}
void update_fps(SDL_Surface *screen)
{
#define FRAME_VALUES 32
static Uint32 times[FRAME_VALUES];
static Uint32 lastticks = 0;
static int index = 0;
Uint32 ticks;
int i;
double fps;
ticks = SDL_GetTicks();
times[index] = ticks - lastticks;
index = (index + 1) % FRAME_VALUES;
lastticks = ticks;
fps = 0.0;
for(i = 0; i < FRAME_VALUES; i++)
{
fps += (double) times;
}
fps /= (double) FRAME_VALUES;
/* Fps is now average frame time */
fps = 1000.0 / fps;
/* Now frame frequency in Hz */
print_text(screen, 0, 0, "Fps: %.2f", fps);
}
int read_thread(void *p)
{
int err = 0;
int frame = 0;
fd_set saveset, readset;
int count;
int sock = *(int *) p;
struct JoyScrHeader head;
FD_ZERO(&saveset);
FD_SET(sock, &saveset);
while(!err)
{
readset = saveset;
count = select(FD_SETSIZE, &readset, NULL, NULL, NULL);
if(count > 0)
{
int ret;
int mode;
int size;
if(FD_ISSET(sock, &readset))
{
ret = read(sock, &head, sizeof(head));
if((ret != sizeof(head)) || (LE32(head.magic) != JOY_MAGIC))
{
fprintf(stderr, "Error in socket %d, magic %08X\n", ret, head.magic);
flush_socket(sock);
break;
}
mode = LE32(head.mode);
size = LE32(head.size);
g_buffers[frame].head.mode = mode;
g_buffers[frame].head.size = size;
if(mode < 0)
{
if(g_context.args.video)
{
post_event(EVENT_ENABLE_SCREEN);
}
else
{
g_context.scron = 0;
}
}
else if(mode > 3)
{
/* Flush socket */
flush_socket(sock);
}
else
{
/* Try and read in screen */
/* If we do not get a full frame read and we timeout in quater second or so then
* reset sync as it probably means the rest isn't coming */
int loc = 0;
//fprintf(stderr, "Size %d\n", size);
while(1)
{
readset = saveset;
/* Should have a time out */
count = select(FD_SETSIZE, &readset, NULL, NULL, NULL);
if(count > 0)
{
ret = read(sock, &(g_buffers[frame].buf[loc]), size-loc);
if(ret < 0)
{
if(errno != EINTR)
{
perror("read:");
err = 1;
break;
}
}
else if(ret == 0)
{
fprintf(stderr, "EOF\n");
break;
}
//fprintf(stderr, "Read %d\n", loc);
loc += ret;
if(loc == size)
{
break;
}
}
else if(count < 0)
{
if(errno != EINTR)
{
perror("select:");
err = 1;
break;
}
}
}
if(!err)
{
if(frame)
{
post_event(EVENT_RENDER_FRAME_2);
}
else
{
post_event(EVENT_RENDER_FRAME_1);
}
frame ^= 1;
}
}
}
}
else if(count < 0)
{
if(errno != EINTR)
{
perror("select:");
err = 1;
}
}
}
return 0;
}
SDL_Surface *create_surface(void *buf, int mode)
{
unsigned int rmask, bmask, gmask, amask;
int currw, currh;
int bpp;
currw = PSP_SCREEN_W;
currh = PSP_SCREEN_H;
if(g_context.args.halfsize)
{
currw >>= 1;
currh >>= 1;
}
if(VERBOSE)
{
printf("Mode %d\n", mode);
}
switch(mode)
{
case 3:
rmask = LE32(0x000000FF);
gmask = LE32(0x0000FF00);
bmask = LE32(0x00FF0000);
amask = 0;
bpp = 32;
break;
case 2:
rmask = LE16(0x000F);
gmask = LE16(0x00F0);
bmask = LE16(0x0F00);
amask = 0;
bpp = 16;
break;
case 1:
rmask = LE16(0x1F);
gmask = LE16(0x1F << 5);
bmask = LE16(0x1F << 10);
amask = 0;
bpp = 16;
break;
case 0:
rmask = LE16(0x1F);
gmask = LE16(0x3F << 5);
bmask = LE16(0x1F << 11);
amask = 0;
bpp = 16;
break;
default: return NULL;
};
return SDL_CreateRGBSurfaceFrom(buf, currw, currh, bpp, currw*(bpp/8),
rmask, gmask, bmask, amask);
}
void save_screenshot(SDL_Surface *surface)
{
int i;
char path[PATH_MAX];
struct stat s;
/* If we cant find one in the next 1000 then dont bother */
for(i = 0; i < 1000; i++)
{
snprintf(path, PATH_MAX, "scrshot%03d.bmp", i);
if(stat(path, &s) < 0)
{
break;
}
}
if(i == 1000)
{
return;
}
if(SDL_SaveBMP(surface, path) == 0)
{
printf("Saved screenshot to %s\n", path);
}
else
{
printf("Error saving screenshot\n");
}
}
void mainloop(void)
{
SDL_Joystick *stick = NULL;
SDL_Surface *screen = NULL;
SDL_Surface *buf1 = NULL;
SDL_Surface *buf2 = NULL;
SDL_Thread *thread = NULL;
int currw, currh;
int sdl_init = 0;
int sock = -1;
unsigned int button_state = 0;
int currmode[2] = { 3, 3 };
int flags = SDL_HWSURFACE;
int pspflags = 0;
int showfps = 0;
Hit my limit deleted non essentials to controller mapping and left some display transmission info in.
But no one will probably even read this... I LOVE XDADEVELOPERS!