source: global.c

Last change on this file was 4fd3c04, checked in by Anders Kaseorg <andersk@mit.edu>, 7 years ago
Remove AIM support This code has received almost no security attention, and anyway, AIM is shutting down on December 15, 2017. https://aimemories.tumblr.com/post/166091776077/aimemories Signed-off-by: Anders Kaseorg <andersk@mit.edu>
  • Property mode set to 100644
File size: 19.2 KB
Line 
1#include "owl.h"
2#include <stdio.h>
3#include <sys/ioctl.h>
4
5static void _owl_global_init_windows(owl_global *g);
6
7void owl_global_init(owl_global *g) {
8  char *cd;
9  const char *homedir;
10
11#if !GLIB_CHECK_VERSION(2, 35, 0)
12  g_type_init();
13#endif
14#if !GLIB_CHECK_VERSION(2, 31, 0)
15  g_thread_init(NULL);
16#endif
17
18  owl_select_init();
19
20  g->lines=LINES;
21  g->cols=COLS;
22  /* We shouldn't need this if we initialize lines and cols before the first
23   * owl_window_get_screen, but to be safe, we synchronize. */
24  owl_window_resize(owl_window_get_screen(), g->lines, g->cols);
25
26  g->context_stack = NULL;
27  owl_global_push_context(g, OWL_CTX_STARTUP, NULL, NULL, NULL);
28
29  g->curmsg=0;
30  g->topmsg=0;
31  g->markedmsgid=-1;
32  g->startupargs=NULL;
33
34  owl_variable_dict_setup(&(g->vars));
35
36  g->rightshift=0;
37
38  g->pw = NULL;
39  g->vw = NULL;
40  g->tw = NULL;
41
42  owl_keyhandler_init(&g->kh);
43  owl_keys_setup_keymaps(&g->kh);
44
45  owl_dict_create(&(g->filters));
46  g->filterlist = NULL;
47  g->puntlist = g_ptr_array_new();
48  g->messagequeue = g_queue_new();
49  owl_dict_create(&(g->styledict));
50  g->curmsg_vert_offset=0;
51  g->resizepending=0;
52  g->direction=OWL_DIRECTION_DOWNWARDS;
53  owl_fmtext_init_colorpair_mgr(&(g->cpmgr));
54  g->debug=OWL_DEBUG;
55  owl_regex_init(&g->search_re);
56  g->starttime=time(NULL); /* assumes we call init only a start time */
57  g->lastinputtime=g->starttime;
58  g->last_wakeup_time = g->starttime;
59  g->newmsgproc_pid=0;
60 
61  owl_global_set_config_format(g, 0);
62  owl_global_set_no_have_config(g);
63  owl_history_init(&(g->msghist));
64  owl_history_init(&(g->cmdhist));
65  g->nextmsgid=0;
66
67  /* Fill in some variables which don't have constant defaults */
68
69  /* glib's g_get_home_dir prefers passwd entries to $HOME, so we
70   * explicitly check getenv first. */
71  homedir = getenv("HOME");
72  if (!homedir)
73    homedir = g_get_home_dir();
74  g->homedir = g_strdup(homedir);
75
76  g->confdir = NULL;
77  g->startupfile = NULL;
78  cd = g_build_filename(g->homedir, OWL_CONFIG_DIR, NULL);
79  owl_global_set_confdir(g, cd);
80  g_free(cd);
81
82  g->msglist = owl_messagelist_new();
83
84  _owl_global_init_windows(g);
85
86  g->havezephyr=0;
87
88  owl_errqueue_init(&(g->errqueue));
89
90  owl_zbuddylist_create(&(g->zbuddies));
91
92  g->zaldlist = NULL;
93  g->pseudologin_notify = 0;
94
95  owl_message_init_fmtext_cache();
96  g->kill_buffer = NULL;
97
98  g->interrupt_count = 0;
99#if GLIB_CHECK_VERSION(2, 31, 0)
100  g_mutex_init(&g->interrupt_lock);
101#else
102  g->interrupt_lock = g_mutex_new();
103#endif
104}
105
106static void _owl_global_init_windows(owl_global *g)
107{
108  /* Create the main window */
109  owl_mainpanel_init(&(g->mainpanel));
110
111  /* Create the widgets */
112  g->mw = owl_mainwin_new(g->mainpanel.recwin);
113  owl_msgwin_init(&(g->msgwin), g->mainpanel.msgwin);
114  owl_sepbar_init(g->mainpanel.sepwin);
115
116  owl_window_set_default_cursor(g->mainpanel.sepwin);
117
118  /* set up a pad for input */
119  g->input_pad = newpad(1, 1);
120  nodelay(g->input_pad, 1);
121  keypad(g->input_pad, 1);
122  meta(g->input_pad, 1);
123}
124
125void owl_global_sepbar_dirty(owl_global *g)
126{
127  owl_window_dirty(g->mainpanel.sepwin);
128}
129
130/* Called once perl has been initialized */
131void owl_global_complete_setup(owl_global *g)
132{
133  owl_cmddict_setup(&(g->cmds));
134}
135
136owl_context *owl_global_get_context(const owl_global *g) {
137  if (!g->context_stack)
138    return NULL;
139  return g->context_stack->data;
140}
141
142static void owl_global_activate_context(owl_global *g, owl_context *c) {
143  if (!c)
144    return;
145
146  if (c->keymap) {
147    if (!owl_keyhandler_activate(owl_global_get_keyhandler(g), c->keymap)) {
148      owl_function_error("Unable to activate keymap '%s'", c->keymap);
149    }
150  }
151  owl_window_set_cursor(c->cursor);
152}
153
154void owl_global_push_context(owl_global *g, int mode, void *data, const char *keymap, owl_window *cursor) {
155  owl_context *c;
156  c = owl_context_new(mode, data, keymap, cursor);
157  owl_global_push_context_obj(g, c);
158}
159
160void owl_global_push_context_obj(owl_global *g, owl_context *c)
161{
162  g->context_stack = g_list_prepend(g->context_stack, c);
163  owl_global_activate_context(g, owl_global_get_context(g));
164}
165
166/* Pops the current context from the context stack and returns it. Caller is
167 * responsible for freeing. */
168CALLER_OWN owl_context *owl_global_pop_context_no_delete(owl_global *g)
169{
170  owl_context *c;
171  if (!g->context_stack)
172    return NULL;
173  c = owl_global_get_context(g);
174  owl_context_deactivate(c);
175  g->context_stack = g_list_delete_link(g->context_stack,
176                                        g->context_stack);
177  owl_global_activate_context(g, owl_global_get_context(g));
178  return c;
179}
180
181/* Pops the current context from the context stack and deletes it. */
182void owl_global_pop_context(owl_global *g) {
183  owl_context *c;
184  c = owl_global_pop_context_no_delete(g);
185  if (c)
186    owl_context_delete(c);
187}
188
189int owl_global_get_lines(const owl_global *g) {
190  return(g->lines);
191}
192
193int owl_global_get_cols(const owl_global *g) {
194  return(g->cols);
195}
196
197int owl_global_get_recwin_lines(const owl_global *g) {
198  return g->mainpanel.recwinlines;
199}
200
201/* curmsg */
202
203int owl_global_get_curmsg(const owl_global *g) {
204  return(g->curmsg);
205}
206
207void owl_global_set_curmsg(owl_global *g, int i) {
208  g->curmsg=i;
209  /* we will reset the vertical offset from here */
210  /* we might want to move this out to the functions later */
211  owl_global_set_curmsg_vert_offset(g, 0);
212}
213
214/* topmsg */
215
216int owl_global_get_topmsg(const owl_global *g) {
217  return(g->topmsg);
218}
219
220void owl_global_set_topmsg(owl_global *g, int i) {
221  g->topmsg=i;
222}
223
224/* markedmsgid */
225
226int owl_global_get_markedmsgid(const owl_global *g) {
227  return(g->markedmsgid);
228}
229
230void owl_global_set_markedmsgid(owl_global *g, int i) {
231  g->markedmsgid=i;
232  /* i; index of message in the current view.
233  const owl_message *m;
234  owl_view *v;
235
236  v = owl_global_get_current_view(&g);
237  m = owl_view_get_element(v, i);
238  g->markedmsgid = m ? owl_message_get_id(m) : 0;
239  */
240}
241
242/* windows */
243
244owl_mainwin *owl_global_get_mainwin(owl_global *g)
245{
246  return g->mw;
247}
248
249owl_popwin *owl_global_get_popwin(owl_global *g) {
250  return g->pw;
251}
252
253void owl_global_set_popwin(owl_global *g, owl_popwin *pw) {
254  g->pw = pw;
255}
256
257/* msglist */
258
259owl_messagelist *owl_global_get_msglist(owl_global *g) {
260  return g->msglist;
261}
262
263/* keyhandler */
264
265owl_keyhandler *owl_global_get_keyhandler(owl_global *g) {
266  return(&(g->kh));
267}
268
269/* Gets the currently active typwin out of the current context. */
270owl_editwin *owl_global_current_typwin(const owl_global *g) {
271  owl_context *ctx = owl_global_get_context(g);
272  return owl_editcontext_get_editwin(ctx);
273}
274
275/* variable dictionary */
276
277owl_vardict *owl_global_get_vardict(owl_global *g) {
278  return &(g->vars);
279}
280
281/* command dictionary */
282
283owl_cmddict *owl_global_get_cmddict(owl_global *g) {
284  return &(g->cmds);
285}
286
287/* rightshift */
288
289void owl_global_set_rightshift(owl_global *g, int i) {
290  g->rightshift = i;
291  owl_mainwin_redisplay(owl_global_get_mainwin(g));
292}
293
294int owl_global_get_rightshift(const owl_global *g) {
295  return(g->rightshift);
296}
297
298/* typwin */
299
300owl_editwin *owl_global_set_typwin_active(owl_global *g, int style, owl_history *hist) {
301  int d;
302  d = owl_global_get_typewindelta(g);
303  if (d > 0 && style == OWL_EDITWIN_STYLE_MULTILINE)
304      owl_function_resize_typwin(owl_global_get_typwin_lines(g) + d);
305
306  if (g->typwin_erase_id) {
307    g_signal_handler_disconnect(g->mainpanel.typwin, g->typwin_erase_id);
308    g->typwin_erase_id = 0;
309  }
310
311  g->tw = owl_editwin_new(g->mainpanel.typwin,
312                          owl_global_get_typwin_lines(g),
313                          g->cols,
314                          style,
315                          hist);
316  return g->tw;
317}
318
319void owl_global_deactivate_editcontext(owl_context *ctx) {
320  owl_global *g = ctx->cbdata;
321  owl_global_set_typwin_inactive(g);
322}
323
324void owl_global_set_typwin_inactive(owl_global *g) {
325  int d = owl_global_get_typewindelta(g);
326  if (d > 0 && owl_editwin_get_style(g->tw) == OWL_EDITWIN_STYLE_MULTILINE)
327      owl_function_resize_typwin(owl_global_get_typwin_lines(g) - d);
328
329  if (!g->typwin_erase_id) {
330    g->typwin_erase_id =
331      g_signal_connect(g->mainpanel.typwin, "redraw", G_CALLBACK(owl_window_erase_cb), NULL);
332  }
333  owl_window_dirty(g->mainpanel.typwin);
334
335  owl_editwin_unref(g->tw);
336  g->tw = NULL;
337}
338
339/* resize */
340
341void owl_global_set_resize_pending(owl_global *g) {
342  g->resizepending = true;
343}
344
345const char *owl_global_get_homedir(const owl_global *g) {
346  if (g->homedir) return(g->homedir);
347  return("/");
348}
349
350const char *owl_global_get_confdir(const owl_global *g) {
351  if (g->confdir) return(g->confdir);
352  return("/");
353}
354
355/*
356 * Setting this also sets startupfile to confdir/startup
357 */
358void owl_global_set_confdir(owl_global *g, const char *cd) {
359  g_free(g->confdir);
360  g->confdir = g_strdup(cd);
361  g_free(g->startupfile);
362  g->startupfile = g_build_filename(cd, "startup", NULL);
363}
364
365const char *owl_global_get_startupfile(const owl_global *g) {
366  if(g->startupfile) return(g->startupfile);
367  return("/");
368}
369
370int owl_global_get_direction(const owl_global *g) {
371  return(g->direction);
372}
373
374void owl_global_set_direction_downwards(owl_global *g) {
375  g->direction=OWL_DIRECTION_DOWNWARDS;
376}
377
378void owl_global_set_direction_upwards(owl_global *g) {
379  g->direction=OWL_DIRECTION_UPWARDS;
380}
381
382/* perl stuff */
383
384void owl_global_set_perlinterp(owl_global *g, void *p) {
385  g->perl=p;
386}
387
388void *owl_global_get_perlinterp(const owl_global *g) {
389  return(g->perl);
390}
391
392int owl_global_is_config_format(const owl_global *g) {
393  if (g->config_format) return(1);
394  return(0);
395}
396
397void owl_global_set_config_format(owl_global *g, int state) {
398  if (state==1) {
399    g->config_format=1;
400  } else {
401    g->config_format=0;
402  }
403}
404
405void owl_global_set_have_config(owl_global *g) {
406  g->haveconfig=1;
407}
408
409void owl_global_set_no_have_config(owl_global *g) {
410  g->haveconfig=0;
411}
412
413int owl_global_have_config(owl_global *g) {
414  if (g->haveconfig) return(1);
415  return(0);
416}
417
418/*
419 * Compute the size of the terminal. Try a ioctl, fallback to other stuff on
420 * fail.
421 */
422void owl_global_get_terminal_size(int *lines, int *cols) {
423  struct winsize size;
424  /* get the new size */
425  ioctl(STDIN_FILENO, TIOCGWINSZ, &size);
426  if (size.ws_row) {
427    *lines = size.ws_row;
428  } else {
429    *lines = LINES;
430  }
431
432  if (size.ws_col) {
433    *cols = size.ws_col;
434  } else {
435    *cols = COLS;
436  }
437}
438
439void owl_global_check_resize(owl_global *g) {
440  /* resize the screen.  If lines or cols is 0 use the terminal size */
441  if (!g->resizepending) return;
442  g->resizepending = false;
443
444  owl_global_get_terminal_size(&g->lines, &g->cols);
445  owl_window_resize(owl_window_get_screen(), g->lines, g->cols);
446
447  owl_function_debugmsg("New size is %i lines, %i cols.", g->lines, g->cols);
448}
449
450/* debug */
451
452int owl_global_is_debug_fast(const owl_global *g) {
453  if (g->debug) return(1);
454  return(0);
455}
456
457/* starttime */
458
459time_t owl_global_get_starttime(const owl_global *g) {
460  return(g->starttime);
461}
462
463time_t owl_global_get_runtime(const owl_global *g) {
464  return(time(NULL)-g->starttime);
465}
466
467time_t owl_global_get_lastinputtime(const owl_global *g) {
468  return(g->lastinputtime);
469}
470
471void owl_global_set_lastinputtime(owl_global *g, time_t time) {
472  g->lastinputtime = time;
473}
474
475time_t owl_global_get_idletime(const owl_global *g) {
476  return(time(NULL)-g->lastinputtime);
477}
478
479void owl_global_wakeup(owl_global *g)
480{
481  if (time(NULL) - g->last_wakeup_time >= 1) {
482    g_free(owl_perlconfig_execute("BarnOwl::Hooks::_wakeup()"));
483    g->last_wakeup_time = time(NULL);
484  }
485}
486
487/* viewwin */
488
489owl_viewwin *owl_global_get_viewwin(owl_global *g) {
490  return g->vw;
491}
492
493void owl_global_set_viewwin(owl_global *g, owl_viewwin *vw) {
494  g->vw = vw;
495}
496
497
498/* vert offset */
499
500int owl_global_get_curmsg_vert_offset(const owl_global *g) {
501  return(g->curmsg_vert_offset);
502}
503
504void owl_global_set_curmsg_vert_offset(owl_global *g, int i) {
505  g->curmsg_vert_offset = i;
506}
507
508/* startup args */
509
510void owl_global_set_startupargs(owl_global *g, int argc, char **argv) {
511  g_free(g->startupargs);
512  g->startupargs = g_strjoinv(" ", argv);
513}
514
515const char *owl_global_get_startupargs(const owl_global *g) {
516  if (g->startupargs) return(g->startupargs);
517  return("");
518}
519
520/* history */
521
522owl_history *owl_global_get_msg_history(owl_global *g) {
523  return(&(g->msghist));
524}
525
526owl_history *owl_global_get_cmd_history(owl_global *g) {
527  return(&(g->cmdhist));
528}
529
530/* filterlist */
531typedef struct _owl_global_filter_ent {         /* noproto */
532  owl_global *g;
533  owl_filter *f;
534} owl_global_filter_ent;
535
536owl_filter *owl_global_get_filter(const owl_global *g, const char *name) {
537  owl_global_filter_ent *e = owl_dict_find_element(&(g->filters), name);
538  if (e) return e->f;
539  return NULL;
540}
541
542static void owl_global_delete_filter_ent(void *data)
543{
544  owl_global_filter_ent *e = data;
545  e->g->filterlist = g_list_remove(e->g->filterlist, e->f);
546  owl_filter_delete(e->f);
547  g_slice_free(owl_global_filter_ent, e);
548}
549
550void owl_global_add_filter(owl_global *g, owl_filter *f) {
551  owl_global_filter_ent *e = g_slice_new(owl_global_filter_ent);
552  e->g = g;
553  e->f = f;
554
555  owl_dict_insert_element(&(g->filters), owl_filter_get_name(f),
556                          e, owl_global_delete_filter_ent);
557  g->filterlist = g_list_append(g->filterlist, f);
558}
559
560void owl_global_remove_filter(owl_global *g, const char *name) {
561  owl_global_filter_ent *e = owl_dict_remove_element(&(g->filters), name);
562  if (e)
563    owl_global_delete_filter_ent(e);
564}
565
566/* nextmsgid */
567
568int owl_global_get_nextmsgid(owl_global *g) {
569  return(g->nextmsgid++);
570}
571
572/* current view */
573
574owl_view *owl_global_get_current_view(owl_global *g) {
575  return(&(g->current_view));
576}
577
578owl_colorpair_mgr *owl_global_get_colorpair_mgr(owl_global *g) {
579  return(&(g->cpmgr));
580}
581
582/* puntlist */
583
584GPtrArray *owl_global_get_puntlist(owl_global *g) {
585  return g->puntlist;
586}
587
588int owl_global_message_is_puntable(owl_global *g, const owl_message *m) {
589  const GPtrArray *pl;
590  int i;
591
592  pl = owl_global_get_puntlist(g);
593  for (i = 0; i < pl->len; i++) {
594    if (owl_filter_message_match(pl->pdata[i], m)) return 1;
595  }
596  return 0;
597}
598
599int owl_global_should_followlast(owl_global *g) {
600  const owl_view *v;
601 
602  if (!owl_global_is__followlast(g)) return(0);
603 
604  v=owl_global_get_current_view(g);
605 
606  if (owl_global_get_curmsg(g)==owl_view_get_size(v)-1) return(1);
607  return(0);
608}
609
610int owl_global_is_search_active(const owl_global *g) {
611  if (owl_regex_is_set(&g->search_re)) return(1);
612  return(0);
613}
614
615void owl_global_set_search_re(owl_global *g, const owl_regex *re) {
616  if (owl_regex_is_set(&g->search_re)) {
617    owl_regex_cleanup(&g->search_re);
618    owl_regex_init(&g->search_re);
619  }
620  if (re != NULL)
621    owl_regex_copy(re, &g->search_re);
622  /* TODO: Emit a signal so we don't depend on the viewwin and mainwin */
623  if (owl_global_get_viewwin(g))
624    owl_viewwin_dirty(owl_global_get_viewwin(g));
625  owl_mainwin_redisplay(owl_global_get_mainwin(g));
626}
627
628const owl_regex *owl_global_get_search_re(const owl_global *g) {
629  return &g->search_re;
630}
631
632void owl_global_set_newmsgproc_pid(owl_global *g, pid_t i) {
633  g->newmsgproc_pid=i;
634}
635
636pid_t owl_global_get_newmsgproc_pid(const owl_global *g) {
637  return(g->newmsgproc_pid);
638}
639
640/* message queue */
641
642void owl_global_messagequeue_addmsg(owl_global *g, owl_message *m)
643{
644  g_queue_push_tail(g->messagequeue, m);
645}
646
647/* pop off the first message and return it.  Return NULL if the queue
648 * is empty.  The caller should free the message after using it, if
649 * necessary.
650 */
651CALLER_OWN owl_message *owl_global_messagequeue_popmsg(owl_global *g)
652{
653  owl_message *out;
654
655  if (g_queue_is_empty(g->messagequeue))
656    return NULL;
657  out = g_queue_pop_head(g->messagequeue);
658  return out;
659}
660
661int owl_global_messagequeue_pending(owl_global *g)
662{
663  return !g_queue_is_empty(g->messagequeue);
664}
665
666/* style */
667
668/* Return the style with name 'name'.  If it does not exist return
669 * NULL */
670const owl_style *owl_global_get_style_by_name(const owl_global *g, const char *name)
671{
672  return owl_dict_find_element(&(g->styledict), name);
673}
674
675CALLER_OWN GPtrArray *owl_global_get_style_names(const owl_global *g)
676{
677  return owl_dict_get_keys(&g->styledict);
678}
679
680void owl_global_add_style(owl_global *g, owl_style *s)
681{
682  /*
683   * If we're redefining the current style, make sure to update
684   * pointers to it.
685   */
686  if(g->current_view.style
687     && !strcmp(owl_style_get_name(g->current_view.style),
688                owl_style_get_name(s)))
689    g->current_view.style = s;
690  owl_dict_insert_element(&(g->styledict), owl_style_get_name(s),
691                          s, (void (*)(void *))owl_style_delete);
692}
693
694void owl_global_set_havezephyr(owl_global *g)
695{
696  g->havezephyr=1;
697}
698
699int owl_global_is_havezephyr(const owl_global *g)
700{
701  if (g->havezephyr) return(1);
702  return(0);
703}
704
705owl_errqueue *owl_global_get_errqueue(owl_global *g)
706{
707  return(&(g->errqueue));
708}
709
710owl_zbuddylist *owl_global_get_zephyr_buddylist(owl_global *g)
711{
712  return(&(g->zbuddies));
713}
714
715GList **owl_global_get_zaldlist(owl_global *g)
716{
717  return &(g->zaldlist);
718}
719
720int owl_global_get_pseudologin_notify(owl_global *g)
721{
722  return g->pseudologin_notify;
723}
724
725void owl_global_set_pseudologin_notify(owl_global *g, int notify)
726{
727  g->pseudologin_notify = notify;
728}
729
730struct termios *owl_global_get_startup_tio(owl_global *g)
731{
732  return(&(g->startup_tio));
733}
734
735void owl_global_setup_default_filters(owl_global *g)
736{
737  int i;
738  static const struct {
739    const char *name;
740    const char *desc;
741  } filters[] = {
742    { "personal",
743      "isprivate ^true$ and ( not type ^zephyr$ or ( class ^message$ ) )" },
744    { "trash",
745      "class ^mail$ or opcode ^ping$ or type ^admin$ or ( not login ^none$ )" },
746    { "wordwrap", "not ( type ^admin$ or type ^zephyr$ )" },
747    { "ping", "opcode ^ping$" },
748    { "auto", "opcode ^auto$" },
749    { "login", "not login ^none$" },
750    { "reply-lockout", "class ^mail$ or class ^filsrv$" },
751    { "out", "direction ^out$" },
752    { "zephyr", "type ^zephyr$" },
753    { "none", "false" },
754    { "all", "true" },
755    { NULL, NULL }
756  };
757
758  owl_function_debugmsg("startup: creating default filters");
759
760  for (i = 0; filters[i].name != NULL; i++)
761    owl_global_add_filter(g, owl_filter_new_fromstring(filters[i].name,
762                                                       filters[i].desc));
763}
764
765FILE *owl_global_get_debug_file_handle(owl_global *g) {
766  static char *open_file = NULL;
767  const char *filename = owl_global_get_debug_file(g);
768  if (g->debug_file == NULL ||
769      (open_file && strcmp(filename, open_file) != 0)) {
770    char *path;
771    int fd;
772
773    if (g->debug_file)
774      fclose(g->debug_file);
775
776    g->debug_file = NULL;
777
778    path = g_strdup_printf("%s.%d", filename, getpid());
779    fd = open(path, O_CREAT|O_WRONLY|O_EXCL, 0600);
780    g_free(path);
781
782    if (fd >= 0)
783      g->debug_file = fdopen(fd, "a");
784
785    g_free(open_file);
786    open_file = g_strdup(filename);
787  }
788  return g->debug_file;
789}
790
791const char *owl_global_get_kill_buffer(owl_global *g) {
792  return g->kill_buffer;
793}
794
795void owl_global_set_kill_buffer(owl_global *g, const char *kill, int len) {
796  g_free(g->kill_buffer);
797  g->kill_buffer = g_strndup(kill, len);
798}
799
800static GMutex *owl_global_get_interrupt_lock(owl_global *g)
801{
802#if GLIB_CHECK_VERSION(2, 31, 0)
803  return &g->interrupt_lock;
804#else
805  return g->interrupt_lock;
806#endif
807}
808
809void owl_global_add_interrupt(owl_global *g) {
810  /* TODO: This can almost certainly be done with atomic
811   * operations. Whatever. */
812  g_mutex_lock(owl_global_get_interrupt_lock(g));
813  g->interrupt_count++;
814  g_mutex_unlock(owl_global_get_interrupt_lock(g));
815}
816
817bool owl_global_take_interrupt(owl_global *g) {
818  bool ans = false;
819  g_mutex_lock(owl_global_get_interrupt_lock(g));
820  if (g->interrupt_count > 0) {
821    ans = true;
822    g->interrupt_count--;
823  }
824  g_mutex_unlock(owl_global_get_interrupt_lock(g));
825  return ans;
826}
Note: See TracBrowser for help on using the repository browser.