wizard: actually write the output config

This commit is contained in:
Michael Stapelberg
2011-05-01 13:56:35 +02:00
parent 9101f4cce2
commit 43ec3ddbaf
5 changed files with 146 additions and 14 deletions

View File

@ -18,6 +18,7 @@
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <err.h>
#include <stdint.h>
@ -43,6 +44,7 @@
while (0)
#include "xcb.h"
#include "ipc.h"
enum { STEP_WELCOME, STEP_GENERATE } current_step = STEP_WELCOME;
enum { MOD_ALT, MOD_SUPER } modifier = MOD_SUPER;
@ -52,7 +54,6 @@ static xcb_connection_t *conn;
static uint32_t font_id;
static uint32_t font_bold_id;
static char *socket_path;
static int sockfd;
static int font_height;
static int font_bold_height;
static xcb_window_t win;
@ -62,6 +63,7 @@ static xcb_key_symbols_t *symbols;
xcb_window_t root;
Display *dpy;
char *rewrite_binding(const char *bindingline);
static void finish();
/*
@ -205,36 +207,82 @@ static int handle_key_press(void *ignored, xcb_connection_t *conn, xcb_key_press
return 1;
}
/*
* Creates the config file and tells i3 to reload.
*
*/
static void finish() {
printf("finishing the wizard\n");
printf("creating \"%s\"...\n", config_path);
#if 0
dpy = XOpenDisplay(NULL);
if (!(dpy = XOpenDisplay(NULL)))
errx(1, "Could not connect to X11");
FILE *kc_config = fopen("../i3.config.kc", "r");
if (kc_config == NULL)
err(1, "Could not open input file \"%s\"", "../i3.config.kc");
FILE *ks_config = fopen(config_path, "w");
if (ks_config == NULL)
err(1, "Could not open output config file \"%s\"", config_path);
char *line = NULL;
size_t len = 0;
ssize_t read;
bool head_of_file = true;
/* write a header about auto-generation to the output file */
fputs("# This file has been auto-generated by i3-config-wizard(1).\n", ks_config);
fputs("# It will not be overwritten, so edit it as you like.\n", ks_config);
fputs("#\n", ks_config);
fputs("# Should you change your keyboard layout somewhen, delete\n", ks_config);
fputs("# this file and re-run i3-config-wizard(1).\n", ks_config);
fputs("#\n", ks_config);
fputs("# See http://i3wm.org/docs/userguide.html\n", ks_config);
while ((read = getline(&line, &len, kc_config)) != -1) {
/* See if that line is interesting by skipping leading whitespaces,
* then checking for 'bindcode' */
/* skip the warning block at the beginning of the input file */
if (head_of_file &&
strncmp("# WARNING", line, strlen("# WARNING")) == 0)
continue;
head_of_file = false;
/* Skip leading whitespace */
char *walk = line;
while (isspace(*walk) && walk < (line + len))
walk++;
if (strncmp(walk, "bindcode", strlen("bindcode")) != 0)
continue;
char *result = rewrite_binding(walk);
printf("in: %s", walk);
printf("out: %s", result);
free(result);
/* Set the modifier the user chose */
if (strncmp(walk, "set $mod ", strlen("set $mod ")) == 0) {
if (modifier == MOD_ALT)
fputs("set $mod Mod1\n", ks_config);
else fputs("set $mod Mod4\n", ks_config);
continue;
}
/* Check for 'bindcode'. If its not a bindcode line, we
* just copy it to the output file */
if (strncmp(walk, "bindcode", strlen("bindcode")) != 0) {
fputs(line, ks_config);
continue;
}
char *result = rewrite_binding(walk);
fputs(result, ks_config);
free(result);
}
/* sync to do our best in order to have the file really stored on disk */
fflush(ks_config);
fsync(fileno(ks_config));
free(line);
fclose(kc_config);
fclose(ks_config);
exit(0);
#endif
/* tell i3 to reload the config file */
int sockfd = connect_ipc(socket_path);
ipc_send_message(sockfd, strlen("reload"), 0, (uint8_t*)"reload");
close(sockfd);
exit(0);
}