Skip to content

Instantly share code, notes, and snippets.

@tmzt
Created October 23, 2010 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmzt/642297 to your computer and use it in GitHub Desktop.
Save tmzt/642297 to your computer and use it in GitHub Desktop.
/*
* Copyright © 2004 Keith Packard
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation, and that the name of Keith Packard not be used in
* advertising or publicity pertaining to distribution of the software without
* specific, written prior permission. Keith Packard makes no
* representations about the suitability of this software for any purpose. It
* is provided "as is" without express or implied warranty.
*
* KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
* EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
#ifdef HAVE_CONFIG_H
#include <kdrive-config.h>
#endif
#include "kdandroid.h"
extern int KdTsPhyScreen;
KdKeyboardInfo *androidKbd;
KdPointerInfo *androidMouse;
Bool
androidInitialize (KdCardInfo *card, AndroidPriv *priv)
{
// androidInitNative(priv);
// androidInitFramebuffer(priv, 800, 480, 16);
priv->base = 0;
priv->bytes_per_line = 0;
return TRUE;
}
Bool
androidCardInit (KdCardInfo *card)
{
AndroidPriv *priv;
priv = (AndroidPriv *) malloc(sizeof (AndroidPriv));
if (!priv)
return FALSE;
if (!androidInitialize (card, priv))
{
free(priv);
return FALSE;
}
card->driver = priv;
return TRUE;
}
Bool
androidScreenInitialize (KdScreenInfo *screen, AndroidScreenPriv *scrpriv)
{
if (!screen->width || !screen->height)
{
screen->width = 800; //1024;
screen->height = 480; //768;
screen->rate = 72;
}
if (screen->width <= 0)
screen->width = 1;
if (screen->height <= 0)
screen->height = 1;
if (!screen->fb.depth)
screen->fb.depth = 16;
if (screen->fb.depth <= 8)
{
screen->fb.visuals = ((1 << StaticGray) |
(1 << GrayScale) |
(1 << StaticColor) |
(1 << PseudoColor) |
(1 << TrueColor) |
(1 << DirectColor));
}
else
{
screen->fb.visuals = (1 << TrueColor);
#define Mask(o,l) (((1 << l) - 1) << o)
if (screen->fb.depth <= 15)
{
screen->fb.depth = 15;
screen->fb.bitsPerPixel = 16;
screen->fb.redMask = Mask (10, 5);
screen->fb.greenMask = Mask (5, 5);
screen->fb.blueMask = Mask (0, 5);
}
else if (screen->fb.depth <= 16)
{
screen->fb.depth = 16;
screen->fb.bitsPerPixel = 16;
screen->fb.redMask = Mask (11, 5);
screen->fb.greenMask = Mask (5, 6);
screen->fb.blueMask = Mask (0, 5);
}
else
{
screen->fb.depth = 24;
screen->fb.bitsPerPixel = 32;
screen->fb.redMask = Mask (16, 8);
screen->fb.greenMask = Mask (8, 8);
screen->fb.blueMask = Mask (0, 8);
}
}
scrpriv->randr = screen->randr;
return androidMapFramebuffer (screen);
}
Bool
androidScreenInit (KdScreenInfo *screen)
{
AndroidScreenPriv *scrpriv;
scrpriv = calloc(1, sizeof (AndroidScreenPriv));
if (!scrpriv)
return FALSE;
screen->driver = scrpriv;
if (!androidScreenInitialize (screen, scrpriv))
{
screen->driver = 0;
free(scrpriv);
return FALSE;
}
return TRUE;
}
void *
androidWindowLinear (ScreenPtr pScreen,
CARD32 row,
CARD32 offset,
int mode,
CARD32 *size,
void *closure)
{
KdScreenPriv(pScreen);
AndroidPriv *priv = pScreenPriv->card->driver;
if (!pScreenPriv->enabled)
return 0;
*size = priv->bytes_per_line;
return priv->base + row * priv->bytes_per_line;
}
Bool
androidMapFramebuffer (KdScreenInfo *screen)
{
AndroidScreenPriv *scrpriv = screen->driver;
KdPointerMatrix m;
AndroidPriv *priv = screen->card->driver;
#if 1
if (scrpriv->randr != RR_Rotate_0)
scrpriv->shadow = TRUE;
else
scrpriv->shadow = FALSE;
#else
scrpriv->shadow = TRUE;
#endif
LogMessage(X_DEFAULT, "[native] androidMapFramebuffer: shadow: %d", (int)scrpriv->shadow);
KdComputePointerMatrix (&m, scrpriv->randr, screen->width, screen->height);
KdSetPointerMatrix (&m);
priv->bytes_per_line = ((screen->width * screen->fb.bitsPerPixel + 31) >> 5) << 2;
free(priv->base);
priv->base = malloc (priv->bytes_per_line * screen->height);
if (scrpriv->shadow)
{
if (!KdShadowFbAlloc (screen, scrpriv->randr & (RR_Rotate_90|RR_Rotate_270)))
return FALSE;
}
else
{
screen->fb.byteStride = priv->bytes_per_line;
screen->fb.pixelStride = (priv->bytes_per_line * 8/
screen->fb.bitsPerPixel);
screen->fb.frameBuffer = (CARD8 *) (priv->base);
}
/* initFramebuffer here for now */
// androidInitNativeFramebuffer(screen, screen->width, screen->height, screen->fb.depth);
LogMessage(X_DEFAULT, "[android] androidMapFramebuffer: initFramebuffer base: %p buf: %p width: %d height: %d depth: %d", priv->base, priv->buf, screen->width, screen->height, screen->fb.depth);
androidInitNativeFramebuffer(priv->base, &(priv->buf), screen->width, screen->height, screen->fb.depth);
return TRUE;
}
void
androidSetScreenSizes (ScreenPtr pScreen)
{
KdScreenPriv(pScreen);
KdScreenInfo *screen = pScreenPriv->screen;
AndroidScreenPriv *scrpriv = screen->driver;
if (scrpriv->randr & (RR_Rotate_0|RR_Rotate_180))
{
pScreen->width = screen->width;
pScreen->height = screen->height;
pScreen->mmWidth = screen->width_mm;
pScreen->mmHeight = screen->height_mm;
}
else
{
pScreen->width = screen->width;
pScreen->height = screen->height;
pScreen->mmWidth = screen->height_mm;
pScreen->mmHeight = screen->width_mm;
}
}
Bool
androidUnmapFramebuffer (KdScreenInfo *screen)
{
AndroidPriv *priv = screen->card->driver;
KdShadowFbFree (screen);
if (priv->base)
{
free (priv->base);
priv->base = 0;
}
return TRUE;
}
Bool
androidSetShadow (ScreenPtr pScreen)
{
KdScreenPriv(pScreen);
KdScreenInfo *screen = pScreenPriv->screen;
AndroidScreenPriv *scrpriv = screen->driver;
ShadowUpdateProc update;
ShadowWindowProc window;
window = androidWindowLinear;
update = 0;
if (scrpriv->randr)
update = shadowUpdateRotatePacked;
else
update = shadowUpdatePacked;
return KdShadowSet (pScreen, scrpriv->randr, update, window);
}
#ifdef RANDR
Bool
androidRandRGetInfo (ScreenPtr pScreen, Rotation *rotations)
{
KdScreenPriv(pScreen);
KdScreenInfo *screen = pScreenPriv->screen;
AndroidScreenPriv *scrpriv = screen->driver;
RRScreenSizePtr pSize;
Rotation randr;
int n;
*rotations = RR_Rotate_All|RR_Reflect_All;
for (n = 0; n < pScreen->numDepths; n++)
if (pScreen->allowedDepths[n].numVids)
break;
if (n == pScreen->numDepths)
return FALSE;
pSize = RRRegisterSize (pScreen,
screen->width,
screen->height,
screen->width_mm,
screen->height_mm);
randr = KdSubRotation (scrpriv->randr, screen->randr);
RRSetCurrentConfig (pScreen, randr, 0, pSize);
return TRUE;
}
Bool
androidRandRSetConfig (ScreenPtr pScreen,
Rotation randr,
int rate,
RRScreenSizePtr pSize)
{
KdScreenPriv(pScreen);
KdScreenInfo *screen = pScreenPriv->screen;
AndroidScreenPriv *scrpriv = screen->driver;
Bool wasEnabled = pScreenPriv->enabled;
AndroidScreenPriv oldscr;
int oldwidth;
int oldheight;
int oldmmwidth;
int oldmmheight;
int newwidth, newheight;
if (screen->randr & (RR_Rotate_0|RR_Rotate_180))
{
newwidth = pSize->width;
newheight = pSize->height;
}
else
{
newwidth = pSize->height;
newheight = pSize->width;
}
if (wasEnabled)
KdDisableScreen (pScreen);
oldscr = *scrpriv;
oldwidth = screen->width;
oldheight = screen->height;
oldmmwidth = pScreen->mmWidth;
oldmmheight = pScreen->mmHeight;
/*
* Set new configuration
*/
scrpriv->randr = KdAddRotation (screen->randr, randr);
androidUnmapFramebuffer (screen);
if (!androidMapFramebuffer (screen))
goto bail4;
KdShadowUnset (screen->pScreen);
if (!androidSetShadow (screen->pScreen))
goto bail4;
androidSetScreenSizes (screen->pScreen);
/*
* Set frame buffer mapping
*/
(*pScreen->ModifyPixmapHeader) (fbGetScreenPixmap (pScreen),
pScreen->width,
pScreen->height,
screen->fb.depth,
screen->fb.bitsPerPixel,
screen->fb.byteStride,
screen->fb.frameBuffer);
/* set the subpixel order */
KdSetSubpixelOrder (pScreen, scrpriv->randr);
if (wasEnabled)
KdEnableScreen (pScreen);
return TRUE;
bail4:
androidUnmapFramebuffer (screen);
*scrpriv = oldscr;
(void) androidMapFramebuffer (screen);
pScreen->width = oldwidth;
pScreen->height = oldheight;
pScreen->mmWidth = oldmmwidth;
pScreen->mmHeight = oldmmheight;
if (wasEnabled)
KdEnableScreen (pScreen);
return FALSE;
}
Bool
androidRandRInit (ScreenPtr pScreen)
{
rrScrPrivPtr pScrPriv;
if (!RRScreenInit (pScreen))
return FALSE;
pScrPriv = rrGetScrPriv(pScreen);
pScrPriv->rrGetInfo = androidRandRGetInfo;
pScrPriv->rrSetConfig = androidRandRSetConfig;
return TRUE;
}
#endif
Bool
androidCreateColormap (ColormapPtr pmap)
{
return fbInitializeColormap (pmap);
}
Bool
androidInitScreen (ScreenPtr pScreen)
{
KdScreenPriv(pScreen);
KdScreenInfo *screen = pScreenPriv->screen;
#ifdef TOUCHSCREEN
KdTsPhyScreen = pScreen->myNum;
#endif
androidInitNativeScreen(screen);
pScreen->CreateColormap = androidCreateColormap;
return TRUE;
}
Bool
androidFinishInitScreen (ScreenPtr pScreen)
{
if (!shadowSetup (pScreen))
return FALSE;
#ifdef RANDR
if (!androidRandRInit (pScreen))
return FALSE;
#endif
return TRUE;
}
Bool
androidCreateResources (ScreenPtr pScreen)
{
KdScreenPriv(pScreen);
KdScreenInfo *screen = pScreenPriv->screen;
AndroidScreenPriv *scrpriv = screen->driver;
if (scrpriv->shadow)
return KdShadowSet (pScreen,
scrpriv->randr,
androidShadowUpdate,
androidWindowLinear
);
else
return androidSetInternalDamage(pScreen);
//return androidSetShadow (pScreen);
}
void
androidPreserve (KdCardInfo *card)
{
}
Bool
androidEnable (ScreenPtr pScreen)
{
return TRUE;
}
Bool
androidDPMS (ScreenPtr pScreen, int mode)
{
return TRUE;
}
void
androidDisable (ScreenPtr pScreen)
{
}
void
androidRestore (KdCardInfo *card)
{
}
void
androidScreenFini (KdScreenInfo *screen)
{
}
void
androidCardFini (KdCardInfo *card)
{
AndroidPriv *priv = card->driver;
free (priv->base);
free(priv);
}
void
androidGetColors (ScreenPtr pScreen, int n, xColorItem *pdefs)
{
while (n--)
{
pdefs->red = 0;
pdefs->green = 0;
pdefs->blue = 0;
pdefs++;
}
}
void
androidPutColors (ScreenPtr pScreen, int n, xColorItem *pdefs)
{
}
/* track damage and update the process */
/* based on Android */
void
androidShadowUpdate (ScreenPtr pScreen, shadowBufPtr pBuf)
{
KdScreenPriv(pScreen);
KdScreenInfo *screen = pScreenPriv->screen;
// Android_LOG("slow paint");
/* FIXME: Slow Rotated/Reflected updates could be much
* much faster efficiently updating via tranforming
* pBuf->pDamage regions
*/
shadowUpdateRotatePacked(pScreen, pBuf);
//hostx_paint_rect(screen, 0,0,0,0, screen->width, screen->height);
//androidDraw(screen,0, 0, /*?*/ screen->width, screen->height);
androidDraw(0, 0, /*?*/ screen->width, screen->height);
}
static void
androidInternalDamageRedisplay (ScreenPtr pScreen)
{
KdScreenPriv(pScreen);
KdScreenInfo *screen = pScreenPriv->screen;
AndroidScreenPriv *scrpriv = screen->driver;
RegionPtr pRegion;
if (!scrpriv || !scrpriv->pDamage)
return;
pRegion = DamageRegion (scrpriv->pDamage);
if (RegionNotEmpty(pRegion))
{
int nbox;
BoxPtr pbox;
nbox = RegionNumRects (pRegion);
pbox = RegionRects (pRegion);
while (nbox--)
{
/*
hostx_paint_rect(screen,
pbox->x1, pbox->y1,
pbox->x1, pbox->y1,
pbox->x2 - pbox->x1,
pbox->y2 - pbox->y1);
*/
androidDraw(pbox->x1, pbox->y1,
/* ? */
pbox->x2 - pbox->x1,
pbox->y2 - pbox->y1);
pbox++;
}
DamageEmpty (scrpriv->pDamage);
}
}
static void
androidInternalDamageBlockHandler (pointer data,
OSTimePtr pTimeout,
pointer pRead)
{
ScreenPtr pScreen = (ScreenPtr) data;
androidInternalDamageRedisplay (pScreen);
}
static void
androidInternalDamageWakeupHandler (pointer data, int i, pointer LastSelectMask)
{
/* FIXME: Not needed ? */
}
Bool
androidSetInternalDamage (ScreenPtr pScreen)
{
KdScreenPriv(pScreen);
KdScreenInfo *screen = pScreenPriv->screen;
AndroidScreenPriv *scrpriv = screen->driver;
PixmapPtr pPixmap = NULL;
scrpriv->pDamage = DamageCreate ((DamageReportFunc) 0,
(DamageDestroyFunc) 0,
DamageReportNone,
TRUE,
pScreen,
pScreen);
if (!RegisterBlockAndWakeupHandlers (androidInternalDamageBlockHandler,
androidInternalDamageWakeupHandler,
(pointer) pScreen))
return FALSE;
pPixmap = (*pScreen->GetScreenPixmap) (pScreen);
DamageRegister (&pPixmap->drawable, scrpriv->pDamage);
return TRUE;
}
void
androidUnsetInternalDamage (ScreenPtr pScreen)
{
KdScreenPriv(pScreen);
KdScreenInfo *screen = pScreenPriv->screen;
AndroidScreenPriv *scrpriv = screen->driver;
PixmapPtr pPixmap = NULL;
pPixmap = (*pScreen->GetScreenPixmap) (pScreen);
DamageUnregister (&pPixmap->drawable, scrpriv->pDamage);
DamageDestroy (scrpriv->pDamage);
RemoveBlockAndWakeupHandlers (androidInternalDamageBlockHandler,
androidInternalDamageWakeupHandler,
(pointer) pScreen);
}
/* AndroidMouse */
static Status
AndroidMouseInit (KdPointerInfo *pi)
{
int res;
pi->driverPrivate = (AndroidPointerPriv *)
calloc(sizeof(AndroidPointerPriv), 1);
((AndroidPointerPriv *)pi->driverPrivate)->enabled = FALSE;
pi->nAxes = 3;
pi->nButtons = 32;
free(pi->name);
pi->name = strdup("Xephyr virtual AndroidMouse");
androidMouse = pi;
res = androidInitNativeMouse(pi);
LogMessage(X_DEFAULT, "[native] AndroidMouseInit: res: %d", res);
if (res) return res;
return Success;
}
static Status
AndroidMouseEnable (KdPointerInfo *pi)
{
((AndroidPointerPriv *)pi->driverPrivate)->enabled = TRUE;
return Success;
}
static void
AndroidMouseDisable (KdPointerInfo *pi)
{
((AndroidPointerPriv *)pi->driverPrivate)->enabled = FALSE;
return;
}
static void
AndroidMouseFini (KdPointerInfo *pi)
{
androidMouse = NULL;
return;
}
KdPointerDriver AndroidMouseDriver = {
"android",
AndroidMouseInit,
AndroidMouseEnable,
AndroidMouseDisable,
AndroidMouseFini,
NULL,
};
/* Keyboard */
static Status
AndroidKeyboardInit (KdKeyboardInfo *ki)
{
int res;
LogMessage(X_DEFAULT, "[native] AndroidKeyboardInit: ki: %p", ki);
ki->driverPrivate = (AndroidKeyboardPriv *)
calloc(sizeof(AndroidKeyboardPriv), 1);
/*
hostx_load_keymap();
if (!AndroidKeySyms.map) {
ErrorF("Couldn't load keymap from host\n");
return BadAlloc;
}
*/
/*
ki->minScanCode = AndroidKeySyms.minKeyCode;
ki->maxScanCode = AndroidKeySyms.maxKeyCode;
*/
/*
http://developer.android.com/reference/android/view/KeyEvent.html#MAX_KEYCODE
This constant is deprecated.
There are now more than MAX_KEYCODE keycodes. Use getMaxKeyCode() instead.
MAX_KEYCODE current value: 84
*/
// ki->minScanCode = 0; /* ? */
// ki->maxScanCode = 84; /* FIXME */
ki->minScanCode = 0;
ki->maxScanCode = 255;
free(ki->name);
ki->name = strdup("AndroiX virtual keyboard");
androidKbd = ki;
res = androidInitNativeKeyboard(ki);
LogMessage(X_DEFAULT, "[native] AndroidKeyboardInit: res: %d", res);
if (res) return res;
return Success;
}
static Status
AndroidKeyboardEnable (KdKeyboardInfo *ki)
{
LogMessage(X_DEFAULT, "[native] AndroidKeyboardEnable: ki: %p", ki);
((AndroidKeyboardPriv *)ki->driverPrivate)->enabled = TRUE;
return Success;
}
static void
AndroidKeyboardDisable (KdKeyboardInfo *ki)
{
((AndroidKeyboardPriv *)ki->driverPrivate)->enabled = FALSE;
}
static void
AndroidKeyboardFini (KdKeyboardInfo *ki)
{
androidKbd = NULL;
return;
}
static void
AndroidKeyboardLeds (KdKeyboardInfo *ki, int leds)
{
}
static void
AndroidKeyboardBell (KdKeyboardInfo *ki, int volume, int frequency, int duration)
{
}
KdKeyboardDriver AndroidKeyboardDriver = {
"android",
AndroidKeyboardInit,
AndroidKeyboardEnable,
AndroidKeyboardLeds,
AndroidKeyboardBell,
AndroidKeyboardDisable,
AndroidKeyboardFini,
NULL,
};
#include <android.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/stat.h>
static pthread_mutex_t miEventQueueMutex = PTHREAD_MUTEX_INITIALIZER;
jint
JNI_OnLoad(JavaVM *jvm, void *reserved) {
// xandroid_jvm = jvm; // pass to kdandroid driver
// LOG("xandroid_jvm: %.8x", xandroid_jvm);
Android = (AndroidVars *)calloc(sizeof(AndroidVars), 1);
Android->miEventQueueMutex = &miEventQueueMutex;
androidInitNative(jvm);
return JNI_VERSION_1_4;
}
void
Java_net_homeip_ofn_androix_AndroiXLib_init( JNIEnv* env, jobject thiz )
{
struct stat stats;
int mode = 0666;
char *argv[] = {":1"};
char *envp[] = {};
xandroid_jni_env = env; // pass to kdandroid driver
LOG("xandroid_jni_env: %p", xandroid_jni_env);
LOG("fixing up /data/data/net.homeip.ofn.androix/usr/bin/xkbcomp");
chmod("/data/data/net.homeip.ofn.androix/usr/bin/xkbcomp", 0775);
LOG("done.");
stat("/data/data/net.homeip.ofn.androix/usr/bin/xkbcomp", &stats);
LOG("/data/data/net.homeip.ofn.androix/usr/bin/xkbcomp mode: %o" + stats.st_mode);
LOG("creating event wakeup socket pair");
socketpair(AF_UNIX, SOCK_STREAM, 0, &(Android->wakeupFD));
LOG("remote (Xorg) socket: %d", Android->wakeupFD[0]);
LOG("local (android) socket: %d", Android->wakeupFD[1]);
LOG("starting DIX");
dix_main(1, argv, envp);
LOG("returning from DIX (this shouldn't happen)");
}
void wakeupFD() {
char nullbyte=0;
// <daniels> oh, i ... er ... christ.
write(Android->wakeupFD[1], &nullbyte, sizeof(nullbyte));
}
void
Java_net_homeip_ofn_androix_AndroiXLib_keyDown( JNIEnv* env, jobject thiz, jint kbd, jint keyCode )
{
LOG("keyDown: kbd: %p keyCode: %d", kbd, keyCode);
wakeupFD();
//androidRequestInputLock();
// LOG("keyDown: taking miEventQueueMutex");
// pthread_mutex_lock(Android->miEventQueueMutex);
androidCallbackKeyDown((void *)kbd, keyCode);
wakeupFD();
//androidReleaseInputLock();
// LOG("keyDown: releasing miEventQueueMutex");
// pthread_mutex_unlock(Android->miEventQueueMutex);
}
void
Java_net_homeip_ofn_androix_AndroiXLib_keyUp( JNIEnv* env, jobject thiz, jint kbd, jint keyCode )
{
LOG("keyUp: kbd: %p keyCode: %d", (unsigned int)kbd, keyCode);
//androidRequestInputLock();
// LOG("keyUp: taking miEventQueueMutex");
// pthread_mutex_lock(Android->miEventQueueMutex);
androidCallbackKeyUp((void *)kbd, keyCode);
wakeupFD();
//androidReleaseInputLock();
// LOG("keyUp: releasing miEventQueueMutex");
// pthread_mutex_unlock(Android->miEventQueueMutex);
}
void
Java_net_homeip_ofn_androix_AndroiXLib_touchDown( JNIEnv* env, jobject thiz, jint mouse, jint x, jint y )
{
LOG("touchDown: mouse: %p x: %d y: %d", mouse, x, y);
//androidRequestInputLock();
// LOG("touchDown: taking miEventQueueMutex");
// pthread_mutex_lock(Android->miEventQueueMutex);
androidCallbackTouchDown((void *)mouse, x, y);
wakeupFD();
//androidReleaseInputLock();
// LOG("touchDown: releasing miEventQueueMutex");
// pthread_mutex_unlock(Android->miEventQueueMutex);
}
void
Java_net_homeip_ofn_androix_AndroiXLib_touchUp( JNIEnv* env, jobject thiz, jint mouse, jint x, jint y )
{
LOG("touchUp: mouse: %p x: %d y: %d", mouse, x, y);
//androidRequestInputLock();
// LOG("touchUp: taking miEventQueueMutex");
// pthread_mutex_lock(Android->miEventQueueMutex);
androidCallbackTouchUp((void *)mouse, x, y);
wakeupFD();
//androidReleaseInputLock();
// LOG("touchUp: releasing miEventQueueMutex");
// pthread_mutex_unlock(Android->miEventQueueMutex);
}
#include <jni.h>
#ifdef HAVE_CONFIG_H
#include <kdrive-config.h>
#endif
#include "kdandroid.h"
#include <inputstr.h>
#include <mi.h>
CARD32 lastEventTime = 0;
EventList *androidEvents;
void androidCallbackKeyDown(void *kbdPtr, int keyCode) {
int i, n;
KdKeyboardInfo *kbd = kbdPtr;
LogMessage(X_DEFAULT, "[native] androidCallbackKeyDown: kbd: %.8x keyCode: %d", (unsigned int)kbd, keyCode);
LogMessage(X_DEFAULT, "[native] androidCallbackKeyDown: kbd->dixdev: %.8x", (unsigned int)(kbd->dixdev));
LogMessage(X_DEFAULT, "[native] androidCallbackKeyDown: kbd->dixdev->kbdfeed: %.8x", (unsigned int)(kbd->dixdev->kbdfeed));
LogMessage(X_DEFAULT, "[native] androidCallbackKeyDown: kbd->dixdev->key: %.8x", (unsigned int)(kbd->dixdev->key));
GetEventList(&androidEvents);
//lastEventTime = GetTimeInMillis();
n = GetKeyboardEvents(androidEvents, kbd->dixdev, KeyPress, keyCode);
LogMessage(X_DEFAULT, "[native] androidCallbackKeyDown: n: %d", n);
for (i = 0; i < n; i++) {
mieqEnqueue(kbd->dixdev, (InternalEvent*)(androidEvents + i)->event);
LogMessage(X_DEFAULT, "[native] androidCallbackKeyDown: enqueueing event KeyPress %d", keyCode);
};
//KdEnqueueKeyboardEvent (kbd, keyCode, FALSE);
}
void androidCallbackKeyUp(void *kbdPtr, int keyCode) {
int i, n;
KdKeyboardInfo *kbd = kbdPtr;
LogMessage(X_DEFAULT, "[native] androidCallbackKeyUp: kbd: %p keyCode: %d", kbd, keyCode);
LogMessage(X_DEFAULT, "[native] androidCallbackKeyUp: kbd->dixdev: %p", kbd->dixdev);
LogMessage(X_DEFAULT, "[native] androidCallbackKeyUp: kbd->dixdev->kbdfeed: %p", kbd->dixdev->kbdfeed);
LogMessage(X_DEFAULT, "[native] androidCallbackKeyUp: kbd->dixdev->key: %p", kbd->dixdev->key);
GetEventList(&androidEvents);
//lastEventTime = GetTimeInMillis();
n = GetKeyboardEvents(androidEvents, kbd->dixdev, KeyRelease, keyCode);
LogMessage(X_DEFAULT, "[native] androidCallbackKeyUp: n: %d", n);
for (i = 0; i < n; i++) {
mieqEnqueue(kbd->dixdev, (InternalEvent*)(androidEvents + i)->event);
LogMessage(X_DEFAULT, "[native] androidCallbackKeyUp: enqueueing event KeyRelease %d", keyCode);
};
//KdEnqueueKeyboardEvent (kbd, keyCode, TRUE);
}
void androidCallbackTouchDown(void *mousePtr, int x, int y) {
int i, n;
int v[3] = {x, y, 1};
KdPointerInfo *mouse = mousePtr;
LogMessage(X_DEFAULT, "[native] androidCallbackTouchDown: mouse: %p x: %d y: %d", mouse, x, y);
GetEventList(&androidEvents);
n = GetPointerEvents(androidEvents, mouse->dixdev, MotionNotify, 1, POINTER_ABSOLUTE, 0, 3, v);
LogMessage(X_DEFAULT, "[native] andriodCallbackTouchDown mouse->dixdev->enabled: %d", mouse->dixdev->enabled);
LogMessage(X_DEFAULT, "[native] androidCallbackTouchDown: n: %d", n);
for (i = 0; i < n; i++) {
mieqEnqueue(mouse->dixdev, (InternalEvent*)(androidEvents + i)->event);
LogMessage(X_DEFAULT, "[native] androidCallbackTouchDown: enqueueing event MotionNotify %d %d %d", x, y, 1);
};
}
void androidCallbackTouchUp(void *mousePtr, int x, int y) {
int i, n;
int v[3] = {x, y, 0};
KdPointerInfo *mouse = mousePtr;
LogMessage(X_DEFAULT, "[native] androidCallbackTouchUp: mouse: %p x: %d y: %d", mouse, x, y);
GetEventList(&androidEvents);
n = GetPointerEvents(androidEvents, mouse->dixdev, MotionNotify, 1, POINTER_ABSOLUTE, 0, 3, v);
LogMessage(X_DEFAULT, "[native] androidCallbackTouchUp mouse->dixdev->enable: %d", mouse->dixdev->enabled);
LogMessage(X_DEFAULT, "[native] androidCallbackTouchUp: n: %d", n);
for (i = 0; i < n; i++) {
mieqEnqueue(mouse->dixdev, (InternalEvent*)(androidEvents + i)->event);
LogMessage(X_DEFAULT, "[native] androidCallbackTouchUp: enqueueing event MotionNotify %d %d %d", x, y, 0);
};
}
/*
* Copyright © 2004 Keith Packard
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation, and that the name of Keith Packard not be used in
* advertising or publicity pertaining to distribution of the software without
* specific, written prior permission. Keith Packard makes no
* representations about the suitability of this software for any purpose. It
* is provided "as is" without express or implied warranty.
*
* KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
* EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
#ifndef _KDANDROID_H_
#define _KDANDROID_H_
#include <stdio.h>
#include <unistd.h>
#include "kdrive.h"
#ifdef RANDR
#include "randrstr.h"
#endif
#include "private.h"
#include "native.h"
extern KdCardFuncs androidFuncs;
extern KdKeyboardInfo *androidKbd;
extern KdPointerInfo *androidMouse;
/* drivers */
extern KdPointerDriver AndroidMouseDriver;
extern KdKeyboardDriver AndroidKeyboardDriver;
Bool
androidInitialize (KdCardInfo *card, AndroidPriv *priv);
Bool
androidCardInit (KdCardInfo *card);
Bool
androidScreenInit (KdScreenInfo *screen);
Bool
androidScreenInitialize (KdScreenInfo *screen, AndroidScreenPriv *scrpriv);
Bool
androidInitScreen (ScreenPtr pScreen);
Bool
androidFinishInitScreen (ScreenPtr pScreen);
Bool
androidCreateResources (ScreenPtr pScreen);
void
androidPreserve (KdCardInfo *card);
Bool
androidEnable (ScreenPtr pScreen);
Bool
androidDPMS (ScreenPtr pScreen, int mode);
void
androidDisable (ScreenPtr pScreen);
void
androidRestore (KdCardInfo *card);
void
androidScreenFini (KdScreenInfo *screen);
void
androidCardFini (KdCardInfo *card);
void
androidGetColors (ScreenPtr pScreen, int n, xColorItem *pdefs);
void
androidPutColors (ScreenPtr pScreen, int n, xColorItem *pdefs);
Bool
androidMapFramebuffer (KdScreenInfo *screen);
void *
androidWindowLinear (ScreenPtr pScreen,
CARD32 row,
CARD32 offset,
int mode,
CARD32 *size,
void *closure);
void
androidSetScreenSizes (ScreenPtr pScreen);
Bool
androidUnmapFramebuffer (KdScreenInfo *screen);
Bool
androidSetShadow (ScreenPtr pScreen);
Bool
androidCreateColormap (ColormapPtr pmap);
#ifdef RANDR
Bool
androidRandRGetInfo (ScreenPtr pScreen, Rotation *rotations);
Bool
androidRandRSetConfig (ScreenPtr pScreen,
Rotation randr,
int rate,
RRScreenSizePtr pSize);
Bool
androidRandRInit (ScreenPtr pScreen);
void
androidShadowUpdate (ScreenPtr pScreen, shadowBufPtr pBuf);
#endif
extern KdPointerDriver FakePointerDriver;
extern KdKeyboardDriver FakeKeyboardDriver;
extern KdOsFuncs AndroidOsFuncs;
/* internal damage */
void
androidUnsetInternalDamage (ScreenPtr pScreen);
Bool
androidSetInternalDamage (ScreenPtr pScreen);
#endif /* _KDANDROID_H_ */
/*
*
Copyright 1990, 1998 The Open Group
Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of The Open Group shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from The Open Group.
*
* Author: Keith Packard, MIT X Consortium
*/
/*
* mieq.c
*
* Machine independent event queue
*
*/
#if HAVE_DIX_CONFIG_H
#include <dix-config.h>
#endif
# include <X11/X.h>
# include <X11/Xmd.h>
# include <X11/Xproto.h>
# include "misc.h"
# include "windowstr.h"
# include "pixmapstr.h"
# include "inputstr.h"
# include "mi.h"
# include "mipointer.h"
# include "scrnintstr.h"
# include <X11/extensions/XI.h>
# include <X11/extensions/XIproto.h>
# include <X11/extensions/geproto.h>
# include "extinit.h"
# include "exglobals.h"
# include "eventstr.h"
#ifdef DPMSExtension
# include "dpmsproc.h"
# include <X11/extensions/dpmsconst.h>
#endif
#define QUEUE_SIZE 512
#define EnqueueScreen(dev) dev->spriteInfo->sprite->pEnqueueScreen
#define DequeueScreen(dev) dev->spriteInfo->sprite->pDequeueScreen
typedef struct _Event {
EventListPtr events;
ScreenPtr pScreen;
DeviceIntPtr pDev; /* device this event _originated_ from */
} EventRec, *EventPtr;
typedef struct _EventQueue {
HWEventQueueType head, tail; /* long for SetInputCheck */
CARD32 lastEventTime; /* to avoid time running backwards */
int lastMotion; /* device ID if last event motion? */
EventRec events[QUEUE_SIZE]; /* static allocation for signals */
mieqHandler handlers[128]; /* custom event handler */
} EventQueueRec, *EventQueuePtr;
static EventQueueRec miEventQueue;
#ifdef XQUARTZ
#include <pthread.h>
static pthread_mutex_t miEventQueueMutex = PTHREAD_MUTEX_INITIALIZER;
extern BOOL serverInitComplete;
extern pthread_mutex_t serverInitCompleteMutex;
extern pthread_cond_t serverInitCompleteCond;
static inline void wait_for_server_init(void) {
/* If the server hasn't finished initializing, wait for it... */
if(!serverInitComplete) {
pthread_mutex_lock(&serverInitCompleteMutex);
while(!serverInitComplete)
pthread_cond_wait(&serverInitCompleteCond, &serverInitCompleteMutex);
pthread_mutex_unlock(&serverInitCompleteMutex);
}
}
#endif
#ifdef ANDROID
#include <pthread.h>
#include "../android/jni/android.h"
//static pthread_mutex_t miEventQueueMutex = PTHREAD_MUTEX_INITIALIZER;
//Bool hasAndroidLock = FALSE;
#endif
Bool
mieqInit(void)
{
int i;
miEventQueue.head = miEventQueue.tail = 0;
miEventQueue.lastEventTime = GetTimeInMillis ();
miEventQueue.lastMotion = FALSE;
for (i = 0; i < 128; i++)
miEventQueue.handlers[i] = NULL;
for (i = 0; i < QUEUE_SIZE; i++)
{
if (miEventQueue.events[i].events == NULL) {
EventListPtr evlist = InitEventList(1);
if (!evlist)
FatalError("Could not allocate event queue.\n");
miEventQueue.events[i].events = evlist;
}
}
SetInputCheck(&miEventQueue.head, &miEventQueue.tail);
return TRUE;
}
void
mieqFini(void)
{
int i;
for (i = 0; i < QUEUE_SIZE; i++)
{
if (miEventQueue.events[i].events != NULL) {
FreeEventList(miEventQueue.events[i].events, 1);
miEventQueue.events[i].events = NULL;
}
}
}
/*
* Must be reentrant with ProcessInputEvents. Assumption: mieqEnqueue
* will never be interrupted. If this is called from both signal
* handlers and regular code, make sure the signal is suspended when
* called from regular code.
*/
void
mieqEnqueue(DeviceIntPtr pDev, InternalEvent *e)
{
unsigned int oldtail = miEventQueue.tail;
EventListPtr evt;
int isMotion = 0;
int evlen;
Time time;
#ifdef XQUARTZ
wait_for_server_init();
pthread_mutex_lock(&miEventQueueMutex);
#endif
#ifdef XANDROID
LogMessage(X_DEFAULT, "[native] [mieq] mieqEnqueue: waiting on lock");
pthread_mutex_lock(Android->miEventQueueMutex);
LogMessage(X_DEFAULT, "[native] [mieq] mieqEnqueue: acquired lock");
#endif
CHECKEVENT(e);
/* avoid merging events from different devices */
if (e->any.type == ET_Motion)
isMotion = pDev->id;
if (isMotion && isMotion == miEventQueue.lastMotion &&
oldtail != miEventQueue.head) {
oldtail = (oldtail - 1) % QUEUE_SIZE;
}
else {
static int stuck = 0;
/* Toss events which come in late. Usually this means your server's
* stuck in an infinite loop somewhere, but SIGIO is still getting
* handled. */
if (((oldtail + 1) % QUEUE_SIZE) == miEventQueue.head) {
if (!stuck) {
ErrorF("[mi] EQ overflowing. The server is probably stuck "
"in an infinite loop.\n");
xorg_backtrace();
stuck = 1;
}
#ifdef XQUARTZ
pthread_mutex_unlock(&miEventQueueMutex);
#endif
#ifdef XANDROID
// pthread_mutex_unlock(&miEventQueueMutex);
// androidReleaseInputLock();
pthread_mutex_unlock(Android->miEventQueueMutex);
LogMessage(X_DEFAULT, "[native] [mieq] mieqEnqueue: releasing miEventQueueMutex");
#endif
return;
}
stuck = 0;
}
evlen = e->any.length;
evt = miEventQueue.events[oldtail].events;
if (evt->evlen < evlen)
{
evt->evlen = evlen;
evt->event = realloc(evt->event, evt->evlen);
if (!evt->event)
{
ErrorF("[mi] Running out of memory. Tossing event.\n");
#ifdef XQUARTZ
pthread_mutex_unlock(&miEventQueueMutex);
#endif
#ifdef XANDROID
// pthread_mutex_unlock(&miEventQueueMutex);
// androidReleaseInputLock();
pthread_mutex_unlock(Android->miEventQueueMutex);
LogMessage(X_DEFAULT, "[native] [mieq] mieqEnqueue: releasing miEventQueueMutex");
#endif
return;
}
}
memcpy(evt->event, e, evlen);
time = e->any.time;
/* Make sure that event times don't go backwards - this
* is "unnecessary", but very useful. */
if (time < miEventQueue.lastEventTime &&
miEventQueue.lastEventTime - time < 10000)
e->any.time = miEventQueue.lastEventTime;
miEventQueue.lastEventTime = ((InternalEvent*)evt->event)->any.time;
miEventQueue.events[oldtail].pScreen = pDev ? EnqueueScreen(pDev) : NULL;
miEventQueue.events[oldtail].pDev = pDev;
miEventQueue.lastMotion = isMotion;
miEventQueue.tail = (oldtail + 1) % QUEUE_SIZE;
#ifdef XQUARTZ
pthread_mutex_unlock(&miEventQueueMutex);
#endif
#ifdef XANDROID
// pthread_mutex_unlock(&miEventQueueMutex);
// androidReleaseInputLock();
pthread_mutex_unlock(Android->miEventQueueMutex);
LogMessage(X_DEFAULT, "[native] [mieq] mieqEnqueue: releasing miEventQueueMutex");
#endif
}
void
mieqSwitchScreen(DeviceIntPtr pDev, ScreenPtr pScreen, Bool fromDIX)
{
#ifdef XQUARTZ
pthread_mutex_lock(&miEventQueueMutex);
#endif
EnqueueScreen(pDev) = pScreen;
if (fromDIX)
DequeueScreen(pDev) = pScreen;
#ifdef XQUARTZ
pthread_mutex_unlock(&miEventQueueMutex);
#endif
}
void
mieqSetHandler(int event, mieqHandler handler)
{
#ifdef XQUARTZ
pthread_mutex_lock(&miEventQueueMutex);
#endif
if (handler && miEventQueue.handlers[event])
ErrorF("[mi] mieq: warning: overriding existing handler %p with %p for "
"event %d\n", miEventQueue.handlers[event], handler, event);
miEventQueue.handlers[event] = handler;
#ifdef XQUARTZ
pthread_mutex_unlock(&miEventQueueMutex);
#endif
}
/**
* Change the device id of the given event to the given device's id.
*/
static void
ChangeDeviceID(DeviceIntPtr dev, InternalEvent* event)
{
switch(event->any.type)
{
case ET_Motion:
case ET_KeyPress:
case ET_KeyRelease:
case ET_ButtonPress:
case ET_ButtonRelease:
case ET_ProximityIn:
case ET_ProximityOut:
case ET_Hierarchy:
case ET_DeviceChanged:
event->device_event.deviceid = dev->id;
break;
#if XFreeXDGA
case ET_DGAEvent:
break;
#endif
case ET_RawKeyPress:
case ET_RawKeyRelease:
case ET_RawButtonPress:
case ET_RawButtonRelease:
case ET_RawMotion:
event->raw_event.deviceid = dev->id;
break;
default:
ErrorF("[mi] Unknown event type (%d), cannot change id.\n",
event->any.type);
}
}
static void
FixUpEventForMaster(DeviceIntPtr mdev, DeviceIntPtr sdev,
InternalEvent* original, InternalEvent *master)
{
CHECKEVENT(original);
CHECKEVENT(master);
/* Ensure chained button mappings, i.e. that the detail field is the
* value of the mapped button on the SD, not the physical button */
if (original->any.type == ET_ButtonPress ||
original->any.type == ET_ButtonRelease)
{
int btn = original->device_event.detail.button;
if (!sdev->button)
return; /* Should never happen */
master->device_event.detail.button = sdev->button->map[btn];
}
}
/**
* Copy the given event into master.
* @param sdev The slave device the original event comes from
* @param original The event as it came from the EQ
* @param copy The event after being copied
* @return The master device or NULL if the device is a floating slave.
*/
DeviceIntPtr
CopyGetMasterEvent(DeviceIntPtr sdev,
InternalEvent* original, InternalEvent *copy)
{
DeviceIntPtr mdev;
int len = original->any.length;
CHECKEVENT(original);
/* ET_XQuartz has sdev == NULL */
if (!sdev || !sdev->u.master)
return NULL;
switch(original->any.type)
{
case ET_KeyPress:
case ET_KeyRelease:
mdev = GetMaster(sdev, MASTER_KEYBOARD);
break;
case ET_ButtonPress:
case ET_ButtonRelease:
case ET_Motion:
case ET_ProximityIn:
case ET_ProximityOut:
mdev = GetMaster(sdev, MASTER_POINTER);
break;
default:
mdev = sdev->u.master;
break;
}
memcpy(copy, original, len);
ChangeDeviceID(mdev, copy);
FixUpEventForMaster(mdev, sdev, original, copy);
return mdev;
}
/**
* Post the given @event through the device hierarchy, as appropriate.
* Use this function if an event must be posted for a given device during the
* usual event processing cycle.
*/
void
mieqProcessDeviceEvent(DeviceIntPtr dev,
InternalEvent *event,
ScreenPtr screen)
{
mieqHandler handler;
int x = 0, y = 0;
DeviceIntPtr master;
InternalEvent mevent; /* master event */
CHECKEVENT(event);
/* Custom event handler */
handler = miEventQueue.handlers[event->any.type];
switch (event->any.type) {
/* Catch events that include valuator information and check if they
* are changing the screen */
case ET_Motion:
case ET_KeyPress:
case ET_KeyRelease:
case ET_ButtonPress:
case ET_ButtonRelease:
if (dev && screen && screen != DequeueScreen(dev) && !handler) {
DequeueScreen(dev) = screen;
x = event->device_event.root_x;
y = event->device_event.root_y;
NewCurrentScreen (dev, DequeueScreen(dev), x, y);
}
break;
default:
break;
}
master = CopyGetMasterEvent(dev, event, &mevent);
if (master)
master->u.lastSlave = dev;
/* If someone's registered a custom event handler, let them
* steal it. */
if (handler)
{
int screenNum = dev && DequeueScreen(dev) ? DequeueScreen(dev)->myNum : (screen ? screen->myNum : 0);
handler(screenNum, event, dev);
/* Check for the SD's master in case the device got detached
* during event processing */
if (master && dev->u.master)
handler(screenNum, &mevent, master);
} else
{
/* process slave first, then master */
dev->public.processInputProc(event, dev);
/* Check for the SD's master in case the device got detached
* during event processing */
if (master && dev->u.master)
master->public.processInputProc(&mevent, master);
}
}
/* Call this from ProcessInputEvents(). */
void
mieqProcessInputEvents(void)
{
EventRec *e = NULL;
int evlen;
ScreenPtr screen;
static InternalEvent *event = NULL;
static size_t event_size = 0;
DeviceIntPtr dev = NULL,
master = NULL;
#ifdef XQUARTZ
pthread_mutex_lock(&miEventQueueMutex);
#endif
#ifdef XANDROID
LogMessage(X_DEFAULT, "[native] [mieq] mieqProcessInputEvents: acquiring miEventQueueMutex");
pthread_mutex_lock(Android->miEventQueueMutex);
#endif
while (miEventQueue.head != miEventQueue.tail) {
e = &miEventQueue.events[miEventQueue.head];
evlen = e->events->evlen;
if(evlen > event_size)
{
event = realloc(event, evlen);
event_size = evlen;
}
if (!event)
FatalError("[mi] No memory left for event processing.\n");
memcpy(event, e->events->event, evlen);
dev = e->pDev;
screen = e->pScreen;
miEventQueue.head = (miEventQueue.head + 1) % QUEUE_SIZE;
#ifdef XQUARTZ
pthread_mutex_unlock(&miEventQueueMutex);
#endif
#ifdef XANDROID
pthread_mutex_unlock(Android->miEventQueueMutex);
LogMessage(X_DEFAULT, "[native] [mieq] mieqProcessInputEvents: releasing miEventQueueMutex");
#endif
master = (dev && !IsMaster(dev) && dev->u.master) ? dev->u.master : NULL;
if (screenIsSaved == SCREEN_SAVER_ON)
dixSaveScreens (serverClient, SCREEN_SAVER_OFF, ScreenSaverReset);
#ifdef DPMSExtension
else if (DPMSPowerLevel != DPMSModeOn)
SetScreenSaverTimer();
if (DPMSPowerLevel != DPMSModeOn)
DPMSSet(serverClient, DPMSModeOn);
#endif
mieqProcessDeviceEvent(dev, event, screen);
/* Update the sprite now. Next event may be from different device. */
if (event->any.type == ET_Motion && master)
miPointerUpdateSprite(dev);
#ifdef XQUARTZ
pthread_mutex_lock(&miEventQueueMutex);
#endif
#ifdef XANDROID
pthread_mutex_lock(Android->miEventQueueMutex);
LogMessage(X_DEFAULT, "[native] [mieq] mieqProcessInputEvents: releasing miEventQueueMutex");
#endif
}
#ifdef XQUARTZ
pthread_mutex_unlock(&miEventQueueMutex);
#endif
#ifdef XANDROID
pthread_mutex_unlock(Android->miEventQueueMutex);
LogMessage(X_DEFAULT, "[native] [mieq] mieqProcessInputEvents: releasing miEventQueueMutex");
#endif
}
/*
Copyright 2010 Timothy Meade
Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "android.h"
void androidInitNative(JavaVM *jvm) {
JNIEnv *jni_env;
jclass AndroiXService_class;
jfieldID blitview_id;
jclass AndroiXBlitView_class;
jobject blitview;
Android->jvm = jvm;
(*(Android->jvm))->AttachCurrentThread(Android->jvm, &jni_env, NULL);
AndroiXService_class = (*jni_env)->FindClass(jni_env, "net/homeip/ofn/androix/AndroiXService");
LOG("[native] init: local AndroiXService_class: %p", AndroiXService_class);
blitview_id = (*jni_env)->GetStaticFieldID(jni_env, AndroiXService_class, "blitView", "Lnet/homeip/ofn/androix/AndroiXBlitView;");
blitview = (*jni_env)->GetStaticObjectField(jni_env, AndroiXService_class, blitview_id);
LOG("[native] init: local blitview: %p", blitview);
AndroiXBlitView_class = (*jni_env)->GetObjectClass(jni_env, blitview);
LOG("[native] init: local AndroiXBlitView_class: %p", AndroiXBlitView_class);
/* setup global references and cache methodIDs */
Android->AndroiXService_class = (*jni_env)->NewGlobalRef(jni_env, AndroiXService_class);
Android->AndroiXBlitView_class = (*jni_env)->NewGlobalRef(jni_env, AndroiXBlitView_class);
Android->blitview = (*jni_env)->NewGlobalRef(jni_env, blitview);
Android->initNativeScreen = (*jni_env)->GetMethodID(jni_env, Android->AndroiXBlitView_class, "initNativeScreen", "(I)I");
Android->initNativeKeyboard = (*jni_env)->GetMethodID(jni_env, Android->AndroiXBlitView_class, "initNativeKeyboard", "(I)I");
Android->initNativeMouse = (*jni_env)->GetMethodID(jni_env, Android->AndroiXBlitView_class, "initNativeMouse", "(I)I");
Android->initFramebuffer = (*jni_env)->GetMethodID(jni_env, Android->AndroiXBlitView_class, "initFramebuffer", "(IIILjava/nio/ByteBuffer;)I");
Android->draw = (*jni_env)->GetMethodID(jni_env, Android->AndroiXBlitView_class, "draw", "(IIII)V");
(*jni_env)->DeleteLocalRef(jni_env, AndroiXService_class);
(*jni_env)->DeleteLocalRef(jni_env, AndroiXBlitView_class);
(*jni_env)->DeleteLocalRef(jni_env, blitview);
};
void androidInitNativeScreen(void *screen) {
JNIEnv *jni_env;
(*(Android->jvm))->AttachCurrentThread(Android->jvm, &jni_env, NULL);
(*jni_env)->CallIntMethod(jni_env, Android->blitview, Android->initNativeScreen, screen);
};
int androidInitNativeKeyboard(void *keyboard) {
JNIEnv *jni_env;
(*(Android->jvm))->AttachCurrentThread(Android->jvm, &jni_env, NULL);
return (*jni_env)->CallIntMethod(jni_env, Android->blitview, Android->initNativeKeyboard, keyboard);
};
int androidInitNativeMouse(void *mouse) {
JNIEnv *jni_env;
(*(Android->jvm))->AttachCurrentThread(Android->jvm, &jni_env, NULL);
return (*jni_env)->CallIntMethod(jni_env, Android->blitview, Android->initNativeMouse, mouse);
};
int androidInitNativeFramebuffer(void *base, void **bufPtr, int width, int height, int depth) {
JNIEnv *jni_env;
jobject *buf = (jobject *)bufPtr;
(*(Android->jvm))->AttachCurrentThread(Android->jvm, &jni_env, NULL);
int bpp = depth/8;
LOG("[native] androidInitNativeFramebuffer: base: %p buf: %p width: %d height: %d bpp: %d", base, buf, width, height, bpp);
buf = (*jni_env)->NewDirectByteBuffer(jni_env, base, (width*height*bpp));
jint res = (*jni_env)->CallIntMethod(jni_env, Android->blitview, Android->initFramebuffer, width, height, depth, buf);
return res;
};
void androidDraw(int x, int y, int w, int h) {
JNIEnv *jni_env;
jmethodID initNativeMouse;
LOG("[native] androidDraw: %d,%d %d x %d", x, y, w, h);
(*(Android->jvm))->AttachCurrentThread(Android->jvm, &jni_env, NULL);
(*jni_env)->CallVoidMethod(jni_env, Android->blitview, Android->draw, x, y, w, h);
};
#ifndef _KDANDROID_NATIVE_H_
#define _KDANDROID_NATIVE_H_
#include <stdio.h>
#include <unistd.h>
#include "kdrive.h"
#include "../../../android/jni/android.h"
/* callbacks */
void androidCallbackKeyDown(void *kbd, int keyCode);
void androidCallbackKeyUp(void *kbd, int keyCode);
void androidCallbackTouchDown(void *mouse, int x, int y);
void androidCallbackTouchUp(void *mouse, int x, int y);
#endif /* _KDANDROID_NATIVE_H_ */
/*
* Copyright © 2004 Keith Packard
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation, and that the name of Keith Packard not be used in
* advertising or publicity pertaining to distribution of the software without
* specific, written prior permission. Keith Packard makes no
* representations about the suitability of this software for any purpose. It
* is provided "as is" without express or implied warranty.
*
* KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
* EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
#ifdef HAVE_CONFIG_H
#include <kdrive-config.h>
#endif
#include "kdandroid.h"
static void
AndroidWakeupFDRead(int wakeupFD, void *closure) {
return;
}
static int
AndroidInit (void)
{
LogMessage(X_DEFAULT, "adding wakeupFD: %d", Android->wakeupFD[0]);
//KdAddFd(Android->wakeupFD[0]);
KdRegisterFd(Android->wakeupFD[0], AndroidWakeupFDRead, NULL);
//AddEnabledDevice(Android->wakeupFD[0]);
return 1;
}
static void
AndroidEnable (void)
{
}
static Bool
AndroidSpecialKey (KeySym sym)
{
return FALSE;
}
static void
AndroidDisable (void)
{
}
static void
AndroidFini (void)
{
}
KdOsFuncs AndroidOsFuncs = {
AndroidInit,
AndroidEnable,
AndroidSpecialKey,
AndroidDisable,
AndroidFini,
0
};
#ifndef _KDANDROID_PRIV_H_
#define _KDANDROID_PRIV_H_
#include <stdio.h>
#include <unistd.h>
#include "kdrive.h"
#ifdef RANDR
#include "randrstr.h"
#endif
#include <jni.h>
typedef struct _androidPriv {
CARD8 *base;
int bytes_per_line;
jobject buf; // the java nio buffer corresponding to base
JavaVM *jvm;
} AndroidPriv;
typedef struct _androidScreenPriv {
Rotation randr;
Bool shadow;
DamagePtr pDamage;
JavaVM *jvm;
jclass AndroiXBlitView_class;
jobject blitview;
jmethodID initFramebuffer;
jmethodID draw;
} AndroidScreenPriv;
typedef struct _androidKeyboardPriv {
Bool enabled;
} AndroidKeyboardPriv;
typedef struct _androidPointerPriv {
Bool enabled;
} AndroidPointerPriv;
#endif /* _KDANDROID_PRIV_H_ */
#include <sys/socket.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
int fds[2];
int handle(int sig) {
char buf[1024];
int n;
if (sig == SIGQUIT) exit(0);
fprintf(stderr, "Got signal %d\n", sig);
if (sig == SIGIO) {
for(;;) {
n = read(fds[0], buf, 1);
write(1, buf, 1);
};
};
}
int main() {
int n;
struct sigaction act, oact;
// memset(act, 0, sizeof(struct sigaction));
act.sa_handler = handle;
act.sa_flags = 0;
socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
sigaction(SIGIO, &act, &oact);
fcntl(fds[0], F_SETOWN, getpid());
for(;;) {
n = write(fds[1], "X", 1);
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment