summaryrefslogtreecommitdiff
path: root/webskeleton.c
blob: 394edfff1a0924e0b4afb5e7e2fbb1672f56f714 (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
/*
 *  This native HTML container is derived from the EFL based MiniBrowser
 *  source in the webkit tree.
 * 
 *  Copyright 2012 Samsung Electronics
 *  Copyright 2012 Intel Corporation. All rights reserved.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library 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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <EWebKit2.h>
#include <Ecore.h>
#include <Ecore_Evas.h>
#include <Eina.h>
#include <Evas.h>
#include <getopt.h>

static const char DEFAULT_URL[] = "http://www.linuxfoundation.org/";
static const char APP_NAME[] = "WebSkeleton";
static int width = 800;
static int height = 600;
Eina_Bool fullscreen = EINA_FALSE;

#define info(format, args...)                   \
    do {                                        \
        if (verbose)                            \
            printf(format, ##args);             \
    } while (0)

static int verbose = 0;

typedef struct _Context {
    Ecore_Evas *ee;
    Evas *evas;
    Evas_Object *bg;
    Evas_Object *browser;
} Context;

static Eina_Bool main_signal_exit(void *data, int ev_type, void *ev)
{
    ecore_main_loop_quit();
    return EINA_TRUE;
}

static void on_ecore_evas_resize(Ecore_Evas *ee)
{
    Evas_Object *webview;
    Evas_Object *bg;
    int w, h;

    ecore_evas_geometry_get(ee, NULL, NULL, &w, &h);

    bg = evas_object_name_find(ecore_evas_get(ee), "bg");
    evas_object_move(bg, 0, 0);
    evas_object_resize(bg, w, h);

    webview = evas_object_name_find(ecore_evas_get(ee), "browser");
    evas_object_move(webview, 0, 0);
    evas_object_resize(webview, w, h);
}

static void
on_key_down(void *data, Evas *e, Evas_Object *obj, void *event_info)
{
    Evas_Event_Key_Down *ev = (Evas_Event_Key_Down*) event_info;
    Context *app = (Context *)data;
    const Evas_Modifier *modifier = evas_key_modifier_get(e);

    if (!strcmp(ev->key, "Left") && evas_key_modifier_is_set(modifier, "Alt")) {
        info("Alt-Left was pressed, moving back in history\n");
        if (!ewk_view_back(obj))
            info("Back ignored: No back history\n");
    } else if (!strcmp(ev->key, "Right") &&
               evas_key_modifier_is_set(modifier, "Alt")) {
        info("Alt-Right was pressed, moving forward in history\n");
        if (!ewk_view_forward(obj))
            info("Forward ignored: No forward history\n");
    } else if (!strcmp(ev->key, "r") && 
               evas_key_modifier_is_set(modifier, "Control")) {
        info("Control-r was pressed, reloading.\n");
        ewk_view_reload(obj);
    } else if (!strcmp(ev->key, "F6")) {
        info("Stop (F6) was pressed, stop loading.\n");
        ewk_view_stop(obj);
    } else if (!strcmp(ev->key, "F11")) {
        info("Fullscreen (F12) was pressed, toggling fullscreen view.\n");
        fullscreen = !fullscreen;
        ecore_evas_fullscreen_set(app->ee, fullscreen);
    } else if (!strcmp(ev->key, "Escape")) {
        info("Escape was pressed, exiting....\n");
        exit(0);
    }
}

static void
title_set(Ecore_Evas *ee, const char *title, int progress)
{
    Eina_Strbuf* buffer;

    if (!title || !*title) {
        ecore_evas_title_set(ee, APP_NAME);
        return;
    }

    buffer = eina_strbuf_new();
    if (progress < 100)
        eina_strbuf_append_printf(buffer, "%s (%d%%) - %s", title, progress, APP_NAME);
    else
        eina_strbuf_append_printf(buffer, "%s - %s", title, APP_NAME);

    ecore_evas_title_set(ee, eina_strbuf_string_get(buffer));
    eina_strbuf_free(buffer);
}

static void
on_title_changed(void *user_data, Evas_Object *webview, void *event_info)
{
    Context *app = (Context *)user_data;
    const char *title = (const char *)event_info;

    title_set(app->ee, title, 100);
}

static void
on_progress(void *user_data, Evas_Object *webview, void *event_info)
{
    Context *app = (Context *)user_data;
    double progress = *(double *)event_info;

    title_set(app->ee, ewk_view_title_get(app->browser), progress * 100);
}

static void
on_error(void *user_data, Evas_Object *webview, void *event_info)
{
    Eina_Strbuf* buffer;
    Ewk_Error *error = (Ewk_Error *)event_info;

    buffer = eina_strbuf_new();
    eina_strbuf_append_printf(buffer, "<html><body><div style=\"color:#ff0000\">ERROR!</div><br><div>Code: %d<br>Description: %s<br>URL: %s</div></body</html>",
                              ewk_error_code_get(error), ewk_error_description_get(error), ewk_error_url_get(error));

    ewk_view_html_contents_set(webview, eina_strbuf_string_get(buffer), 0);

    eina_strbuf_free(buffer);
}

static Context *create_context(const char *url, Eina_Bool fullscreen)
{
    Context *app = malloc(sizeof(Context));

    app->ee = ecore_evas_new(0, 0, 0, width, height, 0);

    ecore_evas_title_set(app->ee, APP_NAME);
    ecore_evas_callback_resize_set(app->ee, on_ecore_evas_resize);
    ecore_evas_borderless_set(app->ee, 0);
    ecore_evas_fullscreen_set(app->ee, fullscreen);
    ecore_evas_show(app->ee);

    app->evas = ecore_evas_get(app->ee);

    app->bg = evas_object_rectangle_add(app->evas);
    evas_object_name_set(app->bg, "bg");
    evas_object_size_hint_weight_set(app->bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);

    evas_object_move(app->bg, 0, 0);
    evas_object_resize(app->bg, width, height);
    evas_object_color_set(app->bg, 255, 150, 150, 255);
    evas_object_show(app->bg);

    /* Create webview */
    app->browser = ewk_view_add(app->evas);
    evas_object_name_set(app->browser, "browser");

    evas_object_smart_callback_add(app->browser, "load,error", on_error, app);
    evas_object_smart_callback_add(app->browser, "load,progress", on_progress, app);
    evas_object_smart_callback_add(app->browser, "title,changed", on_title_changed, app);

    evas_object_event_callback_add(app->browser, EVAS_CALLBACK_KEY_DOWN, on_key_down, app);

    evas_object_size_hint_weight_set(app->browser, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
    evas_object_resize(app->browser, width, height);
    evas_object_show(app->browser);
    evas_object_focus_set(app->browser, EINA_TRUE);

    ewk_view_uri_set(app->browser, url);

    return app;
}

int main(int argc, char *argv[])
{
    const char *url = NULL;
    int n = 0, valid_option_count = 0;
    Eina_Bool fullscreen = EINA_FALSE;

    if (!ecore_evas_init())
        return EXIT_FAILURE;
    if (!elm_init())
        return EXIT_FAILURE;

    static struct option options[] = {
        { "help",	no_argument,       NULL, 'h' },
        { "fullscreen", no_argument,       NULL, 'f' },
        { "url",        required_argument, NULL, 'u' },
        { "width",      required_argument, NULL, 'w' },
        { "height",     required_argument, NULL, 'd' },
        { NULL, 0, 0, 0 }
    };

    while (n >= 0) {
        n = getopt_long(argc, argv, "hfu:w:d:", options, NULL);
        if (n < 0)
            continue;
        switch (n) {
        case 'u':
            url = strdup(optarg);
            break;
        case 'w':
            width = atoi(optarg);
            break;
        case 'd':
            height = atoi(optarg);
            break;
        case 'f':
            fullscreen = EINA_TRUE;
            break;
        case 'h':
        default:
            fprintf(stderr, "Usage: %s [--help] [--fullscreen] [--width=<w>] [--height=<d>] [URL]\n", argv[0]);
            exit(-1);
        }
        valid_option_count++;
    }
    
    if (!url) {
        if (valid_option_count < argc - 1)
            url = strdup(argv[argc - 1]);
        else
            url = DEFAULT_URL;
    }

    Context *browser = create_context(url, fullscreen);
    Ecore_Event_Handler *handle = ecore_event_handler_add(ECORE_EVENT_SIGNAL_EXIT, main_signal_exit, 0);

    ecore_main_loop_begin();

    ecore_event_handler_del(handle);
    ecore_evas_free(browser->ee);
    free(browser);

    ecore_evas_shutdown();

    return 0;
}

/* Local Variables:      */
/* mode:c                */
/* c-basic-offset:4      */
/* indent-tabs-mode: nil */
/* End:                  */