surf.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153
  1. /* See LICENSE file for copyright and license details.
  2. *
  3. * To understand surf, start reading main().
  4. */
  5. #include <sys/file.h>
  6. #include <sys/types.h>
  7. #include <sys/wait.h>
  8. #include <glib.h>
  9. #include <libgen.h>
  10. #include <limits.h>
  11. #include <pwd.h>
  12. #include <regex.h>
  13. #include <signal.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <unistd.h>
  18. #include <gdk/gdk.h>
  19. #include <gdk/gdkkeysyms.h>
  20. #include <gdk/gdkx.h>
  21. #include <glib/gstdio.h>
  22. #include <gtk/gtk.h>
  23. #include <gtk/gtkx.h>
  24. #include <gcr/gcr.h>
  25. #include <JavaScriptCore/JavaScript.h>
  26. #include <webkit2/webkit2.h>
  27. #include <X11/X.h>
  28. #include <X11/Xatom.h>
  29. #include <glib.h>
  30. #include "arg.h"
  31. #include "common.h"
  32. #define LENGTH(x) (sizeof(x) / sizeof(x[0]))
  33. #define CLEANMASK(mask) (mask & (MODKEY|GDK_SHIFT_MASK))
  34. enum { AtomFind, AtomSearch, AtomGo, AtomUri, AtomLast };
  35. enum {
  36. OnDoc = WEBKIT_HIT_TEST_RESULT_CONTEXT_DOCUMENT,
  37. OnLink = WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK,
  38. OnImg = WEBKIT_HIT_TEST_RESULT_CONTEXT_IMAGE,
  39. OnMedia = WEBKIT_HIT_TEST_RESULT_CONTEXT_MEDIA,
  40. OnEdit = WEBKIT_HIT_TEST_RESULT_CONTEXT_EDITABLE,
  41. OnBar = WEBKIT_HIT_TEST_RESULT_CONTEXT_SCROLLBAR,
  42. OnSel = WEBKIT_HIT_TEST_RESULT_CONTEXT_SELECTION,
  43. OnAny = OnDoc | OnLink | OnImg | OnMedia | OnEdit | OnBar | OnSel,
  44. };
  45. typedef enum {
  46. AcceleratedCanvas,
  47. AccessMicrophone,
  48. AccessWebcam,
  49. CaretBrowsing,
  50. Certificate,
  51. CookiePolicies,
  52. DiskCache,
  53. DefaultCharset,
  54. DNSPrefetch,
  55. FileURLsCrossAccess,
  56. FontSize,
  57. FrameFlattening,
  58. Geolocation,
  59. HideBackground,
  60. Inspector,
  61. Java,
  62. JavaScript,
  63. KioskMode,
  64. LoadImages,
  65. MediaManualPlay,
  66. Plugins,
  67. PreferredLanguages,
  68. RunInFullscreen,
  69. ScrollBars,
  70. ShowIndicators,
  71. SiteQuirks,
  72. SmoothScrolling,
  73. SpellChecking,
  74. SpellLanguages,
  75. StrictTLS,
  76. Style,
  77. WebGL,
  78. ZoomLevel,
  79. ClipboardNotPrimary,
  80. ParameterLast
  81. } ParamName;
  82. typedef union {
  83. int i;
  84. float f;
  85. const void *v;
  86. } Arg;
  87. typedef struct {
  88. Arg val;
  89. int prio;
  90. } Parameter;
  91. typedef struct Client {
  92. GtkWidget *win;
  93. WebKitWebView *view;
  94. WebKitWebInspector *inspector;
  95. WebKitFindController *finder;
  96. WebKitHitTestResult *mousepos;
  97. GTlsCertificate *cert, *failedcert;
  98. GTlsCertificateFlags tlserr;
  99. Window xid;
  100. unsigned long pageid;
  101. int progress, fullscreen, https, insecure, errorpage;
  102. const char *title, *overtitle, *targeturi;
  103. const char *needle;
  104. struct Client *next;
  105. } Client;
  106. typedef struct {
  107. guint mod;
  108. guint keyval;
  109. void (*func)(Client *c, const Arg *a);
  110. const Arg arg;
  111. } Key;
  112. typedef struct {
  113. unsigned int target;
  114. unsigned int mask;
  115. guint button;
  116. void (*func)(Client *c, const Arg *a, WebKitHitTestResult *h);
  117. const Arg arg;
  118. unsigned int stopevent;
  119. } Button;
  120. typedef struct {
  121. const char *uri;
  122. Parameter config[ParameterLast];
  123. regex_t re;
  124. } UriParameters;
  125. typedef struct {
  126. char *regex;
  127. char *file;
  128. regex_t re;
  129. } SiteSpecific;
  130. /* Surf */
  131. static void usage(void);
  132. static void setup(void);
  133. static void sigchld(int unused);
  134. static void sighup(int unused);
  135. static char *buildfile(const char *path);
  136. static char *buildpath(const char *path);
  137. static char *untildepath(const char *path);
  138. static const char *getuserhomedir(const char *user);
  139. static const char *getcurrentuserhomedir(void);
  140. static Client *newclient(Client *c);
  141. static void loaduri(Client *c, const Arg *a);
  142. static const char *geturi(Client *c);
  143. static void setatom(Client *c, int a, const char *v);
  144. static const char *getatom(Client *c, int a);
  145. static void updatetitle(Client *c);
  146. static void gettogglestats(Client *c);
  147. static void getpagestats(Client *c);
  148. static WebKitCookieAcceptPolicy cookiepolicy_get(void);
  149. static char cookiepolicy_set(const WebKitCookieAcceptPolicy p);
  150. static void seturiparameters(Client *c, const char *uri, ParamName *params);
  151. static void setparameter(Client *c, int refresh, ParamName p, const Arg *a);
  152. static const char *getcert(const char *uri);
  153. static void setcert(Client *c, const char *file);
  154. static const char *getstyle(const char *uri);
  155. static void setstyle(Client *c, const char *file);
  156. static void runscript(Client *c);
  157. static void evalscript(Client *c, const char *jsstr, ...);
  158. static void updatewinid(Client *c);
  159. static void handleplumb(Client *c, const char *uri);
  160. static void newwindow(Client *c, const Arg *a, int noembed);
  161. static void spawn(Client *c, const Arg *a);
  162. static void msgext(Client *c, char type, const Arg *a);
  163. static void destroyclient(Client *c);
  164. static void cleanup(void);
  165. /* GTK/WebKit */
  166. static WebKitWebView *newview(Client *c, WebKitWebView *rv);
  167. static void initwebextensions(WebKitWebContext *wc, Client *c);
  168. static GtkWidget *createview(WebKitWebView *v, WebKitNavigationAction *a,
  169. Client *c);
  170. static gboolean buttonreleased(GtkWidget *w, GdkEvent *e, Client *c);
  171. static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event,
  172. gpointer d);
  173. static gboolean winevent(GtkWidget *w, GdkEvent *e, Client *c);
  174. static gboolean readpipe(GIOChannel *s, GIOCondition ioc, gpointer unused);
  175. static void showview(WebKitWebView *v, Client *c);
  176. static GtkWidget *createwindow(Client *c);
  177. static gboolean loadfailedtls(WebKitWebView *v, gchar *uri,
  178. GTlsCertificate *cert,
  179. GTlsCertificateFlags err, Client *c);
  180. static void loadchanged(WebKitWebView *v, WebKitLoadEvent e, Client *c);
  181. static void progresschanged(WebKitWebView *v, GParamSpec *ps, Client *c);
  182. static void titlechanged(WebKitWebView *view, GParamSpec *ps, Client *c);
  183. static void mousetargetchanged(WebKitWebView *v, WebKitHitTestResult *h,
  184. guint modifiers, Client *c);
  185. static gboolean permissionrequested(WebKitWebView *v,
  186. WebKitPermissionRequest *r, Client *c);
  187. static gboolean decidepolicy(WebKitWebView *v, WebKitPolicyDecision *d,
  188. WebKitPolicyDecisionType dt, Client *c);
  189. static void decidenavigation(WebKitPolicyDecision *d, Client *c);
  190. static void decidenewwindow(WebKitPolicyDecision *d, Client *c);
  191. static void decideresource(WebKitPolicyDecision *d, Client *c);
  192. static void insecurecontent(WebKitWebView *v, WebKitInsecureContentEvent e,
  193. Client *c);
  194. static void downloadstarted(WebKitWebContext *wc, WebKitDownload *d,
  195. Client *c);
  196. static void responsereceived(WebKitDownload *d, GParamSpec *ps, Client *c);
  197. static void download(Client *c, WebKitURIResponse *r);
  198. static void webprocessterminated(WebKitWebView *v,
  199. WebKitWebProcessTerminationReason r,
  200. Client *c);
  201. static void closeview(WebKitWebView *v, Client *c);
  202. static void destroywin(GtkWidget* w, Client *c);
  203. /* Hotkeys */
  204. static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
  205. static void reload(Client *c, const Arg *a);
  206. static void print(Client *c, const Arg *a);
  207. static void showcert(Client *c, const Arg *a);
  208. static void clipboard(Client *c, const Arg *a);
  209. static void zoom(Client *c, const Arg *a);
  210. static void scrollv(Client *c, const Arg *a);
  211. static void scrollh(Client *c, const Arg *a);
  212. static void navigate(Client *c, const Arg *a);
  213. static void stop(Client *c, const Arg *a);
  214. static void toggle(Client *c, const Arg *a);
  215. static void togglefullscreen(Client *c, const Arg *a);
  216. static void togglecookiepolicy(Client *c, const Arg *a);
  217. static void toggleinspector(Client *c, const Arg *a);
  218. static void find(Client *c, const Arg *a);
  219. static void search(Client *c, const Arg *a);
  220. /* Buttons */
  221. static void clicknavigate(Client *c, const Arg *a, WebKitHitTestResult *h);
  222. static void clicknewwindow(Client *c, const Arg *a, WebKitHitTestResult *h);
  223. static void clickexternplayer(Client *c, const Arg *a, WebKitHitTestResult *h);
  224. static char winid[64];
  225. static char togglestats[12];
  226. static char pagestats[2];
  227. static Atom atoms[AtomLast];
  228. static Window embed;
  229. static int showxid;
  230. static int cookiepolicy;
  231. static Display *dpy;
  232. static Client *clients;
  233. static GdkDevice *gdkkb;
  234. static char *stylefile;
  235. static const char *useragent;
  236. static Parameter *curconfig;
  237. static int modparams[ParameterLast];
  238. static int pipein[2], pipeout[2];
  239. char *argv0;
  240. static ParamName loadtransient[] = {
  241. Certificate,
  242. CookiePolicies,
  243. DiskCache,
  244. DNSPrefetch,
  245. FileURLsCrossAccess,
  246. JavaScript,
  247. LoadImages,
  248. PreferredLanguages,
  249. ShowIndicators,
  250. StrictTLS,
  251. ParameterLast
  252. };
  253. static ParamName loadcommitted[] = {
  254. AcceleratedCanvas,
  255. // AccessMicrophone,
  256. // AccessWebcam,
  257. CaretBrowsing,
  258. DefaultCharset,
  259. FontSize,
  260. FrameFlattening,
  261. Geolocation,
  262. HideBackground,
  263. Inspector,
  264. Java,
  265. // KioskMode,
  266. MediaManualPlay,
  267. Plugins,
  268. RunInFullscreen,
  269. ScrollBars,
  270. SiteQuirks,
  271. SmoothScrolling,
  272. SpellChecking,
  273. SpellLanguages,
  274. Style,
  275. ZoomLevel,
  276. ClipboardNotPrimary,
  277. ParameterLast
  278. };
  279. static ParamName loadfinished[] = {
  280. ParameterLast
  281. };
  282. /* configuration, allows nested code to access above variables */
  283. #include "config.h"
  284. void
  285. usage(void)
  286. {
  287. die("usage: surf [-bBdDfFgGiIkKmMnNpPsStTvwxX]\n"
  288. "[-a cookiepolicies ] [-c cookiefile] [-C stylefile] [-e xid]\n"
  289. "[-r scriptfile] [-u useragent] [-z zoomlevel] [uri]\n");
  290. }
  291. void
  292. setup(void)
  293. {
  294. GIOChannel *gchanin;
  295. GdkDisplay *gdpy;
  296. int i, j;
  297. /* clean up any zombies immediately */
  298. sigchld(0);
  299. if (signal(SIGHUP, sighup) == SIG_ERR)
  300. die("Can't install SIGHUP handler");
  301. if (!(dpy = XOpenDisplay(NULL)))
  302. die("Can't open default display");
  303. /* atoms */
  304. atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
  305. atoms[AtomSearch] = XInternAtom(dpy, "_SURF_SEARCH", False);
  306. atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
  307. atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
  308. gtk_init(NULL, NULL);
  309. gdpy = gdk_display_get_default();
  310. curconfig = defconfig;
  311. /* dirs and files */
  312. cookiefile = buildfile(cookiefile);
  313. scriptfile = buildfile(scriptfile);
  314. cachedir = buildpath(cachedir);
  315. certdir = buildpath(certdir);
  316. gdkkb = gdk_seat_get_keyboard(gdk_display_get_default_seat(gdpy));
  317. if (pipe(pipeout) < 0 || pipe(pipein) < 0) {
  318. fputs("Unable to create pipes\n", stderr);
  319. } else {
  320. gchanin = g_io_channel_unix_new(pipein[0]);
  321. g_io_channel_set_encoding(gchanin, NULL, NULL);
  322. g_io_channel_set_close_on_unref(gchanin, TRUE);
  323. g_io_add_watch(gchanin, G_IO_IN, readpipe, NULL);
  324. }
  325. for (i = 0; i < LENGTH(certs); ++i) {
  326. if (!regcomp(&(certs[i].re), certs[i].regex, REG_EXTENDED)) {
  327. certs[i].file = g_strconcat(certdir, "/", certs[i].file,
  328. NULL);
  329. } else {
  330. fprintf(stderr, "Could not compile regex: %s\n",
  331. certs[i].regex);
  332. certs[i].regex = NULL;
  333. }
  334. }
  335. if (!stylefile) {
  336. styledir = buildpath(styledir);
  337. for (i = 0; i < LENGTH(styles); ++i) {
  338. if (!regcomp(&(styles[i].re), styles[i].regex,
  339. REG_EXTENDED)) {
  340. styles[i].file = g_strconcat(styledir, "/",
  341. styles[i].file, NULL);
  342. } else {
  343. fprintf(stderr, "Could not compile regex: %s\n",
  344. styles[i].regex);
  345. styles[i].regex = NULL;
  346. }
  347. }
  348. g_free(styledir);
  349. } else {
  350. stylefile = buildfile(stylefile);
  351. }
  352. for (i = 0; i < LENGTH(uriparams); ++i) {
  353. if (regcomp(&(uriparams[i].re), uriparams[i].uri,
  354. REG_EXTENDED)) {
  355. fprintf(stderr, "Could not compile regex: %s\n",
  356. uriparams[i].uri);
  357. uriparams[i].uri = NULL;
  358. continue;
  359. }
  360. /* copy default parameters with higher priority */
  361. for (j = 0; j < ParameterLast; ++j) {
  362. if (defconfig[j].prio >= uriparams[i].config[j].prio)
  363. uriparams[i].config[j] = defconfig[j];
  364. }
  365. }
  366. }
  367. void
  368. sigchld(int unused)
  369. {
  370. if (signal(SIGCHLD, sigchld) == SIG_ERR)
  371. die("Can't install SIGCHLD handler");
  372. while (waitpid(-1, NULL, WNOHANG) > 0)
  373. ;
  374. }
  375. void
  376. sighup(int unused)
  377. {
  378. Arg a = { .i = 0 };
  379. Client *c;
  380. for (c = clients; c; c = c->next)
  381. reload(c, &a);
  382. }
  383. char *
  384. buildfile(const char *path)
  385. {
  386. char *dname, *bname, *bpath, *fpath;
  387. FILE *f;
  388. dname = g_path_get_dirname(path);
  389. bname = g_path_get_basename(path);
  390. bpath = buildpath(dname);
  391. g_free(dname);
  392. fpath = g_build_filename(bpath, bname, NULL);
  393. g_free(bpath);
  394. g_free(bname);
  395. if (!(f = fopen(fpath, "a")))
  396. die("Could not open file: %s\n", fpath);
  397. g_chmod(fpath, 0600); /* always */
  398. fclose(f);
  399. return fpath;
  400. }
  401. static const char*
  402. getuserhomedir(const char *user)
  403. {
  404. struct passwd *pw = getpwnam(user);
  405. if (!pw)
  406. die("Can't get user %s login information.\n", user);
  407. return pw->pw_dir;
  408. }
  409. static const char*
  410. getcurrentuserhomedir(void)
  411. {
  412. const char *homedir;
  413. const char *user;
  414. struct passwd *pw;
  415. homedir = getenv("HOME");
  416. if (homedir)
  417. return homedir;
  418. user = getenv("USER");
  419. if (user)
  420. return getuserhomedir(user);
  421. pw = getpwuid(getuid());
  422. if (!pw)
  423. die("Can't get current user home directory\n");
  424. return pw->pw_dir;
  425. }
  426. char *
  427. buildpath(const char *path)
  428. {
  429. char *apath, *fpath;
  430. if (path[0] == '~')
  431. apath = untildepath(path);
  432. else
  433. apath = g_strdup(path);
  434. /* creating directory */
  435. if (g_mkdir_with_parents(apath, 0700) < 0)
  436. die("Could not access directory: %s\n", apath);
  437. fpath = realpath(apath, NULL);
  438. g_free(apath);
  439. return fpath;
  440. }
  441. char *
  442. untildepath(const char *path)
  443. {
  444. char *apath, *name, *p;
  445. const char *homedir;
  446. if (path[1] == '/' || path[1] == '\0') {
  447. p = (char *)&path[1];
  448. homedir = getcurrentuserhomedir();
  449. } else {
  450. if ((p = strchr(path, '/')))
  451. name = g_strndup(&path[1], p - (path + 1));
  452. else
  453. name = g_strdup(&path[1]);
  454. homedir = getuserhomedir(name);
  455. g_free(name);
  456. }
  457. apath = g_build_filename(homedir, p, NULL);
  458. return apath;
  459. }
  460. Client *
  461. newclient(Client *rc)
  462. {
  463. Client *c;
  464. if (!(c = calloc(1, sizeof(Client))))
  465. die("Cannot malloc!\n");
  466. c->next = clients;
  467. clients = c;
  468. c->progress = 100;
  469. c->view = newview(c, rc ? rc->view : NULL);
  470. return c;
  471. }
  472. void
  473. loaduri(Client *c, const Arg *a)
  474. {
  475. struct stat st;
  476. char *url, *path, *apath;
  477. const char *uri = a->v;
  478. if (g_strcmp0(uri, "") == 0)
  479. return;
  480. if (g_str_has_prefix(uri, "http://") ||
  481. g_str_has_prefix(uri, "https://") ||
  482. g_str_has_prefix(uri, "file://") ||
  483. g_str_has_prefix(uri, "about:")) {
  484. url = g_strdup(uri);
  485. } else {
  486. if (uri[0] == '~')
  487. apath = untildepath(uri);
  488. else
  489. apath = (char *)uri;
  490. if (!stat(apath, &st) && (path = realpath(apath, NULL))) {
  491. url = g_strdup_printf("file://%s", path);
  492. free(path);
  493. } else {
  494. url = g_strdup_printf("http://%s", uri);
  495. }
  496. if (apath != uri)
  497. free(apath);
  498. }
  499. setatom(c, AtomUri, url);
  500. if (strcmp(url, geturi(c)) == 0) {
  501. reload(c, a);
  502. } else {
  503. webkit_web_view_load_uri(c->view, url);
  504. updatetitle(c);
  505. }
  506. g_free(url);
  507. }
  508. void
  509. search(Client *c, const Arg *a)
  510. {
  511. Arg arg;
  512. char *url;
  513. url = g_strdup_printf(searchurl, a->v);
  514. arg.v = url;
  515. loaduri(c, &arg);
  516. g_free(url);
  517. }
  518. const char *
  519. geturi(Client *c)
  520. {
  521. const char *uri;
  522. if (!(uri = webkit_web_view_get_uri(c->view)))
  523. uri = "about:blank";
  524. return uri;
  525. }
  526. void
  527. setatom(Client *c, int a, const char *v)
  528. {
  529. XChangeProperty(dpy, c->xid,
  530. atoms[a], XA_STRING, 8, PropModeReplace,
  531. (unsigned char *)v, strlen(v) + 1);
  532. XSync(dpy, False);
  533. }
  534. const char *
  535. getatom(Client *c, int a)
  536. {
  537. static char buf[BUFSIZ];
  538. Atom adummy;
  539. int idummy;
  540. unsigned long ldummy;
  541. unsigned char *p = NULL;
  542. XSync(dpy, False);
  543. XGetWindowProperty(dpy, c->xid, atoms[a], 0L, BUFSIZ, False, XA_STRING,
  544. &adummy, &idummy, &ldummy, &ldummy, &p);
  545. if (p)
  546. strncpy(buf, (char *)p, LENGTH(buf) - 1);
  547. else
  548. buf[0] = '\0';
  549. XFree(p);
  550. return buf;
  551. }
  552. void
  553. updatetitle(Client *c)
  554. {
  555. char *title;
  556. const char *name = c->overtitle ? c->overtitle :
  557. c->title ? c->title : "";
  558. if (curconfig[ShowIndicators].val.i) {
  559. gettogglestats(c);
  560. getpagestats(c);
  561. if (c->progress != 100)
  562. title = g_strdup_printf("[%i%%] %s:%s | %s",
  563. c->progress, togglestats, pagestats, name);
  564. else
  565. title = g_strdup_printf("%s:%s | %s",
  566. togglestats, pagestats, name);
  567. gtk_window_set_title(GTK_WINDOW(c->win), title);
  568. g_free(title);
  569. } else {
  570. gtk_window_set_title(GTK_WINDOW(c->win), name);
  571. }
  572. }
  573. void
  574. gettogglestats(Client *c)
  575. {
  576. togglestats[0] = cookiepolicy_set(cookiepolicy_get());
  577. togglestats[1] = curconfig[CaretBrowsing].val.i ? 'C' : 'c';
  578. togglestats[2] = curconfig[Geolocation].val.i ? 'G' : 'g';
  579. togglestats[3] = curconfig[DiskCache].val.i ? 'D' : 'd';
  580. togglestats[4] = curconfig[LoadImages].val.i ? 'I' : 'i';
  581. togglestats[5] = curconfig[JavaScript].val.i ? 'S' : 's';
  582. togglestats[6] = curconfig[Plugins].val.i ? 'V' : 'v';
  583. togglestats[7] = curconfig[Style].val.i ? 'M' : 'm';
  584. togglestats[8] = curconfig[FrameFlattening].val.i ? 'F' : 'f';
  585. togglestats[9] = curconfig[Certificate].val.i ? 'X' : 'x';
  586. togglestats[10] = curconfig[StrictTLS].val.i ? 'T' : 't';
  587. togglestats[11] = '\0';
  588. }
  589. void
  590. getpagestats(Client *c)
  591. {
  592. if (c->https)
  593. pagestats[0] = (c->tlserr || c->insecure) ? 'U' : 'T';
  594. else
  595. pagestats[0] = '-';
  596. pagestats[1] = '\0';
  597. }
  598. WebKitCookieAcceptPolicy
  599. cookiepolicy_get(void)
  600. {
  601. switch (((char *)curconfig[CookiePolicies].val.v)[cookiepolicy]) {
  602. case 'a':
  603. return WEBKIT_COOKIE_POLICY_ACCEPT_NEVER;
  604. case '@':
  605. return WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY;
  606. default: /* fallthrough */
  607. case 'A':
  608. return WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS;
  609. }
  610. }
  611. char
  612. cookiepolicy_set(const WebKitCookieAcceptPolicy p)
  613. {
  614. switch (p) {
  615. case WEBKIT_COOKIE_POLICY_ACCEPT_NEVER:
  616. return 'a';
  617. case WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY:
  618. return '@';
  619. default: /* fallthrough */
  620. case WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS:
  621. return 'A';
  622. }
  623. }
  624. void
  625. seturiparameters(Client *c, const char *uri, ParamName *params)
  626. {
  627. Parameter *config, *uriconfig = NULL;
  628. int i, p;
  629. for (i = 0; i < LENGTH(uriparams); ++i) {
  630. if (uriparams[i].uri &&
  631. !regexec(&(uriparams[i].re), uri, 0, NULL, 0)) {
  632. uriconfig = uriparams[i].config;
  633. break;
  634. }
  635. }
  636. curconfig = uriconfig ? uriconfig : defconfig;
  637. for (i = 0; (p = params[i]) != ParameterLast; ++i) {
  638. switch(p) {
  639. default: /* FALLTHROUGH */
  640. if (!(defconfig[p].prio < curconfig[p].prio ||
  641. defconfig[p].prio < modparams[p]))
  642. continue;
  643. case Certificate:
  644. case CookiePolicies:
  645. case Style:
  646. setparameter(c, 0, p, &curconfig[p].val);
  647. }
  648. }
  649. }
  650. void
  651. setparameter(Client *c, int refresh, ParamName p, const Arg *a)
  652. {
  653. GdkRGBA bgcolor = { 0 };
  654. WebKitSettings *s = webkit_web_view_get_settings(c->view);
  655. modparams[p] = curconfig[p].prio;
  656. switch (p) {
  657. case AcceleratedCanvas:
  658. webkit_settings_set_enable_accelerated_2d_canvas(s, a->i);
  659. break;
  660. case AccessMicrophone:
  661. return; /* do nothing */
  662. case AccessWebcam:
  663. return; /* do nothing */
  664. case CaretBrowsing:
  665. webkit_settings_set_enable_caret_browsing(s, a->i);
  666. refresh = 0;
  667. break;
  668. case Certificate:
  669. if (a->i)
  670. setcert(c, geturi(c));
  671. return; /* do not update */
  672. case CookiePolicies:
  673. webkit_cookie_manager_set_accept_policy(
  674. webkit_web_context_get_cookie_manager(
  675. webkit_web_view_get_context(c->view)),
  676. cookiepolicy_get());
  677. refresh = 0;
  678. break;
  679. case DiskCache:
  680. webkit_web_context_set_cache_model(
  681. webkit_web_view_get_context(c->view), a->i ?
  682. WEBKIT_CACHE_MODEL_WEB_BROWSER :
  683. WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
  684. return; /* do not update */
  685. case DefaultCharset:
  686. webkit_settings_set_default_charset(s, a->v);
  687. return; /* do not update */
  688. case DNSPrefetch:
  689. webkit_settings_set_enable_dns_prefetching(s, a->i);
  690. return; /* do not update */
  691. case FileURLsCrossAccess:
  692. webkit_settings_set_allow_file_access_from_file_urls(s, a->i);
  693. webkit_settings_set_allow_universal_access_from_file_urls(s, a->i);
  694. return; /* do not update */
  695. case FontSize:
  696. webkit_settings_set_default_font_size(s, a->i);
  697. return; /* do not update */
  698. case FrameFlattening:
  699. webkit_settings_set_enable_frame_flattening(s, a->i);
  700. break;
  701. case Geolocation:
  702. refresh = 0;
  703. break;
  704. case HideBackground:
  705. if (a->i)
  706. webkit_web_view_set_background_color(c->view, &bgcolor);
  707. return; /* do not update */
  708. case Inspector:
  709. webkit_settings_set_enable_developer_extras(s, a->i);
  710. return; /* do not update */
  711. case Java:
  712. webkit_settings_set_enable_java(s, a->i);
  713. return; /* do not update */
  714. case JavaScript:
  715. webkit_settings_set_enable_javascript(s, a->i);
  716. break;
  717. case KioskMode:
  718. return; /* do nothing */
  719. case LoadImages:
  720. webkit_settings_set_auto_load_images(s, a->i);
  721. break;
  722. case MediaManualPlay:
  723. webkit_settings_set_media_playback_requires_user_gesture(s, a->i);
  724. break;
  725. case Plugins:
  726. webkit_settings_set_enable_plugins(s, a->i);
  727. break;
  728. case PreferredLanguages:
  729. return; /* do nothing */
  730. case RunInFullscreen:
  731. return; /* do nothing */
  732. case ScrollBars:
  733. /* Disabled until we write some WebKitWebExtension for
  734. * manipulating the DOM directly.
  735. enablescrollbars = !enablescrollbars;
  736. evalscript(c, "document.documentElement.style.overflow = '%s'",
  737. enablescrollbars ? "auto" : "hidden");
  738. */
  739. return; /* do not update */
  740. case ShowIndicators:
  741. break;
  742. case SmoothScrolling:
  743. webkit_settings_set_enable_smooth_scrolling(s, a->i);
  744. return; /* do not update */
  745. case SiteQuirks:
  746. webkit_settings_set_enable_site_specific_quirks(s, a->i);
  747. break;
  748. case SpellChecking:
  749. webkit_web_context_set_spell_checking_enabled(
  750. webkit_web_view_get_context(c->view), a->i);
  751. return; /* do not update */
  752. case SpellLanguages:
  753. return; /* do nothing */
  754. case StrictTLS:
  755. webkit_web_context_set_tls_errors_policy(
  756. webkit_web_view_get_context(c->view), a->i ?
  757. WEBKIT_TLS_ERRORS_POLICY_FAIL :
  758. WEBKIT_TLS_ERRORS_POLICY_IGNORE);
  759. break;
  760. case Style:
  761. webkit_user_content_manager_remove_all_style_sheets(
  762. webkit_web_view_get_user_content_manager(c->view));
  763. if (a->i)
  764. setstyle(c, getstyle(geturi(c)));
  765. refresh = 0;
  766. break;
  767. case WebGL:
  768. webkit_settings_set_enable_webgl(s, a->i);
  769. break;
  770. case ZoomLevel:
  771. webkit_web_view_set_zoom_level(c->view, a->f);
  772. return; /* do not update */
  773. default:
  774. return; /* do nothing */
  775. }
  776. updatetitle(c);
  777. if (refresh)
  778. reload(c, a);
  779. }
  780. const char *
  781. getcert(const char *uri)
  782. {
  783. int i;
  784. for (i = 0; i < LENGTH(certs); ++i) {
  785. if (certs[i].regex &&
  786. !regexec(&(certs[i].re), uri, 0, NULL, 0))
  787. return certs[i].file;
  788. }
  789. return NULL;
  790. }
  791. void
  792. setcert(Client *c, const char *uri)
  793. {
  794. const char *file = getcert(uri);
  795. char *host;
  796. GTlsCertificate *cert;
  797. if (!file)
  798. return;
  799. if (!(cert = g_tls_certificate_new_from_file(file, NULL))) {
  800. fprintf(stderr, "Could not read certificate file: %s\n", file);
  801. return;
  802. }
  803. if ((uri = strstr(uri, "https://"))) {
  804. uri += sizeof("https://") - 1;
  805. host = g_strndup(uri, strchr(uri, '/') - uri);
  806. webkit_web_context_allow_tls_certificate_for_host(
  807. webkit_web_view_get_context(c->view), cert, host);
  808. g_free(host);
  809. }
  810. g_object_unref(cert);
  811. }
  812. const char *
  813. getstyle(const char *uri)
  814. {
  815. int i;
  816. if (stylefile)
  817. return stylefile;
  818. for (i = 0; i < LENGTH(styles); ++i) {
  819. if (styles[i].regex &&
  820. !regexec(&(styles[i].re), uri, 0, NULL, 0))
  821. return styles[i].file;
  822. }
  823. return "";
  824. }
  825. void
  826. setstyle(Client *c, const char *file)
  827. {
  828. gchar *style;
  829. if (!g_file_get_contents(file, &style, NULL, NULL)) {
  830. fprintf(stderr, "Could not read style file: %s\n", file);
  831. return;
  832. }
  833. webkit_user_content_manager_add_style_sheet(
  834. webkit_web_view_get_user_content_manager(c->view),
  835. webkit_user_style_sheet_new(style,
  836. WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES,
  837. WEBKIT_USER_STYLE_LEVEL_USER,
  838. NULL, NULL));
  839. g_free(style);
  840. }
  841. void
  842. runscript(Client *c)
  843. {
  844. gchar *script;
  845. gsize l;
  846. if (g_file_get_contents(scriptfile, &script, &l, NULL) && l)
  847. evalscript(c, "%s", script);
  848. g_free(script);
  849. }
  850. void
  851. evalscript(Client *c, const char *jsstr, ...)
  852. {
  853. va_list ap;
  854. gchar *script;
  855. va_start(ap, jsstr);
  856. script = g_strdup_vprintf(jsstr, ap);
  857. va_end(ap);
  858. webkit_web_view_run_javascript(c->view, script, NULL, NULL, NULL);
  859. g_free(script);
  860. }
  861. void
  862. updatewinid(Client *c)
  863. {
  864. snprintf(winid, LENGTH(winid), "%lu", c->xid);
  865. }
  866. void
  867. handleplumb(Client *c, const char *uri)
  868. {
  869. Arg a = (Arg)PLUMB(uri);
  870. spawn(c, &a);
  871. }
  872. void
  873. newwindow(Client *c, const Arg *a, int noembed)
  874. {
  875. int i = 0;
  876. char tmp[64];
  877. const char *cmd[29], *uri;
  878. const Arg arg = { .v = cmd };
  879. cmd[i++] = argv0;
  880. cmd[i++] = "-a";
  881. cmd[i++] = curconfig[CookiePolicies].val.v;
  882. cmd[i++] = curconfig[ScrollBars].val.i ? "-B" : "-b";
  883. if (cookiefile && g_strcmp0(cookiefile, "")) {
  884. cmd[i++] = "-c";
  885. cmd[i++] = cookiefile;
  886. }
  887. if (stylefile && g_strcmp0(stylefile, "")) {
  888. cmd[i++] = "-C";
  889. cmd[i++] = stylefile;
  890. }
  891. cmd[i++] = curconfig[DiskCache].val.i ? "-D" : "-d";
  892. if (embed && !noembed) {
  893. cmd[i++] = "-e";
  894. snprintf(tmp, LENGTH(tmp), "%lu", embed);
  895. cmd[i++] = tmp;
  896. }
  897. cmd[i++] = curconfig[RunInFullscreen].val.i ? "-F" : "-f" ;
  898. cmd[i++] = curconfig[Geolocation].val.i ? "-G" : "-g" ;
  899. cmd[i++] = curconfig[LoadImages].val.i ? "-I" : "-i" ;
  900. cmd[i++] = curconfig[KioskMode].val.i ? "-K" : "-k" ;
  901. cmd[i++] = curconfig[Style].val.i ? "-M" : "-m" ;
  902. cmd[i++] = curconfig[Inspector].val.i ? "-N" : "-n" ;
  903. cmd[i++] = curconfig[Plugins].val.i ? "-P" : "-p" ;
  904. if (scriptfile && g_strcmp0(scriptfile, "")) {
  905. cmd[i++] = "-r";
  906. cmd[i++] = scriptfile;
  907. }
  908. cmd[i++] = curconfig[JavaScript].val.i ? "-S" : "-s";
  909. cmd[i++] = curconfig[StrictTLS].val.i ? "-T" : "-t";
  910. if (fulluseragent && g_strcmp0(fulluseragent, "")) {
  911. cmd[i++] = "-u";
  912. cmd[i++] = fulluseragent;
  913. }
  914. if (showxid)
  915. cmd[i++] = "-w";
  916. cmd[i++] = curconfig[Certificate].val.i ? "-X" : "-x" ;
  917. /* do not keep zoom level */
  918. cmd[i++] = "--";
  919. if ((uri = a->v))
  920. cmd[i++] = uri;
  921. cmd[i] = NULL;
  922. spawn(c, &arg);
  923. }
  924. void
  925. spawn(Client *c, const Arg *a)
  926. {
  927. if (fork() == 0) {
  928. if (dpy)
  929. close(ConnectionNumber(dpy));
  930. close(pipein[0]);
  931. close(pipeout[1]);
  932. setsid();
  933. execvp(((char **)a->v)[0], (char **)a->v);
  934. fprintf(stderr, "%s: execvp %s", argv0, ((char **)a->v)[0]);
  935. perror(" failed");
  936. exit(1);
  937. }
  938. }
  939. void
  940. destroyclient(Client *c)
  941. {
  942. Client *p;
  943. webkit_web_view_stop_loading(c->view);
  944. /* Not needed, has already been called
  945. gtk_widget_destroy(c->win);
  946. */
  947. for (p = clients; p && p->next != c; p = p->next)
  948. ;
  949. if (p)
  950. p->next = c->next;
  951. else
  952. clients = c->next;
  953. free(c);
  954. }
  955. void
  956. cleanup(void)
  957. {
  958. while (clients)
  959. destroyclient(clients);
  960. close(pipein[0]);
  961. close(pipeout[1]);
  962. g_free(cookiefile);
  963. g_free(scriptfile);
  964. g_free(stylefile);
  965. g_free(cachedir);
  966. XCloseDisplay(dpy);
  967. }
  968. WebKitWebView *
  969. newview(Client *c, WebKitWebView *rv)
  970. {
  971. WebKitWebView *v;
  972. WebKitSettings *settings;
  973. WebKitWebContext *context;
  974. WebKitCookieManager *cookiemanager;
  975. WebKitUserContentManager *contentmanager;
  976. /* Webview */
  977. if (rv) {
  978. v = WEBKIT_WEB_VIEW(webkit_web_view_new_with_related_view(rv));
  979. } else {
  980. settings = webkit_settings_new_with_settings(
  981. "allow-file-access-from-file-urls", curconfig[FileURLsCrossAccess].val.i,
  982. "allow-universal-access-from-file-urls", curconfig[FileURLsCrossAccess].val.i,
  983. "auto-load-images", curconfig[LoadImages].val.i,
  984. "default-charset", curconfig[DefaultCharset].val.v,
  985. "default-font-size", curconfig[FontSize].val.i,
  986. "enable-caret-browsing", curconfig[CaretBrowsing].val.i,
  987. "enable-developer-extras", curconfig[Inspector].val.i,
  988. "enable-dns-prefetching", curconfig[DNSPrefetch].val.i,
  989. "enable-frame-flattening", curconfig[FrameFlattening].val.i,
  990. "enable-html5-database", curconfig[DiskCache].val.i,
  991. "enable-html5-local-storage", curconfig[DiskCache].val.i,
  992. "enable-java", curconfig[Java].val.i,
  993. "enable-javascript", curconfig[JavaScript].val.i,
  994. "enable-plugins", curconfig[Plugins].val.i,
  995. "enable-accelerated-2d-canvas", curconfig[AcceleratedCanvas].val.i,
  996. "enable-site-specific-quirks", curconfig[SiteQuirks].val.i,
  997. "enable-smooth-scrolling", curconfig[SmoothScrolling].val.i,
  998. "enable-webgl", curconfig[WebGL].val.i,
  999. "media-playback-requires-user-gesture", curconfig[MediaManualPlay].val.i,
  1000. NULL);
  1001. /* For more interesting settings, have a look at
  1002. * http://webkitgtk.org/reference/webkit2gtk/stable/WebKitSettings.html */
  1003. if (strcmp(fulluseragent, "")) {
  1004. webkit_settings_set_user_agent(settings, fulluseragent);
  1005. } else if (surfuseragent) {
  1006. webkit_settings_set_user_agent_with_application_details(
  1007. settings, "Surf", VERSION);
  1008. }
  1009. useragent = webkit_settings_get_user_agent(settings);
  1010. contentmanager = webkit_user_content_manager_new();
  1011. context = webkit_web_context_new_with_website_data_manager(
  1012. webkit_website_data_manager_new(
  1013. "base-cache-directory", cachedir,
  1014. "base-data-directory", cachedir,
  1015. NULL));
  1016. cookiemanager = webkit_web_context_get_cookie_manager(context);
  1017. /* rendering process model, can be a shared unique one
  1018. * or one for each view */
  1019. webkit_web_context_set_process_model(context,
  1020. WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
  1021. /* TLS */
  1022. webkit_web_context_set_tls_errors_policy(context,
  1023. curconfig[StrictTLS].val.i ? WEBKIT_TLS_ERRORS_POLICY_FAIL :
  1024. WEBKIT_TLS_ERRORS_POLICY_IGNORE);
  1025. /* disk cache */
  1026. webkit_web_context_set_cache_model(context,
  1027. curconfig[DiskCache].val.i ? WEBKIT_CACHE_MODEL_WEB_BROWSER :
  1028. WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
  1029. /* Currently only works with text file to be compatible with curl */
  1030. webkit_cookie_manager_set_persistent_storage(cookiemanager,
  1031. cookiefile, WEBKIT_COOKIE_PERSISTENT_STORAGE_TEXT);
  1032. /* cookie policy */
  1033. webkit_cookie_manager_set_accept_policy(cookiemanager,
  1034. cookiepolicy_get());
  1035. /* languages */
  1036. webkit_web_context_set_preferred_languages(context,
  1037. curconfig[PreferredLanguages].val.v);
  1038. webkit_web_context_set_spell_checking_languages(context,
  1039. curconfig[SpellLanguages].val.v);
  1040. webkit_web_context_set_spell_checking_enabled(context,
  1041. curconfig[SpellChecking].val.i);
  1042. g_signal_connect(G_OBJECT(context), "download-started",
  1043. G_CALLBACK(downloadstarted), c);
  1044. g_signal_connect(G_OBJECT(context), "initialize-web-extensions",
  1045. G_CALLBACK(initwebextensions), c);
  1046. v = g_object_new(WEBKIT_TYPE_WEB_VIEW,
  1047. "settings", settings,
  1048. "user-content-manager", contentmanager,
  1049. "web-context", context,
  1050. NULL);
  1051. }
  1052. g_signal_connect(G_OBJECT(v), "notify::estimated-load-progress",
  1053. G_CALLBACK(progresschanged), c);
  1054. g_signal_connect(G_OBJECT(v), "notify::title",
  1055. G_CALLBACK(titlechanged), c);
  1056. g_signal_connect(G_OBJECT(v), "button-release-event",
  1057. G_CALLBACK(buttonreleased), c);
  1058. g_signal_connect(G_OBJECT(v), "close",
  1059. G_CALLBACK(closeview), c);
  1060. g_signal_connect(G_OBJECT(v), "create",
  1061. G_CALLBACK(createview), c);
  1062. g_signal_connect(G_OBJECT(v), "decide-policy",
  1063. G_CALLBACK(decidepolicy), c);
  1064. g_signal_connect(G_OBJECT(v), "insecure-content-detected",
  1065. G_CALLBACK(insecurecontent), c);
  1066. g_signal_connect(G_OBJECT(v), "load-failed-with-tls-errors",
  1067. G_CALLBACK(loadfailedtls), c);
  1068. g_signal_connect(G_OBJECT(v), "load-changed",
  1069. G_CALLBACK(loadchanged), c);
  1070. g_signal_connect(G_OBJECT(v), "mouse-target-changed",
  1071. G_CALLBACK(mousetargetchanged), c);
  1072. g_signal_connect(G_OBJECT(v), "permission-request",
  1073. G_CALLBACK(permissionrequested), c);
  1074. g_signal_connect(G_OBJECT(v), "ready-to-show",
  1075. G_CALLBACK(showview), c);
  1076. g_signal_connect(G_OBJECT(v), "web-process-terminated",
  1077. G_CALLBACK(webprocessterminated), c);
  1078. return v;
  1079. }
  1080. static gboolean
  1081. readpipe(GIOChannel *s, GIOCondition ioc, gpointer unused)
  1082. {
  1083. static char msg[MSGBUFSZ], msgsz;
  1084. GError *gerr = NULL;
  1085. if (g_io_channel_read_chars(s, msg, sizeof(msg), NULL, &gerr) !=
  1086. G_IO_STATUS_NORMAL) {
  1087. fprintf(stderr, "surf: error reading pipe: %s\n",
  1088. gerr->message);
  1089. g_error_free(gerr);
  1090. return TRUE;
  1091. }
  1092. if ((msgsz = msg[0]) < 3) {
  1093. fprintf(stderr, "surf: message too short: %d\n", msgsz);
  1094. return TRUE;
  1095. }
  1096. switch (msg[2]) {
  1097. case 'i':
  1098. close(pipein[1]);
  1099. close(pipeout[0]);
  1100. break;
  1101. }
  1102. return TRUE;
  1103. }
  1104. void
  1105. initwebextensions(WebKitWebContext *wc, Client *c)
  1106. {
  1107. GVariant *gv;
  1108. if (!pipeout[0] || !pipein[1])
  1109. return;
  1110. gv = g_variant_new("(ii)", pipeout[0], pipein[1]);
  1111. webkit_web_context_set_web_extensions_initialization_user_data(wc, gv);
  1112. webkit_web_context_set_web_extensions_directory(wc, WEBEXTDIR);
  1113. }
  1114. GtkWidget *
  1115. createview(WebKitWebView *v, WebKitNavigationAction *a, Client *c)
  1116. {
  1117. Client *n;
  1118. switch (webkit_navigation_action_get_navigation_type(a)) {
  1119. case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
  1120. /*
  1121. * popup windows of type “other” are almost always triggered
  1122. * by user gesture, so inverse the logic here
  1123. */
  1124. /* instead of this, compare destination uri to mouse-over uri for validating window */
  1125. if (webkit_navigation_action_is_user_gesture(a))
  1126. return NULL;
  1127. case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
  1128. case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
  1129. case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
  1130. case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
  1131. case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED:
  1132. n = newclient(c);
  1133. break;
  1134. default:
  1135. return NULL;
  1136. }
  1137. return GTK_WIDGET(n->view);
  1138. }
  1139. gboolean
  1140. buttonreleased(GtkWidget *w, GdkEvent *e, Client *c)
  1141. {
  1142. WebKitHitTestResultContext element;
  1143. int i;
  1144. element = webkit_hit_test_result_get_context(c->mousepos);
  1145. for (i = 0; i < LENGTH(buttons); ++i) {
  1146. if (element & buttons[i].target &&
  1147. e->button.button == buttons[i].button &&
  1148. CLEANMASK(e->button.state) == CLEANMASK(buttons[i].mask) &&
  1149. buttons[i].func) {
  1150. buttons[i].func(c, &buttons[i].arg, c->mousepos);
  1151. return buttons[i].stopevent;
  1152. }
  1153. }
  1154. return FALSE;
  1155. }
  1156. GdkFilterReturn
  1157. processx(GdkXEvent *e, GdkEvent *event, gpointer d)
  1158. {
  1159. Client *c = (Client *)d;
  1160. XPropertyEvent *ev;
  1161. Arg a;
  1162. if (((XEvent *)e)->type == PropertyNotify) {
  1163. ev = &((XEvent *)e)->xproperty;
  1164. if (ev->state == PropertyNewValue) {
  1165. if (ev->atom == atoms[AtomFind]) {
  1166. find(c, NULL);
  1167. return GDK_FILTER_REMOVE;
  1168. } else if (ev->atom == atoms[AtomSearch]) {
  1169. a.v = getatom(c, AtomSearch);
  1170. search(c, &a);
  1171. } else if (ev->atom == atoms[AtomGo]) {
  1172. a.v = getatom(c, AtomGo);
  1173. loaduri(c, &a);
  1174. return GDK_FILTER_REMOVE;
  1175. }
  1176. }
  1177. }
  1178. return GDK_FILTER_CONTINUE;
  1179. }
  1180. gboolean
  1181. winevent(GtkWidget *w, GdkEvent *e, Client *c)
  1182. {
  1183. int i;
  1184. switch (e->type) {
  1185. case GDK_ENTER_NOTIFY:
  1186. c->overtitle = c->targeturi;
  1187. updatetitle(c);
  1188. break;
  1189. case GDK_KEY_PRESS:
  1190. if (!curconfig[KioskMode].val.i) {
  1191. for (i = 0; i < LENGTH(keys); ++i) {
  1192. if (gdk_keyval_to_lower(e->key.keyval) ==
  1193. keys[i].keyval &&
  1194. CLEANMASK(e->key.state) == keys[i].mod &&
  1195. keys[i].func) {
  1196. updatewinid(c);
  1197. keys[i].func(c, &(keys[i].arg));
  1198. return TRUE;
  1199. }
  1200. }
  1201. }
  1202. case GDK_LEAVE_NOTIFY:
  1203. c->overtitle = NULL;
  1204. updatetitle(c);
  1205. break;
  1206. case GDK_WINDOW_STATE:
  1207. if (e->window_state.changed_mask ==
  1208. GDK_WINDOW_STATE_FULLSCREEN)
  1209. c->fullscreen = e->window_state.new_window_state &
  1210. GDK_WINDOW_STATE_FULLSCREEN;
  1211. break;
  1212. default:
  1213. break;
  1214. }
  1215. return FALSE;
  1216. }
  1217. void
  1218. showview(WebKitWebView *v, Client *c)
  1219. {
  1220. GdkRGBA bgcolor = { 0 };
  1221. GdkWindow *gwin;
  1222. c->finder = webkit_web_view_get_find_controller(c->view);
  1223. c->inspector = webkit_web_view_get_inspector(c->view);
  1224. c->pageid = webkit_web_view_get_page_id(c->view);
  1225. c->win = createwindow(c);
  1226. gtk_container_add(GTK_CONTAINER(c->win), GTK_WIDGET(c->view));
  1227. gtk_widget_show_all(c->win);
  1228. gtk_widget_grab_focus(GTK_WIDGET(c->view));
  1229. gwin = gtk_widget_get_window(GTK_WIDGET(c->win));
  1230. c->xid = gdk_x11_window_get_xid(gwin);
  1231. updatewinid(c);
  1232. if (showxid) {
  1233. gdk_display_sync(gtk_widget_get_display(c->win));
  1234. puts(winid);
  1235. fflush(stdout);
  1236. }
  1237. if (curconfig[HideBackground].val.i)
  1238. webkit_web_view_set_background_color(c->view, &bgcolor);
  1239. if (!curconfig[KioskMode].val.i) {
  1240. gdk_window_set_events(gwin, GDK_ALL_EVENTS_MASK);
  1241. gdk_window_add_filter(gwin, processx, c);
  1242. }
  1243. if (curconfig[RunInFullscreen].val.i)
  1244. togglefullscreen(c, NULL);
  1245. if (curconfig[ZoomLevel].val.f != 1.0)
  1246. webkit_web_view_set_zoom_level(c->view,
  1247. curconfig[ZoomLevel].val.f);
  1248. setatom(c, AtomFind, "");
  1249. setatom(c, AtomUri, "about:blank");
  1250. }
  1251. GtkWidget *
  1252. createwindow(Client *c)
  1253. {
  1254. char *wmstr;
  1255. GtkWidget *w;
  1256. if (embed) {
  1257. w = gtk_plug_new(embed);
  1258. } else {
  1259. w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  1260. wmstr = g_path_get_basename(argv0);
  1261. gtk_window_set_wmclass(GTK_WINDOW(w), wmstr, "Surf");
  1262. g_free(wmstr);
  1263. wmstr = g_strdup_printf("%s[%lu]", "Surf", c->pageid);
  1264. gtk_window_set_role(GTK_WINDOW(w), wmstr);
  1265. g_free(wmstr);
  1266. gtk_window_set_default_size(GTK_WINDOW(w), winsize[0], winsize[1]);
  1267. }
  1268. g_signal_connect(G_OBJECT(w), "destroy",
  1269. G_CALLBACK(destroywin), c);
  1270. g_signal_connect(G_OBJECT(w), "enter-notify-event",
  1271. G_CALLBACK(winevent), c);
  1272. g_signal_connect(G_OBJECT(w), "key-press-event",
  1273. G_CALLBACK(winevent), c);
  1274. g_signal_connect(G_OBJECT(w), "leave-notify-event",
  1275. G_CALLBACK(winevent), c);
  1276. g_signal_connect(G_OBJECT(w), "window-state-event",
  1277. G_CALLBACK(winevent), c);
  1278. return w;
  1279. }
  1280. gboolean
  1281. loadfailedtls(WebKitWebView *v, gchar *uri, GTlsCertificate *cert,
  1282. GTlsCertificateFlags err, Client *c)
  1283. {
  1284. GString *errmsg = g_string_new(NULL);
  1285. gchar *html, *pem;
  1286. c->failedcert = g_object_ref(cert);
  1287. c->tlserr = err;
  1288. c->errorpage = 1;
  1289. if (err & G_TLS_CERTIFICATE_UNKNOWN_CA)
  1290. g_string_append(errmsg,
  1291. "The signing certificate authority is not known.<br>");
  1292. if (err & G_TLS_CERTIFICATE_BAD_IDENTITY)
  1293. g_string_append(errmsg,
  1294. "The certificate does not match the expected identity "
  1295. "of the site that it was retrieved from.<br>");
  1296. if (err & G_TLS_CERTIFICATE_NOT_ACTIVATED)
  1297. g_string_append(errmsg,
  1298. "The certificate's activation time "
  1299. "is still in the future.<br>");
  1300. if (err & G_TLS_CERTIFICATE_EXPIRED)
  1301. g_string_append(errmsg, "The certificate has expired.<br>");
  1302. if (err & G_TLS_CERTIFICATE_REVOKED)
  1303. g_string_append(errmsg,
  1304. "The certificate has been revoked according to "
  1305. "the GTlsConnection's certificate revocation list.<br>");
  1306. if (err & G_TLS_CERTIFICATE_INSECURE)
  1307. g_string_append(errmsg,
  1308. "The certificate's algorithm is considered insecure.<br>");
  1309. if (err & G_TLS_CERTIFICATE_GENERIC_ERROR)
  1310. g_string_append(errmsg,
  1311. "Some error occurred validating the certificate.<br>");
  1312. g_object_get(cert, "certificate-pem", &pem, NULL);
  1313. html = g_strdup_printf("<p>Could not validate TLS for “%s”<br>%s</p>"
  1314. "<p>You can inspect the following certificate "
  1315. "with Ctrl-t (default keybinding).</p>"
  1316. "<p><pre>%s</pre></p>", uri, errmsg->str, pem);
  1317. g_free(pem);
  1318. g_string_free(errmsg, TRUE);
  1319. webkit_web_view_load_alternate_html(c->view, html, uri, NULL);
  1320. g_free(html);
  1321. return TRUE;
  1322. }
  1323. void
  1324. loadchanged(WebKitWebView *v, WebKitLoadEvent e, Client *c)
  1325. {
  1326. const char *uri = geturi(c);
  1327. switch (e) {
  1328. case WEBKIT_LOAD_STARTED:
  1329. setatom(c, AtomUri, uri);
  1330. c->title = uri;
  1331. c->https = c->insecure = 0;
  1332. seturiparameters(c, uri, loadtransient);
  1333. if (c->errorpage)
  1334. c->errorpage = 0;
  1335. else
  1336. g_clear_object(&c->failedcert);
  1337. break;
  1338. case WEBKIT_LOAD_REDIRECTED:
  1339. setatom(c, AtomUri, uri);
  1340. c->title = uri;
  1341. seturiparameters(c, uri, loadtransient);
  1342. break;
  1343. case WEBKIT_LOAD_COMMITTED:
  1344. seturiparameters(c, uri, loadcommitted);
  1345. c->https = webkit_web_view_get_tls_info(c->view, &c->cert,
  1346. &c->tlserr);
  1347. break;
  1348. case WEBKIT_LOAD_FINISHED:
  1349. seturiparameters(c, uri, loadfinished);
  1350. /* Disabled until we write some WebKitWebExtension for
  1351. * manipulating the DOM directly.
  1352. evalscript(c, "document.documentElement.style.overflow = '%s'",
  1353. enablescrollbars ? "auto" : "hidden");
  1354. */
  1355. runscript(c);
  1356. break;
  1357. }
  1358. updatetitle(c);
  1359. }
  1360. void
  1361. progresschanged(WebKitWebView *v, GParamSpec *ps, Client *c)
  1362. {
  1363. c->progress = webkit_web_view_get_estimated_load_progress(c->view) *
  1364. 100;
  1365. updatetitle(c);
  1366. }
  1367. void
  1368. titlechanged(WebKitWebView *view, GParamSpec *ps, Client *c)
  1369. {
  1370. c->title = webkit_web_view_get_title(c->view);
  1371. updatetitle(c);
  1372. }
  1373. void
  1374. mousetargetchanged(WebKitWebView *v, WebKitHitTestResult *h, guint modifiers,
  1375. Client *c)
  1376. {
  1377. WebKitHitTestResultContext hc = webkit_hit_test_result_get_context(h);
  1378. /* Keep the hit test to know where is the pointer on the next click */
  1379. c->mousepos = h;
  1380. if (hc & OnLink)
  1381. c->targeturi = webkit_hit_test_result_get_link_uri(h);
  1382. else if (hc & OnImg)
  1383. c->targeturi = webkit_hit_test_result_get_image_uri(h);
  1384. else if (hc & OnMedia)
  1385. c->targeturi = webkit_hit_test_result_get_media_uri(h);
  1386. else
  1387. c->targeturi = NULL;
  1388. c->overtitle = c->targeturi;
  1389. updatetitle(c);
  1390. }
  1391. gboolean
  1392. permissionrequested(WebKitWebView *v, WebKitPermissionRequest *r, Client *c)
  1393. {
  1394. ParamName param = ParameterLast;
  1395. if (WEBKIT_IS_GEOLOCATION_PERMISSION_REQUEST(r)) {
  1396. param = Geolocation;
  1397. } else if (WEBKIT_IS_USER_MEDIA_PERMISSION_REQUEST(r)) {
  1398. if (webkit_user_media_permission_is_for_audio_device(
  1399. WEBKIT_USER_MEDIA_PERMISSION_REQUEST(r)))
  1400. param = AccessMicrophone;
  1401. else if (webkit_user_media_permission_is_for_video_device(
  1402. WEBKIT_USER_MEDIA_PERMISSION_REQUEST(r)))
  1403. param = AccessWebcam;
  1404. } else {
  1405. return FALSE;
  1406. }
  1407. if (curconfig[param].val.i)
  1408. webkit_permission_request_allow(r);
  1409. else
  1410. webkit_permission_request_deny(r);
  1411. return TRUE;
  1412. }
  1413. gboolean
  1414. decidepolicy(WebKitWebView *v, WebKitPolicyDecision *d,
  1415. WebKitPolicyDecisionType dt, Client *c)
  1416. {
  1417. switch (dt) {
  1418. case WEBKIT_POLICY_DECISION_TYPE_NAVIGATION_ACTION:
  1419. decidenavigation(d, c);
  1420. break;
  1421. case WEBKIT_POLICY_DECISION_TYPE_NEW_WINDOW_ACTION:
  1422. decidenewwindow(d, c);
  1423. break;
  1424. case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
  1425. decideresource(d, c);
  1426. break;
  1427. default:
  1428. webkit_policy_decision_ignore(d);
  1429. break;
  1430. }
  1431. return TRUE;
  1432. }
  1433. void
  1434. decidenavigation(WebKitPolicyDecision *d, Client *c)
  1435. {
  1436. WebKitNavigationAction *a =
  1437. webkit_navigation_policy_decision_get_navigation_action(
  1438. WEBKIT_NAVIGATION_POLICY_DECISION(d));
  1439. switch (webkit_navigation_action_get_navigation_type(a)) {
  1440. case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
  1441. case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
  1442. case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
  1443. case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
  1444. case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED: /* fallthrough */
  1445. case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
  1446. default:
  1447. /* Do not navigate to links with a "_blank" target (popup) */
  1448. if (webkit_navigation_policy_decision_get_frame_name(
  1449. WEBKIT_NAVIGATION_POLICY_DECISION(d))) {
  1450. webkit_policy_decision_ignore(d);
  1451. } else {
  1452. /* Filter out navigation to different domain ? */
  1453. /* get action→urirequest, copy and load in new window+view
  1454. * on Ctrl+Click ? */
  1455. webkit_policy_decision_use(d);
  1456. }
  1457. break;
  1458. }
  1459. }
  1460. void
  1461. decidenewwindow(WebKitPolicyDecision *d, Client *c)
  1462. {
  1463. Arg arg;
  1464. WebKitNavigationAction *a =
  1465. webkit_navigation_policy_decision_get_navigation_action(
  1466. WEBKIT_NAVIGATION_POLICY_DECISION(d));
  1467. switch (webkit_navigation_action_get_navigation_type(a)) {
  1468. case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
  1469. case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
  1470. case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
  1471. case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
  1472. case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED:
  1473. /* Filter domains here */
  1474. /* If the value of “mouse-button” is not 0, then the navigation was triggered by a mouse event.
  1475. * test for link clicked but no button ? */
  1476. arg.v = webkit_uri_request_get_uri(
  1477. webkit_navigation_action_get_request(a));
  1478. newwindow(c, &arg, 0);
  1479. break;
  1480. case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
  1481. default:
  1482. break;
  1483. }
  1484. webkit_policy_decision_ignore(d);
  1485. }
  1486. void
  1487. decideresource(WebKitPolicyDecision *d, Client *c)
  1488. {
  1489. int i, isascii = 1;
  1490. WebKitResponsePolicyDecision *r = WEBKIT_RESPONSE_POLICY_DECISION(d);
  1491. WebKitURIResponse *res =
  1492. webkit_response_policy_decision_get_response(r);
  1493. const gchar *uri = webkit_uri_response_get_uri(res);
  1494. if (g_str_has_suffix(uri, "/favicon.ico")) {
  1495. webkit_policy_decision_ignore(d);
  1496. return;
  1497. }
  1498. if (!g_str_has_prefix(uri, "http://")
  1499. && !g_str_has_prefix(uri, "https://")
  1500. && !g_str_has_prefix(uri, "about:")
  1501. && !g_str_has_prefix(uri, "file://")
  1502. && !g_str_has_prefix(uri, "data:")
  1503. && !g_str_has_prefix(uri, "blob:")
  1504. && strlen(uri) > 0) {
  1505. for (i = 0; i < strlen(uri); i++) {
  1506. if (!g_ascii_isprint(uri[i])) {
  1507. isascii = 0;
  1508. break;
  1509. }
  1510. }
  1511. if (isascii) {
  1512. handleplumb(c, uri);
  1513. webkit_policy_decision_ignore(d);
  1514. return;
  1515. }
  1516. }
  1517. if (webkit_response_policy_decision_is_mime_type_supported(r)) {
  1518. webkit_policy_decision_use(d);
  1519. } else {
  1520. webkit_policy_decision_ignore(d);
  1521. download(c, res);
  1522. }
  1523. }
  1524. void
  1525. insecurecontent(WebKitWebView *v, WebKitInsecureContentEvent e, Client *c)
  1526. {
  1527. c->insecure = 1;
  1528. }
  1529. void
  1530. downloadstarted(WebKitWebContext *wc, WebKitDownload *d, Client *c)
  1531. {
  1532. g_signal_connect(G_OBJECT(d), "notify::response",
  1533. G_CALLBACK(responsereceived), c);
  1534. }
  1535. void
  1536. responsereceived(WebKitDownload *d, GParamSpec *ps, Client *c)
  1537. {
  1538. download(c, webkit_download_get_response(d));
  1539. webkit_download_cancel(d);
  1540. }
  1541. void
  1542. download(Client *c, WebKitURIResponse *r)
  1543. {
  1544. Arg a = (Arg)DOWNLOAD(webkit_uri_response_get_uri(r), geturi(c));
  1545. spawn(c, &a);
  1546. }
  1547. void
  1548. webprocessterminated(WebKitWebView *v, WebKitWebProcessTerminationReason r,
  1549. Client *c)
  1550. {
  1551. fprintf(stderr, "web process terminated: %s\n",
  1552. r == WEBKIT_WEB_PROCESS_CRASHED ? "crashed" : "no memory");
  1553. closeview(v, c);
  1554. }
  1555. void
  1556. closeview(WebKitWebView *v, Client *c)
  1557. {
  1558. gtk_widget_destroy(c->win);
  1559. }
  1560. void
  1561. destroywin(GtkWidget* w, Client *c)
  1562. {
  1563. destroyclient(c);
  1564. if (!clients)
  1565. gtk_main_quit();
  1566. }
  1567. void
  1568. pasteuri(GtkClipboard *clipboard, const char *text, gpointer d)
  1569. {
  1570. Arg a = {.v = text };
  1571. if (text)
  1572. loaduri((Client *) d, &a);
  1573. }
  1574. void
  1575. reload(Client *c, const Arg *a)
  1576. {
  1577. if (a->i)
  1578. webkit_web_view_reload_bypass_cache(c->view);
  1579. else
  1580. webkit_web_view_reload(c->view);
  1581. }
  1582. void
  1583. print(Client *c, const Arg *a)
  1584. {
  1585. webkit_print_operation_run_dialog(webkit_print_operation_new(c->view),
  1586. GTK_WINDOW(c->win));
  1587. }
  1588. void
  1589. showcert(Client *c, const Arg *a)
  1590. {
  1591. GTlsCertificate *cert = c->failedcert ? c->failedcert : c->cert;
  1592. GcrCertificate *gcrt;
  1593. GByteArray *crt;
  1594. GtkWidget *win;
  1595. GcrCertificateWidget *wcert;
  1596. if (!cert)
  1597. return;
  1598. g_object_get(cert, "certificate", &crt, NULL);
  1599. gcrt = gcr_simple_certificate_new(crt->data, crt->len);
  1600. g_byte_array_unref(crt);
  1601. win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  1602. wcert = gcr_certificate_widget_new(gcrt);
  1603. g_object_unref(gcrt);
  1604. gtk_container_add(GTK_CONTAINER(win), GTK_WIDGET(wcert));
  1605. gtk_widget_show_all(win);
  1606. }
  1607. void
  1608. clipboard(Client *c, const Arg *a)
  1609. {
  1610. /* User defined choice of selection, see config.h */
  1611. GdkAtom selection = GDK_SELECTION_PRIMARY;
  1612. if (curconfig[ClipboardNotPrimary].val.i > 0)
  1613. selection = GDK_SELECTION_CLIPBOARD;
  1614. if (a->i) { /* load clipboard uri */
  1615. gtk_clipboard_request_text(gtk_clipboard_get(
  1616. selection),
  1617. pasteuri, c);
  1618. } else { /* copy uri */
  1619. gtk_clipboard_set_text(gtk_clipboard_get(
  1620. selection), c->targeturi
  1621. ? c->targeturi : geturi(c), -1);
  1622. }
  1623. }
  1624. void
  1625. zoom(Client *c, const Arg *a)
  1626. {
  1627. if (a->i > 0)
  1628. webkit_web_view_set_zoom_level(c->view,
  1629. curconfig[ZoomLevel].val.f + 0.1);
  1630. else if (a->i < 0)
  1631. webkit_web_view_set_zoom_level(c->view,
  1632. curconfig[ZoomLevel].val.f - 0.1);
  1633. else
  1634. webkit_web_view_set_zoom_level(c->view, 1.0);
  1635. curconfig[ZoomLevel].val.f = webkit_web_view_get_zoom_level(c->view);
  1636. }
  1637. static void
  1638. msgext(Client *c, char type, const Arg *a)
  1639. {
  1640. static char msg[MSGBUFSZ];
  1641. int ret;
  1642. if ((ret = snprintf(msg, sizeof(msg), "%c%c%c%c",
  1643. 4, c->pageid, type, a->i))
  1644. >= sizeof(msg)) {
  1645. fprintf(stderr, "surf: message too long: %d\n", ret);
  1646. return;
  1647. }
  1648. if (pipeout[1] && write(pipeout[1], msg, sizeof(msg)) < 0)
  1649. fprintf(stderr, "surf: error sending: %.*s\n", ret-2, msg+2);
  1650. }
  1651. void
  1652. scrollv(Client *c, const Arg *a)
  1653. {
  1654. msgext(c, 'v', a);
  1655. }
  1656. void
  1657. scrollh(Client *c, const Arg *a)
  1658. {
  1659. msgext(c, 'h', a);
  1660. }
  1661. void
  1662. navigate(Client *c, const Arg *a)
  1663. {
  1664. if (a->i < 0)
  1665. webkit_web_view_go_back(c->view);
  1666. else if (a->i > 0)
  1667. webkit_web_view_go_forward(c->view);
  1668. }
  1669. void
  1670. stop(Client *c, const Arg *a)
  1671. {
  1672. webkit_web_view_stop_loading(c->view);
  1673. }
  1674. void
  1675. toggle(Client *c, const Arg *a)
  1676. {
  1677. curconfig[a->i].val.i ^= 1;
  1678. setparameter(c, 1, (ParamName)a->i, &curconfig[a->i].val);
  1679. }
  1680. void
  1681. togglefullscreen(Client *c, const Arg *a)
  1682. {
  1683. /* toggling value is handled in winevent() */
  1684. if (c->fullscreen)
  1685. gtk_window_unfullscreen(GTK_WINDOW(c->win));
  1686. else
  1687. gtk_window_fullscreen(GTK_WINDOW(c->win));
  1688. }
  1689. void
  1690. togglecookiepolicy(Client *c, const Arg *a)
  1691. {
  1692. ++cookiepolicy;
  1693. cookiepolicy %= strlen(curconfig[CookiePolicies].val.v);
  1694. setparameter(c, 0, CookiePolicies, NULL);
  1695. }
  1696. void
  1697. toggleinspector(Client *c, const Arg *a)
  1698. {
  1699. if (webkit_web_inspector_is_attached(c->inspector))
  1700. webkit_web_inspector_close(c->inspector);
  1701. else if (curconfig[Inspector].val.i)
  1702. webkit_web_inspector_show(c->inspector);
  1703. }
  1704. void
  1705. find(Client *c, const Arg *a)
  1706. {
  1707. const char *s, *f;
  1708. if (a && a->i) {
  1709. if (a->i > 0)
  1710. webkit_find_controller_search_next(c->finder);
  1711. else
  1712. webkit_find_controller_search_previous(c->finder);
  1713. } else {
  1714. s = getatom(c, AtomFind);
  1715. f = webkit_find_controller_get_search_text(c->finder);
  1716. if (g_strcmp0(f, s) == 0) /* reset search */
  1717. webkit_find_controller_search(c->finder, "", findopts,
  1718. G_MAXUINT);
  1719. webkit_find_controller_search(c->finder, s, findopts,
  1720. G_MAXUINT);
  1721. if (strcmp(s, "") == 0)
  1722. webkit_find_controller_search_finish(c->finder);
  1723. }
  1724. }
  1725. void
  1726. clicknavigate(Client *c, const Arg *a, WebKitHitTestResult *h)
  1727. {
  1728. navigate(c, a);
  1729. }
  1730. void
  1731. clicknewwindow(Client *c, const Arg *a, WebKitHitTestResult *h)
  1732. {
  1733. Arg arg;
  1734. arg.v = webkit_hit_test_result_get_link_uri(h);
  1735. newwindow(c, &arg, a->i);
  1736. }
  1737. void
  1738. clickexternplayer(Client *c, const Arg *a, WebKitHitTestResult *h)
  1739. {
  1740. Arg arg;
  1741. arg = (Arg)VIDEOPLAY(webkit_hit_test_result_get_media_uri(h));
  1742. spawn(c, &arg);
  1743. }
  1744. int
  1745. main(int argc, char *argv[])
  1746. {
  1747. Arg arg;
  1748. Client *c;
  1749. memset(&arg, 0, sizeof(arg));
  1750. /* command line args */
  1751. ARGBEGIN {
  1752. case 'a':
  1753. defconfig[CookiePolicies].val.v = EARGF(usage());
  1754. defconfig[CookiePolicies].prio = 2;
  1755. break;
  1756. case 'b':
  1757. defconfig[ScrollBars].val.i = 0;
  1758. defconfig[ScrollBars].prio = 2;
  1759. break;
  1760. case 'B':
  1761. defconfig[ScrollBars].val.i = 1;
  1762. defconfig[ScrollBars].prio = 2;
  1763. break;
  1764. case 'c':
  1765. cookiefile = EARGF(usage());
  1766. break;
  1767. case 'C':
  1768. stylefile = EARGF(usage());
  1769. break;
  1770. case 'd':
  1771. defconfig[DiskCache].val.i = 0;
  1772. defconfig[DiskCache].prio = 2;
  1773. break;
  1774. case 'D':
  1775. defconfig[DiskCache].val.i = 1;
  1776. defconfig[DiskCache].prio = 2;
  1777. break;
  1778. case 'e':
  1779. embed = strtol(EARGF(usage()), NULL, 0);
  1780. break;
  1781. case 'f':
  1782. defconfig[RunInFullscreen].val.i = 0;
  1783. defconfig[RunInFullscreen].prio = 2;
  1784. break;
  1785. case 'F':
  1786. defconfig[RunInFullscreen].val.i = 1;
  1787. defconfig[RunInFullscreen].prio = 2;
  1788. break;
  1789. case 'g':
  1790. defconfig[Geolocation].val.i = 0;
  1791. defconfig[Geolocation].prio = 2;
  1792. break;
  1793. case 'G':
  1794. defconfig[Geolocation].val.i = 1;
  1795. defconfig[Geolocation].prio = 2;
  1796. break;
  1797. case 'i':
  1798. defconfig[LoadImages].val.i = 0;
  1799. defconfig[LoadImages].prio = 2;
  1800. break;
  1801. case 'I':
  1802. defconfig[LoadImages].val.i = 1;
  1803. defconfig[LoadImages].prio = 2;
  1804. break;
  1805. case 'k':
  1806. defconfig[KioskMode].val.i = 0;
  1807. defconfig[KioskMode].prio = 2;
  1808. break;
  1809. case 'K':
  1810. defconfig[KioskMode].val.i = 1;
  1811. defconfig[KioskMode].prio = 2;
  1812. break;
  1813. case 'm':
  1814. defconfig[Style].val.i = 0;
  1815. defconfig[Style].prio = 2;
  1816. break;
  1817. case 'M':
  1818. defconfig[Style].val.i = 1;
  1819. defconfig[Style].prio = 2;
  1820. break;
  1821. case 'n':
  1822. defconfig[Inspector].val.i = 0;
  1823. defconfig[Inspector].prio = 2;
  1824. break;
  1825. case 'N':
  1826. defconfig[Inspector].val.i = 1;
  1827. defconfig[Inspector].prio = 2;
  1828. break;
  1829. case 'p':
  1830. defconfig[Plugins].val.i = 0;
  1831. defconfig[Plugins].prio = 2;
  1832. break;
  1833. case 'P':
  1834. defconfig[Plugins].val.i = 1;
  1835. defconfig[Plugins].prio = 2;
  1836. break;
  1837. case 'r':
  1838. scriptfile = EARGF(usage());
  1839. break;
  1840. case 's':
  1841. defconfig[JavaScript].val.i = 0;
  1842. defconfig[JavaScript].prio = 2;
  1843. break;
  1844. case 'S':
  1845. defconfig[JavaScript].val.i = 1;
  1846. defconfig[JavaScript].prio = 2;
  1847. break;
  1848. case 't':
  1849. defconfig[StrictTLS].val.i = 0;
  1850. defconfig[StrictTLS].prio = 2;
  1851. break;
  1852. case 'T':
  1853. defconfig[StrictTLS].val.i = 1;
  1854. defconfig[StrictTLS].prio = 2;
  1855. break;
  1856. case 'u':
  1857. fulluseragent = EARGF(usage());
  1858. break;
  1859. case 'v':
  1860. die("surf-"VERSION", see LICENSE for © details\n");
  1861. case 'w':
  1862. showxid = 1;
  1863. break;
  1864. case 'x':
  1865. defconfig[Certificate].val.i = 0;
  1866. defconfig[Certificate].prio = 2;
  1867. break;
  1868. case 'X':
  1869. defconfig[Certificate].val.i = 1;
  1870. defconfig[Certificate].prio = 2;
  1871. break;
  1872. case 'z':
  1873. defconfig[ZoomLevel].val.f = strtof(EARGF(usage()), NULL);
  1874. defconfig[ZoomLevel].prio = 2;
  1875. break;
  1876. default:
  1877. usage();
  1878. } ARGEND;
  1879. if (argc > 0)
  1880. arg.v = argv[0];
  1881. else
  1882. arg.v = "about:blank";
  1883. setup();
  1884. c = newclient(NULL);
  1885. showview(NULL, c);
  1886. loaduri(c, &arg);
  1887. updatetitle(c);
  1888. gtk_main();
  1889. cleanup();
  1890. return 0;
  1891. }