style: start enforcing coding style

Previous .clang-format wasn't processed

$ clang-format -style=webkit -dump-config > .clang-format
$ clang-format -i rose.c
This commit is contained in:
NunoSempere 2023-03-28 10:14:13 -06:00
parent 527d88efa6
commit ec97a8f3f3
2 changed files with 472 additions and 366 deletions

View File

@ -1,37 +1,137 @@
# vim:ft=yaml ---
Language: Cpp
# BasedOnStyle: WebKit
AccessModifierOffset: -4
AlignAfterOpenBracket: DontAlign
AlignConsecutiveMacros: false
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Right
AlignOperands: false
AlignTrailingComments: false
AllowAllArgumentsOnNextLine: true
AllowAllConstructorInitializersOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: Empty
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortLambdasOnASingleLine: All
AllowShortIfStatementsOnASingleLine: Never
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: MultiLine
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: true
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBinaryOperators: All
BreakBeforeBraces: WebKit
BreakBeforeInheritanceComma: false
BreakInheritanceList: BeforeColon
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeComma
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 0
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: false
DeriveLineEnding: true
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: false
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IncludeBlocks: Preserve
IncludeCategories:
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
Priority: 2
SortPriority: 0
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
Priority: 3
SortPriority: 0
- Regex: '.*'
Priority: 1
SortPriority: 0
IncludeIsMainRegex: '(Test)?$'
IncludeIsMainSourceRegex: ''
IndentCaseLabels: false
IndentGotoLabels: true
IndentPPDirectives: None
IndentWidth: 4
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: Inner
ObjCBinPackProtocolList: Auto
ObjCBlockIndentWidth: 4
ObjCSpaceAfterProperty: true
ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Left
ReflowComments: true
SortIncludes: true
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: true
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyBlock: true
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInConditionalStatement: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
SpaceBeforeSquareBrackets: false
Standard: Latest
StatementMacros:
- Q_UNUSED
- QT_REQUIRE_VERSION
TabWidth: 8
UseCRLF: false
UseTab: Never
...
IndentWidth: 8
BreakBeforeBraces: Linux
UseTab: Always
AlignArrayOfStructures: Left
AllowShortFunctionsOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: WithoutElse
AllowShortLoopsOnASingleLine: True
AlwaysBreakBeforeMultilineStrings: True
BreakBeforeTernaryOperators: True
BreakStringLiterals: True
ColumnLimit: 100
IncludeBlocks: Regroup
KeepEmptyLinesAtTheStartOfBlocks: True
RemoveBracesLLVM: True
SortIncludes: CaseInsensitive
SortUsingDeclarations: True
SpaceAfterLogicalNot: True
SpaceAfterCStyleCast: True
SpaceAfterTemplateKeyword: False
PointerAlignment: Right
SpaceBeforeAssignmentOperators: True
SpaceBeforeCaseColon: False
SpaceBeforeCpp11BracedList: True
SpaceBeforeCtorInitializerColon: False
SpaceBeforeInheritanceColon: False
SpaceBeforeParens: Custom
SpaceBeforeParensOptions:
AfterControlStatements: True
AfterForeachMacros: True
AfterFunctionDeclarationName: False
AfterFunctionDefinitionName: False
AfterIfMacros: True
AfterOverloadedOperator: False
BeforeNonEmptyParentheses: False
SpaceBeforeRangeBasedForLoopColon: True

660
rose.c
View File

@ -8,9 +8,9 @@
* This code does not come with any warranty. * This code does not come with any warranty.
* *
*/ */
#include <stdbool.h>
#include <stdlib.h> // necessary for free, malloc. #include <stdlib.h> // necessary for free, malloc.
#include <string.h> #include <string.h>
#include <stdbool.h>
#include <webkit2/webkit2.h> #include <webkit2/webkit2.h>
#include "config.h" #include "config.h"
@ -32,436 +32,442 @@ int CUSTOM_USER_AGENT = true;
// - set their variable to true // - set their variable to true
// - in build.sh, uncomment: REQS= #./plugins/*/*.c // - in build.sh, uncomment: REQS= #./plugins/*/*.c
// 2. Remove stand_in code; // 2. Remove stand_in code;
// - Comment out #include "plugins/stand_in/stand_in.h" line, or edit it together with stand_in.c so as to not include the plugin functions. // - Comment out #include "plugins/stand_in/stand_in.h" line, or edit it
// together with stand_in.c so as to not include the plugin functions.
// - In build.sh, comment out REQS=./plugins/stand_in/stand_in.c // - In build.sh, comment out REQS=./plugins/stand_in/stand_in.c
#define CACHE \ #define CACHE \
"base-cache-directory", CACHE_DIR, \ "base-cache-directory", CACHE_DIR, "base-data-directory", CACHE_DIR, \
"base-data-directory", CACHE_DIR, \ "disk-cache-directory", CACHE_DIR, "dom-cache-directory", CACHE_DIR, \
"disk-cache-directory", CACHE_DIR, \ "hsts-cache-directory", CACHE_DIR, "indexeddb-directory", CACHE_DIR, \
"dom-cache-directory", CACHE_DIR, \ "itp-directory", CACHE_DIR, "local-storage-directory", CACHE_DIR, \
"hsts-cache-directory", CACHE_DIR, \ "offline-application-cache-directory", CACHE_DIR, \
"indexeddb-directory", CACHE_DIR, \ "service-worker-registrations-directory", CACHE_DIR
"itp-directory", CACHE_DIR, \
"local-storage-directory", CACHE_DIR, \
"offline-application-cache-directory", CACHE_DIR, \
"service-worker-registrations-directory", CACHE_DIR
enum { _SEARCH, _FIND, _HIDDEN }; enum { _SEARCH,
_FIND,
_HIDDEN };
static int entry_mode; static int entry_mode;
static GtkWindow *window; static GtkWindow* window;
static GtkHeaderBar *bar; static GtkHeaderBar* bar;
static GtkEntryBuffer *search_buf; static GtkEntryBuffer* search_buf;
static GtkEntry *search; static GtkEntry* search;
WebKitWebView *webview_new() WebKitWebView* webview_new()
{ {
char *style; char* style;
WebKitSettings *settings; WebKitSettings* settings;
WebKitWebContext *web_context; WebKitWebContext* web_context;
WebKitCookieManager *cookiemanager; WebKitCookieManager* cookiemanager;
WebKitUserContentManager *contentmanager; WebKitUserContentManager* contentmanager;
settings = webkit_settings_new_with_settings(WEBKIT, NULL); settings = webkit_settings_new_with_settings(WEBKIT, NULL);
if(CUSTOM_USER_AGENT){ if (CUSTOM_USER_AGENT) {
webkit_settings_set_user_agent (settings, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36"); webkit_settings_set_user_agent(
// See: <https://www.useragents.me/> for some common user agents settings,
} "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, "
web_context = webkit_web_context_new_with_website_data_manager( "like Gecko) Chrome/110.0.0.0 Safari/537.36");
webkit_website_data_manager_new(CACHE, NULL)); // See: <https://www.useragents.me/> for some common user agents
contentmanager = webkit_user_content_manager_new(); }
cookiemanager = webkit_web_context_get_cookie_manager(web_context); web_context = webkit_web_context_new_with_website_data_manager(
webkit_website_data_manager_new(CACHE, NULL));
contentmanager = webkit_user_content_manager_new();
cookiemanager = webkit_web_context_get_cookie_manager(web_context);
webkit_cookie_manager_set_persistent_storage(cookiemanager, CACHE_DIR "/cookies.sqlite", webkit_cookie_manager_set_persistent_storage(
WEBKIT_COOKIE_PERSISTENT_STORAGE_SQLITE); cookiemanager, CACHE_DIR "/cookies.sqlite",
WEBKIT_COOKIE_PERSISTENT_STORAGE_SQLITE);
webkit_cookie_manager_set_accept_policy(cookiemanager, WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS); webkit_cookie_manager_set_accept_policy(cookiemanager,
WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS);
webkit_web_context_set_process_model(web_context, webkit_web_context_set_process_model(
WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES); web_context, WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
if (g_file_get_contents("~/.config/rose/style.css", &style, NULL, NULL)) if (g_file_get_contents("~/.config/rose/style.css", &style, NULL, NULL))
webkit_user_content_manager_add_style_sheet( webkit_user_content_manager_add_style_sheet(
contentmanager, contentmanager, webkit_user_style_sheet_new(style, WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES, WEBKIT_USER_STYLE_LEVEL_USER, NULL, NULL));
webkit_user_style_sheet_new(style, WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES,
WEBKIT_USER_STYLE_LEVEL_USER, NULL, NULL));
return g_object_new(WEBKIT_TYPE_WEB_VIEW, "settings", settings, "web-context", web_context, return g_object_new(WEBKIT_TYPE_WEB_VIEW, "settings", settings, "web-context",
"user-content-manager", contentmanager, NULL); web_context, "user-content-manager", contentmanager,
NULL);
} }
WebKitWebView *notebook_get_webview(GtkNotebook *notebook) WebKitWebView* notebook_get_webview(GtkNotebook* notebook)
{
return WEBKIT_WEB_VIEW(
gtk_notebook_get_nth_page(notebook, gtk_notebook_get_current_page(notebook)));
}
void load_uri(WebKitWebView *view, const char *uri)
{ {
if (g_str_has_prefix(uri, "http://") || g_str_has_prefix(uri, "https://") || return WEBKIT_WEB_VIEW(gtk_notebook_get_nth_page(
g_str_has_prefix(uri, "file://") || g_str_has_prefix(uri, "about:")) { notebook, gtk_notebook_get_current_page(notebook)));
webkit_web_view_load_uri(view, uri);
} else {
// webkit_web_view_load_uri(view, uri);
char tmp[strlen(uri) + strlen(SEARCH)];
snprintf(tmp, sizeof(tmp), SEARCH, uri);
webkit_web_view_load_uri(view, tmp);
}
} }
void redirect_if_annoying(WebKitWebView *view, const char *uri){ void load_uri(WebKitWebView* view, const char* uri)
int l = LIBRE_N + strlen(uri) + 1; {
char uri_filtered[l]; if (g_str_has_prefix(uri, "http://") || g_str_has_prefix(uri, "https://") || g_str_has_prefix(uri, "file://") || g_str_has_prefix(uri, "about:")) {
str_init(uri_filtered, l); webkit_web_view_load_uri(view, uri);
} else {
// webkit_web_view_load_uri(view, uri);
char tmp[strlen(uri) + strlen(SEARCH)];
snprintf(tmp, sizeof(tmp), SEARCH, uri);
webkit_web_view_load_uri(view, tmp);
}
}
int check = libre_redirect(uri, uri_filtered); void redirect_if_annoying(WebKitWebView* view, const char* uri)
{
int l = LIBRE_N + strlen(uri) + 1;
char uri_filtered[l];
str_init(uri_filtered, l);
if (check == 2){ int check = libre_redirect(uri, uri_filtered);
webkit_web_view_load_uri(view, uri_filtered);
if (check == 2) {
webkit_web_view_load_uri(view, uri_filtered);
}
}
void load_changed(WebKitWebView* self, WebKitLoadEvent load_event,
GtkNotebook* notebook)
{
switch (load_event) {
/* see <https://webkitgtk.org/reference/webkit2gtk/2.5.1/WebKitWebView.html>
*/
case WEBKIT_LOAD_STARTED:
if (CUSTOM_STYLE_ENABLED) {
char* style_js = malloc(STYLE_N + 1);
read_style_js(style_js);
webkit_web_view_run_javascript(notebook_get_webview(notebook), style_js,
NULL, NULL, NULL);
free(style_js);
} }
if (LIBRE_REDIRECT_ENABLED) {
} redirect_if_annoying(self, webkit_web_view_get_uri(self));
}
void load_changed(WebKitWebView *self, WebKitLoadEvent load_event, GtkNotebook *notebook) break;
{ case WEBKIT_LOAD_REDIRECTED:
switch (load_event) { if (LIBRE_REDIRECT_ENABLED) {
/* see <https://webkitgtk.org/reference/webkit2gtk/2.5.1/WebKitWebView.html> */ redirect_if_annoying(self, webkit_web_view_get_uri(self));
case WEBKIT_LOAD_STARTED: }
if(CUSTOM_STYLE_ENABLED){ break;
char* style_js = malloc(STYLE_N+1); case WEBKIT_LOAD_COMMITTED:
read_style_js(style_js); if (LIBRE_REDIRECT_ENABLED) {
webkit_web_view_run_javascript(notebook_get_webview(notebook), redirect_if_annoying(self, webkit_web_view_get_uri(self));
style_js, }
NULL, NULL, NULL); if (CUSTOM_STYLE_ENABLED) {
free(style_js); char* style_js = malloc(STYLE_N + 1);
} read_style_js(style_js);
if(LIBRE_REDIRECT_ENABLED){ webkit_web_view_run_javascript(notebook_get_webview(notebook), style_js,
redirect_if_annoying(self, webkit_web_view_get_uri(self)); NULL, NULL, NULL);
} free(style_js);
break; }
case WEBKIT_LOAD_REDIRECTED: break;
if(LIBRE_REDIRECT_ENABLED){ case WEBKIT_LOAD_FINISHED: {
redirect_if_annoying(self, webkit_web_view_get_uri(self)); /* Add gtk tab title */
} const char* webpage_title = webkit_web_view_get_title(self);
break; const int max_length = 25;
case WEBKIT_LOAD_COMMITTED: char tab_title[max_length + 1];
if(LIBRE_REDIRECT_ENABLED){ if (webpage_title != NULL) {
redirect_if_annoying(self, webkit_web_view_get_uri(self)); for (int i = 0; i < (max_length); i++) {
} tab_title[i] = webpage_title[i];
if(CUSTOM_STYLE_ENABLED){ if (webpage_title[i] == '\0') {
char* style_js = malloc(STYLE_N+1); break;
read_style_js(style_js);
webkit_web_view_run_javascript(notebook_get_webview(notebook),
style_js,
NULL, NULL, NULL);
free(style_js);
}
break;
case WEBKIT_LOAD_FINISHED:
{
/* Add gtk tab title */
const char* webpage_title = webkit_web_view_get_title(self);
const int max_length = 25;
char tab_title[max_length + 1];
if(webpage_title != NULL){
for(int i = 0; i<(max_length); i++){
tab_title[i] = webpage_title[i];
if(webpage_title[i] == '\0'){
break;
}
}
tab_title[max_length] = '\0';
}
gtk_notebook_set_tab_label_text(notebook, GTK_WIDGET(self),
webpage_title == NULL ? "" : tab_title );
// gtk_widget_hide(GTK_WIDGET(bar));
} }
}
tab_title[max_length] = '\0';
} }
gtk_notebook_set_tab_label_text(notebook, GTK_WIDGET(self),
webpage_title == NULL ? "" : tab_title);
// gtk_widget_hide(GTK_WIDGET(bar));
}
}
} }
void notebook_append(GtkNotebook *notebook, const char *uri); void notebook_append(GtkNotebook* notebook, const char* uri);
/* notebook_append calls handle_create, but handle_create also calls notebook_append. /* notebook_append calls handle_create, but handle_create also calls
* Therefore we need to declare notebook_append, so that handle_create_new_tab knows its type. * notebook_append. Therefore we need to declare notebook_append, so that
* handle_create_new_tab knows its type.
*/ */
GtkWidget* handle_create_new_tab(WebKitWebView *self, WebKitNavigationAction *navigation_action, GtkNotebook *notebook){ GtkWidget* handle_create_new_tab(WebKitWebView* self,
WebKitURIRequest *uri_request = webkit_navigation_action_get_request(navigation_action); WebKitNavigationAction* navigation_action,
const char *uri = webkit_uri_request_get_uri (uri_request); GtkNotebook* notebook)
printf("Creating new window: %s\n", uri); {
notebook_append(notebook, uri); WebKitURIRequest* uri_request = webkit_navigation_action_get_request(navigation_action);
gtk_notebook_set_show_tabs(notebook, true); const char* uri = webkit_uri_request_get_uri(uri_request);
return NULL; printf("Creating new window: %s\n", uri);
/* WebKitGTK documentation recommends returning the new webview. notebook_append(notebook, uri);
* I imagine that this might allow e.g., to go back in a new tab gtk_notebook_set_show_tabs(notebook, true);
* or generally to keep track of history. return NULL;
* However, this would require either modifying notebook_append /* WebKitGTK documentation recommends returning the new webview.
* or duplicating its contents, for unclear gain. * I imagine that this might allow e.g., to go back in a new tab
*/ * or generally to keep track of history.
* However, this would require either modifying notebook_append
* or duplicating its contents, for unclear gain.
*/
} }
void notebook_append(GtkNotebook *notebook, const char *uri) void notebook_append(GtkNotebook* notebook, const char* uri)
{ {
GdkScreen *screen = gtk_window_get_screen(GTK_WINDOW(window)); GdkScreen* screen = gtk_window_get_screen(GTK_WINDOW(window));
GdkVisual *rgba_visual = gdk_screen_get_rgba_visual(screen); GdkVisual* rgba_visual = gdk_screen_get_rgba_visual(screen);
GdkRGBA rgba; GdkRGBA rgba;
gdk_rgba_parse(&rgba, BG_COLOR); gdk_rgba_parse(&rgba, BG_COLOR);
WebKitWebView *view = webview_new(); WebKitWebView* view = webview_new();
gtk_widget_set_visual(GTK_WIDGET(window), rgba_visual); gtk_widget_set_visual(GTK_WIDGET(window), rgba_visual);
g_signal_connect(view, "load_changed", G_CALLBACK(load_changed), notebook); g_signal_connect(view, "load_changed", G_CALLBACK(load_changed), notebook);
g_signal_connect(view, "create", G_CALLBACK(handle_create_new_tab), notebook); g_signal_connect(view, "create", G_CALLBACK(handle_create_new_tab), notebook);
int n = gtk_notebook_append_page(notebook, GTK_WIDGET(view), NULL); int n = gtk_notebook_append_page(notebook, GTK_WIDGET(view), NULL);
gtk_notebook_set_tab_reorderable(notebook, GTK_WIDGET(view), true); gtk_notebook_set_tab_reorderable(notebook, GTK_WIDGET(view), true);
gtk_widget_show_all(GTK_WIDGET(window)); gtk_widget_show_all(GTK_WIDGET(window));
gtk_widget_hide(GTK_WIDGET(bar)); gtk_widget_hide(GTK_WIDGET(bar));
webkit_web_view_set_background_color(view, &rgba); webkit_web_view_set_background_color(view, &rgba);
load_uri(view, (uri) ? uri : HOME); load_uri(view, (uri) ? uri : HOME);
gtk_notebook_set_current_page(notebook, n); gtk_notebook_set_current_page(notebook, n);
gtk_notebook_set_tab_label_text(notebook, GTK_WIDGET(view), "-" ); gtk_notebook_set_tab_label_text(notebook, GTK_WIDGET(view), "-");
webkit_web_view_set_zoom_level(view, ZOOM); webkit_web_view_set_zoom_level(view, ZOOM);
} }
void show_bar(GtkNotebook *notebook) void show_bar(GtkNotebook* notebook)
{ {
if (entry_mode == _SEARCH) { if (entry_mode == _SEARCH) {
const char *url = webkit_web_view_get_uri(notebook_get_webview(notebook)); const char* url = webkit_web_view_get_uri(notebook_get_webview(notebook));
gtk_entry_set_placeholder_text(search, "Search"); gtk_entry_set_placeholder_text(search, "Search");
gtk_entry_buffer_set_text(search_buf, url, strlen(url)); gtk_entry_buffer_set_text(search_buf, url, strlen(url));
gtk_widget_show(GTK_WIDGET(bar)); gtk_widget_show(GTK_WIDGET(bar));
gtk_window_set_focus(window, GTK_WIDGET(search)); gtk_window_set_focus(window, GTK_WIDGET(search));
} else if (entry_mode == _HIDDEN) { } else if (entry_mode == _HIDDEN) {
gtk_widget_hide(GTK_WIDGET(bar)); gtk_widget_hide(GTK_WIDGET(bar));
} else { } else {
const char *search_text = webkit_find_controller_get_search_text( const char* search_text = webkit_find_controller_get_search_text(
webkit_web_view_get_find_controller(notebook_get_webview(notebook))); webkit_web_view_get_find_controller(notebook_get_webview(notebook)));
if (search_text != NULL) if (search_text != NULL)
gtk_entry_buffer_set_text(search_buf, search_text, strlen(search_text)); gtk_entry_buffer_set_text(search_buf, search_text, strlen(search_text));
gtk_entry_set_placeholder_text(search, "Find"); gtk_entry_set_placeholder_text(search, "Find");
gtk_widget_show(GTK_WIDGET(bar)); gtk_widget_show(GTK_WIDGET(bar));
gtk_window_set_focus(window, GTK_WIDGET(search)); gtk_window_set_focus(window, GTK_WIDGET(search));
} }
} }
int handle_key(func id, GtkNotebook *notebook) int handle_key(func id, GtkNotebook* notebook)
{ {
static double zoom = ZOOM; static double zoom = ZOOM;
static bool is_fullscreen = 0; static bool is_fullscreen = 0;
switch (id) { switch (id) {
case goback: case goback:
webkit_web_view_go_back(notebook_get_webview(notebook)); webkit_web_view_go_back(notebook_get_webview(notebook));
break; break;
case goforward: case goforward:
webkit_web_view_go_forward(notebook_get_webview(notebook)); webkit_web_view_go_forward(notebook_get_webview(notebook));
break; break;
case refresh: case refresh:
webkit_web_view_reload(notebook_get_webview(notebook)); webkit_web_view_reload(notebook_get_webview(notebook));
break; break;
case refresh_force: case refresh_force:
webkit_web_view_reload_bypass_cache(notebook_get_webview(notebook)); webkit_web_view_reload_bypass_cache(notebook_get_webview(notebook));
break; break;
case back_to_home: case back_to_home:
load_uri(notebook_get_webview(notebook), HOME); load_uri(notebook_get_webview(notebook), HOME);
break; break;
case zoomin: case zoomin:
webkit_web_view_set_zoom_level(notebook_get_webview(notebook), (zoom += ZOOM_VAL)); webkit_web_view_set_zoom_level(notebook_get_webview(notebook),
break; (zoom += ZOOM_VAL));
break;
case zoomout: case zoomout:
webkit_web_view_set_zoom_level(notebook_get_webview(notebook), (zoom -= ZOOM_VAL)); webkit_web_view_set_zoom_level(notebook_get_webview(notebook),
break; (zoom -= ZOOM_VAL));
break;
case zoom_reset: case zoom_reset:
webkit_web_view_set_zoom_level(notebook_get_webview(notebook), (zoom = ZOOM)); webkit_web_view_set_zoom_level(notebook_get_webview(notebook),
break; (zoom = ZOOM));
break;
case prev_tab: case prev_tab:
if (gtk_notebook_get_current_page(notebook) == 0) { if (gtk_notebook_get_current_page(notebook) == 0) {
gtk_notebook_set_current_page(notebook, gtk_notebook_set_current_page(notebook,
gtk_notebook_get_n_pages(notebook) - 1); gtk_notebook_get_n_pages(notebook) - 1);
} else { } else {
gtk_notebook_prev_page(notebook); gtk_notebook_prev_page(notebook);
} }
break; break;
case next_tab: case next_tab:
if (gtk_notebook_get_current_page(notebook) == if (gtk_notebook_get_current_page(notebook) == gtk_notebook_get_n_pages(notebook) - 1) {
gtk_notebook_get_n_pages(notebook) - 1) { notebook_append(notebook, NULL);
notebook_append(notebook, NULL); gtk_notebook_set_show_tabs(notebook, true);
gtk_notebook_set_show_tabs(notebook, true); webkit_web_view_set_zoom_level(notebook_get_webview(notebook), zoom);
webkit_web_view_set_zoom_level(notebook_get_webview(notebook), zoom); } else {
} else { gtk_notebook_next_page(notebook);
gtk_notebook_next_page(notebook); }
} break;
break;
case close_tab: case close_tab:
gtk_notebook_remove_page(notebook, gtk_notebook_get_current_page(notebook)); gtk_notebook_remove_page(notebook, gtk_notebook_get_current_page(notebook));
switch (gtk_notebook_get_n_pages(notebook)) { switch (gtk_notebook_get_n_pages(notebook)) {
case 0: case 0:
exit(0); exit(0);
break; break;
case 1: case 1:
gtk_notebook_set_show_tabs(notebook, false); gtk_notebook_set_show_tabs(notebook, false);
break; break;
} }
break; break;
case toggle_fullscreen: case toggle_fullscreen:
if (is_fullscreen) if (is_fullscreen)
gtk_window_unfullscreen(window); gtk_window_unfullscreen(window);
else else
gtk_window_fullscreen(window); gtk_window_fullscreen(window);
is_fullscreen = ! is_fullscreen; is_fullscreen = !is_fullscreen;
break; break;
case show_searchbar: case show_searchbar:
entry_mode = _SEARCH; entry_mode = _SEARCH;
show_bar(notebook); show_bar(notebook);
break; break;
case show_finder: case show_finder:
entry_mode = _FIND; entry_mode = _FIND;
show_bar(notebook); show_bar(notebook);
break; break;
case finder_next: case finder_next:
webkit_find_controller_search_next( webkit_find_controller_search_next(
webkit_web_view_get_find_controller(notebook_get_webview(notebook))); webkit_web_view_get_find_controller(notebook_get_webview(notebook)));
break; break;
case finder_prev: case finder_prev:
webkit_find_controller_search_previous( webkit_find_controller_search_previous(
webkit_web_view_get_find_controller(notebook_get_webview(notebook))); webkit_web_view_get_find_controller(notebook_get_webview(notebook)));
break; break;
case new_tab: case new_tab:
notebook_append(notebook, NULL); notebook_append(notebook, NULL);
gtk_notebook_set_show_tabs(notebook, true); gtk_notebook_set_show_tabs(notebook, true);
entry_mode = _SEARCH; entry_mode = _SEARCH;
show_bar(notebook); show_bar(notebook);
break; break;
case hide_bar: case hide_bar:
entry_mode = _HIDDEN; entry_mode = _HIDDEN;
show_bar(notebook); show_bar(notebook);
break; break;
case prettify: case prettify: {
{ if (READABILITY_ENABLED) {
if(READABILITY_ENABLED){ char* readability_js = malloc(READABILITY_N + 1);
char* readability_js = malloc(READABILITY_N+1); read_readability_js(readability_js);
read_readability_js(readability_js); webkit_web_view_run_javascript(notebook_get_webview(notebook),
webkit_web_view_run_javascript(notebook_get_webview(notebook), readability_js, NULL, NULL, NULL);
readability_js, free(readability_js);
NULL, NULL, NULL); }
free(readability_js); break;
}
}
} return 1;
break;
}
}
return 1;
} }
int keypress(void *self, GdkEvent *e, GtkNotebook *notebook) int keypress(void* self, GdkEvent* e, GtkNotebook* notebook)
{ {
(void) self; (void)self;
for (int i = 0; i < sizeof(keys) / sizeof(keys[0]); i++) for (int i = 0; i < sizeof(keys) / sizeof(keys[0]); i++)
if (e->key.keyval == keys[i].key && e->key.state == keys[i].mod) if (e->key.keyval == keys[i].key && e->key.state == keys[i].mod)
return handle_key(keys[i].id, notebook); return handle_key(keys[i].id, notebook);
return 0; return 0;
} }
void search_activate(GtkEntry *self, GtkNotebook *notebook) void search_activate(GtkEntry* self, GtkNotebook* notebook)
{ {
if (entry_mode == _SEARCH) if (entry_mode == _SEARCH)
load_uri(notebook_get_webview(notebook), gtk_entry_buffer_get_text(search_buf)); load_uri(notebook_get_webview(notebook),
else if (entry_mode == _FIND) gtk_entry_buffer_get_text(search_buf));
webkit_find_controller_search( else if (entry_mode == _FIND)
webkit_web_view_get_find_controller(notebook_get_webview(notebook)), webkit_find_controller_search(
gtk_entry_buffer_get_text(search_buf), webkit_web_view_get_find_controller(notebook_get_webview(notebook)),
WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE | WEBKIT_FIND_OPTIONS_WRAP_AROUND, gtk_entry_buffer_get_text(search_buf),
G_MAXUINT); WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE | WEBKIT_FIND_OPTIONS_WRAP_AROUND,
G_MAXUINT);
gtk_widget_hide(GTK_WIDGET(bar)); gtk_widget_hide(GTK_WIDGET(bar));
} }
void window_init(GtkNotebook *notebook) void window_init(GtkNotebook* notebook)
{ {
GtkCssProvider *css = gtk_css_provider_new(); GtkCssProvider* css = gtk_css_provider_new();
gtk_css_provider_load_from_path(css, "/usr/share/themes/rose/style.css", NULL); gtk_css_provider_load_from_path(css, "/usr/share/themes/rose/style.css",
gtk_style_context_add_provider_for_screen(gdk_screen_get_default(), GTK_STYLE_PROVIDER(css), NULL);
800); gtk_style_context_add_provider_for_screen(gdk_screen_get_default(),
gtk_entry_buffer_new("", 0); GTK_STYLE_PROVIDER(css), 800);
gtk_entry_set_alignment(search, 0.48); gtk_entry_buffer_new("", 0);
gtk_widget_set_size_request(GTK_WIDGET(search), 1200, -1); gtk_entry_set_alignment(search, 0.48);
gtk_header_bar_set_custom_title(bar, GTK_WIDGET(search)); gtk_widget_set_size_request(GTK_WIDGET(search), 1200, -1);
gtk_window_set_titlebar(window, GTK_WIDGET(bar)); gtk_header_bar_set_custom_title(bar, GTK_WIDGET(search));
g_signal_connect(search, "activate", G_CALLBACK(search_activate), notebook); gtk_window_set_titlebar(window, GTK_WIDGET(bar));
g_signal_connect(window, "key-press-event", G_CALLBACK(keypress), notebook); g_signal_connect(search, "activate", G_CALLBACK(search_activate), notebook);
g_signal_connect(window, "destroy", G_CALLBACK(exit), notebook); g_signal_connect(window, "key-press-event", G_CALLBACK(keypress), notebook);
g_signal_connect(window, "destroy", G_CALLBACK(exit), notebook);
} }
void notebook_init(GtkNotebook *notebook, const char *uri) void notebook_init(GtkNotebook* notebook, const char* uri)
{ {
gtk_notebook_set_show_border(notebook, false); gtk_notebook_set_show_border(notebook, false);
gtk_notebook_set_show_tabs(notebook, false); gtk_notebook_set_show_tabs(notebook, false);
notebook_append(notebook, uri); notebook_append(notebook, uri);
} }
void setup(GtkNotebook *notebook, int argc, char **argv) void setup(GtkNotebook* notebook, int argc, char** argv)
{ {
// Define GTK entities // Define GTK entities
window = GTK_WINDOW(gtk_window_new(0)); window = GTK_WINDOW(gtk_window_new(0));
notebook = GTK_NOTEBOOK(gtk_notebook_new()); notebook = GTK_NOTEBOOK(gtk_notebook_new());
bar = GTK_HEADER_BAR(gtk_header_bar_new()); bar = GTK_HEADER_BAR(gtk_header_bar_new());
search_buf = GTK_ENTRY_BUFFER(gtk_entry_buffer_new("", 0)); search_buf = GTK_ENTRY_BUFFER(gtk_entry_buffer_new("", 0));
search = GTK_ENTRY(gtk_entry_new_with_buffer(search_buf)); search = GTK_ENTRY(gtk_entry_new_with_buffer(search_buf));
gtk_window_set_default_size(window, WIDTH, HEIGHT); gtk_window_set_default_size(window, WIDTH, HEIGHT);
window_init(notebook); window_init(notebook);
// Initialize with first uri // Initialize with first uri
char *first_uri = argc > 1 ? argv[1] : NULL; char* first_uri = argc > 1 ? argv[1] : NULL;
notebook_init(notebook, first_uri); notebook_init(notebook, first_uri);
g_object_set(gtk_settings_get_default(), GTK, NULL); g_object_set(gtk_settings_get_default(), GTK, NULL);
// More GTK stuff // More GTK stuff
gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(notebook)); gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(notebook));
gtk_widget_show_all(GTK_WIDGET(window)); gtk_widget_show_all(GTK_WIDGET(window));
gtk_widget_hide(GTK_WIDGET(bar)); gtk_widget_hide(GTK_WIDGET(bar));
// Deal with more uris, if this is necessary. // Deal with more uris, if this is necessary.
if(argc > 2){ if (argc > 2) {
gtk_notebook_set_show_tabs(notebook, true); gtk_notebook_set_show_tabs(notebook, true);
for(int i = 2; i<argc; i++){ for (int i = 2; i < argc; i++) {
notebook_append(notebook, argv[i]); notebook_append(notebook, argv[i]);
} }
} }
} }
int main(int argc, char **argv) int main(int argc, char** argv)
{ {
GtkNotebook *notebook; GtkNotebook* notebook;
gtk_init(NULL, NULL); gtk_init(NULL, NULL);
setup(notebook, argc, argv); setup(notebook, argc, argv);
gtk_main(); gtk_main();
// this point is never reached, since gtk_main(); never exits. // this point is never reached, since gtk_main(); never exits.
} }