int main(int argc, char *argv[]) { gchar **uris = NULL; xdgHandle xdg; /* clean up any zombies */ sigchld(0); gtk_init(&argc, &argv); if (!g_thread_supported()) g_thread_init(NULL); /* parse command line opts and get uris to load */ uris = parseopts(argc, argv); /* get XDG basedir data */ xdgInitHandle(&xdg); init_directories(&xdg); init_lua(uris, &xdg); /* we are finished with this */ xdgWipeHandle(&xdg); gtk_main(); return EXIT_SUCCESS; }
FILE *pdfapp_getmarkfile(pdfapp_t *app) { xdgHandle xdg; struct stat st; const char * const *conf_dirs; const char * const *dir; char path[PATH_MAX]; if (!xdgInitHandle(&xdg)) return NULL; conf_dirs = xdgSearchableConfigDirectories(&xdg); for (dir = conf_dirs; *dir; dir++) { pdfapp_buildfilepath(*dir, MARK_FILE, path); if (stat(path, &st)) { if (errno == ENOENT) continue; else perror(NULL); } else { if (S_ISREG(st.st_mode)) { app->hdlmark = fopen(path, "r+"); break; } else printf("Unknown config file format"); } } if (!app->hdlmark) { for (dir = conf_dirs; *dir; dir++) { // Use folder in home dir to create config file if (strstr(*dir, "/home") == *dir) { pdfapp_buildfilepath(*dir, MARK_FILE, path); app->hdlmark = fopen(path, "w+"); break; } } } xdgWipeHandle(&xdg); return app->hdlmark; }
int main(int argc, char* argv[]) { const char * const * item; xdgHandle handle; if (!xdgInitHandle(&handle)) return 1; printf("${XDG_DATA_HOME:-$HOME/.local/share}=%s\n", xdgDataHome(&handle)); printf("${XDG_CONFIG_HOME:-$HOME/.config}=%s\n", xdgConfigHome(&handle)); printf("${XDG_DATA_DIRS:-/usr/local/share/:/usr/share/}="); for (item = xdgDataDirectories(&handle); *item; item++) printf("%s%c", *item, (item[1] ? ':' : '\n')); printf("${XDG_DATA_HOME:-$HOME/.local/share}:${XDG_DATA_DIRS:-/usr/local/share/:/usr/share/}="); for (item = xdgSearchableDataDirectories(&handle); *item; item++) printf("%s%c", *item, (item[1] ? ':' : '\n')); printf("${XDG_CONFIG_DIRS:-/etc/xdg}="); for (item = xdgConfigDirectories(&handle); *item; item++) printf("%s%c", *item, (item[1] ? ':' : '\n')); printf("${XDG_CONFIG_HOME:-$HOME/.config}:${XDG_CONFIG_DIRS:-/etc/xdg}="); for (item = xdgSearchableConfigDirectories(&handle); *item; item++) printf("%s%c", *item, (item[1] ? ':' : '\n')); printf("${XDG_CACHE_HOME:-$HOME/.cache}=%s\n", xdgCacheHome(&handle)); xdgWipeHandle(&handle); return 0; }
void xdg_uninit() { xdgWipeHandle(handle); g_free(handle); }
/** Parse command line parameters * * \param argc The number of command line parameters * \param argv The strings list of arguments */ static void parse_command_line_parameters(int argc, char **argv) { const struct option long_options[] = { { "help", 0, NULL, 'h' }, { "version", 0, NULL, 'v' }, { "config", 1, NULL, 'c' }, { "rendering-path", 1, NULL, 'r' }, { "plugins-path", 1, NULL, 'p' }, { NULL, 0, NULL, 0 } }; int opt; FILE *config_fp = NULL; while((opt = getopt_long(argc, argv, "vhc:r:p:", long_options, NULL)) != -1) { switch(opt) { case 'v': printf(VERSION); exit(EXIT_SUCCESS); break; case 'h': display_help(); exit(EXIT_SUCCESS); break; case 'c': if(!strlen(optarg) || !(config_fp = fopen(optarg, "r"))) { display_help(); exit(EXIT_FAILURE); } break; case 'r': if(optarg && strlen(optarg)) globalconf.rendering_dir = strdup(optarg); else fatal("-r option requires a directory"); break; case 'p': if(optarg && strlen(optarg)) globalconf.plugins_dir = strdup(optarg); else fatal("-p option requires a directory"); break; } } /* Get the configuration file */ if(!config_fp) { /* Look for the configuration file in Autoconf $sysconfigdir/xdg, then fall back on XDG if not found */ if((config_fp = fopen(XDG_CONFIG_DIR "/" CONFIG_FILENAME, "r")) == NULL) { xdgHandle xdg; xdgInitHandle(&xdg); config_fp = xdgConfigOpen(CONFIG_FILENAME, "r", &xdg); xdgWipeHandle(&xdg); } if(!config_fp) fatal("Can't open configuration file"); } /* Parse configuration file */ if(!parse_configuration_file(config_fp)) { fclose(config_fp); fatal("Can't parse configuration file"); } fclose(config_fp); /* Get the rendering backend path if not given in the command line parameters */ if(!globalconf.rendering_dir) globalconf.rendering_dir = strdup(RENDERING_DIR); /* Get the plugins path if not given in the command line parameters */ if(!globalconf.plugins_dir) globalconf.plugins_dir = strdup(PLUGINS_DIR); }
cXdg::~cXdg() { // Destroy our handle if (bIsValid) xdgWipeHandle(&handle); }
/** Hello, this is main. * \param argc Who knows. * \param argv Who knows. * \return EXIT_SUCCESS I hope. */ int main(int argc, char **argv) { char *confpath = NULL; int xfd, i, opt; ssize_t cmdlen = 1; xdgHandle xdg; bool no_argb = false; xcb_generic_event_t *event; xcb_query_tree_cookie_t tree_c; static struct option long_options[] = { { "help", 0, NULL, 'h' }, { "version", 0, NULL, 'v' }, { "config", 1, NULL, 'c' }, { "check", 0, NULL, 'k' }, { "no-argb", 0, NULL, 'a' }, { NULL, 0, NULL, 0 } }; /* event loop watchers */ ev_io xio = { .fd = -1 }; ev_check xcheck; ev_prepare a_refresh; ev_signal sigint; ev_signal sigterm; ev_signal sighup; /* clear the globalconf structure */ p_clear(&globalconf, 1); globalconf.keygrabber = LUA_REFNIL; globalconf.mousegrabber = LUA_REFNIL; buffer_init(&globalconf.startup_errors); /* save argv */ for(i = 0; i < argc; i++) cmdlen += a_strlen(argv[i]) + 1; awesome_argv = p_new(char, cmdlen); a_strcpy(awesome_argv, cmdlen, argv[0]); for(i = 1; i < argc; i++) { a_strcat(awesome_argv, cmdlen, " "); a_strcat(awesome_argv, cmdlen, argv[i]); } /* Text won't be printed correctly otherwise */ setlocale(LC_CTYPE, ""); /* Get XDG basedir data */ xdgInitHandle(&xdg); /* init lua */ luaA_init(&xdg); /* check args */ while((opt = getopt_long(argc, argv, "vhkc:a", long_options, NULL)) != -1) switch(opt) { case 'v': eprint_version(); break; case 'h': exit_help(EXIT_SUCCESS); break; case 'k': if(!luaA_parserc(&xdg, confpath, false)) { fprintf(stderr, "✘ Configuration file syntax error.\n"); return EXIT_FAILURE; } else { fprintf(stderr, "✔ Configuration file syntax OK.\n"); return EXIT_SUCCESS; } case 'c': if(a_strlen(optarg)) confpath = a_strdup(optarg); else fatal("-c option requires a file name"); break; case 'a': no_argb = true; break; } globalconf.loop = ev_default_loop(EVFLAG_NOSIGFD); /* register function for signals */ ev_signal_init(&sigint, exit_on_signal, SIGINT); ev_signal_init(&sigterm, exit_on_signal, SIGTERM); ev_signal_init(&sighup, restart_on_signal, SIGHUP); ev_signal_start(globalconf.loop, &sigint); ev_signal_start(globalconf.loop, &sigterm); ev_signal_start(globalconf.loop, &sighup); ev_unref(globalconf.loop); ev_unref(globalconf.loop); ev_unref(globalconf.loop); struct sigaction sa = { .sa_handler = signal_fatal, .sa_flags = 0 }; sigemptyset(&sa.sa_mask); sigaction(SIGSEGV, &sa, 0); /* X stuff */ globalconf.connection = xcb_connect(NULL, &globalconf.default_screen); if(xcb_connection_has_error(globalconf.connection)) fatal("cannot open display"); globalconf.screen = xcb_aux_get_screen(globalconf.connection, globalconf.default_screen); /* FIXME The following two assignments were swapped on purpose */ if(!no_argb) globalconf.visual = a_default_visual(globalconf.screen); if(!globalconf.visual) globalconf.visual = a_argb_visual(globalconf.screen); globalconf.default_depth = a_visual_depth(globalconf.screen, globalconf.visual->visual_id); globalconf.default_cmap = globalconf.screen->default_colormap; if(globalconf.default_depth != globalconf.screen->root_depth) { // We need our own color map if we aren't using the default depth globalconf.default_cmap = xcb_generate_id(globalconf.connection); xcb_create_colormap(globalconf.connection, XCB_COLORMAP_ALLOC_NONE, globalconf.default_cmap, globalconf.screen->root, globalconf.visual->visual_id); } /* Prefetch all the extensions we might need */ xcb_prefetch_extension_data(globalconf.connection, &xcb_big_requests_id); xcb_prefetch_extension_data(globalconf.connection, &xcb_test_id); xcb_prefetch_extension_data(globalconf.connection, &xcb_randr_id); xcb_prefetch_extension_data(globalconf.connection, &xcb_xinerama_id); /* initialize dbus */ a_dbus_init(); /* Get the file descriptor corresponding to the X connection */ xfd = xcb_get_file_descriptor(globalconf.connection); ev_io_init(&xio, &a_xcb_io_cb, xfd, EV_READ); ev_io_start(globalconf.loop, &xio); ev_check_init(&xcheck, &a_xcb_check_cb); ev_check_start(globalconf.loop, &xcheck); ev_unref(globalconf.loop); ev_prepare_init(&a_refresh, &a_refresh_cb); ev_prepare_start(globalconf.loop, &a_refresh); ev_unref(globalconf.loop); /* Grab server */ xcb_grab_server(globalconf.connection); /* Make sure there are no pending events. Since we didn't really do anything * at all yet, we will just discard all events which we received so far. * The above GrabServer should make sure no new events are generated. */ xcb_aux_sync(globalconf.connection); while ((event = xcb_poll_for_event(globalconf.connection)) != NULL) { /* Make sure errors are printed */ uint8_t response_type = XCB_EVENT_RESPONSE_TYPE(event); if(response_type == 0) event_handle(event); p_delete(&event); } { const uint32_t select_input_val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT; /* This causes an error if some other window manager is running */ xcb_change_window_attributes(globalconf.connection, globalconf.screen->root, XCB_CW_EVENT_MASK, &select_input_val); } /* Need to xcb_flush to validate error handler */ xcb_aux_sync(globalconf.connection); /* Process all errors in the queue if any. There can be no events yet, so if * this function returns something, it must be an error. */ if (xcb_poll_for_event(globalconf.connection) != NULL) fatal("another window manager is already running"); /* Prefetch the maximum request length */ xcb_prefetch_maximum_request_length(globalconf.connection); /* check for xtest extension */ const xcb_query_extension_reply_t *xtest_query; xtest_query = xcb_get_extension_data(globalconf.connection, &xcb_test_id); globalconf.have_xtest = xtest_query->present; /* Allocate the key symbols */ globalconf.keysyms = xcb_key_symbols_alloc(globalconf.connection); xcb_get_modifier_mapping_cookie_t xmapping_cookie = xcb_get_modifier_mapping_unchecked(globalconf.connection); /* init atom cache */ atoms_init(globalconf.connection); /* init screens information */ screen_scan(); xutil_lock_mask_get(globalconf.connection, xmapping_cookie, globalconf.keysyms, &globalconf.numlockmask, &globalconf.shiftlockmask, &globalconf.capslockmask, &globalconf.modeswitchmask); /* do this only for real screen */ ewmh_init(); systray_init(); /* init spawn (sn) */ spawn_init(); /* The default GC is just a newly created associated with a window with * depth globalconf.default_depth */ xcb_window_t tmp_win = xcb_generate_id(globalconf.connection); globalconf.gc = xcb_generate_id(globalconf.connection); xcb_create_window(globalconf.connection, globalconf.default_depth, tmp_win, globalconf.screen->root, -1, -1, 1, 1, 0, XCB_COPY_FROM_PARENT, globalconf.visual->visual_id, XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL | XCB_CW_COLORMAP, (const uint32_t []) { globalconf.screen->black_pixel, globalconf.screen->black_pixel, globalconf.default_cmap }); xcb_create_gc(globalconf.connection, globalconf.gc, tmp_win, XCB_GC_FOREGROUND | XCB_GC_BACKGROUND, (const uint32_t[]) { globalconf.screen->black_pixel, globalconf.screen->white_pixel }); xcb_destroy_window(globalconf.connection, tmp_win); /* Get the window tree associated to this screen */ tree_c = xcb_query_tree_unchecked(globalconf.connection, globalconf.screen->root); xcb_change_window_attributes(globalconf.connection, globalconf.screen->root, XCB_CW_EVENT_MASK, ROOT_WINDOW_EVENT_MASK); /* we will receive events, stop grabbing server */ xcb_ungrab_server(globalconf.connection); /* Parse and run configuration file */ if (!luaA_parserc(&xdg, confpath, true)) fatal("couldn't find any rc file"); p_delete(&confpath); xdgWipeHandle(&xdg); /* scan existing windows */ scan(tree_c); xcb_flush(globalconf.connection); /* main event loop */ ev_loop(globalconf.loop, 0); /* cleanup event loop */ ev_ref(globalconf.loop); ev_check_stop(globalconf.loop, &xcheck); ev_ref(globalconf.loop); ev_prepare_stop(globalconf.loop, &a_refresh); ev_ref(globalconf.loop); ev_io_stop(globalconf.loop, &xio); awesome_atexit(false); return EXIT_SUCCESS; }