summaryrefslogtreecommitdiff
path: root/src/main.h
blob: a4d630ab40f4fefca4da27b67ed8b594863c15d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/**
 * vimb - a webkit based vim like browser.
 *
 * Copyright (C) 2012-2013 Daniel Carl
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
 */

#ifndef _MAIN_H
#define _MAIN_H

#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <webkit/webkit.h>
#include <JavaScriptCore/JavaScript.h>
#include <fcntl.h>
#include <stdio.h>
#ifdef HAS_GTK3
#include <gdk/gdkx.h>
#include <gtk/gtkx.h>
#else
#endif
#include "config.h"

/* size of some I/O buffer */
#define BUF_SIZE  512

#define LENGTH(x) (sizeof x / sizeof x[0])

#ifdef DEBUG
#define PRINT_DEBUG(...) { \
    fprintf(stderr, "\n\033[31;1mDEBUG:\033[0m %s:%d:%s()\t", __FILE__, __LINE__, __func__); \
    fprintf(stderr, __VA_ARGS__);\
}
#define TIMER_START GTimer *__timer; {__timer = g_timer_new(); g_timer_start(__timer);}
#define TIMER_END {gulong __debug_micro = 0; gdouble __debug_elapsed = g_timer_elapsed(__timer, &__debug_micro);\
    PRINT_DEBUG("\033[33mtimer:\033[0m elapsed: %f, micro: %lu", __debug_elapsed, __debug_micro);\
    g_timer_destroy(__timer); \
}
#else
#define PRINT_DEBUG(message, ...)
#define TIMER_START
#define TIMER_END
#endif

#define GET_TEXT() (gtk_entry_get_text(GTK_ENTRY(vb.gui.inputbox)))
#define PUT_TEXT(x) { \
    gtk_entry_set_text(GTK_ENTRY(vb.gui.inputbox), x); \
    gtk_editable_set_position(GTK_EDITABLE(vb.gui.inputbox), -1); \
}
#define GET_URI() (webkit_web_view_get_uri(vb.gui.webview))
#define CLEAN_MODE(mode) ((mode) & (VB_MODE_NORMAL|VB_MODE_COMMAND|VB_MODE_INSERT))
#define CLEAR_INPUT() (vb_echo(VB_MSG_NORMAL, ""))
#define PRIMARY_CLIPBOARD() gtk_clipboard_get(GDK_SELECTION_PRIMARY)
#define SECONDARY_CLIPBOARD() gtk_clipboard_get(GDK_NONE)

#define OVERWRITE_STRING(t, s) if (t) {g_free(t); t = NULL;} t = g_strdup(s);

#define IS_ESCAPE_KEY(k, s) ((k == GDK_Escape && s == 0) || (k == GDK_c && s == GDK_CONTROL_MASK))
#define CLEAN_STATE_WITH_SHIFT(e) ((e)->state & (GDK_MOD1_MASK|GDK_MOD4_MASK|GDK_SHIFT_MASK|GDK_CONTROL_MASK))
#define CLEAN_STATE(e)            ((e)->state & (GDK_MOD1_MASK|GDK_MOD4_MASK|GDK_CONTROL_MASK))

#define file_lock_set(fd, cmd) \
{ \
    struct flock lock = { .l_type = cmd, .l_start = 0, .l_whence = SEEK_SET, .l_len = 0}; \
    fcntl(fd, F_SETLK, lock); \
}

#ifdef HAS_GTK3
#define VbColor GdkRGBA
#define VB_COLOR_PARSE(color, string)   (gdk_rgba_parse(color, string))
#define VB_COLOR_TO_STRING(color)       (gdk_rgba_to_string(color))
#define VB_WIDGET_OVERRIDE_BACKGROUND   gtk_widget_override_background_color
#define VB_WIDGET_OVERRIDE_BASE         gtk_widget_override_background_color
#define VB_WIDGET_OVERRIDE_COLOR        gtk_widget_override_color
#define VB_WIDGET_OVERRIDE_TEXT         gtk_widget_override_color
#define VB_WIDGET_OVERRIDE_FONT         gtk_widget_override_font

#define VB_GTK_STATE_NORMAL             GTK_STATE_FLAG_NORMAL
#define VB_GTK_STATE_ACTIVE             GTK_STATE_FLAG_ACTIVE
#define VB_GTK_STATE_SELECTED           GTK_STATE_FLAG_SELECTED
#define VB_WIDGET_SET_STATE(w, s)       gtk_widget_set_state_flags(w, s, true)

#else

#define VbColor GdkColor
#define VB_COLOR_PARSE(color, string)   (gdk_color_parse(string, color))
#define VB_COLOR_TO_STRING(color)       (gdk_color_to_string(color))
#define VB_WIDGET_OVERRIDE_BACKGROUND   gtk_widget_modify_bg
#define VB_WIDGET_OVERRIDE_BASE         gtk_widget_modify_base
#define VB_WIDGET_OVERRIDE_COLOR        gtk_widget_modify_fg
#define VB_WIDGET_OVERRIDE_TEXT         gtk_widget_modify_text
#define VB_WIDGET_OVERRIDE_FONT         gtk_widget_modify_font

#define VB_GTK_STATE_NORMAL             GTK_STATE_NORMAL
#define VB_GTK_STATE_ACTIVE             GTK_STATE_ACTIVE
#define VB_GTK_STATE_SELECTED           GTK_STATE_SELECTED
#define VB_WIDGET_SET_STATE(w, s)       gtk_widget_set_state(w, s)
#endif

/* enums */
typedef enum _vb_mode {
    /* main modes */
    VB_MODE_NORMAL        = 1<<0,
    VB_MODE_COMMAND       = 1<<1,
    VB_MODE_INSERT        = 1<<2,
    /* sub modes */
    VB_MODE_PASSTHROUGH   = 1<<3, /* normal or insert mode */
    VB_MODE_SEARCH        = 1<<4, /* normal mode */
    VB_MODE_COMPLETE      = 1<<5, /* command mode */
    VB_MODE_HINTING       = 1<<6, /* command mode */
} Mode;

typedef enum {
    VB_INPUT_UNKNOWN,
    VB_INPUT_SET              = 1<<0,
    VB_INPUT_OPEN             = 1<<1,
    VB_INPUT_TABOPEN          = 1<<2,
    VB_INPUT_COMMAND          = 1<<3,
    VB_INPUT_SEARCH_FORWARD   = 1<<4,
    VB_INPUT_SEARCH_BACKWARD  = 1<<5,
    VB_INPUT_ALL              = VB_INPUT_OPEN | VB_INPUT_TABOPEN | VB_INPUT_SET | VB_INPUT_COMMAND | VB_INPUT_SEARCH_FORWARD | VB_INPUT_SEARCH_BACKWARD,
} VbInputType;

enum {
    VB_NAVIG_BACK,
    VB_NAVIG_FORWARD,
    VB_NAVIG_RELOAD,
    VB_NAVIG_RELOAD_FORCE,
    VB_NAVIG_STOP_LOADING
};

enum {
    VB_TARGET_CURRENT,
    VB_TARGET_NEW
};

enum {
    VB_INPUT_CURRENT_URI = 1
};

/*
1 << 0:  0 = jump,              1 = scroll
1 << 1:  0 = vertical,          1 = horizontal
1 << 2:  0 = top/left,          1 = down/right
1 << 3:  0 = paging/halfpage,   1 = line
1 << 4:  0 = paging,            1 = halfpage
*/
enum {VB_SCROLL_TYPE_JUMP, VB_SCROLL_TYPE_SCROLL};
enum {
    VB_SCROLL_AXIS_V,
    VB_SCROLL_AXIS_H = (1 << 1)
};
enum {
    VB_SCROLL_DIRECTION_TOP,
    VB_SCROLL_DIRECTION_DOWN  = (1 << 2),
    VB_SCROLL_DIRECTION_LEFT  = VB_SCROLL_AXIS_H,
    VB_SCROLL_DIRECTION_RIGHT = VB_SCROLL_AXIS_H | (1 << 2)
};
enum {
    VB_SCROLL_UNIT_PAGE,
    VB_SCROLL_UNIT_LINE     = (1 << 3),
    VB_SCROLL_UNIT_HALFPAGE = (1 << 4)
};

typedef enum {
    VB_SEARCH_OFF,
    VB_SEARCH_FORWARD  = (1<<0),
    VB_SEARCH_BACKWARD = (1<<1),
} SearchDirection;

typedef enum {
    VB_MSG_NORMAL,
    VB_MSG_ERROR,
    VB_MSG_LAST
} MessageType;

typedef enum {
    VB_STATUS_NORMAL,
    VB_STATUS_SSL_VALID,
    VB_STATUS_SSL_INVALID,
    VB_STATUS_LAST
} StatusType;

typedef enum {
    VB_COMP_NORMAL,
    VB_COMP_ACTIVE,
    VB_COMP_LAST
} CompletionStyle;

typedef enum {
    FILES_CONFIG,
#ifdef FEATURE_COOKIE
    FILES_COOKIE,
#endif
    FILES_CLOSED,
    FILES_SCRIPT,
    FILES_HISTORY,
    FILES_COMMAND,
    FILES_SEARCH,
    FILES_BOOKMARK,
#ifdef FEATURE_QUEUE
    FILES_QUEUE,
#endif
    FILES_USER_STYLE,
    FILES_LAST
} VbFile;

typedef enum {
    TYPE_CHAR,
    TYPE_BOOLEAN,
    TYPE_INTEGER,
    TYPE_FLOAT,
    TYPE_COLOR,
    TYPE_FONT,
} Type;

enum {
    VB_CLIPBOARD_PRIMARY   = (1<<1),
    VB_CLIPBOARD_SECONDARY = (1<<2)
};

/* structs */
typedef struct {
    int  i;
    char *s;
} Arg;

/* statusbar */
typedef struct {
    GtkBox    *box;
    GtkWidget *left;
    GtkWidget *right;
} StatusBar;

/* gui */
typedef struct {
    GtkWidget          *window;
    GtkWidget          *scroll;
    WebKitWebView      *webview;
    WebKitWebInspector *inspector;
    GtkBox             *box;
    GtkWidget          *eventbox;
    GtkWidget          *inputbox;
    GtkWidget          *pane;
    StatusBar          statusbar;
    GtkScrollbar       *sb_h;
    GtkScrollbar       *sb_v;
    GtkAdjustment      *adjust_h;
    GtkAdjustment      *adjust_v;
} Gui;

/* state */
typedef struct {
    Mode            mode;
    char            modkey;
    guint           count;
    guint           progress;
    StatusType      status_type;
    MessageType     input_type;
    gboolean        is_inspecting;
    GList           *downloads;
} State;

typedef struct {
    time_t cookie_timeout;
    int    scrollstep;
    char   *home_page;
    char   *download_dir;
    guint  history_max;
    char   *editor_command;
} Config;

typedef struct {
    VbColor              input_fg[VB_MSG_LAST];
    VbColor              input_bg[VB_MSG_LAST];
    PangoFontDescription *input_font[VB_MSG_LAST];
    /* completion */
    VbColor              comp_fg[VB_COMP_LAST];
    VbColor              comp_bg[VB_COMP_LAST];
    PangoFontDescription *comp_font;
    /* status bar */
    VbColor              status_bg[VB_STATUS_LAST];
    VbColor              status_fg[VB_STATUS_LAST];
    PangoFontDescription *status_font[VB_STATUS_LAST];
} Style;

typedef struct {
    Gui             gui;
    State           state;

    char            *files[FILES_LAST];
    Config          config;
    Style           style;
    SoupSession     *session;
#ifdef HAS_GTK3
    Window          embed;
#else
    GdkNativeWindow embed;
#endif
    char            *custom_config;
} VbCore;

/* main object */
extern VbCore core;

/* functions */
void vb_echo_force(const MessageType type,gboolean hide, const char *error, ...);
void vb_echo(const MessageType type, gboolean hide, const char *error, ...);
gboolean vb_eval_script(WebKitWebFrame *frame, char *script, char *file, char **value);
gboolean vb_load_uri(const Arg *arg);
gboolean vb_set_clipboard(const Arg *arg);
gboolean vb_set_mode(Mode mode, gboolean clean);
void vb_set_widget_font(GtkWidget *widget, const VbColor *fg, const VbColor *bg, PangoFontDescription *font);
void vb_update_statusbar(void);
void vb_update_status_style(void);
void vb_update_input_style(void);
void vb_update_urlbar(const char *uri);
VbInputType vb_get_input_parts(const char* input, unsigned int use,
    const char **prefix, const char **clean);
gboolean vb_download(WebKitWebView *view, WebKitDownload *download, const char *path);
void vb_quit(void);

#endif /* end of include guard: _MAIN_H */