]> git.sur5r.net Git - i3/i3/blobdiff - i3-config-wizard/main.c
logging: make libi3 use verboselog()/errorlog(), provide it in each caller
[i3/i3] / i3-config-wizard / main.c
index 84a7f77ee22f3f76b688c89aed0e8d0c7cfa9a1b..679c5e6db5206eefb951c7d115099cb7fbf86044 100644 (file)
@@ -2,12 +2,25 @@
  * vim:ts=4:sw=4:expandtab
  *
  * i3 - an improved dynamic tiling window manager
- * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
+ * © 2009-2012 Michael Stapelberg and contributors (see also: LICENSE)
  *
  * i3-config-wizard: Program to convert configs using keycodes to configs using
  *                   keysyms.
  *
  */
+#if defined(__FreeBSD__)
+#include <sys/param.h>
+#endif
+
+/* For systems without getline, fall back to fgetln */
+#if defined(__APPLE__) || (defined(__FreeBSD__) && __FreeBSD_version < 800000) || defined(__OpenBSD__)
+#define USE_FGETLN
+#elif defined(__FreeBSD__)
+/* Defining this macro before including stdio.h is necessary in order to have
+ * a prototype for getline in FreeBSD. */
+#define _WITH_GETLINE
+#endif
+
 #include <ev.h>
 #include <stdio.h>
 #include <sys/types.h>
@@ -56,6 +69,7 @@ enum { MOD_Mod1, MOD_Mod4 } modifier = MOD_Mod4;
 static char *config_path;
 static uint32_t xcb_numlock_mask;
 xcb_connection_t *conn;
+xcb_screen_t *root_screen;
 static xcb_get_modifier_mapping_reply_t *modmap_reply;
 static i3Font font;
 static i3Font bold_font;
@@ -70,6 +84,26 @@ Display *dpy;
 char *rewrite_binding(const char *bindingline);
 static void finish();
 
+/*
+ * Having verboselog() and errorlog() is necessary when using libi3.
+ *
+ */
+void verboselog(char *fmt, ...) {
+    va_list args;
+
+    va_start(args, fmt);
+    vfprintf(stdout, fmt, args);
+    va_end(args);
+}
+
+void errorlog(char *fmt, ...) {
+    va_list args;
+
+    va_start(args, fmt);
+    vfprintf(stderr, fmt, args);
+    va_end(args);
+}
+
 /*
  * This function resolves ~ in pathnames.
  * It may resolve wildcards in the first part of the path, but if no match
@@ -115,7 +149,7 @@ static int handle_expose() {
     set_font(&font);
 
 #define txt(x, row, text) \
-    draw_text(text, strlen(text), false, pixmap, pixmap_gc,\
+    draw_text_ascii(text, pixmap, pixmap_gc,\
             x, (row - 1) * font.height + 4, 300 - x * 2)
 
     if (current_step == STEP_WELCOME) {
@@ -155,8 +189,8 @@ static int handle_expose() {
         set_font(&bold_font);
         set_font_colors(pixmap_gc, get_colorpixel("#FFFFFF"), get_colorpixel("#000000"));
         if (modifier == MOD_Mod4)
-            txt(31, 4, "<Win>");
-        else txt(31, 5, "<Alt>");
+            txt(10, 4, "-> <Win>");
+        else txt(10, 5, "-> <Alt>");
 
         /* green */
         set_font(&font);
@@ -282,10 +316,11 @@ static void finish() {
     FILE *ks_config = fopen(config_path, "w");
     if (ks_config == NULL)
         err(1, "Could not open output config file \"%s\"", config_path);
+    free(config_path);
 
     char *line = NULL;
     size_t len = 0;
-#if !defined(__APPLE__)
+#ifndef USE_FGETLN
     ssize_t read;
 #endif
     bool head_of_file = true;
@@ -298,10 +333,16 @@ static void finish() {
     fputs("# this file and re-run i3-config-wizard(1).\n", ks_config);
     fputs("#\n", ks_config);
 
-#if defined(__APPLE__)
-    while ((line = fgetln(kc_config, &len)) != NULL) {
+#ifdef USE_FGETLN
+    char *buf = NULL;
+    while ((buf = fgetln(kc_config, &len)) != NULL) {
+        /* fgetln does not return null-terminated strings */
+        FREE(line);
+        sasprintf(&line, "%.*s", len, buf);
 #else
-    while ((read = getline(&line, &len, kc_config)) != -1) {
+    size_t linecap = 0;
+    while ((read = getline(&line, &linecap, kc_config)) != -1) {
+        len = strlen(line);
 #endif
         /* skip the warning block at the beginning of the input file */
         if (head_of_file &&
@@ -312,8 +353,11 @@ static void finish() {
 
         /* Skip leading whitespace */
         char *walk = line;
-        while (isspace(*walk) && walk < (line + len))
+        while (isspace(*walk) && walk < (line + len)) {
+            /* Pre-output the skipped whitespaces to keep proper indentation */
+            fputc(*walk, ks_config);
             walk++;
+        }
 
         /* Set the modifier the user chose */
         if (strncmp(walk, "set $mod ", strlen("set $mod ")) == 0) {
@@ -326,7 +370,7 @@ static void finish() {
         /* Check for 'bindcode'. If it’s not a bindcode line, we
          * just copy it to the output file */
         if (strncmp(walk, "bindcode", strlen("bindcode")) != 0) {
-            fputs(line, ks_config);
+            fputs(walk, ks_config);
             continue;
         }
         char *result = rewrite_binding(walk);
@@ -338,7 +382,10 @@ static void finish() {
     fflush(ks_config);
     fsync(fileno(ks_config));
 
+#ifndef USE_FGETLN
     free(line);
+#endif
+
     fclose(kc_config);
     fclose(ks_config);
 
@@ -410,7 +457,7 @@ int main(int argc, char *argv[]) {
     unlink(config_path);
 
     if (socket_path == NULL)
-        socket_path = socket_path_from_x11();
+        socket_path = root_atom_contents("I3_SOCKET_PATH");
 
     if (socket_path == NULL)
         socket_path = "/tmp/i3-ipc.sock";
@@ -430,7 +477,7 @@ int main(int argc, char *argv[]) {
     #include "atoms.xmacro"
     #undef xmacro
 
-    xcb_screen_t *root_screen = xcb_aux_get_screen(conn, screens);
+    root_screen = xcb_aux_get_screen(conn, screens);
     root = root_screen->root;
 
     if (!(modmap_reply = xcb_get_modifier_mapping_reply(conn, modmap_cookie, NULL)))