The crux of your problem statement deals with one of the most common wh-words, "when" which is most publicly used either to ask questions, as a conjunction or even to introduce relative clauses.
*Statement number two sounds more effective to me out of the two, though not correctly stated tense-wise, since while using when as a conjunction meaning ‘at the time that’, the clause attached with when is a subordinate clause (sc) and needs a main clause (mc) to complete its meaning.
So in our case, the correct tense usage can be attributed wholly towards the past tense wherein the contraction I'd is followed by a Past Participle (Past Participle of "to be : been") which is to say:
"I'd (read:had) been in the library when someone was sleeping in one of the chairs." or
If the when-clause comes before the main clause, we use a comma.
"When someone was sleeping in one of the chairs, I'd been in the library"
Some further helpful links to your rescue: