about summary refs log tree commit diff stats
path: root/304screen.subx
blob: 8a8df8f9a3ea3ad83a30fb0e92c965358c29514d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
# Primitives for screen control.
# Require Linux and a modern terminal.

== code

enable-screen-grid-mode:
    # . prologue
    55/push-ebp
    89/<- %ebp 4/r32/esp
    #
    (flush Stdout)
    (flush Stderr)
    # switch to second screen buffer
    (write 1 Esc)
    (write 1 "[?1049h")
    #
    (clear-real-screen)
$enable-screen-grid-mode:end:
    # . epilogue
    89/<- %esp 5/r32/ebp
    5d/pop-to-ebp
    c3/return

enable-screen-type-mode:
    # . prologue
    55/push-ebp
    89/<- %ebp 4/r32/esp
    # switch to first screen buffer
    (write 1 Esc)
    (write 1 "[?1049l")
$enable-screen-type-mode:end:
    # . epilogue
    89/<- %esp 5/r32/ebp
    5d/pop-to-ebp
    c3/return

real-screen-size:  # -> nrows/eax: int, ncols/ecx: int
    # . prologue
    55/push-ebp
    89/<- %ebp 4/r32/esp
    # . save registers
    52/push-edx
    53/push-ebx
    56/push-esi
    57/push-edi
    #
    (_maybe-open-terminal)
    # var window-size-info/esi: (addr winsize)
    # winsize is a type from the Linux kernel. We don't care how large it is.
    81 5/subop/subtract %esp 0x40/imm32
    89/<- %esi 4/r32/esp
    # ioctl(*Terminal-file-descriptor, TIOCGWINSZ, window-size-info)
    89/<- %edx 6/r32/esi
    b9/copy-to-ecx 0x5413/imm32/TIOCGWINSZ
    8b/-> *Terminal-file-descriptor 3/r32/ebx
    e8/call syscall_ioctl/disp32
    # some bitworking to extract 2 16-bit shorts
    8b/-> *esi 0/r32/eax
    81 4/subop/and %eax 0xffff/imm32
    8b/-> *esi 1/r32/ecx
    c1/shift 5/subop/logical-right %ecx 0x10/imm8
$real-screen-size:end:
    # . reclaim locals
    81 0/subop/add %esp 0x40/imm32
    # . restore registers
    5f/pop-to-edi
    5e/pop-to-esi
    5b/pop-to-ebx
    5a/pop-to-edx
    # . epilogue
    89/<- %esp 5/r32/ebp
    5d/pop-to-ebp
    c3/return

clear-real-screen:
    # . prologue
    55/push-ebp
    89/<- %ebp 4/r32/esp
    #
    (write 1 Esc)
    (write 1 "[H")
    (write 1 Esc)
    (write 1 "[2J")
$clear-real-screen:end:
    # . epilogue
    89/<- %esp 5/r32/ebp
    5d/pop-to-ebp
    c3/return

# row and col count from the top-left as (1, 1)
move-cursor-on-real-screen:  # row: int, column: int
    # . prologue
    55/push-ebp
    89/<- %ebp 4/r32/esp
    # . save registers
    51/push-ecx
    # var buf/ecx: (stream byte 32)
    81 5/subop/subtract %esp 0x20/imm32
    68/push 0x20/imm32/size
    68/push 0/imm32/read
    68/push 0/imm32/write
    89/<- %ecx 4/r32/esp
    # construct directive in buf
    (write %ecx Esc)
    (write %ecx "[")
    (write-int32-decimal %ecx *(ebp+8))
    (write %ecx ";")
    (write-int32-decimal %ecx *(ebp+0xc))
    (write %ecx "H")
    # flush
    (write-stream 2 %ecx)
$move-cursor-on-real-screen:end:
    # . reclaim locals
    81 0/subop/add %esp 0x2c/imm32
    # . restore registers
    59/pop-to-ecx
    # . epilogue
    89/<- %esp 5/r32/ebp
    5d/pop-to-ebp
    c3/return

print-string-to-real-screen:  # s: (addr array byte)
    # . prologue
    55/push-ebp
    89/<- %ebp 4/r32/esp
    #
    (write 1 *(ebp+8))
$print-string-to-real-screen:end:
    # . epilogue
    89/<- %esp 5/r32/ebp
    5d/pop-to-ebp
    c3/return

print-slice-to-real-screen:  # s: (addr slice)
    # . prologue
    55/push-ebp
    89/<- %ebp 4/r32/esp
    #
    (write-slice-buffered Stdout *(ebp+8))
    (flush Stdout)
$print-slice-to-real-screen:end:
    # . epilogue
    89/<- %esp 5/r32/ebp
    5d/pop-to-ebp
    c3/return

print-stream-to-real-screen:  # s: (addr stream byte)
    # . prologue
    55/push-ebp
    89/<- %ebp 4/r32/esp
    #
    (write-stream-data Stdout *(ebp+8))
    (flush Stdout)
$print-stream-to-real-screen:end:
    # . epilogue
    89/<- %esp 5/r32/ebp
    5d/pop-to-ebp
    c3/return

# print a grapheme in utf-8 (only up to 4 bytes so far)
print-grapheme-to-real-screen:  # c: grapheme
    # . prologue
    55/push-ebp
    89/<- %ebp 4/r32/esp
    # . save registers
    50/push-eax
    # var curr/eax: byte = 0
    b8/copy-to-eax 0/imm32
    # curr = *(ebp+8)
    8a/byte-> *(ebp+8) 0/r32/al
    # if (curr == 0) return
    3d/compare-eax-and 0/imm32
    74/jump-if-= $print-grapheme-to-real-screen:end/disp8
    #
    (print-byte-to-real-screen %eax)
    # curr = *(ebp+9)
    8a/byte-> *(ebp+9) 0/r32/al
    # if (curr == 0) return
    3d/compare-eax-and 0/imm32
    74/jump-if-= $print-grapheme-to-real-screen:end/disp8
    #
    (print-byte-to-real-screen %eax)
    # curr = *(ebp+10)
    8a/byte-> *(ebp+0xa) 0/r32/al
    # if (curr == 0) return
    3d/compare-eax-and 0/imm32
    74/jump-if-= $print-grapheme-to-real-screen:end/disp8
    #
    (print-byte-to-real-screen %eax)
    # curr = *(ebp+11)
    8a/byte-> *(ebp+0xb) 0/r32/al
    # if (curr == 0) return
    3d/compare-eax-and 0/imm32
    74/jump-if-= $print-grapheme-to-real-screen:end/disp8
    #
    (print-byte-to-real-screen %eax)
$print-grapheme-to-real-screen:end:
    # . restore registers
    58/pop-to-eax
    # . epilogue
    89/<- %esp 5/r32/ebp
    5d/pop-to-ebp
    c3/return

print-byte-to-real-screen:  # c: byte
    # . prologue
    55/push-ebp
    89/<- %ebp 4/r32/esp
    # . save registers
    51/push-ecx
    # var s/ecx: (addr array byte)
    ff 6/subop/push *(ebp+8)
    68/push 1/imm32/size
    89/<- %ecx 4/r32/esp
    (write 1 %ecx)
$print-byte-to-real-screen:end:
    # . reclaim locals
    81 0/subop/add %esp 8/imm32
    # . restore registers
    59/pop-to-ecx
    # . epilogue
    89/<- %esp 5/r32/ebp
    5d/pop-to-ebp
    c3/return

print-int32-hex-to-real-screen:  # n: int
    # . prologue
    55/push-ebp
    89/<- %ebp 4/r32/esp
    #
    (write-int32-hex-buffered Stdout *(ebp+8))
    (flush Stdout)
$print-int32-hex-to-real-screen:end:
    # . epilogue
    89/<- %esp 5/r32/ebp
    5d/pop-to-ebp
    c3/return

print-int32-hex-bits-to-real-screen:  # n: int, bits: int
    # . prologue
    55/push-ebp
    89/<- %ebp 4/r32/esp
    #
    (write-int32-hex-bits-buffered Stdout *(ebp+8) *(ebp+0xc) *(ebp+0x10))
    (flush Stdout)
$print-int32-hex-bits-to-real-screen:end:
    # . epilogue
    89/<- %esp 5/r32/ebp
    5d/pop-to-ebp
    c3/return

print-int32-decimal-to-real-screen:  # n: int
    # . prologue
    55/push-ebp
    89/<- %ebp 4/r32/esp
    #
    (write-int32-decimal-buffered Stdout *(ebp+8))
    (flush Stdout)
$print-int32-decimal-to-real-screen:end:
    # . epilogue
    89/<- %esp 5/r32/ebp
    5d/pop-to-ebp
    c3/return

write-int32-decimal-buffered:  # f: (addr buffered-file), n: int
    # . prologue
    55/push-ebp
    89/<- %ebp 4/r32/esp
    # . save registers
    51/push-ecx
    # var ecx: (stream byte 16)
    81 5/subop/subtract %esp 0x10/imm32
    68/push 0x10/imm32/size
    68/push 0/imm32/read
    68/push 0/imm32/write
    89/<- %ecx 4/r32/esp
    (write-int32-decimal %ecx *(ebp+0xc))
    (write-stream-data *(ebp+8) %ecx)
$write-int32-decimal-buffered:end:
    # . reclaim locals
    81 0/subop/add %esp 0x1c/imm32
    # . restore registers
    59/pop-to-ecx
    # . epilogue
    89/<- %esp 5/r32/ebp
    5d/pop-to-ebp
    c3/return

reset-formatting-on-real-screen:
    # . prologue
    55/push-ebp
    89/<- %ebp 4/r32/esp
    #
    (write 1 Esc)
    (write 1 "(B")
    (write 1 Esc)
    (write 1 "[m")
$reset-formatting-on-real-screen:end:
    # . epilogue
    89/<- %esp 5/r32/ebp
    5d/pop-to-ebp
    c3/return

start-color-on-real-screen:  # fg: int, bg: int
    # . prologue
    55/push-ebp
    89/<- %ebp 4/r32/esp
    # . save registers
    51/push-ecx
    # var buf/ecx: (stream byte 32)
    81 5/subop/subtract %esp 0x20/imm32
    68/push 0x20/imm32/size
    68/push 0/imm32/read
    68/push 0/imm32/write
    89/<- %ecx 4/r32/esp
    # construct directive in buf
    # . set fg
    (write %ecx Esc)
    (write %ecx "[38;5;")
    (write-int32-decimal %ecx *(ebp+8))
    (write %ecx "m")
    # . set bg
    (write %ecx Esc)
    (write %ecx "[48;5;")
    (write-int32-decimal %ecx *(ebp+0xc))
    (write %ecx "m")
    # flush
    (write-stream 2 %ecx)
$start-color-on-real-screen:end:
    # . reclaim locals
    81 0/subop/add %esp 0x2c/imm32
    # . restore registers
    59/pop-to-ecx
    # . epilogue
    89/<- %esp 5/r32/ebp
    5d/pop-to-ebp
    c3/return

start-bold-on-real-screen:
    # . prologue
    55/push-ebp
    89/<- %ebp 4/r32/esp
    #
    (write 1 Esc)
    (write 1 "[1m")
$start-bold-on-real-screen:end:
    # . epilogue
    89/<- %esp 5/r32/ebp
    5d/pop-to-ebp
    c3/return

start-underline-on-real-screen:
    # . prologue
    55/push-ebp
    89/<- %ebp 4/r32/esp
    #
    (write 1 Esc)
    (write 1 "[4m")
$start-underline-on-real-screen:end:
    # . epilogue
    89/<- %esp 5/r32/ebp
    5d/pop-to-ebp
    c3/return

start-reverse-video-on-real-screen:
    # . prologue
    55/push-ebp
    89/<- %ebp 4/r32/esp
    #
    (write 1 Esc)
    (write 1 "[7m")
$start-reverse-video-on-real-screen:end:
    # . epilogue
    89/<- %esp 5/r32/ebp
    5d/pop-to-ebp
    c3/return

# might require enabling blinking in your terminal program
start-blinking-on-real-screen:
    # . prologue
    55/push-ebp
    89/<- %ebp 4/r32/esp
    #
    (write 1 Esc)
    (write 1 "[5m")
$start-blinking-on-real-screen:end:
    # . epilogue
    89/<- %esp 5/r32/ebp
    5d/pop-to-ebp
    c3/return

hide-cursor-on-real-screen:
    # . prologue
    55/push-ebp
    89/<- %ebp 4/r32/esp
    #
    (write 1 Esc)
    (write 1 "[?25l")
$hide-cursor-on-real-screen:end:
    # . epilogue
    89/<- %esp 5/r32/ebp
    5d/pop-to-ebp
    c3/return

show-cursor-on-real-screen:
    # . prologue
    55/push-ebp
    89/<- %ebp 4/r32/esp
    #
    (write 1 Esc)
    (write 1 "[?12l")
    (write 1 Esc)
    (write 1 "[?25h")
$show-cursor-on-real-screen:end:
    # . epilogue
    89/<- %esp 5/r32/ebp
    5d/pop-to-ebp
    c3/return

# This is a low-level detail; I don't think everything should be a file.
#
# Open "/dev/tty" if necessary and cache its file descriptor in Terminal-file-descriptor
# where later primitives can use it.
_maybe-open-terminal:
    81 7/subop/compare *Terminal-file-descriptor -1/imm32
    75/jump-if-!= $_maybe-open-terminal:epilogue/disp8
    # . save registers
    50/push-eax
    51/push-ecx
    53/push-ebx
    # open("/dev/tty", O_RDWR)
    bb/copy-to-ebx Terminal-filename/imm32
    b9/copy-to-ecx 2/imm32/O_RDWR
    e8/call syscall_open/disp32
    89/<- *Terminal-file-descriptor 0/r32/eax
$_maybe-open-terminal:end:
    # . restore registers
    5b/pop-to-ebx
    59/pop-to-ecx
    58/pop-to-eax
$_maybe-open-terminal:epilogue:
    c3/return

== data

Terminal-file-descriptor:  # (addr int)
  -1/imm32

Esc:  # (addr array byte)
  # size
  1/imm32
  # data
  0x1b

Terminal-filename:  # (addr kernel-string)
  # "/dev/tty"
  2f/slash 64/d 65/e 76/v 2f/slash 74/t 74/t 79/y 0/nul
  # on Linux console
#?   # "/dev/console"
#?   2f/slash 64/d 65/e 76/v 2f/slash 63/c 6f/o 6e/n 73/s 6f/o 6c/l 65/e 0/nul
_SEARCH_LIBS for otr' href='/danisanti/profani-tty/commit/configure.ac?id=50afe7366e00767208fc9d17167a768cf924d6cb'>50afe736 ^
fa89e2aa ^











4d00f788 ^
d2035496 ^

3ceb9b0d ^








2519e450 ^

dcccfacb ^

28f5cdf7 ^
2519e450 ^
d28930ea ^
8e0d8d2a ^
11066f20 ^
2519e450 ^
4cb302ab ^
dcccfacb ^

575b6acd ^
aa6e2284 ^
575b6acd ^
3c062438 ^



2519e450 ^
8014e902 ^
2655d9e8 ^


dcccfacb ^

b5171cb0 ^
dcccfacb ^



3cae0208 ^
3ceb9b0d ^

dcccfacb ^

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243


                                                                
                                                     

                                 
                              
                                          
 
                        

          
                      
                 





                                  
                            










                                                                                 
                                    
                                                  
 
           

                                                                               

                                                              

                                                                             
                           
                                                                                        

                                                                               
 


































                                                                                         

                             

                                                                               
                        

                             
                                         
                                                                                           
                                            
                                                                                            
                                             
                                                
                                                                          

                                          
                                                           

                                                    













                                                                       
 

                                                               
 




                                                                    
 
                                                        





















                                                                                                       

                    

                                          
                                                                    

                                                                    


                                                                                 
                                   
                                                                                 
  
 
                                    
                                     
                                     
                                   
                                      











                                                                                                                                      

                                                                        
                                      











                                                                                                                                      
                                                                                

  








                                                                                    

                                                                            

                                                                          
 
                                                                              
                                              
                                     
 
                      
                                              

                                               
                                                                      
                                                                 
                                                  



                     
                                 
                                             


                           

       
                                



                                       
                               

                                       

                                                 
#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_INIT([profanity], [0.4.5], [boothj5web@gmail.com])
AC_CONFIG_SRCDIR([src/main.c])
AC_CONFIG_HEADERS([src/config.h])
AC_CONFIG_AUX_DIR([build-aux])
AM_INIT_AUTOMAKE([foreign subdir-objects])

### Checks for programs.
AC_PROG_CC

### Get canonical host
AC_CANONICAL_HOST
PLATFORM="unknown"
AS_CASE([$host_os],
    [darwin*], [PLATFORM="osx"],
    [cygwin], [PLATFORM="cygwin"],
    [PLATFORM="nix"])

PACKAGE_STATUS="development"

### Get git branch and revision if in development
if test "x$PACKAGE_STATUS" = xdevelopment; then
    AM_CONDITIONAL([INCLUDE_GIT_VERSION], [true])
    AC_DEFINE([HAVE_GIT_VERSION], [1], [Include git info])
else
    AM_CONDITIONAL([INCLUDE_GIT_VERSION], [false])
fi

AC_DEFINE_UNQUOTED([PACKAGE_STATUS], ["$PACKAGE_STATUS"], [Status of this build])

AS_IF([test "x$PLATFORM" = xcygwin],
    [AC_DEFINE([PLATFORM_CYGWIN], [1], [Cygwin])])

### Options
AC_ARG_ENABLE([notifications],
    [AS_HELP_STRING([--enable-notifications], [enable desktop notifications])])
AC_ARG_ENABLE([otr],
    [AS_HELP_STRING([--enable-otr], [enable otr encryption])])
AC_ARG_WITH([libxml2],
    [AS_HELP_STRING([--with-libxml2], [link with libxml2 instead of expat])])
AC_ARG_WITH([xscreensaver],
    [AS_HELP_STRING([--with-xscreensaver], [use libXScrnSaver to determine idle time])])
AC_ARG_WITH([themes],
    [AS_HELP_STRING([--with-themes[[=PATH]]], [install themes (default yes)])])

### Select first existing xml library among expat and libxml2
PARSER=""
PARSER_LIBS=""
PARSER_CFLAGS=""
AS_IF([test "x$with_libxml2" != xyes],
    [PKG_CHECK_MODULES([expat], [expat],
        [PARSER_CFLAGS="$expat_CFLAGS"; PARSER_LIBS="$expat_LIBS"; PARSER="expat"],
        [AC_CHECK_LIB([expat], [XML_ParserCreate],
            [PARSER_LIBS="-lexpat"; PARSER="expat"],
            AS_IF([test "x$with_libxml2" = xno],
                [AC_MSG_ERROR([expat is required but does not exist])]))])
    ])

AS_IF([test "x$PARSER" = x -a "x$with_libxml2" != xno],
    [PKG_CHECK_MODULES([libxml2], [libxml-2.0],
        [PARSER_CFLAGS="$libxml2_CFLAGS"; PARSER_LIBS="$libxml2_LIBS"; PARSER="libxml2"],
        AS_IF([test "x$with_libxml2" = xyes],
            [AC_MSG_ERROR([libxml2 is required but does not exist])]))
    ])

AS_IF([test "x$PARSER" = x],
    [AC_MSG_ERROR([either expat or libxml2 is required for profanity])])
AM_CPPFLAGS="$AM_CPPFLAGS $PARSER_CFLAGS"
LIBS="$LIBS $PARSER_LIBS"

### Check for libstrophe dependencies
AC_CHECK_LIB([resolv], [res_query], [],
    [AC_CHECK_LIB([resolv], [__res_query], [],
        [AC_MSG_ERROR([libresolv is required for profanity])])])
PKG_CHECK_MODULES([openssl], [openssl], [],
    [AC_MSG_ERROR([openssl is required for profanity])])
AM_CPPFLAGS="$AM_CPPFLAGS $openssl_CFLAGS"
LIBS="$LIBS $openssl_LIBS"

# TODO: autodetect of XML parser libstrophe linked with
CFLAGS_RESTORE="$CFLAGS"
CFLAGS="$CFLAGS $AM_CPPFLAGS"
AC_CHECK_LIB([strophe], [parser_new], [],
    [AC_MSG_ERROR([libstrophe linked with $PARSER is required for profanity])])
CFLAGS="$CFLAGS_RESTORE"

