Skip to content

Instantly share code, notes, and snippets.

View santileortiz's full-sized avatar

Santiago León santileortiz

  • Mexico
View GitHub Profile
@santileortiz
santileortiz / profiling_commands.txt
Last active October 16, 2018 01:26
Profiling tools
gprof
Prints a report containing time spent in each function. Also prints a report on the call graph by
telling for each function how many times was called and by whom, and also how many times it called
each other function.
1. Compile with flag -pg and optimizations.
2. Run executable, file gmon.out will be created.
3. Run $ gprof <path to binary>
@santileortiz
santileortiz / gist:698adbde7b1af1a1e3299886551aa864
Created January 24, 2018 07:40 — forked from ChickenProp/gist:3194723
The Liang-Barsky algorithm for line-rectangle collisions

The Liang-Barsky algorithm is a cheap way to find the intersection points between a line segment and an axis-aligned rectangle. It's a simple algorithm, but the resources I was pointed to didn't have particularly good explanations, so I tried to write a better one.

Consider a rectangle defined by x_min ≤ x ≤ x_max and y_min ≤ y ≤ y_max, and a line segment from (x_0, y_0) to (x_0 + Δ_x, y_0 + Δ_y). We'll be assuming at least one of Δ_x and Δ_y is nonzero.

Image depicting the situation

(I'm working with Flash, so I'll be using the convention that y increases as you go down.)

We want to distinguish between the following cases:

@santileortiz
santileortiz / gnome_shell_env
Created May 14, 2018 08:26
Script to setup a virtual terminal to be able to run a debug version of gnome-shell in Ubuntu
gnome_session=$(pgrep --full -u $USER gnome-session-binary.*ubuntu)
eval export $(sed 's/\o000/\n/g;' < /proc/$gnome_session/environ | grep DISPLAY)
eval export $(sed 's/\o000/\n/g;' < /proc/$gnome_session/environ | grep XAUTHORITY)
eval export $(sed 's/\o000/\n/g;' < /proc/$gnome_session/environ | grep XDG_SESSION_TYPE)
eval export $(sed 's/\o000/\n/g;' < /proc/$gnome_session/environ | grep DBUS_SESSION_BUS_ADDRESS)
Compiled libraries from Gtk's Git repositoriy will end up inside <gtk repo>/gtk/.libs/. To run an application with these
libraries use:
LD_LIBRARY_PATH=<git repo>/gtk/.libs/ <app>
I normally call it from inside the repository using
LD_LIBRARY_PATH=./gtk/.libs/ ~/<app path>
NOTE: Gtk moved from Autotools to Meson, maybe this will put resulting libraries somewhere else.
The syntax for setting up a third level key in a .xkb file seems to be vey
picky, I still need to explore more xkbcomp's source code to know how things
work.
I noticed this because recently the way I used to define the AltGr key in my
custom layout stopped working. I'm not sure which commit in which project
caused this to stop working, could have been libxkbcommon (xkbcomp), xserver,
Gdk among others. What I do know is that suddenly pressing AltGr would set
GDK_MOD1_MASK in the received key release event. This modifier is treated by
Gdk (Together with GDK_CONTROL_MASK) as one that will NEVER serve to translate
@santileortiz
santileortiz / xkb keymap testing
Created October 16, 2018 15:15
commands I use to study/debug keymaps in Linux
Current (unresolved) keymap file
================================
The following command prints one include statement for each component of the
current keymap:
$ setxkbmap -print
Note that this is not a resolved keymap file!. Quite the opposite, it shows
what will be passed to xkbcomp as components so that it resolves them to an
Information on how to create a custom layout
https://medium.com/@damko/a-simple-humble-but-comprehensive-guide-to-xkb-for-linux-6f1ad5e13450
https://github.com/damko/xkb_kinesis_advantage_dvorak_layout
Information about the RMLVO configuration format and syntax of the 'rules' file
http://who-t.blogspot.com/2008/09/rmlvo-keyboard-configuration.html
Very detailed description of how to configure XKB, it includes information about the syntax of .xkb files.
https://www.charvolant.org/doug/xkb/html/xkb.html
@santileortiz
santileortiz / gist:08724683e74270efd3a5ac22788b694f
Last active March 13, 2019 04:56
Unexpected behavior of GTree
When using GTree I made the mistake of keeping pointers to keys and overwriting
them. I expected this would only affect the overwritten keys, and make lookups
for them return NULL. What actually happened was that other keys not related to
these became unaccessible. I am puzzled as to why this happened, it may be
interesting to dig inside GTree's implementation and see what's going on.
My guess is doing this breaks the sorted invariant of the tree, then things
will go crazy next time it tries to balance it, maybe for some reason it tries
to fix it by replacing existing keys?. Or, maybe the result of the string
comparison function does not really check for equallity, and instead has an
@santileortiz
santileortiz / C_conditional_macro.c
Created March 29, 2020 19:47
Conditional macro based on number of passed arguments
// This is a way of executing a different macro based on the number of argument
// passed to a macro with variable arguments. Essentially this is a way of
// executing macros conditionally even though C doesn't support using #if inside
// a macro definition.
//
// Source: https://stackoverflow.com/questions/3046889/optional-parameters-with-c-macros
// The multiple macros that you would need anyway [as per: Crazy Eddie]
#define XXX_0() <code for no arguments>
#define XXX_1(A) <code for one argument>
@santileortiz
santileortiz / CountryCoodrinates
Last active April 4, 2020 04:23
Wikidata query that returns country coordinates
SELECT ?code ?countryLabel ?coordinates WHERE
{
?country wdt:P31 wd:Q3624078 ;
p:P625 ?location .
#not a former country
FILTER NOT EXISTS {?country wdt:P31 wd:Q3024240}
#and no an ancient civilisation (needed to exclude ancient Egypt)
FILTER NOT EXISTS {?country wdt:P31 wd:Q28171280}
# This doesn't bring all countries. It misses Armenia for example.