Add operator for toggling cmd box on Windows.
authorNathan Letwory <nathan@letworyinteractive.com>
Mon, 18 Oct 2010 20:41:52 +0000 (20:41 +0000)
committerNathan Letwory <nathan@letworyinteractive.com>
Mon, 18 Oct 2010 20:41:52 +0000 (20:41 +0000)
Use through search menu (Toggle System Console) or
call bpy.ops.wm.toggle_console().

This is based on patch [#6927] Optional Console for Windows
by Fahrezal Effendi, submitted on July 10th, 2007 (!)

This paves the way for adding a command-line option to Blender to
toggle this cmd console, and for having a user preference option
for this. Command-line option I haven't added yet, as there seemed
to be problems with Python interpreter initialization (read: crashes).

This works by redirecting stdout and stderr to blenderlog.txt in user
temp directory (most likely %TEMP%\blenderlog.txt). When python problem
is fixed we can use this to always redirect stdout and stderr to this
logfile, making it also easier for us to ask users for this file in
bugreports.

source/blender/windowmanager/WM_api.h
source/blender/windowmanager/intern/wm_operators.c

index 197f99b..2a36e91 100644 (file)
@@ -119,7 +119,7 @@ wmKeyMapItem *WM_keymap_add_item(struct wmKeyMap *keymap, char *idname, int type
 wmKeyMapItem *WM_keymap_add_menu(struct wmKeyMap *keymap, char *idname, int type,
                                                                 int val, int modifier, int keymodifier);
 
-void         WM_keymap_remove_item(struct wmKeyMap *keymap, struct wmKeyMapItem *kmi);
+void           WM_keymap_remove_item(struct wmKeyMap *keymap, struct wmKeyMapItem *kmi);
 char            *WM_keymap_item_to_string(wmKeyMapItem *kmi, char *str, int len);
 
 wmKeyMap       *WM_keymap_list_find(ListBase *lb, char *idname, int spaceid, int regionid);
@@ -220,7 +220,7 @@ struct wmOperatorTypeMacro *WM_operatortype_macro_define(struct wmOperatorType *
 int                    WM_operator_poll                (struct bContext *C, struct wmOperatorType *ot);
 int                    WM_operator_call                (struct bContext *C, struct wmOperator *op);
 int                    WM_operator_repeat              (struct bContext *C, struct wmOperator *op);
-int         WM_operator_name_call      (struct bContext *C, const char *opstring, int context, struct PointerRNA *properties);
+int                    WM_operator_name_call   (struct bContext *C, const char *opstring, int context, struct PointerRNA *properties);
 int                    WM_operator_call_py(struct bContext *C, struct wmOperatorType *ot, int context, struct PointerRNA *properties, struct ReportList *reports);
 
 void           WM_operator_properties_alloc(struct PointerRNA **ptr, struct IDProperty **properties, const char *opstring); /* used for keymap and macro items */
@@ -342,6 +342,11 @@ void               WM_clipboard_text_set(char *buf, int selection);
 void           WM_progress_set(struct wmWindow *win, float progress);
 void           WM_progress_clear(struct wmWindow *win);
 
+#ifdef WIN32
+                       /* Windows System Console */
+void           WM_toggle_console(struct bContext *C, short show);
+#endif
+
 #ifdef __cplusplus
 }
 #endif
index de08c20..ccdb19a 100644 (file)
 #include <ctype.h>
 #include <stdio.h>
 #include <stddef.h>
-
+#ifdef WIN32
+#include <windows.h>
+#include <io.h>
+#endif
 
 #include "DNA_ID.h"
 #include "DNA_object_types.h"
@@ -2007,6 +2010,60 @@ static void WM_OT_quit_blender(wmOperatorType *ot)
        ot->poll= WM_operator_winactive;
 }
 
+/* *********************** */
+#ifdef WIN32
+static int console= 1;
+void WM_toggle_console(bContext *C, short show)
+{
+       if(show) {
+               FILE *fp;
+               char fn[FILE_MAX];
+               char tmp[FILE_MAXDIR];
+               BLI_where_is_temp(tmp, 1);
+               BLI_make_file_string("/", fn, tmp, "blenderlog.txt");
+               /* open the console */
+               AllocConsole();
+               
+               /* redirect stdin */
+               fp= freopen(fn, "r", stdin);
+               SetStdHandle(STD_INPUT_HANDLE, (HANDLE)_get_osfhandle(_fileno(stdin)));
+               /* redirect stdout */
+               fp= freopen(fn, "w", stdout);
+               SetStdHandle(STD_OUTPUT_HANDLE, (HANDLE)_get_osfhandle(_fileno(stdout)));
+               /* redirect stderr */
+               fp= freopen(fn, "w", stderr);
+               SetStdHandle(STD_ERROR_HANDLE, (HANDLE)_get_osfhandle(_fileno(stderr)));
+               
+               console= 1;
+       }
+       else {
+               FreeConsole();
+               console= 0;
+       }
+}
+
+static int wm_toggle_console_op(bContext *C, wmOperator *op)
+{
+       if(console) {
+               WM_toggle_console(C, 0);
+       }
+       else {
+               WM_toggle_console(C, 1);
+       }
+       return OPERATOR_FINISHED;
+}
+
+static void WM_OT_toggle_console(wmOperatorType *ot)
+{
+       ot->name= "Toggle System Console";
+       ot->idname= "WM_OT_toggle_console";
+       ot->description= "Toggle System Console";
+       
+       ot->exec= wm_toggle_console_op;
+       ot->poll= WM_operator_winactive;
+}
+#endif
+
 /* ************ default paint cursors, draw always around cursor *********** */
 /*
  - returns handler to free 
@@ -3079,6 +3136,9 @@ void wm_operatortype_init(void)
        WM_operatortype_append(WM_OT_splash);
        WM_operatortype_append(WM_OT_search_menu);
        WM_operatortype_append(WM_OT_call_menu);
+#ifdef WIN32
+       WM_operatortype_append(WM_OT_toggle_console);
+#endif
 
 #ifdef WITH_COLLADA
        /* XXX: move these */