### Check for ncurses library
PKG_CHECK_MODULES([ncursesw], [ncursesw],
    [NCURSES_CFLAGS="$ncursesw_CFLAGS"; NCURSES_LIBS="$ncursesw_LIBS"; NCURSES="ncursesw"],
    [PKG_CHECK_MODULES([ncurses], [ncurses],
        [NCURSES_CFLAGS="$ncurses_CFLAGS"; NCURSES_LIBS="$ncurses_LIBS"; NCURSES="ncurses"],
        [AC_CHECK_LIB([ncursesw], [main], [],
            [AC_CHECK_LIB([ncurses], [main], [],
            	[AC_MSG_ERROR([ncurses is required for profanity])])])])])
AM_CPPFLAGS="$AM_CPPFLAGS $NCURSES_CFLAGS"
LIBS="$LIBS $NCURSES_LIBS"
AS_IF([test "x$PLATFORM" = xosx], [LIBS="$LIBS -lncurses"])

### Check wide characters support in ncurses library
CFLAGS_RESTORE="$CFLAGS"
CFLAGS="$CFLAGS $NCURSES_CFLAGS"
AC_CACHE_CHECK([for wget_wch support in $NCURSES], ncurses_cv_wget_wch,
   [AC_LINK_IFELSE([AC_LANG_SOURCE([
       #include <ncurses.h>
       int main() {
           (void)wget_wch(NULL, NULL);
           return 0;
       }
       ])],
       [ncurses_cv_wget_wch=yes],
       [ncurses_cv_wget_wch=no])
   ])
CFLAGS="$CFLAGS_RESTORE"

AS_IF([test "x$ncurses_cv_wget_wch" != xyes],
    [AC_MSG_ERROR([ncurses does not support wide characters])])

### Check for other profanity dependencies
PKG_CHECK_MODULES([glib], [glib-2.0 >= 2.26], [],
    [AC_MSG_ERROR([glib 2.26 or higher is required for profanity])])
PKG_CHECK_MODULES([curl], [libcurl], [],
    [AC_MSG_ERROR([libcurl is required for profanity])])

AS_IF([test "x$PLATFORM" = xosx], [LIBS="$LIBS -lcurl"])

### Check for desktop notification support
### Linux requires libnotify
### Windows uses native OS calls
### OSX requires terminal-notifier

AS_IF([test "x$PLATFORM" = xosx],
        [AS_IF([test "x$enable_notifications" != xno],
            [NOTIFIER_PATH="no"
            AC_PATH_PROG(NOTIFIER_PATH, terminal-notifier, no)
            AS_IF([test "x$NOTIFIER_PATH" = xno],
                [AS_IF([test "x$enable_notifications" = xyes],
                    [AC_MSG_ERROR([terminal-notifier not found, required for desktop notifications.])],
                    [AC_MSG_NOTICE([Desktop notifications not supported.])])],
                [AC_DEFINE([HAVE_OSXNOTIFY], [1], [terminal notifier])])])],
    [test "x$PLATFORM" = xnix],
        [AS_IF([test "x$enable_notifications" != xno],
            [PKG_CHECK_MODULES([libnotify], [libnotify],
                [AC_DEFINE([HAVE_LIBNOTIFY], [1], [libnotify module])],
                [AS_IF([test "x$enable_notifications" = xyes],
                    [AC_MSG_ERROR([libnotify is required but does not exist])],
                    [AC_MSG_NOTICE([libnotify support will be disabled])])])])])

# TODO: rewrite this
if test "x$with_xscreensaver" = xyes; then
    AC_CHECK_LIB([Xss], [main], [],
        [AC_MSG_ERROR([libXss is required for x autoaway support])])
    AC_CHECK_LIB([X11], [main], [],
        [AC_MSG_ERROR([libX11 is required for x autoaway support])])
elif test "x$with_xscreensaver" = x; then
    AC_CHECK_LIB([Xss], [main], [],
        [AC_MSG_NOTICE([libXss not found, falling back to profanity auto-away])])
    AC_CHECK_LIB([X11], [main], [],
        [AC_MSG_NOTICE([libX11 not found, falling back to profanity auto-away])])
fi

