📄 nativegui.c
字号:
/* * @(#)nativeGUI.c 1.152 02/11/06 @(#) * * Copyright (c) 1999-2002 Sun Microsystems, Inc. All rights reserved. * PROPRIETARY/CONFIDENTIAL * Use is subject to license terms. */#include <kni.h>#include <defaultLCDUI.h>#include <nativeGUI.h>#include <configuration.h>#include <midpMalloc.h>#include <midpServices.h>#include <stdio.h>#include <stdlib.h>#include <time.h>#include <sys/time.h>#include <string.h>#include <strings.h>#include <math.h>#include <X11/Xlib.h>#include <X11/Xutil.h>#include <X11/Intrinsic.h>#include <X11/Shell.h>#include <X11/StringDefs.h>#include <X11/cursorfont.h>#include <X11/keysym.h>#include <X11/Sunkeysym.h>#include <X11/xpm.h>#ifdef INCLUDE_I18N#include "localeMethod.h"#include "lcWin.h"LcWinMethods lcWin=NULL;static XIC xic=NULL;static XIM xim=NULL;#endif#include "staticGraphics.h"#define ARROWS_WIDTH 20#define ARROWS_HEIGHT 18#define PAINT_WIDTH DISPLAY_WIDTH#define BOTTOM_BAR_HEIGHT 19#define EMULATOR_WIDTH 241#define EMULATOR_HEIGHT 635#define X_SCREEN X_SCREEN_OFFSETstatic int y_screen;/* Trusted MIDlet icon info */#define TrustIcon_width 12#define TrustIcon_height 12#define TrustIcon_xposition (PAINT_WIDTH - 17 - 10 - TrustIcon_width)#define TrustIcon_yposition 0/* NOTE : * trusted midlet icon x position is = * right corner (screenoffset+paintwidth) * - widht of battery (17 pixels) * - space between battery and right edge of icon (10 pixels) * - the width of the trusted icon */Display *display = NULL;Screen *screen;static Visual *visual;Colormap colormap;GC gcErase = NULL;static GC gcNormal = NULL;GC gc=NULL;static Cursor handCursor, stylusCursor;int visualDepth;int numColors = 4;static int initialized = 0;/* a greenish pixel that looks kind of like a typical LCD screen */XColor lightPixel = { 0, 0xaeae, 0xc0c0, 0xaeae, DoRed | DoGreen | DoBlue, 0 };XColor darkPixel = { 0, 0x6060, 0x6060, 0x6060, DoRed | DoGreen | DoBlue, 0};XColor grayTable[16];XColor rgbTable[256];static XtAppContext appContext;Window backgroundWindow;Drawable backgroundDrawable;/* The bits of the topbar image: drawn into the background Window */XImage *topbar = None;Window paintWindow;Drawable paintDrawable;jboolean doubleBuffer = KNI_TRUE;/* The led window. Put the led to be displayed here.*/Window LEDWindow;Drawable LEDDrawable;/* The bits of the LED images */XImage *LED_on_Image;XImage *LED_off_Image;/* The bits of the trusted MIDlet icon image */XImage *trustedicon;/* The BackLight window. Put the BackLight to be displayed here.*/Window BackLightWindow;Drawable BackLightDrawable;/* The bits of the BackLight image */XImage *BackLight_on_Image = None;XImage *BackLight_off_Image = None;/* Network Indicator status info */int latestNICount = 0;jboolean isLED_on = KNI_FALSE;jlong latestNITime = 0;#define NET_INDICATOR_DELAY 2500 /* 2.5 seconds *//* fn prototypes */void updateNetIndicator();void drawNetIndicator(jboolean active);/* The emulator window. We put the "global areas" in here */Window outerWindow;static Pixmap casePixmap;/* The image to use when we're iconified */static Pixmap dukeIcon;/* An atom that the window manager sends to ask the process to close */static Atom deleteAtom;/* An atom used for timer support */static Atom timerAtom;/* An atom used for jsr135 EOM event */Atom mmEOMAtom; /* Atoms used for fetching underline parameters */Atom underlinePositionAtom;Atom underlineThicknessAtom;Atom rawPositionAtom;Atom rawThicknessAtom;/* TEMPORARY, until we have multiple font support */#define DEFAULT_FONT_NAME \ "-*-lucidatypewriter-medium-r-normal-sans-10-*-*-*-*-*-*-*"static XFontStruct *systemFontInfo= NULL;#define INSIDE(e, _r) \ ((e.xbutton.x >= (_r).x) && (e.xbutton.x < ((_r).x + (_r).width)) && \ (e.xbutton.y >= (_r).y) && (e.xbutton.y < ((_r).y + (_r).height)))typedef struct { KeyType button; XRectangle bounds; char *name;} Key;static const Key keys[] = {#ifdef NO_POWER_BUTTON /* * Add -DNO_POWER_BUTTON to the Makefile if you want to disable * the power button during user testing. */ {KEY_POWER, {-10, -10, 1, 1}, "POWER"},#else {KEY_POWER, {160, 59, 24, 24}, "POWER"},#endif {KEY_SOFT1, {32, 347, 36, 27}, "SOFT1"}, {KEY_SOFT2, {174, 347, 36, 27}, "SOFT2"}, {KEY_UP, {105, 343, 30, 25}, "UP"}, {KEY_DOWN, {105, 399, 30, 25}, "DOWN"}, {KEY_LEFT, {72, 370, 25, 25}, "LEFT"}, {KEY_RIGHT, {148, 370, 25, 25}, "RIGHT"}, {KEY_SELECT, {110, 371, 24, 24}, "SELECT"}, {KEY_SEND, {27, 387, 31, 36}, "SEND"}, {KEY_END, {187, 387, 31, 36}, "END"}, {KEY_CLEAR, {93, 427, 56, 29}, "CLEAR"}, {KEY_1, {36, 462, 39, 31}, "1"}, {KEY_2, {101, 466, 44, 28}, "2"}, {KEY_3, {170, 462, 39, 31}, "3"}, {KEY_4, {40, 499, 39, 31}, "4"}, {KEY_5, {101, 505, 44, 28}, "5"}, {KEY_6, {166, 500, 39, 31}, "6"}, {KEY_7, {46, 539, 39, 31}, "7"}, {KEY_8, {101, 543, 44, 28}, "8"}, {KEY_9, {158, 539, 39, 31}, "9"}, {KEY_ASTERISK, {50, 582, 41, 23}, "*"}, {KEY_0, {101, 580, 44, 28}, "0"}, {KEY_POUND, {153, 582, 41, 23}, "#"},} ;#define refreshTopBar() \XClearArea(display, backgroundWindow,\ 0, 0, \ PAINT_WIDTH, topBarHeight, 0)#define refreshBottomBar() \XClearArea(display, backgroundWindow, \ 0, topBarHeight + paintHeight, \ PAINT_WIDTH, BOTTOM_BAR_HEIGHT, 0)#define refreshLEDArea() \XClearArea(display, LEDWindow,0, 0, LED_width, LED_height, 0);#define refreshBackLightArea() \XClearArea(display, BackLightWindow, 0, 0, Bklite_width, Bklite_height, 0);static const Key *lastButton = NULL;typedef struct _applicationData { Position x; Position y; Pixel foreground; Pixel background; char *title; int depth;} applicationData;extern voidconvertText(FONTPARAMS_PROTO, const unicode *chars, int n, XTextItem16 **items, int *numItems, XTextItem16 *itemBuf, int itemBufSize, XChar2b *charBuf, int charBufSize);extern voidfreeText(XTextItem16 *items, int numItems, XTextItem16 *itemBuf, XChar2b *charBuf, int charBufSize);extern voidgetTextBounds(XTextItem16 *items, int numItems, int *width, int *ascent, int *descent);static XtResource resources[] = { { XtNx, XtCX, XtRPosition, sizeof(Position), XtOffset(applicationData*, x), XtRImmediate, (void *)100 }, { XtNy, XtCY, XtRPosition, sizeof(Position), XtOffset(applicationData*, y), XtRImmediate, (void *)100 }, { XtNforeground, XtCForeground, XtRPixel, sizeof(Pixel), XtOffset(applicationData*, foreground), XtRString, "WHITE" }, { XtNbackground, XtCBackground, XtRPixel, sizeof(Pixel), XtOffset(applicationData*, background), XtRString, "BLACK" }, { XtNtitle, XtCTitle, XtRString, sizeof(char *), XtOffset(applicationData*, title), XtRImmediate, 0 }, { XtNdepth, XtCDepth, XtRInt, sizeof(int), XtOffset(applicationData*, depth), XtRImmediate, (void *)1 },};/* REMIND: this is 3:3:2. Consider 6x6x6 + 16 grays instead. */#define GETRGBPIXEL(r, g, b) \ rgbTable[((r & 0xe0) >> 0) | ((g & 0xe0) >> 3) | ((b & 0xc0) >> 6)].pixelstatic intgetGrayPixel(int gray) { switch(numColors) { case 2: return grayTable[gray >> 7].pixel; case 4: return grayTable[gray >> 6].pixel; case 16: return grayTable[gray >> 4].pixel; default: fprintf(stderr, "Unsupported number of gray levels -- %d\n", numColors); } return 0;}static int getPixel(int r, int g, int b, int gray) { return numColors < 256 ? getGrayPixel(gray) : GETRGBPIXEL(r, g, b);}static XImage *loadImage(unsigned char *bits, int width, int height){ XImage *img = XCreateImage(display, visual, 1, XYBitmap, 0, (char*)bits, width, height, 8, 0); if (img != NULL) { img->bitmap_unit = 8; } return img;}/* ------- */static int topBarHeight;static int bottomBarHeight;static int paintHeight;static jboolean inFullScreenMode;static jboolean requestedFullScreenMode;static jboolean drawTrustedMIDletIcon;void setUpOffsets(int fullscreen) { switch (fullscreen) { case 1: topBarHeight = topbar_height; bottomBarHeight = 0; break; case 0: topBarHeight = topbar_height; bottomBarHeight = BOTTOM_BAR_HEIGHT; break; } paintHeight = (DISPLAY_HEIGHT - (topBarHeight + bottomBarHeight)); y_screen = Y_SCREEN_OFFSET + topBarHeight;}void drawEmulatorScreen(jboolean fullscreen) { int status; Pixmap topbarPixmap = None; XpmAttributes topbarAttributes; if (KNI_FALSE == fullscreen) { if (topbar == None) { /* Load the topbar image */ topbarAttributes.valuemask = XpmVisual | XpmDepth | XpmReturnPixels | XpmCloseness | XpmSize | XpmColormap; topbarAttributes.closeness = 40000; topbarAttributes.visual = visual; topbarAttributes.depth = visualDepth; topbarAttributes.colormap = colormap; status = XpmCreatePixmapFromData(display, backgroundDrawable, topbar_xpm, &topbarPixmap, NULL, &topbarAttributes); topbar = XGetImage(display, topbarPixmap, 0, 0, 180, topbar_height, 0xffffffff, XYPixmap); /* We keep the XImages around, so we don't need the pixmaps...*/ if (topbarPixmap != None) { XFreePixmap(display, topbarPixmap); topbarPixmap = None; } XpmFreeAttributes(&topbarAttributes); } XPutImage(display, backgroundDrawable, gcNormal, topbar, 0, 0, 0, 0, topbar_width, topbar_height); } if (drawTrustedMIDletIcon == KNI_TRUE) { XPutImage(display, backgroundDrawable, gcNormal, trustedicon, 0, 0, TrustIcon_xposition, TrustIcon_yposition, TrustIcon_width, TrustIcon_height); } refreshTopBar(); refreshBottomBar(); XResizeWindow(display, paintWindow, PAINT_WIDTH, paintHeight); XMoveWindow(display, paintWindow, X_SCREEN, y_screen); XFlush(display);}/* Sets the top bar to display a trusted MIDlet icon, * then calls drawEmulatorScreen to cause an invalidate of * the top bar */void LCDUIdrawTrustedIcon(jboolean trusted) { drawTrustedMIDletIcon = trusted; drawEmulatorScreen(inFullScreenMode);}void setEmulatorScreenMode(jboolean fullscreen) { setUpOffsets(fullscreen); drawEmulatorScreen(fullscreen); /* defined as static in defaultLCDUI.c */ inFullScreenMode = fullscreen;}void restoreEmulatorScreenMode() { if (inFullScreenMode != requestedFullScreenMode) { setEmulatorScreenMode(requestedFullScreenMode); }}void LCDUISetEmulatorFullScreenMode(jboolean mode) { /* defined as static in defaultLCDUI.c */ requestedFullScreenMode = mode; setCommandsFullScreenMode(mode); setEmulatorScreenMode(mode);}/* ------- */static voidsetWMHints(Window window, char *windowName, char *iconName, int width, int height){ XSizeHints *hints = XAllocSizeHints(); XWMHints *wmhints = XAllocWMHints(); XSetIconName(display, window, iconName); XStoreName(display, window, windowName); /* We have an exact width and height. Do not resize. */ hints->min_width = hints->max_width = hints->width = width; hints->min_height = hints->max_height = hints->height = height; hints->flags = PSize | PMaxSize | PMinSize | PPosition; XSetWMNormalHints(display, window, hints); wmhints->icon_pixmap = dukeIcon; wmhints->input = True; wmhints->flags = InputHint | IconPixmapHint; XSetWMHints(display, window, wmhints); XFree(hints); XFree(wmhints);}static intlocalXErrorHandler(Display *disp, XErrorEvent *err){ char msg[128]; char buf[128]; XGetErrorText(disp, err->error_code, msg, sizeof(msg)); fprintf(stderr, "Xerror %s\n", msg);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -