//-------------------------------------------------- // this is a work-around of the difference between // xvnkb and unikey in using viqr method //-------------------------------------------------- void fixUnikeyToSyncMethod(int method) { long v; v = UnikeyToSyncMethod(method); UkSetPropValue(AIMUsing, v); UkSetPropValue(AIMMethod, v); }
//------------------------------------------------- long UkGetPropValue(Atom atom, long defValue) { long v = atom_get_value(atom); if (v == -1) { v = defValue; UkSetPropValue(atom, v); } return v; }
//------------------------------------------------------ void switchUnikey() { if (UnikeyOn) UkSetPropValue(AIMMethod, VKM_OFF); else { fixUnikeyToSyncMethod(UkActiveMethod); /* long v = UnikeyToSyncMethod(UkActiveMethod); UkSetPropValue(AIMMethod, v); UkSetPropValue(AIMUsing, v); */ } }
//------------------------------------------------------ void rotateUkMode() { int v; int mod = sizeof(UkModeList)/sizeof(UkModeList[0]); int newActiveMode, newUnikeyOn; newActiveMode = (UkActiveMode+1) % mod; newUnikeyOn = 1; //synchronize charset v = UnikeyToSyncCharset(newActiveMode); UkSetPropValue(AIMCharset, v); //synchronize on/off flag fixUnikeyToSyncMethod(UkActiveMethod); }
//------------------------------------------------------ int main(int argc, char **argv) { XSizeHints *sizeHints; XWMHints *wmHints; XClassHint *classHints; XTextProperty windowNameP, iconNameP; char *windowName = "UniKey GUI"; char *iconName = "UniKey"; XSetWindowAttributes attrs; Bool setXim = False; int i; pid_t pid; progname = argv[0]; for (i = 1; i < argc; i++) { if (!strcmp(argv[i], "-display")) { DisplayName = argv[++i]; } else if (!strcmp(argv[i], "-xim")) { setXim = True; strcpy(XimPath, argv[++i]); } else if (!strcmp(argv[i], "-macro")) { MacroFile = argv[++i]; } else if (!strcmp(argv[i], "-config")) { ConfigFile = argv[++i]; } else if (!strcmp(argv[i], "-locales") || !strcmp(argv[i], "-l")) { XimLocales = argv[++i]; } else if (!strcmp(argv[i], "-help") || !strcmp(argv[i], "-h")) { usage(); exit(0); } else if (!strcmp(argv[i], "-version") || !strcmp(argv[i], "-v")) { showVersion(); exit(0); } else { puts("Wrong options! Run \"unikey -h\" for help\n"); exit(-1); } } if (!setXim) getXimPath(); pid = fork(); if (pid > 0) exit(0); else if (pid < 0) { fputs("failed to launch new child process\n", stderr); exit(-1); } getOptions(); if (!(sizeHints = XAllocSizeHints())) { fprintf(stderr, "%s: failure allocating memory\n", progname); exit(0); } if (!(wmHints = XAllocWMHints())) { fprintf(stderr, "%s: failure allocating memory\n", progname); exit(0); } if (!(classHints = XAllocClassHint())) { fprintf(stderr, "%s: failure allocating memory\n", progname); exit(0); } if ((display = XOpenDisplay(DisplayName)) == NULL) { fprintf(stderr, "Can't Open Display: %s\n", XDisplayName(DisplayName)); XCloseDisplay(display); exit(1); } StartTime = time(0); RootWindow = DefaultRootWindow(display); setRootPropMask(); getSyncAtoms(); if (!singleLaunch()) { //check if previous instance is hidden if (!UkGetPropValue(AGUIVisible, 0)) { //wake up UkSetPropValue(AGUIVisible, 1); XFlush(display); } else { UkSetPropValue(ARaiseWindow, 1); XFlush(display); fputs("An instance of unikey has already started!\n", stderr); } exit(1); } UkSetPropValue(AGUIVisible, 1); UkGUIVisible = 1; /* get screen size from display structure macro */ ScreenNum = DefaultScreen(display); displayWidth = DisplayWidth(display, ScreenNum); displayHeight = DisplayHeight(display, ScreenNum); MainColormap = DefaultColormap(display, ScreenNum); // MainWinX = displayWidth-MainWinWidth-50; // MainWinY = displayHeight-MainWinHeight-50; getInitPos(); MainWindow = XCreateSimpleWindow(display, DefaultRootWindow(display), MainWinX, MainWinY, MainWinWidth, MainWinHeight, 0, WhitePixel(display, ScreenNum), WhitePixel(display, ScreenNum)); attrs.override_redirect = True; XChangeWindowAttributes(display, MainWindow, CWOverrideRedirect, &attrs); if (MainWindow == (Window)NULL) { fprintf(stderr, "Can't Create Window\n"); exit(1); } // Setup window properties sizeHints->flags = PPosition | PSize | PMinSize; sizeHints->min_width = MainWinWidth; sizeHints->min_height = MainWinHeight; if (XStringListToTextProperty(&windowName, 1, &windowNameP) == 0) { fprintf( stderr, "Structure allocation for windowName failed.\n"); exit(-1); } if (XStringListToTextProperty(&iconName, 1, &iconNameP) == 0) { fprintf( stderr, "Structure allocation for iconName failed.\n"); exit(-1); } wmHints->initial_state = NormalState; wmHints->input = True; wmHints->flags = StateHint | InputHint; classHints->res_name = progname; classHints->res_class = "UnikeyWindow"; XSetWMProperties(display, MainWindow, &windowNameP, &iconNameP, argv, argc, sizeHints, wmHints, classHints); XChangeProperty(display, MainWindow, _NET_WM_WINDOW_TYPE, XA_ATOM, 32, PropModeReplace, (unsigned char *)&_NET_WM_WINDOW_TYPE_DOCK, 1); // XStoreName(display, im_window, "Unikey"); XSetTransientForHint(display, MainWindow, MainWindow); XSelectInput(display, MainWindow, StructureNotifyMask | ExposureMask | ButtonPressMask | ButtonReleaseMask | VisibilityChangeMask | PointerMotionMask | PropertyChangeMask); allocXResources(); getInitSettings(); XMapWindow(display, MainWindow); UkSetPropValue(AGUIPosX, MainWinX); UkSetPropValue(AGUIPosY, MainWinY); signalSetup(); ChildPid = childProcess(); UkLoopContinue = 1; while (UkLoopContinue) { XEvent event; XNextEvent(display, &event); /* if (XFilterEvent(&event, None) == True) { continue; } */ MyXEventHandler(MainWindow, &event); } cleanup(); return 0; }
//------------------------------------------------------ void MyXEventHandler(Window im_window, XEvent *event) { static int dragX, dragY; static int dragging = 0; static int moved = 0; switch (event->type) { case DestroyNotify: break; case ButtonPress: switch (event->xbutton.button) { case Button1: { XButtonEvent *bev = (XButtonEvent *)event; if ((bev->state & ControlMask) && (bev->state & Mod1Mask)) { UkSetPropValue(AGUIVisible, 0); break; } if ((bev->state & ShiftMask) && (bev->state & ControlMask)) { reloadXimConfig(); break; } dragging = 1; dragX = bev->x_root - MainWinX; dragY = bev->y_root - MainWinY; } break; case Button3: if (event->xbutton.state & ControlMask) rotateUkMethod(); else rotateUkMode(); break; } break; case ButtonRelease: if (event->xbutton.button == Button1) { if (dragging) { if (!moved) { switchUnikey(); } } dragging = 0; moved = 0; } break; case Expose: if (event->xexpose.count == 0) drawMainWindow(MainWindow, MainGC, FontInfo, MainWinWidth, MainWinHeight); break; case MotionNotify: if (dragging) { XMotionEvent *mev = (XMotionEvent *)event; moved = 1; MainWinX = mev->x_root - dragX; MainWinY = mev->y_root - dragY; if (MainWinX < 0) MainWinX = 0; else if (MainWinX + MainWinWidth > displayWidth) MainWinX = displayWidth - MainWinWidth; if (MainWinY < 0) MainWinY = 0; else if (MainWinY + MainWinHeight > displayHeight) MainWinY = displayHeight - MainWinHeight; XMoveWindow(display, im_window, MainWinX, MainWinY); dragX = mev->x_root - MainWinX; dragY = mev->y_root - MainWinY; UkSetPropValue(AGUIPosX, MainWinX); UkSetPropValue(AGUIPosY, MainWinY); } break; case PropertyNotify: if (event->xproperty.window == RootWindow) handlePropertyChanged(im_window, event); break; case VisibilityNotify: { if (event->xvisibility.state != VisibilityUnobscured) { time_t current = time(0); if (current - StartTime < 3*60) XRaiseWindow(display, im_window); } /* static int count = 10; if (event->xvisibility.state != VisibilityUnobscured && count > 0) { count--; // fprintf(stderr, "Visibility count: %d\n", count); XRaiseWindow(display, im_window); } */ } break; default: break; } return; }