AM_CONDITIONAL([BUILD_OTR], [false])
AM_CONDITIONAL([BUILD_OTR3], [false])
AM_CONDITIONAL([BUILD_OTR4], [false])
if test "x$enable_otr" = xyes; then
    AC_SEARCH_LIBS([otrl_init], [otr],
        [AC_COMPILE_IFELSE(
            [AC_LANG_PROGRAM([[
            #include <libotr/version.h>
            ]],[[
            #if OTRL_VERSION_MAJOR == 4
            // OK
            #else
            # assume version 3
            #endif
            ]])],
            [AM_CONDITIONAL([BUILD_OTR], [true]) AM_CONDITIONAL([BUILD_OTR4], [true]) AC_DEFINE([HAVE_LIBOTR], [1], [Have libotr])],
            [AM_CONDITIONAL([BUILD_OTR], [true]) AM_CONDITIONAL([BUILD_OTR3], [true]) AC_DEFINE([HAVE_LIBOTR], [1], [Have libotr])])],
        [AC_MSG_ERROR([libotr is required for otr encryption support])])
elif test "x$enable_otr" = x; then
    AC_SEARCH_LIBS([otrl_init], [otr],
        [AC_COMPILE_IFELSE(
            [AC_LANG_PROGRAM([[
            #include <libotr/version.h>
            ]],[[
            #if OTRL_VERSION_MAJOR == 4
            // OK
            #else
            # assume version 3
            #endif
            ]])],
            [AM_CONDITIONAL([BUILD_OTR], [true]) AM_CONDITIONAL([BUILD_OTR4], [true]) AC_DEFINE([HAVE_LIBOTR], [1], [Have libotr])],
            [AM_CONDITIONAL([BUILD_OTR], [true]) AM_CONDITIONAL([BUILD_OTR3], [true]) AC_DEFINE([HAVE_LIBOTR], [1], [Have libotr])])],
        [AC_MSG_NOTICE([libotr not found, otr encryption support not enabled])])
fi

AS_IF([test "x$with_themes" = xno],
    [THEMES_INSTALL="false"],
    [THEMES_INSTALL="true"])
AS_IF([test "x$with_themes" = xno -o "x$with_themes" = xyes -o "x$with_themes" = x],
    [THEMES_PATH='${pkgdatadir}/themes'],
    [THEMES_PATH="$with_themes"])
AC_SUBST(THEMES_PATH)
AM_CONDITIONAL([THEMES_INSTALL], "$THEMES_INSTALL")

### cmocka is required only for tests, profanity shouldn't be linked with it
### TODO: pass cmocka_CFLAGS and cmocka_LIBS to Makefile.am
PKG_CHECK_MODULES([cmocka], [cmocka], [],
    [AC_MSG_NOTICE([cmocka is not found, will not be able to run tests])])

### Check for ncursesw/ncurses.h first, Arch linux uses ncurses.h for ncursesw
AC_CHECK_HEADERS([ncursesw/ncurses.h], [], [])
AC_CHECK_HEADERS([ncurses.h], [], [])

### Default parameters
AM_CFLAGS="-Wall -Wno-deprecated-declarations"
AS_IF([test "x$PACKAGE_STATUS" = xdevelopment],
    [AM_CFLAGS="$AM_CFLAGS -Wunused -Werror"])
AM_CPPFLAGS="$AM_CPPFLAGS $glib_CFLAGS $curl_CFLAGS $libnotify_CFLAGS"
AM_CPPFLAGS="$AM_CPPFLAGS -DTHEMES_PATH=\"\\\"$THEMES_PATH\\\"\""
LIBS="$LIBS $glib_LIBS $curl_LIBS $libnotify_LIBS"

AC_SUBST(AM_CFLAGS)
AC_SUBST(AM_CPPFLAGS)

### Checks for library functions.
AC_CHECK_FUNCS([atexit memset strdup strstr])

AC_CONFIG_FILES([Makefile])
AC_OUTPUT

echo ""
echo "PLATFORM       : $host_os"
echo "PACKAGE_STATUS : $PACKAGE_STATUS"
echo "AM_CFLAGS      : $AM_CFLAGS"
echo "AM_CPPFLAGS    : $AM_CPPFLAGS"
echo "LIBS           : $LIBS"
echo "XML Parser     : $PARSER"
echo "Install themes : $THEMES_INSTALL"
echo "Themes path    : $THEMES_PATH"
echo ""
echo "Now you can run \`make' to build profanity"