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.

4543 lines
98 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 *configured;
  3023. FcPattern *match;
  3024. FcResult result;
  3025. XGlyphInfo extents;
  3026. int wantattr, haveattr;
  3027. /*
  3028. * Manually configure instead of calling XftMatchFont
  3029. * so that we can use the configured pattern for
  3030. * "missing glyph" lookups.
  3031. */
  3032. configured = FcPatternDuplicate(pattern);
  3033. if (!configured)
  3034. return 1;
  3035. FcConfigSubstitute(NULL, configured, FcMatchPattern);
  3036. XftDefaultSubstitute(xw.dpy, xw.scr, configured);
  3037. match = FcFontMatch(NULL, configured, &result);
  3038. if (!match) {
  3039. FcPatternDestroy(configured);
  3040. return 1;
  3041. }
  3042. if (!(f->match = XftFontOpenPattern(xw.dpy, match))) {
  3043. FcPatternDestroy(configured);
  3044. FcPatternDestroy(match);
  3045. return 1;
  3046. }
  3047. if ((XftPatternGetInteger(pattern, "slant", 0, &wantattr) ==
  3048. XftResultMatch)) {
  3049. /*
  3050. * Check if xft was unable to find a font with the appropriate
  3051. * slant but gave us one anyway. Try to mitigate.
  3052. */
  3053. if ((XftPatternGetInteger(f->match->pattern, "slant", 0,
  3054. &haveattr) != XftResultMatch) || haveattr < wantattr) {
  3055. f->badslant = 1;
  3056. fputs("st: font slant does not match\n", stderr);
  3057. }
  3058. }
  3059. if ((XftPatternGetInteger(pattern, "weight", 0, &wantattr) ==
  3060. XftResultMatch)) {
  3061. if ((XftPatternGetInteger(f->match->pattern, "weight", 0,
  3062. &haveattr) != XftResultMatch) || haveattr != wantattr) {
  3063. f->badweight = 1;
  3064. fputs("st: font weight does not match\n", stderr);
  3065. }
  3066. }
  3067. XftTextExtentsUtf8(xw.dpy, f->match,
  3068. (const FcChar8 *) ascii_printable,
  3069. strlen(ascii_printable), &extents);
  3070. f->set = NULL;
  3071. f->pattern = configured;
  3072. f->ascent = f->match->ascent;
  3073. f->descent = f->match->descent;
  3074. f->lbearing = 0;
  3075. f->rbearing = f->match->max_advance_width;
  3076. f->height = f->ascent + f->descent;
  3077. f->width = DIVCEIL(extents.xOff, strlen(ascii_printable));
  3078. return 0;
  3079. }
  3080. void
  3081. xloadfonts(char *fontstr, double fontsize)
  3082. {
  3083. FcPattern *pattern;
  3084. double fontval;
  3085. float ceilf(float);
  3086. if (fontstr[0] == '-') {
  3087. pattern = XftXlfdParse(fontstr, False, False);
  3088. } else {
  3089. pattern = FcNameParse((FcChar8 *)fontstr);
  3090. }
  3091. if (!pattern)
  3092. die("st: can't open font %s\n", fontstr);
  3093. if (fontsize > 1) {
  3094. FcPatternDel(pattern, FC_PIXEL_SIZE);
  3095. FcPatternDel(pattern, FC_SIZE);
  3096. FcPatternAddDouble(pattern, FC_PIXEL_SIZE, (double)fontsize);
  3097. usedfontsize = fontsize;
  3098. } else {
  3099. if (FcPatternGetDouble(pattern, FC_PIXEL_SIZE, 0, &fontval) ==
  3100. FcResultMatch) {
  3101. usedfontsize = fontval;
  3102. } else if (FcPatternGetDouble(pattern, FC_SIZE, 0, &fontval) ==
  3103. FcResultMatch) {
  3104. usedfontsize = -1;
  3105. } else {
  3106. /*
  3107. * Default font size is 12, if none given. This is to
  3108. * have a known usedfontsize value.
  3109. */
  3110. FcPatternAddDouble(pattern, FC_PIXEL_SIZE, 12);
  3111. usedfontsize = 12;
  3112. }
  3113. defaultfontsize = usedfontsize;
  3114. }
  3115. if (xloadfont(&dc.font, pattern))
  3116. die("st: can't open font %s\n", fontstr);
  3117. if (usedfontsize < 0) {
  3118. FcPatternGetDouble(dc.font.match->pattern,
  3119. FC_PIXEL_SIZE, 0, &fontval);
  3120. FcPatternAddDouble(pattern, FC_PIXEL_SIZE, fontval);
  3121. if (xloadfont(&dc.font, pattern))
  3122. die("st: can't open font %s\n", fontstr);
  3123. usedfontsize = fontval;
  3124. if (fontsize == 0)
  3125. defaultfontsize = fontval;
  3126. }
  3127. /* Setting character width and height. */
  3128. xw.cw = ceilf(dc.font.width * cwscale);
  3129. xw.ch = ceilf(dc.font.height * chscale);
  3130. FcPatternDel(pattern, FC_SLANT);
  3131. FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ITALIC);
  3132. if (xloadfont(&dc.ifont, pattern))
  3133. die("st: can't open font %s\n", fontstr);
  3134. FcPatternDel(pattern, FC_WEIGHT);
  3135. FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_BOLD);
  3136. if (xloadfont(&dc.ibfont, pattern))
  3137. die("st: can't open font %s\n", fontstr);
  3138. FcPatternDel(pattern, FC_SLANT);
  3139. FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ROMAN);
  3140. if (xloadfont(&dc.bfont, pattern))
  3141. die("st: can't open font %s\n", fontstr);
  3142. FcPatternDestroy(pattern);
  3143. }
  3144. void
  3145. xunloadfont(Font *f)
  3146. {
  3147. XftFontClose(xw.dpy, f->match);
  3148. FcPatternDestroy(f->pattern);
  3149. if (f->set)
  3150. FcFontSetDestroy(f->set);
  3151. }
  3152. void
  3153. xunloadfonts(void)
  3154. {
  3155. /* Free the loaded fonts in the font cache. */
  3156. while (frclen > 0)
  3157. XftFontClose(xw.dpy, frc[--frclen].font);
  3158. xunloadfont(&dc.font);
  3159. xunloadfont(&dc.bfont);
  3160. xunloadfont(&dc.ifont);
  3161. xunloadfont(&dc.ibfont);
  3162. }
  3163. void
  3164. xzoom(const Arg *arg)
  3165. {
  3166. Arg larg;
  3167. larg.f = usedfontsize + arg->f;
  3168. xzoomabs(&larg);
  3169. }
  3170. void
  3171. xzoomabs(const Arg *arg)
  3172. {
  3173. xunloadfonts();
  3174. xloadfonts(usedfont, arg->f);
  3175. cresize(0, 0);
  3176. ttyresize();
  3177. redraw();
  3178. xhints();
  3179. }
  3180. void
  3181. xzoomreset(const Arg *arg)
  3182. {
  3183. Arg larg;
  3184. if (defaultfontsize > 0) {
  3185. larg.f = defaultfontsize;
  3186. xzoomabs(&larg);
  3187. }
  3188. }
  3189. void
  3190. xinit(void)
  3191. {
  3192. XGCValues gcvalues;
  3193. Cursor cursor;
  3194. Window parent;
  3195. pid_t thispid = getpid();
  3196. XColor xmousefg, xmousebg;
  3197. if (!(xw.dpy = XOpenDisplay(NULL)))
  3198. die("Can't open display\n");
  3199. xw.scr = XDefaultScreen(xw.dpy);
  3200. xw.vis = XDefaultVisual(xw.dpy, xw.scr);
  3201. /* font */
  3202. if (!FcInit())
  3203. die("Could not init fontconfig.\n");
  3204. usedfont = (opt_font == NULL)? font : opt_font;
  3205. xloadfonts(usedfont, 0);
  3206. /* colors */
  3207. xw.cmap = XDefaultColormap(xw.dpy, xw.scr);
  3208. xloadcols();
  3209. /* adjust fixed window geometry */
  3210. xw.w = 2 * borderpx + term.col * xw.cw;
  3211. xw.h = 2 * borderpx + term.row * xw.ch;
  3212. if (xw.gm & XNegative)
  3213. xw.l += DisplayWidth(xw.dpy, xw.scr) - xw.w - 2;
  3214. if (xw.gm & YNegative)
  3215. xw.t += DisplayHeight(xw.dpy, xw.scr) - xw.h - 2;
  3216. /* Events */
  3217. xw.attrs.background_pixel = dc.col[defaultbg].pixel;
  3218. xw.attrs.border_pixel = dc.col[defaultbg].pixel;
  3219. xw.attrs.bit_gravity = NorthWestGravity;
  3220. xw.attrs.event_mask = FocusChangeMask | KeyPressMask
  3221. | ExposureMask | VisibilityChangeMask | StructureNotifyMask
  3222. | ButtonMotionMask | ButtonPressMask | ButtonReleaseMask;
  3223. xw.attrs.colormap = xw.cmap;
  3224. if (!(opt_embed && (parent = strtol(opt_embed, NULL, 0))))
  3225. parent = XRootWindow(xw.dpy, xw.scr);
  3226. xw.win = XCreateWindow(xw.dpy, parent, xw.l, xw.t,
  3227. xw.w, xw.h, 0, XDefaultDepth(xw.dpy, xw.scr), InputOutput,
  3228. xw.vis, CWBackPixel | CWBorderPixel | CWBitGravity
  3229. | CWEventMask | CWColormap, &xw.attrs);
  3230. memset(&gcvalues, 0, sizeof(gcvalues));
  3231. gcvalues.graphics_exposures = False;
  3232. dc.gc = XCreateGC(xw.dpy, parent, GCGraphicsExposures,
  3233. &gcvalues);
  3234. xw.buf = XCreatePixmap(xw.dpy, xw.win, xw.w, xw.h,
  3235. DefaultDepth(xw.dpy, xw.scr));
  3236. XSetForeground(xw.dpy, dc.gc, dc.col[defaultbg].pixel);
  3237. XFillRectangle(xw.dpy, xw.buf, dc.gc, 0, 0, xw.w, xw.h);
  3238. /* Xft rendering context */
  3239. xw.draw = XftDrawCreate(xw.dpy, xw.buf, xw.vis, xw.cmap);
  3240. /* input methods */
  3241. if ((xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL)) == NULL) {
  3242. XSetLocaleModifiers("@im=local");
  3243. if ((xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL)) == NULL) {
  3244. XSetLocaleModifiers("@im=");
  3245. if ((xw.xim = XOpenIM(xw.dpy,
  3246. NULL, NULL, NULL)) == NULL) {
  3247. die("XOpenIM failed. Could not open input"
  3248. " device.\n");
  3249. }
  3250. }
  3251. }
  3252. xw.xic = XCreateIC(xw.xim, XNInputStyle, XIMPreeditNothing
  3253. | XIMStatusNothing, XNClientWindow, xw.win,
  3254. XNFocusWindow, xw.win, NULL);
  3255. if (xw.xic == NULL)
  3256. die("XCreateIC failed. Could not obtain input method.\n");
  3257. /* white cursor, black outline */
  3258. cursor = XCreateFontCursor(xw.dpy, mouseshape);
  3259. XDefineCursor(xw.dpy, xw.win, cursor);
  3260. if (XParseColor(xw.dpy, xw.cmap, colorname[mousefg], &xmousefg) == 0) {
  3261. xmousefg.red = 0xffff;
  3262. xmousefg.green = 0xffff;
  3263. xmousefg.blue = 0xffff;
  3264. }
  3265. if (XParseColor(xw.dpy, xw.cmap, colorname[mousebg], &xmousebg) == 0) {
  3266. xmousebg.red = 0x0000;
  3267. xmousebg.green = 0x0000;
  3268. xmousebg.blue = 0x0000;
  3269. }
  3270. XRecolorCursor(xw.dpy, cursor, &xmousefg, &xmousebg);
  3271. xw.xembed = XInternAtom(xw.dpy, "_XEMBED", False);
  3272. xw.wmdeletewin = XInternAtom(xw.dpy, "WM_DELETE_WINDOW", False);
  3273. xw.netwmname = XInternAtom(xw.dpy, "_NET_WM_NAME", False);
  3274. XSetWMProtocols(xw.dpy, xw.win, &xw.wmdeletewin, 1);
  3275. xw.netwmpid = XInternAtom(xw.dpy, "_NET_WM_PID", False);
  3276. XChangeProperty(xw.dpy, xw.win, xw.netwmpid, XA_CARDINAL, 32,
  3277. PropModeReplace, (uchar *)&thispid, 1);
  3278. xresettitle();
  3279. XMapWindow(xw.dpy, xw.win);
  3280. xhints();
  3281. XSync(xw.dpy, False);
  3282. }
  3283. int
  3284. xmakeglyphfontspecs(XftGlyphFontSpec *specs, const Glyph *glyphs, int len, int x, int y)
  3285. {
  3286. float winx = borderpx + x * xw.cw, winy = borderpx + y * xw.ch, xp, yp;
  3287. ushort mode, prevmode = USHRT_MAX;
  3288. Font *font = &dc.font;
  3289. int frcflags = FRC_NORMAL;
  3290. float runewidth = xw.cw;
  3291. Rune rune;
  3292. FT_UInt glyphidx;
  3293. FcResult fcres;
  3294. FcPattern *fcpattern, *fontpattern;
  3295. FcFontSet *fcsets[] = { NULL };
  3296. FcCharSet *fccharset;
  3297. int i, f, numspecs = 0;
  3298. for (i = 0, xp = winx, yp = winy + font->ascent; i < len; ++i) {
  3299. /* Fetch rune and mode for current glyph. */
  3300. rune = glyphs[i].u;
  3301. mode = glyphs[i].mode;
  3302. /* Skip dummy wide-character spacing. */
  3303. if (mode == ATTR_WDUMMY)
  3304. continue;
  3305. /* Determine font for glyph if different from previous glyph. */
  3306. if (prevmode != mode) {
  3307. prevmode = mode;
  3308. font = &dc.font;
  3309. frcflags = FRC_NORMAL;
  3310. runewidth = xw.cw * ((mode & ATTR_WIDE) ? 2.0f : 1.0f);
  3311. if ((mode & ATTR_ITALIC) && (mode & ATTR_BOLD)) {
  3312. font = &dc.ibfont;
  3313. frcflags = FRC_ITALICBOLD;
  3314. } else if (mode & ATTR_ITALIC) {
  3315. font = &dc.ifont;
  3316. frcflags = FRC_ITALIC;
  3317. } else if (mode & ATTR_BOLD) {
  3318. font = &dc.bfont;
  3319. frcflags = FRC_BOLD;
  3320. }
  3321. yp = winy + font->ascent;
  3322. }
  3323. /* Lookup character index with default font. */
  3324. glyphidx = XftCharIndex(xw.dpy, font->match, rune);
  3325. if (glyphidx) {
  3326. specs[numspecs].font = font->match;
  3327. specs[numspecs].glyph = glyphidx;
  3328. specs[numspecs].x = (short)xp;
  3329. specs[numspecs].y = (short)yp;
  3330. xp += runewidth;
  3331. numspecs++;
  3332. continue;
  3333. }
  3334. /* Fallback on font cache, search the font cache for match. */
  3335. for (f = 0; f < frclen; f++) {
  3336. glyphidx = XftCharIndex(xw.dpy, frc[f].font, rune);
  3337. /* Everything correct. */
  3338. if (glyphidx && frc[f].flags == frcflags)
  3339. break;
  3340. /* We got a default font for a not found glyph. */
  3341. if (!glyphidx && frc[f].flags == frcflags
  3342. && frc[f].unicodep == rune) {
  3343. break;
  3344. }
  3345. }
  3346. /* Nothing was found. Use fontconfig to find matching font. */
  3347. if (f >= frclen) {
  3348. if (!font->set)
  3349. font->set = FcFontSort(0, font->pattern,
  3350. 1, 0, &fcres);
  3351. fcsets[0] = font->set;
  3352. /*
  3353. * Nothing was found in the cache. Now use
  3354. * some dozen of Fontconfig calls to get the
  3355. * font for one single character.
  3356. *
  3357. * Xft and fontconfig are design failures.
  3358. */
  3359. fcpattern = FcPatternDuplicate(font->pattern);
  3360. fccharset = FcCharSetCreate();
  3361. FcCharSetAddChar(fccharset, rune);
  3362. FcPatternAddCharSet(fcpattern, FC_CHARSET,
  3363. fccharset);
  3364. FcPatternAddBool(fcpattern, FC_SCALABLE, 1);
  3365. FcConfigSubstitute(0, fcpattern,
  3366. FcMatchPattern);
  3367. FcDefaultSubstitute(fcpattern);
  3368. fontpattern = FcFontSetMatch(0, fcsets, 1,
  3369. fcpattern, &fcres);
  3370. /*
  3371. * Overwrite or create the new cache entry.
  3372. */
  3373. if (frclen >= LEN(frc)) {
  3374. frclen = LEN(frc) - 1;
  3375. XftFontClose(xw.dpy, frc[frclen].font);
  3376. frc[frclen].unicodep = 0;
  3377. }
  3378. frc[frclen].font = XftFontOpenPattern(xw.dpy,
  3379. fontpattern);
  3380. frc[frclen].flags = frcflags;
  3381. frc[frclen].unicodep = rune;
  3382. glyphidx = XftCharIndex(xw.dpy, frc[frclen].font, rune);
  3383. f = frclen;
  3384. frclen++;
  3385. FcPatternDestroy(fcpattern);
  3386. FcCharSetDestroy(fccharset);
  3387. }
  3388. specs[numspecs].font = frc[f].font;
  3389. specs[numspecs].glyph = glyphidx;
  3390. specs[numspecs].x = (short)xp;
  3391. specs[numspecs].y = (short)yp;
  3392. xp += runewidth;
  3393. numspecs++;
  3394. }
  3395. return numspecs;
  3396. }
  3397. void
  3398. xdrawglyphfontspecs(const XftGlyphFontSpec *specs, Glyph base, int len, int x, int y)
  3399. {
  3400. int charlen = len * ((base.mode & ATTR_WIDE) ? 2 : 1);
  3401. int winx = borderpx + x * xw.cw, winy = borderpx + y * xw.ch,
  3402. width = charlen * xw.cw;
  3403. Color *fg, *bg, *temp, revfg, revbg, truefg, truebg;
  3404. XRenderColor colfg, colbg;
  3405. XRectangle r;
  3406. /* Fallback on color display for attributes not supported by the font */
  3407. if (base.mode & ATTR_ITALIC && base.mode & ATTR_BOLD) {
  3408. if (dc.ibfont.badslant || dc.ibfont.badweight)
  3409. base.fg = defaultattr;
  3410. } else if ((base.mode & ATTR_ITALIC && dc.ifont.badslant) ||
  3411. (base.mode & ATTR_BOLD && dc.bfont.badweight)) {
  3412. base.fg = defaultattr;
  3413. }
  3414. if (IS_TRUECOL(base.fg)) {
  3415. colfg.alpha = 0xffff;
  3416. colfg.red = TRUERED(base.fg);
  3417. colfg.green = TRUEGREEN(base.fg);
  3418. colfg.blue = TRUEBLUE(base.fg);
  3419. XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg, &truefg);
  3420. fg = &truefg;
  3421. } else {
  3422. fg = &dc.col[base.fg];
  3423. }
  3424. if (IS_TRUECOL(base.bg)) {
  3425. colbg.alpha = 0xffff;
  3426. colbg.green = TRUEGREEN(base.bg);
  3427. colbg.red = TRUERED(base.bg);
  3428. colbg.blue = TRUEBLUE(base.bg);
  3429. XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colbg, &truebg);
  3430. bg = &truebg;
  3431. } else {
  3432. bg = &dc.col[base.bg];
  3433. }
  3434. /* Change basic system colors [0-7] to bright system colors [8-15] */
  3435. if ((base.mode & ATTR_BOLD_FAINT) == ATTR_BOLD && BETWEEN(base.fg, 0, 7))
  3436. fg = &dc.col[base.fg + 8];
  3437. if (IS_SET(MODE_REVERSE)) {
  3438. if (fg == &dc.col[defaultfg]) {
  3439. fg = &dc.col[defaultbg];
  3440. } else {
  3441. colfg.red = ~fg->color.red;
  3442. colfg.green = ~fg->color.green;
  3443. colfg.blue = ~fg->color.blue;
  3444. colfg.alpha = fg->color.alpha;
  3445. XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg,
  3446. &revfg);
  3447. fg = &revfg;
  3448. }
  3449. if (bg == &dc.col[defaultbg]) {
  3450. bg = &dc.col[defaultfg];
  3451. } else {
  3452. colbg.red = ~bg->color.red;
  3453. colbg.green = ~bg->color.green;
  3454. colbg.blue = ~bg->color.blue;
  3455. colbg.alpha = bg->color.alpha;
  3456. XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colbg,
  3457. &revbg);
  3458. bg = &revbg;
  3459. }
  3460. }
  3461. if (base.mode & ATTR_REVERSE) {
  3462. temp = fg;
  3463. fg = bg;
  3464. bg = temp;
  3465. }
  3466. if ((base.mode & ATTR_BOLD_FAINT) == ATTR_FAINT) {
  3467. colfg.red = fg->color.red / 2;
  3468. colfg.green = fg->color.green / 2;
  3469. colfg.blue = fg->color.blue / 2;
  3470. XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg, &revfg);
  3471. fg = &revfg;
  3472. }
  3473. if (base.mode & ATTR_BLINK && term.mode & MODE_BLINK)
  3474. fg = bg;
  3475. if (base.mode & ATTR_INVISIBLE)
  3476. fg = bg;
  3477. /* Intelligent cleaning up of the borders. */
  3478. if (x == 0) {
  3479. xclear(0, (y == 0)? 0 : winy, borderpx,
  3480. winy + xw.ch + ((y >= term.row-1)? xw.h : 0));
  3481. }
  3482. if (x + charlen >= term.col) {
  3483. xclear(winx + width, (y == 0)? 0 : winy, xw.w,
  3484. ((y >= term.row-1)? xw.h : (winy + xw.ch)));
  3485. }
  3486. if (y == 0)
  3487. xclear(winx, 0, winx + width, borderpx);
  3488. if (y == term.row-1)
  3489. xclear(winx, winy + xw.ch, winx + width, xw.h);
  3490. /* Clean up the region we want to draw to. */
  3491. XftDrawRect(xw.draw, bg, winx, winy, width, xw.ch);
  3492. /* Set the clip region because Xft is sometimes dirty. */
  3493. r.x = 0;
  3494. r.y = 0;
  3495. r.height = xw.ch;
  3496. r.width = width;
  3497. XftDrawSetClipRectangles(xw.draw, winx, winy, &r, 1);
  3498. /* Render the glyphs. */
  3499. XftDrawGlyphFontSpec(xw.draw, fg, specs, len);
  3500. /* Render underline and strikethrough. */
  3501. if (base.mode & ATTR_UNDERLINE) {
  3502. XftDrawRect(xw.draw, fg, winx, winy + dc.font.ascent + 1,
  3503. width, 1);
  3504. }
  3505. if (base.mode & ATTR_STRUCK) {
  3506. XftDrawRect(xw.draw, fg, winx, winy + 2 * dc.font.ascent / 3,
  3507. width, 1);
  3508. }
  3509. /* Reset clip to none. */
  3510. XftDrawSetClip(xw.draw, 0);
  3511. }
  3512. void
  3513. xdrawglyph(Glyph g, int x, int y)
  3514. {
  3515. int numspecs;
  3516. XftGlyphFontSpec spec;
  3517. numspecs = xmakeglyphfontspecs(&spec, &g, 1, x, y);
  3518. xdrawglyphfontspecs(&spec, g, numspecs, x, y);
  3519. }
  3520. void
  3521. xdrawcursor(void)
  3522. {
  3523. static int oldx = 0, oldy = 0;
  3524. int curx;
  3525. Glyph g = {' ', ATTR_NULL, defaultbg, defaultcs}, og;
  3526. int ena_sel = sel.ob.x != -1 && sel.alt == IS_SET(MODE_ALTSCREEN);
  3527. Color drawcol;
  3528. LIMIT(oldx, 0, term.col-1);
  3529. LIMIT(oldy, 0, term.row-1);
  3530. curx = term.c.x;
  3531. /* adjust position if in dummy */
  3532. if (term.line[oldy][oldx].mode & ATTR_WDUMMY)
  3533. oldx--;
  3534. if (term.line[term.c.y][curx].mode & ATTR_WDUMMY)
  3535. curx--;
  3536. /* remove the old cursor */
  3537. og = term.line[oldy][oldx];
  3538. if (ena_sel && selected(oldx, oldy))
  3539. og.mode ^= ATTR_REVERSE;
  3540. xdrawglyph(og, oldx, oldy);
  3541. g.u = term.line[term.c.y][term.c.x].u;
  3542. /*
  3543. * Select the right color for the right mode.
  3544. */
  3545. if (IS_SET(MODE_REVERSE)) {
  3546. g.mode |= ATTR_REVERSE;
  3547. g.bg = defaultfg;
  3548. if (ena_sel && selected(term.c.x, term.c.y)) {
  3549. drawcol = dc.col[defaultcs];
  3550. g.fg = defaultrcs;
  3551. } else {
  3552. drawcol = dc.col[defaultrcs];
  3553. g.fg = defaultcs;
  3554. }
  3555. } else {
  3556. if (ena_sel && selected(term.c.x, term.c.y)) {
  3557. drawcol = dc.col[defaultrcs];
  3558. g.fg = defaultfg;
  3559. g.bg = defaultrcs;
  3560. } else {
  3561. drawcol = dc.col[defaultcs];
  3562. }
  3563. }
  3564. if (IS_SET(MODE_HIDE))
  3565. return;
  3566. /* draw the new one */
  3567. if (xw.state & WIN_FOCUSED) {
  3568. switch (xw.cursor) {
  3569. case 7: /* st extension: snowman */
  3570. utf8decode("", &g.u, UTF_SIZ);
  3571. case 0: /* Blinking Block */
  3572. case 1: /* Blinking Block (Default) */
  3573. case 2: /* Steady Block */
  3574. g.mode |= term.line[term.c.y][curx].mode & ATTR_WIDE;
  3575. xdrawglyph(g, term.c.x, term.c.y);
  3576. break;
  3577. case 3: /* Blinking Underline */
  3578. case 4: /* Steady Underline */
  3579. XftDrawRect(xw.draw, &drawcol,
  3580. borderpx + curx * xw.cw,
  3581. borderpx + (term.c.y + 1) * xw.ch - \
  3582. cursorthickness,
  3583. xw.cw, cursorthickness);
  3584. break;
  3585. case 5: /* Blinking bar */
  3586. case 6: /* Steady bar */
  3587. XftDrawRect(xw.draw, &drawcol,
  3588. borderpx + curx * xw.cw,
  3589. borderpx + term.c.y * xw.ch,
  3590. cursorthickness, xw.ch);
  3591. break;
  3592. }
  3593. } else {
  3594. XftDrawRect(xw.draw, &drawcol,
  3595. borderpx + curx * xw.cw,
  3596. borderpx + term.c.y * xw.ch,
  3597. xw.cw - 1, 1);
  3598. XftDrawRect(xw.draw, &drawcol,
  3599. borderpx + curx * xw.cw,
  3600. borderpx + term.c.y * xw.ch,
  3601. 1, xw.ch - 1);
  3602. XftDrawRect(xw.draw, &drawcol,
  3603. borderpx + (curx + 1) * xw.cw - 1,
  3604. borderpx + term.c.y * xw.ch,
  3605. 1, xw.ch - 1);
  3606. XftDrawRect(xw.draw, &drawcol,
  3607. borderpx + curx * xw.cw,
  3608. borderpx + (term.c.y + 1) * xw.ch - 1,
  3609. xw.cw, 1);
  3610. }
  3611. oldx = curx, oldy = term.c.y;
  3612. }
  3613. void
  3614. xsettitle(char *p)
  3615. {
  3616. XTextProperty prop;
  3617. Xutf8TextListToTextProperty(xw.dpy, &p, 1, XUTF8StringStyle,
  3618. &prop);
  3619. XSetWMName(xw.dpy, xw.win, &prop);
  3620. XSetTextProperty(xw.dpy, xw.win, &prop, xw.netwmname);
  3621. XFree(prop.value);
  3622. }
  3623. void
  3624. xresettitle(void)
  3625. {
  3626. xsettitle(opt_title ? opt_title : "st");
  3627. }
  3628. void
  3629. redraw(void)
  3630. {
  3631. tfulldirt();
  3632. draw();
  3633. }
  3634. void
  3635. draw(void)
  3636. {
  3637. drawregion(0, 0, term.col, term.row);
  3638. XCopyArea(xw.dpy, xw.buf, xw.win, dc.gc, 0, 0, xw.w,
  3639. xw.h, 0, 0);
  3640. XSetForeground(xw.dpy, dc.gc,
  3641. dc.col[IS_SET(MODE_REVERSE)?
  3642. defaultfg : defaultbg].pixel);
  3643. }
  3644. void
  3645. drawregion(int x1, int y1, int x2, int y2)
  3646. {
  3647. int i, x, y, ox, numspecs;
  3648. Glyph base, new;
  3649. XftGlyphFontSpec *specs;
  3650. int ena_sel = sel.ob.x != -1 && sel.alt == IS_SET(MODE_ALTSCREEN);
  3651. if (!(xw.state & WIN_VISIBLE))
  3652. return;
  3653. for (y = y1; y < y2; y++) {
  3654. if (!term.dirty[y])
  3655. continue;
  3656. term.dirty[y] = 0;
  3657. specs = term.specbuf;
  3658. numspecs = xmakeglyphfontspecs(specs, &term.line[y][x1], x2 - x1, x1, y);
  3659. i = ox = 0;
  3660. for (x = x1; x < x2 && i < numspecs; x++) {
  3661. new = term.line[y][x];
  3662. if (new.mode == ATTR_WDUMMY)
  3663. continue;
  3664. if (ena_sel && selected(x, y))
  3665. new.mode ^= ATTR_REVERSE;
  3666. if (i > 0 && ATTRCMP(base, new)) {
  3667. xdrawglyphfontspecs(specs, base, i, ox, y);
  3668. specs += i;
  3669. numspecs -= i;
  3670. i = 0;
  3671. }
  3672. if (i == 0) {
  3673. ox = x;
  3674. base = new;
  3675. }
  3676. i++;
  3677. }
  3678. if (i > 0)
  3679. xdrawglyphfontspecs(specs, base, i, ox, y);
  3680. }
  3681. xdrawcursor();
  3682. }
  3683. void
  3684. expose(XEvent *ev)
  3685. {
  3686. redraw();
  3687. }
  3688. void
  3689. visibility(XEvent *ev)
  3690. {
  3691. XVisibilityEvent *e = &ev->xvisibility;
  3692. MODBIT(xw.state, e->state != VisibilityFullyObscured, WIN_VISIBLE);
  3693. }
  3694. void
  3695. unmap(XEvent *ev)
  3696. {
  3697. xw.state &= ~WIN_VISIBLE;
  3698. }
  3699. void
  3700. xsetpointermotion(int set)
  3701. {
  3702. MODBIT(xw.attrs.event_mask, set, PointerMotionMask);
  3703. XChangeWindowAttributes(xw.dpy, xw.win, CWEventMask, &xw.attrs);
  3704. }
  3705. void
  3706. xseturgency(int add)
  3707. {
  3708. XWMHints *h = XGetWMHints(xw.dpy, xw.win);
  3709. MODBIT(h->flags, add, XUrgencyHint);
  3710. XSetWMHints(xw.dpy, xw.win, h);
  3711. XFree(h);
  3712. }
  3713. void
  3714. focus(XEvent *ev)
  3715. {
  3716. XFocusChangeEvent *e = &ev->xfocus;
  3717. if (e->mode == NotifyGrab)
  3718. return;
  3719. if (ev->type == FocusIn) {
  3720. XSetICFocus(xw.xic);
  3721. xw.state |= WIN_FOCUSED;
  3722. xseturgency(0);
  3723. if (IS_SET(MODE_FOCUS))
  3724. ttywrite("\033[I", 3);
  3725. } else {
  3726. XUnsetICFocus(xw.xic);
  3727. xw.state &= ~WIN_FOCUSED;
  3728. if (IS_SET(MODE_FOCUS))
  3729. ttywrite("\033[O", 3);
  3730. }
  3731. }
  3732. int
  3733. match(uint mask, uint state)
  3734. {
  3735. return mask == XK_ANY_MOD || mask == (state & ~ignoremod);
  3736. }
  3737. void
  3738. numlock(const Arg *dummy)
  3739. {
  3740. term.numlock ^= 1;
  3741. }
  3742. char*
  3743. kmap(KeySym k, uint state)
  3744. {
  3745. Key *kp;
  3746. int i;
  3747. /* Check for mapped keys out of X11 function keys. */
  3748. for (i = 0; i < LEN(mappedkeys); i++) {
  3749. if (mappedkeys[i] == k)
  3750. break;
  3751. }
  3752. if (i == LEN(mappedkeys)) {
  3753. if ((k & 0xFFFF) < 0xFD00)
  3754. return NULL;
  3755. }
  3756. for (kp = key; kp < key + LEN(key); kp++) {
  3757. if (kp->k != k)
  3758. continue;
  3759. if (!match(kp->mask, state))
  3760. continue;
  3761. if (IS_SET(MODE_APPKEYPAD) ? kp->appkey < 0 : kp->appkey > 0)
  3762. continue;
  3763. if (term.numlock && kp->appkey == 2)
  3764. continue;
  3765. if (IS_SET(MODE_APPCURSOR) ? kp->appcursor < 0 : kp->appcursor > 0)
  3766. continue;
  3767. if (IS_SET(MODE_CRLF) ? kp->crlf < 0 : kp->crlf > 0)
  3768. continue;
  3769. return kp->s;
  3770. }
  3771. return NULL;
  3772. }
  3773. void
  3774. kpress(XEvent *ev)
  3775. {
  3776. XKeyEvent *e = &ev->xkey;
  3777. KeySym ksym;
  3778. char buf[32], *customkey;
  3779. int len;
  3780. Rune c;
  3781. Status status;
  3782. Shortcut *bp;
  3783. if (IS_SET(MODE_KBDLOCK))
  3784. return;
  3785. len = XmbLookupString(xw.xic, e, buf, sizeof buf, &ksym, &status);
  3786. /* 1. shortcuts */
  3787. for (bp = shortcuts; bp < shortcuts + LEN(shortcuts); bp++) {
  3788. if (ksym == bp->keysym && match(bp->mod, e->state)) {
  3789. bp->func(&(bp->arg));
  3790. return;
  3791. }
  3792. }
  3793. /* 2. custom keys from config.h */
  3794. if ((customkey = kmap(ksym, e->state))) {
  3795. ttysend(customkey, strlen(customkey));
  3796. return;
  3797. }
  3798. /* 3. composed string from input method */
  3799. if (len == 0)
  3800. return;
  3801. if (len == 1 && e->state & Mod1Mask) {
  3802. if (IS_SET(MODE_8BIT)) {
  3803. if (*buf < 0177) {
  3804. c = *buf | 0x80;
  3805. len = utf8encode(c, buf);
  3806. }
  3807. } else {
  3808. buf[1] = buf[0];
  3809. buf[0] = '\033';
  3810. len = 2;
  3811. }
  3812. }
  3813. ttysend(buf, len);
  3814. }
  3815. void
  3816. cmessage(XEvent *e)
  3817. {
  3818. /*
  3819. * See xembed specs
  3820. * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
  3821. */
  3822. if (e->xclient.message_type == xw.xembed && e->xclient.format == 32) {
  3823. if (e->xclient.data.l[1] == XEMBED_FOCUS_IN) {
  3824. xw.state |= WIN_FOCUSED;
  3825. xseturgency(0);
  3826. } else if (e->xclient.data.l[1] == XEMBED_FOCUS_OUT) {
  3827. xw.state &= ~WIN_FOCUSED;
  3828. }
  3829. } else if (e->xclient.data.l[0] == xw.wmdeletewin) {
  3830. /* Send SIGHUP to shell */
  3831. kill(pid, SIGHUP);
  3832. exit(0);
  3833. }
  3834. }
  3835. void
  3836. cresize(int width, int height)
  3837. {
  3838. int col, row;
  3839. if (width != 0)
  3840. xw.w = width;
  3841. if (height != 0)
  3842. xw.h = height;
  3843. col = (xw.w - 2 * borderpx) / xw.cw;
  3844. row = (xw.h - 2 * borderpx) / xw.ch;
  3845. tresize(col, row);
  3846. xresize(col, row);
  3847. }
  3848. void
  3849. resize(XEvent *e)
  3850. {
  3851. if (e->xconfigure.width == xw.w && e->xconfigure.height == xw.h)
  3852. return;
  3853. cresize(e->xconfigure.width, e->xconfigure.height);
  3854. ttyresize();
  3855. }
  3856. void
  3857. run(void)
  3858. {
  3859. XEvent ev;
  3860. int w = xw.w, h = xw.h;
  3861. fd_set rfd;
  3862. int xfd = XConnectionNumber(xw.dpy), xev, blinkset = 0, dodraw = 0;
  3863. struct timespec drawtimeout, *tv = NULL, now, last, lastblink;
  3864. long deltatime;
  3865. /* Waiting for window mapping */
  3866. do {
  3867. XNextEvent(xw.dpy, &ev);
  3868. /*
  3869. * This XFilterEvent call is required because of XOpenIM. It
  3870. * does filter out the key event and some client message for
  3871. * the input method too.
  3872. */
  3873. if (XFilterEvent(&ev, None))
  3874. continue;
  3875. if (ev.type == ConfigureNotify) {
  3876. w = ev.xconfigure.width;
  3877. h = ev.xconfigure.height;
  3878. }
  3879. } while (ev.type != MapNotify);
  3880. cresize(w, h);
  3881. ttynew();
  3882. ttyresize();
  3883. clock_gettime(CLOCK_MONOTONIC, &last);
  3884. lastblink = last;
  3885. for (xev = actionfps;;) {
  3886. FD_ZERO(&rfd);
  3887. FD_SET(cmdfd, &rfd);
  3888. FD_SET(xfd, &rfd);
  3889. if (pselect(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, tv, NULL) < 0) {
  3890. if (errno == EINTR)
  3891. continue;
  3892. die("select failed: %s\n", strerror(errno));
  3893. }
  3894. if (FD_ISSET(cmdfd, &rfd)) {
  3895. ttyread();
  3896. if (blinktimeout) {
  3897. blinkset = tattrset(ATTR_BLINK);
  3898. if (!blinkset)
  3899. MODBIT(term.mode, 0, MODE_BLINK);
  3900. }
  3901. }
  3902. if (FD_ISSET(xfd, &rfd))
  3903. xev = actionfps;
  3904. clock_gettime(CLOCK_MONOTONIC, &now);
  3905. drawtimeout.tv_sec = 0;
  3906. drawtimeout.tv_nsec = (1000 * 1E6)/ xfps;
  3907. tv = &drawtimeout;
  3908. dodraw = 0;
  3909. if (blinktimeout && TIMEDIFF(now, lastblink) > blinktimeout) {
  3910. tsetdirtattr(ATTR_BLINK);
  3911. term.mode ^= MODE_BLINK;
  3912. lastblink = now;
  3913. dodraw = 1;
  3914. }
  3915. deltatime = TIMEDIFF(now, last);
  3916. if (deltatime > 1000 / (xev ? xfps : actionfps)) {
  3917. dodraw = 1;
  3918. last = now;
  3919. }
  3920. if (dodraw) {
  3921. while (XPending(xw.dpy)) {
  3922. XNextEvent(xw.dpy, &ev);
  3923. if (XFilterEvent(&ev, None))
  3924. continue;
  3925. if (handler[ev.type])
  3926. (handler[ev.type])(&ev);
  3927. }
  3928. draw();
  3929. XFlush(xw.dpy);
  3930. if (xev && !FD_ISSET(xfd, &rfd))
  3931. xev--;
  3932. if (!FD_ISSET(cmdfd, &rfd) && !FD_ISSET(xfd, &rfd)) {
  3933. if (blinkset) {
  3934. if (TIMEDIFF(now, lastblink) \
  3935. > blinktimeout) {
  3936. drawtimeout.tv_nsec = 1000;
  3937. } else {
  3938. drawtimeout.tv_nsec = (1E6 * \
  3939. (blinktimeout - \
  3940. TIMEDIFF(now,
  3941. lastblink)));
  3942. }
  3943. drawtimeout.tv_sec = \
  3944. drawtimeout.tv_nsec / 1E9;
  3945. drawtimeout.tv_nsec %= (long)1E9;
  3946. } else {
  3947. tv = NULL;
  3948. }
  3949. }
  3950. }
  3951. }
  3952. }
  3953. void
  3954. usage(void)
  3955. {
  3956. die("usage: %s [-aiv] [-c class] [-f font] [-g geometry]"
  3957. " [-n name] [-o file]\n"
  3958. " [-T title] [-t title] [-w windowid]"
  3959. " [[-e] command [args ...]]\n"
  3960. " %s [-aiv] [-c class] [-f font] [-g geometry]"
  3961. " [-n name] [-o file]\n"
  3962. " [-T title] [-t title] [-w windowid] -l line"
  3963. " [stty_args ...]\n", argv0, argv0);
  3964. }
  3965. int
  3966. main(int argc, char *argv[])
  3967. {
  3968. uint cols = 80, rows = 24;
  3969. xw.l = xw.t = 0;
  3970. xw.isfixed = False;
  3971. xw.cursor = cursorshape;
  3972. ARGBEGIN {
  3973. case 'a':
  3974. allowaltscreen = 0;
  3975. break;
  3976. case 'c':
  3977. opt_class = EARGF(usage());
  3978. break;
  3979. case 'e':
  3980. if (argc > 0)
  3981. --argc, ++argv;
  3982. goto run;
  3983. case 'f':
  3984. opt_font = EARGF(usage());
  3985. break;
  3986. case 'g':
  3987. xw.gm = XParseGeometry(EARGF(usage()),
  3988. &xw.l, &xw.t, &cols, &rows);
  3989. break;
  3990. case 'i':
  3991. xw.isfixed = 1;
  3992. break;
  3993. case 'o':
  3994. opt_io = EARGF(usage());
  3995. break;
  3996. case 'l':
  3997. opt_line = EARGF(usage());
  3998. break;
  3999. case 'n':
  4000. opt_name = EARGF(usage());
  4001. break;
  4002. case 't':
  4003. case 'T':
  4004. opt_title = EARGF(usage());
  4005. break;
  4006. case 'w':
  4007. opt_embed = EARGF(usage());
  4008. break;
  4009. case 'v':
  4010. die("%s " VERSION " (c) 2010-2016 st engineers\n", argv0);
  4011. break;
  4012. default:
  4013. usage();
  4014. } ARGEND;
  4015. run:
  4016. if (argc > 0) {
  4017. /* eat all remaining arguments */
  4018. opt_cmd = argv;
  4019. if (!opt_title && !opt_line)
  4020. opt_title = basename(xstrdup(argv[0]));
  4021. }
  4022. setlocale(LC_CTYPE, "");
  4023. XSetLocaleModifiers("");
  4024. tnew(MAX(cols, 1), MAX(rows, 1));
  4025. xinit();
  4026. selinit();
  4027. run();
  4028. return 0;
  4029. }