示例#1
0
int main(int argc, char **argv)
{
    gtk_init(&argc, &argv);                     
    Display *dsp = XOpenDisplay( NULL );
    if( !dsp ){ return 1; }


    screens scrinfo;
    getScreens(&scrinfo);

    Window parentWin;

    int action=0;
    int verbose=0;
    int isdrag=0;
    int isinitialclick=1;
    int offset=10;
    mousestate mousepos;
    mousestate relativeMousepos;
    XEvent event;
    Window activeWindow;
    char launch[MY_MAXPATH];
    char configbase[MY_MAXPATH];
    strcpy(configbase,"~/.config/opensnap/");

    struct option longopts[] = {
        {"config",  1, NULL, 'c'},
        {"offset",  1, NULL, 'o'},
        {"daemon",  0, NULL, 'd'},
        {"info",    0, NULL, 'i'},
        {"verbose", 0, NULL, 'v'},
        {"help",    0, NULL, 'h'},
        {"version", 0, NULL, 'V'},
        {0, 0, 0, 0}};

    int opt=0;
    while((opt = getopt_long(argc,argv,"c:o:divVh",longopts,NULL)) != -1){
        switch(opt){
            case 'c':
                strncpy(configbase,optarg,MY_MAXPATH);
                configbase[MY_MAXPATH-1]='\0';
                break;
            case 'd':
                if(daemon(0,0) == -1){
                    perror("daemon");
                    exit(EXIT_FAILURE);
                }
                break;
            case 'i':
                dumpInfo(&scrinfo);
                exit(EXIT_SUCCESS);
                break;
            case 'v':
                verbose=1;
                break;
            case 'V':
                printf("opensnap version %s\n", versionstring);
                exit(EXIT_SUCCESS);
            case 'o':
                offset=atoi(optarg);
                break;
            case 'h':
            case '?':
                printHelp();
                exit(EXIT_FAILURE);
                break;
        }
    }

    while(1){
        getMousePosition(dsp, &event, &mousepos);
        int scrnn;
        scrnn = gdk_screen_get_monitor_at_point(gdk_screen_get_default(), mousepos.x, mousepos.y);
        //make mouse coordinates relative to screen
        relativeMousepos.x=mousepos.x-scrinfo.screens[scrnn].x;
        relativeMousepos.y=mousepos.y-scrinfo.screens[scrnn].y;
        if(verbose)
            printf("Mouse Coordinates: %d %d %d\n", mousepos.x, mousepos.y, mousepos.state );
        if((LEFTCLICK & mousepos.state)==LEFTCLICK){
            if(!isdrag && isinitialclick) {
                if(isTitlebarHit(dsp, &mousepos)){
                    isdrag=1;
                }
            }
            if(relativeMousepos.y<=offset)
                action=HIT_TOP;
            else if(relativeMousepos.x<=offset)
                action=HIT_LEFT;
            else if(relativeMousepos.x>=scrinfo.screens[scrnn].width-offset-1)
                action=HIT_RIGHT;
            else if(relativeMousepos.y>=scrinfo.screens[scrnn].height-offset-1)
                action=HIT_BOTTOM;
            else
                action=0;
            isinitialclick=false;
        }
        if(verbose)printf("action is: %d, isdrag is: %d\n",action,isdrag);
        if((16 & mousepos.state) == mousepos.state && isdrag){
            if(action){
                getFocusedWindow(dsp,&activeWindow);
                findParentWindow(dsp,&activeWindow,&parentWin);
                if(verbose)printf("Running script: %s",SCRIPT_NAMES[action]);
                sprintf(launch,"/bin/sh %s/%s %lu %i %i %i %i",configbase,SCRIPT_NAMES[action],parentWin,
                        scrinfo.screens[scrnn].width,scrinfo.screens[scrnn].height,scrinfo.screens[scrnn].x, scrinfo.screens[scrnn].y);
                system(launch);
            }
            action=0;
        }
        if((LEFTCLICK & mousepos.state) != LEFTCLICK){
            isdrag=0;
            isinitialclick=1;
        }
        usleep(10000);
    }
    XCloseDisplay(dsp);
    free(scrinfo.screens);
    return 0;
}
int main(int argc, char **argv)
{
	gtk_init(&argc, &argv);
	Display *dsp = XOpenDisplay( NULL );
	if( !dsp ) { return 1; }

	screens scrinfo;
	getScreens(&scrinfo);
	int isdrag=0;
	int isinitialclick=1;
	int offset=10;
	mousestate mousepos;
	mousestate relativeMousepos;
	XEvent event;
	int mode = 0;
	int Continue = 0;

	struct option longopts[] = {
		{"offset",    1, NULL, 'o'},
		{"daemon",    0, NULL, 'd'},
		{"help",      0, NULL, 'h'},
		{"version",   0, NULL, 'V'},
		{"titledrag", 0, NULL, 't'},
		{"altdrag",   0, NULL, 'a'},
		{0, 0, 0, 0}};

	int opt=0;

	// Handle command line options
	while((opt = getopt_long(argc,argv,"o:dvhta",longopts,NULL)) != -1)
	{
		switch(opt)
		{
		case 'd':
			if(daemon(0,0) == -1)
			{
				perror("daemon");
				exit(EXIT_FAILURE);
			}
			break;

		case 'v':
			printf("opensnap-quicktile " VERSION);
			exit(EXIT_SUCCESS);
			break;

		case 'o':
			offset=atoi(optarg);
			break;

		case 't':
			mode = 1;
			break;

		case 'a':
			mode = 2;
			break;

		case 'h':
		case '?':
			printf("Usage: opensnap-quicktile <OPTION>\n\n");
			printf("Options:\n");
			printf("  -d, --daemon            Run opensnap-quicktile as daemon.\n");
			printf("  -o, --offset <PIXEL>    Offset in pixel.\n");
			printf("  -v, --version           Print opensnap-quicktile version number.\n");
			printf("  -h, --help              Print this help.\n");
			printf("  -t, --titledrag         Only drags by titlebar.\n");
			printf("  -a, --altdrag           Only drags by Alt+Dragging.\n\n");

			exit(EXIT_FAILURE);
			break;
		}
	}

	// Main loop
	while(1)
	{
		getMousePosition(dsp, &event, &mousepos);

		int scrnn;
		scrnn = gdk_screen_get_monitor_at_point(gdk_screen_get_default(), mousepos.x, mousepos.y);

		relativeMousepos.x=mousepos.x-scrinfo.screens[scrnn].x;
		relativeMousepos.y=mousepos.y-scrinfo.screens[scrnn].y;

		/* Check if the window has been dragged to a screen edge. */
		if((LEFTCLICK & mousepos.state)==LEFTCLICK)
		{
			if(relativeMousepos.x<=offset || relativeMousepos.x>=scrinfo.screens[scrnn].width-offset-1 || relativeMousepos.y>=scrinfo.screens[scrnn].height-offset-1 || relativeMousepos.y<=offset) { Continue = 1; }

			else
			{
				if(!isdrag && isinitialclick)
				{
					if(mode == 0 || mode == 2)
					{
						if(mousepos.state & 8)
						{
							isdrag=1;
						}
					}

					if(mode == 0 || mode == 1)
					{
						if(isTitlebarHit(dsp, &mousepos))
						{

							isdrag=1;
						}
					}
				}
				Continue = 0;
			}
			isinitialclick=false;
		}

		if(Continue == 1)
		{
			if(((16 & mousepos.state) == mousepos.state || (24 & mousepos.state) == mousepos.state) && isdrag)
			{
				/* Check to see if window is still over there. This is to prevent the window from snapping after the mouse has been moved away. */
				if(relativeMousepos.x<=offset)
				{
					if(relativeMousepos.y<=offset) { string2exec("top-left"); }

					else if(relativeMousepos.y>=scrinfo.screens[scrnn].height-offset-1) { string2exec("bottom-left"); }

					else { string2exec("left"); }
				}

				else if(relativeMousepos.x>=scrinfo.screens[scrnn].width-offset-1)
				{
					if(relativeMousepos.y<=offset) { string2exec("top-right"); }

					else if(relativeMousepos.y>=scrinfo.screens[scrnn].height-offset-1) { string2exec("bottom-right"); }

					else { string2exec("right"); }
				}

				else if(relativeMousepos.y>=scrinfo.screens[scrnn].height-offset-1) { string2exec("bottom"); }

				else if(relativeMousepos.y<=offset) { string2exec("maximize"); }

				Continue = 0;
			}
		}

		if((LEFTCLICK & mousepos.state) != LEFTCLICK)
		{
			isdrag=0;
			isinitialclick=1;
		}
		usleep(10000);
	}

	XCloseDisplay(dsp);
	free(scrinfo.screens);
	return 0;
}