My build of suckless st terminal
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4524 lines
97 KiB

Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
10 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
10 years ago
12 years ago
12 years ago
14 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
10 years ago
11 years ago
10 years ago
14 years ago
10 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
11 years ago
11 years ago
14 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
10 years ago
14 years ago
11 years ago
14 years ago
14 years ago
11 years ago
14 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
10 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
10 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
10 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
10 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
10 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
10 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
10 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
10 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
10 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
10 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
10 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
10 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
10 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
10 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
10 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
10 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
10 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
10 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
10 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
10 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
10 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
10 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
10 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
10 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
10 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
10 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
10 years ago
11 years ago
14 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
10 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
10 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
10 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
10 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
10 years ago
Clean up xdraws and optimize glyph drawing with non-unit kerning values I have another patch here for review that optimizes the performance of glyph drawing, primarily when using non-unit kerning values, and fixes a few other minor issues. It's dependent on the earlier patch from me that stores unicode codepoints in a Rune type, typedef'd to uint_least32_t. This patch is a pretty big change to xdraws so your scrutiny is appreciated. First, some performance numbers. I used Yu-Jie Lin termfps.sh shell script to benchmark before and after, and you can find it in the attachments. On my Kaveri A10 7850k machine, I get the following results: Before Patch ============ 1) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.553 Frames/second: 64.352 Chars /second: 1,458,159 2) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 159.286 Frames/second: 0.627 Chars /second: 10,953 After Patch =========== 3) Font: "Liberation Mono:pixelsize=12:antialias=false:autohint=false" cwscale: 1.0, chscale: 1.0 For 273x83 100 frames. Elapsed time : 1.544 Frames/second: 64.728 Chars /second: 1,466,690 4) Font: "Inconsolata:pixelsize=14:antialias=true:autohint=true" cwscale: 1.001, chscale: 1.001 For 239x73 100 frames. Elapsed time : 1.955 Frames/second: 51.146 Chars /second: 892,361 As you can see, while the improvements for fonts with unit-kerning is marginal, there's a huge ~81x performance increase with the patch when using kerning values other than 1.0. So what does the patch do? The `xdraws' function would render each glyph one at a time if non-unit kerning values were configured, and this was the primary cause of the slow down. Xft provides a handful of functions which allow you to render multiple characters or glyphs at time, each with a unique <x,y> position, so it was simply a matter of massaging the data into a format that would allow us to use one of these functions. I've split `xdraws' up into two functions. In the first pass with `xmakeglyphfontspecs' it will iterate over all of the glyphs in a given row and it will build up an array of corresponding XftGlyphFontSpec records. Much of the old logic for resolving fonts for glyphs using Xft and fontconfig went into this function. The second pass is done with `xrenderglyphfontspecs' which contains the old logic for determining colors, clearing the background, and finally rendering the array of XftGlyphFontSpec records. There's a couple of other things that have been improved by this patch. For instance, the UTF-32 codepoints in the Line's were being re-encoded back into UTF-8 strings to be passed to `xdraws' which in turn would then decode back to UTF-32 to verify that the Font contained a matching glyph for the code point. Next, the UTF-8 string was being passed to `XftDrawStringUtf8' which internally mallocs a scratch buffer and decodes back to UTF-32 and does the lookup of the glyphs all over again. This patch gets rid of all of this redundant round-trip encoding and decoding of characters to be rendered and only looks up the glyph index once (per font) during the font resolution phase. So this is probably what's responsible for the marginal improvements seen when kerning values are kept to 1.0. I imagine there are other performance improvements here too, not seen in the above benchmarks, if the user has lots of non-ASCII code plane characters on the screen, or several different fonts are being utilized during screen redraw. Anyway, if you see any problems, please let me know and I can fix them.
10 years ago
14 years ago
14 years ago
10 years ago
  1. /* See LICENSE for license details. */
  2. #include <ctype.h>
  3. #include <errno.h>
  4. #include <fcntl.h>
  5. #include <limits.h>
  6. #include <locale.h>
  7. #include <pwd.h>
  8. #include <stdarg.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <signal.h>
  13. #include <stdint.h>
  14. #include <sys/ioctl.h>
  15. #include <sys/select.h>
  16. #include <sys/stat.h>
  17. #include <sys/time.h>
  18. #include <sys/types.h>
  19. #include <sys/wait.h>
  20. #include <termios.h>
  21. #include <time.h>
  22. #include <unistd.h>
  23. #include <libgen.h>
  24. #include <X11/Xatom.h>
  25. #include <X11/Xlib.h>
  26. #include <X11/Xutil.h>
  27. #include <X11/cursorfont.h>
  28. #include <X11/keysym.h>
  29. #include <X11/Xft/Xft.h>
  30. #include <X11/XKBlib.h>
  31. #include <fontconfig/fontconfig.h>
  32. #include <wchar.h>
  33. #include "arg.h"
  34. char *argv0;
  35. #define Glyph Glyph_
  36. #define Font Font_
  37. #if defined(__linux)
  38. #include <pty.h>
  39. #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
  40. #include <util.h>
  41. #elif defined(__FreeBSD__) || defined(__DragonFly__)
  42. #include <libutil.h>
  43. #endif
  44. /* XEMBED messages */
  45. #define XEMBED_FOCUS_IN 4
  46. #define XEMBED_FOCUS_OUT 5
  47. /* Arbitrary sizes */
  48. #define UTF_INVALID 0xFFFD
  49. #define UTF_SIZ 4
  50. #define ESC_BUF_SIZ (128*UTF_SIZ)
  51. #define ESC_ARG_SIZ 16
  52. #define STR_BUF_SIZ ESC_BUF_SIZ
  53. #define STR_ARG_SIZ ESC_ARG_SIZ
  54. #define XK_ANY_MOD UINT_MAX
  55. #define XK_NO_MOD 0
  56. #define XK_SWITCH_MOD (1<<13)
  57. /* macros */
  58. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  59. #define MAX(a, b) ((a) < (b) ? (b) : (a))
  60. #define LEN(a) (sizeof(a) / sizeof(a)[0])
  61. #define NUMMAXLEN(x) ((int)(sizeof(x) * 2.56 + 0.5) + 1)
  62. #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
  63. #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
  64. #define DIVCEIL(n, d) (((n) + ((d) - 1)) / (d))
  65. #define ISCONTROLC0(c) (BETWEEN(c, 0, 0x1f) || (c) == '\177')
  66. #define ISCONTROLC1(c) (BETWEEN(c, 0x80, 0x9f))
  67. #define ISCONTROL(c) (ISCONTROLC0(c) || ISCONTROLC1(c))
  68. #define ISDELIM(u) (utf8strchr(worddelimiters, u) != NULL)
  69. #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
  70. #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || \
  71. (a).bg != (b).bg)
  72. #define IS_SET(flag) ((term.mode & (flag)) != 0)
  73. #define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + \
  74. (t1.tv_nsec-t2.tv_nsec)/1E6)
  75. #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
  76. #define TRUECOLOR(r,g,b) (1 << 24 | (r) << 16 | (g) << 8 | (b))
  77. #define IS_TRUECOL(x) (1 << 24 & (x))
  78. #define TRUERED(x) (((x) & 0xff0000) >> 8)
  79. #define TRUEGREEN(x) (((x) & 0xff00))
  80. #define TRUEBLUE(x) (((x) & 0xff) << 8)
  81. /* constants */
  82. #define ISO14755CMD "dmenu -w %lu -p codepoint: </dev/null"
  83. enum glyph_attribute {
  84. ATTR_NULL = 0,
  85. ATTR_BOLD = 1 << 0,
  86. ATTR_FAINT = 1 << 1,
  87. ATTR_ITALIC = 1 << 2,
  88. ATTR_UNDERLINE = 1 << 3,
  89. ATTR_BLINK = 1 << 4,
  90. ATTR_REVERSE = 1 << 5,
  91. ATTR_INVISIBLE = 1 << 6,
  92. ATTR_STRUCK = 1 << 7,
  93. ATTR_WRAP = 1 << 8,
  94. ATTR_WIDE = 1 << 9,
  95. ATTR_WDUMMY = 1 << 10,
  96. ATTR_BOLD_FAINT = ATTR_BOLD | ATTR_FAINT,
  97. };
  98. enum cursor_movement {
  99. CURSOR_SAVE,
  100. CURSOR_LOAD
  101. };
  102. enum cursor_state {
  103. CURSOR_DEFAULT = 0,
  104. CURSOR_WRAPNEXT = 1,
  105. CURSOR_ORIGIN = 2
  106. };
  107. enum term_mode {
  108. MODE_WRAP = 1 << 0,
  109. MODE_INSERT = 1 << 1,
  110. MODE_APPKEYPAD = 1 << 2,
  111. MODE_ALTSCREEN = 1 << 3,
  112. MODE_CRLF = 1 << 4,
  113. MODE_MOUSEBTN = 1 << 5,
  114. MODE_MOUSEMOTION = 1 << 6,
  115. MODE_REVERSE = 1 << 7,
  116. MODE_KBDLOCK = 1 << 8,
  117. MODE_HIDE = 1 << 9,
  118. MODE_ECHO = 1 << 10,
  119. MODE_APPCURSOR = 1 << 11,
  120. MODE_MOUSESGR = 1 << 12,
  121. MODE_8BIT = 1 << 13,
  122. MODE_BLINK = 1 << 14,
  123. MODE_FBLINK = 1 << 15,
  124. MODE_FOCUS = 1 << 16,
  125. MODE_MOUSEX10 = 1 << 17,
  126. MODE_MOUSEMANY = 1 << 18,
  127. MODE_BRCKTPASTE = 1 << 19,
  128. MODE_PRINT = 1 << 20,
  129. MODE_UTF8 = 1 << 21,
  130. MODE_SIXEL = 1 << 22,
  131. MODE_MOUSE = MODE_MOUSEBTN|MODE_MOUSEMOTION|MODE_MOUSEX10\
  132. |MODE_MOUSEMANY,
  133. };
  134. enum charset {
  135. CS_GRAPHIC0,
  136. CS_GRAPHIC1,
  137. CS_UK,
  138. CS_USA,
  139. CS_MULTI,
  140. CS_GER,
  141. CS_FIN
  142. };
  143. enum escape_state {
  144. ESC_START = 1,
  145. ESC_CSI = 2,
  146. ESC_STR = 4, /* OSC, PM, APC */
  147. ESC_ALTCHARSET = 8,
  148. ESC_STR_END = 16, /* a final string was encountered */
  149. ESC_TEST = 32, /* Enter in test mode */
  150. ESC_UTF8 = 64,
  151. ESC_DCS =128,
  152. };
  153. enum window_state {
  154. WIN_VISIBLE = 1,
  155. WIN_FOCUSED = 2
  156. };
  157. enum selection_mode {
  158. SEL_IDLE = 0,
  159. SEL_EMPTY = 1,
  160. SEL_READY = 2
  161. };
  162. enum selection_type {
  163. SEL_REGULAR = 1,
  164. SEL_RECTANGULAR = 2
  165. };
  166. enum selection_snap {
  167. SNAP_WORD = 1,
  168. SNAP_LINE = 2
  169. };
  170. typedef unsigned char uchar;
  171. typedef unsigned int uint;
  172. typedef unsigned long ulong;
  173. typedef unsigned short ushort;
  174. typedef uint_least32_t Rune;
  175. typedef XftDraw *Draw;
  176. typedef XftColor Color;
  177. typedef struct {
  178. Rune u; /* character code */
  179. ushort mode; /* attribute flags */
  180. uint32_t fg; /* foreground */
  181. uint32_t bg; /* background */
  182. } Glyph;
  183. typedef Glyph *Line;
  184. typedef struct {
  185. Glyph attr; /* current char attributes */
  186. int x;
  187. int y;
  188. char state;
  189. } TCursor;
  190. /* CSI Escape sequence structs */
  191. /* ESC '[' [[ [<priv>] <arg> [;]] <mode> [<mode>]] */
  192. typedef struct {
  193. char buf[ESC_BUF_SIZ]; /* raw string */
  194. int len; /* raw string length */
  195. char priv;
  196. int arg[ESC_ARG_SIZ];
  197. int narg; /* nb of args */
  198. char mode[2];
  199. } CSIEscape;
  200. /* STR Escape sequence structs */
  201. /* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
  202. typedef struct {
  203. char type; /* ESC type ... */
  204. char buf[STR_BUF_SIZ]; /* raw string */
  205. int len; /* raw string length */
  206. char *args[STR_ARG_SIZ];
  207. int narg; /* nb of args */
  208. } STREscape;
  209. /* Internal representation of the screen */
  210. typedef struct {
  211. int row; /* nb row */
  212. int col; /* nb col */
  213. Line *line; /* screen */
  214. Line *alt; /* alternate screen */
  215. int *dirty; /* dirtyness of lines */
  216. XftGlyphFontSpec *specbuf; /* font spec buffer used for rendering */
  217. TCursor c; /* cursor */
  218. int top; /* top scroll limit */
  219. int bot; /* bottom scroll limit */
  220. int mode; /* terminal mode flags */
  221. int esc; /* escape state flags */
  222. char trantbl[4]; /* charset table translation */
  223. int charset; /* current charset */
  224. int icharset; /* selected charset for sequence */
  225. int numlock; /* lock numbers in keyboard */
  226. int *tabs;
  227. } Term;
  228. /* Purely graphic info */
  229. typedef struct {
  230. Display *dpy;
  231. Colormap cmap;
  232. Window win;
  233. Drawable buf;
  234. Atom xembed, wmdeletewin, netwmname, netwmpid;
  235. XIM xim;
  236. XIC xic;
  237. Draw draw;
  238. Visual *vis;
  239. XSetWindowAttributes attrs;
  240. int scr;
  241. int isfixed; /* is fixed geometry? */
  242. int l, t; /* left and top offset */
  243. int gm; /* geometry mask */
  244. int tw, th; /* tty width and height */
  245. int w, h; /* window width and height */
  246. int ch; /* char height */
  247. int cw; /* char width */
  248. char state; /* focus, redraw, visible */
  249. int cursor; /* cursor style */
  250. } XWindow;
  251. typedef struct {
  252. uint b;
  253. uint mask;
  254. char *s;
  255. } MouseShortcut;
  256. typedef struct {
  257. KeySym k;
  258. uint mask;
  259. char *s;
  260. /* three valued logic variables: 0 indifferent, 1 on, -1 off */
  261. signed char appkey; /* application keypad */
  262. signed char appcursor; /* application cursor */
  263. signed char crlf; /* crlf mode */
  264. } Key;
  265. typedef struct {
  266. int mode;
  267. int type;
  268. int snap;
  269. /*
  270. * Selection variables:
  271. * nb normalized coordinates of the beginning of the selection
  272. * ne normalized coordinates of the end of the selection
  273. * ob original coordinates of the beginning of the selection
  274. * oe original coordinates of the end of the selection
  275. */
  276. struct {
  277. int x, y;
  278. } nb, ne, ob, oe;
  279. char *primary, *clipboard;
  280. Atom xtarget;
  281. int alt;
  282. struct timespec tclick1;
  283. struct timespec tclick2;
  284. } Selection;
  285. typedef union {
  286. int i;
  287. uint ui;
  288. float f;
  289. const void *v;
  290. } Arg;
  291. typedef struct {
  292. uint mod;
  293. KeySym keysym;
  294. void (*func)(const Arg *);
  295. const Arg arg;
  296. } Shortcut;
  297. /* function definitions used in config.h */
  298. static void clipcopy(const Arg *);
  299. static void clippaste(const Arg *);
  300. static void numlock(const Arg *);
  301. static void selpaste(const Arg *);
  302. static void xzoom(const Arg *);
  303. static void xzoomabs(const Arg *);
  304. static void xzoomreset(const Arg *);
  305. static void printsel(const Arg *);
  306. static void printscreen(const Arg *) ;
  307. static void iso14755(const Arg *);
  308. static void toggleprinter(const Arg *);
  309. static void sendbreak(const Arg *);
  310. /* Config.h for applying patches and the configuration. */
  311. #include "config.h"
  312. /* Font structure */
  313. typedef struct {
  314. int height;
  315. int width;
  316. int ascent;
  317. int descent;
  318. int badslant;
  319. int badweight;
  320. short lbearing;
  321. short rbearing;
  322. XftFont *match;
  323. FcFontSet *set;
  324. FcPattern *pattern;
  325. } Font;
  326. /* Drawing Context */
  327. typedef struct {
  328. Color col[MAX(LEN(colorname), 256)];
  329. Font font, bfont, ifont, ibfont;
  330. GC gc;
  331. } DC;
  332. static void die(const char *, ...);
  333. static void draw(void);
  334. static void redraw(void);
  335. static void drawregion(int, int, int, int);
  336. static void execsh(void);
  337. static void stty(void);
  338. static void sigchld(int);
  339. static void run(void);
  340. static void csidump(void);
  341. static void csihandle(void);
  342. static void csiparse(void);
  343. static void csireset(void);
  344. static int eschandle(uchar);
  345. static void strdump(void);
  346. static void strhandle(void);
  347. static void strparse(void);
  348. static void strreset(void);
  349. static int tattrset(int);
  350. static void tprinter(char *, size_t);
  351. static void tdumpsel(void);
  352. static void tdumpline(int);
  353. static void tdump(void);
  354. static void tclearregion(int, int, int, int);
  355. static void tcursor(int);
  356. static void tdeletechar(int);
  357. static void tdeleteline(int);
  358. static void tinsertblank(int);
  359. static void tinsertblankline(int);
  360. static int tlinelen(int);
  361. static void tmoveto(int, int);
  362. static void tmoveato(int, int);
  363. static void tnew(int, int);
  364. static void tnewline(int);
  365. static void tputtab(int);
  366. static void tputc(Rune);
  367. static void treset(void);
  368. static void tresize(int, int);
  369. static void tscrollup(int, int);
  370. static void tscrolldown(int, int);
  371. static void tsetattr(int *, int);
  372. static void tsetchar(Rune, Glyph *, int, int);
  373. static void tsetscroll(int, int);
  374. static void tswapscreen(void);
  375. static void tsetdirt(int, int);
  376. static void tsetdirtattr(int);
  377. static void tsetmode(int, int, int *, int);
  378. static void tfulldirt(void);
  379. static void techo(Rune);
  380. static void tcontrolcode(uchar );
  381. static void tdectest(char );
  382. static void tdefutf8(char);
  383. static int32_t tdefcolor(int *, int *, int);
  384. static void tdeftran(char);
  385. static inline int match(uint, uint);
  386. static void ttynew(void);
  387. static size_t ttyread(void);
  388. static void ttyresize(void);
  389. static void ttysend(char *, size_t);
  390. static void ttywrite(const char *, size_t);
  391. static void tstrsequence(uchar);
  392. static inline ushort sixd_to_16bit(int);
  393. static int xmakeglyphfontspecs(XftGlyphFontSpec *, const Glyph *, int, int, int);
  394. static void xdrawglyphfontspecs(const XftGlyphFontSpec *, Glyph, int, int, int);
  395. static void xdrawglyph(Glyph, int, int);
  396. static void xhints(void);
  397. static void xclear(int, int, int, int);
  398. static void xdrawcursor(void);
  399. static void xinit(void);
  400. static void xloadcols(void);
  401. static int xsetcolorname(int, const char *);
  402. static int xgeommasktogravity(int);
  403. static int xloadfont(Font *, FcPattern *);
  404. static void xloadfonts(char *, double);
  405. static void xsettitle(char *);
  406. static void xresettitle(void);
  407. static void xsetpointermotion(int);
  408. static void xseturgency(int);
  409. static void xsetsel(char *, Time);
  410. static void xunloadfont(Font *);
  411. static void xunloadfonts(void);
  412. static void xresize(int, int);
  413. static void expose(XEvent *);
  414. static void visibility(XEvent *);
  415. static void unmap(XEvent *);
  416. static char *kmap(KeySym, uint);
  417. static void kpress(XEvent *);
  418. static void cmessage(XEvent *);
  419. static void cresize(int, int);
  420. static void resize(XEvent *);
  421. static void focus(XEvent *);
  422. static void brelease(XEvent *);
  423. static void bpress(XEvent *);
  424. static void bmotion(XEvent *);
  425. static void propnotify(XEvent *);
  426. static void selnotify(XEvent *);
  427. static void selclear(XEvent *);
  428. static void selrequest(XEvent *);
  429. static void selinit(void);
  430. static void selnormalize(void);
  431. static inline int selected(int, int);
  432. static char *getsel(void);
  433. static void selcopy(Time);
  434. static void selscroll(int, int);
  435. static void selsnap(int *, int *, int);
  436. static int x2col(int);
  437. static int y2row(int);
  438. static void getbuttoninfo(XEvent *);
  439. static void mousereport(XEvent *);
  440. static size_t utf8decode(char *, Rune *, size_t);
  441. static Rune utf8decodebyte(char, size_t *);
  442. static size_t utf8encode(Rune, char *);
  443. static char utf8encodebyte(Rune, size_t);
  444. static char *utf8strchr(char *s, Rune u);
  445. static size_t utf8validate(Rune *, size_t);
  446. static ssize_t xwrite(int, const char *, size_t);
  447. static void *xmalloc(size_t);
  448. static void *xrealloc(void *, size_t);
  449. static char *xstrdup(char *);
  450. static void usage(void);
  451. static void (*handler[LASTEvent])(XEvent *) = {
  452. [KeyPress] = kpress,
  453. [ClientMessage] = cmessage,
  454. [ConfigureNotify] = resize,
  455. [VisibilityNotify] = visibility,
  456. [UnmapNotify] = unmap,
  457. [Expose] = expose,
  458. [FocusIn] = focus,
  459. [FocusOut] = focus,
  460. [MotionNotify] = bmotion,
  461. [ButtonPress] = bpress,
  462. [ButtonRelease] = brelease,
  463. /*
  464. * Uncomment if you want the selection to disappear when you select something
  465. * different in another window.
  466. */
  467. /* [SelectionClear] = selclear, */
  468. [SelectionNotify] = selnotify,
  469. /*
  470. * PropertyNotify is only turned on when there is some INCR transfer happening
  471. * for the selection retrieval.
  472. */
  473. [PropertyNotify] = propnotify,
  474. [SelectionRequest] = selrequest,
  475. };
  476. /* Globals */
  477. static DC dc;
  478. static XWindow xw;
  479. static Term term;
  480. static CSIEscape csiescseq;
  481. static STREscape strescseq;
  482. static int cmdfd;
  483. static pid_t pid;
  484. static Selection sel;
  485. static int iofd = 1;
  486. static char **opt_cmd = NULL;
  487. static char *opt_class = NULL;
  488. static char *opt_embed = NULL;
  489. static char *opt_font = NULL;
  490. static char *opt_io = NULL;
  491. static char *opt_line = NULL;
  492. static char *opt_name = NULL;
  493. static char *opt_title = NULL;
  494. static int oldbutton = 3; /* button event on startup: 3 = release */
  495. static char *usedfont = NULL;
  496. static double usedfontsize = 0;
  497. static double defaultfontsize = 0;
  498. static uchar utfbyte[UTF_SIZ + 1] = {0x80, 0, 0xC0, 0xE0, 0xF0};
  499. static uchar utfmask[UTF_SIZ + 1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8};
  500. static Rune utfmin[UTF_SIZ + 1] = { 0, 0, 0x80, 0x800, 0x10000};
  501. static Rune utfmax[UTF_SIZ + 1] = {0x10FFFF, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF};
  502. /* Font Ring Cache */
  503. enum {
  504. FRC_NORMAL,
  505. FRC_ITALIC,
  506. FRC_BOLD,
  507. FRC_ITALICBOLD
  508. };
  509. typedef struct {
  510. XftFont *font;
  511. int flags;
  512. Rune unicodep;
  513. } Fontcache;
  514. /* Fontcache is an array now. A new font will be appended to the array. */
  515. static Fontcache frc[16];
  516. static int frclen = 0;
  517. ssize_t
  518. xwrite(int fd, const char *s, size_t len)
  519. {
  520. size_t aux = len;
  521. ssize_t r;
  522. while (len > 0) {
  523. r = write(fd, s, len);
  524. if (r < 0)
  525. return r;
  526. len -= r;
  527. s += r;
  528. }
  529. return aux;
  530. }
  531. void *
  532. xmalloc(size_t len)
  533. {
  534. void *p = malloc(len);
  535. if (!p)
  536. die("Out of memory\n");
  537. return p;
  538. }
  539. void *
  540. xrealloc(void *p, size_t len)
  541. {
  542. if ((p = realloc(p, len)) == NULL)
  543. die("Out of memory\n");
  544. return p;
  545. }
  546. char *
  547. xstrdup(char *s)
  548. {
  549. if ((s = strdup(s)) == NULL)
  550. die("Out of memory\n");
  551. return s;
  552. }
  553. size_t
  554. utf8decode(char *c, Rune *u, size_t clen)
  555. {
  556. size_t i, j, len, type;
  557. Rune udecoded;
  558. *u = UTF_INVALID;
  559. if (!clen)
  560. return 0;
  561. udecoded = utf8decodebyte(c[0], &len);
  562. if (!BETWEEN(len, 1, UTF_SIZ))
  563. return 1;
  564. for (i = 1, j = 1; i < clen && j < len; ++i, ++j) {
  565. udecoded = (udecoded << 6) | utf8decodebyte(c[i], &type);
  566. if (type != 0)
  567. return j;
  568. }
  569. if (j < len)
  570. return 0;
  571. *u = udecoded;
  572. utf8validate(u, len);
  573. return len;
  574. }
  575. Rune
  576. utf8decodebyte(char c, size_t *i)
  577. {
  578. for (*i = 0; *i < LEN(utfmask); ++(*i))
  579. if (((uchar)c & utfmask[*i]) == utfbyte[*i])
  580. return (uchar)c & ~utfmask[*i];
  581. return 0;
  582. }
  583. size_t
  584. utf8encode(Rune u, char *c)
  585. {
  586. size_t len, i;
  587. len = utf8validate(&u, 0);
  588. if (len > UTF_SIZ)
  589. return 0;
  590. for (i = len - 1; i != 0; --i) {
  591. c[i] = utf8encodebyte(u, 0);
  592. u >>= 6;
  593. }
  594. c[0] = utf8encodebyte(u, len);
  595. return len;
  596. }
  597. char
  598. utf8encodebyte(Rune u, size_t i)
  599. {
  600. return utfbyte[i] | (u & ~utfmask[i]);
  601. }
  602. char *
  603. utf8strchr(char *s, Rune u)
  604. {
  605. Rune r;
  606. size_t i, j, len;
  607. len = strlen(s);
  608. for (i = 0, j = 0; i < len; i += j) {
  609. if (!(j = utf8decode(&s[i], &r, len - i)))
  610. break;
  611. if (r == u)
  612. return &(s[i]);
  613. }
  614. return NULL;
  615. }
  616. size_t
  617. utf8validate(Rune *u, size_t i)
  618. {
  619. if (!BETWEEN(*u, utfmin[i], utfmax[i]) || BETWEEN(*u, 0xD800, 0xDFFF))
  620. *u = UTF_INVALID;
  621. for (i = 1; *u > utfmax[i]; ++i)
  622. ;
  623. return i;
  624. }
  625. void
  626. selinit(void)
  627. {
  628. clock_gettime(CLOCK_MONOTONIC, &sel.tclick1);
  629. clock_gettime(CLOCK_MONOTONIC, &sel.tclick2);
  630. sel.mode = SEL_IDLE;
  631. sel.snap = 0;
  632. sel.ob.x = -1;
  633. sel.primary = NULL;
  634. sel.clipboard = NULL;
  635. sel.xtarget = XInternAtom(xw.dpy, "UTF8_STRING", 0);
  636. if (sel.xtarget == None)
  637. sel.xtarget = XA_STRING;
  638. }
  639. int
  640. x2col(int x)
  641. {
  642. x -= borderpx;
  643. x /= xw.cw;
  644. return LIMIT(x, 0, term.col-1);
  645. }
  646. int
  647. y2row(int y)
  648. {
  649. y -= borderpx;
  650. y /= xw.ch;
  651. return LIMIT(y, 0, term.row-1);
  652. }
  653. int
  654. tlinelen(int y)
  655. {
  656. int i = term.col;
  657. if (term.line[y][i - 1].mode & ATTR_WRAP)
  658. return i;
  659. while (i > 0 && term.line[y][i - 1].u == ' ')
  660. --i;
  661. return i;
  662. }
  663. void
  664. selnormalize(void)
  665. {
  666. int i;
  667. if (sel.type == SEL_REGULAR && sel.ob.y != sel.oe.y) {
  668. sel.nb.x = sel.ob.y < sel.oe.y ? sel.ob.x : sel.oe.x;
  669. sel.ne.x = sel.ob.y < sel.oe.y ? sel.oe.x : sel.ob.x;
  670. } else {
  671. sel.nb.x = MIN(sel.ob.x, sel.oe.x);
  672. sel.ne.x = MAX(sel.ob.x, sel.oe.x);
  673. }
  674. sel.nb.y = MIN(sel.ob.y, sel.oe.y);
  675. sel.ne.y = MAX(sel.ob.y, sel.oe.y);
  676. selsnap(&sel.nb.x, &sel.nb.y, -1);
  677. selsnap(&sel.ne.x, &sel.ne.y, +1);
  678. /* expand selection over line breaks */
  679. if (sel.type == SEL_RECTANGULAR)
  680. return;
  681. i = tlinelen(sel.nb.y);
  682. if (i < sel.nb.x)
  683. sel.nb.x = i;
  684. if (tlinelen(sel.ne.y) <= sel.ne.x)
  685. sel.ne.x = term.col - 1;
  686. }
  687. int
  688. selected(int x, int y)
  689. {
  690. if (sel.mode == SEL_EMPTY)
  691. return 0;
  692. if (sel.type == SEL_RECTANGULAR)
  693. return BETWEEN(y, sel.nb.y, sel.ne.y)
  694. && BETWEEN(x, sel.nb.x, sel.ne.x);
  695. return BETWEEN(y, sel.nb.y, sel.ne.y)
  696. && (y != sel.nb.y || x >= sel.nb.x)
  697. && (y != sel.ne.y || x <= sel.ne.x);
  698. }
  699. void
  700. selsnap(int *x, int *y, int direction)
  701. {
  702. int newx, newy, xt, yt;
  703. int delim, prevdelim;
  704. Glyph *gp, *prevgp;
  705. switch (sel.snap) {
  706. case SNAP_WORD:
  707. /*
  708. * Snap around if the word wraps around at the end or
  709. * beginning of a line.
  710. */
  711. prevgp = &term.line[*y][*x];
  712. prevdelim = ISDELIM(prevgp->u);
  713. for (;;) {
  714. newx = *x + direction;
  715. newy = *y;
  716. if (!BETWEEN(newx, 0, term.col - 1)) {
  717. newy += direction;
  718. newx = (newx + term.col) % term.col;
  719. if (!BETWEEN(newy, 0, term.row - 1))
  720. break;
  721. if (direction > 0)
  722. yt = *y, xt = *x;
  723. else
  724. yt = newy, xt = newx;
  725. if (!(term.line[yt][xt].mode & ATTR_WRAP))
  726. break;
  727. }
  728. if (newx >= tlinelen(newy))
  729. break;
  730. gp = &term.line[newy][newx];
  731. delim = ISDELIM(gp->u);
  732. if (!(gp->mode & ATTR_WDUMMY) && (delim != prevdelim
  733. || (delim && gp->u != prevgp->u)))
  734. break;
  735. *x = newx;
  736. *y = newy;
  737. prevgp = gp;
  738. prevdelim = delim;
  739. }
  740. break;
  741. case SNAP_LINE:
  742. /*
  743. * Snap around if the the previous line or the current one
  744. * has set ATTR_WRAP at its end. Then the whole next or
  745. * previous line will be selected.
  746. */
  747. *x = (direction < 0) ? 0 : term.col - 1;
  748. if (direction < 0) {
  749. for (; *y > 0; *y += direction) {
  750. if (!(term.line[*y-1][term.col-1].mode
  751. & ATTR_WRAP)) {
  752. break;
  753. }
  754. }
  755. } else if (direction > 0) {
  756. for (; *y < term.row-1; *y += direction) {
  757. if (!(term.line[*y][term.col-1].mode
  758. & ATTR_WRAP)) {
  759. break;
  760. }
  761. }
  762. }
  763. break;
  764. }
  765. }
  766. void
  767. getbuttoninfo(XEvent *e)
  768. {
  769. int type;
  770. uint state = e->xbutton.state & ~(Button1Mask | forceselmod);
  771. sel.alt = IS_SET(MODE_ALTSCREEN);
  772. sel.oe.x = x2col(e->xbutton.x);
  773. sel.oe.y = y2row(e->xbutton.y);
  774. selnormalize();
  775. sel.type = SEL_REGULAR;
  776. for (type = 1; type < LEN(selmasks); ++type) {
  777. if (match(selmasks[type], state)) {
  778. sel.type = type;
  779. break;
  780. }
  781. }
  782. }
  783. void
  784. mousereport(XEvent *e)
  785. {
  786. int x = x2col(e->xbutton.x), y = y2row(e->xbutton.y),
  787. button = e->xbutton.button, state = e->xbutton.state,
  788. len;
  789. char buf[40];
  790. static int ox, oy;
  791. /* from urxvt */
  792. if (e->xbutton.type == MotionNotify) {
  793. if (x == ox && y == oy)
  794. return;
  795. if (!IS_SET(MODE_MOUSEMOTION) && !IS_SET(MODE_MOUSEMANY))
  796. return;
  797. /* MOUSE_MOTION: no reporting if no button is pressed */
  798. if (IS_SET(MODE_MOUSEMOTION) && oldbutton == 3)
  799. return;
  800. button = oldbutton + 32;
  801. ox = x;
  802. oy = y;
  803. } else {
  804. if (!IS_SET(MODE_MOUSESGR) && e->xbutton.type == ButtonRelease) {
  805. button = 3;
  806. } else {
  807. button -= Button1;
  808. if (button >= 3)
  809. button += 64 - 3;
  810. }
  811. if (e->xbutton.type == ButtonPress) {
  812. oldbutton = button;
  813. ox = x;
  814. oy = y;
  815. } else if (e->xbutton.type == ButtonRelease) {
  816. oldbutton = 3;
  817. /* MODE_MOUSEX10: no button release reporting */
  818. if (IS_SET(MODE_MOUSEX10))
  819. return;
  820. if (button == 64 || button == 65)
  821. return;
  822. }
  823. }
  824. if (!IS_SET(MODE_MOUSEX10)) {
  825. button += ((state & ShiftMask ) ? 4 : 0)
  826. + ((state & Mod4Mask ) ? 8 : 0)
  827. + ((state & ControlMask) ? 16 : 0);
  828. }
  829. if (IS_SET(MODE_MOUSESGR)) {
  830. len = snprintf(buf, sizeof(buf), "\033[<%d;%d;%d%c",
  831. button, x+1, y+1,
  832. e->xbutton.type == ButtonRelease ? 'm' : 'M');
  833. } else if (x < 223 && y < 223) {
  834. len = snprintf(buf, sizeof(buf), "\033[M%c%c%c",
  835. 32+button, 32+x+1, 32+y+1);
  836. } else {
  837. return;
  838. }
  839. ttywrite(buf, len);
  840. }
  841. void
  842. bpress(XEvent *e)
  843. {
  844. struct timespec now;
  845. MouseShortcut *ms;
  846. if (IS_SET(MODE_MOUSE) && !(e->xbutton.state & forceselmod)) {
  847. mousereport(e);
  848. return;
  849. }
  850. for (ms = mshortcuts; ms < mshortcuts + LEN(mshortcuts); ms++) {
  851. if (e->xbutton.button == ms->b
  852. && match(ms->mask, e->xbutton.state)) {
  853. ttysend(ms->s, strlen(ms->s));
  854. return;
  855. }
  856. }
  857. if (e->xbutton.button == Button1) {
  858. clock_gettime(CLOCK_MONOTONIC, &now);
  859. /* Clear previous selection, logically and visually. */
  860. selclear(NULL);
  861. sel.mode = SEL_EMPTY;
  862. sel.type = SEL_REGULAR;
  863. sel.oe.x = sel.ob.x = x2col(e->xbutton.x);
  864. sel.oe.y = sel.ob.y = y2row(e->xbutton.y);
  865. /*
  866. * If the user clicks below predefined timeouts specific
  867. * snapping behaviour is exposed.
  868. */
  869. if (TIMEDIFF(now, sel.tclick2) <= tripleclicktimeout) {
  870. sel.snap = SNAP_LINE;
  871. } else if (TIMEDIFF(now, sel.tclick1) <= doubleclicktimeout) {
  872. sel.snap = SNAP_WORD;
  873. } else {
  874. sel.snap = 0;
  875. }
  876. selnormalize();
  877. if (sel.snap != 0)
  878. sel.mode = SEL_READY;
  879. tsetdirt(sel.nb.y, sel.ne.y);
  880. sel.tclick2 = sel.tclick1;
  881. sel.tclick1 = now;
  882. }
  883. }
  884. char *
  885. getsel(void)
  886. {
  887. char *str, *ptr;
  888. int y, bufsize, lastx, linelen;
  889. Glyph *gp, *last;
  890. if (sel.ob.x == -1)
  891. return NULL;
  892. bufsize = (term.col+1) * (sel.ne.y-sel.nb.y+1) * UTF_SIZ;
  893. ptr = str = xmalloc(bufsize);
  894. /* append every set & selected glyph to the selection */
  895. for (y = sel.nb.y; y <= sel.ne.y; y++) {
  896. if ((linelen = tlinelen(y)) == 0) {
  897. *ptr++ = '\n';
  898. continue;
  899. }
  900. if (sel.type == SEL_RECTANGULAR) {
  901. gp = &term.line[y][sel.nb.x];
  902. lastx = sel.ne.x;
  903. } else {
  904. gp = &term.line[y][sel.nb.y == y ? sel.nb.x : 0];
  905. lastx = (sel.ne.y == y) ? sel.ne.x : term.col-1;
  906. }
  907. last = &term.line[y][MIN(lastx, linelen-1)];
  908. while (last >= gp && last->u == ' ')
  909. --last;
  910. for ( ; gp <= last; ++gp) {
  911. if (gp->mode & ATTR_WDUMMY)
  912. continue;
  913. ptr += utf8encode(gp->u, ptr);
  914. }
  915. /*
  916. * Copy and pasting of line endings is inconsistent
  917. * in the inconsistent terminal and GUI world.
  918. * The best solution seems like to produce '\n' when
  919. * something is copied from st and convert '\n' to
  920. * '\r', when something to be pasted is received by
  921. * st.
  922. * FIXME: Fix the computer world.
  923. */
  924. if ((y < sel.ne.y || lastx >= linelen) && !(last->mode & ATTR_WRAP))
  925. *ptr++ = '\n';
  926. }
  927. *ptr = 0;
  928. return str;
  929. }
  930. void
  931. selcopy(Time t)
  932. {
  933. xsetsel(getsel(), t);
  934. }
  935. void
  936. propnotify(XEvent *e)
  937. {
  938. XPropertyEvent *xpev;
  939. Atom clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
  940. xpev = &e->xproperty;
  941. if (xpev->state == PropertyNewValue &&
  942. (xpev->atom == XA_PRIMARY ||
  943. xpev->atom == clipboard)) {
  944. selnotify(e);
  945. }
  946. }
  947. void
  948. selnotify(XEvent *e)
  949. {
  950. ulong nitems, ofs, rem;
  951. int format;
  952. uchar *data, *last, *repl;
  953. Atom type, incratom, property;
  954. incratom = XInternAtom(xw.dpy, "INCR", 0);
  955. ofs = 0;
  956. if (e->type == SelectionNotify) {
  957. property = e->xselection.property;
  958. } else if(e->type == PropertyNotify) {
  959. property = e->xproperty.atom;
  960. } else {
  961. return;
  962. }
  963. if (property == None)
  964. return;
  965. do {
  966. if (XGetWindowProperty(xw.dpy, xw.win, property, ofs,
  967. BUFSIZ/4, False, AnyPropertyType,
  968. &type, &format, &nitems, &rem,
  969. &data)) {
  970. fprintf(stderr, "Clipboard allocation failed\n");
  971. return;
  972. }
  973. if (e->type == PropertyNotify && nitems == 0 && rem == 0) {
  974. /*
  975. * If there is some PropertyNotify with no data, then
  976. * this is the signal of the selection owner that all
  977. * data has been transferred. We won't need to receive
  978. * PropertyNotify events anymore.
  979. */
  980. MODBIT(xw.attrs.event_mask, 0, PropertyChangeMask);
  981. XChangeWindowAttributes(xw.dpy, xw.win, CWEventMask,
  982. &xw.attrs);
  983. }
  984. if (type == incratom) {
  985. /*
  986. * Activate the PropertyNotify events so we receive
  987. * when the selection owner does send us the next
  988. * chunk of data.
  989. */
  990. MODBIT(xw.attrs.event_mask, 1, PropertyChangeMask);
  991. XChangeWindowAttributes(xw.dpy, xw.win, CWEventMask,
  992. &xw.attrs);
  993. /*
  994. * Deleting the property is the transfer start signal.
  995. */
  996. XDeleteProperty(xw.dpy, xw.win, (int)property);
  997. continue;
  998. }
  999. /*
  1000. * As seen in getsel:
  1001. * Line endings are inconsistent in the terminal and GUI world
  1002. * copy and pasting. When receiving some selection data,
  1003. * replace all '\n' with '\r'.
  1004. * FIXME: Fix the computer world.
  1005. */
  1006. repl = data;
  1007. last = data + nitems * format / 8;
  1008. while ((repl = memchr(repl, '\n', last - repl))) {
  1009. *repl++ = '\r';
  1010. }
  1011. if (IS_SET(MODE_BRCKTPASTE) && ofs == 0)
  1012. ttywrite("\033[200~", 6);
  1013. ttysend((char *)data, nitems * format / 8);
  1014. if (IS_SET(MODE_BRCKTPASTE) && rem == 0)
  1015. ttywrite("\033[201~", 6);
  1016. XFree(data);
  1017. /* number of 32-bit chunks returned */
  1018. ofs += nitems * format / 32;
  1019. } while (rem > 0);
  1020. /*
  1021. * Deleting the property again tells the selection owner to send the
  1022. * next data chunk in the property.
  1023. */
  1024. XDeleteProperty(xw.dpy, xw.win, (int)property);
  1025. }
  1026. void
  1027. selpaste(const Arg *dummy)
  1028. {
  1029. XConvertSelection(xw.dpy, XA_PRIMARY, sel.xtarget, XA_PRIMARY,
  1030. xw.win, CurrentTime);
  1031. }
  1032. void
  1033. clipcopy(const Arg *dummy)
  1034. {
  1035. Atom clipboard;
  1036. if (sel.clipboard != NULL)
  1037. free(sel.clipboard);
  1038. if (sel.primary != NULL) {
  1039. sel.clipboard = xstrdup(sel.primary);
  1040. clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
  1041. XSetSelectionOwner(xw.dpy, clipboard, xw.win, CurrentTime);
  1042. }
  1043. }
  1044. void
  1045. clippaste(const Arg *dummy)
  1046. {
  1047. Atom clipboard;
  1048. clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
  1049. XConvertSelection(xw.dpy, clipboard, sel.xtarget, clipboard,
  1050. xw.win, CurrentTime);
  1051. }
  1052. void
  1053. selclear(XEvent *e)
  1054. {
  1055. if (sel.ob.x == -1)
  1056. return;
  1057. sel.mode = SEL_IDLE;
  1058. sel.ob.x = -1;
  1059. tsetdirt(sel.nb.y, sel.ne.y);
  1060. }
  1061. void
  1062. selrequest(XEvent *e)
  1063. {
  1064. XSelectionRequestEvent *xsre;
  1065. XSelectionEvent xev;
  1066. Atom xa_targets, string, clipboard;
  1067. char *seltext;
  1068. xsre = (XSelectionRequestEvent *) e;
  1069. xev.type = SelectionNotify;
  1070. xev.requestor = xsre->requestor;
  1071. xev.selection = xsre->selection;
  1072. xev.target = xsre->target;
  1073. xev.time = xsre->time;
  1074. if (xsre->property == None)
  1075. xsre->property = xsre->target;
  1076. /* reject */
  1077. xev.property = None;
  1078. xa_targets = XInternAtom(xw.dpy, "TARGETS", 0);
  1079. if (xsre->target == xa_targets) {
  1080. /* respond with the supported type */
  1081. string = sel.xtarget;
  1082. XChangeProperty(xsre->display, xsre->requestor, xsre->property,
  1083. XA_ATOM, 32, PropModeReplace,
  1084. (uchar *) &string, 1);
  1085. xev.property = xsre->property;
  1086. } else if (xsre->target == sel.xtarget || xsre->target == XA_STRING) {
  1087. /*
  1088. * xith XA_STRING non ascii characters may be incorrect in the
  1089. * requestor. It is not our problem, use utf8.
  1090. */
  1091. clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
  1092. if (xsre->selection == XA_PRIMARY) {
  1093. seltext = sel.primary;
  1094. } else if (xsre->selection == clipboard) {
  1095. seltext = sel.clipboard;
  1096. } else {
  1097. fprintf(stderr,
  1098. "Unhandled clipboard selection 0x%lx\n",
  1099. xsre->selection);
  1100. return;
  1101. }
  1102. if (seltext != NULL) {
  1103. XChangeProperty(xsre->display, xsre->requestor,
  1104. xsre->property, xsre->target,
  1105. 8, PropModeReplace,
  1106. (uchar *)seltext, strlen(seltext));
  1107. xev.property = xsre->property;
  1108. }
  1109. }
  1110. /* all done, send a notification to the listener */
  1111. if (!XSendEvent(xsre->display, xsre->requestor, 1, 0, (XEvent *) &xev))
  1112. fprintf(stderr, "Error sending SelectionNotify event\n");
  1113. }
  1114. void
  1115. xsetsel(char *str, Time t)
  1116. {
  1117. free(sel.primary);
  1118. sel.primary = str;
  1119. XSetSelectionOwner(xw.dpy, XA_PRIMARY, xw.win, t);
  1120. if (XGetSelectionOwner(xw.dpy, XA_PRIMARY) != xw.win)
  1121. selclear(0);
  1122. }
  1123. void
  1124. brelease(XEvent *e)
  1125. {
  1126. if (IS_SET(MODE_MOUSE) && !(e->xbutton.state & forceselmod)) {
  1127. mousereport(e);
  1128. return;
  1129. }
  1130. if (e->xbutton.button == Button2) {
  1131. selpaste(NULL);
  1132. } else if (e->xbutton.button == Button1) {
  1133. if (sel.mode == SEL_READY) {
  1134. getbuttoninfo(e);
  1135. selcopy(e->xbutton.time);
  1136. } else
  1137. selclear(NULL);
  1138. sel.mode = SEL_IDLE;
  1139. tsetdirt(sel.nb.y, sel.ne.y);
  1140. }
  1141. }
  1142. void
  1143. bmotion(XEvent *e)
  1144. {
  1145. int oldey, oldex, oldsby, oldsey;
  1146. if (IS_SET(MODE_MOUSE) && !(e->xbutton.state & forceselmod)) {
  1147. mousereport(e);
  1148. return;
  1149. }
  1150. if (!sel.mode)
  1151. return;
  1152. sel.mode = SEL_READY;
  1153. oldey = sel.oe.y;
  1154. oldex = sel.oe.x;
  1155. oldsby = sel.nb.y;
  1156. oldsey = sel.ne.y;
  1157. getbuttoninfo(e);
  1158. if (oldey != sel.oe.y || oldex != sel.oe.x)
  1159. tsetdirt(MIN(sel.nb.y, oldsby), MAX(sel.ne.y, oldsey));
  1160. }
  1161. void
  1162. die(const char *errstr, ...)
  1163. {
  1164. va_list ap;
  1165. va_start(ap, errstr);
  1166. vfprintf(stderr, errstr, ap);
  1167. va_end(ap);
  1168. exit(1);
  1169. }
  1170. void
  1171. execsh(void)
  1172. {
  1173. char **args, *sh, *prog;
  1174. const struct passwd *pw;
  1175. char buf[sizeof(long) * 8 + 1];
  1176. errno = 0;
  1177. if ((pw = getpwuid(getuid())) == NULL) {
  1178. if (errno)
  1179. die("getpwuid:%s\n", strerror(errno));
  1180. else
  1181. die("who are you?\n");
  1182. }
  1183. if ((sh = getenv("SHELL")) == NULL)
  1184. sh = (pw->pw_shell[0]) ? pw->pw_shell : shell;
  1185. if (opt_cmd)
  1186. prog = opt_cmd[0];
  1187. else if (utmp)
  1188. prog = utmp;
  1189. else
  1190. prog = sh;
  1191. args = (opt_cmd) ? opt_cmd : (char *[]) {prog, NULL};
  1192. snprintf(buf, sizeof(buf), "%lu", xw.win);
  1193. unsetenv("COLUMNS");
  1194. unsetenv("LINES");
  1195. unsetenv("TERMCAP");
  1196. setenv("LOGNAME", pw->pw_name, 1);
  1197. setenv("USER", pw->pw_name, 1);
  1198. setenv("SHELL", sh, 1);
  1199. setenv("HOME", pw->pw_dir, 1);
  1200. setenv("TERM", termname, 1);
  1201. setenv("WINDOWID", buf, 1);
  1202. signal(SIGCHLD, SIG_DFL);
  1203. signal(SIGHUP, SIG_DFL);
  1204. signal(SIGINT, SIG_DFL);
  1205. signal(SIGQUIT, SIG_DFL);
  1206. signal(SIGTERM, SIG_DFL);
  1207. signal(SIGALRM, SIG_DFL);
  1208. execvp(prog, args);
  1209. _exit(1);
  1210. }
  1211. void
  1212. sigchld(int a)
  1213. {
  1214. int stat;
  1215. pid_t p;
  1216. if ((p = waitpid(pid, &stat, WNOHANG)) < 0)
  1217. die("Waiting for pid %hd failed: %s\n", pid, strerror(errno));
  1218. if (pid != p)
  1219. return;
  1220. if (!WIFEXITED(stat) || WEXITSTATUS(stat))
  1221. die("child finished with error '%d'\n", stat);
  1222. exit(0);
  1223. }
  1224. void
  1225. stty(void)
  1226. {
  1227. char cmd[_POSIX_ARG_MAX], **p, *q, *s;
  1228. size_t n, siz;
  1229. if ((n = strlen(stty_args)) > sizeof(cmd)-1)
  1230. die("incorrect stty parameters\n");
  1231. memcpy(cmd, stty_args, n);
  1232. q = cmd + n;
  1233. siz = sizeof(cmd) - n;
  1234. for (p = opt_cmd; p && (s = *p); ++p) {
  1235. if ((n = strlen(s)) > siz-1)
  1236. die("stty parameter length too long\n");
  1237. *q++ = ' ';
  1238. memcpy(q, s, n);
  1239. q += n;
  1240. siz -= n + 1;
  1241. }
  1242. *q = '\0';
  1243. if (system(cmd) != 0)
  1244. perror("Couldn't call stty");
  1245. }
  1246. void
  1247. ttynew(void)
  1248. {
  1249. int m, s;
  1250. struct winsize w = {term.row, term.col, 0, 0};
  1251. if (opt_io) {
  1252. term.mode |= MODE_PRINT;
  1253. iofd = (!strcmp(opt_io, "-")) ?
  1254. 1 : open(opt_io, O_WRONLY | O_CREAT, 0666);
  1255. if (iofd < 0) {
  1256. fprintf(stderr, "Error opening %s:%s\n",
  1257. opt_io, strerror(errno));
  1258. }
  1259. }
  1260. if (opt_line) {
  1261. if ((cmdfd = open(opt_line, O_RDWR)) < 0)
  1262. die("open line failed: %s\n", strerror(errno));
  1263. dup2(cmdfd, 0);
  1264. stty();
  1265. return;
  1266. }
  1267. /* seems to work fine on linux, openbsd and freebsd */
  1268. if (openpty(&m, &s, NULL, NULL, &w) < 0)
  1269. die("openpty failed: %s\n", strerror(errno));
  1270. switch (pid = fork()) {
  1271. case -1:
  1272. die("fork failed\n");
  1273. break;
  1274. case 0:
  1275. close(iofd);
  1276. setsid(); /* create a new process group */
  1277. dup2(s, 0);
  1278. dup2(s, 1);
  1279. dup2(s, 2);
  1280. if (ioctl(s, TIOCSCTTY, NULL) < 0)
  1281. die("ioctl TIOCSCTTY failed: %s\n", strerror(errno));
  1282. close(s);
  1283. close(m);
  1284. execsh();
  1285. break;
  1286. default:
  1287. close(s);
  1288. cmdfd = m;
  1289. signal(SIGCHLD, sigchld);
  1290. break;
  1291. }
  1292. }
  1293. size_t
  1294. ttyread(void)
  1295. {
  1296. static char buf[BUFSIZ];
  1297. static int buflen = 0;
  1298. char *ptr;
  1299. int charsize; /* size of utf8 char in bytes */
  1300. Rune unicodep;
  1301. int ret;
  1302. /* append read bytes to unprocessed bytes */
  1303. if ((ret = read(cmdfd, buf+buflen, LEN(buf)-buflen)) < 0)
  1304. die("Couldn't read from shell: %s\n", strerror(errno));
  1305. buflen += ret;
  1306. ptr = buf;
  1307. for (;;) {
  1308. if (IS_SET(MODE_UTF8) && !IS_SET(MODE_SIXEL)) {
  1309. /* process a complete utf8 char */
  1310. charsize = utf8decode(ptr, &unicodep, buflen);
  1311. if (charsize == 0)
  1312. break;
  1313. tputc(unicodep);
  1314. ptr += charsize;
  1315. buflen -= charsize;
  1316. } else {
  1317. if (buflen <= 0)
  1318. break;
  1319. tputc(*ptr++ & 0xFF);
  1320. buflen--;
  1321. }
  1322. }
  1323. /* keep any uncomplete utf8 char for the next call */
  1324. if (buflen > 0)
  1325. memmove(buf, ptr, buflen);
  1326. return ret;
  1327. }
  1328. void
  1329. ttywrite(const char *s, size_t n)
  1330. {
  1331. fd_set wfd, rfd;
  1332. ssize_t r;
  1333. size_t lim = 256;
  1334. /*
  1335. * Remember that we are using a pty, which might be a modem line.
  1336. * Writing too much will clog the line. That's why we are doing this
  1337. * dance.
  1338. * FIXME: Migrate the world to Plan 9.
  1339. */
  1340. while (n > 0) {
  1341. FD_ZERO(&wfd);
  1342. FD_ZERO(&rfd);
  1343. FD_SET(cmdfd, &wfd);
  1344. FD_SET(cmdfd, &rfd);
  1345. /* Check if we can write. */
  1346. if (pselect(cmdfd+1, &rfd, &wfd, NULL, NULL, NULL) < 0) {
  1347. if (errno == EINTR)
  1348. continue;
  1349. die("select failed: %s\n", strerror(errno));
  1350. }
  1351. if (FD_ISSET(cmdfd, &wfd)) {
  1352. /*
  1353. * Only write the bytes written by ttywrite() or the
  1354. * default of 256. This seems to be a reasonable value
  1355. * for a serial line. Bigger values might clog the I/O.
  1356. */
  1357. if ((r = write(cmdfd, s, (n < lim)? n : lim)) < 0)
  1358. goto write_error;
  1359. if (r < n) {
  1360. /*
  1361. * We weren't able to write out everything.
  1362. * This means the buffer is getting full
  1363. * again. Empty it.
  1364. */
  1365. if (n < lim)
  1366. lim = ttyread();
  1367. n -= r;
  1368. s += r;
  1369. } else {
  1370. /* All bytes have been written. */
  1371. break;
  1372. }
  1373. }
  1374. if (FD_ISSET(cmdfd, &rfd))
  1375. lim = ttyread();
  1376. }
  1377. return;
  1378. write_error:
  1379. die("write error on tty: %s\n", strerror(errno));
  1380. }
  1381. void
  1382. ttysend(char *s, size_t n)
  1383. {
  1384. int len;
  1385. char *t, *lim;
  1386. Rune u;
  1387. ttywrite(s, n);
  1388. if (!IS_SET(MODE_ECHO))
  1389. return;
  1390. lim = &s[n];
  1391. for (t = s; t < lim; t += len) {
  1392. if (IS_SET(MODE_UTF8) && !IS_SET(MODE_SIXEL)) {
  1393. len = utf8decode(t, &u, n);
  1394. } else {
  1395. u = *t & 0xFF;
  1396. len = 1;
  1397. }
  1398. if (len <= 0)
  1399. break;
  1400. techo(u);
  1401. n -= len;
  1402. }
  1403. }
  1404. void
  1405. ttyresize(void)
  1406. {
  1407. struct winsize w;
  1408. w.ws_row = term.row;
  1409. w.ws_col = term.col;
  1410. w.ws_xpixel = xw.tw;
  1411. w.ws_ypixel = xw.th;
  1412. if (ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
  1413. fprintf(stderr, "Couldn't set window size: %s\n", strerror(errno));
  1414. }
  1415. int
  1416. tattrset(int attr)
  1417. {
  1418. int i, j;
  1419. for (i = 0; i < term.row-1; i++) {
  1420. for (j = 0; j < term.col-1; j++) {
  1421. if (term.line[i][j].mode & attr)
  1422. return 1;
  1423. }
  1424. }
  1425. return 0;
  1426. }
  1427. void
  1428. tsetdirt(int top, int bot)
  1429. {
  1430. int i;
  1431. LIMIT(top, 0, term.row-1);
  1432. LIMIT(bot, 0, term.row-1);
  1433. for (i = top; i <= bot; i++)
  1434. term.dirty[i] = 1;
  1435. }
  1436. void
  1437. tsetdirtattr(int attr)
  1438. {
  1439. int i, j;
  1440. for (i = 0; i < term.row-1; i++) {
  1441. for (j = 0; j < term.col-1; j++) {
  1442. if (term.line[i][j].mode & attr) {
  1443. tsetdirt(i, i);
  1444. break;
  1445. }
  1446. }
  1447. }
  1448. }
  1449. void
  1450. tfulldirt(void)
  1451. {
  1452. tsetdirt(0, term.row-1);
  1453. }
  1454. void
  1455. tcursor(int mode)
  1456. {
  1457. static TCursor c[2];
  1458. int alt = IS_SET(MODE_ALTSCREEN);
  1459. if (mode == CURSOR_SAVE) {
  1460. c[alt] = term.c;
  1461. } else if (mode == CURSOR_LOAD) {
  1462. term.c = c[alt];
  1463. tmoveto(c[alt].x, c[alt].y);
  1464. }
  1465. }
  1466. void
  1467. treset(void)
  1468. {
  1469. uint i;
  1470. term.c = (TCursor){{
  1471. .mode = ATTR_NULL,
  1472. .fg = defaultfg,
  1473. .bg = defaultbg
  1474. }, .x = 0, .y = 0, .state = CURSOR_DEFAULT};
  1475. memset(term.tabs, 0, term.col * sizeof(*term.tabs));
  1476. for (i = tabspaces; i < term.col; i += tabspaces)
  1477. term.tabs[i] = 1;
  1478. term.top = 0;
  1479. term.bot = term.row - 1;
  1480. term.mode = MODE_WRAP|MODE_UTF8;
  1481. memset(term.trantbl, CS_USA, sizeof(term.trantbl));
  1482. term.charset = 0;
  1483. for (i = 0; i < 2; i++) {
  1484. tmoveto(0, 0);
  1485. tcursor(CURSOR_SAVE);
  1486. tclearregion(0, 0, term.col-1, term.row-1);
  1487. tswapscreen();
  1488. }
  1489. }
  1490. void
  1491. tnew(int col, int row)
  1492. {
  1493. term = (Term){ .c = { .attr = { .fg = defaultfg, .bg = defaultbg } } };
  1494. tresize(col, row);
  1495. term.numlock = 1;
  1496. treset();
  1497. }
  1498. void
  1499. tswapscreen(void)
  1500. {
  1501. Line *tmp = term.line;
  1502. term.line = term.alt;
  1503. term.alt = tmp;
  1504. term.mode ^= MODE_ALTSCREEN;
  1505. tfulldirt();
  1506. }
  1507. void
  1508. tscrolldown(int orig, int n)
  1509. {
  1510. int i;
  1511. Line temp;
  1512. LIMIT(n, 0, term.bot-orig+1);
  1513. tsetdirt(orig, term.bot-n);
  1514. tclearregion(0, term.bot-n+1, term.col-1, term.bot);
  1515. for (i = term.bot; i >= orig+n; i--) {
  1516. temp = term.line[i];
  1517. term.line[i] = term.line[i-n];
  1518. term.line[i-n] = temp;
  1519. }
  1520. selscroll(orig, n);
  1521. }
  1522. void
  1523. tscrollup(int orig, int n)
  1524. {
  1525. int i;
  1526. Line temp;
  1527. LIMIT(n, 0, term.bot-orig+1);
  1528. tclearregion(0, orig, term.col-1, orig+n-1);
  1529. tsetdirt(orig+n, term.bot);
  1530. for (i = orig; i <= term.bot-n; i++) {
  1531. temp = term.line[i];
  1532. term.line[i] = term.line[i+n];
  1533. term.line[i+n] = temp;
  1534. }
  1535. selscroll(orig, -n);
  1536. }
  1537. void
  1538. selscroll(int orig, int n)
  1539. {
  1540. if (sel.ob.x == -1)
  1541. return;
  1542. if (BETWEEN(sel.ob.y, orig, term.bot) || BETWEEN(sel.oe.y, orig, term.bot)) {
  1543. if ((sel.ob.y += n) > term.bot || (sel.oe.y += n) < term.top) {
  1544. selclear(NULL);
  1545. return;
  1546. }
  1547. if (sel.type == SEL_RECTANGULAR) {
  1548. if (sel.ob.y < term.top)
  1549. sel.ob.y = term.top;
  1550. if (sel.oe.y > term.bot)
  1551. sel.oe.y = term.bot;
  1552. } else {
  1553. if (sel.ob.y < term.top) {
  1554. sel.ob.y = term.top;
  1555. sel.ob.x = 0;
  1556. }
  1557. if (sel.oe.y > term.bot) {
  1558. sel.oe.y = term.bot;
  1559. sel.oe.x = term.col;
  1560. }
  1561. }
  1562. selnormalize();
  1563. }
  1564. }
  1565. void
  1566. tnewline(int first_col)
  1567. {
  1568. int y = term.c.y;
  1569. if (y == term.bot) {
  1570. tscrollup(term.top, 1);
  1571. } else {
  1572. y++;
  1573. }
  1574. tmoveto(first_col ? 0 : term.c.x, y);
  1575. }
  1576. void
  1577. csiparse(void)
  1578. {
  1579. char *p = csiescseq.buf, *np;
  1580. long int v;
  1581. csiescseq.narg = 0;
  1582. if (*p == '?') {
  1583. csiescseq.priv = 1;
  1584. p++;
  1585. }
  1586. csiescseq.buf[csiescseq.len] = '\0';
  1587. while (p < csiescseq.buf+csiescseq.len) {
  1588. np = NULL;
  1589. v = strtol(p, &np, 10);
  1590. if (np == p)
  1591. v = 0;
  1592. if (v == LONG_MAX || v == LONG_MIN)
  1593. v = -1;
  1594. csiescseq.arg[csiescseq.narg++] = v;
  1595. p = np;
  1596. if (*p != ';' || csiescseq.narg == ESC_ARG_SIZ)
  1597. break;
  1598. p++;
  1599. }
  1600. csiescseq.mode[0] = *p++;
  1601. csiescseq.mode[1] = (p < csiescseq.buf+csiescseq.len) ? *p : '\0';
  1602. }
  1603. /* for absolute user moves, when decom is set */
  1604. void
  1605. tmoveato(int x, int y)
  1606. {
  1607. tmoveto(x, y + ((term.c.state & CURSOR_ORIGIN) ? term.top: 0));
  1608. }
  1609. void
  1610. tmoveto(int x, int y)
  1611. {
  1612. int miny, maxy;
  1613. if (term.c.state & CURSOR_ORIGIN) {
  1614. miny = term.top;
  1615. maxy = term.bot;
  1616. } else {
  1617. miny = 0;
  1618. maxy = term.row - 1;
  1619. }
  1620. term.c.state &= ~CURSOR_WRAPNEXT;
  1621. term.c.x = LIMIT(x, 0, term.col-1);
  1622. term.c.y = LIMIT(y, miny, maxy);
  1623. }
  1624. void
  1625. tsetchar(Rune u, Glyph *attr, int x, int y)
  1626. {
  1627. static char *vt100_0[62] = { /* 0x41 - 0x7e */
  1628. "", "", "", "", "", "", "", /* A - G */
  1629. 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
  1630. 0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
  1631. 0, 0, 0, 0, 0, 0, 0, " ", /* X - _ */
  1632. "", "", "", "", "", "", "°", "±", /* ` - g */
  1633. "", "", "", "", "", "", "", "", /* h - o */
  1634. "", "", "", "", "", "", "", "", /* p - w */
  1635. "", "", "", "π", "", "£", "·", /* x - ~ */
  1636. };
  1637. /*
  1638. * The table is proudly stolen from rxvt.
  1639. */
  1640. if (term.trantbl[term.charset] == CS_GRAPHIC0 &&
  1641. BETWEEN(u, 0x41, 0x7e) && vt100_0[u - 0x41])
  1642. utf8decode(vt100_0[u - 0x41], &u, UTF_SIZ);
  1643. if (term.line[y][x].mode & ATTR_WIDE) {
  1644. if (x+1 < term.col) {
  1645. term.line[y][x+1].u = ' ';
  1646. term.line[y][x+1].mode &= ~ATTR_WDUMMY;
  1647. }
  1648. } else if (term.line[y][x].mode & ATTR_WDUMMY) {
  1649. term.line[y][x-1].u = ' ';
  1650. term.line[y][x-1].mode &= ~ATTR_WIDE;
  1651. }
  1652. term.dirty[y] = 1;
  1653. term.line[y][x] = *attr;
  1654. term.line[y][x].u = u;
  1655. }
  1656. void
  1657. tclearregion(int x1, int y1, int x2, int y2)
  1658. {
  1659. int x, y, temp;
  1660. Glyph *gp;
  1661. if (x1 > x2)
  1662. temp = x1, x1 = x2, x2 = temp;
  1663. if (y1 > y2)
  1664. temp = y1, y1 = y2, y2 = temp;
  1665. LIMIT(x1, 0, term.col-1);
  1666. LIMIT(x2, 0, term.col-1);
  1667. LIMIT(y1, 0, term.row-1);
  1668. LIMIT(y2, 0, term.row-1);
  1669. for (y = y1; y <= y2; y++) {
  1670. term.dirty[y] = 1;
  1671. for (x = x1; x <= x2; x++) {
  1672. gp = &term.line[y][x];
  1673. if (selected(x, y))
  1674. selclear(NULL);
  1675. gp->fg = term.c.attr.fg;
  1676. gp->bg = term.c.attr.bg;
  1677. gp->mode = 0;
  1678. gp->u = ' ';
  1679. }
  1680. }
  1681. }
  1682. void
  1683. tdeletechar(int n)
  1684. {
  1685. int dst, src, size;
  1686. Glyph *line;
  1687. LIMIT(n, 0, term.col - term.c.x);
  1688. dst = term.c.x;
  1689. src = term.c.x + n;
  1690. size = term.col - src;
  1691. line = term.line[term.c.y];
  1692. memmove(&line[dst], &line[src], size * sizeof(Glyph));
  1693. tclearregion(term.col-n, term.c.y, term.col-1, term.c.y);
  1694. }
  1695. void
  1696. tinsertblank(int n)
  1697. {
  1698. int dst, src, size;
  1699. Glyph *line;
  1700. LIMIT(n, 0, term.col - term.c.x);
  1701. dst = term.c.x + n;
  1702. src = term.c.x;
  1703. size = term.col - dst;
  1704. line = term.line[term.c.y];
  1705. memmove(&line[dst], &line[src], size * sizeof(Glyph));
  1706. tclearregion(src, term.c.y, dst - 1, term.c.y);
  1707. }
  1708. void
  1709. tinsertblankline(int n)
  1710. {
  1711. if (BETWEEN(term.c.y, term.top, term.bot))
  1712. tscrolldown(term.c.y, n);
  1713. }
  1714. void
  1715. tdeleteline(int n)
  1716. {
  1717. if (BETWEEN(term.c.y, term.top, term.bot))
  1718. tscrollup(term.c.y, n);
  1719. }
  1720. int32_t
  1721. tdefcolor(int *attr, int *npar, int l)
  1722. {
  1723. int32_t idx = -1;
  1724. uint r, g, b;
  1725. switch (attr[*npar + 1]) {
  1726. case 2: /* direct color in RGB space */
  1727. if (*npar + 4 >= l) {
  1728. fprintf(stderr,
  1729. "erresc(38): Incorrect number of parameters (%d)\n",
  1730. *npar);
  1731. break;
  1732. }
  1733. r = attr[*npar + 2];
  1734. g = attr[*npar + 3];
  1735. b = attr[*npar + 4];
  1736. *npar += 4;
  1737. if (!BETWEEN(r, 0, 255) || !BETWEEN(g, 0, 255) || !BETWEEN(b, 0, 255))
  1738. fprintf(stderr, "erresc: bad rgb color (%u,%u,%u)\n",
  1739. r, g, b);
  1740. else
  1741. idx = TRUECOLOR(r, g, b);
  1742. break;
  1743. case 5: /* indexed color */
  1744. if (*npar + 2 >= l) {
  1745. fprintf(stderr,
  1746. "erresc(38): Incorrect number of parameters (%d)\n",
  1747. *npar);
  1748. break;
  1749. }
  1750. *npar += 2;
  1751. if (!BETWEEN(attr[*npar], 0, 255))
  1752. fprintf(stderr, "erresc: bad fgcolor %d\n", attr[*npar]);
  1753. else
  1754. idx = attr[*npar];
  1755. break;
  1756. case 0: /* implemented defined (only foreground) */
  1757. case 1: /* transparent */
  1758. case 3: /* direct color in CMY space */
  1759. case 4: /* direct color in CMYK space */
  1760. default:
  1761. fprintf(stderr,
  1762. "erresc(38): gfx attr %d unknown\n", attr[*npar]);
  1763. break;
  1764. }
  1765. return idx;
  1766. }
  1767. void
  1768. tsetattr(int *attr, int l)
  1769. {
  1770. int i;
  1771. int32_t idx;
  1772. for (i = 0; i < l; i++) {
  1773. switch (attr[i]) {
  1774. case 0:
  1775. term.c.attr.mode &= ~(
  1776. ATTR_BOLD |
  1777. ATTR_FAINT |
  1778. ATTR_ITALIC |
  1779. ATTR_UNDERLINE |
  1780. ATTR_BLINK |
  1781. ATTR_REVERSE |
  1782. ATTR_INVISIBLE |
  1783. ATTR_STRUCK );
  1784. term.c.attr.fg = defaultfg;
  1785. term.c.attr.bg = defaultbg;
  1786. break;
  1787. case 1:
  1788. term.c.attr.mode |= ATTR_BOLD;
  1789. break;
  1790. case 2:
  1791. term.c.attr.mode |= ATTR_FAINT;
  1792. break;
  1793. case 3:
  1794. term.c.attr.mode |= ATTR_ITALIC;
  1795. break;
  1796. case 4:
  1797. term.c.attr.mode |= ATTR_UNDERLINE;
  1798. break;
  1799. case 5: /* slow blink */
  1800. /* FALLTHROUGH */
  1801. case 6: /* rapid blink */
  1802. term.c.attr.mode |= ATTR_BLINK;
  1803. break;
  1804. case 7:
  1805. term.c.attr.mode |= ATTR_REVERSE;
  1806. break;
  1807. case 8:
  1808. term.c.attr.mode |= ATTR_INVISIBLE;
  1809. break;
  1810. case 9:
  1811. term.c.attr.mode |= ATTR_STRUCK;
  1812. break;
  1813. case 22:
  1814. term.c.attr.mode &= ~(ATTR_BOLD | ATTR_FAINT);
  1815. break;
  1816. case 23:
  1817. term.c.attr.mode &= ~ATTR_ITALIC;
  1818. break;
  1819. case 24:
  1820. term.c.attr.mode &= ~ATTR_UNDERLINE;
  1821. break;
  1822. case 25:
  1823. term.c.attr.mode &= ~ATTR_BLINK;
  1824. break;
  1825. case 27:
  1826. term.c.attr.mode &= ~ATTR_REVERSE;
  1827. break;
  1828. case 28:
  1829. term.c.attr.mode &= ~ATTR_INVISIBLE;
  1830. break;
  1831. case 29:
  1832. term.c.attr.mode &= ~ATTR_STRUCK;
  1833. break;
  1834. case 38:
  1835. if ((idx = tdefcolor(attr, &i, l)) >= 0)
  1836. term.c.attr.fg = idx;
  1837. break;
  1838. case 39:
  1839. term.c.attr.fg = defaultfg;
  1840. break;
  1841. case 48:
  1842. if ((idx = tdefcolor(attr, &i, l)) >= 0)
  1843. term.c.attr.bg = idx;
  1844. break;
  1845. case 49:
  1846. term.c.attr.bg = defaultbg;
  1847. break;
  1848. default:
  1849. if (BETWEEN(attr[i], 30, 37)) {
  1850. term.c.attr.fg = attr[i] - 30;
  1851. } else if (BETWEEN(attr[i], 40, 47)) {
  1852. term.c.attr.bg = attr[i] - 40;
  1853. } else if (BETWEEN(attr[i], 90, 97)) {
  1854. term.c.attr.fg = attr[i] - 90 + 8;
  1855. } else if (BETWEEN(attr[i], 100, 107)) {
  1856. term.c.attr.bg = attr[i] - 100 + 8;
  1857. } else {
  1858. fprintf(stderr,
  1859. "erresc(default): gfx attr %d unknown\n",
  1860. attr[i]), csidump();
  1861. }
  1862. break;
  1863. }
  1864. }
  1865. }
  1866. void
  1867. tsetscroll(int t, int b)
  1868. {
  1869. int temp;
  1870. LIMIT(t, 0, term.row-1);
  1871. LIMIT(b, 0, term.row-1);
  1872. if (t > b) {
  1873. temp = t;
  1874. t = b;
  1875. b = temp;
  1876. }
  1877. term.top = t;
  1878. term.bot = b;
  1879. }
  1880. void
  1881. tsetmode(int priv, int set, int *args, int narg)
  1882. {
  1883. int *lim, mode;
  1884. int alt;
  1885. for (lim = args + narg; args < lim; ++args) {
  1886. if (priv) {
  1887. switch (*args) {
  1888. case 1: /* DECCKM -- Cursor key */
  1889. MODBIT(term.mode, set, MODE_APPCURSOR);
  1890. break;
  1891. case 5: /* DECSCNM -- Reverse video */
  1892. mode = term.mode;
  1893. MODBIT(term.mode, set, MODE_REVERSE);
  1894. if (mode != term.mode)
  1895. redraw();
  1896. break;
  1897. case 6: /* DECOM -- Origin */
  1898. MODBIT(term.c.state, set, CURSOR_ORIGIN);
  1899. tmoveato(0, 0);
  1900. break;
  1901. case 7: /* DECAWM -- Auto wrap */
  1902. MODBIT(term.mode, set, MODE_WRAP);
  1903. break;
  1904. case 0: /* Error (IGNORED) */
  1905. case 2: /* DECANM -- ANSI/VT52 (IGNORED) */
  1906. case 3: /* DECCOLM -- Column (IGNORED) */
  1907. case 4: /* DECSCLM -- Scroll (IGNORED) */
  1908. case 8: /* DECARM -- Auto repeat (IGNORED) */
  1909. case 18: /* DECPFF -- Printer feed (IGNORED) */
  1910. case 19: /* DECPEX -- Printer extent (IGNORED) */
  1911. case 42: /* DECNRCM -- National characters (IGNORED) */
  1912. case 12: /* att610 -- Start blinking cursor (IGNORED) */
  1913. break;
  1914. case 25: /* DECTCEM -- Text Cursor Enable Mode */
  1915. MODBIT(term.mode, !set, MODE_HIDE);
  1916. break;
  1917. case 9: /* X10 mouse compatibility mode */
  1918. xsetpointermotion(0);
  1919. MODBIT(term.mode, 0, MODE_MOUSE);
  1920. MODBIT(term.mode, set, MODE_MOUSEX10);
  1921. break;
  1922. case 1000: /* 1000: report button press */
  1923. xsetpointermotion(0);
  1924. MODBIT(term.mode, 0, MODE_MOUSE);
  1925. MODBIT(term.mode, set, MODE_MOUSEBTN);
  1926. break;
  1927. case 1002: /* 1002: report motion on button press */
  1928. xsetpointermotion(0);
  1929. MODBIT(term.mode, 0, MODE_MOUSE);
  1930. MODBIT(term.mode, set, MODE_MOUSEMOTION);
  1931. break;
  1932. case 1003: /* 1003: enable all mouse motions */
  1933. xsetpointermotion(set);
  1934. MODBIT(term.mode, 0, MODE_MOUSE);
  1935. MODBIT(term.mode, set, MODE_MOUSEMANY);
  1936. break;
  1937. case 1004: /* 1004: send focus events to tty */
  1938. MODBIT(term.mode, set, MODE_FOCUS);
  1939. break;
  1940. case 1006: /* 1006: extended reporting mode */
  1941. MODBIT(term.mode, set, MODE_MOUSESGR);
  1942. break;
  1943. case 1034:
  1944. MODBIT(term.mode, set, MODE_8BIT);
  1945. break;
  1946. case 1049: /* swap screen & set/restore cursor as xterm */
  1947. if (!allowaltscreen)
  1948. break;
  1949. tcursor((set) ? CURSOR_SAVE : CURSOR_LOAD);
  1950. /* FALLTHROUGH */
  1951. case 47: /* swap screen */
  1952. case 1047:
  1953. if (!allowaltscreen)
  1954. break;
  1955. alt = IS_SET(MODE_ALTSCREEN);
  1956. if (alt) {
  1957. tclearregion(0, 0, term.col-1,
  1958. term.row-1);
  1959. }
  1960. if (set ^ alt) /* set is always 1 or 0 */
  1961. tswapscreen();
  1962. if (*args != 1049)
  1963. break;
  1964. /* FALLTHROUGH */
  1965. case 1048:
  1966. tcursor((set) ? CURSOR_SAVE : CURSOR_LOAD);
  1967. break;
  1968. case 2004: /* 2004: bracketed paste mode */
  1969. MODBIT(term.mode, set, MODE_BRCKTPASTE);
  1970. break;
  1971. /* Not implemented mouse modes. See comments there. */
  1972. case 1001: /* mouse highlight mode; can hang the
  1973. terminal by design when implemented. */
  1974. case 1005: /* UTF-8 mouse mode; will confuse
  1975. applications not supporting UTF-8
  1976. and luit. */
  1977. case 1015: /* urxvt mangled mouse mode; incompatible
  1978. and can be mistaken for other control
  1979. codes. */
  1980. default:
  1981. fprintf(stderr,
  1982. "erresc: unknown private set/reset mode %d\n",
  1983. *args);
  1984. break;
  1985. }
  1986. } else {
  1987. switch (*args) {
  1988. case 0: /* Error (IGNORED) */
  1989. break;
  1990. case 2: /* KAM -- keyboard action */
  1991. MODBIT(term.mode, set, MODE_KBDLOCK);
  1992. break;
  1993. case 4: /* IRM -- Insertion-replacement */
  1994. MODBIT(term.mode, set, MODE_INSERT);
  1995. break;
  1996. case 12: /* SRM -- Send/Receive */
  1997. MODBIT(term.mode, !set, MODE_ECHO);
  1998. break;
  1999. case 20: /* LNM -- Linefeed/new line */
  2000. MODBIT(term.mode, set, MODE_CRLF);
  2001. break;
  2002. default:
  2003. fprintf(stderr,
  2004. "erresc: unknown set/reset mode %d\n",
  2005. *args);
  2006. break;
  2007. }
  2008. }
  2009. }
  2010. }
  2011. void
  2012. csihandle(void)
  2013. {
  2014. char buf[40];
  2015. int len;
  2016. switch (csiescseq.mode[0]) {
  2017. default:
  2018. unknown:
  2019. fprintf(stderr, "erresc: unknown csi ");
  2020. csidump();
  2021. /* die(""); */
  2022. break;
  2023. case '@': /* ICH -- Insert <n> blank char */
  2024. DEFAULT(csiescseq.arg[0], 1);
  2025. tinsertblank(csiescseq.arg[0]);
  2026. break;
  2027. case 'A': /* CUU -- Cursor <n> Up */
  2028. DEFAULT(csiescseq.arg[0], 1);
  2029. tmoveto(term.c.x, term.c.y-csiescseq.arg[0]);
  2030. break;
  2031. case 'B': /* CUD -- Cursor <n> Down */
  2032. case 'e': /* VPR --Cursor <n> Down */
  2033. DEFAULT(csiescseq.arg[0], 1);
  2034. tmoveto(term.c.x, term.c.y+csiescseq.arg[0]);
  2035. break;
  2036. case 'i': /* MC -- Media Copy */
  2037. switch (csiescseq.arg[0]) {
  2038. case 0:
  2039. tdump();
  2040. break;
  2041. case 1:
  2042. tdumpline(term.c.y);
  2043. break;
  2044. case 2:
  2045. tdumpsel();
  2046. break;
  2047. case 4:
  2048. term.mode &= ~MODE_PRINT;
  2049. break;
  2050. case 5:
  2051. term.mode |= MODE_PRINT;
  2052. break;
  2053. }
  2054. break;
  2055. case 'c': /* DA -- Device Attributes */
  2056. if (csiescseq.arg[0] == 0)
  2057. ttywrite(vtiden, sizeof(vtiden) - 1);
  2058. break;
  2059. case 'C': /* CUF -- Cursor <n> Forward */
  2060. case 'a': /* HPR -- Cursor <n> Forward */
  2061. DEFAULT(csiescseq.arg[0], 1);
  2062. tmoveto(term.c.x+csiescseq.arg[0], term.c.y);
  2063. break;
  2064. case 'D': /* CUB -- Cursor <n> Backward */
  2065. DEFAULT(csiescseq.arg[0], 1);
  2066. tmoveto(term.c.x-csiescseq.arg[0], term.c.y);
  2067. break;
  2068. case 'E': /* CNL -- Cursor <n> Down and first col */
  2069. DEFAULT(csiescseq.arg[0], 1);
  2070. tmoveto(0, term.c.y+csiescseq.arg[0]);
  2071. break;
  2072. case 'F': /* CPL -- Cursor <n> Up and first col */
  2073. DEFAULT(csiescseq.arg[0], 1);
  2074. tmoveto(0, term.c.y-csiescseq.arg[0]);
  2075. break;
  2076. case 'g': /* TBC -- Tabulation clear */
  2077. switch (csiescseq.arg[0]) {
  2078. case 0: /* clear current tab stop */
  2079. term.tabs[term.c.x] = 0;
  2080. break;
  2081. case 3: /* clear all the tabs */
  2082. memset(term.tabs, 0, term.col * sizeof(*term.tabs));
  2083. break;
  2084. default:
  2085. goto unknown;
  2086. }
  2087. break;
  2088. case 'G': /* CHA -- Move to <col> */
  2089. case '`': /* HPA */
  2090. DEFAULT(csiescseq.arg[0], 1);
  2091. tmoveto(csiescseq.arg[0]-1, term.c.y);
  2092. break;
  2093. case 'H': /* CUP -- Move to <row> <col> */
  2094. case 'f': /* HVP */
  2095. DEFAULT(csiescseq.arg[0], 1);
  2096. DEFAULT(csiescseq.arg[1], 1);
  2097. tmoveato(csiescseq.arg[1]-1, csiescseq.arg[0]-1);
  2098. break;
  2099. case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
  2100. DEFAULT(csiescseq.arg[0], 1);
  2101. tputtab(csiescseq.arg[0]);
  2102. break;
  2103. case 'J': /* ED -- Clear screen */
  2104. selclear(NULL);
  2105. switch (csiescseq.arg[0]) {
  2106. case 0: /* below */
  2107. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  2108. if (term.c.y < term.row-1) {
  2109. tclearregion(0, term.c.y+1, term.col-1,
  2110. term.row-1);
  2111. }
  2112. break;
  2113. case 1: /* above */
  2114. if (term.c.y > 1)
  2115. tclearregion(0, 0, term.col-1, term.c.y-1);
  2116. tclearregion(0, term.c.y, term.c.x, term.c.y);
  2117. break;
  2118. case 2: /* all */
  2119. tclearregion(0, 0, term.col-1, term.row-1);
  2120. break;
  2121. default:
  2122. goto unknown;
  2123. }
  2124. break;
  2125. case 'K': /* EL -- Clear line */
  2126. switch (csiescseq.arg[0]) {
  2127. case 0: /* right */
  2128. tclearregion(term.c.x, term.c.y, term.col-1,
  2129. term.c.y);
  2130. break;
  2131. case 1: /* left */
  2132. tclearregion(0, term.c.y, term.c.x, term.c.y);
  2133. break;
  2134. case 2: /* all */
  2135. tclearregion(0, term.c.y, term.col-1, term.c.y);
  2136. break;
  2137. }
  2138. break;
  2139. case 'S': /* SU -- Scroll <n> line up */
  2140. DEFAULT(csiescseq.arg[0], 1);
  2141. tscrollup(term.top, csiescseq.arg[0]);
  2142. break;
  2143. case 'T': /* SD -- Scroll <n> line down */
  2144. DEFAULT(csiescseq.arg[0], 1);
  2145. tscrolldown(term.top, csiescseq.arg[0]);
  2146. break;
  2147. case 'L': /* IL -- Insert <n> blank lines */
  2148. DEFAULT(csiescseq.arg[0], 1);
  2149. tinsertblankline(csiescseq.arg[0]);
  2150. break;
  2151. case 'l': /* RM -- Reset Mode */
  2152. tsetmode(csiescseq.priv, 0, csiescseq.arg, csiescseq.narg);
  2153. break;
  2154. case 'M': /* DL -- Delete <n> lines */
  2155. DEFAULT(csiescseq.arg[0], 1);
  2156. tdeleteline(csiescseq.arg[0]);
  2157. break;
  2158. case 'X': /* ECH -- Erase <n> char */
  2159. DEFAULT(csiescseq.arg[0], 1);
  2160. tclearregion(term.c.x, term.c.y,
  2161. term.c.x + csiescseq.arg[0] - 1, term.c.y);
  2162. break;
  2163. case 'P': /* DCH -- Delete <n> char */
  2164. DEFAULT(csiescseq.arg[0], 1);
  2165. tdeletechar(csiescseq.arg[0]);
  2166. break;
  2167. case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
  2168. DEFAULT(csiescseq.arg[0], 1);
  2169. tputtab(-csiescseq.arg[0]);
  2170. break;
  2171. case 'd': /* VPA -- Move to <row> */
  2172. DEFAULT(csiescseq.arg[0], 1);
  2173. tmoveato(term.c.x, csiescseq.arg[0]-1);
  2174. break;
  2175. case 'h': /* SM -- Set terminal mode */
  2176. tsetmode(csiescseq.priv, 1, csiescseq.arg, csiescseq.narg);
  2177. break;
  2178. case 'm': /* SGR -- Terminal attribute (color) */
  2179. tsetattr(csiescseq.arg, csiescseq.narg);
  2180. break;
  2181. case 'n': /* DSR – Device Status Report (cursor position) */
  2182. if (csiescseq.arg[0] == 6) {
  2183. len = snprintf(buf, sizeof(buf),"\033[%i;%iR",
  2184. term.c.y+1, term.c.x+1);
  2185. ttywrite(buf, len);
  2186. }
  2187. break;
  2188. case 'r': /* DECSTBM -- Set Scrolling Region */
  2189. if (csiescseq.priv) {
  2190. goto unknown;
  2191. } else {
  2192. DEFAULT(csiescseq.arg[0], 1);
  2193. DEFAULT(csiescseq.arg[1], term.row);
  2194. tsetscroll(csiescseq.arg[0]-1, csiescseq.arg[1]-1);
  2195. tmoveato(0, 0);
  2196. }
  2197. break;
  2198. case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
  2199. tcursor(CURSOR_SAVE);
  2200. break;
  2201. case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
  2202. tcursor(CURSOR_LOAD);
  2203. break;
  2204. case ' ':
  2205. switch (csiescseq.mode[1]) {
  2206. case 'q': /* DECSCUSR -- Set Cursor Style */
  2207. DEFAULT(csiescseq.arg[0], 1);
  2208. if (!BETWEEN(csiescseq.arg[0], 0, 6)) {
  2209. goto unknown;
  2210. }
  2211. xw.cursor = csiescseq.arg[0];
  2212. break;
  2213. default:
  2214. goto unknown;
  2215. }
  2216. break;
  2217. }
  2218. }
  2219. void
  2220. csidump(void)
  2221. {
  2222. int i;
  2223. uint c;
  2224. fprintf(stderr, "ESC[");
  2225. for (i = 0; i < csiescseq.len; i++) {
  2226. c = csiescseq.buf[i] & 0xff;
  2227. if (isprint(c)) {
  2228. putc(c, stderr);
  2229. } else if (c == '\n') {
  2230. fprintf(stderr, "(\\n)");
  2231. } else if (c == '\r') {
  2232. fprintf(stderr, "(\\r)");
  2233. } else if (c == 0x1b) {
  2234. fprintf(stderr, "(\\e)");
  2235. } else {
  2236. fprintf(stderr, "(%02x)", c);
  2237. }
  2238. }
  2239. putc('\n', stderr);
  2240. }
  2241. void
  2242. csireset(void)
  2243. {
  2244. memset(&csiescseq, 0, sizeof(csiescseq));
  2245. }
  2246. void
  2247. strhandle(void)
  2248. {
  2249. char *p = NULL;
  2250. int j, narg, par;
  2251. term.esc &= ~(ESC_STR_END|ESC_STR);
  2252. strparse();
  2253. par = (narg = strescseq.narg) ? atoi(strescseq.args[0]) : 0;
  2254. switch (strescseq.type) {
  2255. case ']': /* OSC -- Operating System Command */
  2256. switch (par) {
  2257. case 0:
  2258. case 1:
  2259. case 2:
  2260. if (narg > 1)
  2261. xsettitle(strescseq.args[1]);
  2262. return;
  2263. case 4: /* color set */
  2264. if (narg < 3)
  2265. break;
  2266. p = strescseq.args[2];
  2267. /* FALLTHROUGH */
  2268. case 104: /* color reset, here p = NULL */
  2269. j = (narg > 1) ? atoi(strescseq.args[1]) : -1;
  2270. if (xsetcolorname(j, p)) {
  2271. fprintf(stderr, "erresc: invalid color %s\n", p);
  2272. } else {
  2273. /*
  2274. * TODO if defaultbg color is changed, borders
  2275. * are dirty
  2276. */
  2277. redraw();
  2278. }
  2279. return;
  2280. }
  2281. break;
  2282. case 'k': /* old title set compatibility */
  2283. xsettitle(strescseq.args[0]);
  2284. return;
  2285. case 'P': /* DCS -- Device Control String */
  2286. term.mode |= ESC_DCS;
  2287. case '_': /* APC -- Application Program Command */
  2288. case '^': /* PM -- Privacy Message */
  2289. return;
  2290. }
  2291. fprintf(stderr, "erresc: unknown str ");
  2292. strdump();
  2293. }
  2294. void
  2295. strparse(void)
  2296. {
  2297. int c;
  2298. char *p = strescseq.buf;
  2299. strescseq.narg = 0;
  2300. strescseq.buf[strescseq.len] = '\0';
  2301. if (*p == '\0')
  2302. return;
  2303. while (strescseq.narg < STR_ARG_SIZ) {
  2304. strescseq.args[strescseq.narg++] = p;
  2305. while ((c = *p) != ';' && c != '\0')
  2306. ++p;
  2307. if (c == '\0')
  2308. return;
  2309. *p++ = '\0';
  2310. }
  2311. }
  2312. void
  2313. strdump(void)
  2314. {
  2315. int i;
  2316. uint c;
  2317. fprintf(stderr, "ESC%c", strescseq.type);
  2318. for (i = 0; i < strescseq.len; i++) {
  2319. c = strescseq.buf[i] & 0xff;
  2320. if (c == '\0') {
  2321. putc('\n', stderr);
  2322. return;
  2323. } else if (isprint(c)) {
  2324. putc(c, stderr);
  2325. } else if (c == '\n') {
  2326. fprintf(stderr, "(\\n)");
  2327. } else if (c == '\r') {
  2328. fprintf(stderr, "(\\r)");
  2329. } else if (c == 0x1b) {
  2330. fprintf(stderr, "(\\e)");
  2331. } else {
  2332. fprintf(stderr, "(%02x)", c);
  2333. }
  2334. }
  2335. fprintf(stderr, "ESC\\\n");
  2336. }
  2337. void
  2338. strreset(void)
  2339. {
  2340. memset(&strescseq, 0, sizeof(strescseq));
  2341. }
  2342. void
  2343. sendbreak(const Arg *arg)
  2344. {
  2345. if (tcsendbreak(cmdfd, 0))
  2346. perror("Error sending break");
  2347. }
  2348. void
  2349. tprinter(char *s, size_t len)
  2350. {
  2351. if (iofd != -1 && xwrite(iofd, s, len) < 0) {
  2352. fprintf(stderr, "Error writing in %s:%s\n",
  2353. opt_io, strerror(errno));
  2354. close(iofd);
  2355. iofd = -1;
  2356. }
  2357. }
  2358. void
  2359. iso14755(const Arg *arg)
  2360. {
  2361. char cmd[sizeof(ISO14755CMD) + NUMMAXLEN(xw.win)];
  2362. FILE *p;
  2363. char *us, *e, codepoint[9], uc[UTF_SIZ];
  2364. unsigned long utf32;
  2365. snprintf(cmd, sizeof(cmd), ISO14755CMD, xw.win);
  2366. if (!(p = popen(cmd, "r")))
  2367. return;
  2368. us = fgets(codepoint, sizeof(codepoint), p);
  2369. pclose(p);
  2370. if (!us || *us == '\0' || *us == '-' || strlen(us) > 7)
  2371. return;
  2372. if ((utf32 = strtoul(us, &e, 16)) == ULONG_MAX ||
  2373. (*e != '\n' && *e != '\0'))
  2374. return;
  2375. ttysend(uc, utf8encode(utf32, uc));
  2376. }
  2377. void
  2378. toggleprinter(const Arg *arg)
  2379. {
  2380. term.mode ^= MODE_PRINT;
  2381. }
  2382. void
  2383. printscreen(const Arg *arg)
  2384. {
  2385. tdump();
  2386. }
  2387. void
  2388. printsel(const Arg *arg)
  2389. {
  2390. tdumpsel();
  2391. }
  2392. void
  2393. tdumpsel(void)
  2394. {
  2395. char *ptr;
  2396. if ((ptr = getsel())) {
  2397. tprinter(ptr, strlen(ptr));
  2398. free(ptr);
  2399. }
  2400. }
  2401. void
  2402. tdumpline(int n)
  2403. {
  2404. char buf[UTF_SIZ];
  2405. Glyph *bp, *end;
  2406. bp = &term.line[n][0];
  2407. end = &bp[MIN(tlinelen(n), term.col) - 1];
  2408. if (bp != end || bp->u != ' ') {
  2409. for ( ;bp <= end; ++bp)
  2410. tprinter(buf, utf8encode(bp->u, buf));
  2411. }
  2412. tprinter("\n", 1);
  2413. }
  2414. void
  2415. tdump(void)
  2416. {
  2417. int i;
  2418. for (i = 0; i < term.row; ++i)
  2419. tdumpline(i);
  2420. }
  2421. void
  2422. tputtab(int n)
  2423. {
  2424. uint x = term.c.x;
  2425. if (n > 0) {
  2426. while (x < term.col && n--)
  2427. for (++x; x < term.col && !term.tabs[x]; ++x)
  2428. /* nothing */ ;
  2429. } else if (n < 0) {
  2430. while (x > 0 && n++)
  2431. for (--x; x > 0 && !term.tabs[x]; --x)
  2432. /* nothing */ ;
  2433. }
  2434. term.c.x = LIMIT(x, 0, term.col-1);
  2435. }
  2436. void
  2437. techo(Rune u)
  2438. {
  2439. if (ISCONTROL(u)) { /* control code */
  2440. if (u & 0x80) {
  2441. u &= 0x7f;
  2442. tputc('^');
  2443. tputc('[');
  2444. } else if (u != '\n' && u != '\r' && u != '\t') {
  2445. u ^= 0x40;
  2446. tputc('^');
  2447. }
  2448. }
  2449. tputc(u);
  2450. }
  2451. void
  2452. tdefutf8(char ascii)
  2453. {
  2454. if (ascii == 'G')
  2455. term.mode |= MODE_UTF8;
  2456. else if (ascii == '@')
  2457. term.mode &= ~MODE_UTF8;
  2458. }
  2459. void
  2460. tdeftran(char ascii)
  2461. {
  2462. static char cs[] = "0B";
  2463. static int vcs[] = {CS_GRAPHIC0, CS_USA};
  2464. char *p;
  2465. if ((p = strchr(cs, ascii)) == NULL) {
  2466. fprintf(stderr, "esc unhandled charset: ESC ( %c\n", ascii);
  2467. } else {
  2468. term.trantbl[term.icharset] = vcs[p - cs];
  2469. }
  2470. }
  2471. void
  2472. tdectest(char c)
  2473. {
  2474. int x, y;
  2475. if (c == '8') { /* DEC screen alignment test. */
  2476. for (x = 0; x < term.col; ++x) {
  2477. for (y = 0; y < term.row; ++y)
  2478. tsetchar('E', &term.c.attr, x, y);
  2479. }
  2480. }
  2481. }
  2482. void
  2483. tstrsequence(uchar c)
  2484. {
  2485. strreset();
  2486. switch (c) {
  2487. case 0x90: /* DCS -- Device Control String */
  2488. c = 'P';
  2489. term.esc |= ESC_DCS;
  2490. break;
  2491. case 0x9f: /* APC -- Application Program Command */
  2492. c = '_';
  2493. break;
  2494. case 0x9e: /* PM -- Privacy Message */
  2495. c = '^';
  2496. break;
  2497. case 0x9d: /* OSC -- Operating System Command */
  2498. c = ']';
  2499. break;
  2500. }
  2501. strescseq.type = c;
  2502. term.esc |= ESC_STR;
  2503. }
  2504. void
  2505. tcontrolcode(uchar ascii)
  2506. {
  2507. switch (ascii) {
  2508. case '\t': /* HT */
  2509. tputtab(1);
  2510. return;
  2511. case '\b': /* BS */
  2512. tmoveto(term.c.x-1, term.c.y);
  2513. return;
  2514. case '\r': /* CR */
  2515. tmoveto(0, term.c.y);
  2516. return;
  2517. case '\f': /* LF */
  2518. case '\v': /* VT */
  2519. case '\n': /* LF */
  2520. /* go to first col if the mode is set */
  2521. tnewline(IS_SET(MODE_CRLF));
  2522. return;
  2523. case '\a': /* BEL */
  2524. if (term.esc & ESC_STR_END) {
  2525. /* backwards compatibility to xterm */
  2526. strhandle();
  2527. } else {
  2528. if (!(xw.state & WIN_FOCUSED))
  2529. xseturgency(1);
  2530. if (bellvolume)
  2531. XkbBell(xw.dpy, xw.win, bellvolume, (Atom)NULL);
  2532. }
  2533. break;
  2534. case '\033': /* ESC */
  2535. csireset();
  2536. term.esc &= ~(ESC_CSI|ESC_ALTCHARSET|ESC_TEST);
  2537. term.esc |= ESC_START;
  2538. return;
  2539. case '\016': /* SO (LS1 -- Locking shift 1) */
  2540. case '\017': /* SI (LS0 -- Locking shift 0) */
  2541. term.charset = 1 - (ascii - '\016');
  2542. return;
  2543. case '\032': /* SUB */
  2544. tsetchar('?', &term.c.attr, term.c.x, term.c.y);
  2545. case '\030': /* CAN */
  2546. csireset();
  2547. break;
  2548. case '\005': /* ENQ (IGNORED) */
  2549. case '\000': /* NUL (IGNORED) */
  2550. case '\021': /* XON (IGNORED) */
  2551. case '\023': /* XOFF (IGNORED) */
  2552. case 0177: /* DEL (IGNORED) */
  2553. return;
  2554. case 0x80: /* TODO: PAD */
  2555. case 0x81: /* TODO: HOP */
  2556. case 0x82: /* TODO: BPH */
  2557. case 0x83: /* TODO: NBH */
  2558. case 0x84: /* TODO: IND */
  2559. break;
  2560. case 0x85: /* NEL -- Next line */
  2561. tnewline(1); /* always go to first col */
  2562. break;
  2563. case 0x86: /* TODO: SSA */
  2564. case 0x87: /* TODO: ESA */
  2565. break;
  2566. case 0x88: /* HTS -- Horizontal tab stop */
  2567. term.tabs[term.c.x] = 1;
  2568. break;
  2569. case 0x89: /* TODO: HTJ */
  2570. case 0x8a: /* TODO: VTS */
  2571. case 0x8b: /* TODO: PLD */
  2572. case 0x8c: /* TODO: PLU */
  2573. case 0x8d: /* TODO: RI */
  2574. case 0x8e: /* TODO: SS2 */
  2575. case 0x8f: /* TODO: SS3 */
  2576. case 0x91: /* TODO: PU1 */
  2577. case 0x92: /* TODO: PU2 */
  2578. case 0x93: /* TODO: STS */
  2579. case 0x94: /* TODO: CCH */
  2580. case 0x95: /* TODO: MW */
  2581. case 0x96: /* TODO: SPA */
  2582. case 0x97: /* TODO: EPA */
  2583. case 0x98: /* TODO: SOS */
  2584. case 0x99: /* TODO: SGCI */
  2585. break;
  2586. case 0x9a: /* DECID -- Identify Terminal */
  2587. ttywrite(vtiden, sizeof(vtiden) - 1);
  2588. break;
  2589. case 0x9b: /* TODO: CSI */
  2590. case 0x9c: /* TODO: ST */
  2591. break;
  2592. case 0x90: /* DCS -- Device Control String */
  2593. case 0x9d: /* OSC -- Operating System Command */
  2594. case 0x9e: /* PM -- Privacy Message */
  2595. case 0x9f: /* APC -- Application Program Command */
  2596. tstrsequence(ascii);
  2597. return;
  2598. }
  2599. /* only CAN, SUB, \a and C1 chars interrupt a sequence */
  2600. term.esc &= ~(ESC_STR_END|ESC_STR);
  2601. }
  2602. /*
  2603. * returns 1 when the sequence is finished and it hasn't to read
  2604. * more characters for this sequence, otherwise 0
  2605. */
  2606. int
  2607. eschandle(uchar ascii)
  2608. {
  2609. switch (ascii) {
  2610. case '[':
  2611. term.esc |= ESC_CSI;
  2612. return 0;
  2613. case '#':
  2614. term.esc |= ESC_TEST;
  2615. return 0;
  2616. case '%':
  2617. term.esc |= ESC_UTF8;
  2618. return 0;
  2619. case 'P': /* DCS -- Device Control String */
  2620. case '_': /* APC -- Application Program Command */
  2621. case '^': /* PM -- Privacy Message */
  2622. case ']': /* OSC -- Operating System Command */
  2623. case 'k': /* old title set compatibility */
  2624. tstrsequence(ascii);
  2625. return 0;
  2626. case 'n': /* LS2 -- Locking shift 2 */
  2627. case 'o': /* LS3 -- Locking shift 3 */
  2628. term.charset = 2 + (ascii - 'n');
  2629. break;
  2630. case '(': /* GZD4 -- set primary charset G0 */
  2631. case ')': /* G1D4 -- set secondary charset G1 */
  2632. case '*': /* G2D4 -- set tertiary charset G2 */
  2633. case '+': /* G3D4 -- set quaternary charset G3 */
  2634. term.icharset = ascii - '(';
  2635. term.esc |= ESC_ALTCHARSET;
  2636. return 0;
  2637. case 'D': /* IND -- Linefeed */
  2638. if (term.c.y == term.bot) {
  2639. tscrollup(term.top, 1);
  2640. } else {
  2641. tmoveto(term.c.x, term.c.y+1);
  2642. }
  2643. break;
  2644. case 'E': /* NEL -- Next line */
  2645. tnewline(1); /* always go to first col */
  2646. break;
  2647. case 'H': /* HTS -- Horizontal tab stop */
  2648. term.tabs[term.c.x] = 1;
  2649. break;
  2650. case 'M': /* RI -- Reverse index */
  2651. if (term.c.y == term.top) {
  2652. tscrolldown(term.top, 1);
  2653. } else {
  2654. tmoveto(term.c.x, term.c.y-1);
  2655. }
  2656. break;
  2657. case 'Z': /* DECID -- Identify Terminal */
  2658. ttywrite(vtiden, sizeof(vtiden) - 1);
  2659. break;
  2660. case 'c': /* RIS -- Reset to inital state */
  2661. treset();
  2662. xresettitle();
  2663. xloadcols();
  2664. break;
  2665. case '=': /* DECPAM -- Application keypad */
  2666. term.mode |= MODE_APPKEYPAD;
  2667. break;
  2668. case '>': /* DECPNM -- Normal keypad */
  2669. term.mode &= ~MODE_APPKEYPAD;
  2670. break;
  2671. case '7': /* DECSC -- Save Cursor */
  2672. tcursor(CURSOR_SAVE);
  2673. break;
  2674. case '8': /* DECRC -- Restore Cursor */
  2675. tcursor(CURSOR_LOAD);
  2676. break;
  2677. case '\\': /* ST -- String Terminator */
  2678. if (term.esc & ESC_STR_END)
  2679. strhandle();
  2680. break;
  2681. default:
  2682. fprintf(stderr, "erresc: unknown sequence ESC 0x%02X '%c'\n",
  2683. (uchar) ascii, isprint(ascii)? ascii:'.');
  2684. break;
  2685. }
  2686. return 1;
  2687. }
  2688. void
  2689. tputc(Rune u)
  2690. {
  2691. char c[UTF_SIZ];
  2692. int control;
  2693. int width, len;
  2694. Glyph *gp;
  2695. control = ISCONTROL(u);
  2696. if (!IS_SET(MODE_UTF8) && !IS_SET(MODE_SIXEL)) {
  2697. c[0] = u;
  2698. width = len = 1;
  2699. } else {
  2700. len = utf8encode(u, c);
  2701. if (!control && (width = wcwidth(u)) == -1) {
  2702. memcpy(c, "\357\277\275", 4); /* UTF_INVALID */
  2703. width = 1;
  2704. }
  2705. }
  2706. if (IS_SET(MODE_PRINT))
  2707. tprinter(c, len);
  2708. /*
  2709. * STR sequence must be checked before anything else
  2710. * because it uses all following characters until it
  2711. * receives a ESC, a SUB, a ST or any other C1 control
  2712. * character.
  2713. */
  2714. if (term.esc & ESC_STR) {
  2715. if (u == '\a' || u == 030 || u == 032 || u == 033 ||
  2716. ISCONTROLC1(u)) {
  2717. term.esc &= ~(ESC_START|ESC_STR|ESC_DCS);
  2718. if (IS_SET(MODE_SIXEL)) {
  2719. /* TODO: render sixel */;
  2720. term.mode &= ~MODE_SIXEL;
  2721. return;
  2722. }
  2723. term.esc |= ESC_STR_END;
  2724. goto check_control_code;
  2725. }
  2726. if (IS_SET(MODE_SIXEL)) {
  2727. /* TODO: implement sixel mode */
  2728. return;
  2729. }
  2730. if (term.esc&ESC_DCS && strescseq.len == 0 && u == 'q')
  2731. term.mode |= MODE_SIXEL;
  2732. if (strescseq.len+len >= sizeof(strescseq.buf)-1) {
  2733. /*
  2734. * Here is a bug in terminals. If the user never sends
  2735. * some code to stop the str or esc command, then st
  2736. * will stop responding. But this is better than
  2737. * silently failing with unknown characters. At least
  2738. * then users will report back.
  2739. *
  2740. * In the case users ever get fixed, here is the code:
  2741. */
  2742. /*
  2743. * term.esc = 0;
  2744. * strhandle();
  2745. */
  2746. return;
  2747. }
  2748. memmove(&strescseq.buf[strescseq.len], c, len);
  2749. strescseq.len += len;
  2750. return;
  2751. }
  2752. check_control_code:
  2753. /*
  2754. * Actions of control codes must be performed as soon they arrive
  2755. * because they can be embedded inside a control sequence, and
  2756. * they must not cause conflicts with sequences.
  2757. */
  2758. if (control) {
  2759. tcontrolcode(u);
  2760. /*
  2761. * control codes are not shown ever
  2762. */
  2763. return;
  2764. } else if (term.esc & ESC_START) {
  2765. if (term.esc & ESC_CSI) {
  2766. csiescseq.buf[csiescseq.len++] = u;
  2767. if (BETWEEN(u, 0x40, 0x7E)
  2768. || csiescseq.len >= \
  2769. sizeof(csiescseq.buf)-1) {
  2770. term.esc = 0;
  2771. csiparse();
  2772. csihandle();
  2773. }
  2774. return;
  2775. } else if (term.esc & ESC_UTF8) {
  2776. tdefutf8(u);
  2777. } else if (term.esc & ESC_ALTCHARSET) {
  2778. tdeftran(u);
  2779. } else if (term.esc & ESC_TEST) {
  2780. tdectest(u);
  2781. } else {
  2782. if (!eschandle(u))
  2783. return;
  2784. /* sequence already finished */
  2785. }
  2786. term.esc = 0;
  2787. /*
  2788. * All characters which form part of a sequence are not
  2789. * printed
  2790. */
  2791. return;
  2792. }
  2793. if (sel.ob.x != -1 && BETWEEN(term.c.y, sel.ob.y, sel.oe.y))
  2794. selclear(NULL);
  2795. gp = &term.line[term.c.y][term.c.x];
  2796. if (IS_SET(MODE_WRAP) && (term.c.state & CURSOR_WRAPNEXT)) {
  2797. gp->mode |= ATTR_WRAP;
  2798. tnewline(1);
  2799. gp = &term.line[term.c.y][term.c.x];
  2800. }
  2801. if (IS_SET(MODE_INSERT) && term.c.x+width < term.col)
  2802. memmove(gp+width, gp, (term.col - term.c.x - width) * sizeof(Glyph));
  2803. if (term.c.x+width > term.col) {
  2804. tnewline(1);
  2805. gp = &term.line[term.c.y][term.c.x];
  2806. }
  2807. tsetchar(u, &term.c.attr, term.c.x, term.c.y);
  2808. if (width == 2) {
  2809. gp->mode |= ATTR_WIDE;
  2810. if (term.c.x+1 < term.col) {
  2811. gp[1].u = '\0';
  2812. gp[1].mode = ATTR_WDUMMY;
  2813. }
  2814. }
  2815. if (term.c.x+width < term.col) {
  2816. tmoveto(term.c.x+width, term.c.y);
  2817. } else {
  2818. term.c.state |= CURSOR_WRAPNEXT;
  2819. }
  2820. }
  2821. void
  2822. tresize(int col, int row)
  2823. {
  2824. int i;
  2825. int minrow = MIN(row, term.row);
  2826. int mincol = MIN(col, term.col);
  2827. int *bp;
  2828. TCursor c;
  2829. if (col < 1 || row < 1) {
  2830. fprintf(stderr,
  2831. "tresize: error resizing to %dx%d\n", col, row);
  2832. return;
  2833. }
  2834. /*
  2835. * slide screen to keep cursor where we expect it -
  2836. * tscrollup would work here, but we can optimize to
  2837. * memmove because we're freeing the earlier lines
  2838. */
  2839. for (i = 0; i <= term.c.y - row; i++) {
  2840. free(term.line[i]);
  2841. free(term.alt[i]);
  2842. }
  2843. /* ensure that both src and dst are not NULL */
  2844. if (i > 0) {
  2845. memmove(term.line, term.line + i, row * sizeof(Line));
  2846. memmove(term.alt, term.alt + i, row * sizeof(Line));
  2847. }
  2848. for (i += row; i < term.row; i++) {
  2849. free(term.line[i]);
  2850. free(term.alt[i]);
  2851. }
  2852. /* resize to new width */
  2853. term.specbuf = xrealloc(term.specbuf, col * sizeof(XftGlyphFontSpec));
  2854. /* resize to new height */
  2855. term.line = xrealloc(term.line, row * sizeof(Line));
  2856. term.alt = xrealloc(term.alt, row * sizeof(Line));
  2857. term.dirty = xrealloc(term.dirty, row * sizeof(*term.dirty));
  2858. term.tabs = xrealloc(term.tabs, col * sizeof(*term.tabs));
  2859. /* resize each row to new width, zero-pad if needed */
  2860. for (i = 0; i < minrow; i++) {
  2861. term.line[i] = xrealloc(term.line[i], col * sizeof(Glyph));
  2862. term.alt[i] = xrealloc(term.alt[i], col * sizeof(Glyph));
  2863. }
  2864. /* allocate any new rows */
  2865. for (/* i == minrow */; i < row; i++) {
  2866. term.line[i] = xmalloc(col * sizeof(Glyph));
  2867. term.alt[i] = xmalloc(col * sizeof(Glyph));
  2868. }
  2869. if (col > term.col) {
  2870. bp = term.tabs + term.col;
  2871. memset(bp, 0, sizeof(*term.tabs) * (col - term.col));
  2872. while (--bp > term.tabs && !*bp)
  2873. /* nothing */ ;
  2874. for (bp += tabspaces; bp < term.tabs + col; bp += tabspaces)
  2875. *bp = 1;
  2876. }
  2877. /* update terminal size */
  2878. term.col = col;
  2879. term.row = row;
  2880. /* reset scrolling region */
  2881. tsetscroll(0, row-1);
  2882. /* make use of the LIMIT in tmoveto */
  2883. tmoveto(term.c.x, term.c.y);
  2884. /* Clearing both screens (it makes dirty all lines) */
  2885. c = term.c;
  2886. for (i = 0; i < 2; i++) {
  2887. if (mincol < col && 0 < minrow) {
  2888. tclearregion(mincol, 0, col - 1, minrow - 1);
  2889. }
  2890. if (0 < col && minrow < row) {
  2891. tclearregion(0, minrow, col - 1, row - 1);
  2892. }
  2893. tswapscreen();
  2894. tcursor(CURSOR_LOAD);
  2895. }
  2896. term.c = c;
  2897. }
  2898. void
  2899. xresize(int col, int row)
  2900. {
  2901. xw.tw = MAX(1, col * xw.cw);
  2902. xw.th = MAX(1, row * xw.ch);
  2903. XFreePixmap(xw.dpy, xw.buf);
  2904. xw.buf = XCreatePixmap(xw.dpy, xw.win, xw.w, xw.h,
  2905. DefaultDepth(xw.dpy, xw.scr));
  2906. XftDrawChange(xw.draw, xw.buf);
  2907. xclear(0, 0, xw.w, xw.h);
  2908. }
  2909. ushort
  2910. sixd_to_16bit(int x)
  2911. {
  2912. return x == 0 ? 0 : 0x3737 + 0x2828 * x;
  2913. }
  2914. int
  2915. xloadcolor(int i, const char *name, Color *ncolor)
  2916. {
  2917. XRenderColor color = { .alpha = 0xffff };
  2918. if (!name) {
  2919. if (BETWEEN(i, 16, 255)) { /* 256 color */
  2920. if (i < 6*6*6+16) { /* same colors as xterm */
  2921. color.red = sixd_to_16bit( ((i-16)/36)%6 );
  2922. color.green = sixd_to_16bit( ((i-16)/6) %6 );
  2923. color.blue = sixd_to_16bit( ((i-16)/1) %6 );
  2924. } else { /* greyscale */
  2925. color.red = 0x0808 + 0x0a0a * (i - (6*6*6+16));
  2926. color.green = color.blue = color.red;
  2927. }
  2928. return XftColorAllocValue(xw.dpy, xw.vis,
  2929. xw.cmap, &color, ncolor);
  2930. } else
  2931. name = colorname[i];
  2932. }
  2933. return XftColorAllocName(xw.dpy, xw.vis, xw.cmap, name, ncolor);
  2934. }
  2935. void
  2936. xloadcols(void)
  2937. {
  2938. int i;
  2939. static int loaded;
  2940. Color *cp;
  2941. if (loaded) {
  2942. for (cp = dc.col; cp < &dc.col[LEN(dc.col)]; ++cp)
  2943. XftColorFree(xw.dpy, xw.vis, xw.cmap, cp);
  2944. }
  2945. for (i = 0; i < LEN(dc.col); i++)
  2946. if (!xloadcolor(i, NULL, &dc.col[i])) {
  2947. if (colorname[i])
  2948. die("Could not allocate color '%s'\n", colorname[i]);
  2949. else
  2950. die("Could not allocate color %d\n", i);
  2951. }
  2952. loaded = 1;
  2953. }
  2954. int
  2955. xsetcolorname(int x, const char *name)
  2956. {
  2957. Color ncolor;
  2958. if (!BETWEEN(x, 0, LEN(dc.col)))
  2959. return 1;
  2960. if (!xloadcolor(x, name, &ncolor))
  2961. return 1;
  2962. XftColorFree(xw.dpy, xw.vis, xw.cmap, &dc.col[x]);
  2963. dc.col[x] = ncolor;
  2964. return 0;
  2965. }
  2966. /*
  2967. * Absolute coordinates.
  2968. */
  2969. void
  2970. xclear(int x1, int y1, int x2, int y2)
  2971. {
  2972. XftDrawRect(xw.draw,
  2973. &dc.col[IS_SET(MODE_REVERSE)? defaultfg : defaultbg],
  2974. x1, y1, x2-x1, y2-y1);
  2975. }
  2976. void
  2977. xhints(void)
  2978. {
  2979. XClassHint class = {opt_name ? opt_name : termname,
  2980. opt_class ? opt_class : termname};
  2981. XWMHints wm = {.flags = InputHint, .input = 1};
  2982. XSizeHints *sizeh = NULL;
  2983. sizeh = XAllocSizeHints();
  2984. sizeh->flags = PSize | PResizeInc | PBaseSize;
  2985. sizeh->height = xw.h;
  2986. sizeh->width = xw.w;
  2987. sizeh->height_inc = xw.ch;
  2988. sizeh->width_inc = xw.cw;
  2989. sizeh->base_height = 2 * borderpx;
  2990. sizeh->base_width = 2 * borderpx;
  2991. if (xw.isfixed) {
  2992. sizeh->flags |= PMaxSize | PMinSize;
  2993. sizeh->min_width = sizeh->max_width = xw.w;
  2994. sizeh->min_height = sizeh->max_height = xw.h;
  2995. }
  2996. if (xw.gm & (XValue|YValue)) {
  2997. sizeh->flags |= USPosition | PWinGravity;
  2998. sizeh->x = xw.l;
  2999. sizeh->y = xw.t;
  3000. sizeh->win_gravity = xgeommasktogravity(xw.gm);
  3001. }
  3002. XSetWMProperties(xw.dpy, xw.win, NULL, NULL, NULL, 0, sizeh, &wm,
  3003. &class);
  3004. XFree(sizeh);
  3005. }
  3006. int
  3007. xgeommasktogravity(int mask)
  3008. {
  3009. switch (mask & (XNegative|YNegative)) {
  3010. case 0:
  3011. return NorthWestGravity;
  3012. case XNegative:
  3013. return NorthEastGravity;
  3014. case YNegative:
  3015. return SouthWestGravity;
  3016. }
  3017. return SouthEastGravity;
  3018. }
  3019. int
  3020. xloadfont(Font *f, FcPattern *pattern)
  3021. {
  3022. FcPattern *match;
  3023. FcResult result;
  3024. XGlyphInfo extents;
  3025. int wantattr, haveattr;
  3026. match = XftFontMatch(xw.dpy, xw.scr, pattern, &result);
  3027. if (!match)
  3028. return 1;
  3029. if (!(f->match = XftFontOpenPattern(xw.dpy, match))) {
  3030. FcPatternDestroy(match);
  3031. return 1;
  3032. }
  3033. if ((XftPatternGetInteger(pattern, "slant", 0, &wantattr) ==
  3034. XftResultMatch)) {
  3035. /*
  3036. * Check if xft was unable to find a font with the appropriate
  3037. * slant but gave us one anyway. Try to mitigate.
  3038. */
  3039. if ((XftPatternGetInteger(f->match->pattern, "slant", 0,
  3040. &haveattr) != XftResultMatch) || haveattr < wantattr) {
  3041. f->badslant = 1;
  3042. fputs("st: font slant does not match\n", stderr);
  3043. }
  3044. }
  3045. if ((XftPatternGetInteger(pattern, "weight", 0, &wantattr) ==
  3046. XftResultMatch)) {
  3047. if ((XftPatternGetInteger(f->match->pattern, "weight", 0,
  3048. &haveattr) != XftResultMatch) || haveattr != wantattr) {
  3049. f->badweight = 1;
  3050. fputs("st: font weight does not match\n", stderr);
  3051. }
  3052. }
  3053. XftTextExtentsUtf8(xw.dpy, f->match,
  3054. (const FcChar8 *) ascii_printable,
  3055. strlen(ascii_printable), &extents);
  3056. f->set = NULL;
  3057. f->pattern = FcPatternDuplicate(pattern);
  3058. f->ascent = f->match->ascent;
  3059. f->descent = f->match->descent;
  3060. f->lbearing = 0;
  3061. f->rbearing = f->match->max_advance_width;
  3062. f->height = f->ascent + f->descent;
  3063. f->width = DIVCEIL(extents.xOff, strlen(ascii_printable));
  3064. return 0;
  3065. }
  3066. void
  3067. xloadfonts(char *fontstr, double fontsize)
  3068. {
  3069. FcPattern *pattern;
  3070. double fontval;
  3071. float ceilf(float);
  3072. if (fontstr[0] == '-') {
  3073. pattern = XftXlfdParse(fontstr, False, False);
  3074. } else {
  3075. pattern = FcNameParse((FcChar8 *)fontstr);
  3076. }
  3077. if (!pattern)
  3078. die("st: can't open font %s\n", fontstr);
  3079. if (fontsize > 1) {
  3080. FcPatternDel(pattern, FC_PIXEL_SIZE);
  3081. FcPatternDel(pattern, FC_SIZE);
  3082. FcPatternAddDouble(pattern, FC_PIXEL_SIZE, (double)fontsize);
  3083. usedfontsize = fontsize;
  3084. } else {
  3085. if (FcPatternGetDouble(pattern, FC_PIXEL_SIZE, 0, &fontval) ==
  3086. FcResultMatch) {
  3087. usedfontsize = fontval;
  3088. } else if (FcPatternGetDouble(pattern, FC_SIZE, 0, &fontval) ==
  3089. FcResultMatch) {
  3090. usedfontsize = -1;
  3091. } else {
  3092. /*
  3093. * Default font size is 12, if none given. This is to
  3094. * have a known usedfontsize value.
  3095. */
  3096. FcPatternAddDouble(pattern, FC_PIXEL_SIZE, 12);
  3097. usedfontsize = 12;
  3098. }
  3099. defaultfontsize = usedfontsize;
  3100. }
  3101. if (xloadfont(&dc.font, pattern))
  3102. die("st: can't open font %s\n", fontstr);
  3103. if (usedfontsize < 0) {
  3104. FcPatternGetDouble(dc.font.match->pattern,
  3105. FC_PIXEL_SIZE, 0, &fontval);
  3106. usedfontsize = fontval;
  3107. if (fontsize == 0)
  3108. defaultfontsize = fontval;
  3109. }
  3110. /* Setting character width and height. */
  3111. xw.cw = ceilf(dc.font.width * cwscale);
  3112. xw.ch = ceilf(dc.font.height * chscale);
  3113. FcPatternDel(pattern, FC_SLANT);
  3114. FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ITALIC);
  3115. if (xloadfont(&dc.ifont, pattern))
  3116. die("st: can't open font %s\n", fontstr);
  3117. FcPatternDel(pattern, FC_WEIGHT);
  3118. FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_BOLD);
  3119. if (xloadfont(&dc.ibfont, pattern))
  3120. die("st: can't open font %s\n", fontstr);
  3121. FcPatternDel(pattern, FC_SLANT);
  3122. FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ROMAN);
  3123. if (xloadfont(&dc.bfont, pattern))
  3124. die("st: can't open font %s\n", fontstr);
  3125. FcPatternDestroy(pattern);
  3126. }
  3127. void
  3128. xunloadfont(Font *f)
  3129. {
  3130. XftFontClose(xw.dpy, f->match);
  3131. FcPatternDestroy(f->pattern);
  3132. if (f->set)
  3133. FcFontSetDestroy(f->set);
  3134. }
  3135. void
  3136. xunloadfonts(void)
  3137. {
  3138. /* Free the loaded fonts in the font cache. */
  3139. while (frclen > 0)
  3140. XftFontClose(xw.dpy, frc[--frclen].font);
  3141. xunloadfont(&dc.font);
  3142. xunloadfont(&dc.bfont);
  3143. xunloadfont(&dc.ifont);
  3144. xunloadfont(&dc.ibfont);
  3145. }
  3146. void
  3147. xzoom(const Arg *arg)
  3148. {
  3149. Arg larg;
  3150. larg.f = usedfontsize + arg->f;
  3151. xzoomabs(&larg);
  3152. }
  3153. void
  3154. xzoomabs(const Arg *arg)
  3155. {
  3156. xunloadfonts();
  3157. xloadfonts(usedfont, arg->f);
  3158. cresize(0, 0);
  3159. ttyresize();
  3160. redraw();
  3161. xhints();
  3162. }
  3163. void
  3164. xzoomreset(const Arg *arg)
  3165. {
  3166. Arg larg;
  3167. if (defaultfontsize > 0) {
  3168. larg.f = defaultfontsize;
  3169. xzoomabs(&larg);
  3170. }
  3171. }
  3172. void
  3173. xinit(void)
  3174. {
  3175. XGCValues gcvalues;
  3176. Cursor cursor;
  3177. Window parent;
  3178. pid_t thispid = getpid();
  3179. XColor xmousefg, xmousebg;
  3180. if (!(xw.dpy = XOpenDisplay(NULL)))
  3181. die("Can't open display\n");
  3182. xw.scr = XDefaultScreen(xw.dpy);
  3183. xw.vis = XDefaultVisual(xw.dpy, xw.scr);
  3184. /* font */
  3185. if (!FcInit())
  3186. die("Could not init fontconfig.\n");
  3187. usedfont = (opt_font == NULL)? font : opt_font;
  3188. xloadfonts(usedfont, 0);
  3189. /* colors */
  3190. xw.cmap = XDefaultColormap(xw.dpy, xw.scr);
  3191. xloadcols();
  3192. /* adjust fixed window geometry */
  3193. xw.w = 2 * borderpx + term.col * xw.cw;
  3194. xw.h = 2 * borderpx + term.row * xw.ch;
  3195. if (xw.gm & XNegative)
  3196. xw.l += DisplayWidth(xw.dpy, xw.scr) - xw.w - 2;
  3197. if (xw.gm & YNegative)
  3198. xw.t += DisplayHeight(xw.dpy, xw.scr) - xw.h - 2;
  3199. /* Events */
  3200. xw.attrs.background_pixel = dc.col[defaultbg].pixel;
  3201. xw.attrs.border_pixel = dc.col[defaultbg].pixel;
  3202. xw.attrs.bit_gravity = NorthWestGravity;
  3203. xw.attrs.event_mask = FocusChangeMask | KeyPressMask
  3204. | ExposureMask | VisibilityChangeMask | StructureNotifyMask
  3205. | ButtonMotionMask | ButtonPressMask | ButtonReleaseMask;
  3206. xw.attrs.colormap = xw.cmap;
  3207. if (!(opt_embed && (parent = strtol(opt_embed, NULL, 0))))
  3208. parent = XRootWindow(xw.dpy, xw.scr);
  3209. xw.win = XCreateWindow(xw.dpy, parent, xw.l, xw.t,
  3210. xw.w, xw.h, 0, XDefaultDepth(xw.dpy, xw.scr), InputOutput,
  3211. xw.vis, CWBackPixel | CWBorderPixel | CWBitGravity
  3212. | CWEventMask | CWColormap, &xw.attrs);
  3213. memset(&gcvalues, 0, sizeof(gcvalues));
  3214. gcvalues.graphics_exposures = False;
  3215. dc.gc = XCreateGC(xw.dpy, parent, GCGraphicsExposures,
  3216. &gcvalues);
  3217. xw.buf = XCreatePixmap(xw.dpy, xw.win, xw.w, xw.h,
  3218. DefaultDepth(xw.dpy, xw.scr));
  3219. XSetForeground(xw.dpy, dc.gc, dc.col[defaultbg].pixel);
  3220. XFillRectangle(xw.dpy, xw.buf, dc.gc, 0, 0, xw.w, xw.h);
  3221. /* Xft rendering context */
  3222. xw.draw = XftDrawCreate(xw.dpy, xw.buf, xw.vis, xw.cmap);
  3223. /* input methods */
  3224. if ((xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL)) == NULL) {
  3225. XSetLocaleModifiers("@im=local");
  3226. if ((xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL)) == NULL) {
  3227. XSetLocaleModifiers("@im=");
  3228. if ((xw.xim = XOpenIM(xw.dpy,
  3229. NULL, NULL, NULL)) == NULL) {
  3230. die("XOpenIM failed. Could not open input"
  3231. " device.\n");
  3232. }
  3233. }
  3234. }
  3235. xw.xic = XCreateIC(xw.xim, XNInputStyle, XIMPreeditNothing
  3236. | XIMStatusNothing, XNClientWindow, xw.win,
  3237. XNFocusWindow, xw.win, NULL);
  3238. if (xw.xic == NULL)
  3239. die("XCreateIC failed. Could not obtain input method.\n");
  3240. /* white cursor, black outline */
  3241. cursor = XCreateFontCursor(xw.dpy, mouseshape);
  3242. XDefineCursor(xw.dpy, xw.win, cursor);
  3243. if (XParseColor(xw.dpy, xw.cmap, colorname[mousefg], &xmousefg) == 0) {
  3244. xmousefg.red = 0xffff;
  3245. xmousefg.green = 0xffff;
  3246. xmousefg.blue = 0xffff;
  3247. }
  3248. if (XParseColor(xw.dpy, xw.cmap, colorname[mousebg], &xmousebg) == 0) {
  3249. xmousebg.red = 0x0000;
  3250. xmousebg.green = 0x0000;
  3251. xmousebg.blue = 0x0000;
  3252. }
  3253. XRecolorCursor(xw.dpy, cursor, &xmousefg, &xmousebg);
  3254. xw.xembed = XInternAtom(xw.dpy, "_XEMBED", False);
  3255. xw.wmdeletewin = XInternAtom(xw.dpy, "WM_DELETE_WINDOW", False);
  3256. xw.netwmname = XInternAtom(xw.dpy, "_NET_WM_NAME", False);
  3257. XSetWMProtocols(xw.dpy, xw.win, &xw.wmdeletewin, 1);
  3258. xw.netwmpid = XInternAtom(xw.dpy, "_NET_WM_PID", False);
  3259. XChangeProperty(xw.dpy, xw.win, xw.netwmpid, XA_CARDINAL, 32,
  3260. PropModeReplace, (uchar *)&thispid, 1);
  3261. xresettitle();
  3262. XMapWindow(xw.dpy, xw.win);
  3263. xhints();
  3264. XSync(xw.dpy, False);
  3265. }
  3266. int
  3267. xmakeglyphfontspecs(XftGlyphFontSpec *specs, const Glyph *glyphs, int len, int x, int y)
  3268. {
  3269. float winx = borderpx + x * xw.cw, winy = borderpx + y * xw.ch, xp, yp;
  3270. ushort mode, prevmode = USHRT_MAX;
  3271. Font *font = &dc.font;
  3272. int frcflags = FRC_NORMAL;
  3273. float runewidth = xw.cw;
  3274. Rune rune;
  3275. FT_UInt glyphidx;
  3276. FcResult fcres;
  3277. FcPattern *fcpattern, *fontpattern;
  3278. FcFontSet *fcsets[] = { NULL };
  3279. FcCharSet *fccharset;
  3280. int i, f, numspecs = 0;
  3281. for (i = 0, xp = winx, yp = winy + font->ascent; i < len; ++i) {
  3282. /* Fetch rune and mode for current glyph. */
  3283. rune = glyphs[i].u;
  3284. mode = glyphs[i].mode;
  3285. /* Skip dummy wide-character spacing. */
  3286. if (mode == ATTR_WDUMMY)
  3287. continue;
  3288. /* Determine font for glyph if different from previous glyph. */
  3289. if (prevmode != mode) {
  3290. prevmode = mode;
  3291. font = &dc.font;
  3292. frcflags = FRC_NORMAL;
  3293. runewidth = xw.cw * ((mode & ATTR_WIDE) ? 2.0f : 1.0f);
  3294. if ((mode & ATTR_ITALIC) && (mode & ATTR_BOLD)) {
  3295. font = &dc.ibfont;
  3296. frcflags = FRC_ITALICBOLD;
  3297. } else if (mode & ATTR_ITALIC) {
  3298. font = &dc.ifont;
  3299. frcflags = FRC_ITALIC;
  3300. } else if (mode & ATTR_BOLD) {
  3301. font = &dc.bfont;
  3302. frcflags = FRC_BOLD;
  3303. }
  3304. yp = winy + font->ascent;
  3305. }
  3306. /* Lookup character index with default font. */
  3307. glyphidx = XftCharIndex(xw.dpy, font->match, rune);
  3308. if (glyphidx) {
  3309. specs[numspecs].font = font->match;
  3310. specs[numspecs].glyph = glyphidx;
  3311. specs[numspecs].x = (short)xp;
  3312. specs[numspecs].y = (short)yp;
  3313. xp += runewidth;
  3314. numspecs++;
  3315. continue;
  3316. }
  3317. /* Fallback on font cache, search the font cache for match. */
  3318. for (f = 0; f < frclen; f++) {
  3319. glyphidx = XftCharIndex(xw.dpy, frc[f].font, rune);
  3320. /* Everything correct. */
  3321. if (glyphidx && frc[f].flags == frcflags)
  3322. break;
  3323. /* We got a default font for a not found glyph. */
  3324. if (!glyphidx && frc[f].flags == frcflags
  3325. && frc[f].unicodep == rune) {
  3326. break;
  3327. }
  3328. }
  3329. /* Nothing was found. Use fontconfig to find matching font. */
  3330. if (f >= frclen) {
  3331. if (!font->set)
  3332. font->set = FcFontSort(0, font->pattern,
  3333. 1, 0, &fcres);
  3334. fcsets[0] = font->set;
  3335. /*
  3336. * Nothing was found in the cache. Now use
  3337. * some dozen of Fontconfig calls to get the
  3338. * font for one single character.
  3339. *
  3340. * Xft and fontconfig are design failures.
  3341. */
  3342. fcpattern = FcPatternDuplicate(font->pattern);
  3343. fccharset = FcCharSetCreate();
  3344. FcCharSetAddChar(fccharset, rune);
  3345. FcPatternAddCharSet(fcpattern, FC_CHARSET,
  3346. fccharset);
  3347. FcPatternAddBool(fcpattern, FC_SCALABLE, 1);
  3348. FcConfigSubstitute(0, fcpattern,
  3349. FcMatchPattern);
  3350. FcDefaultSubstitute(fcpattern);
  3351. fontpattern = FcFontSetMatch(0, fcsets, 1,
  3352. fcpattern, &fcres);
  3353. /*
  3354. * Overwrite or create the new cache entry.
  3355. */
  3356. if (frclen >= LEN(frc)) {
  3357. frclen = LEN(frc) - 1;
  3358. XftFontClose(xw.dpy, frc[frclen].font);
  3359. frc[frclen].unicodep = 0;
  3360. }
  3361. frc[frclen].font = XftFontOpenPattern(xw.dpy,
  3362. fontpattern);
  3363. frc[frclen].flags = frcflags;
  3364. frc[frclen].unicodep = rune;
  3365. glyphidx = XftCharIndex(xw.dpy, frc[frclen].font, rune);
  3366. f = frclen;
  3367. frclen++;
  3368. FcPatternDestroy(fcpattern);
  3369. FcCharSetDestroy(fccharset);
  3370. }
  3371. specs[numspecs].font = frc[f].font;
  3372. specs[numspecs].glyph = glyphidx;
  3373. specs[numspecs].x = (short)xp;
  3374. specs[numspecs].y = (short)yp;
  3375. xp += runewidth;
  3376. numspecs++;
  3377. }
  3378. return numspecs;
  3379. }
  3380. void
  3381. xdrawglyphfontspecs(const XftGlyphFontSpec *specs, Glyph base, int len, int x, int y)
  3382. {
  3383. int charlen = len * ((base.mode & ATTR_WIDE) ? 2 : 1);
  3384. int winx = borderpx + x * xw.cw, winy = borderpx + y * xw.ch,
  3385. width = charlen * xw.cw;
  3386. Color *fg, *bg, *temp, revfg, revbg, truefg, truebg;
  3387. XRenderColor colfg, colbg;
  3388. XRectangle r;
  3389. /* Fallback on color display for attributes not supported by the font */
  3390. if (base.mode & ATTR_ITALIC && base.mode & ATTR_BOLD) {
  3391. if (dc.ibfont.badslant || dc.ibfont.badweight)
  3392. base.fg = defaultattr;
  3393. } else if ((base.mode & ATTR_ITALIC && dc.ifont.badslant) ||
  3394. (base.mode & ATTR_BOLD && dc.bfont.badweight)) {
  3395. base.fg = defaultattr;
  3396. }
  3397. if (IS_TRUECOL(base.fg)) {
  3398. colfg.alpha = 0xffff;
  3399. colfg.red = TRUERED(base.fg);
  3400. colfg.green = TRUEGREEN(base.fg);
  3401. colfg.blue = TRUEBLUE(base.fg);
  3402. XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg, &truefg);
  3403. fg = &truefg;
  3404. } else {
  3405. fg = &dc.col[base.fg];
  3406. }
  3407. if (IS_TRUECOL(base.bg)) {
  3408. colbg.alpha = 0xffff;
  3409. colbg.green = TRUEGREEN(base.bg);
  3410. colbg.red = TRUERED(base.bg);
  3411. colbg.blue = TRUEBLUE(base.bg);
  3412. XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colbg, &truebg);
  3413. bg = &truebg;
  3414. } else {
  3415. bg = &dc.col[base.bg];
  3416. }
  3417. /* Change basic system colors [0-7] to bright system colors [8-15] */
  3418. if ((base.mode & ATTR_BOLD_FAINT) == ATTR_BOLD && BETWEEN(base.fg, 0, 7))
  3419. fg = &dc.col[base.fg + 8];
  3420. if (IS_SET(MODE_REVERSE)) {
  3421. if (fg == &dc.col[defaultfg]) {
  3422. fg = &dc.col[defaultbg];
  3423. } else {
  3424. colfg.red = ~fg->color.red;
  3425. colfg.green = ~fg->color.green;
  3426. colfg.blue = ~fg->color.blue;
  3427. colfg.alpha = fg->color.alpha;
  3428. XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg,
  3429. &revfg);
  3430. fg = &revfg;
  3431. }
  3432. if (bg == &dc.col[defaultbg]) {
  3433. bg = &dc.col[defaultfg];
  3434. } else {
  3435. colbg.red = ~bg->color.red;
  3436. colbg.green = ~bg->color.green;
  3437. colbg.blue = ~bg->color.blue;
  3438. colbg.alpha = bg->color.alpha;
  3439. XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colbg,
  3440. &revbg);
  3441. bg = &revbg;
  3442. }
  3443. }
  3444. if (base.mode & ATTR_REVERSE) {
  3445. temp = fg;
  3446. fg = bg;
  3447. bg = temp;
  3448. }
  3449. if ((base.mode & ATTR_BOLD_FAINT) == ATTR_FAINT) {
  3450. colfg.red = fg->color.red / 2;
  3451. colfg.green = fg->color.green / 2;
  3452. colfg.blue = fg->color.blue / 2;
  3453. XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg, &revfg);
  3454. fg = &revfg;
  3455. }
  3456. if (base.mode & ATTR_BLINK && term.mode & MODE_BLINK)
  3457. fg = bg;
  3458. if (base.mode & ATTR_INVISIBLE)
  3459. fg = bg;
  3460. /* Intelligent cleaning up of the borders. */
  3461. if (x == 0) {
  3462. xclear(0, (y == 0)? 0 : winy, borderpx,
  3463. winy + xw.ch + ((y >= term.row-1)? xw.h : 0));
  3464. }
  3465. if (x + charlen >= term.col) {
  3466. xclear(winx + width, (y == 0)? 0 : winy, xw.w,
  3467. ((y >= term.row-1)? xw.h : (winy + xw.ch)));
  3468. }
  3469. if (y == 0)
  3470. xclear(winx, 0, winx + width, borderpx);
  3471. if (y == term.row-1)
  3472. xclear(winx, winy + xw.ch, winx + width, xw.h);
  3473. /* Clean up the region we want to draw to. */
  3474. XftDrawRect(xw.draw, bg, winx, winy, width, xw.ch);
  3475. /* Set the clip region because Xft is sometimes dirty. */
  3476. r.x = 0;
  3477. r.y = 0;
  3478. r.height = xw.ch;
  3479. r.width = width;
  3480. XftDrawSetClipRectangles(xw.draw, winx, winy, &r, 1);
  3481. /* Render the glyphs. */
  3482. XftDrawGlyphFontSpec(xw.draw, fg, specs, len);
  3483. /* Render underline and strikethrough. */
  3484. if (base.mode & ATTR_UNDERLINE) {
  3485. XftDrawRect(xw.draw, fg, winx, winy + dc.font.ascent + 1,
  3486. width, 1);
  3487. }
  3488. if (base.mode & ATTR_STRUCK) {
  3489. XftDrawRect(xw.draw, fg, winx, winy + 2 * dc.font.ascent / 3,
  3490. width, 1);
  3491. }
  3492. /* Reset clip to none. */
  3493. XftDrawSetClip(xw.draw, 0);
  3494. }
  3495. void
  3496. xdrawglyph(Glyph g, int x, int y)
  3497. {
  3498. int numspecs;
  3499. XftGlyphFontSpec spec;
  3500. numspecs = xmakeglyphfontspecs(&spec, &g, 1, x, y);
  3501. xdrawglyphfontspecs(&spec, g, numspecs, x, y);
  3502. }
  3503. void
  3504. xdrawcursor(void)
  3505. {
  3506. static int oldx = 0, oldy = 0;
  3507. int curx;
  3508. Glyph g = {' ', ATTR_NULL, defaultbg, defaultcs}, og;
  3509. int ena_sel = sel.ob.x != -1 && sel.alt == IS_SET(MODE_ALTSCREEN);
  3510. Color drawcol;
  3511. LIMIT(oldx, 0, term.col-1);
  3512. LIMIT(oldy, 0, term.row-1);
  3513. curx = term.c.x;
  3514. /* adjust position if in dummy */
  3515. if (term.line[oldy][oldx].mode & ATTR_WDUMMY)
  3516. oldx--;
  3517. if (term.line[term.c.y][curx].mode & ATTR_WDUMMY)
  3518. curx--;
  3519. /* remove the old cursor */
  3520. og = term.line[oldy][oldx];
  3521. if (ena_sel && selected(oldx, oldy))
  3522. og.mode ^= ATTR_REVERSE;
  3523. xdrawglyph(og, oldx, oldy);
  3524. g.u = term.line[term.c.y][term.c.x].u;
  3525. /*
  3526. * Select the right color for the right mode.
  3527. */
  3528. if (IS_SET(MODE_REVERSE)) {
  3529. g.mode |= ATTR_REVERSE;
  3530. g.bg = defaultfg;
  3531. if (ena_sel && selected(term.c.x, term.c.y)) {
  3532. drawcol = dc.col[defaultcs];
  3533. g.fg = defaultrcs;
  3534. } else {
  3535. drawcol = dc.col[defaultrcs];
  3536. g.fg = defaultcs;
  3537. }
  3538. } else {
  3539. if (ena_sel && selected(term.c.x, term.c.y)) {
  3540. drawcol = dc.col[defaultrcs];
  3541. g.fg = defaultfg;
  3542. g.bg = defaultrcs;
  3543. } else {
  3544. drawcol = dc.col[defaultcs];
  3545. }
  3546. }
  3547. if (IS_SET(MODE_HIDE))
  3548. return;
  3549. /* draw the new one */
  3550. if (xw.state & WIN_FOCUSED) {
  3551. switch (xw.cursor) {
  3552. case 7: /* st extension: snowman */
  3553. utf8decode("", &g.u, UTF_SIZ);
  3554. case 0: /* Blinking Block */
  3555. case 1: /* Blinking Block (Default) */
  3556. case 2: /* Steady Block */
  3557. g.mode |= term.line[term.c.y][curx].mode & ATTR_WIDE;
  3558. xdrawglyph(g, term.c.x, term.c.y);
  3559. break;
  3560. case 3: /* Blinking Underline */
  3561. case 4: /* Steady Underline */
  3562. XftDrawRect(xw.draw, &drawcol,
  3563. borderpx + curx * xw.cw,
  3564. borderpx + (term.c.y + 1) * xw.ch - \
  3565. cursorthickness,
  3566. xw.cw, cursorthickness);
  3567. break;
  3568. case 5: /* Blinking bar */
  3569. case 6: /* Steady bar */
  3570. XftDrawRect(xw.draw, &drawcol,
  3571. borderpx + curx * xw.cw,
  3572. borderpx + term.c.y * xw.ch,
  3573. cursorthickness, xw.ch);
  3574. break;
  3575. }
  3576. } else {
  3577. XftDrawRect(xw.draw, &drawcol,
  3578. borderpx + curx * xw.cw,
  3579. borderpx + term.c.y * xw.ch,
  3580. xw.cw - 1, 1);
  3581. XftDrawRect(xw.draw, &drawcol,
  3582. borderpx + curx * xw.cw,
  3583. borderpx + term.c.y * xw.ch,
  3584. 1, xw.ch - 1);
  3585. XftDrawRect(xw.draw, &drawcol,
  3586. borderpx + (curx + 1) * xw.cw - 1,
  3587. borderpx + term.c.y * xw.ch,
  3588. 1, xw.ch - 1);
  3589. XftDrawRect(xw.draw, &drawcol,
  3590. borderpx + curx * xw.cw,
  3591. borderpx + (term.c.y + 1) * xw.ch - 1,
  3592. xw.cw, 1);
  3593. }
  3594. oldx = curx, oldy = term.c.y;
  3595. }
  3596. void
  3597. xsettitle(char *p)
  3598. {
  3599. XTextProperty prop;
  3600. Xutf8TextListToTextProperty(xw.dpy, &p, 1, XUTF8StringStyle,
  3601. &prop);
  3602. XSetWMName(xw.dpy, xw.win, &prop);
  3603. XSetTextProperty(xw.dpy, xw.win, &prop, xw.netwmname);
  3604. XFree(prop.value);
  3605. }
  3606. void
  3607. xresettitle(void)
  3608. {
  3609. xsettitle(opt_title ? opt_title : "st");
  3610. }
  3611. void
  3612. redraw(void)
  3613. {
  3614. tfulldirt();
  3615. draw();
  3616. }
  3617. void
  3618. draw(void)
  3619. {
  3620. drawregion(0, 0, term.col, term.row);
  3621. XCopyArea(xw.dpy, xw.buf, xw.win, dc.gc, 0, 0, xw.w,
  3622. xw.h, 0, 0);
  3623. XSetForeground(xw.dpy, dc.gc,
  3624. dc.col[IS_SET(MODE_REVERSE)?
  3625. defaultfg : defaultbg].pixel);
  3626. }
  3627. void
  3628. drawregion(int x1, int y1, int x2, int y2)
  3629. {
  3630. int i, x, y, ox, numspecs;
  3631. Glyph base, new;
  3632. XftGlyphFontSpec *specs;
  3633. int ena_sel = sel.ob.x != -1 && sel.alt == IS_SET(MODE_ALTSCREEN);
  3634. if (!(xw.state & WIN_VISIBLE))
  3635. return;
  3636. for (y = y1; y < y2; y++) {
  3637. if (!term.dirty[y])
  3638. continue;
  3639. term.dirty[y] = 0;
  3640. specs = term.specbuf;
  3641. numspecs = xmakeglyphfontspecs(specs, &term.line[y][x1], x2 - x1, x1, y);
  3642. i = ox = 0;
  3643. for (x = x1; x < x2 && i < numspecs; x++) {
  3644. new = term.line[y][x];
  3645. if (new.mode == ATTR_WDUMMY)
  3646. continue;
  3647. if (ena_sel && selected(x, y))
  3648. new.mode ^= ATTR_REVERSE;
  3649. if (i > 0 && ATTRCMP(base, new)) {
  3650. xdrawglyphfontspecs(specs, base, i, ox, y);
  3651. specs += i;
  3652. numspecs -= i;
  3653. i = 0;
  3654. }
  3655. if (i == 0) {
  3656. ox = x;
  3657. base = new;
  3658. }
  3659. i++;
  3660. }
  3661. if (i > 0)
  3662. xdrawglyphfontspecs(specs, base, i, ox, y);
  3663. }
  3664. xdrawcursor();
  3665. }
  3666. void
  3667. expose(XEvent *ev)
  3668. {
  3669. redraw();
  3670. }
  3671. void
  3672. visibility(XEvent *ev)
  3673. {
  3674. XVisibilityEvent *e = &ev->xvisibility;
  3675. MODBIT(xw.state, e->state != VisibilityFullyObscured, WIN_VISIBLE);
  3676. }
  3677. void
  3678. unmap(XEvent *ev)
  3679. {
  3680. xw.state &= ~WIN_VISIBLE;
  3681. }
  3682. void
  3683. xsetpointermotion(int set)
  3684. {
  3685. MODBIT(xw.attrs.event_mask, set, PointerMotionMask);
  3686. XChangeWindowAttributes(xw.dpy, xw.win, CWEventMask, &xw.attrs);
  3687. }
  3688. void
  3689. xseturgency(int add)
  3690. {
  3691. XWMHints *h = XGetWMHints(xw.dpy, xw.win);
  3692. MODBIT(h->flags, add, XUrgencyHint);
  3693. XSetWMHints(xw.dpy, xw.win, h);
  3694. XFree(h);
  3695. }
  3696. void
  3697. focus(XEvent *ev)
  3698. {
  3699. XFocusChangeEvent *e = &ev->xfocus;
  3700. if (e->mode == NotifyGrab)
  3701. return;
  3702. if (ev->type == FocusIn) {
  3703. XSetICFocus(xw.xic);
  3704. xw.state |= WIN_FOCUSED;
  3705. xseturgency(0);
  3706. if (IS_SET(MODE_FOCUS))
  3707. ttywrite("\033[I", 3);
  3708. } else {
  3709. XUnsetICFocus(xw.xic);
  3710. xw.state &= ~WIN_FOCUSED;
  3711. if (IS_SET(MODE_FOCUS))
  3712. ttywrite("\033[O", 3);
  3713. }
  3714. }
  3715. int
  3716. match(uint mask, uint state)
  3717. {
  3718. return mask == XK_ANY_MOD || mask == (state & ~ignoremod);
  3719. }
  3720. void
  3721. numlock(const Arg *dummy)
  3722. {
  3723. term.numlock ^= 1;
  3724. }
  3725. char*
  3726. kmap(KeySym k, uint state)
  3727. {
  3728. Key *kp;
  3729. int i;
  3730. /* Check for mapped keys out of X11 function keys. */
  3731. for (i = 0; i < LEN(mappedkeys); i++) {
  3732. if (mappedkeys[i] == k)
  3733. break;
  3734. }
  3735. if (i == LEN(mappedkeys)) {
  3736. if ((k & 0xFFFF) < 0xFD00)
  3737. return NULL;
  3738. }
  3739. for (kp = key; kp < key + LEN(key); kp++) {
  3740. if (kp->k != k)
  3741. continue;
  3742. if (!match(kp->mask, state))
  3743. continue;
  3744. if (IS_SET(MODE_APPKEYPAD) ? kp->appkey < 0 : kp->appkey > 0)
  3745. continue;
  3746. if (term.numlock && kp->appkey == 2)
  3747. continue;
  3748. if (IS_SET(MODE_APPCURSOR) ? kp->appcursor < 0 : kp->appcursor > 0)
  3749. continue;
  3750. if (IS_SET(MODE_CRLF) ? kp->crlf < 0 : kp->crlf > 0)
  3751. continue;
  3752. return kp->s;
  3753. }
  3754. return NULL;
  3755. }
  3756. void
  3757. kpress(XEvent *ev)
  3758. {
  3759. XKeyEvent *e = &ev->xkey;
  3760. KeySym ksym;
  3761. char buf[32], *customkey;
  3762. int len;
  3763. Rune c;
  3764. Status status;
  3765. Shortcut *bp;
  3766. if (IS_SET(MODE_KBDLOCK))
  3767. return;
  3768. len = XmbLookupString(xw.xic, e, buf, sizeof buf, &ksym, &status);
  3769. /* 1. shortcuts */
  3770. for (bp = shortcuts; bp < shortcuts + LEN(shortcuts); bp++) {
  3771. if (ksym == bp->keysym && match(bp->mod, e->state)) {
  3772. bp->func(&(bp->arg));
  3773. return;
  3774. }
  3775. }
  3776. /* 2. custom keys from config.h */
  3777. if ((customkey = kmap(ksym, e->state))) {
  3778. ttysend(customkey, strlen(customkey));
  3779. return;
  3780. }
  3781. /* 3. composed string from input method */
  3782. if (len == 0)
  3783. return;
  3784. if (len == 1 && e->state & Mod1Mask) {
  3785. if (IS_SET(MODE_8BIT)) {
  3786. if (*buf < 0177) {
  3787. c = *buf | 0x80;
  3788. len = utf8encode(c, buf);
  3789. }
  3790. } else {
  3791. buf[1] = buf[0];
  3792. buf[0] = '\033';
  3793. len = 2;
  3794. }
  3795. }
  3796. ttysend(buf, len);
  3797. }
  3798. void
  3799. cmessage(XEvent *e)
  3800. {
  3801. /*
  3802. * See xembed specs
  3803. * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
  3804. */
  3805. if (e->xclient.message_type == xw.xembed && e->xclient.format == 32) {
  3806. if (e->xclient.data.l[1] == XEMBED_FOCUS_IN) {
  3807. xw.state |= WIN_FOCUSED;
  3808. xseturgency(0);
  3809. } else if (e->xclient.data.l[1] == XEMBED_FOCUS_OUT) {
  3810. xw.state &= ~WIN_FOCUSED;
  3811. }
  3812. } else if (e->xclient.data.l[0] == xw.wmdeletewin) {
  3813. /* Send SIGHUP to shell */
  3814. kill(pid, SIGHUP);
  3815. exit(0);
  3816. }
  3817. }
  3818. void
  3819. cresize(int width, int height)
  3820. {
  3821. int col, row;
  3822. if (width != 0)
  3823. xw.w = width;
  3824. if (height != 0)
  3825. xw.h = height;
  3826. col = (xw.w - 2 * borderpx) / xw.cw;
  3827. row = (xw.h - 2 * borderpx) / xw.ch;
  3828. tresize(col, row);
  3829. xresize(col, row);
  3830. }
  3831. void
  3832. resize(XEvent *e)
  3833. {
  3834. if (e->xconfigure.width == xw.w && e->xconfigure.height == xw.h)
  3835. return;
  3836. cresize(e->xconfigure.width, e->xconfigure.height);
  3837. ttyresize();
  3838. }
  3839. void
  3840. run(void)
  3841. {
  3842. XEvent ev;
  3843. int w = xw.w, h = xw.h;
  3844. fd_set rfd;
  3845. int xfd = XConnectionNumber(xw.dpy), xev, blinkset = 0, dodraw = 0;
  3846. struct timespec drawtimeout, *tv = NULL, now, last, lastblink;
  3847. long deltatime;
  3848. /* Waiting for window mapping */
  3849. do {
  3850. XNextEvent(xw.dpy, &ev);
  3851. /*
  3852. * This XFilterEvent call is required because of XOpenIM. It
  3853. * does filter out the key event and some client message for
  3854. * the input method too.
  3855. */
  3856. if (XFilterEvent(&ev, None))
  3857. continue;
  3858. if (ev.type == ConfigureNotify) {
  3859. w = ev.xconfigure.width;
  3860. h = ev.xconfigure.height;
  3861. }
  3862. } while (ev.type != MapNotify);
  3863. cresize(w, h);
  3864. ttynew();
  3865. ttyresize();
  3866. clock_gettime(CLOCK_MONOTONIC, &last);
  3867. lastblink = last;
  3868. for (xev = actionfps;;) {
  3869. FD_ZERO(&rfd);
  3870. FD_SET(cmdfd, &rfd);
  3871. FD_SET(xfd, &rfd);
  3872. if (pselect(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, tv, NULL) < 0) {
  3873. if (errno == EINTR)
  3874. continue;
  3875. die("select failed: %s\n", strerror(errno));
  3876. }
  3877. if (FD_ISSET(cmdfd, &rfd)) {
  3878. ttyread();
  3879. if (blinktimeout) {
  3880. blinkset = tattrset(ATTR_BLINK);
  3881. if (!blinkset)
  3882. MODBIT(term.mode, 0, MODE_BLINK);
  3883. }
  3884. }
  3885. if (FD_ISSET(xfd, &rfd))
  3886. xev = actionfps;
  3887. clock_gettime(CLOCK_MONOTONIC, &now);
  3888. drawtimeout.tv_sec = 0;
  3889. drawtimeout.tv_nsec = (1000 * 1E6)/ xfps;
  3890. tv = &drawtimeout;
  3891. dodraw = 0;
  3892. if (blinktimeout && TIMEDIFF(now, lastblink) > blinktimeout) {
  3893. tsetdirtattr(ATTR_BLINK);
  3894. term.mode ^= MODE_BLINK;
  3895. lastblink = now;
  3896. dodraw = 1;
  3897. }
  3898. deltatime = TIMEDIFF(now, last);
  3899. if (deltatime > 1000 / (xev ? xfps : actionfps)) {
  3900. dodraw = 1;
  3901. last = now;
  3902. }
  3903. if (dodraw) {
  3904. while (XPending(xw.dpy)) {
  3905. XNextEvent(xw.dpy, &ev);
  3906. if (XFilterEvent(&ev, None))
  3907. continue;
  3908. if (handler[ev.type])
  3909. (handler[ev.type])(&ev);
  3910. }
  3911. draw();
  3912. XFlush(xw.dpy);
  3913. if (xev && !FD_ISSET(xfd, &rfd))
  3914. xev--;
  3915. if (!FD_ISSET(cmdfd, &rfd) && !FD_ISSET(xfd, &rfd)) {
  3916. if (blinkset) {
  3917. if (TIMEDIFF(now, lastblink) \
  3918. > blinktimeout) {
  3919. drawtimeout.tv_nsec = 1000;
  3920. } else {
  3921. drawtimeout.tv_nsec = (1E6 * \
  3922. (blinktimeout - \
  3923. TIMEDIFF(now,
  3924. lastblink)));
  3925. }
  3926. drawtimeout.tv_sec = \
  3927. drawtimeout.tv_nsec / 1E9;
  3928. drawtimeout.tv_nsec %= (long)1E9;
  3929. } else {
  3930. tv = NULL;
  3931. }
  3932. }
  3933. }
  3934. }
  3935. }
  3936. void
  3937. usage(void)
  3938. {
  3939. die("usage: %s [-aiv] [-c class] [-f font] [-g geometry]"
  3940. " [-n name] [-o file]\n"
  3941. " [-T title] [-t title] [-w windowid]"
  3942. " [[-e] command [args ...]]\n"
  3943. " %s [-aiv] [-c class] [-f font] [-g geometry]"
  3944. " [-n name] [-o file]\n"
  3945. " [-T title] [-t title] [-w windowid] -l line"
  3946. " [stty_args ...]\n", argv0, argv0);
  3947. }
  3948. int
  3949. main(int argc, char *argv[])
  3950. {
  3951. uint cols = 80, rows = 24;
  3952. xw.l = xw.t = 0;
  3953. xw.isfixed = False;
  3954. xw.cursor = cursorshape;
  3955. ARGBEGIN {
  3956. case 'a':
  3957. allowaltscreen = 0;
  3958. break;
  3959. case 'c':
  3960. opt_class = EARGF(usage());
  3961. break;
  3962. case 'e':
  3963. if (argc > 0)
  3964. --argc, ++argv;
  3965. goto run;
  3966. case 'f':
  3967. opt_font = EARGF(usage());
  3968. break;
  3969. case 'g':
  3970. xw.gm = XParseGeometry(EARGF(usage()),
  3971. &xw.l, &xw.t, &cols, &rows);
  3972. break;
  3973. case 'i':
  3974. xw.isfixed = 1;
  3975. break;
  3976. case 'o':
  3977. opt_io = EARGF(usage());
  3978. break;
  3979. case 'l':
  3980. opt_line = EARGF(usage());
  3981. break;
  3982. case 'n':
  3983. opt_name = EARGF(usage());
  3984. break;
  3985. case 't':
  3986. case 'T':
  3987. opt_title = EARGF(usage());
  3988. break;
  3989. case 'w':
  3990. opt_embed = EARGF(usage());
  3991. break;
  3992. case 'v':
  3993. die("%s " VERSION " (c) 2010-2016 st engineers\n", argv0);
  3994. break;
  3995. default:
  3996. usage();
  3997. } ARGEND;
  3998. run:
  3999. if (argc > 0) {
  4000. /* eat all remaining arguments */
  4001. opt_cmd = argv;
  4002. if (!opt_title && !opt_line)
  4003. opt_title = basename(xstrdup(argv[0]));
  4004. }
  4005. setlocale(LC_CTYPE, "");
  4006. XSetLocaleModifiers("");
  4007. tnew(MAX(cols, 1), MAX(rows, 1));
  4008. xinit();
  4009. selinit();
  4010. run();
  4011. return 0;
  4012. }