\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[a4paper, margin=0.75in]{geometry}

% set up hyperlink coloring
\usepackage{hyperref}
\hypersetup{
    colorlinks=true,
    linkcolor=blue,
    filecolor=magenta,
    urlcolor=cyan
}

\setlength{\parindent}{0pt} % no indentation

% checkbox list: https://tex.stackexchange.com/questions/247681/how-to-create-checkbox-todo-list
\usepackage{enumitem,amssymb}
\newlist{boxlist}{itemize}{2}
\setlist[boxlist]{label=$\square$}

% code support: listings and fancy verbatim
\usepackage{listings}
\lstset{basicstyle=\ttfamily\footnotesize}
\usepackage{fancyvrb}

\renewcommand{\familydefault}{\sfdefault} % default to sans serif

\usepackage{xcolor}
\newcommand{\code}[1]{\colorbox{lightgray}{\texttt{#1}}}
\newcommand{\sh}[1]{\code{\$ #1}}

%%%%%%%%%%%%%%%%
\begin{document}

\title{Advanced Exercises Set 10\\Libraries}
\author{EECS 201 Winter 2020 COVID-19 Edition}
\date{}
\maketitle

\section*{Submission Instructions}
To receive credit for this assignment you will need to email
the staff at \href{mailto:eecs201-staff@eecs.umich.edu}{eecs201-staff@eecs.umich.edu}
with \texttt{tar} archive(s) of your submissions attached as well as any additional
information requested in the body of your email.
You may choose the exercises that you wish to do from this set.
Each exercise is denoted by its point count.
\textbf{Extra credit is given for early turn-ins of advanced exercises.
        These details can be found on the website under the advanced homework grading policy.
        For these email-based submissions, your latest submission of the \texttt{tar}
        archive/questions will determine your extra credit.}

\section*{Preface}
\textbf{For these exercises you're going to need to be in a Linux environment,
be it WSL or your Ubuntu VM or whatever else you have.}
\\\\
There are also some provided example files.
\begin{Verbatim}[fontsize=\footnotesize]
  curl -O https://www.eecs.umich.edu/courses/eecs201/files/assignments/adv10-files.tar.gz
  tar xzf adv10-files.tar.gz
\end{Verbatim}

\section{Let's have some fun: ncurses (5)}
ncurses is a library that provides an abstraction layer
to make it easier to build user interfaces in a terminal.
It's used in many different applications that do more with
the terminal than print lines, such as text editors
(e.g. Vim, Emacs) or games (NetHack).
\\\\
For this exercise you'll be playing around with linking against
a library and learning how to use it by making a C/C++ application
that makes use of ncurses.
\\\\
What I want you to do is write a C/C++ application that 
draws a character (your choice) on the terminal,
which can be moved with vi/Vim style navigation (h/j/k/l for left/down/up/right)
and quits when 'q' is pressed.
In addition, create a Makefile that builds the application.
Feel free to add onto it, or, if you want to do something else,
ask the staff for permission.
\\\\
The package for ncurses on Ubuntu is \code{libncurses5-dev}.
\\\\
A nice resource is the \href{http://www.tldp.org/HOWTO/NCURSES-Programming-HOWTO/index.html}{HOWTO} on it.
\begin{itemize}
  \item[$\square$] In your tarball include the source code for your application
                   and a Makefile to build it.
  \item[$\square$] In the body of your email let me know if you've used ncurses before
                   and your general thoughts about this exercise.
\end{itemize}


\section{Let's have some fun: SDL2 (10)}
SDL2 (Simple DirectMedia Layer) is a library that offers cross-platform
abstractions for video-game and other media related hardware such as keyboard, audio, and graphics.
The package for SDL2 on Ubuntu is \code{libsdl2-dev}.
\\\\
In a similar manner to \#1, you'll be playing around with SDL2 to make a C/C++ application.
\\\\
Following the theme for \#1, I want you to write a C/C++ application that uses SDL2
that draws a rectangle (your choice of color) that moves around using the arrow keys.
\\\\
\href{https://wiki.libsdl.org/FrontPage}{Here} is the official reference for SDL2.
\href{http://lazyfoo.net/tutorials/SDL/index.php}{Here} is a tutorial for SDL2.
Particularly relevant for this exercises are the Event Driven Programming, Key Presses,
and the Geometry Rendering lessons.
\begin{itemize}
  \item[$\square$] Include this report in the body of your email.
  \item[$\square$] In the body of your email let me know if you've used SDL2 before
                   and your general thoughts about this exercise.
\end{itemize}

\section{Runtime library linking (10)}
A closely related topic to the idea of dynamic/shared libraries
is the idea of dynamic linking at \textit{runtime}.

Normally when you run a program, the dynamic libraries that its linked
against would be loaded at program \textit{load time}.

While doing it at load time more dynamic than say, static linking,
you can be even more dynamic by loading libraries at runtime!

On POSIX systems we have the \code{dl} library that provides
the ability to run the dynamic linker to load libraries at runtime
{\footnotesize{}(on Windows there's a similar function called \code{LoadLibrary})}.

With the ability to load code at runtime, we can implement the idea of plugins!
In this exercise, we'll be filling out the framework for a plugin system.

Files are provided in the archive under \code{plugin}.
How this is structured is that \code{runner.c} is compiled into \code{runner}
and serves as the ``driver'' program that loads plugins.
Plugins are merely dynamic shared objects: \code{*.so} files.
There's a Makefile target that turns \code{plugin*.c} files into \code{plugin*.so} files.
The plugins for this application follow a standard where they must implement three
functions: \code{init()}, \code{run()}, and \code{cleanup()}.
\\\\
Your job is to utilize \code{dlopen}, \code{dlsym}, and \code{dlclose} (they have manpages!),
as well as a bit of function pointers, to implement the TODOs in the \code{runner.c} code.
\\\\
If you have not taken EECS 370, you may be unaware of what a ``symbol'' is.
A ``symbol'' is an identifier for some resource, such as a global variable or a function.
In the context of binary executables and libraries, these symbols are associated with
an address in memory where the resource is.
For instance, the symbol ``init'' for one of the plugins may resolve to address
\code{0x4100} and the symbol ``cleanup'' may resolve to \code{0x4200}.
By getting symbols from a library you're effectively locating where the resource
is so you can use it (via the magic of pointers!).

\begin{itemize}
  \item[$\square$] Include your modified \code{runner.c} in your tarball.
  \item[$\square$] In your email, mention if you have taken EECS 370 before and if
                   you knew what symbols were before doing this exercise.
\end{itemize}

\end{document}
