示例#1
0
文件: turf.c 项目: vlaadbrain/turf
int
main(int argc, char *argv[]) {
	Arg arg;
	Client *c;
	GdkGeometry hints = { 1, 1 };

	memset(&arg, 0, sizeof(arg));

	/* command line args */
	ARGBEGIN {
	case 'e':
		embed = strtol(EARGF(usage()), NULL, 0);
		break;
	case 'v':
		die("turf-"VERSION", ©2014 turf engineers, "
				"see LICENSE for details\n");
	case 'x':
		showxid = TRUE;
		break;
	default:
		usage();
	} ARGEND;
	if(argc > 0)
		arg.v = argv[0];

	sigchld(0);

	gtk_init(NULL, NULL);
	gtk_gl_init(NULL, NULL);

	dpy = GDK_DISPLAY();

	/* atoms */
	atoms[AtomFind] = XInternAtom(dpy, "_TURF_FIND", False);
	atoms[AtomGo] = XInternAtom(dpy, "_TURF_GO", False);
	atoms[AtomUri] = XInternAtom(dpy, "_TURF_URI", False);

	if(!(c = calloc(1, sizeof(Client))))
		die("Cannot malloc!\n");

	/* WIndow */
	if(embed) {
		c->win = gtk_plug_new(embed);
	} else {
		c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
		gtk_window_set_wmclass(GTK_WINDOW(c->win), "turf", "Turf");
		gtk_window_set_role(GTK_WINDOW(c->win), "Turf");
	}
	gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
	g_signal_connect(G_OBJECT(c->win),
			"destroy",
			G_CALLBACK(destroywin), c);

	c->vbox = gtk_vbox_new(FALSE, 0);

	/* cef init */
	cef_settings_t *settings;
	if(!(settings = malloc(sizeof(cef_settings_t))))
		die("Cannot malloc\n");

	cef_initialize(NULL, settings, NULL);

	/* cef set as child (vbox) */

	gtk_container_add(GTK_CONTAINER(c->win), c->vbox);


	gtk_widget_show_all(GTK_WIDGET(c->win));
	gtk_window_set_geometry_hints(GTK_WINDOW(c->win), NULL, &hints,
			GDK_HINT_MIN_SIZE);
	gdk_window_set_events(GTK_WIDGET(c->win)->window, GDK_ALL_EVENTS_MASK);
	/* process X events for window - aka AtomFind/AtomGo
	 * gdk_window_add_filter(GTK_WIDGET(c->win)->window, processx, c);
	 * setatom(c, AtomFind, "");
	 * setatom(c, AtomUri, "about:blank");
	 */

	c->title = NULL;
	c->next = clients;
	clients = c;

	if(showxid) {
		gdk_display_sync(gtk_widget_get_display(c->win));
		printf("%u\n",
			(guint)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
		fflush(NULL);
		if (fclose(stdout) != 0) {
			die("Error closing stdout");
		}
	}

	if(arg.v)
		loaduri(clients, &arg);

	gtk_main();
	cleanup();

	return EXIT_SUCCESS;
}
示例#2
0
int main(int argc, char** argv) {
    // This executable is called many times, because it
    // is also used for subprocesses. Let's print args
    // so we can differentiate between main process and
    // subprocesses. If one of the first args is for
    // example "--type=renderer" then it means that
    // this is a Renderer process. There may be more
    // subprocesses like GPU (--type=gpu-process) and
    // others. On Linux there are also special Zygote
    // processes.
    printf("\nProcess args: ");
    if (argc == 1) {
        printf("none (Main process)");
    } else {
        for (int i = 1; i < argc; i++) {
            if (strlen(argv[i]) > 128)
                printf("... ");
            else
                printf("%s ", argv[i]);
        }
    }
    printf("\n\n");

    // CEF version
    if (argc == 0) {
        printf("CEF version: %s\n", CEF_VERSION);
    }

    // Main args
    cef_main_args_t main_args = {};
    main_args.instance = GetModuleHandle(NULL);

    // Cef app
    cef_app_t app = {};
    initialize_cef_app(&app);
    
    // Execute subprocesses. It is also possible to have
    // a separate executable for subprocesses by setting
    // cef_settings_t.browser_subprocess_path. In such
    // case cef_execute_process should not be called here.
    printf("cef_execute_process, argc=%d\n", argc);
    int code = cef_execute_process(&main_args, &app, NULL);
    if (code >= 0) {
        _exit(code);
    }
    
    // Application settings. It is mandatory to set the
    // "size" member.
    cef_settings_t settings = {};
    settings.size = sizeof(cef_settings_t);
    settings.log_severity = LOGSEVERITY_WARNING; // Show only warnings/errors
    settings.no_sandbox = 1;

    // Initialize CEF
    printf("cef_initialize\n");
    cef_initialize(&main_args, &settings, &app, NULL);

    // Window info
    cef_window_info_t window_info = {};
    window_info.style = WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN \
            | WS_CLIPSIBLINGS | WS_VISIBLE;
    window_info.parent_window = NULL;
    window_info.x = CW_USEDEFAULT;
    window_info.y = CW_USEDEFAULT;
    window_info.width = CW_USEDEFAULT;
    window_info.height = CW_USEDEFAULT;

    // Window info - window title
    char window_name[] = "cefcapi example";
    cef_string_t cef_window_name = {};
    cef_string_utf8_to_utf16(window_name, strlen(window_name),
                             &cef_window_name);
    window_info.window_name = cef_window_name;

    // Initial url
    char url[] = "https://www.google.com/ncr";
    cef_string_t cef_url = {};
    cef_string_utf8_to_utf16(url, strlen(url), &cef_url);

    // Browser settings. It is mandatory to set the
    // "size" member.
    cef_browser_settings_t browser_settings = {};
    browser_settings.size = sizeof(cef_browser_settings_t);
    
    // Client handlers
    cef_client_t client = {};
    initialize_cef_client(&client);
    initialize_cef_life_span_handler(&g_life_span_handler);

    // Create browser asynchronously. There is also a
    // synchronous version of this function available.
    printf("cef_browser_host_create_browser\n");
    cef_browser_host_create_browser(&window_info, &client, &cef_url,
                                    &browser_settings, NULL);

    // Message loop. There is also cef_do_message_loop_work()
    // that allow for integrating with existing message loops.
    // On Windows for best performance you should set
    // cef_settings_t.multi_threaded_message_loop to true.
    // Note however that when you do that CEF UI thread is no
    // more application main thread and using CEF API is more
    // difficult and require using functions like cef_post_task
    // for running tasks on CEF UI thread.
    printf("cef_run_message_loop\n");
    cef_run_message_loop();

    // Shutdown CEF
    printf("cef_shutdown\n");
    cef_shutdown();

    return 0;
}
示例#3
0
int main(int argc, char** argv) {
    // Main args.
    cef_main_args_t mainArgs = {};
    mainArgs.instance = GetModuleHandle(NULL);
    
    // Application handler and its callbacks.
    // cef_app_t structure must be filled. It must implement
    // reference counting. You cannot pass a structure 
    // initialized with zeroes.
    cef_app_t app = {};
    initialize_app_handler(&app);
    
    // Execute subprocesses.
    printf("cef_execute_process, argc=%d\n", argc);
    int code = cef_execute_process(&mainArgs, &app, NULL);
    if (code >= 0) {
        _exit(code);
    }
    
    // Application settings.
    // It is mandatory to set the "size" member.
    cef_settings_t settings = {};
    settings.size = sizeof(cef_settings_t);
    settings.no_sandbox = 1;

    // Initialize CEF.
    printf("cef_initialize\n");
    cef_initialize(&mainArgs, &settings, &app, NULL);

    // Create GTK window. You can pass a NULL handle 
    // to CEF and then it will create a window of its own.
    //initialize_gtk();
    //GtkWidget* hwnd = create_gtk_window("cefcapi example", 1024, 768);
    //cef_window_info_t windowInfo = {};
    //windowInfo.parent_widget = hwnd;

    cef_window_info_t windowInfo = {};
    windowInfo.style = WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN \
            | WS_CLIPSIBLINGS | WS_VISIBLE;
    windowInfo.parent_window = NULL;
    windowInfo.x = CW_USEDEFAULT;
    windowInfo.y = CW_USEDEFAULT;
    windowInfo.width = CW_USEDEFAULT;
    windowInfo.height = CW_USEDEFAULT;

    // Initial url.
    char cwd[1024] = "";
    if (getcwd(cwd, sizeof(cwd)) == '\0') {
        printf("ERROR: getcwd() failed\n");
    }
    char url[1024];
    snprintf(url, sizeof(url), "file://%s/example.html", cwd);
    // There is no _cef_string_t type.
    cef_string_t cefUrl = {};
    cef_string_utf8_to_utf16(url, strlen(url), &cefUrl);
    
    // Browser settings.
    // It is mandatory to set the "size" member.
    cef_browser_settings_t browserSettings = {};
    browserSettings.size = sizeof(cef_browser_settings_t);
    
    // Client handler and its callbacks.
    // cef_client_t structure must be filled. It must implement
    // reference counting. You cannot pass a structure 
    // initialized with zeroes.
    cef_client_t client = {};
    initialize_client_handler(&client);

    // Create browser.
    printf("cef_browser_host_create_browser\n");
    cef_browser_host_create_browser(&windowInfo, &client, &cefUrl,
            &browserSettings, NULL);

    // Message loop.
    printf("cef_run_message_loop\n");
    cef_run_message_loop();

    // Shutdown CEF.
    printf("cef_shutdown\n");
    cef_shutdown();

    return 0;
}